ExpendController.php 9.9 KB

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