Bladeren bron

减少余额

shish 1 jaar geleden
bovenliggende
commit
c44798f9e4

+ 19 - 0
app-hd/controllers/DeductController.php

@@ -3,6 +3,8 @@
 namespace hd\controllers;
 
 use bizHd\custom\classes\CustomClass;
+use bizHd\custom\classes\HdClass;
+use bizHd\deduct\classes\DeductClass;
 use common\components\util;
 use Yii;
 
@@ -16,6 +18,7 @@ class DeductController extends BaseController
     {
         $post = Yii::$app->request->post();
         $customId = $post['customId'] ?? 0;
+        $remark = $post['remark'] ?? '';
         $custom = CustomClass::getByID($customId, true);
         if (empty($custom)) {
             util::fail('没有找到客户');
@@ -23,6 +26,11 @@ class DeductController extends BaseController
         if ($custom->shopId != $this->shopId) {
             util::fail('不是你的客户');
         }
+        $hdId = $custom->hdId;
+        $hd = HdClass::getByID($hdId, true);
+        if (empty($hd)) {
+            util::fail('客户信息缺失');
+        }
         $amount = $post['amount'] ?? 0;
         if (empty($amount)) {
             util::fail('请填写金额');
@@ -30,6 +38,17 @@ class DeductController extends BaseController
         if ($amount < 0) {
             util::fail('金额要大于0');
         }
+        $shop = $this->shop;
+        $staff = $this->shopAdmin;
+        $staffId = $staff->id;
+        $staffName = $staff->name;
+        $params = [
+            'remark' => $remark,
+            'staffId' => $staffId,
+            'staffName' => $staffName,
+        ];
+        DeductClass::doDeduct($shop, $custom, $hd, $amount, $params);
+
         util::complete('减少成功');
     }
 

+ 56 - 0
biz-hd/deduct/classes/DeductClass.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace bizHd\deduct\classes;
+
+use common\components\util;
+use Yii;
+use bizHd\base\classes\BaseClass;
+
+class DeductClass extends BaseClass
+{
+
+    public static $baseFile = '\bizHd\deduct\models\Deduct';
+
+    public static function doDeduct($shop, $custom, $hd, $amount, $params)
+    {
+        $currentBalance = $custom->balance;
+        if ($amount > $currentBalance) {
+            util::fail('余额不够扣');
+        }
+        $custom->balance = bcsub($custom->balance, $amount, 2);
+        $custom->save();
+
+        $hd->balance = bcsub($hd->balance, $amount, 2);
+        $hd->save();
+
+        if (floatval($hd->balance) != floatval($custom->balance)) {
+            util::fail('余额有问题');
+        }
+
+        $remark = $params['remark'] ?? '';
+        $staffId = $params['staffId'] ?? 0;
+        $staffName = $params['staffName'] ?? '';
+        $shopId = $shop->id;
+        $mainId = $shop->mainId;
+        $hdId = $hd->id;
+        $hdName = $hd->name;
+        $customId = $custom->id;
+        $customName = $custom->name;
+        $data = [
+            'shopId' => $shopId,
+            'mainId' => $mainId,
+            'hdId' => $hdId,
+            'hdName' => $hdName,
+            'amount' => $amount,
+            'balance' => $custom->balance,
+            'customId' => $customId,
+            'customName' => $customName,
+            'staffId' => $staffId,
+            'staffName' => $staffName,
+            'status' => 1,
+            'remark' => $remark,
+        ];
+        self::add($data, true);
+    }
+
+}

+ 13 - 0
biz-hd/deduct/models/Deduct.php

@@ -0,0 +1,13 @@
+<?php
+namespace bizHd\deduct\models;
+use bizHd\base\models\Base;
+
+class Deduct extends Base
+{
+
+    public static function tableName()
+    {
+        return 'xhDeduct';
+    }
+
+}

+ 12 - 0
biz-hd/deduct/services/DeductService.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace bizHd\deduct\services;
+
+use bizHd\base\services\BaseService;
+use Yii;
+
+class DeductService extends BaseService
+{
+
+    public static $baseFile = '\bizHd\deduct\classes\DeductClass';
+}