ApproachDeclarationForm.php 46 KB

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