ExpendController.php 17 KB

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