ApproachDeclarationForm.php 46 KB

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