'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 * @throws Exception * @throws \yii\db\Exception */ public function add($allData){ if(!$this->validate()){ return null; } // 首购单 if($this->type == self::TYPE_ZC){ //报单商品及PV判断 $decLevelConfig = Cache::getDecLevelConfig(); $decLevel = $decLevelConfig[$this->decLv]; $toDecLevel = $this->decLv; if(!$this->decLv){ throw new Exception('Please select upgrade level');//请选择升级级别 } $baseInfo = Info::baseInfoZhByUserName($this->insertUserName); $userId = $baseInfo['ID']; $userDecPvSum = User::sumDevPvByUserId($userId); // 用户所有报单PV总和 if ($userDecPvSum != $this->nowPerf) { throw new Exception('Please contact the customer service personnel to check the performance of upgraded members');//请联系客服人员核对升级会员业绩 } // 获取用户是否是观察期 $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('The upgrade method is incorrect. Please contact the customer service personnel');//升级方式不正确,请联系客服人员 } 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; $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0); // 汇率 foreach ($this->goodsNum as $k => $v) { if ($v) { $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1',[':ID'=> $ids[$k]]); if($goods['STORE_NUMS']>0){ $totalAmount += $goods['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' => $goods['SELL_PRICE'], 'REAL_PRICE' => $goods['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' => $exchangeRate, 'TAX_RATE' => $goods['TAX_RATE'], ]; } } } // 这里特殊是用户原报单PV之和+用户购买的商品总PV $checkPv = $totalPv + $diffPerf; if($checkPv < $decLevel['PERF']) { throw new Exception('Total PV cannot be less than the selected level PV');//总PV不能小于所选级别PV } foreach ($decLevelConfig as $key=>$val){ if($checkPv>=$val['PERF']){ $toDecLevel = $key; } } if($this->decLv!=$toDecLevel){ throw new Exception('The total PV cannot exceed the PV value of the next level under the selected level');//总PV不能超过已选级别下一个级别的PV值 } $this->_decAmount = $totalAmount; $this->_decPv = $totalPv; $this->_decAmountStandard = $totalAmountStandard; $this->_standardAmount = $this->_decAmountStandard; } $baseInfo = Info::baseInfoZhByUserName($this->insertUserName); $this->_insertUserId = $baseInfo['ID']; // 被报单人,通过insername 查找用户id $insertConId = $baseInfo['CON_UID']; $insertRecId = $baseInfo['REC_UID']; $decResult = $this->addDecOrder($insertConId, $insertRecId, $baseInfo['DEC_LV'], $isObserve, $this->remark); if (!$decResult) { throw new Exception("operation failed");//操作失败 } return $decResult; } return true; } /** * 添加报单订单 * @throws Exception|\Throwable */ public function addDecOrder($insertConId, $insertRecId, $oriDecLv, $isObserve, $remark = ''): ApproachOrder { $warehouse = Region::getWarehouseByCode($this->province);//仓库 if(!$warehouse){ throw new Exception('Delivery is temporarily not supported in the region. Contact customer service for details');//地区暂时不支持配送,具体联系客服 } $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 ApproachDecOrder(); $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())); } $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0); $orderModel = new ApproachOrder(); $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 = 0; $orderModel->PAY_TYPE = 'pay_stack'; $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->LGA_NAME = $this->lgaName; $orderModel->CITY_NAME = $this->cityName; $orderModel->ADDRESS = $this->address; $orderModel->WAREHOUSE = $warehouse; $orderModel->STATUS = \Yii::$app->params['orderStatus']['notPaid']['value']; $orderModel->CREATED_AT = Date::nowTime(); $orderModel->CREATE_USER = Info::getUserNameByUserId(\Yii::$app->user->id); $orderModel->EMAIL = Info::getUserEmailByUserId(\Yii::$app->user->id) ?? ''; $orderModel->ORDER_AMOUNT_STANDARD = $this->_decAmountStandard; $orderModel->PAY_AMOUNT_STANDARD = $this->_standardAmount; $orderModel->EXCHANGE_RATE = $exchangeRate; if(!$orderModel->save()){ $transaction->rollBack(); throw new Exception(Form::formatErrorsForApi($orderModel->getErrors())); } 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");//"商品".$goods['GOODS_NAME']."库存不足" } } else { throw new Exception("product".$goods['GOODS_NAME']."Removed from the shelf");// "商品".$goods['GOODS_NAME']."已下架" } } } // 加入商品到订单商品表 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); } ApproachOrderGoods::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->PROVINCE = $this->province; $addressModel->LGA_NAME = $this->lgaName; $addressModel->CITY_NAME = $this->cityName; $addressModel->ADDRESS = $this->address; $addressModel->IS_DEFAULT = 0; if (!$addressModel->save()) { $transaction->rollBack(); throw new Exception(Form::formatErrorsForApi($addressModel->getErrors())); } $transaction->commit(); return $orderModel; } catch(Exception $e) { $transaction->rollBack(); throw new Exception($e->getMessage()); } } /** * 判断报单中心是否存在 * @param $attribute * @return false|void */ 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, 'Entry center 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, declaration Center' . $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; } }