DeclarationForm.php 39 KB

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