DeclarationForm.php 49 KB

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