ApproachDeclarationUpgradeForm.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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\user\Cash;
  8. use common\helpers\user\Info;
  9. use common\models\ApproachDecOrder;
  10. use common\models\ApproachOrder;
  11. use common\models\ApproachOrderGoods;
  12. use common\models\Config;
  13. use common\models\DeclarationLevel;
  14. use common\models\DeclarationPackage;
  15. use common\models\DecLevelLog;
  16. use common\models\DecOrder;
  17. use common\models\EmployLevel;
  18. use common\models\Order;
  19. use common\models\OrderGoods;
  20. use common\models\Period;
  21. use common\models\ReceiveAddress;
  22. use common\models\Region;
  23. use common\models\ShopGoods;
  24. use common\models\User;
  25. use common\models\UserInfo;
  26. use common\models\UserNetwork;
  27. use Yii;
  28. use yii\base\Exception;
  29. /**
  30. * 升级管理,进行升级
  31. */
  32. class ApproachDeclarationUpgradeForm extends Model
  33. {
  34. public $type;
  35. public $decLv;
  36. public $decWay;
  37. public $packageId;
  38. public $goodsId;
  39. public $goodsNum;
  40. public $insertUserName;
  41. public $consignee;
  42. public $acceptMobile;
  43. public $province;
  44. public $city;
  45. public $county;
  46. public $lgaName;
  47. public $cityName;
  48. public $address;
  49. public $nowPerf;
  50. public $nextPerf;
  51. public $decUserName;
  52. public $remark;
  53. public $payType;
  54. // 传过来的全部数据
  55. public $allData;
  56. private $_decId;
  57. public $_insertUserId;
  58. private $_decAmount;
  59. private $_decPv;
  60. private $_orderGoods;
  61. private $_standardAmount;
  62. private $_decAmountStandard;
  63. const TYPE_ZC = 'ZC';
  64. private $_userForm = null;
  65. // 全部的安置网上级
  66. private $_tempNetworkParentUser = [];
  67. /**
  68. * @inheritdoc
  69. */
  70. public function rules()
  71. {
  72. return [
  73. [['remark','type','decLv','decWay','packageId','goodsId', 'goodsNum', 'insertUserName','consignee','acceptMobile','province',/*'city','county',*/'lgaName','cityName','address','nowPerf','nextPerf'], 'trim'],
  74. [['type','decLv','decWay','insertUserName','nowPerf','province',/*'city','county',*/'address','acceptMobile'], 'required'],
  75. [['decUserName'], 'issetDec'], // 必须是报单中心
  76. [['decWay'], 'hasProduct'],// 必须选择商品
  77. [['decLv'], 'alreadyMaxDec'], //判断要升级用户是否已经是最高级
  78. ];
  79. }
  80. public function attributeLabels()
  81. {
  82. return [
  83. 'type' => 'Upgrade type',//升级类型
  84. 'decLv' => 'Upgrade level',//升级级别
  85. 'decWay' => 'Declaration method',//报单方式
  86. 'packageId' => 'Upgrade package',//升级套餐
  87. 'goodsId' => 'Product ID',//商品ID
  88. 'goodsNum' => 'Product quantity',//商品数量
  89. 'insertUserName' => 'Member number to be upgraded',//要升级的会员编号
  90. 'consignee' => 'consignee',//收货人
  91. 'acceptMobile' => 'The phone number of Consignee',//收货人手机
  92. 'province' => 'Receiving Province',//收货省
  93. 'city' => 'Receiving City',//收货市
  94. 'county' => 'Receiving area / county',//收货区县
  95. 'lgaName' => 'Local Government Area',
  96. 'cityName' => 'City',
  97. 'address' => 'Receiving detailed address',//收货详细地址
  98. ];
  99. }
  100. /**
  101. * 添加报单
  102. * @param $allData
  103. * @throws Exception
  104. * @throws \yii\db\Exception
  105. */
  106. public function add($allData){
  107. if(!$this->validate()){
  108. return null;
  109. }
  110. // 首购单
  111. if($this->type == self::TYPE_ZC){
  112. //报单商品及PV判断
  113. $decLevelConfig = Cache::getDecLevelConfig();
  114. $decLevel = $decLevelConfig[$this->decLv];
  115. $toDecLevel = $this->decLv;
  116. if(!$this->decLv){
  117. throw new Exception(Yii::t('app', 'pleaseSelectUpgradeLevel'), 400);
  118. }
  119. $baseInfo = Info::baseInfoZhByUserName($this->insertUserName);
  120. $userId = $baseInfo['ID'];
  121. $userDecPvSum = User::sumDevPvByUserId($userId); // 用户所有报单PV总和
  122. if ($userDecPvSum != $this->nowPerf) {
  123. throw new Exception(Yii::t('app', 'checkPerformanceOfUpgradedMember'), 400);
  124. }
  125. // 获取用户是否是观察期
  126. $observe = Config::getConfigByType('observe'); // 获取观察期配置信息
  127. $observeLimit = $observe['observePeriodLimit']['value']; // 月份限制
  128. $isObserve = User::checkIsObserve($baseInfo['CREATED_AT'], $observeLimit); // 判断用户是否再观察期中
  129. $diffPerf = $isObserve ? $this->nowPerf : 0; // 观察期内升级要加上用户累计的PV,全额则基础PV为0,全额购买
  130. if ($this->decWay != 2) {
  131. throw new Exception(Yii::t('app', 'upgradeMethodIncorrect'), 400);
  132. }
  133. if($this->decWay==1) {
  134. // 先不加套餐升级方式
  135. // $decPackage = DeclarationPackage::findOneAsArray('ID=:ID', [':ID'=>$this->packageId]);
  136. // $this->_decAmount = $decPackage['AMOUNT'];
  137. // $this->_decPv = $decPackage['PV'];
  138. // $this->_orderGoods[] = [
  139. // 'GOODS_ID' => $this->packageId,
  140. // 'PRICE' => $this->_decAmount,
  141. // 'REAL_PRICE' => $this->_decAmount,
  142. // 'PV' => $this->_decPv,
  143. // 'REAL_PV' => $this->_decPv,
  144. // 'BUY_NUMS' => 1,
  145. // 'SKU_CODE' => $decPackage['PACKAGE_NO'],
  146. // 'GOODS_TITLE' => $decPackage['PACKAGE_NAME']
  147. // ];
  148. } else {
  149. $ids = $this->goodsId;
  150. $totalAmount = 0;
  151. $totalAmountStandard = 0;
  152. $totalPv = 0;
  153. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0); // 汇率
  154. foreach ($this->goodsNum as $k => $v) {
  155. if ($v) {
  156. $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1',[':ID'=> $ids[$k]]);
  157. if (!$goods) {
  158. throw new Exception('Products does not exists!');
  159. }
  160. if($goods['STORE_NUMS']>0){
  161. $totalAmount += $goods['SELL_PRICE'] * intval($v);
  162. $totalAmountStandard += $goods['SELL_PRICE_STANDARD'] * intval($v);
  163. $realPriceStandard = $goods['SELL_PRICE_STANDARD'];
  164. $totalPv += $goods['PRICE_PV'] * intval($v);
  165. $this->_orderGoods[] = [
  166. 'GOODS_ID' => $goods['ID'],
  167. 'PRICE' => $goods['SELL_PRICE'],
  168. 'REAL_PRICE' => $goods['SELL_PRICE'],
  169. 'PV' => $goods['PRICE_PV'],
  170. 'REAL_PV' => $goods['PRICE_PV'],
  171. 'POINT' => $goods['POINT'],
  172. 'BUY_NUMS' => intval($v),
  173. 'SKU_CODE' => $goods['GOODS_NO'],
  174. 'GOODS_TITLE' => $goods['GOODS_NAME'],
  175. 'EMAIL' => Info::getUserEmailByUserId(\Yii::$app->user->id) ?? '',
  176. 'STANDARD_PRICE' => $goods['SELL_PRICE_STANDARD'],
  177. 'REAL_STANDARD_PRICE' => $realPriceStandard,
  178. 'EXCHANGE_RATE' => $exchangeRate,
  179. 'TAX_RATE' => $goods['TAX_RATE'],
  180. ];
  181. }
  182. }
  183. }
  184. // 这里特殊是用户原报单PV之和+用户购买的商品总PV
  185. $checkPv = $totalPv + $diffPerf;
  186. if($checkPv < $decLevel['PERF']) {
  187. throw new Exception(Yii::t('app', 'totalPVLessThan'), 400);
  188. }
  189. foreach ($decLevelConfig as $key=>$val){
  190. if($checkPv>=$val['PERF']){
  191. $toDecLevel = $key;
  192. }
  193. }
  194. if($this->decLv!=$toDecLevel){
  195. throw new Exception(Yii::t('app', 'totalPvExceedPv'), 400);
  196. }
  197. $this->_decAmount = $totalAmount;
  198. $this->_decPv = $totalPv;
  199. $this->_decAmountStandard = $totalAmountStandard;
  200. $this->_standardAmount = $this->_decAmountStandard;
  201. }
  202. $baseInfo = Info::baseInfoZhByUserName($this->insertUserName);
  203. $this->_insertUserId = $baseInfo['ID']; // 被报单人,通过insername 查找用户id
  204. $insertConId = $baseInfo['CON_UID'];
  205. $insertRecId = $baseInfo['REC_UID'];
  206. $decResult = $this->addDecOrder($insertConId, $insertRecId, $baseInfo['DEC_LV'], $isObserve, $this->remark);
  207. if (!$decResult) {
  208. throw new Exception(Yii::t('app', 'failed'), 400);
  209. }
  210. return $decResult;
  211. }
  212. return true;
  213. }
  214. /**
  215. * 添加报单订单
  216. * @throws Exception|\Throwable
  217. */
  218. public function addDecOrder($insertConId, $insertRecId, $oriDecLv, $isObserve, $remark = ''): ApproachOrder
  219. {
  220. $warehouse = Region::getWarehouseByCode($this->province);//仓库
  221. if(!$warehouse){
  222. throw new Exception(Yii::t('app', 'deliveryTemporarilyNotSupported'), 400);
  223. }
  224. $upgradeType = $isObserve ? 1 : 2; // 1补差 2全额
  225. $periodObj = Period::instance();
  226. $nowPeriodNum = $periodObj->getNowPeriodNum();
  227. $nowCalcMonth = $periodObj->getYearMonth($nowPeriodNum);
  228. $ord = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 10);
  229. // 加入报单信息
  230. $db = \Yii::$app->db;
  231. $transaction = $db->beginTransaction();
  232. try {
  233. $decOrderModel = new ApproachDecOrder();
  234. $decOrderModel->DEC_SN = 'DS'.$ord;
  235. $decOrderModel->ORDER_SN = 'OS'.$ord;
  236. $decOrderModel->TYPE = $this->type;
  237. $decOrderModel->USER_ID = \Yii::$app->user->id; // 报单人
  238. $decOrderModel->TO_USER_ID = $this->_insertUserId; // 被报单人
  239. $decOrderModel->DEC_AMOUNT = $this->_decAmount;
  240. $decOrderModel->DEC_PV = $this->_decPv;
  241. $decOrderModel->PERIOD_NUM = $nowPeriodNum;
  242. $decOrderModel->CALC_MONTH = $nowCalcMonth;
  243. $decOrderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  244. $decOrderModel->PAID_WALLET = 'cash';
  245. $decOrderModel->CON_USER_ID = $insertConId;
  246. $decOrderModel->REC_USER_ID = $insertRecId;
  247. $decOrderModel->DEC_ID = $this->_decId;
  248. $decOrderModel->IS_DEL = 0;
  249. $decOrderModel->DETAIL_TYPE = 2;
  250. $decOrderModel->CREATED_AT = Date::nowTime();
  251. $decOrderModel->UPGRADE_TYPE = $upgradeType;
  252. $decOrderModel->REMARK = $remark;
  253. $decOrderModel->ORI_LV = $oriDecLv; // 变更前的级别
  254. $decOrderModel->UPGRADE_LV = $this->decLv; // 变更后的级别
  255. if (!$decOrderModel->save()) {
  256. $transaction->rollBack();
  257. throw new Exception(Form::formatErrorsForApi($decOrderModel->getErrors()));
  258. }
  259. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0);
  260. $orderModel = new ApproachOrder();
  261. $orderModel->SN = 'OS' . $ord;
  262. $orderModel->DEC_SN = 'DS' . $ord;
  263. $orderModel->ORDER_TYPE = $this->type;
  264. $orderModel->USER_ID = $this->_insertUserId;
  265. $orderModel->USER_NAME = $this->insertUserName; // 要升级的用户
  266. $orderModel->ORDER_AMOUNT = $this->_decAmount;
  267. $orderModel->PV = $this->_decPv;
  268. $orderModel->PAY_AMOUNT = $this->_decAmount;
  269. $orderModel->PAY_PV = $this->_decPv;
  270. $orderModel->PAY_AT = 0;
  271. $orderModel->PAY_TYPE = 'pay_stack';
  272. $orderModel->PERIOD_NUM = $nowPeriodNum;
  273. $orderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  274. $orderModel->FREIGHT = 0;
  275. $orderModel->PAY_FREIGHT = 0;
  276. $orderModel->CONSIGNEE = $this->consignee;
  277. $orderModel->MOBILE = $this->acceptMobile;
  278. $orderModel->PROVINCE = $this->province;
  279. $orderModel->LGA_NAME = $this->lgaName;
  280. $orderModel->CITY_NAME = $this->cityName;
  281. $orderModel->ADDRESS = $this->address;
  282. $orderModel->WAREHOUSE = $warehouse;
  283. $orderModel->STATUS = \Yii::$app->params['orderStatus']['notPaid']['value'];
  284. $orderModel->CREATED_AT = Date::nowTime();
  285. $orderModel->CREATE_USER = Info::getUserNameByUserId(\Yii::$app->user->id);
  286. $orderModel->EMAIL = Info::getUserEmailByUserId(\Yii::$app->user->id) ?? '';
  287. $orderModel->ORDER_AMOUNT_STANDARD = $this->_decAmountStandard;
  288. $orderModel->PAY_AMOUNT_STANDARD = $this->_standardAmount;
  289. $orderModel->EXCHANGE_RATE = $exchangeRate;
  290. if(!$orderModel->save()){
  291. $transaction->rollBack();
  292. throw new Exception(Form::formatErrorsForApi($orderModel->getErrors()));
  293. }
  294. foreach ($this->goodsNum as $k => $v) {
  295. if ($v) {
  296. $goods = ShopGoods::findOneAsArray('ID=:ID',[':ID'=> $this->goodsId[$k]]);
  297. $storenums = $goods['STORE_NUMS'] - $this->goodsNum[$k];
  298. if($goods['STATUS']==1){
  299. if($goods['STORE_NUMS'] >=$this->goodsNum[$k]) {
  300. $data = ShopGoods::find()->where(['ID' => $this->goodsId[$k]])->one();
  301. $data->STORE_NUMS = $storenums;
  302. $data->update();
  303. if ($storenums==0) {
  304. $data->STATUS = 0;
  305. $data->UPDATED_AT = Date::nowTime();
  306. $data->update();
  307. }
  308. } else {
  309. throw new Exception("product".$goods['GOODS_NAME']."Insufficient inventory");//"商品".$goods['GOODS_NAME']."库存不足"
  310. }
  311. } else {
  312. throw new Exception("product".$goods['GOODS_NAME']."Removed from the shelf");// "商品".$goods['GOODS_NAME']."已下架"
  313. }
  314. }
  315. }
  316. // 加入商品到订单商品表
  317. foreach($this->_orderGoods as $key=>$value){
  318. $this->_orderGoods[$key]['ORDER_SN'] = $orderModel->SN;
  319. $this->_orderGoods[$key]['P_CALC_MONTH'] = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  320. }
  321. ApproachOrderGoods::batchInsert($this->_orderGoods);
  322. //写入收货地址信息
  323. $addressModel = new ReceiveAddress();
  324. $addressModel->USER_ID = $this->_insertUserId;
  325. $addressModel->USER_NAME = $this->insertUserName;
  326. $addressModel->CONSIGNEE = $this->consignee;
  327. $addressModel->MOBILE = $this->acceptMobile;
  328. $addressModel->PROVINCE = $this->province;
  329. $addressModel->LGA_NAME = $this->lgaName;
  330. $addressModel->CITY_NAME = $this->cityName;
  331. $addressModel->ADDRESS = $this->address;
  332. $addressModel->IS_DEFAULT = 0;
  333. if (!$addressModel->save()) {
  334. $transaction->rollBack();
  335. throw new Exception(Form::formatErrorsForApi($addressModel->getErrors()));
  336. }
  337. $transaction->commit();
  338. return $orderModel;
  339. } catch(Exception $e) {
  340. $transaction->rollBack();
  341. throw new Exception($e->getMessage());
  342. }
  343. }
  344. /**
  345. * 判断报单中心是否存在
  346. * @param $attribute
  347. * @return false|void
  348. */
  349. public function issetDec($attribute){
  350. $decUser = User::find()
  351. ->select('ID')
  352. ->where('IS_DEC=1 AND USER_NAME=:USER_NAME', [':USER_NAME' => $this->decUserName])
  353. ->asArray()
  354. ->one();
  355. if (!$decUser) {
  356. $this->addError($attribute, 'Entry center does not exist');//报单中心不存在
  357. return false;
  358. } else {
  359. // 判断报单中心是否在新加入会员的安置网上级中
  360. $this->loopFindParentToNetwork($this->insertUserName);
  361. //反转数组,in_array搜索错误
  362. //in_array($this->decUserName, $this->_tempNetworkParentUser[$this->insertUserName]);
  363. // $flipParent = array_flip(array_filter($this->_tempNetworkParentUser[$this->insertUserName]));
  364. // if (!isset($flipParent[$this->decUserName])) {
  365. //// $this->addError($attribute, '为' . $this->insertUserName . '升级报单,报单中心' . $this->decUserName . '不在' . $this->insertUserName . '的安置网上级中');
  366. // $this->addError($attribute, 'by' . $this->insertUserName . 'Upgrade declaration, declaration Center' . $this->decUserName . 'be not in' . $this->insertUserName . 'Online level of resettlement');
  367. // return ;
  368. // }
  369. $this->_decId = $decUser['ID'];
  370. }
  371. }
  372. // 判断是否已选择商品或套餐
  373. public function hasProduct($attribute) {
  374. if ($this->decWay==1 && empty($this->packageId)) {
  375. $this->addError($attribute, 'Purchase package upgrade, please select package');//购买套餐升级,请选择套餐
  376. return false;
  377. }
  378. if ($this->decWay!=1 && empty($this->goodsId)) {
  379. $this->addError($attribute, 'Purchase product upgrade, please select product');//购买商品升级,请选择商品'
  380. return false;
  381. }
  382. return true;
  383. }
  384. // 判断要升级的会员,是否已是最高级别
  385. public function alreadyMaxDec($attribute) {
  386. $baseInfo = Info::baseInfoZhByUserName($this->insertUserName);
  387. $userDecId = $baseInfo['DEC_LV'];// 用户当前的级别
  388. $maxPerfInfo = DeclarationLevel::getMaxDecPref();
  389. $maxDecId = $maxPerfInfo['ID']; // 级别配置中最高级别ID
  390. if ($maxDecId == $userDecId) {
  391. $this->addError($attribute, 'It is already the highest level and no upgrade is required');//已是最高级别,无需升级
  392. return false;
  393. }
  394. return true;
  395. }
  396. /**
  397. * 寻找被升级人的上级
  398. * @param null $conUserName
  399. * @return bool
  400. */
  401. private function loopFindParentToNetwork($conUserName = null) {
  402. if($conUserName == null ){
  403. $conUserName = $this->insertUserName;
  404. }
  405. $baseUser = Info::getBaseUserByUserName($conUserName);
  406. $userNetworkInfo = UserNetwork::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $baseUser['ID']]);
  407. unset($baseUser);
  408. $allParentUserIdsArr = explode(',', $userNetworkInfo['PARENT_UIDS']);
  409. unset($userNetworkInfo);
  410. $allParentUserIds = array_reverse($allParentUserIdsArr);
  411. unset($allParentUserIdsArr);
  412. if($allParentUserIds){
  413. foreach($allParentUserIds as $parentUserId) {
  414. $parentBaseUser = Info::getBaseUserById($parentUserId);
  415. if (is_null($parentBaseUser)) {
  416. continue;
  417. }
  418. $this->_tempNetworkParentUser[$this->insertUserName][] = $parentBaseUser['USER_NAME'] ;
  419. unset($parentUserId, $parentBaseUser);
  420. }
  421. }
  422. unset($allParentUserIds);
  423. return true;
  424. }
  425. }