DeductController.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\custom\classes\CustomClass;
  4. use bizHd\custom\classes\HdClass;
  5. use bizHd\deduct\classes\DeductClass;
  6. use common\components\util;
  7. use Yii;
  8. class DeductController extends BaseController
  9. {
  10. public $guestAccess = [];
  11. //减少余额 ssh 20250329(这个方法应该建在CustomController的)
  12. public function actionDeductBalance()
  13. {
  14. $post = Yii::$app->request->post();
  15. $customId = $post['customId'] ?? 0;
  16. $remark = $post['remark'] ?? '';
  17. $custom = CustomClass::getByID($customId, true);
  18. if (empty($custom)) {
  19. util::fail('没有找到客户');
  20. }
  21. if ($custom->shopId != $this->shopId) {
  22. util::fail('不是你的客户');
  23. }
  24. $hdId = $custom->hdId;
  25. $hd = HdClass::getByID($hdId, true);
  26. if (empty($hd)) {
  27. util::fail('客户信息缺失');
  28. }
  29. $amount = $post['amount'] ?? 0;
  30. if (empty($amount)) {
  31. util::fail('请填写金额');
  32. }
  33. if ($amount < 0) {
  34. util::fail('金额要大于0');
  35. }
  36. $connection = Yii::$app->db;
  37. $transaction = $connection->beginTransaction();
  38. try {
  39. $shop = $this->shop;
  40. $staff = $this->shopAdmin;
  41. $staffId = $staff->id;
  42. $staffName = $staff->name;
  43. $params = [
  44. 'remark' => $remark,
  45. 'staffId' => $staffId,
  46. 'staffName' => $staffName,
  47. ];
  48. DeductClass::doDeduct($shop, $custom, $hd, $amount, $params);
  49. $transaction->commit();
  50. util::complete('减少成功');
  51. } catch (\Exception $exception) {
  52. $transaction->rollBack();
  53. Yii::info("减少报错:" . $exception->getMessage());
  54. util::fail('减少失败');
  55. }
  56. }
  57. }