DeclarationForm.php 47 KB

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