'Upgrade type',//升级类型 'decLv' => 'Upgrade level',//升级级别 'decWay' => 'Declaration method',//报单方式 'packageId' => 'Upgrade package',//升级套餐 'goodsId' => 'Product ID',//商品ID 'goodsNum' => 'Product quantity',//商品数量 'insertUserName' => 'Member number to be upgraded',//要升级的会员编号 'consignee' => 'consignee',//收货人 'acceptMobile' => 'The Phone Number of Consignee',//收货人手机 'province' => 'Receiving Province',//收货省 'city' => 'Receiving City',//收货市 'county' => 'Receiving area / county',//收货区县 'lgaName' => 'Local Government Area', 'cityName' => 'City', 'address' => 'Receiving detailed address',//收货详细地址 ]; } /** * 添加报单 * @param $allData * @return bool|null * @throws Exception * @throws \yii\db\Exception */ public function add($allData){ if(!$this->validate()){ return null; } $loginUserId = \Yii::$app->user->id; $hasInstalment = 0; // 首购单 if($this->type == self::TYPE_ZC){ //报单商品及PV判断 $decLevelConfig = Cache::getDecLevelConfig(); $decLevel = $decLevelConfig[$this->decLv]; $toDecLevel = $this->decLv; if(!$this->decLv){ throw new Exception(Yii::t('app', 'pleaseSelectUpgradeLevel')); } $baseInfo = Info::baseInfoZhByUserName($this->insertUserName); $userId = $baseInfo['ID']; $userDecPvSum = User::sumDevPvByUserId($userId); // 用户所有报单PV总和 if ($userDecPvSum != $this->nowPerf) { throw new Exception(Yii::t('app', 'checkPerformanceOfUpgradedMember')); } // 获取用户是否是观察期 $observe = Config::getConfigByType('observe'); // 获取观察期配置信息 $observeLimit = $observe['observePeriodLimit']['value']; // 月份限制 $isObserve = User::checkIsObserve($baseInfo['CREATED_AT'], $observeLimit); // 判断用户是否再观察期中 $diffPerf = $isObserve ? $this->nowPerf : 0; // 观察期内升级要加上用户累计的PV,全额则基础PV为0,全额购买 if ($this->decWay != 2) { throw new Exception(Yii::t('app', 'upgradeMethodIncorrect')); } // 报单中心汇率 $decCountry = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID']; $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']); // 升级会员汇率 $country = Countries::getById($baseInfo['COUNTRY_ID']); $currencyRate = CurrencyConversions::getToUSDRate($country['LOCAL_CURRENCY_ID']); if($this->decWay==1) { // 先不加套餐升级方式 // $decPackage = DeclarationPackage::findOneAsArray('ID=:ID', [':ID'=>$this->packageId]); // $this->_decAmount = $decPackage['AMOUNT']; // $this->_decPv = $decPackage['PV']; // $this->_orderGoods[] = [ // 'GOODS_ID' => $this->packageId, // 'PRICE' => $this->_decAmount, // 'REAL_PRICE' => $this->_decAmount, // 'PV' => $this->_decPv, // 'REAL_PV' => $this->_decPv, // 'BUY_NUMS' => 1, // 'SKU_CODE' => $decPackage['PACKAGE_NO'], // 'GOODS_TITLE' => $decPackage['PACKAGE_NAME'] // ]; } else { $ids = $this->goodsId; $totalAmount = 0; $totalAmountStandard = 0; $totalPv = 0; foreach ($this->goodsNum as $k => $v) { if ($v) { $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1',[':ID'=> $ids[$k]]); if (!$goods) { throw new Exception('Products does not exists!'); } $goodsNature = ShopGoodsNature::findOneAsArray('GOODS_ID=:GOODS_ID AND COUNTRY_ID=:COUNTRY_ID', [':GOODS_ID' => $ids[$k], ':COUNTRY_ID' => $baseInfo['COUNTRY_ID']]); if (!$goodsNature) { throw new Exception(Yii::t('app', 'productsDoesSoldOut')); } if($goods['STORE_NUMS']>0){ $totalAmount += $goodsNature['SELL_PRICE'] * intval($v); $totalAmountStandard += $goods['SELL_PRICE_STANDARD'] * intval($v); $realPriceStandard = $goods['SELL_PRICE_STANDARD']; $totalPv += $goods['PRICE_PV'] * intval($v); $this->_orderGoods[] = [ 'GOODS_ID' => $goods['ID'], 'PRICE' => $goodsNature['SELL_PRICE'], 'REAL_PRICE' => $goodsNature['SELL_PRICE'], 'PV' => $goods['PRICE_PV'], 'REAL_PV' => $goods['PRICE_PV'], 'POINT' => $goods['POINT'], 'BUY_NUMS' => intval($v), 'SKU_CODE' => $goods['GOODS_NO'], 'GOODS_TITLE' => $goods['GOODS_NAME'], 'EMAIL' => Info::getUserEmailByUserId(\Yii::$app->user->id) ?? '', 'STANDARD_PRICE' => $goods['SELL_PRICE_STANDARD'], 'REAL_STANDARD_PRICE' => $realPriceStandard, 'EXCHANGE_RATE' => $currencyRate, 'TAX_RATE' => $goodsNature['TAX_RATE'], ]; } if($goods['INSTALMENT']>0){ // 如果有分期付款商品,检查用户的分期付款状态 if($v>1){ // 不可以购买多个 throw new Exception(Yii::t('app', 'allowOnlyOne')); } // 分期的总期数 $instalment = intval(Cache::getSystemConfig()['instalment']['VALUE'] ?? 3); // 分期商品的期数不能大于总分期数限制 if (intval($goods['INSTALMENT']) > $instalment) { throw new Exception(Yii::t('app', 'instalmentGoodsNoError')); } $userStage = Instalment::getStage($userId); $userInstalmentInfo = Instalment::getInfo($userId); if (!$userInstalmentInfo){ // 如果没有分期付款记录 throw new Exception(Yii::t('app', 'canNotBuy')); } else if ($userInstalmentInfo['ORDER_TYPE']!='BD' || (($userStage == $instalment) && ($goods['INSTALMENT'] != 1))) { // 如果分期付款记录中,不是报单,或已是最后一期 throw new Exception(Yii::t('app', 'canNotBuy')); } if($userStage + 1 != $goods['INSTALMENT']){ // 若用户分期阶段+1不等于商品的分期阶段,则报异常 throw new Exception(Yii::t('app', 'canNotBuy')); } if ($userStage + 1 > $instalment){ // 若用户分期阶段+1大于总分期阶段,则报异常 throw new Exception(Yii::t('app', 'canNotBuy')); } $hasInstalment = $goods['INSTALMENT']; } } } // 这里特殊是用户原报单PV之和+用户购买的商品总PV $checkPv = $totalPv + $diffPerf; if ($hasInstalment){ // 如果买了分期付款商品,则不判断总pv $allData['hasInstalment'] = 1; }else if($checkPv < $decLevel['PERF']) { throw new Exception(Yii::t('app', 'totalPVLessThan'), 400); } // 会员已有PV+本次订单PV 不能多于 下一个级别所需PV $nextDecLv = DeclarationLevel::getNextDecLv($toDecLevel); if ($nextDecLv && ($checkPv >= $nextDecLv['PERF'])) { throw new Exception(Yii::t('app', 'totalPvExceedPv'), 400); } $this->_decAmount = $totalAmount; $this->_decPv = $totalPv; $this->_decAmountStandard = $totalAmountStandard; $this->_standardAmount = $this->_decAmountStandard; } //看现金余额是否充足 $decCash = Cash::getAvailableBalance($loginUserId); // 转换后的余额 $localCash = Tool::convertAmount($decCash, $decUserCurrencyRate, $currencyRate); if ($localCash < $this->_decAmount){ throw new Exception(Yii::t('app', 'applicantCashShort'), 400); } $baseInfo = Info::baseInfoZhByUserName($this->insertUserName); $this->_insertUserId = $baseInfo['ID']; // 被报单人,通过insername 查找用户id $insertConId = $baseInfo['CON_UID']; $insertRecId = $baseInfo['REC_UID']; if(!($decResult = $this->addDecOrder($insertConId,$insertRecId, $baseInfo['DEC_LV'],$isObserve,$this->remark))) { throw new Exception(Yii::t('app', 'failed'), 400); } if ($hasInstalment){ // 如果有分期付款的商品,写入信息至分期付款表 $instalmentModel = Instalment::findOne(['USER_ID'=>$userId]); if(!$instalmentModel) { $instalmentModel = new Instalment(); } $instalmentModel->USER_ID = $userId; $instalmentModel->STAGE = $hasInstalment; $instalmentModel->ORDER_TYPE = 'BD'; $instalmentModel->UPDATE_TIME = time(); $instalmentModel->save(); } } return true; } /** * 添加报单订单 * @return bool|UserInfo|null * @throws \yii\db\Exception */ public function addDecOrder($insertConId,$insertRecId,$oriDecLv,$isObserve,$remark=''){ $warehouse = Region::getWarehouseByCode($this->province);//仓库 if(!$warehouse){ throw new Exception(Yii::t('app', 'deliveryTemporarilyNotSupported'), 400); } $upgradeType = $isObserve ? 1 : 2; // 1补差 2全额 $periodObj = Period::instance(); $nowPeriodNum = $periodObj->getNowPeriodNum(); $nowCalcMonth = $periodObj->getYearMonth($nowPeriodNum); $ord = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 10); // 加入报单信息 $db = \Yii::$app->db; $transaction = $db->beginTransaction(); try { $decOrderModel = new DecOrder(); $decOrderModel->DEC_SN = 'DS'.$ord; $decOrderModel->ORDER_SN = 'OS'.$ord; $decOrderModel->TYPE = $this->type; $decOrderModel->USER_ID = \Yii::$app->user->id; // 报单人 $decOrderModel->TO_USER_ID = $this->_insertUserId; // 被报单人 $decOrderModel->DEC_AMOUNT = $this->_decAmount; $decOrderModel->DEC_PV = $this->_decPv; $decOrderModel->PERIOD_NUM = $nowPeriodNum; $decOrderModel->CALC_MONTH = $nowCalcMonth; $decOrderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH); $decOrderModel->PAID_WALLET = 'cash'; $decOrderModel->CON_USER_ID = $insertConId; $decOrderModel->REC_USER_ID = $insertRecId; $decOrderModel->DEC_ID = $this->_decId; $decOrderModel->IS_DEL = 0; $decOrderModel->DETAIL_TYPE = 2; $decOrderModel->CREATED_AT = Date::nowTime(); $decOrderModel->UPGRADE_TYPE = $upgradeType; $decOrderModel->REMARK = $remark; $decOrderModel->ORI_LV = $oriDecLv; // 变更前的级别 $decOrderModel->UPGRADE_LV = $this->decLv; // 变更后的级别 if(!$decOrderModel->save()){ $transaction->rollBack(); throw new Exception(Form::formatErrorsForApi($decOrderModel->getErrors())); } // 升级会员 $userCountry = User::getEnCodeInfo($this->_insertUserId); $userCurrencyRate = CurrencyConversions::getToUSDRate($userCountry['LOCAL_CURRENCY_ID']); // 报单中心汇率 $decCountry = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID']; $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']); $orderModel = new Order(); $orderModel->SN = 'OS'.$ord; $orderModel->DEC_SN = 'DS'.$ord; $orderModel->ORDER_TYPE = $this->type; $orderModel->USER_ID = $this->_insertUserId; $orderModel->USER_NAME = $this->insertUserName; // 要升级的用户 $orderModel->ORDER_AMOUNT = $this->_decAmount; $orderModel->PV = $this->_decPv; $orderModel->PAY_AMOUNT = $this->_decAmount; $orderModel->PAY_PV = $this->_decPv; $orderModel->PAY_AT = Date::nowTime(); $orderModel->PAY_TYPE = 'cash'; $orderModel->PERIOD_NUM = $nowPeriodNum; $orderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH); $orderModel->FREIGHT = 0; $orderModel->PAY_FREIGHT = 0; $orderModel->CONSIGNEE = $this->consignee; $orderModel->MOBILE = $this->acceptMobile; $orderModel->PROVINCE = $this->province; // $orderModel->CITY = $this->city; // $orderModel->COUNTY = intval($this->county) ?? 0; $orderModel->LGA_NAME = $this->lgaName; $orderModel->CITY_NAME = $this->cityName; $orderModel->ADDRESS = $this->address; $orderModel->WAREHOUSE = $warehouse; $orderModel->STATUS = 1; $orderModel->CREATED_AT = Date::nowTime(); $orderModel->CREATE_USER = Info::getUserNameByUserId(\Yii::$app->user->id); $orderModel->ORDER_AMOUNT_STANDARD = $this->_decAmountStandard; $orderModel->PAY_AMOUNT_STANDARD = $this->_standardAmount; $orderModel->EXCHANGE_RATE = $userCurrencyRate; $orderModel->COUNTRY_ID = $userCountry['COUNTRY_ID']; $orderModel->CURRENCY_ID = $userCountry['LOCAL_CURRENCY_ID'] ?? 0; if(!$orderModel->save()){ $transaction->rollBack(); throw new Exception(Form::formatErrorsForApi($orderModel->getErrors())); } /** * 2022-05-10 * York */ foreach ($this->goodsNum as $k => $v) { if ($v) { $goods = ShopGoods::findOneAsArray('ID=:ID',[':ID'=> $this->goodsId[$k]]); $storenums = $goods['STORE_NUMS'] - $this->goodsNum[$k]; if($goods['STATUS']==1){ if($goods['STORE_NUMS'] >=$this->goodsNum[$k]){ $data = ShopGoods::find()->where(['ID' => $this->goodsId[$k]])->one(); $data->STORE_NUMS = $storenums; $data->update(); if ($storenums==0){ $data->STATUS = 0; $data->UPDATED_AT = Date::nowTime(); $data->update(); } }else{ throw new Exception("product".$goods['GOODS_NAME']."Insufficient inventory"); } }else{ throw new Exception("product".$goods['GOODS_NAME']."Removed from the shelf"); } } } // 加入商品到订单商品表 foreach($this->_orderGoods as $key=>$value){ $this->_orderGoods[$key]['ORDER_SN'] = $orderModel->SN; $this->_orderGoods[$key]['P_CALC_MONTH'] = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH); } OrderGoods::batchInsert($this->_orderGoods); //写入收货地址信息 $addressModel = new ReceiveAddress(); $addressModel->USER_ID = $this->_insertUserId; $addressModel->USER_NAME = $this->insertUserName; $addressModel->CONSIGNEE = $this->consignee; $addressModel->MOBILE = $this->acceptMobile; $addressModel->COUNTRY_ID = $userCountry['COUNTRY_ID']; $addressModel->PROVINCE = $this->province; // $addressModel->CITY = $this->city; // $addressModel->COUNTY = intval($this->county) ?? 0; $addressModel->LGA_NAME = $this->lgaName; $addressModel->CITY_NAME = $this->cityName; $addressModel->ADDRESS = $this->address; $addressModel->IS_DEFAULT = 0; // print_r($addressModel); if(!$addressModel->save()){ $transaction->rollBack(); throw new Exception(Form::formatErrorsForApi($addressModel->getErrors())); } // 扣报单人现金钱包 $decAmount = Tool::convertAmount($this->_decAmount, $userCurrencyRate, $decUserCurrencyRate); Cash::changeUserCash(\Yii::$app->user->id, 'CASH', -abs($decAmount), ['REMARK' =>'by'.$this->insertUserName.'Upgrade entry', 'ORDER_SN' => $orderModel->SN]); // 为被升级人进行升级操作 $decLevelLog = new DecLevelLog(); $decLog = [ 'userId' => $this->_insertUserId,//会员ID 'fromId' => $oriDecLv, // 变动前的级别 'levelId' => $this->decLv,// 变动后的级别 'actionId' => \Yii::$app->user->id, 'remark' => $this->remark, 'lvPv' => $this->_decPv ]; $modifyDecLv = $decLevelLog->frontendChange($decLog); if (empty($modifyDecLv)) { $transaction->rollBack(); throw new Exception(Yii::t('app', 'failedToUpgrade'), 400); } $transaction->commit(); } catch(Exception $e) { $transaction->rollBack(); throw new Exception($e->getMessage()); return false; } return $modifyDecLv; } /** * 判断报单中心是否存在 * @param $attribute */ public function issetDec($attribute){ $decUser = User::find() ->select('ID') ->where('IS_DEC=1 AND USER_NAME=:USER_NAME', [':USER_NAME' => $this->decUserName]) ->asArray() ->one(); if (!$decUser) { $this->addError($attribute, 'Stockist does not exist');//报单中心不存在 return false; } else { // 判断报单中心是否在新加入会员的安置网上级中 // $this->loopFindParentToNetwork($this->insertUserName); // //反转数组,in_array搜索错误 // //in_array($this->decUserName, $this->_tempNetworkParentUser[$this->insertUserName]); // $flipParent = array_flip(array_filter($this->_tempNetworkParentUser[$this->insertUserName])); // if (!isset($flipParent[$this->decUserName])) { //// $this->addError($attribute, '为' . $this->insertUserName . '升级报单,报单中心' . $this->decUserName . '不在' . $this->insertUserName . '的安置网上级中'); // $this->addError($attribute, 'by' . $this->insertUserName . 'Upgrade declaration, Stockist' . $this->decUserName . 'be not in' . $this->insertUserName . 'Online level of resettlement'); // return ; // } $this->_decId = $decUser['ID']; } } // 判断是否已选择商品或套餐 public function hasProduct($attribute) { if ($this->decWay==1 && empty($this->packageId)) { $this->addError($attribute, 'Purchase package upgrade, please select package');//购买套餐升级,请选择套餐 return false; } if ($this->decWay!=1 && empty($this->goodsId)) { $this->addError($attribute, 'Purchase product upgrade, please select product');//购买商品升级,请选择商品' return false; } return true; } // 判断要升级的会员,是否已是最高级别 public function alreadyMaxDec($attribute) { $baseInfo = Info::baseInfoZhByUserName($this->insertUserName); $userDecId = $baseInfo['DEC_LV'];// 用户当前的级别 $maxPerfInfo = DeclarationLevel::getMaxDecPref(); $maxDecId = $maxPerfInfo['ID']; // 级别配置中最高级别ID if ($maxDecId == $userDecId) { $this->addError($attribute, 'It is already the highest level and no upgrade is required');//已是最高级别,无需升级 return false; } return true; } /** * 寻找被升级人的上级 * @param null $conUserName * @return bool */ private function loopFindParentToNetwork($conUserName = null) { if($conUserName == null ){ $conUserName = $this->insertUserName; } $baseUser = Info::getBaseUserByUserName($conUserName); $userNetworkInfo = UserNetwork::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $baseUser['ID']]); unset($baseUser); $allParentUserIdsArr = explode(',', $userNetworkInfo['PARENT_UIDS']); unset($userNetworkInfo); $allParentUserIds = array_reverse($allParentUserIdsArr); unset($allParentUserIdsArr); if($allParentUserIds){ foreach($allParentUserIds as $parentUserId) { $parentBaseUser = Info::getBaseUserById($parentUserId); if (is_null($parentBaseUser)) continue; $this->_tempNetworkParentUser[$this->insertUserName][] = $parentBaseUser['USER_NAME'] ; unset($parentUserId, $parentBaseUser); } } unset($allParentUserIds); return true; } // /** // * 删单 // * @return bool // * @throws \yii\db\Exception // */ // public function delete(){ // if(!$this->validate()){ // return false; // } // $transaction = \Yii::$app->db->beginTransaction(); // try { // $oneOrder = $this->_oneOrder; // // 首购单要删除会员 // if($this->type == self::TYPE_ZC){ // UserInfo::deleteUser($oneOrder['TO_USER_ID']); // } // // 如果是复销单的话,还需要考虑给会员的复销池减去金额 // elseif($this->type == self::TYPE_FX){ // Reconsume::changePoolPV($oneOrder['TO_USER_ID'], -abs($oneOrder['DEC_PV']), ['REMARK'=>'删单扣除', 'DEAL_TYPE'=>Reconsume::TYPE_AUDIT_PV]); // } // $transaction->commit(); // } catch (Exception $e) { // $transaction->rollBack(); // $this->addError('delete', $e->getMessage()); // return false; // } // return true; // } }