ExpendController.php 3.3 KB

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