Jelajahi Sumber

供货商的采购单优化

shish 5 tahun lalu
induk
melakukan
795b9fa7fc

+ 26 - 0
app-ghs/controllers/PurchaseController.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace ghs\controllers;
+
+use biz\ghs\classes\GhsClass;
+use bizGhs\order\classes\PurchaseOrderClass;
+use bizGhs\order\services\PurchaseOrderService;
+use common\components\util;
+use Yii;
+
+class PurchaseController extends BaseController
+{
+
+    //待结款明细 ssh 2021.1.26
+    public function actionDebtList()
+    {
+        $id = Yii::$app->request->get('id', 0);
+        $info = GhsClass::getGhsInfo($id);
+        GhsClass::valid($info, $this->shopId);
+        $where = ['ghsId' => $id, 'debt' => PurchaseOrderClass::DEBT_YES];
+        $respond = PurchaseOrderService::getDebtList($where);
+        $respond['ghsInfo'] = $info;
+        util::success($respond);
+    }
+
+}

+ 9 - 4
app-ghs/controllers/PurchaseOrderController.php

@@ -56,13 +56,18 @@ class PurchaseOrderController extends BaseController
             if ($bigNum <= 0 && $smallNum <= 0) {
                 util::fail('花材数量不能为0');
             }
-            $weight += $v['weight'];
-
+            if (getenv('YII_ENV', 'local') == 'production') {
+                $currentWeight = $v['weight'] ?? 0;
+            } else {
+                $currentWeight = 0.6;
+            }
+            $weight = bcadd($weight, $currentWeight, 2);
         }
-        if($weight <= 0 ){
-            util::fail('花材重量不能为0');
+        if ($weight <= 0) {
+            util::fail('请填写重量');
         }
         $post['itemInfo'] = $ghsItemInfo;
+        $post['debt'] = PurchaseOrderClass::DEBT_YES;
         $order = PurchaseOrderClass::addOrder($post);
         if ($order) {
             util::success($order);

+ 54 - 12
biz-ghs/order/classes/PurchaseOrderClass.php

@@ -8,6 +8,7 @@ use biz\shop\classes\ShopClass;
 use biz\stat\classes\StatCgClass;
 use biz\stat\classes\StatOutClass;
 use bizGhs\base\classes\BaseClass;
+use bizGhs\custom\classes\CustomClass;
 use bizGhs\order\traits\OrderTrait;
 use bizGhs\stock\services\StockRecordService;
 use bizGhs\product\classes\ProductClass;
@@ -34,6 +35,10 @@ class PurchaseOrderClass extends BaseClass
         self::PURCHASE_ORDER_STATUS_COMPLETE => '已完成',
     ];
 
+    const DEBT_UNKNOWN = 0;
+    const DEBT_YES = 1;
+    const DEBT_NO = 2;
+
     //添加采购单 lqh 2021.1.19
     public static function addOrder($data)
     {
@@ -57,8 +62,12 @@ class PurchaseOrderClass extends BaseClass
                 $smallNum += $v['smallNum'] ?? 0;
                 $totalItemPrice = bcadd($totalItemPrice, $v['itemPrice'], 2);
                 $ghsItemInfoMap[$v['productId']] = $v;
-                $weight = $v['weight']??0;
-                $totalItemWeight = bcadd($totalItemWeight,$weight,2);
+                if (getenv('YII_ENV', 'local') == 'production') {
+                    $weight = $v['weight'] ?? 0;
+                } else {
+                    $weight = 0.6;
+                }
+                $totalItemWeight = bcadd($totalItemWeight, $weight, 2);
             }
         }
         $data['bigNum'] = $bigNum;
@@ -86,6 +95,9 @@ class PurchaseOrderClass extends BaseClass
         $orderPrice = bcadd($orderPrice, $pickCharge, 2);
         $orderPrice = bcadd($orderPrice, $localCharge, 2);
         $data['orderPrice'] = $orderPrice;
+        $data['prePrice'] = $orderPrice;
+        $data['actPrice'] = $orderPrice;
+        $data['realPrice'] = $orderPrice;
 
         //总运费
         $totalFreight = bcadd($packingCharge, $shortCharge, 2);
@@ -93,6 +105,10 @@ class PurchaseOrderClass extends BaseClass
         $totalFreight = bcadd($totalFreight, $pickCharge, 2);
         $totalFreight = bcadd($totalFreight, $localCharge, 2);
 
+        if ($totalFreight <= 0) {
+            util::fail('请填写运费');
+        }
+
         $connection = Yii::$app->db;
         $transaction = $connection->beginTransaction();
 
@@ -112,11 +128,12 @@ class PurchaseOrderClass extends BaseClass
             foreach ($batchData as $v) {
                 $totalItemNum = bcadd($totalItemNum, $v['itemNum'], 2);
             }
+
             //平均计算出每扎得运费:总运费/总扎数
-            $avgFreight = bcdiv($totalFreight, $totalItemNum, 2);
+            //$avgFreight = bcdiv($totalFreight, $totalItemNum, 2);
 
