DeclarationForm.php 49 KB

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