shish 5 éve
szülő
commit
5308ac3c7a

+ 52 - 0
app-ghs/controllers/PurchaseClearController.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace ghs\controllers;
+
+use bizGhs\clear\classes\ClearClass;
+use bizHd\purchase\classes\PurchaseClearClass;
+use common\components\util;
+use Yii;
+
+class PurchaseClearController extends BaseController
+{
+
+    //生成结算订单 ssh 2021.1.26
+    public function actionCreateOrder()
+    {
+        $post = Yii::$app->request->post();
+        $current = time();
+        $list = ClearClass::getAllByCondition(['shopId' => $this->shopId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY], null, '*', null, true);
+        if (!empty($list)) {
+            foreach ($list as $item) {
+                $deadline = $item->deadline;
+                $deadTime = strtotime($deadline);
+                if ($current > $deadTime) {
+                    $item->status = PurchaseClearClass::STATUS_EXPIRE;
+                    $item->save();
+                } else {
+                    util::fail('您还有结算订单没有付款');
+                }
+            }
+        }
+        $post['sjId'] = $this->sjId;
+        $post['shopId'] = $this->shopId;
+        $post['type'] = 1;
+        $post['customShopAdminId'] = $this->shopAdminId;
+        $shopAdmin = $this->shopAdmin;
+        $shopAdminName = $shopAdmin['name'] ?? '';
+        $post['customShopAdminName'] = $shopAdminName;
+
+        $connection = Yii::$app->db;
+        $transaction = $connection->beginTransaction();
+        try {
+            $respond = ClearClass::addOrder($post);
+            $transaction->commit();
+            util::success($respond);
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            Yii::info("下单失败原因:" . $e->getMessage());
+            util::fail('下单失败');
+        }
+    }
+
+}

+ 144 - 0
biz-ghs/clear/classes/ClearClass.php

@@ -0,0 +1,144 @@
+<?php
+
+namespace bizGhs\clear\classes;
+
+use biz\ghs\classes\GhsClass;
+use biz\shop\classes\ShopClass;
+use bizGhs\base\classes\BaseClass;
+use bizGhs\custom\classes\CustomClass;
+use bizGhs\order\classes\PurchaseOrderClass;
+use common\components\dict;
+use common\components\orderSn;
+use common\components\util;
+use Yii;
+
+class ClearClass extends BaseClass
+{
+
+    public static $baseFile = '\bizGhs\clear\models\Clear';
+
+    const STATUS_AWAIT_PAY = 1;
+    const STATUS_HAS_PAY = 2;
+    const STATUS_EXPIRE = 3;
+
+    public static function addOrder($data)
+    {
+        $orderSn = orderSn::getPurchaseClearSn();
+        $idJson = $data['id'];
+        $idArr = json_decode($idJson, true);
+        $arr = array_column($idArr, 'id');
+        if (empty($arr)) {
+            util::fail('请选择采购单');
+        }
+        $purchaseList = PurchaseOrderClass::getAllByCondition(['id' => ['in', $arr]], null, ['id', 'actPrice', 'realPrice', 'debt', 'ghsId'], null, true);
+        if (empty($purchaseList)) {
+            util::fail('请选择采购单');
+        }
+        $totalPrice = 0;
+        $currentGhsId = $data['ghsId'] ?? 0;
+        foreach ($purchaseList as $purchase) {
+            if ($purchase->debt != PurchaseOrderClass::DEBT_YES) {
+                util::fail('请选择有欠款的采购单');
+            }
+            if ($purchase->ghsId != $currentGhsId) {
+                util::fail('请选择同个供货商的订单');
+            }
+            $totalPrice = bcadd($totalPrice, $purchase->realPrice, 2);
+        }
+        $data['actPrice'] = $totalPrice;
+        $data['prePrice'] = $totalPrice;
+        $data['orderSn'] = $orderSn;
+        $data['purchaseIds'] = implode(',', $arr);
+        $deadTime = time() + 120;
+        $data['deadline'] = date("Y-m-d H:i:s", $deadTime);
+        $data['status'] = self::STATUS_AWAIT_PAY;
+        $order = self::add($data, true);
+        $payWay = dict::getDict('payWay', 'wxPay');
+        self::complete($order, $payWay);
+        return $order;
+    }
+
+
+    //支付成功后的流程 shish 2021.4.28
+    public static function complete($order, $payWay)
+    {
+        if (empty($order)) {
+            util::fail('没有找到订单');
+        }
+        if (isset($order->status) == false || $order->status != self::STATUS_AWAIT_PAY) {
+            util::fail('订单不是待付款状态');
+        }
+        $order->status = self::STATUS_HAS_PAY;
+        $order->save();
+
+        $clearId = $order->id;
+        $purchaseIds = $order->purchaseIds;
+        $arr = explode(',', $purchaseIds);
+        if (empty($arr)) {
+            $msg = "没有找到采购订单";
+            util::fail($msg);
+        }
+        $list = PurchaseOrderClass::getAllByCondition(['id' => ['in', $arr]], null, '*', null, true);
+        if (empty($list)) {
+            $msg = "没有找到采购订单!";
+            util::fail($msg);
+        }
+
+        //供货商欠款变更
+        $ghsId = $order->ghsId;
+        $ghs = GhsClass::getLockById($ghsId);
+        if (empty($ghs)) {
+            util::fail('没有找到供货商信息');
+        }
+        $currentAmount = bcsub($ghs->debtAmount, $order->actPrice, 2);
+        $ghs->debtAmount = $currentAmount;
+        $ghs->debtNum -= count($arr);
+        $ghs->debt = $currentAmount > 0 ? 2 : 1;
+        $ghs->save();
+
+        $shopId = $ghs->shopId;
+        $shop = ShopClass::getLockById($shopId);
+        if (empty($shop)) {
+            util::fail('没有找到供货商门店');
+        }
+        //供货商应收客户款减少
+        $currentGathering = bcsub($shop->mayGathering, $order->actPrice, 2);
+        $shop->mayGathering = $currentGathering;
+        $shop->save();
+
+        //采购单状态变更
+        foreach ($list as $purchase) {
+            $purchase->debt = PurchaseOrderClass::DEBT_NO;
+            $purchase->payWay = $payWay;
+            $purchase->clearId = $clearId;
+            $purchase->save();
+        }
+
+        //供货商的客户欠款变化
+        $customId = $ghs->customId;
+        $custom = CustomClass::getLockById($customId);
+        if (empty($custom)) {
+            util::fail('没有找到客户信息');
+        }
+        $remain = bcsub($custom->debtAmount, $order->actPrice, 2);
+        $custom->debtAmount = $remain;
+        $custom->debtNum -= count($arr);
+        $custom->isDebt = $remain > 0 ? 1 : 0;
+        $custom->save();
+        if ($custom->debtNum == 0) {
+            $shop->mayGatheringNum -= 1;
+            $shop->save();
+        }
+
+        //客户应付供货商款减少
+        $customShopId = $custom->shopId;
+        $customShop = ShopClass::getLockById($customShopId);
+        if (empty($customShop)) {
+            util::fail('没有找到客户门店');
+        }
+        $currentMayPay = bcsub($customShop->mayPay, $order->actPrice, 2);
+        $customShop->mayPay = $currentMayPay;
+        $customShop->save();
+    }
+
+}

+ 13 - 0
biz-ghs/clear/models/Clear.php

@@ -0,0 +1,13 @@
+<?php
+namespace bizGhs\clear\models;
+use bizGhs\base\models\Base;
+
+class Clear extends Base
+{
+
+    public static function tableName()
+    {
+        return 'xhClear';
+    }
+
+}