ApproachDeclarationForm.php 46 KB

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