DeclarationForm.php 49 KB

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