Просмотр исходного кода

bugfix: EK-1569: 报单时无法使用PayStack支付.

kevinElken 8 месяцев назад
Родитель
Сommit
26a1f9804d
1 измененных файлов с 131 добавлено и 36 удалено
  1. 131 36
      common/models/forms/ApproachReconsumeOrderForm.php

+ 131 - 36
common/models/forms/ApproachReconsumeOrderForm.php

@@ -7,6 +7,7 @@ use common\components\Model;
 use common\helpers\Form;
 use common\helpers\LoggerTool;
 use common\helpers\PayStack;
+use common\helpers\Tool;
 use common\helpers\user\Balance;
 use common\helpers\user\Cash;
 use common\helpers\user\Info;
@@ -16,18 +17,23 @@ use common\models\ApproachOrder;
 use common\models\ApproachReconsumeOrder;
 use common\models\ApproachOrderGoods;
 use common\models\BaUser;
+use common\models\Countries;
+use common\models\CurrencyConversions;
 use common\models\DealType;
 use common\models\DecLevelLog;
 use common\models\DecOrder;
+use common\models\FreeTemplate;
 use common\models\Order;
 use common\models\OrderGoods;
 use common\models\Period;
 use common\models\ReceiveAddress;
 use common\models\Region;
 use common\models\ShopGoods;
+use common\models\ShopGoodsNature;
 use common\models\User;
 use common\models\UserNetwork;
 use common\models\Instalment;
+use common\models\UserPerformance;
 use Yii;
 use yii\base\Exception;
 
@@ -52,6 +58,7 @@ class ApproachReconsumeOrderForm extends Model
     public $email;
 
     public $userName;
+    public $decUserName;
     public $consignee;
     public $acceptMobile;
     public $province;
@@ -412,42 +419,78 @@ class ApproachReconsumeOrderForm extends Model
         $ids = $this->goodsId;
         $totalAmount = 0;
         $totalPv = 0;
+        $totalRealPv = 0;
         $totalAmountStandard = 0;
-        $goodsType = ShopGoods::GOODS_TYPE;
+
+        $goodsType = ShopGoods::getGoodType();
         $hasInstalment = 0;
