ExpendController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\expend\classes\ExpendClass;
  4. use bizGhs\shop\classes\MainClass;
  5. use bizGhs\shop\classes\ShopMoneyClass;
  6. use common\components\dict;
  7. use common\components\noticeUtil;
  8. use common\components\util;
  9. use Yii;
  10. class ExpendController extends BaseController
  11. {
  12. //类型
  13. public function actionGetTypeList()
  14. {
  15. $list = dict::getDict('expendTypeMap');
  16. util::success(['list' => $list]);
  17. }
  18. public function actionList()
  19. {
  20. $where = [];
  21. $where['mainId'] = $this->mainId;
  22. $list = ExpendClass::getExpendList($where);
  23. util::success($list);
  24. }
  25. //新增支出 ssh 20220710
  26. public function actionAddExpend()
  27. {
  28. $post = Yii::$app->request->post();
  29. $type = $post['type'] ?? 0;
  30. $amount = $post['amount'] ?? 0;
  31. $remark = $post['remark'] ?? '';
  32. if (is_numeric($amount) == false || $amount <= 0) {
  33. util::fail('请输入正确的金额');
  34. }
  35. $connection = Yii::$app->db;//事务处理
  36. $transaction = $connection->beginTransaction();
  37. try {
  38. $payWay = $post['payWay'] ?? 0;
  39. if ($payWay == dict::getDict('payWay', 'cash')) {
  40. //扣除现金
  41. $mainId = $this->mainId;
  42. $main = MainClass::getLockById($mainId);
  43. if (empty($main)) {
  44. util::fail('没有门店信息');
  45. }
  46. $balance = bcsub($main->money, $amount, 2);
  47. if ($balance < 0) {
  48. util::fail('现金不足');
  49. }
  50. $main->money = $balance;
  51. $main->save();
  52. $staffId = $this->shopAdminId;
  53. $staff = $this->shopAdmin;
  54. $staffName = $staff->name ?? '';
  55. $map = dict::getDict('expendTypeMap');
  56. $expendName = $map[$type] ?? '未知';
  57. $event = $staffName . $expendName . '支出' . $amount . '元';
  58. $capitalType = dict::getDict('capitalType', 'expendRegister', 'id');
  59. $data = [];
  60. $data['balance'] = $balance;
  61. $data['mainId'] = $mainId;
  62. $data['sjId'] = $this->sjId;
  63. $data['amount'] = $amount;
  64. $data['staffId'] = $staffId;
  65. $data['staffName'] = $staffName;
  66. $data['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  67. $data['capitalType'] = $capitalType;
  68. $data['event'] = $event;
  69. $data['remark'] = $remark;
  70. ShopMoneyClass::addData($data);
  71. }
  72. $remark = $post['remark'] ?? '';
  73. $staffName = $this->shopAdmin->name ?? '';
  74. $ptStyle = dict::getDict('ptStyle', 'ghs');
  75. $expendData = [
  76. 'type' => $type,
  77. 'amount' => $amount,
  78. 'remark' => $remark,
  79. 'mainId' => $this->mainId,
  80. 'staffId' => $this->shopAdminId,
  81. 'staffName' => $staffName,
  82. 'ptStyle' => $ptStyle,
  83. 'payWay' => $payWay,
  84. ];
  85. ExpendClass::add($expendData);
  86. $transaction->commit();
  87. util::complete();
  88. } catch (\Exception $e) {
  89. $transaction->rollBack();
  90. $msg = $e->getMessage();
  91. noticeUtil::push("登记支出失败,错误信息:{$msg}", '15280215347');
  92. }
  93. }
  94. }