-            //计算每g的运费  总运费/总重量
-            $avgFreightByWeight = bcdiv($totalFreight, $totalItemWeight*1000, 2);
+            //计算每公斤的运费  总运费/总重量
+            $avgKgWeight = bcdiv($totalFreight, $totalItemWeight, 2);
 
             //现在暂时点击确定,完成 加库存
             foreach ($batchData as $v) {
@@ -144,11 +161,11 @@ class PurchaseOrderClass extends BaseClass
                 // 每扎采购价+每扎运费 = 每扎成本价
                 //$avgCost = bcadd($avgItemCost, $avgFreight, 2);
                 //2021-06-11 修改每扎运费计算
-                $itemInfoTmp = json_decode($v['itemInfo'],true);
+                $itemInfoTmp = json_decode($v['itemInfo'], true);
                 //花材的重量
-                $productWeight = $itemInfoTmp['itemWeight']??0;
-                $avgWeighFreight = bcmul($avgFreightByWeight,$productWeight*1000,2);
-                $avgCost = bcadd($avgItemCost, $avgWeighFreight, 2);
+                $productWeight = $itemInfoTmp['itemWeight'] ?? 0;
+                $productFreight = bcmul($avgKgWeight, $productWeight, 2);
+                $avgCost = bcadd($avgItemCost, $productFreight, 2);
                 //修改 ghsItem 中的成本价
                 // ItemClass::changeCost($itemId,$sjId,$avgCost);
 
@@ -156,7 +173,6 @@ class PurchaseOrderClass extends BaseClass
                 ProductClass::changeCost($productId, $avgCost);
                 //触发自动调价 2021.4.26 linqh
                 ProductClass::autoPriceById($productId);
-
             }
 
             //门店支出增加
@@ -177,6 +193,32 @@ class PurchaseOrderClass extends BaseClass
             //当天和当月支出统计
             StatOutClass::updateOrInsert($shop, $order['orderPrice']);
 
+            //供货商资产增加
+            $ghsId = $data['ghsId'] ?? 0;
+            $ghs = GhsClass::getLockById($ghsId);
+            if (empty($ghs)) {
+                util::fail('没有找到供货商');
+            }
+            $ghs->debt = GhsClass::DEBT_YES;
+            $ghs->debtAmount = bcadd($ghs->debtAmount, $order['orderPrice'], 2);
+            $ghs->debtNum += 1;
+            $ghs->expendAmount = bcadd($ghs->expendAmount, $order['orderPrice'], 2);
+            $ghs->expendNum += 1;
+            $ghs->save();
+
+            //客户资产增加
+            $customId = $ghs->customId;
+            $custom = CustomClass::getLockById($customId);
+            if (empty($custom)) {
+                util::fail('没有找到客户信息');
+            }
+            $custom->isDebt = CustomClass::IS_DEBT_YES;
+            $custom->debtAmount = bcadd($custom->debtAmount, $order['orderPrice'], 2);
+            $custom->debtNum += 1;
+            $custom->buyNum += 1;
+            $custom->buyAmount = bcadd($custom->buyAmount, $order['orderPrice'], 2);
+            $custom->save();
+
             //支出流水
             $payWay = dict::getDict('payWay', 'unknown');
             $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
@@ -292,8 +334,8 @@ class PurchaseOrderClass extends BaseClass
                 'rank' => $rank,// 花材级别
                 'bigUnit' => $productData[$productId]['bigUnit'],//大单位名称 扎
                 'smallUnit' => $productData[$productId]['smallUnit'],//小单位名称 支
-                'purchaseWeight' => $v['weight']??0, //采购重量
-                'itemWeight'=>$productData[$productId]['weight']??0,
+                'purchaseWeight' => $v['weight'] ?? 0, //采购重量
+                'itemWeight' => $productData[$productId]['weight'] ?? 0,
             ];
             $tmp['itemInfo'] = json_encode($itemInfo);
             $batchData[] = $tmp;

+ 8 - 0
biz-ghs/order/services/PurchaseOrderService.php

@@ -3,6 +3,7 @@
 namespace bizGhs\order\services;
 
 use bizGhs\base\services\BaseService;
+use bizGhs\order\classes\PurchaseOrderClass;
 use Yii;
 
 class PurchaseOrderService extends BaseService
@@ -10,5 +11,12 @@ class PurchaseOrderService extends BaseService
 
     public static $baseFile = '\bizGhs\order\classes\PurchaseOrderClass';
 
+    //所有的欠款采购单 ssh 2021.1.26
+    public static function getDebtList($where)
+    {
+        $list = PurchaseOrderClass::getAllList('*', $where, 'addTime DESC');
+        $count = PurchaseOrderClass::getCount($where);
+        return ['list' => $list, 'debtNum' => $count];
+    }
 
 }

+ 3 - 0
biz/ghs/classes/GhsClass.php

@@ -14,6 +14,9 @@ use biz\base\classes\BaseClass;
 class GhsClass extends BaseClass
 {
 
+    const DEBT_NO = 1;
+    const DEBT_YES = 2;
+
     public static $baseFile = '\biz\ghs\models\Ghs';
 
     //新增客户和供货商关系 shish 2021.4.13