DeclarationUpgradeForm.php 26 KB

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