ApproachDeclarationForm.php 47 KB

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