DeclarationForm.php 46 KB

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