ExpendController.php 9.6 KB

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