ExpendController.php 12 KB

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