DeclarationUpgradeForm.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Cache;
  5. use common\helpers\Date;
  6. use common\helpers\Form;
  7. use common\helpers\LoggerTool;
  8. use common\helpers\Tool;
  9. use common\helpers\user\Cash;
  10. use common\helpers\user\Info;
  11. use common\models\Config;
  12. use common\models\Countries;
  13. use common\models\CurrencyConversions;
  14. use common\models\DeclarationLevel;
  15. use common\models\DeclarationPackage;
  16. use common\models\DecLevelLog;
  17. use common\models\DecOrder;
  18. use common\models\EmployLevel;
  19. use common\models\Order;
  20. use common\models\OrderGoods;
  21. use common\models\Period;
  22. use common\models\ReceiveAddress;
  23. use common\models\Region;
  24. use common\models\ShopGoods;
  25. use common\models\ShopGoodsNature;
  26. use common\models\User;
  27. use common\models\UserInfo;
  28. use common\models\UserNetwork;
  29. use common\models\Instalment;
  30. use Yii;
  31. use yii\base\Exception;
  32. /**
  33. * 升级管理,进行升级
  34. */
  35. class DeclarationUpgradeForm extends Model
  36. {
  37. public $type;
  38. public $decLv;
  39. public $decWay;
  40. public $packageId;
  41. public $goodsId;
  42. public $goodsNum;
  43. public $insertUserName;
  44. public $consignee;
  45. public $acceptMobile;
  46. public $province;
  47. public $city;
  48. public $county;
  49. public $lgaName;
  50. public $cityName;
  51. public $address;
  52. public $nowPerf;
  53. public $nextPerf;
  54. public $decUserName;
  55. public $remark;
  56. // 传过来的全部数据
  57. public $allData;
  58. private $_decId;
  59. public $_insertUserId;
  60. private $_decAmount;
  61. private $_decPv;
  62. private $_orderGoods;
  63. private $_standardAmount;
  64. private $_decAmountStandard;
  65. const TYPE_ZC = 'ZC';
  66. private $_userForm = null;
  67. // 全部的安置网上级
  68. private $_tempNetworkParentUser = [];
  69. /**
  70. * @inheritdoc
  71. */
  72. public function rules()
  73. {
  74. return [
  75. [['remark','type','decLv','decWay','packageId','goodsId', 'goodsNum', 'insertUserName','consignee','acceptMobile','province',/*'city','county',*/'lgaName','cityName','address','nowPerf','nextPerf'], 'trim'],
  76. [['type','decLv','decWay','insertUserName','nowPerf','province',/*'city','county',*/'address','acceptMobile'], 'required'],
  77. [['decUserName'], 'issetDec'], // 必须是报单中心
  78. [['decWay'], 'hasProduct'],// 必须选择商品
  79. [['decLv'], 'alreadyMaxDec'], //判断要升级用户是否已经是最高级
  80. ];
  81. }
  82. public function attributeLabels()
  83. {
  84. return [
  85. 'type' => 'Upgrade type',//升级类型
  86. 'decLv' => 'Upgrade level',//升级级别
  87. 'decWay' => 'Declaration method',//报单方式
  88. 'packageId' => 'Upgrade package',//升级套餐
  89. 'goodsId' => 'Product ID',//商品ID
  90. 'goodsNum' => 'Product quantity',//商品数量
  91. 'insertUserName' => 'Member number to be upgraded',//要升级的会员编号
  92. 'consignee' => 'consignee',//收货人
  93. 'acceptMobile' => 'The Phone Number of Consignee',//收货人手机
  94. 'province' => 'Receiving Province',//收货省
  95. 'city' => 'Receiving City',//收货市
  96. 'county' => 'Receiving area / county',//收货区县
  97. 'lgaName' => 'Local Government Area',
  98. 'cityName' => 'City',
  99. 'address' => 'Receiving detailed address',//收货详细地址
  100. ];
  101. }
  102. /**
  103. * 添加报单
  104. * @param $allData
  105. * @return bool|null
  106. * @throws Exception
  107. * @throws \yii\db\Exception
  108. */
  109. public function add($allData){
  110. if(!$this->validate()){
  111. return null;
  112. }
  113. $loginUserId = \Yii::$app->user->id;
  114. $hasInstalment = 0;
  115. // 首购单
  116. if($this->type == self::TYPE_ZC){
  117. //报单商品及PV判断
  118. $decLevelConfig = Cache::getDecLevelConfig();
  119. $decLevel = $decLevelConfig[$this->decLv];
  120. $toDecLevel = $this->decLv;
  121. if(!$this->decLv){
  122. throw new Exception(Yii::t('app', 'pleaseSelectUpgradeLevel'));
  123. }
  124. $baseInfo = Info::baseInfoZhByUserName($this->insertUserName);
  125. $userId = $baseInfo['ID'];
  126. $userDecPvSum = User::sumDevPvByUserId($userId); // 用户所有报单PV总和
  127. if ($userDecPvSum != $this->nowPerf) {
  128. throw new Exception(Yii::t('app', 'checkPerformanceOfUpgradedMember'));
  129. }
  130. // 获取用户是否是观察期
  131. $observe = Config::getConfigByType('observe'); // 获取观察期配置信息
  132. $observeLimit = $observe['observePeriodLimit']['value']; // 月份限制
  133. $isObserve = User::checkIsObserve($baseInfo['CREATED_AT'], $observeLimit); // 判断用户是否再观察期中
  134. $diffPerf = $isObserve ? $this->nowPerf : 0; // 观察期内升级要加上用户累计的PV,全额则基础PV为0,全额购买
  135. if ($this->decWay != 2) {
  136. throw new Exception(Yii::t('app', 'upgradeMethodIncorrect'));
  137. }
  138. // 报单中心汇率
  139. $decCountry = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID'];
  140. $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']);
  141. // 升级会员汇率
  142. $country = Countries::getById($baseInfo['COUNTRY_ID']);
  143. $currencyRate = CurrencyConversions::getToUSDRate($country['LOCAL_CURRENCY_ID']);
  144. if($this->decWay==1) {
  145. // 先不加套餐升级方式
  146. // $decPackage = DeclarationPackage::findOneAsArray('ID=:ID', [':ID'=>$this->packageId]);
  147. // $this->_decAmount = $decPackage['AMOUNT'];
  148. // $this->_decPv = $decPackage['PV'];
  149. // $this->_orderGoods[] = [
  150. // 'GOODS_ID' => $this->packageId,
  151. // 'PRICE' => $this->_decAmount,
  152. // 'REAL_PRICE' => $this->_decAmount,
  153. // 'PV' => $this->_decPv,
  154. // 'REAL_PV' => $this->_decPv,
  155. // 'BUY_NUMS' => 1,
  156. // 'SKU_CODE' => $decPackage['PACKAGE_NO'],
  157. // 'GOODS_TITLE' => $decPackage['PACKAGE_NAME']
  158. // ];
  159. } else {
  160. $ids = $this->goodsId;
  161. $totalAmount = 0;
  162. $totalAmountStandard = 0;
  163. $totalPv = 0;
  164. foreach ($this->goodsNum as $k => $v) {
  165. if ($v) {
  166. $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1',[':ID'=> $ids[$k]]);
  167. if (!$goods) {
  168. throw new Exception('Products does not exists!');
  169. }
  170. $goodsNature = ShopGoodsNature::findOneAsArray('GOODS_ID=:GOODS_ID AND COUNTRY_ID=:COUNTRY_ID',
  171. [':GOODS_ID' => $ids[$k], ':COUNTRY_ID' => $baseInfo['COUNTRY_ID']]);
  172. if (!$goodsNature) {
  173. throw new Exception(Yii::t('app', 'productsDoesSoldOut'));
  174. }
  175. if($goods['STORE_NUMS']>0){
  176. $totalAmount += $goodsNature['SELL_PRICE'] * intval($v);
  177. $totalAmountStandard += $goods['SELL_PRICE_STANDARD'] * intval($v);
  178. $realPriceStandard = $goods['SELL_PRICE_STANDARD'];
  179. $totalPv += $goods['PRICE_PV'] * intval($v);
  180. $this->_orderGoods[] = [
  181. 'GOODS_ID' => $goods['ID'],
  182. 'PRICE' => $goodsNature['SELL_PRICE'],
  183. 'REAL_PRICE' => $goodsNature['SELL_PRICE'],
  184. 'PV' => $goods['PRICE_PV'],
  185. 'REAL_PV' => $goods['PRICE_PV'],
  186. 'POINT' => $goods['POINT'],
  187. 'BUY_NUMS' => intval($v),
  188. 'SKU_CODE' => $goods['GOODS_NO'],
  189. 'GOODS_TITLE' => $goods['GOODS_NAME'],
  190. 'EMAIL' => Info::getUserEmailByUserId(\Yii::$app->user->id) ?? '',
  191. 'STANDARD_PRICE' => $goods['SELL_PRICE_STANDARD'],
  192. 'REAL_STANDARD_PRICE' => $realPriceStandard,
  193. 'EXCHANGE_RATE' => $currencyRate,
  194. 'TAX_RATE' => $goodsNature['TAX_RATE'],
  195. ];
  196. }
  197. if($goods['INSTALMENT']>0){ // 如果有分期付款商品,检查用户的分期付款状态
  198. if($v>1){ // 不可以购买多个
  199. throw new Exception(Yii::t('app', 'allowOnlyOne'));
  200. }
  201. // 分期的总期数
  202. $instalment = intval(Cache::getSystemConfig()['instalment']['VALUE'] ?? 3);
  203. // 分期商品的期数不能大于总分期数限制
  204. if (intval($goods['INSTALMENT']) > $instalment) {
  205. throw new Exception(Yii::t('app', 'instalmentGoodsNoError'));
  206. }
  207. $userStage = Instalment::getStage($userId);
  208. $userInstalmentInfo = Instalment::getInfo($userId);
  209. if (!$userInstalmentInfo){ // 如果没有分期付款记录
  210. throw new Exception(Yii::t('app', 'canNotBuy'));
  211. } else if ($userInstalmentInfo['ORDER_TYPE']!='BD' || (($userStage == $instalment) && ($goods['INSTALMENT'] != 1))) { // 如果分期付款记录中,不是报单,或已是最后一期
  212. throw new Exception(Yii::t('app', 'canNotBuy'));
  213. }
  214. if($userStage + 1 != $goods['INSTALMENT']){ // 若用户分期阶段+1不等于商品的分期阶段,则报异常
  215. throw new Exception(Yii::t('app', 'canNotBuy'));
  216. }
  217. if ($userStage + 1 > $instalment){ // 若用户分期阶段+1大于总分期阶段,则报异常
  218. throw new Exception(Yii::t('app', 'canNotBuy'));
  219. }
  220. $hasInstalment = $goods['INSTALMENT'];
  221. }
  222. }
  223. }
  224. // 这里特殊是用户原报单PV之和+用户购买的商品总PV
  225. $checkPv = $totalPv + $diffPerf;
  226. if ($hasInstalment){ // 如果买了分期付款商品,则不判断总pv
  227. $allData['hasInstalment'] = 1;
  228. }else if($checkPv < $decLevel['PERF']) {
  229. throw new Exception(Yii::t('app', 'totalPVLessThan'), 400);
  230. }
  231. // 会员已有PV+本次订单PV 不能多于 下一个级别所需PV
  232. $nextDecLv = DeclarationLevel::getNextDecLv($toDecLevel);
  233. if ($nextDecLv && ($checkPv >= $nextDecLv['PERF'])) {
  234. throw new Exception(Yii::t('app', 'totalPvExceedPv'), 400);
  235. }
  236. $this->_decAmount = $totalAmount;
  237. $this->_decPv = $totalPv;
  238. $this->_decAmountStandard = $totalAmountStandard;
  239. $this->_standardAmount = $this->_decAmountStandard;
  240. }
  241. //看现金余额是否充足
  242. $decCash = Cash::getAvailableBalance($loginUserId);
  243. // 转换后的余额
  244. $localCash = Tool::convertAmount($decCash, $decUserCurrencyRate, $currencyRate);
  245. if ($localCash < $this->_decAmount){
  246. throw new Exception(Yii::t('app', 'applicantCashShort'), 400);
  247. }
  248. $baseInfo = Info::baseInfoZhByUserName($this->insertUserName);
  249. $this->_insertUserId = $baseInfo['ID']; // 被报单人,通过insername 查找用户id
  250. $insertConId = $baseInfo['CON_UID'];
  251. $insertRecId = $baseInfo['REC_UID'];
  252. if(!($decResult = $this->addDecOrder($insertConId,$insertRecId, $baseInfo['DEC_LV'],$isObserve,$this->remark))) {
  253. throw new Exception(Yii::t('app', 'failed'), 400);
  254. }
  255. if ($hasInstalment){ // 如果有分期付款的商品,写入信息至分期付款表
  256. $instalmentModel = Instalment::findOne(['USER_ID'=>$userId]);
  257. if(!$instalmentModel) {
  258. $instalmentModel = new Instalment();
  259. }
  260. $instalmentModel->USER_ID = $userId;
  261. $instalmentModel->STAGE = $hasInstalment;
  262. $instalmentModel->ORDER_TYPE = 'BD';
  263. $instalmentModel->UPDATE_TIME = time();
  264. $instalmentModel->save();
  265. }
  266. }
  267. return true;
  268. }
  269. /**
  270. * 添加报单订单
  271. * @return bool|UserInfo|null
  272. * @throws \yii\db\Exception
  273. */
  274. public function addDecOrder($insertConId,$insertRecId,$oriDecLv,$isObserve,$remark=''){
  275. $warehouse = Region::getWarehouseByCode($this->province);//仓库
  276. if(!$warehouse){
  277. throw new Exception(Yii::t('app', 'deliveryTemporarilyNotSupported'), 400);
  278. }
  279. $upgradeType = $isObserve ? 1 : 2; // 1补差 2全额
  280. $periodObj = Period::instance();
  281. $nowPeriodNum = $periodObj->getNowPeriodNum();
  282. $nowCalcMonth = $periodObj->getYearMonth($nowPeriodNum);
  283. $ord = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 10);
  284. // 加入报单信息
  285. $db = \Yii::$app->db;
  286. $transaction = $db->beginTransaction();
  287. try {
  288. $decOrderModel = new DecOrder();
  289. $decOrderModel->DEC_SN = 'DS'.$ord;
  290. $decOrderModel->ORDER_SN = 'OS'.$ord;
  291. $decOrderModel->TYPE = $this->type;
  292. $decOrderModel->USER_ID = \Yii::$app->user->id; // 报单人
  293. $decOrderModel->TO_USER_ID = $this->_insertUserId; // 被报单人
  294. $decOrderModel->DEC_AMOUNT = $this->_decAmount;
  295. $decOrderModel->DEC_PV = $this->_decPv;
  296. $decOrderModel->PERIOD_NUM = $nowPeriodNum;
  297. $decOrderModel->CALC_MONTH = $nowCalcMonth;
  298. $decOrderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  299. $decOrderModel->PAID_WALLET = 'cash';
  300. $decOrderModel->CON_USER_ID = $insertConId;
  301. $decOrderModel->REC_USER_ID = $insertRecId;
  302. $decOrderModel->DEC_ID = $this->_decId;
  303. $decOrderModel->IS_DEL = 0;
  304. $decOrderModel->DETAIL_TYPE = 2;
  305. $decOrderModel->CREATED_AT = Date::nowTime();
  306. $decOrderModel->UPGRADE_TYPE = $upgradeType;
  307. $decOrderModel->REMARK = $remark;
  308. $decOrderModel->ORI_LV = $oriDecLv; // 变更前的级别
  309. $decOrderModel->UPGRADE_LV = $this->decLv; // 变更后的级别
  310. if(!$decOrderModel->save()){
  311. $transaction->rollBack();
  312. throw new Exception(Form::formatErrorsForApi($decOrderModel->getErrors()));
  313. }
  314. // 升级会员
  315. $userCountry = User::getEnCodeInfo($this->_insertUserId);
  316. $userCurrencyRate = CurrencyConversions::getToUSDRate($userCountry['LOCAL_CURRENCY_ID']);
  317. // 报单中心汇率
  318. $decCountry = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID'];
  319. $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']);
  320. $orderModel = new Order();
  321. $orderModel->SN = 'OS'.$ord;
  322. $orderModel->DEC_SN = 'DS'.$ord;
  323. $orderModel->ORDER_TYPE = $this->type;
  324. $orderModel->USER_ID = $this->_insertUserId;
  325. $orderModel->USER_NAME = $this->insertUserName; // 要升级的用户
  326. $orderModel->ORDER_AMOUNT = $this->_decAmount;
  327. $orderModel->PV = $this->_decPv;
  328. $orderModel->PAY_AMOUNT = $this->_decAmount;
  329. $orderModel->PAY_PV = $this->_decPv;
  330. $orderModel->PAY_AT = Date::nowTime();
  331. $orderModel->PAY_TYPE = 'cash';
  332. $orderModel->PERIOD_NUM = $nowPeriodNum;
  333. $orderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  334. $orderModel->FREIGHT = 0;
  335. $orderModel->PAY_FREIGHT = 0;
  336. $orderModel->CONSIGNEE = $this->consignee;
  337. $orderModel->MOBILE = $this->acceptMobile;
  338. $orderModel->PROVINCE = $this->province;
  339. // $orderModel->CITY = $this->city;
  340. // $orderModel->COUNTY = intval($this->county) ?? 0;
  341. $orderModel->LGA_NAME = $this->lgaName;
  342. $orderModel->CITY_NAME = $this->cityName;
  343. $orderModel->ADDRESS = $this->address;
  344. $orderModel->WAREHOUSE = $warehouse;
  345. $orderModel->STATUS = 1;
  346. $orderModel->CREATED_AT = Date::nowTime();
  347. $orderModel->CREATE_USER = Info::getUserNameByUserId(\Yii::$app->user->id);
  348. $orderModel->ORDER_AMOUNT_STANDARD = $this->_decAmountStandard;
  349. $orderModel->PAY_AMOUNT_STANDARD = $this->_standardAmount;
  350. $orderModel->EXCHANGE_RATE = $userCurrencyRate;
  351. $orderModel->COUNTRY_ID = $userCountry['COUNTRY_ID'];
  352. $orderModel->CURRENCY_ID = $userCountry['LOCAL_CURRENCY_ID'] ?? 0;
  353. if(!$orderModel->save()){
  354. $transaction->rollBack();
  355. throw new Exception(Form::formatErrorsForApi($orderModel->getErrors()));
  356. }
  357. /**
  358. * 2022-05-10
  359. * York
  360. */
  361. foreach ($this->goodsNum as $k => $v) {
  362. if ($v) {
  363. $goods = ShopGoods::findOneAsArray('ID=:ID',[':ID'=> $this->goodsId[$k]]);
  364. $storenums = $goods['STORE_NUMS'] - $this->goodsNum[$k];
  365. if($goods['STATUS']==1){
  366. if($goods['STORE_NUMS'] >=$this->goodsNum[$k]){
  367. $data = ShopGoods::find()->where(['ID' => $this->goodsId[$k]])->one();
  368. $data->STORE_NUMS = $storenums;
  369. $data->update();
  370. if ($storenums==0){
  371. $data->STATUS = 0;
  372. $data->UPDATED_AT = Date::nowTime();
  373. $data->update();
  374. }
  375. }else{
  376. throw new Exception("product".$goods['GOODS_NAME']."Insufficient inventory");
  377. }
  378. }else{
  379. throw new Exception("product".$goods['GOODS_NAME']."Removed from the shelf");
  380. }
  381. }
  382. }
  383. // 加入商品到订单商品表
  384. foreach($this->_orderGoods as $key=>$value){
  385. $this->_orderGoods[$key]['ORDER_SN'] = $orderModel->SN;
  386. $this->_orderGoods[$key]['P_CALC_MONTH'] = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  387. }
  388. OrderGoods::batchInsert($this->_orderGoods);
  389. //写入收货地址信息
  390. $addressModel = new ReceiveAddress();
  391. $addressModel->USER_ID = $this->_insertUserId;
  392. $addressModel->USER_NAME = $this->insertUserName;
  393. $addressModel->CONSIGNEE = $this->consignee;
  394. $addressModel->MOBILE = $this->acceptMobile;
  395. $addressModel->COUNTRY_ID = $userCountry['COUNTRY_ID'];
  396. $addressModel->PROVINCE = $this->province;
  397. // $addressModel->CITY = $this->city;
  398. // $addressModel->COUNTY = intval($this->county) ?? 0;
  399. $addressModel->LGA_NAME = $this->lgaName;
  400. $addressModel->CITY_NAME = $this->cityName;
  401. $addressModel->ADDRESS = $this->address;
  402. $addressModel->IS_DEFAULT = 0;
  403. // print_r($addressModel);
  404. if(!$addressModel->save()){
  405. $transaction->rollBack();
  406. throw new Exception(Form::formatErrorsForApi($addressModel->getErrors()));
  407. }
  408. // 扣报单人现金钱包
  409. $decAmount = Tool::convertAmount($this->_decAmount, $userCurrencyRate, $decUserCurrencyRate);
  410. Cash::changeUserCash(\Yii::$app->user->id, 'CASH', -abs($decAmount), ['REMARK' =>'by'.$this->insertUserName.'Upgrade entry', 'ORDER_SN' => $orderModel->SN]);
  411. // 为被升级人进行升级操作
  412. $decLevelLog = new DecLevelLog();
  413. $decLog = [
  414. 'userId' => $this->_insertUserId,//会员ID
  415. 'fromId' => $oriDecLv, // 变动前的级别
  416. 'levelId' => $this->decLv,// 变动后的级别
  417. 'actionId' => \Yii::$app->user->id,
  418. 'remark' => $this->remark,
  419. 'lvPv' => $this->_decPv
  420. ];
  421. $modifyDecLv = $decLevelLog->frontendChange($decLog);
  422. if (empty($modifyDecLv)) {
  423. $transaction->rollBack();
  424. throw new Exception(Yii::t('app', 'failedToUpgrade'), 400);
  425. }
  426. $transaction->commit();
  427. } catch(Exception $e) {
  428. $transaction->rollBack();
  429. throw new Exception($e->getMessage());
  430. return false;
  431. }
  432. return $modifyDecLv;
  433. }
  434. /**
  435. * 判断报单中心是否存在
  436. * @param $attribute
  437. */
  438. public function issetDec($attribute){
  439. $decUser = User::find()
  440. ->select('ID')
  441. ->where('IS_DEC=1 AND USER_NAME=:USER_NAME', [':USER_NAME' => $this->decUserName])
  442. ->asArray()
  443. ->one();
  444. if (!$decUser) {
  445. $this->addError($attribute, 'Stockist does not exist');//报单中心不存在
  446. return false;
  447. } else {
  448. // 判断报单中心是否在新加入会员的安置网上级中
  449. // $this->loopFindParentToNetwork($this->insertUserName);
  450. // //反转数组,in_array搜索错误
  451. // //in_array($this->decUserName, $this->_tempNetworkParentUser[$this->insertUserName]);
  452. // $flipParent = array_flip(array_filter($this->_tempNetworkParentUser[$this->insertUserName]));
  453. // if (!isset($flipParent[$this->decUserName])) {
  454. //// $this->addError($attribute, '为' . $this->insertUserName . '升级报单,报单中心' . $this->decUserName . '不在' . $this->insertUserName . '的安置网上级中');
  455. // $this->addError($attribute, 'by' . $this->insertUserName . 'Upgrade declaration, Stockist' . $this->decUserName . 'be not in' . $this->insertUserName . 'Online level of resettlement');
  456. // return ;
  457. // }
  458. $this->_decId = $decUser['ID'];
  459. }
  460. }
  461. // 判断是否已选择商品或套餐
  462. public function hasProduct($attribute) {
  463. if ($this->decWay==1 && empty($this->packageId)) {
  464. $this->addError($attribute, 'Purchase package upgrade, please select package');//购买套餐升级,请选择套餐
  465. return false;
  466. }
  467. if ($this->decWay!=1 && empty($this->goodsId)) {
  468. $this->addError($attribute, 'Purchase product upgrade, please select product');//购买商品升级,请选择商品'
  469. return false;
  470. }
  471. return true;
  472. }
  473. // 判断要升级的会员,是否已是最高级别
  474. public function alreadyMaxDec($attribute) {
  475. $baseInfo = Info::baseInfoZhByUserName($this->insertUserName);
  476. $userDecId = $baseInfo['DEC_LV'];// 用户当前的级别
  477. $maxPerfInfo = DeclarationLevel::getMaxDecPref();
  478. $maxDecId = $maxPerfInfo['ID']; // 级别配置中最高级别ID
  479. if ($maxDecId == $userDecId) {
  480. $this->addError($attribute, 'It is already the highest level and no upgrade is required');//已是最高级别,无需升级
  481. return false;
  482. }
  483. return true;
  484. }
  485. /**
  486. * 寻找被升级人的上级
  487. * @param null $conUserName
  488. * @return bool
  489. */
  490. private function loopFindParentToNetwork($conUserName = null) {
  491. if($conUserName == null ){
  492. $conUserName = $this->insertUserName;
  493. }
  494. $baseUser = Info::getBaseUserByUserName($conUserName);
  495. $userNetworkInfo = UserNetwork::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $baseUser['ID']]);
  496. unset($baseUser);
  497. $allParentUserIdsArr = explode(',', $userNetworkInfo['PARENT_UIDS']);
  498. unset($userNetworkInfo);
  499. $allParentUserIds = array_reverse($allParentUserIdsArr);
  500. unset($allParentUserIdsArr);
  501. if($allParentUserIds){
  502. foreach($allParentUserIds as $parentUserId) {
  503. $parentBaseUser = Info::getBaseUserById($parentUserId);
  504. if (is_null($parentBaseUser)) continue;
  505. $this->_tempNetworkParentUser[$this->insertUserName][] = $parentBaseUser['USER_NAME'] ;
  506. unset($parentUserId, $parentBaseUser);
  507. }
  508. }
  509. unset($allParentUserIds);
  510. return true;
  511. }
  512. // /**
  513. // * 删单
  514. // * @return bool
  515. // * @throws \yii\db\Exception
  516. // */
  517. // public function delete(){
  518. // if(!$this->validate()){
  519. // return false;
  520. // }
  521. // $transaction = \Yii::$app->db->beginTransaction();
  522. // try {
  523. // $oneOrder = $this->_oneOrder;
  524. // // 首购单要删除会员
  525. // if($this->type == self::TYPE_ZC){
  526. // UserInfo::deleteUser($oneOrder['TO_USER_ID']);
  527. // }
  528. // // 如果是复销单的话,还需要考虑给会员的复销池减去金额
  529. // elseif($this->type == self::TYPE_FX){
  530. // Reconsume::changePoolPV($oneOrder['TO_USER_ID'], -abs($oneOrder['DEC_PV']), ['REMARK'=>'删单扣除', 'DEAL_TYPE'=>Reconsume::TYPE_AUDIT_PV]);
  531. // }
  532. // $transaction->commit();
  533. // } catch (Exception $e) {
  534. // $transaction->rollBack();
  535. // $this->addError('delete', $e->getMessage());
  536. // return false;
  537. // }
  538. // return true;
  539. // }
  540. }