DeclarationForm.php 49 KB

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