DeclarationForm.php 49 KB

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