ExpendController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\expend\classes\ExpendBxClass;
  4. use bizGhs\expend\classes\ExpendClass;
  5. use bizGhs\shop\classes\MainClass;
  6. use bizGhs\shop\classes\ShopAdminClass;
  7. use bizGhs\shop\classes\ShopMoneyClass;
  8. use common\components\dateUtil;
  9. use common\components\dict;
  10. use common\components\noticeUtil;
  11. use common\components\util;
  12. use Yii;
  13. class ExpendController extends BaseController
  14. {
  15. //数据统计 ssh 20241122
  16. public function actionStat()
  17. {
  18. ini_set('memory_limit', '2045M');
  19. set_time_limit(0);
  20. $get = Yii::$app->request->get();
  21. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  22. $startTime = $get['startTime'] ?? '';
  23. $endTime = $get['endTime'] ?? '';
  24. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  25. $start = $period['startTime'];
  26. $end = $period['endTime'];
  27. $currentStartDate = date('Y-m-d', strtotime($start));
  28. $currentEndDate = date('Y-m-d', strtotime($end));
  29. $currentStartTime = $currentStartDate . ' 00:00:00';
  30. $currentEndTime = $currentEndDate . ' 23:59:59';
  31. $where = ['mainId' => $this->mainId];
  32. // timeType=pay(默认记账 payTime)/ send(发生 sendTime)
  33. $timeType = isset($get['timeType']) ? strval($get['timeType']) : 'pay';
  34. $timeField = ($timeType === 'send') ? 'sendTime' : 'payTime';
  35. $where[$timeField] = ['between', [$currentStartTime, $currentEndTime]];
  36. $where['delStatus'] = 0; //过滤掉已删除的
  37. $list = ExpendClass::getAllByCondition($where, null, '*', null);
  38. $arr = [];
  39. $cat = [];
  40. $map = dict::getDict('expendTypeMap');
  41. foreach ($map as $id => $name) {
  42. $cat[$id] = ['num' => 0, 'amount' => 0, 'name' => $name];
  43. }
  44. $totalExpend = 0;
  45. if (!empty($list)) {
  46. foreach ($list as $expend) {
  47. $staffId = $expend['staffId'] ?? 0;
  48. $amount = $expend['amount'] ?? 0;
  49. $staffName = $expend['staffName'] ?? '';
  50. $bx = $expend['bx'] ?? 0;
  51. $type = $expend['type'] ?? 0;
  52. $currentAmount = $amount;
  53. if ($bx == 0) {
  54. $hasAmount = 0;
  55. $unAmount = $amount;
  56. } else {
  57. $hasAmount = $amount;
  58. $unAmount = 0;
  59. }
  60. if (isset($arr[$staffId])) {
  61. //总支出
  62. $arr[$staffId]['totalAmount'] = floatval(bcadd($arr[$staffId]['totalAmount'], $currentAmount));
  63. //已报销
  64. $arr[$staffId]['hasAmount'] = floatval(bcadd($arr[$staffId]['hasAmount'], $hasAmount));
  65. //待报销
  66. $arr[$staffId]['amount'] = floatval(bcadd($arr[$staffId]['amount'], $unAmount));
  67. $arr[$staffId]['num'] = bcadd($arr[$staffId]['num'], 1);
  68. } else {
  69. $arr[$staffId]['totalAmount'] = floatval($currentAmount);
  70. $arr[$staffId]['hasAmount'] = floatval($hasAmount);
  71. $arr[$staffId]['amount'] = floatval($unAmount);
  72. $arr[$staffId]['staffName'] = $staffName;
  73. $arr[$staffId]['num'] = 1;
  74. }
  75. $totalExpend = bcadd($totalExpend, $currentAmount, 2);
  76. if (isset($cat[$type])) {
  77. $cat[$type]['num']++;
  78. $cat[$type]['amount'] = floatval(bcadd($cat[$type]['amount'], $amount, 2));
  79. }
  80. }
  81. }
  82. util::success(['list' => $arr, 'totalExpend' => $totalExpend, 'catList' => $cat]);
  83. }
  84. //报销汇总统计 ssh 20240308
  85. public function actionUnBxList()
  86. {
  87. ini_set('memory_limit', '2045M');
  88. set_time_limit(0);
  89. $where = ['mainId' => $this->mainId, 'bx' => 0, 'delStatus' => 0];
  90. $list = ExpendClass::getAllByCondition($where, null, '*', null);
  91. $arr = [];
  92. if (!empty($list)) {
  93. foreach ($list as $expend) {
  94. $staffId = $expend['staffId'] ?? 0;
  95. $amount = $expend['amount'] ?? 0;
  96. $staffName = $expend['staffName'] ?? '';
  97. $currentAmount = $amount;
  98. $unAmount = $amount;
  99. if (isset($arr[$staffId])) {
  100. $arr[$staffId]['amount'] = bcadd($arr[$staffId]['amount'], $unAmount, 2);
  101. $arr[$staffId]['num'] = bcadd($arr[$staffId]['num'], 1);
  102. } else {
  103. $arr[$staffId]['totalAmount'] = $currentAmount;
  104. $arr[$staffId]['amount'] = $unAmount;
  105. $arr[$staffId]['staffName'] = $staffName;
  106. $arr[$staffId]['num'] = 1;
  107. }
  108. }
  109. }
  110. util::success(['list' => $arr]);
  111. }
  112. //报销金额 ssh 20240308
  113. public function actionBx()
  114. {
  115. $staff = $this->shopAdmin;
  116. if (isset($staff->finance) == false || $staff->finance == 0) {
  117. util::fail('请财务操作');
  118. }
  119. $mainId = $this->mainId;
  120. if (getenv('YII_ENV') == 'production') {
  121. if (in_array($mainId, [23390])) {
  122. if (!in_array($this->adminId, [24655])) {
  123. util::fail('请财务人员操作');
  124. }
  125. }
  126. } else {
  127. if (in_array($mainId, [644])) {
  128. if (!in_array($this->adminId, [919])) {
  129. //util::fail('请财务人员操作哈');
  130. }
  131. }
  132. }
  133. $get = Yii::$app->request->get();
  134. $postAmount = $get['amount'] ?? 0;
  135. $staffId = $get['staffId'] ?? 0;
  136. if ($postAmount <= 0) {
  137. util::fail('没有需要报销的金额');
  138. }
  139. $connection = Yii::$app->db;
  140. $transaction = $connection->beginTransaction();
  141. try {
  142. $condition = ['mainId' => $this->mainId, 'bx' => 0, 'staffId' => $staffId, 'delStatus' => 0];
  143. $list = ExpendClass::getAllByCondition($condition, null, '*', null, true);
  144. $total = 0;
  145. $staff = $this->shopAdmin;
  146. $bxData = [
  147. 'mainId' => $this->mainId,
  148. 'staffId' => $staff->id ?? 0,
  149. 'staffName' => $staff->name ?? '',
  150. ];
  151. $bx = ExpendBxClass::add($bxData, true);
  152. $bxId = $bx->id ?? 0;
  153. foreach ($list as $it) {
  154. $amount = $it->amount ?? 0;
  155. $total = bcadd($total, $amount, 2);
  156. $it->bx = 1;
  157. $it->bxId = $bxId;
  158. $it->save();
  159. }
  160. $bx->amount = $total;
  161. $bx->save();
  162. if (floatval($postAmount) != floatval($total)) {
  163. util::fail('金额错误,请刷新页面,实际:' . floatval($total));
  164. }
  165. $transaction->commit();
  166. util::complete();
  167. } catch (\Exception $e) {
  168. $transaction->rollBack();
  169. $msg = $e->getMessage();
  170. noticeUtil::push("报销出错了,错误信息:{$msg}", '15280215347');
  171. util::fail();
  172. }
  173. }
  174. //类型列表 ssh 20240308
  175. public function actionGetTypeList()
  176. {
  177. $list = dict::getDict('expendTypeList');
  178. util::success(['list' => $list]);
  179. }
  180. public function actionList()
  181. {
  182. $get = Yii::$app->request->get();
  183. $bx = $get['bx'] ?? -1;
  184. $type = $get['type'] ?? -1;
  185. $staffId = $get['staffId'] ?? 0;
  186. $where = [];
  187. $where['mainId'] = $this->mainId;
  188. if (!empty($staffId)) {
  189. $where['staffId'] = $staffId;
  190. }
  191. if ($type != -1) {
  192. $where['type'] = $type;
  193. }
  194. if ($bx != -1) {
  195. $where['bx'] = $bx;
  196. }
  197. $where['delStatus'] = 0;
  198. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'thisYear';
  199. $startTime = $get['startTime'] ?? '';
  200. $endTime = $get['endTime'] ?? '';
  201. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  202. $start = $period['startTime'];
  203. $end = $period['endTime'];
  204. $currentStartDate = date('Y-m-d', strtotime($start));
  205. $currentEndDate = date('Y-m-d', strtotime($end));
  206. $currentStartTime = $currentStartDate . ' 00:00:00';
  207. $currentEndTime = $currentEndDate . ' 23:59:59';
  208. // timeType=pay(默认记账 payTime)/ send(发生 sendTime)
  209. $timeType = isset($get['timeType']) ? strval($get['timeType']) : 'pay';
  210. $timeField = ($timeType === 'send') ? 'sendTime' : 'payTime';
  211. $where[$timeField] = ['between', [$currentStartTime, $currentEndTime]];
  212. $respond = ExpendClass::getExpendList($where, $timeField . ' DESC,id DESC');
  213. $map = dict::getDict('expendTypeMap');
  214. if (!empty($respond['list'])) {
  215. foreach ($respond['list'] as $key => $val) {
  216. $type = $val['type'] ?? 0;
  217. $name = $map[$type] ?? '未命名';
  218. $respond['list'][$key]['typeName'] = $name;
  219. }
  220. }
  221. util::success($respond);
  222. }
  223. //新增支出 ssh 20220710
  224. public function actionAddExpend()
  225. {
  226. $mainId = $this->mainId;
  227. if (getenv('YII_ENV') == 'production') {
  228. if (in_array($mainId, [23390])) {
  229. if (!in_array($this->adminId, [24655])) {
  230. util::fail('请财务人员操作吧');
  231. }
  232. } else {
  233. }
  234. } else {
  235. if (in_array($mainId, [644])) {
  236. if (!in_array($this->adminId, [919])) {
  237. //util::fail('请财务人员操作哦');
  238. }
  239. }
  240. }
  241. //避免重复提交
  242. util::checkRepeatCommit($this->adminId, 10);
  243. $post = Yii::$app->request->post();
  244. $type = $post['type'] ?? -1;
  245. $amount = $post['amount'] ?? 0;
  246. $remark = $post['remark'] ?? '';
  247. $bx = $post['bx'] ?? -1;
  248. if (!is_numeric($amount) || $amount <= 0) {
  249. util::fail('请输入正确的金额');
  250. }
  251. if ($bx == -1) {
  252. util::fail('请选报销状态');
  253. }
  254. if ($type == -1) {
  255. util::fail('请选择类型');
  256. }
  257. $connection = Yii::$app->db;
  258. $transaction = $connection->beginTransaction();
  259. try {
  260. $payWay = $post['payWay'] ?? 0;
  261. if ($payWay == dict::getDict('payWay', 'cash')) {
  262. //扣除现金
  263. $mainId = $this->mainId;
  264. $main = MainClass::getLockById($mainId);
  265. if (empty($main)) {
  266. util::fail('没有门店信息');
  267. }
  268. $balance = bcsub($main->money, $amount, 2);
  269. if ($balance < 0) {
  270. util::fail('现金不足');
  271. }
  272. $main->money = $balance;
  273. $main->save();
  274. $staffId = $this->shopAdminId;
  275. $staff = $this->shopAdmin;
  276. $staffName = $staff->name ?? '';
  277. $map = dict::getDict('expendTypeMap');
  278. $expendName = $map[$type] ?? '未知';
  279. $event = $staffName . $expendName . '支出' . $amount . '元';
  280. $capitalType = dict::getDict('capitalType', 'expendRegister', 'id');
  281. $data = [];
  282. $data['balance'] = $balance;
  283. $data['mainId'] = $mainId;
  284. $data['sjId'] = $this->sjId;
  285. $data['amount'] = $amount;
  286. $data['staffId'] = $staffId;
  287. $data['staffName'] = $staffName;
  288. $data['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  289. $data['capitalType'] = $capitalType;
  290. $data['event'] = $event;
  291. $data['remark'] = $remark;
  292. ShopMoneyClass::addData($data);
  293. }
  294. $remark = $post['remark'] ?? '';
  295. $getStaffId = $post['getStaffId'] ?? 0;
  296. $staff = ShopAdminClass::getById($getStaffId, true);
  297. if (empty($staff)) {
  298. util::fail('没有找到员工');
  299. }
  300. if ($staff->mainId != $this->mainId) {
  301. util::fail('没有员工');
  302. }
  303. $staffName = $staff->name ?? '';
  304. $ptStyle = dict::getDict('ptStyle', 'ghs');
  305. // payTime:登记记账时间,固定为当前时刻(不可再改)
  306. $payTime = date("Y-m-d H:i:s");
  307. // sendTime:业务归属时间,前端可选日期;空则当天
  308. $sendTimeInput = isset($post['sendTime']) ? str_replace('/', '-', trim((string)$post['sendTime'])) : '';
  309. if ($sendTimeInput === '') {
  310. $sendTime = $payTime;
  311. } elseif (preg_match('/^\d{4}-\d{2}-\d{2}$/', $sendTimeInput)) {
  312. $sendTime = $sendTimeInput . date(' H:i:s');
  313. } elseif (preg_match('/^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}/', $sendTimeInput)) {
  314. $sendTime = $sendTimeInput;
  315. } else {
  316. $sendTime = $payTime;
  317. }
  318. $expendData = [
  319. 'type' => $type,
  320. 'amount' => $amount,
  321. 'remark' => $remark,
  322. 'mainId' => $this->mainId,
  323. 'staffId' => $getStaffId,
  324. 'staffName' => $staffName,
  325. 'ptStyle' => $ptStyle,
  326. 'payWay' => $payWay,
  327. 'bx' => $bx,
  328. 'payTime' => $payTime,
  329. 'sendTime' => $sendTime,
  330. ];
  331. $respond = ExpendClass::add($expendData, true);
  332. if ($bx == 1) {
  333. $bxData = [
  334. 'mainId' => $this->mainId,
  335. 'staffId' => $getStaffId,
  336. 'staffName' => $staffName,
  337. 'remark' => '登记时已报销',
  338. ];
  339. $bxInfo = ExpendBxClass::add($bxData, true);
  340. $bxId = $bxInfo->id ?? 0;
  341. $bxInfo->amount = $amount;
  342. $bxInfo->save();
  343. $respond->bxId = $bxId;
  344. $respond->save();
  345. }
  346. $transaction->commit();
  347. util::complete();
  348. } catch (\Exception $e) {
  349. $transaction->rollBack();
  350. $msg = $e->getMessage();
  351. noticeUtil::push("登记支出失败,错误信息:{$msg}", '15280215347');
  352. }
  353. }
  354. //删除支出
  355. public function actionDelExpend()
  356. {
  357. $post = Yii::$app->request->post();
  358. $staff = $this->shopAdmin;
  359. if ($staff->finance == 0) {
  360. util::fail('没有财务权限');
  361. }
  362. $staffId = $this->shopAdminId;
  363. $id = intval($post['id']);
  364. $expend = ExpendClass::getById($id, true);
  365. if ($expend->mainId != $this->mainId) {
  366. util::fail('没有找到支出数据');
  367. }
  368. if ($expend->delStatus == 1) {
  369. util::fail('此支出数据已是删除状态');
  370. }
  371. // 处理报销表数据
  372. if ($expend->bxId != 0) {
  373. $expendBx = ExpendBxClass::getById($expend->bxId, true);
  374. $expendBx->amount = bcsub($expendBx->amount, $expend->amount, 2);
  375. //怕remark字段不足,这边缩短长度
  376. $expendBx->remark = $expendBx->remark . "|删除支出ID " . $expend->id;
  377. $expendBx->save();
  378. }
  379. $expend->delStaffId = $staffId;
  380. $expend->delStaffName = $staff->name;
  381. $expend->delStatus = 1;
  382. $rs = $expend->save();
  383. if ($rs) {
  384. util::complete();
  385. } else {
  386. util::fail('删除支出失败');
  387. }
  388. }
  389. //修改账单日期 ssh 20241122
  390. public function actionChangePayTime()
  391. {
  392. $get = Yii::$app->request->get();
  393. $id = $get['id'] ?? 0;
  394. $date = $get['date'] ?? '';
  395. if (empty($date)) {
  396. util::fail('没有日期');
  397. }
  398. util::fail('不能再修改账单日期');
  399. $expend = ExpendClass::getById($id, true);
  400. if (empty($expend)) {
  401. util::fail('没有找到报销');
  402. }
  403. if ($expend->mainId != $this->mainId) {
  404. util::fail('不是你的报销');
  405. }
  406. $addTime = $expend->addTime;
  407. $expend->payTime = $date . date(" H:i:s", strtotime($addTime));
  408. $expend->save();
  409. util::complete('修改成功');
  410. }
  411. /**
  412. * 修改支出业务时间 sendTime(列表「修改」走此接口;payTime 仍不可改)。
  413. */
  414. public function actionChangeSendTime()
  415. {
  416. $get = Yii::$app->request->get();
  417. $id = $get['id'] ?? 0;
  418. $date = $get['date'] ?? '';
  419. if (empty($date)) {
  420. util::fail('没有日期');
  421. }
  422. $date = str_replace('/', '-', trim((string)$date));
  423. if (!preg_match('/^\d{4}-\d{2}-\d{2}/', $date)) {
  424. util::fail('日期格式错误');
  425. }
  426. $expend = ExpendClass::getById($id, true);
  427. if (empty($expend)) {
  428. util::fail('没有找到支出');
  429. }
  430. if ($expend->mainId != $this->mainId) {
  431. util::fail('不是你的支出');
  432. }
  433. // 只改 sendTime;时分秒沿用原 sendTime,空则用 addTime
  434. $baseTime = $expend->sendTime ?? '';
  435. if (empty($baseTime) || strpos((string)$baseTime, '0000-00-00') === 0) {
  436. $baseTime = $expend->addTime ?? date('Y-m-d H:i:s');
  437. }
  438. $day = substr($date, 0, 10);
  439. $expend->sendTime = $day . date(' H:i:s', strtotime($baseTime));
  440. $expend->save();
  441. util::complete('修改成功');
  442. }
  443. }