ExpendController.php 14 KB

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