ExpendController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 20240308
  16. public function actionUnBxList()
  17. {
  18. ini_set('memory_limit', '2045M');
  19. set_time_limit(0);
  20. $list = ExpendClass::getAllByCondition(['mainId' => $this->mainId, 'bx' => 0], null, '*', null);
  21. $arr = [];
  22. if (!empty($list)) {
  23. foreach ($list as $expend) {
  24. $staffId = $expend['staffId'] ?? 0;
  25. $amount = $expend['amount'] ?? 0;
  26. $staffName = $expend['staffName'] ?? '';
  27. if (isset($arr[$staffId])) {
  28. $arr[$staffId]['amount'] = bcadd($arr[$staffId]['amount'], $amount);
  29. $arr[$staffId]['num'] = bcadd($arr[$staffId]['num'], 1);
  30. } else {
  31. $arr[$staffId]['amount'] = $amount;
  32. $arr[$staffId]['staffName'] = $staffName;
  33. $arr[$staffId]['num'] = 1;
  34. }
  35. }
  36. }
  37. util::success(['list' => $arr]);
  38. }
  39. //报销金额 ssh 20240308
  40. public function actionBx()
  41. {
  42. $get = Yii::$app->request->get();
  43. $postAmount = $get['amount'] ?? 0;
  44. $staffId = $get['staffId'] ?? 0;
  45. if ($postAmount <= 0) {
  46. util::fail('没有需要报销的金额');
  47. }
  48. $connection = Yii::$app->db;//事务处理
  49. $transaction = $connection->beginTransaction();
  50. try {
  51. $list = ExpendClass::getAllByCondition(['mainId' => $this->mainId, 'bx' => 0, 'staffId' => $staffId], null, '*', null, true);
  52. $total = 0;
  53. $staff = $this->shopAdmin;
  54. $bxData = [
  55. 'mainId' => $this->mainId,
  56. 'staffId' => $staff->id ?? 0,
  57. 'staffName' => $staff->name ?? '',
  58. ];
  59. $bx = ExpendBxClass::add($bxData, true);
  60. $bxId = $bx->id ?? 0;
  61. foreach ($list as $it) {
  62. $amount = $it->amount ?? 0;
  63. $total = bcadd($total, $amount, 2);
  64. $it->bx = 1;
  65. $it->bxId = $bxId;
  66. $it->save();
  67. }
  68. $bx->amount = $total;
  69. $bx->save();
  70. if (floatval($postAmount) != floatval($total)) {
  71. util::fail('金额有问题,返回上页再进来');
  72. }
  73. $transaction->commit();
  74. util::complete();
  75. } catch (\Exception $e) {
  76. $transaction->rollBack();
  77. $msg = $e->getMessage();
  78. noticeUtil::push("报销出错了,错误信息:{$msg}", '15280215347');
  79. util::fail();
  80. }
  81. }
  82. //类型列表 ssh 20240308
  83. public function actionGetTypeList()
  84. {
  85. $list = dict::getDict('expendTypeList');
  86. util::success(['list' => $list]);
  87. }
  88. public function actionList()
  89. {
  90. $get = Yii::$app->request->get();
  91. $bx = $get['bx'] ?? -1;
  92. $type = $get['type'] ?? -1;
  93. $staffId = $get['staffId'] ?? 0;
  94. $where = [];
  95. $where['mainId'] = $this->mainId;
  96. if (!empty($staffId)) {
  97. $where['staffId'] = $staffId;
  98. }
  99. if ($type != -1) {
  100. $where['type'] = $type;
  101. }
  102. if ($bx != -1) {
  103. $where['bx'] = $bx;
  104. }
  105. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  106. $startTime = $get['startTime'] ?? '';
  107. $endTime = $get['endTime'] ?? '';
  108. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  109. $start = $period['startTime'];
  110. $end = $period['endTime'];
  111. $currentStartDate = date('Y-m-d', strtotime($start));
  112. $currentEndDate = date('Y-m-d', strtotime($end));
  113. $currentStartTime = $currentStartDate . ' 00:00:00';
  114. $currentEndTime = $currentEndDate . ' 23:59:59';
  115. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  116. $list = ExpendClass::getExpendList($where);
  117. util::success($list);
  118. }
  119. //新增支出 ssh 20220710
  120. public function actionAddExpend()
  121. {
  122. $post = Yii::$app->request->post();
  123. $type = $post['type'] ?? 0;
  124. $amount = $post['amount'] ?? 0;
  125. $remark = $post['remark'] ?? '';
  126. $bx = $post['bx'] ?? -1;
  127. if (is_numeric($amount) == false || $amount <= 0) {
  128. util::fail('请输入正确的金额');
  129. }
  130. if ($bx == -1) {
  131. util::fail('请选报销状态');
  132. }
  133. $connection = Yii::$app->db;//事务处理
  134. $transaction = $connection->beginTransaction();
  135. try {
  136. $payWay = $post['payWay'] ?? 0;
  137. if ($payWay == dict::getDict('payWay', 'cash')) {
  138. //扣除现金
  139. $mainId = $this->mainId;
  140. $main = MainClass::getLockById($mainId);
  141. if (empty($main)) {
  142. util::fail('没有门店信息');
  143. }
  144. $balance = bcsub($main->money, $amount, 2);
  145. if ($balance < 0) {
  146. util::fail('现金不足');
  147. }
  148. $main->money = $balance;
  149. $main->save();
  150. $staffId = $this->shopAdminId;
  151. $staff = $this->shopAdmin;
  152. $staffName = $staff->name ?? '';
  153. $map = dict::getDict('expendTypeMap');
  154. $expendName = $map[$type] ?? '未知';
  155. $event = $staffName . $expendName . '支出' . $amount . '元';
  156. $capitalType = dict::getDict('capitalType', 'expendRegister', 'id');
  157. $data = [];
  158. $data['balance'] = $balance;
  159. $data['mainId'] = $mainId;
  160. $data['sjId'] = $this->sjId;
  161. $data['amount'] = $amount;
  162. $data['staffId'] = $staffId;
  163. $data['staffName'] = $staffName;
  164. $data['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  165. $data['capitalType'] = $capitalType;
  166. $data['event'] = $event;
  167. $data['remark'] = $remark;
  168. ShopMoneyClass::addData($data);
  169. }
  170. $remark = $post['remark'] ?? '';
  171. $getStaffId = $post['getStaffId'] ?? 0;
  172. $staff = ShopAdminClass::getById($getStaffId, true);
  173. if (empty($staff)) {
  174. util::fail('没有找到员工');
  175. }
  176. if ($staff->mainId != $this->mainId) {
  177. util::fail('没有员工');
  178. }
  179. $staffName = $staff->name ?? '';
  180. $ptStyle = dict::getDict('ptStyle', 'ghs');
  181. $expendData = [
  182. 'type' => $type,
  183. 'amount' => $amount,
  184. 'remark' => $remark,
  185. 'mainId' => $this->mainId,
  186. 'staffId' => $getStaffId,
  187. 'staffName' => $staffName,
  188. 'ptStyle' => $ptStyle,
  189. 'payWay' => $payWay,
  190. 'bx' => $bx,
  191. ];
  192. $respond = ExpendClass::add($expendData, true);
  193. if ($bx == 1) {
  194. $bxData = [
  195. 'mainId' => $this->mainId,
  196. 'staffId' => $getStaffId,
  197. 'staffName' => $staffName,
  198. 'remark' => '登记时已报销',
  199. ];
  200. $bxInfo = ExpendBxClass::add($bxData, true);
  201. $bxId = $bxInfo->id ?? 0;
  202. $bxInfo->amount = $amount;
  203. $bxInfo->save();
  204. $respond->bxId = $bxId;
  205. $respond->save();
  206. }
  207. $transaction->commit();
  208. util::complete();
  209. } catch (\Exception $e) {
  210. $transaction->rollBack();
  211. $msg = $e->getMessage();
  212. noticeUtil::push("登记支出失败,错误信息:{$msg}", '15280215347');
  213. }
  214. }
  215. }