DeclarationForm.php 47 KB

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