ApproachDeclarationForm.php 45 KB

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