ExpendController.php 18 KB

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