DeclarationForm.php 39 KB

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