ApproachDeclarationForm.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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\Reconsume;
  10. use common\helpers\user\Info;
  11. use common\models\ApproachDecOrder;
  12. use common\models\ApproachOrder;
  13. use common\models\ApproachOrderGoods;
  14. use common\models\Countries;
  15. use common\models\CurrencyConversions;
  16. use common\models\DeclarationPackage;
  17. use common\models\DecOrder;
  18. use common\models\EmployLevel;
  19. use common\models\Language;
  20. use common\models\Order;
  21. use common\models\OrderGoods;
  22. use common\models\Period;
  23. use common\models\ReceiveAddress;
  24. use common\models\ReconsumePool;
  25. use common\models\ReconsumePoolFlow;
  26. use common\models\DeclarationLevel;
  27. use common\models\Region;
  28. use common\models\ShopGoods;
  29. use common\models\ShopGoodsNature;
  30. use common\models\User;
  31. use common\models\UserInfo;
  32. use common\models\UserNetwork;
  33. use common\models\UserRelation;
  34. use common\models\Instalment;
  35. use Yii;
  36. use yii\base\Exception;
  37. /**
  38. * Login form
  39. */
  40. class ApproachDeclarationForm extends Model
  41. {
  42. public $type;
  43. public $decLv;
  44. public $decWay;
  45. public $packageId;
  46. public $goodsId;
  47. public $goodsNum;
  48. public $insertUserName;
  49. public $password;
  50. public $payPassword;
  51. public $realName;
  52. public $insertUserIdCard;
  53. public $mobile;
  54. public $email;
  55. public $address;
  56. public $openBank;
  57. public $bankAddress;
  58. public $bankNo;
  59. public $bankProvince;
  60. public $bankCity;
  61. public $bankCounty;
  62. public $conUserName;
  63. public $recUserName;
  64. public $bottomUserName;
  65. public $decUserName;
  66. public $location;
  67. public $consignee;
  68. public $acceptMobile;
  69. public $countryId;
  70. public $province;
  71. public $city;
  72. public $county;
  73. public $cityName;
  74. public $lgaName;
  75. public $payType;
  76. public $decType;
  77. public $languageId;
  78. // 传过来的全部数据
  79. public $allData;
  80. private $_decId;
  81. public $_insertUserId;
  82. private $_decAmount;
  83. private $_decPv;
  84. private $_orderGoods;
  85. private $_standardAmount;
  86. private $_decAmountStandard;
  87. public $period;
  88. // 批量报单时添加会员的REDIS里面缓存的添加的会员资料
  89. const REDIS_WAIT_ADD_USER = 'user:dec:waitAdd';
  90. const TYPE_ZC = 'ZC';
  91. // const TYPE_YH = 'YH';
  92. const TYPE_ZG = 'ZG';
  93. const TYPE_LS = 'LS';
  94. const TYPE_FX = 'FX';
  95. // private $_modelClass = null;
  96. private $_oneOrder = null;
  97. private $_userForm = null;
  98. // 全部的上级(安置网和开拓网)
  99. private $_tempParentUser = [];
  100. // 全部的安置网上级
  101. private $_tempNetworkParentUser = [];
  102. // 全部的开拓网上级
  103. private $_tempRelationParentUser = [];
  104. private $_types = [
  105. self::TYPE_ZC => [
  106. 'name' => '首购单',
  107. ],
  108. // self::TYPE_YH => [
  109. // 'class' => DeclarationYH::class,
  110. // 'table' => '{{%DECLARATION_YH}}',
  111. // 'name' => '优惠单',
  112. // ],
  113. self::TYPE_ZG => [
  114. 'name' => '升级增购单',
  115. ],
  116. self::TYPE_LS => [
  117. 'name' => '零售单',
  118. ],
  119. self::TYPE_FX => [
  120. 'name' => '复销单',
  121. ],
  122. ];
  123. /**
  124. * @inheritdoc
  125. */
  126. public function rules()
  127. {
  128. return [
  129. [['type','decLv','decWay','packageId', 'insertUserName', 'realName',/* 'insertUserIdCard',*/ 'mobile', 'address', 'openBank', 'bankAddress', 'bankNo','bankProvince','bankCity','bankCounty','consignee','acceptMobile','province','city','county','cityName','lgaName','decUserName', 'conUserName', 'recUserName', 'location'], 'trim'],
  130. [['type','decLv','decWay','insertUserName',/* 'insertUserIdCard',*/'password','payPassword'], 'required'],
  131. [['type'], 'isType', 'on'=>['userDec', 'canDec']],
  132. [['insertUserName'], 'isCanAddUser'],
  133. [['decUserName', 'decType'], 'issetDec'],
  134. [['conUserName'], 'isConUserName'],
  135. // [['recUserName'], 'isRecUserName'],
  136. // [['location'], 'isLocation'],
  137. // [['insertUserIdCard'], 'isSameSystem'],
  138. ];
  139. }
  140. public function attributeLabels()
  141. {
  142. return [
  143. 'type' => 'Entry type',//报单类型
  144. 'decLv' => 'Entry level',//报单级别
  145. //'decPv' => '报单PV',
  146. 'decWay' => 'Entry method',//报单方式
  147. 'packageId' => 'Entry Package',//报单套餐
  148. 'goodsId' => 'product ID',//商品ID
  149. 'goodsNum' => 'Product quantity',//商品数量
  150. //'addType' => '新增会员方式',
  151. 'insertUserName' => 'Member user name',//会员用户名
  152. 'password' => 'Login password',//登陆密码
  153. 'payPassword' => 'Payment password',//支付密码
  154. 'realName' => 'Member Name',//会员姓名
  155. 'insertUserIdCard' => 'ID',//身份证号
  156. 'mobile' => 'Phone Number',//手机号
  157. 'openBank' => 'openBank',//开户行
  158. 'bankAddress' => 'bankAddress',//开户支行
  159. 'bankNo' => 'bankNo',//银行账号
  160. 'bankProvince' => 'bankProvince',//银行省份
  161. 'bankCity' => 'bankCity',//银行城市
  162. 'bankCounty' => 'bankCounty',//银行县区
  163. 'conUserName' => 'Instructor user name',//指导老师用户名
  164. 'recUserName' => 'Developer user name',//开拓人用户名
  165. 'decUserName' => 'Stockist user name',//报单中心用户名
  166. 'conUid' => 'Instructor member ID',//指导老师会员ID
  167. 'recUid' => 'Sponsor member ID',//'开拓人会员ID
  168. 'location' => 'market',//市场
  169. 'consignee' => 'consignee',//收货人
  170. 'acceptMobile' => 'Recipient Phone Number',//收货人手机
  171. 'province' => 'Receiving Province',//收货省
  172. 'city' => 'Receiving City',//收货市
  173. 'county' => 'Receiving area / county',//收货区县
  174. 'address' => 'Receiving detailed address',//收货详细地址
  175. ];
  176. }
  177. /**
  178. * 指定场景
  179. * @return array
  180. */
  181. public function scenarios()
  182. {
  183. $parentScenarios = parent::scenarios();
  184. $customScenarios = [
  185. 'userDec' => ['type','allData', 'decLv','decWay','insertUserName','password','payPassword', 'realName',/* 'insertUserIdCard',*/ 'mobile', 'address', 'openBank', 'bankAddress', 'bankNo', 'bankProvince','bankCity','bankCounty', 'consignee','acceptMobile','province',/*'city','county',*/ 'conUserName', 'recUserName','decUserName', 'location', 'email', 'decType'],
  186. 'canDec' => ['type', 'insertUserName',/* 'insertUserIdCard',*/ 'conUserName', 'recUserName', 'location', 'decType'],
  187. 'notFull' => ['type', 'insertUserName', 'conUserName', 'recUserName', 'location'],
  188. ];
  189. return array_merge($parentScenarios, $customScenarios);
  190. }
  191. /**
  192. * 判断报单中心是否存在
  193. * @param $attribute
  194. */
  195. public function issetDec($attribute){
  196. if(!$this->decUserName){
  197. $this->_decId = '';
  198. } else {
  199. if (!$decUser = User::find()->select('ID')->where('IS_DEC=1 AND USER_NAME=:USER_NAME', [':USER_NAME' => $this->decUserName])->asArray()->one()) {
  200. $this->addError($attribute, Yii::t('app', 'stockistDoesNotExist'));
  201. return false;
  202. } else {
  203. // if ($this->decType !== 'ba') {
  204. // 判断报单中心是否在新加入会员的安置网上级中
  205. // $this->loopFindParentToNetwork($this->insertUserName);
  206. //反转数组,in_array搜索错误
  207. //in_array($this->decUserName, $this->_tempNetworkParentUser[$this->insertUserName]);
  208. // $flipParent = array_flip(array_filter($this->_tempNetworkParentUser[$this->insertUserName]));
  209. // if (!isset($flipParent[$this->decUserName])) {
  210. // //$this->addError($attribute, '为' . $this->insertUserName . '报单,报单中心' . $this->decUserName . '不在' . $this->insertUserName . '的安置网上级中');
  211. // $this->addError($attribute, 'To' . $this->insertUserName . 'Entry, Stockist' . $this->decUserName . 'not in exist' . $this->insertUserName . 'in the placement superiors');
  212. // return ;
  213. // }
  214. // }
  215. $this->_decId = $decUser['ID'];
  216. }
  217. }
  218. }
  219. /**
  220. * 判断指导老师
  221. * @param $attribute
  222. */
  223. public function isConUserName($attribute){
  224. if($this->type == self::TYPE_ZC){
  225. // 从数据库查看接点会员是否在
  226. if(!isset($this->_tempParentUser[$this->conUserName])){
  227. $conUserTemp = UserInfo::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => $this->conUserName]);
  228. if(!$conUserTemp){
  229. if($this->insertUserName){
  230. //$this->addError($attribute, '为'.$this->insertUserName.'报单,指导老师'.$this->conUserName.'不存在');
  231. $this->addError($attribute, 'To'.$this->insertUserName.'Entry,Instructor'.$this->conUserName.'not in exist');
  232. } else {
  233. //$this->addError($attribute, '指导老师'.$this->conUserName.'不存在');
  234. $this->addError($attribute, 'Instructor'.$this->conUserName.'not in exist');
  235. }
  236. return ;
  237. }
  238. $conUserTemp['CON_NUM'] = UserNetwork::firstFloorChildNum($conUserTemp['USER_ID']);
  239. $conUserTemp['REC_NUM'] = UserRelation::firstFloorChildNum($conUserTemp['USER_ID']);
  240. $this->_tempParentUser[$this->conUserName] = [
  241. 'USER_NAME' => $this->conUserName,
  242. 'ZC_PV' => $conUserTemp['ZC_PV'],
  243. 'CON_USER_NAME' => Info::getUserNameByUserId($conUserTemp['CON_UID']),
  244. 'REC_USER_NAME' => Info::getUserNameByUserId($conUserTemp['REC_UID']),
  245. 'CON_NUM' => $conUserTemp['CON_NUM'],
  246. 'REC_NUM' => $conUserTemp['REC_NUM'],
  247. 'SYSTEM_ID' => $conUserTemp['SYSTEM_ID'],
  248. 'LOCATION' => UserNetwork::getLocation($conUserTemp['USER_ID'], $conUserTemp['CON_UID']),
  249. ];
  250. // 把该会员下面的5个区是否存在会员都付上
  251. for($i=1;$i<=5;$i++){
  252. $this->_tempParentUser[$this->conUserName]["LOCATION$i"] = UserNetwork::issetUserInLocation($conUserTemp['USER_ID'], $i);
  253. }
  254. }
  255. $conUser = $this->_tempParentUser[$this->conUserName];
  256. // 判断接点会员的下级会员是否大于三个,如果大于三个则不允许
  257. if($conUser['CON_NUM'] >= 3){
  258. if($this->insertUserName){
  259. // $this->addError($attribute, '为'.$this->insertUserName.'报单,指导老师'.$this->conUserName.'下级点位已满');
  260. $this->addError($attribute, 'To'.$this->insertUserName.'Entry,Instructor'.$this->conUserName.'lower level is full');
  261. } else {
  262. //$this->addError($attribute, '指导老师'.$this->conUserName.'下级点位已满');
  263. $this->addError($attribute, 'Instructor'.$this->conUserName.'lower level is full');
  264. }
  265. return;
  266. }
  267. if($this->insertUserName){
  268. // 把自己加入到临时上级会员数组中
  269. $this->_tempParentUser[$this->insertUserName] = [
  270. 'USER_NAME' => $this->insertUserName,
  271. //'ZC_PV' => $this->decPv,
  272. 'CON_USER_NAME' => $this->conUserName,
  273. 'REC_USER_NAME' => $this->recUserName,
  274. 'CON_NUM' => 0,
  275. 'REC_NUM' => 0,
  276. 'SYSTEM_ID' => $conUser['SYSTEM_ID'],
  277. 'LOCATION' => $this->location,
  278. 'LOCATION1' => 0,
  279. 'LOCATION2' => 0,
  280. 'LOCATION3' => 0,
  281. 'LOCATION4' => 0,
  282. 'LOCATION5' => 0,
  283. ];
  284. }
  285. }
  286. }
  287. /**
  288. * 判断开拓人
  289. * @param $attribute
  290. */
  291. public function isRecUserName($attribute){
  292. if($this->type == self::TYPE_ZC){
  293. // 开拓人
  294. if(!isset($this->_tempParentUser[$this->recUserName])){
  295. $recUserTemp = UserInfo::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => $this->recUserName]);
  296. if(!$recUserTemp){
  297. if($this->insertUserName){
  298. //$this->addError($attribute, '为'.$this->insertUserName.'报单,开拓人'.$this->recUserName.'不存在');
  299. $this->addError($attribute, 'To'.$this->insertUserName.'Entry, Sponsor'.$this->recUserName.'not in exist');
  300. } else {
  301. // $this->addError($attribute, '开拓人'.$this->recUserName.'不存在');
  302. $this->addError($attribute, 'Sponsor'.$this->recUserName.'not in exist');
  303. }
  304. return ;
  305. }
  306. $recUserTemp['CON_NUM'] = UserNetwork::firstFloorChildNum($recUserTemp['USER_ID']);
  307. $recUserTemp['REC_NUM'] = UserRelation::firstFloorChildNum($recUserTemp['USER_ID']);
  308. $this->_tempParentUser[$this->recUserName] = [
  309. 'USER_NAME' => $this->recUserName,
  310. 'ZC_PV' => $recUserTemp['ZC_PV'],
  311. 'CON_USER_NAME' => Info::getUserNameByUserId($recUserTemp['CON_UID']),
  312. 'REC_USER_NAME' => Info::getUserNameByUserId($recUserTemp['REC_UID']),
  313. 'CON_NUM' => $recUserTemp['CON_NUM'],
  314. 'REC_NUM' => $recUserTemp['REC_NUM'],
  315. 'SYSTEM_ID' => $recUserTemp['SYSTEM_ID'],
  316. 'LOCATION' => UserNetwork::getLocation($recUserTemp['USER_ID'], $recUserTemp['CON_UID']),
  317. ];
  318. // 把该会员下面的5个区是否存在会员都付上
  319. for($i=1;$i<=5;$i++){
  320. $this->_tempParentUser[$this->recUserName]["LOCATION$i"] = UserNetwork::issetUserInLocation($recUserTemp['USER_ID'], $i);
  321. }
  322. }
  323. $recUser = $this->_tempParentUser[$this->recUserName];
  324. $this->_tempParentUser[$this->recUserName]['REC_NUM'] += 1;
  325. // 存在新加入会员时查看开拓人是否在新加入会员的安置网上级中
  326. if($this->decType !== 'ba' && $this->insertUserName) {
  327. $this->loopFindParentToNetwork($this->insertUserName);
  328. if (!$this->recUserIsInNetworkParent()) {
  329. // $this->addError($attribute, '为' . $this->insertUserName . '报单,开拓人' . $this->recUserName . '不在' . $this->insertUserName . '的安置网上级中');
  330. $this->addError($attribute, 'To' . $this->insertUserName . 'Entry, Sponsor' . $this->recUserName . 'not in exist' . $this->insertUserName . 'in the placement superiors');
  331. return ;
  332. }
  333. }
  334. // 把自己加入到临时上级会员数组中
  335. $this->_tempParentUser[$this->insertUserName] = [
  336. 'USER_NAME' => $this->insertUserName,
  337. //'ZC_PV' => $this->decPv,
  338. 'CON_USER_NAME' => $this->conUserName,
  339. 'REC_USER_NAME' => $this->recUserName,
  340. 'CON_NUM' => 0,
  341. 'REC_NUM' => 0,
  342. 'SYSTEM_ID' => $recUser['SYSTEM_ID'],
  343. 'LOCATION' => $this->location,
  344. 'LOCATION1' => 0,
  345. 'LOCATION2' => 0,
  346. 'LOCATION3' => 0,
  347. 'LOCATION4' => 0,
  348. 'LOCATION5' => 0,
  349. ];
  350. }
  351. }
  352. /**
  353. * 循环把所有新加入的会员的上级加入到临时变量中
  354. * @param null $conUserName
  355. * @return bool
  356. */
  357. private function loopFindParentToNetwork($conUserName = null) {
  358. if($conUserName == null ){
  359. $conUserName = $this->insertUserName;
  360. }
  361. $findUserKey = array_search($conUserName, array_column($this->allData, 'insertUserName'));
  362. // 如果有这个新加入的会员,则查找其上级接点
  363. if($findUserKey !== false){
  364. if(strtoupper($this->allData[$findUserKey]['type']) == 'ZC'){
  365. $this->_tempNetworkParentUser[$this->insertUserName][] = $this->allData[$findUserKey]['conUserName'];
  366. $this->loopFindParentToNetwork($this->allData[$findUserKey]['conUserName']);
  367. }
  368. }
  369. // 如果没有这个新加入的接点,
  370. else {
  371. if($this->insertUserName == $conUserName){
  372. // $this->addError('recUserName', '新加入的会员不存在');
  373. $this->addError('recUserName', Yii::t('app', 'newMemberDoesNotExist'));
  374. return false;
  375. }
  376. // 去数据库里查找这个会员的所有上级
  377. // $allParentUser = UserNetwork::find()->select('PUI.USER_NAME')->where('UI.USER_NAME=:USER_NAME', [':USER_NAME'=>$conUserName])->from(UserNetwork::tableName().' AS UN')->join('LEFT JOIN', UserInfo::tableName().' AS UI', 'UN.USER_ID=UI.USER_ID')->join('LEFT JOIN', UserInfo::tableName().' AS PUI', 'UN.PARENT_UID=PUI.USER_ID')->orderBy('PUI.NETWORK_DEEP DESC')->asArray()->all();
  378. $baseUser = Info::getBaseUserByUserName($conUserName);
  379. $userNetworkInfo = UserNetwork::findOneAsArray('USER_ID=:USER_ID AND PARENT_UIDS <> ""', [':USER_ID' => $baseUser['ID']]);
  380. unset($baseUser);
  381. if ($userNetworkInfo) {
  382. $allParentUserIdsArr = explode(',', $userNetworkInfo['PARENT_UIDS']);
  383. unset($userNetworkInfo);
  384. $allParentUserIds = array_reverse($allParentUserIdsArr);
  385. unset($allParentUserIdsArr);
  386. if($allParentUserIds && count($allParentUserIds)){
  387. foreach($allParentUserIds as $parentUserId) {
  388. $parentBaseUser = Info::getBaseUserById($parentUserId);
  389. $this->_tempNetworkParentUser[$this->insertUserName][] = $parentBaseUser['USER_NAME'] ;
  390. unset($parentUserId, $parentBaseUser);
  391. }
  392. }
  393. unset($allParentUserIds);
  394. }
  395. }
  396. return true;
  397. }
  398. /**
  399. * 开拓人是否在新加入会员的上级中
  400. * @return bool
  401. */
  402. private function recUserIsInNetworkParent(){
  403. return in_array($this->recUserName, $this->_tempNetworkParentUser[$this->insertUserName]);
  404. }
  405. /**
  406. * 判断区域是否正确
  407. * @param $attribute
  408. */
  409. public function isLocation($attribute){
  410. if($this->type == self::TYPE_ZC){
  411. if(!in_array($this->location, [1, 2, 3])){
  412. //$this->addError($attribute, '市场必须在第1市场、第2市场或第三3市场');
  413. $this->addError($attribute, Yii::t('app', 'pleaseSelectMarket'));
  414. }
  415. $conUser = $recUser = null;
  416. if(isset($this->_tempParentUser[$this->conUserName])){
  417. $conUser = $this->_tempParentUser[$this->conUserName];
  418. }
  419. if(isset($this->_tempParentUser[$this->recUserName])){
  420. $recUser = $this->_tempParentUser[$this->recUserName];
  421. }
  422. if($conUser){
  423. // 判断指导老师相应的区位是否已满
  424. if($conUser['LOCATION'.$this->location]){
  425. if($this->insertUserName){
  426. // $this->addError($attribute, '为'.$this->insertUserName.'报单,指导老师'.$this->conUserName.'的第'.$this->location.'市场已存在会员');
  427. $this->addError($attribute, 'To'.$this->insertUserName.'Entry,Instructor'.$this->conUserName.'the'.($this->location == 1 ? 'left' : 'regiht').' superior already existing member');
  428. } else {
  429. // $this->addError($attribute, '指导老师'.$this->conUserName.'的第'.$this->location.'市场已存在会员');
  430. $this->addError($attribute, 'Instructor'.$this->conUserName.'the'.($this->location == 1 ? 'left' : 'regiht').'superior already existing member');
  431. }
  432. return;
  433. }
  434. $this->_tempParentUser[$this->conUserName]['CON_NUM'] += 1;
  435. $this->_tempParentUser[$this->conUserName]['LOCATION'.$this->location] = true;
  436. }
  437. }
  438. }
  439. /**
  440. * 判断会员是否可加入(通过redis结合数据库判断上级会员的情况)
  441. * @param $attribute
  442. */
  443. public function isCanAddUser($attribute){
  444. if($this->type == self::TYPE_ZC){
  445. // 从数据库查看接点会员是否在
  446. if(!isset($this->_tempParentUser[$this->conUserName])){
  447. $conUserTemp = UserInfo::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => $this->conUserName]);
  448. if(!$conUserTemp){
  449. // $this->addError($attribute, '为'.$this->insertUserName.'报单,指导老师'.$this->conUserName.'不存在');
  450. $this->addError($attribute, 'To'.$this->insertUserName.'Entry,Instructor'.$this->conUserName.'not in exist');
  451. return ;
  452. }
  453. $conUserTemp['CON_NUM'] = UserNetwork::firstFloorChildNum($conUserTemp['USER_ID']);
  454. $conUserTemp['REC_NUM'] = UserRelation::firstFloorChildNum($conUserTemp['USER_ID']);
  455. $this->_tempParentUser[$this->conUserName] = [
  456. 'USER_NAME' => $this->conUserName,
  457. //'ZC_PV' => $conUserTemp['ZC_PV'],
  458. 'CON_USER_NAME' => Info::getUserNameByUserId($conUserTemp['CON_UID']),
  459. 'REC_USER_NAME' => Info::getUserNameByUserId($conUserTemp['REC_UID']),
  460. 'CON_NUM' => $conUserTemp['CON_NUM'],
  461. 'REC_NUM' => $conUserTemp['REC_NUM'],
  462. 'SYSTEM_ID' => $conUserTemp['SYSTEM_ID'],
  463. 'LOCATION' => UserNetwork::getLocation($conUserTemp['USER_ID'], $conUserTemp['CON_UID']),
  464. ];
  465. // 把该会员下面的5个区是否存在会员都付上
  466. for($i=1;$i<=5;$i++){
  467. $this->_tempParentUser[$this->conUserName]["LOCATION$i"] = UserNetwork::issetUserInLocation($conUserTemp['USER_ID'], $i);
  468. }
  469. }
  470. $conUser = $this->_tempParentUser[$this->conUserName];
  471. // 开拓人
  472. if(!isset($this->_tempParentUser[$this->recUserName])){
  473. $recUserTemp = UserInfo::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => $this->recUserName]);
  474. if(!$recUserTemp){
  475. // $this->addError($attribute, '为'.$this->insertUserName.'报单,开拓人'.$this->recUserName.'不存在');
  476. $this->addError($attribute, 'To'.$this->insertUserName.'Entry, Sponsor'.$this->recUserName.'not in exist');
  477. return ;
  478. }
  479. $recUserTemp['CON_NUM'] = UserNetwork::firstFloorChildNum($recUserTemp['USER_ID']);
  480. $recUserTemp['REC_NUM'] = UserRelation::firstFloorChildNum($recUserTemp['USER_ID']);
  481. $this->_tempParentUser[$this->recUserName] = [
  482. 'USER_NAME' => $this->recUserName,
  483. //'ZC_PV' => $recUserTemp['ZC_PV'],
  484. 'CON_USER_NAME' => Info::getUserNameByUserId($recUserTemp['CON_UID']),
  485. 'REC_USER_NAME' => Info::getUserNameByUserId($recUserTemp['REC_UID']),
  486. 'CON_NUM' => $recUserTemp['CON_NUM'],
  487. 'REC_NUM' => $recUserTemp['REC_NUM'],
  488. 'SYSTEM_ID' => $recUserTemp['SYSTEM_ID'],
  489. 'LOCATION' => UserNetwork::getLocation($recUserTemp['USER_ID'], $recUserTemp['CON_UID']),
  490. ];
  491. // 把该会员下面的5个区是否存在会员都付上
  492. for($i=1;$i<=5;$i++){
  493. $this->_tempParentUser[$this->recUserName]["LOCATION$i"] = UserNetwork::issetUserInLocation($recUserTemp['USER_ID'], $i);
  494. }
  495. }
  496. $recUser = $this->_tempParentUser[$this->recUserName];
  497. // 判断接点会员的下级会员是否大于三个,如果大于三个则不允许
  498. if($conUser['CON_NUM'] >= 3){
  499. // $this->addError($attribute, '为'.$this->insertUserName.'报单,指导老师'.$this->conUserName.'下级点位已满');
  500. $this->addError($attribute, 'To'.$this->insertUserName.'Entry, Sponsor'.$this->conUserName.'lower level is full');
  501. return;
  502. }
  503. // 判断指导老师相应的区位是否已满
  504. if($conUser['LOCATION'.$this->location]){
  505. // $this->addError($attribute, '为'.$this->insertUserName.'报单,指导老师'.$this->conUserName.'该市场已存在会员');
  506. $this->addError($attribute, 'To'.$this->insertUserName.'Entry, Sponsor'.$this->conUserName.'Members already exist in this market');
  507. return;
  508. }
  509. // if($conUser['CON_NUM'] == 0 && $this->location != 1){
  510. // $this->addError($attribute, '为'.$this->insertUserName.'报单,市场必须为指导老师'.$this->conUserName.'下第一市场');
  511. // return;
  512. // }
  513. // 把指导老师的变量的相关数量增加数量
  514. // $this->_tempParentUser[$this->conUserName]['CON_NUM'] += 1;
  515. // $this->_tempParentUser[$this->conUserName]['LOCATION'.$this->location] = true;
  516. // $this->_tempParentUser[$this->recUserName]['REC_NUM'] += 1;
  517. // 把自己加入到临时上级会员数组中
  518. $this->_tempParentUser[$this->insertUserName] = [
  519. 'USER_NAME' => $this->insertUserName,
  520. //'ZC_PV' => $this->decPv,
  521. 'CON_USER_NAME' => $this->conUserName,
  522. 'REC_USER_NAME' => $this->recUserName,
  523. 'CON_NUM' => 0,
  524. 'REC_NUM' => 0,
  525. 'SYSTEM_ID' => $recUser['SYSTEM_ID'],
  526. 'LOCATION' => $this->location,
  527. 'LOCATION1' => 0,
  528. 'LOCATION2' => 0,
  529. 'LOCATION3' => 0,
  530. 'LOCATION4' => 0,
  531. 'LOCATION5' => 0,
  532. ];
  533. }
  534. }
  535. /**
  536. * 报单类型
  537. * @param $attribute
  538. * @param $params
  539. */
  540. public function isType($attribute, $params){
  541. if(array_key_exists($this->type, $this->_types)){
  542. if($this->type == self::TYPE_ZC){
  543. if($this->scenario == 'userDec'){
  544. if(!$this->insertUserName) $this->addError($attribute, Yii::t('app', 'MembershipNumberFilledInitialPurchase'));
  545. }
  546. if(!$this->insertUserName) $this->addError($attribute, Yii::t('app', 'MembershipNumberFilledInitialPurchase'));
  547. // if(!$this->insertUserIdCard) $this->addError($attribute, 'For the first purchase, the ID number of the member must be filled in');//首购必须填写加入会员的身份证号
  548. if(!$this->conUserName) $this->addError($attribute, Yii::t('app', 'fillTheInstructorNumberTheMember'));
  549. if(!$this->recUserName) $this->addError($attribute, Yii::t('app', 'sponsorNumberMustBeFilled'));
  550. if(!$this->location) $this->addError($attribute, Yii::t('app', 'beFilledTheMarketMember'));
  551. }
  552. } else {
  553. $this->addError($attribute, Yii::t('app', 'incorrectEntryType'));
  554. }
  555. }
  556. /**
  557. * 检验相同身份证 会员是否同一体系内(接点)
  558. * @param $attribute
  559. */
  560. // public function isSameSystem($attribute){
  561. // if(isset($this->_tempParentUser[$this->conUserName])){
  562. // // 新加入会员的身份证号对应网内的其他会员
  563. // $otherUser = User::findAllAsArray("ID_CARD=:ID_CARD AND IS_UNION=0 AND DELETED=0 AND (ID_CARD_PREFIX IS NULL OR ID_CARD_PREFIX='')", [':ID_CARD'=>$this->insertUserIdCard]);
  564. // if(count($otherUser)>=7){
  565. // $this->addError($attribute, 'Only 7 documents can be reported for the same ID card');//同一个身份证限制只能报7单
  566. // }
  567. // if($otherUser){
  568. // $flipParent = array_flip(array_filter($this->_tempNetworkParentUser[$this->insertUserName]));
  569. // $isExsit = false;
  570. // foreach ($otherUser as $conUser){
  571. // if(isset($flipParent[$conUser['USER_NAME']])){
  572. // $isExsit = true;
  573. // break;
  574. // }
  575. // }
  576. // if (!$isExsit) {
  577. //// $this->addError($attribute, '为' . $this->insertUserName . '报单,身份证号码相同的会员'.$otherUser[0]['USER_NAME'].'不在' . $this->insertUserName . '的安置网上级中');
  578. // $this->addError($attribute, 'To' . $this->insertUserName . 'Entry,Members with the same ID number'.$otherUser[0]['USER_NAME'].'not in exist' . $this->insertUserName . 'in the placement superiors');
  579. // return ;
  580. // }
  581. // }
  582. // } else {
  583. //// $this->addError($attribute, '为'.$this->insertUserName.'报单,接点人'.$this->conUserName.'不存在');
  584. // $this->addError($attribute, 'To'.$this->insertUserName.'Entry,Contact person'.$this->conUserName.'not in exist');
  585. // }
  586. // }
  587. /**
  588. * 添加报单
  589. * @param $allData
  590. * @return ApproachOrder
  591. * @throws Exception
  592. * @throws \yii\db\Exception
  593. */
  594. public function add($allData){
  595. if(!$this->validate()){
  596. return null;
  597. }
  598. $hasInstalment = 0;
  599. // 首购单,需要添加会员操作
  600. if($this->type == self::TYPE_ZC) {
  601. try {
  602. if (preg_match("/[\x7f-\xff]/", $this->insertUserName)) { //判断字符串中是否有中文
  603. throw new Exception(Yii::t('app', 'memberNumberCanNotContainChineseCharacters'));
  604. }
  605. //报单商品及PV判断
  606. $decLevelConfig = Cache::getDecLevelConfig();
  607. $decLevel = $decLevelConfig[$this->decLv];
  608. $toDecLevel = $this->decLv;
  609. if (!$this->decLv) {
  610. throw new Exception(Yii::t('app', 'pleaseSelectTheEntryLevel'));
  611. }
  612. // 报单中心汇率
  613. $decCountryId = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID'];
  614. $decCountry = Countries::getById($decCountryId);
  615. $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']);
  616. // 升级会员汇率
  617. $country = Countries::getById($this->countryId);
  618. $currencyRate = CurrencyConversions::getToUSDRate($country['LOCAL_CURRENCY_ID']);
  619. if ($this->decWay == 1) {
  620. $decPackage = DeclarationPackage::findOneAsArray('ID=:ID', [':ID' => $this->packageId]);
  621. $this->_decAmount = $decPackage['AMOUNT'];
  622. $this->_decPv = $decPackage['PV'];
  623. $this->_orderGoods[] = [
  624. 'GOODS_ID' => $this->packageId,
  625. 'PRICE' => $this->_decAmount,
  626. 'REAL_PRICE' => $this->_decAmount,
  627. 'PV' => $this->_decPv,
  628. 'REAL_PV' => $this->_decPv,
  629. 'BUY_NUMS' => 1,
  630. 'SKU_CODE' => $decPackage['PACKAGE_NO'],
  631. 'GOODS_TITLE' => $decPackage['PACKAGE_NAME'],
  632. 'EMAIL' => $this->email
  633. ];
  634. } else {
  635. $ids = $this->goodsId;
  636. $totalAmount = 0;
  637. $totalAmountStandard = 0;
  638. $totalPv = 0;
  639. foreach ($this->goodsNum as $k => $v) {
  640. if ($v) {
  641. $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1', [':ID' => $ids[$k]]);
  642. if ($goods['STORE_NUMS'] > 0) {
  643. $goodsNature = ShopGoodsNature::findOneAsArray('GOODS_ID=:GOODS_ID AND COUNTRY_ID=:COUNTRY_ID',
  644. [':GOODS_ID' => $ids[$k], ':COUNTRY_ID' => $this->countryId]);
  645. if (!$goodsNature) {
  646. throw new Exception(Yii::t('app', 'productsDoesSoldOut'));
  647. }
  648. $totalAmount += $goodsNature['SELL_PRICE'] * intval($v);
  649. $totalAmountStandard += $goods['SELL_PRICE_STANDARD'] * intval($v);
  650. $realPriceStandard = $goods['SELL_PRICE_STANDARD'];
  651. $totalPv += $goods['PRICE_PV'] * intval($v);
  652. $this->_orderGoods[] = [
  653. 'GOODS_ID' => $goods['ID'],
  654. 'PRICE' => $goodsNature['SELL_PRICE'],
  655. 'REAL_PRICE' => $goodsNature['SELL_PRICE'],
  656. 'PV' => $goods['PRICE_PV'],
  657. 'REAL_PV' => $goods['PRICE_PV'],
  658. 'POINT' => $goods['POINT'],
  659. 'BUY_NUMS' => intval($v),
  660. 'SKU_CODE' => $goods['GOODS_NO'],
  661. 'GOODS_TITLE' => $goods['GOODS_NAME'],
  662. 'EMAIL' => $this->email,
  663. 'STANDARD_PRICE' => $goods['SELL_PRICE_STANDARD'],
  664. 'REAL_STANDARD_PRICE' => $realPriceStandard,
  665. 'EXCHANGE_RATE' => $currencyRate,
  666. 'TAX_RATE' => $goodsNature['TAX_RATE'],
  667. ];
  668. }
  669. if($goods['INSTALMENT']>0){
  670. $hasInstalment = $goods['INSTALMENT'];
  671. }
  672. }
  673. }
  674. if($totalPv<$decLevel['PERF']){
  675. if ($hasInstalment && $decLevel['LEVEL_NAME'] == 'Silver'){
  676. }else{
  677. throw new Exception(Yii::t('app', 'totalBVCanNotLessThanSelectedBV'));
  678. }
  679. }
  680. foreach ($decLevelConfig as $key => $val) {
  681. if ($totalPv >= $val['PERF']) {
  682. $toDecLevel = $key;
  683. }
  684. }
  685. if ($this->decLv != $toDecLevel) {
  686. if ($hasInstalment && $decLevel['LEVEL_NAME'] == 'Silver'){
  687. }else{
  688. throw new Exception(Yii::t('app', 'totalBVCanNotLessThanNextSelectedLevelBV'));
  689. }
  690. }
  691. $this->_decAmount = $totalAmount;
  692. $this->_decPv = $totalPv;
  693. $this->_decAmountStandard = $totalAmountStandard;
  694. $this->_standardAmount = $this->_decAmountStandard;
  695. }
  696. if (!($zcResult = $this->addUser($allData))) {
  697. throw new Exception(Form::formatErrorsForApi($this->_userForm->getErrors()));
  698. }
  699. $decResult = $this->addDecOrder();
  700. if (!$decResult) {
  701. throw new Exception(Form::formatErrorsForApi($decResult->getErrors()));
  702. }
  703. if($hasInstalment){
  704. if(!($instalmentResult = $this->insertInstalment($zcResult))) {
  705. throw new Exception(Form::formatErrorsForApi('h'));
  706. }
  707. }
  708. return $decResult;
  709. } catch (\Exception $e) {
  710. // throw new Exception(Form::formatErrorsForApi($e->getFile() . ' ' . $e->getLine() . ' ' . $e->getMessage()));
  711. throw new Exception(Form::formatErrorsForApi($e->getMessage()));
  712. }
  713. }
  714. return true;
  715. }
  716. /**
  717. * 添加会员
  718. * @param $allData
  719. * @return bool|UserInfo|null
  720. * @throws \yii\db\Exception
  721. */
  722. public function addUser($allData){
  723. $periodObj = Period::instance();
  724. $nowPeriodNum = $this->period ?: $periodObj->getNowPeriodNum();
  725. // 增加会员
  726. $user = new User();
  727. $user->USER_NAME = $this->insertUserName;
  728. $user->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
  729. $user->PAY_PASSWORD = \Yii::$app->security->generatePasswordHash($this->payPassword);
  730. $user->NATION = 0;
  731. $user->REAL_NAME = $this->realName;
  732. $user->ID_CARD = $this->mobile;
  733. $user->MOBILE = $this->mobile;
  734. $user->EMAIL = $this->email;
  735. $user->ADDRESS = $this->address ? $this->address : 'nothing';//无
  736. $user->OPEN_BANK = $this->openBank;
  737. $user->BANK_ADDRESS = $this->bankAddress;
  738. $user->BANK_NO = $this->bankNo;
  739. $user->BANK_PROVINCE = $this->bankProvince ?? 0;
  740. $user->BANK_CITY = $this->bankCity ?? 0;
  741. $user->BANK_COUNTY = $this->bankCounty ?? 0;
  742. $user->CREATED_AT = Date::nowTime();
  743. $user->STATUS = 0;
  744. $user->DEC_LV = $this->decLv;
  745. $user->LAST_DEC_LV = $this->decLv;
  746. $user->EMP_LV = EmployLevel::getDefaultLevelId();
  747. $user->PROVINCE = $this->province ?? 0;
  748. $user->LGA_NAME = $this->lgaName;
  749. $user->CITY_NAME = $this->cityName;
  750. // $user->CITY = $this->city ?? 0;
  751. // $user->COUNTY = intval($this->county) ?? 0;
  752. $user->AVATAR = 'avatar/1.png';
  753. $user->IS_DEC = 0;
  754. $user->DEC_ID = $this->_decId ?? null;
  755. $user->DEC_ROLE_ID = $this->decRoleId ?? null;
  756. $user->PERIOD_AT = $nowPeriodNum;
  757. $user->IS_DIRECT_SELLER = 0;
  758. $user->VERIFIED = 1;
  759. $user->VERIFIED_AT = Date::nowTime();
  760. $user->IS_RECHARGE = 1;
  761. $user->COUNTRY_ID = $this->countryId;
  762. $user->LANGUAGE_ID = $this->languageId ?? Language::getEn();
  763. if (!$user->save()) {
  764. throw new Exception(Form::formatErrorsForApi($user->getErrors()));
  765. }
  766. $this->_insertUserId = $user->ID;
  767. $userForm = new UserForm();
  768. $this->_userForm = $userForm;
  769. $userForm->scenario = 'addWithUserName';
  770. $userForm->userId = $this->_insertUserId;
  771. $userForm->userName = $this->insertUserName;
  772. $userForm->zcPv = $this->_decPv;
  773. $userForm->zcAmount = $this->_decAmount;
  774. $userForm->conUserName = $this->conUserName;
  775. $userForm->recUserName = $this->recUserName;
  776. $userForm->bottomUserName = $this->bottomUserName;
  777. $userForm->location = $this->location;
  778. $userForm->idCard = $this->mobile;
  779. $userForm->allData = $allData;
  780. if(!$userForm->validate()){
  781. $this->addErrors($userForm->getErrors());
  782. return false;
  783. }
  784. if($result = $userForm->add()){
  785. return $result;
  786. } else {
  787. return false;
  788. }
  789. }
  790. /**
  791. * 添加报单订单
  792. * @throws \yii\db\Exception
  793. */
  794. public function addDecOrder(){
  795. $periodObj = Period::instance();
  796. $nowPeriodNum = $this->period ?: $periodObj->getNowPeriodNum();
  797. $nowCalcMonth = $periodObj->getYearMonth($nowPeriodNum);
  798. $ord = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 10);
  799. // 加入报单信息
  800. $decOrderModel = new ApproachDecOrder();
  801. $decOrderModel->DEC_SN = 'DS' . $ord;
  802. $decOrderModel->ORDER_SN = 'OS' . $ord;
  803. $decOrderModel->TYPE = $this->type;
  804. $decOrderModel->USER_ID = \Yii::$app->user->id;
  805. $decOrderModel->TO_USER_ID = $this->_insertUserId;
  806. $decOrderModel->DEC_AMOUNT = $this->_decAmount;
  807. $decOrderModel->DEC_PV = $this->_decPv;
  808. $decOrderModel->PERIOD_NUM = $nowPeriodNum;
  809. $decOrderModel->CALC_MONTH = $nowCalcMonth;
  810. $decOrderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  811. $decOrderModel->PAID_WALLET = $this->payType;
  812. $decOrderModel->CON_USER_ID = Info::getUserIdByUserName($this->conUserName);
  813. $decOrderModel->REC_USER_ID = Info::getUserIdByUserName($this->recUserName);
  814. $decOrderModel->DEC_ID = $this->_decId;
  815. $decOrderModel->IS_DEL = 0;
  816. $decOrderModel->CREATED_AT = Date::nowTime();
  817. if(!$decOrderModel->save()){
  818. throw new Exception(Form::formatErrorsForApi($decOrderModel->getErrors()));
  819. }
  820. // 加入订单信息
  821. if ($this->province!=1) {
  822. // 仓库
  823. $warehouse = Region::getWarehouseByCode($this->province);
  824. if (!$warehouse) {
  825. // 地区暂时不支持配送,具体联系客服
  826. throw new Exception(Yii::t('app', 'deliveryTemporarilyNotSupported'));
  827. }
  828. } else {
  829. $warehouse = '01';
  830. }
  831. // 升级会员
  832. $userCountry = User::getEnCodeInfo($this->_insertUserId);
  833. $userCurrencyRate = CurrencyConversions::getToUSDRate($userCountry['LOCAL_CURRENCY_ID']);
  834. // 报单中心汇率
  835. $decCountry = User::getEnCodeInfo(\Yii::$app->user->id)['COUNTRY_ID'];
  836. $decUserCurrencyRate = CurrencyConversions::getToUSDRate($decCountry['LOCAL_CURRENCY_ID']);
  837. $orderModel = new ApproachOrder();
  838. $orderModel->SN = 'OS' . $ord;
  839. $orderModel->DEC_SN = 'DS' . $ord;
  840. $orderModel->DEC_USER_ID = $this->_decId;
  841. $orderModel->ORDER_TYPE = $this->type;
  842. $orderModel->USER_ID = $this->_insertUserId;
  843. $orderModel->USER_NAME = $this->insertUserName;
  844. $orderModel->ORDER_AMOUNT = $this->_decAmount;
  845. $orderModel->PV = $this->_decPv;
  846. $orderModel->PAY_AMOUNT = $this->_decAmount;
  847. $orderModel->PAY_PV = $this->_decPv;
  848. $orderModel->PAY_AT = 0;
  849. $orderModel->PAY_TYPE = $this->payType;
  850. $orderModel->PERIOD_NUM = $nowPeriodNum;
  851. $orderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  852. $orderModel->FREIGHT = 0;
  853. $orderModel->PAY_FREIGHT = 0;
  854. $orderModel->CONSIGNEE = $this->consignee;
  855. $orderModel->MOBILE = $this->acceptMobile;
  856. $orderModel->EMAIL = $this->email ?: $this->insertUserName . '@elken.net';
  857. $orderModel->PROVINCE = $this->province;
  858. $orderModel->LGA_NAME = $this->lgaName;
  859. $orderModel->CITY_NAME = $this->cityName;
  860. $orderModel->ADDRESS = $this->address;
  861. $orderModel->WAREHOUSE = $warehouse;
  862. $orderModel->STATUS = \Yii::$app->params['orderStatus']['notPaid']['value'];
  863. $orderModel->CREATED_AT = Date::nowTime();
  864. $orderModel->CREATE_USER = Info::getUserNameByUserId(\Yii::$app->user->id);
  865. $orderModel->ORDER_AMOUNT_STANDARD = $this->_decAmountStandard;
  866. $orderModel->PAY_AMOUNT_STANDARD = $this->_standardAmount;
  867. $orderModel->EXCHANGE_RATE = $userCurrencyRate;
  868. $orderModel->COUNTRY_ID = $userCountry['COUNTRY_ID'];
  869. $orderModel->CURRENCY_ID = $userCountry['LOCAL_CURRENCY_ID'] ?? 0;
  870. if ($this->province==1) {
  871. $orderModel->EXPRESS_TYPE = 1;
  872. }
  873. if (!$orderModel->save()) {
  874. throw new Exception(Form::formatErrorsForApi($orderModel->getErrors()));
  875. }
  876. // 加入商品到订单商品表
  877. foreach($this->_orderGoods as $key=>$value){
  878. $this->_orderGoods[$key]['ORDER_SN'] = $orderModel->SN;
  879. $this->_orderGoods[$key]['P_CALC_MONTH'] = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  880. }
  881. ApproachOrderGoods::batchInsert($this->_orderGoods);
  882. // 写入收货地址信息,判断province字段,当不为1时,插入收货地址信息
  883. $addressModel = new ReceiveAddress();
  884. if ($this->province != 1) {
  885. $addressModel->USER_ID = $this->_insertUserId;
  886. $addressModel->USER_NAME = $this->insertUserName;
  887. $addressModel->CONSIGNEE = $this->consignee;
  888. $addressModel->MOBILE = $this->acceptMobile;
  889. $addressModel->COUNTRY_ID = $userCountry['COUNTRY_ID'];
  890. $addressModel->PROVINCE = $this->province;
  891. $addressModel->CITY_NAME = $this->cityName;
  892. $addressModel->LGA_NAME = $this->lgaName;
  893. $addressModel->ADDRESS = $this->address;
  894. $addressModel->IS_DEFAULT = 1;
  895. if (!$addressModel->save()) {
  896. throw new Exception(Form::formatErrorsForApi($addressModel->getErrors()));
  897. }
  898. }
  899. return $orderModel;
  900. }
  901. /**
  902. * 删单
  903. * @return bool
  904. * @throws \yii\db\Exception
  905. */
  906. public function delete(){
  907. if(!$this->validate()){
  908. return false;
  909. }
  910. $transaction = \Yii::$app->db->beginTransaction();
  911. try {
  912. $oneOrder = $this->_oneOrder;
  913. // 首购单要删除会员
  914. if($this->type == self::TYPE_ZC){
  915. UserInfo::deleteUser($oneOrder['TO_USER_ID']);
  916. }
  917. // 如果是复销单的话,还需要考虑给会员的复销池减去金额
  918. elseif($this->type == self::TYPE_FX){
  919. // Reconsume::changePoolPV($oneOrder['TO_USER_ID'], -abs($oneOrder['DEC_PV']), ['REMARK'=>'删单扣除', 'DEAL_TYPE'=>Reconsume::TYPE_AUDIT_PV]);
  920. Reconsume::changePoolPV($oneOrder['TO_USER_ID'], -abs($oneOrder['DEC_PV']), ['REMARK'=>'Deletion deduction', 'DEAL_TYPE'=>Reconsume::TYPE_AUDIT_PV]);
  921. }
  922. $transaction->commit();
  923. } catch (Exception $e) {
  924. $transaction->rollBack();
  925. $this->addError('delete', $e->getMessage());
  926. return false;
  927. }
  928. return true;
  929. }
  930. /**
  931. * 添加会员分期付款记录
  932. * @param $allData
  933. * @return bool|UserInfo|null
  934. * @throws \yii\db\Exception
  935. */
  936. public function insertInstalment($zcResult){
  937. // 增加会员分期付款记录
  938. $instalment = new Instalment();
  939. $instalment->USER_ID = $zcResult->USER_ID;
  940. $instalment->STAGE = 1;
  941. $instalment->ORDER_TYPE = 'BD';
  942. $instalment->UPDATE_TIME = time();
  943. if (!$instalment->save()) {
  944. throw new Exception(Form::formatErrorsForApi($instalment->getErrors()));
  945. }
  946. return $instalment;
  947. }
  948. }