ExpendController.php 11 KB

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