ExpendController.php 11 KB

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