DeclarationForm.php 47 KB

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