DeclarationForm.php 49 KB

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