-        $loginUserId = \Yii::$app->user->id;
-        $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0); // 汇率
+        $userId = Info::getUserIdByUserName($this->userName);
+        $user = User::getEnCodeInfo($userId);
+
+        $decUserID = Info::getUserIdByUserName($this->decUserName);
+        $stockist_user = User::getEnCodeInfo($decUserID);
+        if(!$stockist_user || $stockist_user['IS_DEC'] != 1){
+            throw new Exception(Yii::t('app', 'stockistDoesNotExist'));
+        }
+
+        // 报单中心汇率
+        $decCountryId = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID'];
+        $decCountry = Countries::getById($decCountryId);
+        $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']);
+        // 会员汇率
+        $country = Countries::getById($user['COUNTRY_ID']);
+        $currencyRate = CurrencyConversions::getToUSDRate($country['LOCAL_CURRENCY_ID']);
+        //判断是否是报单中心
+        $loginUser = User::getEnCodeInfo(\Yii::$app->user->id);
+        if($loginUser['IS_DEC'] == 1){
+            if($decUserID != \Yii::$app->user->id){
+                throw new Exception(Yii::t('app', 'decUserNameIsWrong'));
+            }
+        }
+
+
         foreach ($this->goodsNum as $k => $v) {
             if ($v) {
                 $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1',[':ID'=> $ids[$k]]);
                 if (!$goods) {
                     throw new Exception(Yii::t('app', 'productsDoesSoldOut'));
                 }
+                $goodsNature = ShopGoodsNature::findOneAsArray('GOODS_ID=:GOODS_ID AND COUNTRY_ID=:COUNTRY_ID',
+                    [':GOODS_ID' => $ids[$k], ':COUNTRY_ID' => $user['COUNTRY_ID']]);
+                if (!$goodsNature) {
+                    throw new Exception(Yii::t('app', 'productsDoesSoldOut'));
+                }
+
                 if($goods['STORE_NUMS']>0){
                     if ($goods['TYPE'] == 1 || $goods['TYPE'] == 2) {
                         $discount = $goodsType[$goods['TYPE']]['discount'];
-                        $realPrice = $goods['SELL_PRICE'] * $discount/100;
+                        $realPrice = $goodsNature['SELL_PRICE'] * $discount/100;
                         $realPv = $goods['PRICE_PV'] * $discount/100;
                         $realPriceStandard = $goods['SELL_PRICE_STANDARD'] * $discount/100;
                     } else {
                         $discount = $goods['SELL_DISCOUNT'];
-                        $realPrice = $goods['SELL_PRICE'] * $discount;
+                        $realPrice = $goodsNature['SELL_PRICE'] * $discount;
                         $realPv = $goods['PRICE_PV'] * $discount;
                         $realPriceStandard = $goods['SELL_PRICE_STANDARD'] * $discount;
                     }
+
+                    $currentPv = $goods['PRICE_PV'];
+                    $totalPv += $this->payType == 'prp' ? 0 : $realPv * intval($v);
+                    $totalRealPv += $this->payType == 'prp' ? 0 : $realPv * intval($v);
+                    $remainPv = 0;
+
                     $totalAmount += $realPrice * intval($v);
-                    $totalPv += $realPv * intval($v);
                     $totalAmountStandard += $realPriceStandard * intval($v);
 
                     $this->_orderGoods[] = [
                         'GOODS_ID' => $goods['ID'],
-                        'PRICE' => $goods['SELL_PRICE'],
-                        'PV' => $goods['PRICE_PV'],
+                        'PRICE' => $goodsNature['SELL_PRICE'],
+                        'PV' => $this->payType == 'prp' ? 0 : $currentPv, // $goods['PRICE_PV'],
                         'REAL_PRICE' => $realPrice,
-                        'REAL_PV' => $realPv,
+                        'REAL_PV' => $this->payType == 'prp' ? 0 : $realPv,
+                        'REMAIN_PV' => $this->payType == 'prp' ? 0 : $remainPv,
                         'POINT' => $goods['POINT'],
                         'BUY_NUMS' => intval($v),
-                        'TAX_RATE' => $goods['TAX_RATE'],
                         'SKU_CODE' => $goods['GOODS_NO'],
                         'GOODS_TITLE' => $goods['GOODS_NAME'],
                         'CATEGORY_TYPE' => $goods['CATEGORY_TYPE'],
@@ -455,7 +498,8 @@ class ApproachReconsumeOrderForm extends Model
                         'EMAIL' => $this->email,
                         'STANDARD_PRICE' => $goods['SELL_PRICE_STANDARD'],
                         'REAL_STANDARD_PRICE' => $realPriceStandard,
-                        'EXCHANGE_RATE' => $exchangeRate,
+                        'EXCHANGE_RATE' => $currencyRate,
+                        'TAX_RATE' => $goodsNature['TAX_RATE'],
                     ];
                 }
                 if($goods['INSTALMENT']>0){ // 如果有分期付款商品,检查用户的分期付款状态
@@ -463,20 +507,24 @@ class ApproachReconsumeOrderForm extends Model
                         throw new Exception(Yii::t('app', 'allowOnlyOne'));
                     }
 
-                    $userStage = Instalment::getStage($loginUserId);
+                    $userStage = Instalment::getStage($userId);
+                    $userInstalmentInfo = Instalment::getInfo($userId);
                     // 分期的总期数
                     $instalment = intval(Cache::getSystemConfig()['instalment']['VALUE'] ?? 3);
                     // 分期商品的期数不能大于总分期数限制
                     if (intval($goods['INSTALMENT']) > $instalment) {
                         throw new Exception(Yii::t('app', 'instalmentGoodsNoError'));
                     }
-                    if ($userStage==0){
-
-                    } else if ($userStage == $instalment) {
+                    if ($userStage == $instalment){
                         if ($goods['INSTALMENT'] != 1) {
                             throw new Exception(Yii::t('app', 'canNotBuy'));
                         }
                     }else{
+                        if($userInstalmentInfo){
+                            if($userInstalmentInfo['STAGE']>0 && $userInstalmentInfo['ORDER_TYPE']!='FX'){
+                                throw new Exception(Yii::t('app', 'canNotBuy'));
+                            }
+                        }
                         if($userStage + 1 != $goods['INSTALMENT']){ // 若用户分期阶段+1不等于商品的分期阶段,则报异常
                             throw new Exception(Yii::t('app', 'canNotBuy'));
                         }
@@ -489,38 +537,97 @@ class ApproachReconsumeOrderForm extends Model
             }
         }
 
-        // 运费.奈拉
-        $freight = floatval(Cache::getSystemConfig()['freight']['VALUE'] ?? 0);
-        // 普通商品免运费阈值.奈拉
-        $freeShipping = floatval(Cache::getSystemConfig()['freeShipping']['VALUE'] ?? 0);
+        // 运费模板
+        $freeTemplate = FreeTemplate::getByCountryId($user['COUNTRY_ID']);
+        // 运费
+        $freight = $freeTemplate['freight'] ?? 0;
+        // 普通商品免运费阈值
+        $freeShipping = $freeTemplate['free_shipping'] ?? 0;
 
         $this->_decAmount = $totalAmount;
-        $this->_decPv = $totalPv;
+        $this->_decPv = $this->payType == 'prp' ? 0 : $totalPv;
         $this->_freight = ($totalAmount >= $freeShipping) ? 0 : $freight;
-
         $this->_payAmount = $this->_decAmount + $this->_freight;
         $this->_decAmountStandard = $totalAmountStandard;
         $this->_standardAmount = $this->_decAmountStandard + $this->_freight;
 
         $db = \Yii::$app->db;
         $transaction = $db->beginTransaction();
-
         try {
+            $loginUserId = \Yii::$app->user->id;
+
+            //写入订单
+            if (!$orderResult = $this->addOrder()) {
+                throw new Exception(Form::formatErrorsForApi($orderResult->getErrors()));
+            }
+
+            //是否开启伞下会员限制
+            $isResaleUmbrella = Cache::getSystemConfig()['isResaleUmbrella']['VALUE'];
+            if($isResaleUmbrella){
+                $userId = Info::getUserIdByUserName($this->userName);
+                $userNetwork = UserNetwork::find()->where("USER_ID=:USER_ID AND INSTR(PARENT_UIDS,'{$loginUserId}')>0", ['USER_ID'=>$userId])->count();
+                if(!$userNetwork){
+                    throw new Exception($this->userName . Yii::t('app', 'doesNotYourSubMemberCanNotReconsume'));
+                }
+            }
+
+            if ($this->payType == 'prp') {
+                //看余额是否充足
+                $decCash = UserPerformance::getAmounts($loginUserId);
+            } else {
+                //看现金余额是否充足
+                $decCash = Cash::getAvailableBalance($loginUserId);
+            }
+
+            // 转换后的余额
+            $localCash = Tool::convertAmount($decCash, $decUserCurrencyRate, $currencyRate);
+            if ($localCash < $this->_decAmount){
+                throw new Exception(Yii::t('app', 'applicantPrpShort'), 400);
+            }
+
+            foreach ($this->goodsNum as $k => $v){
+                if ($v){
+                    $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1',[':ID'=> $ids[$k]]);
+                    if (!$goods) {
+                        throw new Exception(Yii::t('app', 'productsDoesSoldOut'));
+                    }
+                    if ($goods['STORE_NUMS'] >= $this->goodsNum[$k]){
+                        $data = ShopGoods::find()->where(['ID' => $ids[$k]])->one();
+                        $goods_store_nums = $data->STORE_NUMS - $this->goodsNum[$k];
+                        $data->STORE_NUMS = $goods_store_nums;
+                        $data->update();
+                        //下单后库存小于等于0 商品下架
+                        if($goods_store_nums <= 0){
+                            $data->STATUS = 0;
+                            $data->UPDATED_AT = Date::nowTime();
+                            $data->update();
+
+                        }
+                    }else{
+                        throw new Exception($goods['GOODS_NAME'] . Yii::t('app', 'insufficientInventory'));
+
+                    }
+                }
+            }
+
             //写入订单
             if (!$orderResult = $this->addOrder()) {
                 throw new Exception(Form::formatErrorsForApi($orderResult->getErrors()));
             }
+
             if ($hasInstalment){ // 如果有分期付款的商品,写入信息至分期付款表
-                $instalmentModel = Instalment::findOne(['USER_ID'=>$loginUserId]);
+                $instalmentModel = Instalment::findOne(['USER_ID'=>$userId]);
                 if(!$instalmentModel) {
                     $instalmentModel = new Instalment();
                 }
-                $instalmentModel->USER_ID = $loginUserId;
+                $instalmentModel->USER_ID = $userId;
                 $instalmentModel->STAGE = $hasInstalment;
+                $instalmentModel->ORDER_TYPE = 'FX';
                 $instalmentModel->UPDATE_TIME = time();
                 $instalmentModel->save();
             }
 
+
             $transaction->commit();
 
             return $orderResult;
@@ -585,16 +692,6 @@ class ApproachReconsumeOrderForm extends Model
         $orderModel->ORDER_AMOUNT_STANDARD = $this->_decAmountStandard;
         $orderModel->PAY_AMOUNT_STANDARD = $this->_standardAmount;
         $orderModel->EXCHANGE_RATE = $exchangeRate;
-//        if($this->_address['PROVINCE']==1){
-//            $orderModel->EXPRESS_TYPE = 1;
-//            $orderModel->CONSIGNEE = $userRealName;
-//            $orderModel->MOBILE = $userMobile;
-//            $orderModel->PROVINCE = 1;
-//            $orderModel->CITY = 1;
-//            $orderModel->COUNTY = 1;
-//            $orderModel->ADDRESS = '';
-//        }
-
         if(!$orderModel->save()){
             throw new Exception(Form::formatErrorsForApi($orderModel->getErrors()));
         }
@@ -605,8 +702,6 @@ class ApproachReconsumeOrderForm extends Model
         }
         ApproachOrderGoods::batchInsert($this->_orderGoods);
 
-        // TODO: 记录流水
-
         return $orderModel;
     }