DeclarationForm.php 49 KB

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