ExpendController.php 8.4 KB

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