ExpendController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\dict;
  9. use common\components\noticeUtil;
  10. use common\components\util;
  11. use Yii;
  12. class ExpendController extends BaseController
  13. {
  14. //待报销人员列表和金额 ssh 20240308
  15. public function actionUnBxList()
  16. {
  17. ini_set('memory_limit', '2045M');
  18. set_time_limit(0);
  19. $list = ExpendClass::getAllByCondition(['mainId' => $this->mainId, 'bx' => 0], null, '*', null);
  20. $arr = [];
  21. if (!empty($list)) {
  22. foreach ($list as $expend) {
  23. $staffId = $expend['staffId'] ?? 0;
  24. $amount = $expend['amount'] ?? 0;
  25. $staffName = $expend['staffName'] ?? '';
  26. if (isset($arr[$staffId])) {
  27. $arr[$staffId]['amount'] = bcadd($arr[$staffId]['amount'], $amount);
  28. $arr[$staffId]['num'] = bcadd($arr[$staffId]['num'], 1);
  29. } else {
  30. $arr[$staffId]['amount'] = $amount;
  31. $arr[$staffId]['staffName'] = $staffName;
  32. $arr[$staffId]['num'] = 1;
  33. }
  34. }
  35. }
  36. util::success(['list' => $arr]);
  37. }
  38. //报销金额 ssh 20240308
  39. public function actionBx()
  40. {
  41. $get = Yii::$app->request->get();
  42. $postAmount = $get['amount'] ?? 0;
  43. $staffId = $get['staffId'] ?? 0;
  44. if ($postAmount <= 0) {
  45. util::fail('没有需要报销的金额');
  46. }
  47. $connection = Yii::$app->db;//事务处理
  48. $transaction = $connection->beginTransaction();
  49. try {
  50. $list = ExpendClass::getAllByCondition(['mainId' => $this->mainId, 'bx' => 0, 'staffId' => $staffId], null, '*', null, true);
  51. $total = 0;
  52. $staff = $this->shopAdmin;
  53. $bxData = [
  54. 'mainId' => $this->mainId,
  55. 'staffId' => $staff->id ?? 0,
  56. 'staffName' => $staff->name ?? '',
  57. ];
  58. $bx = ExpendBxClass::add($bxData, true);
  59. $bxId = $bx->id ?? 0;
  60. foreach ($list as $it) {
  61. $amount = $it->amount ?? 0;
  62. $total = bcadd($total, $amount, 2);
  63. $it->bx = 1;
  64. $it->bxId = $bxId;
  65. $it->save();
  66. }
  67. $bx->amount = $total;
  68. $bx->save();
  69. if (floatval($postAmount) != floatval($total)) {
  70. util::fail('金额有问题,返回上页再进来');
  71. }
  72. $transaction->commit();
  73. util::complete();
  74. } catch (\Exception $e) {
  75. $transaction->rollBack();
  76. $msg = $e->getMessage();
  77. noticeUtil::push("报销出错了,错误信息:{$msg}", '15280215347');
  78. util::fail();
  79. }
  80. }
  81. //类型列表 ssh 20240308
  82. public function actionGetTypeList()
  83. {
  84. $list = dict::getDict('expendTypeMap');
  85. util::success(['list' => $list]);
  86. }
  87. public function actionList()
  88. {
  89. $where = [];
  90. $where['mainId'] = $this->mainId;
  91. $list = ExpendClass::getExpendList($where);
  92. util::success($list);
  93. }
  94. //新增支出 ssh 20220710
  95. public function actionAddExpend()
  96. {
  97. $post = Yii::$app->request->post();
  98. $type = $post['type'] ?? 0;
  99. $amount = $post['amount'] ?? 0;
  100. $remark = $post['remark'] ?? '';
  101. if (is_numeric($amount) == false || $amount <= 0) {
  102. util::fail('请输入正确的金额');
  103. }
  104. $connection = Yii::$app->db;//事务处理
  105. $transaction = $connection->beginTransaction();
  106. try {
  107. $payWay = $post['payWay'] ?? 0;
  108. if ($payWay == dict::getDict('payWay', 'cash')) {
  109. //扣除现金
  110. $mainId = $this->mainId;
  111. $main = MainClass::getLockById($mainId);
  112. if (empty($main)) {
  113. util::fail('没有门店信息');
  114. }
  115. $balance = bcsub($main->money, $amount, 2);
  116. if ($balance < 0) {
  117. util::fail('现金不足');
  118. }
  119. $main->money = $balance;
  120. $main->save();
  121. $staffId = $this->shopAdminId;
  122. $staff = $this->shopAdmin;
  123. $staffName = $staff->name ?? '';
  124. $map = dict::getDict('expendTypeMap');
  125. $expendName = $map[$type] ?? '未知';
  126. $event = $staffName . $expendName . '支出' . $amount . '元';
  127. $capitalType = dict::getDict('capitalType', 'expendRegister', 'id');
  128. $data = [];
  129. $data['balance'] = $balance;
  130. $data['mainId'] = $mainId;
  131. $data['sjId'] = $this->sjId;
  132. $data['amount'] = $amount;
  133. $data['staffId'] = $staffId;
  134. $data['staffName'] = $staffName;
  135. $data['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  136. $data['capitalType'] = $capitalType;
  137. $data['event'] = $event;
  138. $data['remark'] = $remark;
  139. ShopMoneyClass::addData($data);
  140. }
  141. $remark = $post['remark'] ?? '';
  142. $staffName = $this->shopAdmin->name ?? '';
  143. $ptStyle = dict::getDict('ptStyle', 'ghs');
  144. $expendData = [
  145. 'type' => $type,
  146. 'amount' => $amount,
  147. 'remark' => $remark,
  148. 'mainId' => $this->mainId,
  149. 'staffId' => $this->shopAdminId,
  150. 'staffName' => $staffName,
  151. 'ptStyle' => $ptStyle,
  152. 'payWay' => $payWay,
  153. ];
  154. ExpendClass::add($expendData);
  155. $transaction->commit();
  156. util::complete();
  157. } catch (\Exception $e) {
  158. $transaction->rollBack();
  159. $msg = $e->getMessage();
  160. noticeUtil::push("登记支出失败,错误信息:{$msg}", '15280215347');
  161. }
  162. }
  163. }