ExpendController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 20241122
  16. public function actionStat()
  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['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  33. $where['delStaffId'] = 0; //过滤掉已删除的
  34. $list = ExpendClass::getAllByCondition($where, null, '*', null);
  35. $arr = [];
  36. $cat = [];
  37. $map = dict::getDict('expendTypeMap');
  38. foreach ($map as $id => $name) {
  39. $cat[$id] = ['num' => 0, 'amount' => 0, 'name' => $name];
  40. }
  41. $totalExpend = 0;
  42. if (!empty($list)) {
  43. foreach ($list as $expend) {
  44. $staffId = $expend['staffId'] ?? 0;
  45. $amount = $expend['amount'] ?? 0;
  46. $staffName = $expend['staffName'] ?? '';
  47. $bx = $expend['bx'] ?? 0;
  48. $type = $expend['type'] ?? 0;
  49. $currentAmount = $amount;
  50. if ($bx == 0) {
  51. $hasAmount = 0;
  52. $unAmount = $amount;
  53. } else {
  54. $hasAmount = $amount;
  55. $unAmount = 0;
  56. }
  57. if (isset($arr[$staffId])) {
  58. //总支出
  59. $arr[$staffId]['totalAmount'] = floatval(bcadd($arr[$staffId]['totalAmount'], $currentAmount));
  60. //已报销
  61. $arr[$staffId]['hasAmount'] = floatval(bcadd($arr[$staffId]['hasAmount'], $hasAmount));
  62. //待报销
  63. $arr[$staffId]['amount'] = floatval(bcadd($arr[$staffId]['amount'], $unAmount));
  64. $arr[$staffId]['num'] = bcadd($arr[$staffId]['num'], 1);
  65. } else {
  66. $arr[$staffId]['totalAmount'] = floatval($currentAmount);
  67. $arr[$staffId]['hasAmount'] = floatval($hasAmount);
  68. $arr[$staffId]['amount'] = floatval($unAmount);
  69. $arr[$staffId]['staffName'] = $staffName;
  70. $arr[$staffId]['num'] = 1;
  71. }
  72. $totalExpend = bcadd($totalExpend, $currentAmount, 2);
  73. if (isset($cat[$type])) {
  74. $cat[$type]['num']++;
  75. $cat[$type]['amount'] = floatval(bcadd($cat[$type]['amount'], $amount, 2));
  76. }
  77. }
  78. }
  79. util::success(['list' => $arr, 'totalExpend' => $totalExpend, 'catList' => $cat]);
  80. }
  81. //报销汇总统计 ssh 20240308
  82. public function actionUnBxList()
  83. {
  84. ini_set('memory_limit', '2045M');
  85. set_time_limit(0);
  86. $where = ['mainId' => $this->mainId, 'bx' => 0, 'delStaffId' => 0];
  87. $list = ExpendClass::getAllByCondition($where, null, '*', null);
  88. $arr = [];
  89. if (!empty($list)) {
  90. foreach ($list as $expend) {
  91. $staffId = $expend['staffId'] ?? 0;
  92. $amount = $expend['amount'] ?? 0;
  93. $staffName = $expend['staffName'] ?? '';
  94. $currentAmount = $amount;
  95. $unAmount = $amount;
  96. if (isset($arr[$staffId])) {
  97. $arr[$staffId]['amount'] = bcadd($arr[$staffId]['amount'], $unAmount,2);
  98. $arr[$staffId]['num'] = bcadd($arr[$staffId]['num'], 1);
  99. } else {
  100. $arr[$staffId]['totalAmount'] = $currentAmount;
  101. $arr[$staffId]['amount'] = $unAmount;
  102. $arr[$staffId]['staffName'] = $staffName;
  103. $arr[$staffId]['num'] = 1;
  104. }
  105. }
  106. }
  107. util::success(['list' => $arr]);
  108. }
  109. //报销金额 ssh 20240308
  110. public function actionBx()
  111. {
  112. $staff = $this->shopAdmin;
  113. if (isset($staff->finance) == false || $staff->finance == 0) {
  114. util::fail('请财务操作');
  115. }
  116. $mainId = $this->mainId;
  117. if (getenv('YII_ENV') == 'production') {
  118. if (in_array($mainId, [23390])) {
  119. if (!in_array($this->adminId, [24655])) {
  120. util::fail('请财务人员操作');
  121. }
  122. }
  123. } else {
  124. if (in_array($mainId, [644])) {
  125. if (!in_array($this->adminId, [919])) {
  126. //util::fail('请财务人员操作哈');
  127. }
  128. }
  129. }
  130. $get = Yii::$app->request->get();
  131. $postAmount = $get['amount'] ?? 0;
  132. $staffId = $get['staffId'] ?? 0;
  133. if ($postAmount <= 0) {
  134. util::fail('没有需要报销的金额');
  135. }
  136. $connection = Yii::$app->db;
  137. $transaction = $connection->beginTransaction();
  138. try {
  139. $list = ExpendClass::getAllByCondition(['mainId' => $this->mainId, 'bx' => 0, 'staffId' => $staffId], null, '*', null, true);
  140. $total = 0;
  141. $staff = $this->shopAdmin;
  142. $bxData = [
  143. 'mainId' => $this->mainId,
  144. 'staffId' => $staff->id ?? 0,
  145. 'staffName' => $staff->name ?? '',
  146. ];
  147. $bx = ExpendBxClass::add($bxData, true);
  148. $bxId = $bx->id ?? 0;
  149. foreach ($list as $it) {
  150. $amount = $it->amount ?? 0;
  151. $total = bcadd($total, $amount, 2);
  152. $it->bx = 1;
  153. $it->bxId = $bxId;
  154. $it->save();
  155. }
  156. $bx->amount = $total;
  157. $bx->save();
  158. if (floatval($postAmount) != floatval($total)) {
  159. util::fail('金额错误,请刷新页面,实际:'.floatval($total));
  160. }
  161. $transaction->commit();
  162. util::complete();
  163. } catch (\Exception $e) {
  164. $transaction->rollBack();
  165. $msg = $e->getMessage();
  166. noticeUtil::push("报销出错了,错误信息:{$msg}", '15280215347');
  167. util::fail();
  168. }
  169. }
  170. //类型列表 ssh 20240308
  171. public function actionGetTypeList()
  172. {
  173. $list = dict::getDict('expendTypeList');
  174. util::success(['list' => $list]);
  175. }
  176. public function actionList()
  177. {
  178. $get = Yii::$app->request->get();
  179. $bx = $get['bx'] ?? -1;
  180. $type = $get['type'] ?? -1;
  181. $staffId = $get['staffId'] ?? 0;
  182. $where = [];
  183. $where['mainId'] = $this->mainId;
  184. if (!empty($staffId)) {
  185. $where['staffId'] = $staffId;
  186. }
  187. if ($type != -1) {
  188. $where['type'] = $type;
  189. }
  190. if ($bx != -1) {
  191. $where['bx'] = $bx;
  192. }
  193. if (true) { //过滤掉已删除的
  194. $where['delStaffId'] = 0;
  195. }
  196. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'thisYear';
  197. $startTime = $get['startTime'] ?? '';
  198. $endTime = $get['endTime'] ?? '';
  199. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  200. $start = $period['startTime'];
  201. $end = $period['endTime'];
  202. $currentStartDate = date('Y-m-d', strtotime($start));
  203. $currentEndDate = date('Y-m-d', strtotime($end));
  204. $currentStartTime = $currentStartDate . ' 00:00:00';
  205. $currentEndTime = $currentEndDate . ' 23:59:59';
  206. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  207. $respond = ExpendClass::getExpendList($where);
  208. $map = dict::getDict('expendTypeMap');
  209. if (!empty($respond['list'])) {
  210. foreach ($respond['list'] as $key => $val) {
  211. $type = $val['type'] ?? 0;
  212. $name = $map[$type] ?? '未命名';
  213. $respond['list'][$key]['typeName'] = $name;
  214. }
  215. }
  216. util::success($respond);
  217. }
  218. //新增支出 ssh 20220710
  219. public function actionAddExpend()
  220. {
  221. $mainId = $this->mainId;
  222. if (getenv('YII_ENV') == 'production') {
  223. if (in_array($mainId, [23390])) {
  224. if (!in_array($this->adminId, [24655])) {
  225. util::fail('请财务人员操作吧');
  226. }
  227. } else {
  228. }
  229. } else {
  230. if (in_array($mainId, [644])) {
  231. if (!in_array($this->adminId, [919])) {
  232. //util::fail('请财务人员操作哦');
  233. }
  234. }
  235. }
  236. //避免重复提交
  237. util::checkRepeatCommit($this->adminId, 10);
  238. $post = Yii::$app->request->post();
  239. $type = $post['type'] ?? -1;
  240. $amount = $post['amount'] ?? 0;
  241. $remark = $post['remark'] ?? '';
  242. $bx = $post['bx'] ?? -1;
  243. if (is_numeric($amount) == false || $amount <= 0) {
  244. util::fail('请输入正确的金额');
  245. }
  246. if ($bx == -1) {
  247. util::fail('请选报销状态');
  248. }
  249. if ($type == -1) {
  250. util::fail('请选择类型');
  251. }
  252. $connection = Yii::$app->db;
  253. $transaction = $connection->beginTransaction();
  254. try {
  255. $payWay = $post['payWay'] ?? 0;
  256. if ($payWay == dict::getDict('payWay', 'cash')) {
  257. //扣除现金
  258. $mainId = $this->mainId;
  259. $main = MainClass::getLockById($mainId);
  260. if (empty($main)) {
  261. util::fail('没有门店信息');
  262. }
  263. $balance = bcsub($main->money, $amount, 2);
  264. if ($balance < 0) {
  265. util::fail('现金不足');
  266. }
  267. $main->money = $balance;
  268. $main->save();
  269. $staffId = $this->shopAdminId;
  270. $staff = $this->shopAdmin;
  271. $staffName = $staff->name ?? '';
  272. $map = dict::getDict('expendTypeMap');
  273. $expendName = $map[$type] ?? '未知';
  274. $event = $staffName . $expendName . '支出' . $amount . '元';
  275. $capitalType = dict::getDict('capitalType', 'expendRegister', 'id');
  276. $data = [];
  277. $data['balance'] = $balance;
  278. $data['mainId'] = $mainId;
  279. $data['sjId'] = $this->sjId;
  280. $data['amount'] = $amount;
  281. $data['staffId'] = $staffId;
  282. $data['staffName'] = $staffName;
  283. $data['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  284. $data['capitalType'] = $capitalType;
  285. $data['event'] = $event;
  286. $data['remark'] = $remark;
  287. ShopMoneyClass::addData($data);
  288. }
  289. $remark = $post['remark'] ?? '';
  290. $getStaffId = $post['getStaffId'] ?? 0;
  291. $staff = ShopAdminClass::getById($getStaffId, true);
  292. if (empty($staff)) {
  293. util::fail('没有找到员工');
  294. }
  295. if ($staff->mainId != $this->mainId) {
  296. util::fail('没有员工');
  297. }
  298. $staffName = $staff->name ?? '';
  299. $ptStyle = dict::getDict('ptStyle', 'ghs');
  300. $payTime = date("Y-m-d H:i:s");
  301. $historyDate = $post['historyDate'] ?? '';
  302. if (!empty($historyDate)) {
  303. $payTime = $historyDate . date(" H:i:s");
  304. }
  305. $expendData = [
  306. 'type' => $type,
  307. 'amount' => $amount,
  308. 'remark' => $remark,
  309. 'mainId' => $this->mainId,
  310. 'staffId' => $getStaffId,
  311. 'staffName' => $staffName,
  312. 'ptStyle' => $ptStyle,
  313. 'payWay' => $payWay,
  314. 'bx' => $bx,
  315. 'payTime' => $payTime,
  316. ];
  317. $respond = ExpendClass::add($expendData, true);
  318. if ($bx == 1) {
  319. $bxData = [
  320. 'mainId' => $this->mainId,
  321. 'staffId' => $getStaffId,
  322. 'staffName' => $staffName,
  323. 'remark' => '登记时已报销',
  324. ];
  325. $bxInfo = ExpendBxClass::add($bxData, true);
  326. $bxId = $bxInfo->id ?? 0;
  327. $bxInfo->amount = $amount;
  328. $bxInfo->save();
  329. $respond->bxId = $bxId;
  330. $respond->save();
  331. }
  332. $transaction->commit();
  333. util::complete();
  334. } catch (\Exception $e) {
  335. $transaction->rollBack();
  336. $msg = $e->getMessage();
  337. noticeUtil::push("登记支出失败,错误信息:{$msg}", '15280215347');
  338. }
  339. }
  340. //删除支出
  341. public function actionDelExpend()
  342. {
  343. $mainId = $this->mainId;
  344. if (getenv('YII_ENV') == 'production') {
  345. if (in_array($mainId, [23390])) {
  346. if (!in_array($this->adminId, [24655])) {
  347. util::fail('请财务人员操作吧');
  348. }
  349. }
  350. }
  351. $post = Yii::$app->request->post();
  352. $staffId = intVal($post['staffId']);
  353. $staff = ShopAdminClass::getById($staffId, true);
  354. if (empty($staff)) {
  355. util::fail('没有找到员工');
  356. }
  357. if ($staff->mainId != $this->mainId) {
  358. util::fail('员工不存在');
  359. }
  360. $shop = $this->shop;
  361. $admin = $this->admin;
  362. $id = intval($post['id']);
  363. $expend = ExpendClass::getById($id, true);
  364. $expend->delStaffId = $staffId;
  365. $expend->delStaffName = $staff->name;
  366. $rs = $expend->save();
  367. if ($rs) {
  368. util::complete();
  369. } else {
  370. util::fail('删除支出失败');
  371. }
  372. }
  373. //修改账单日期 ssh 20241122
  374. public function actionChangePayTime()
  375. {
  376. $get = Yii::$app->request->get();
  377. $id = $get['id'] ?? 0;
  378. $date = $get['date'] ?? '';
  379. if (empty($date)) {
  380. util::fail('没有日期');
  381. }
  382. $expend = ExpendClass::getById($id, true);
  383. if (empty($expend)) {
  384. util::fail('没有找到报销');
  385. }
  386. if ($expend->mainId != $this->mainId) {
  387. util::fail('不是你的报销');
  388. }
  389. $addTime = $expend->addTime;
  390. $expend->payTime = $date . date(" H:i:s", strtotime($addTime));
  391. $expend->save();
  392. util::complete('修改成功');
  393. }
  394. }