ExpendController.php 15 KB

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