DeclarationForm.php 49 KB

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