DeclarationUpgradeForm.php 26 KB

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