DeclarationForm.php 48 KB

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