DeclarationForm.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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. // @todoneed 先屏蔽错误,这里因为导入数据确实网络关联,导致用户表找不到数据
  356. try {
  357. $parentBaseUser = Info::getBaseUserById($parentUserId);
  358. $this->_tempNetworkParentUser[$this->insertUserName][] = $parentBaseUser['USER_NAME'] ;
  359. unset($parentUserId, $parentBaseUser);
  360. } catch (\Exception $e) {
  361. file_put_contents('loopFindParentToNetwork_parentUserId',var_export([
  362. 'parentUserId' => $parentUserId
  363. ],true), FILE_APPEND);
  364. }
  365. }
  366. }
  367. unset($allParentUserIds);
  368. }
  369. return true;
  370. }
  371. /**
  372. * 开拓人是否在新加入会员的上级中
  373. * @return bool
  374. */
  375. private function recUserIsInNetworkParent(){
  376. return in_array($this->recUserName, $this->_tempNetworkParentUser[$this->insertUserName]);
  377. }
  378. /**
  379. * 判断区域是否正确
  380. * @param $attribute
  381. */
  382. public function isLocation($attribute){
  383. if($this->type == self::TYPE_ZC){
  384. if(!in_array($this->location, [1, 2, 3])){
  385. $this->addError($attribute, '市场必须在第1市场、第2市场或第三3市场');
  386. }
  387. $conUser = $recUser = null;
  388. if(isset($this->_tempParentUser[$this->conUserName])){
  389. $conUser = $this->_tempParentUser[$this->conUserName];
  390. }
  391. if(isset($this->_tempParentUser[$this->recUserName])){
  392. $recUser = $this->_tempParentUser[$this->recUserName];
  393. }
  394. if($conUser){
  395. // 判断指导老师相应的区位是否已满
  396. if($conUser['LOCATION'.$this->location]){
  397. if($this->insertUserName){
  398. $this->addError($attribute, '为'.$this->insertUserName.'报单,指导老师'.$this->conUserName.'的第'.$this->location.'市场已存在会员');
  399. } else {
  400. $this->addError($attribute, '指导老师'.$this->conUserName.'的第'.$this->location.'市场已存在会员');
  401. }
  402. return;
  403. }
  404. //市场顺序判断
  405. // if($conUser['CON_NUM'] == 0 && $this->location != 1){
  406. // if($this->insertUserName){
  407. // $this->addError($attribute, '为'.$this->insertUserName.'报单,市场必须为指导老师'.$this->conUserName.'下第一市场');
  408. // } else {
  409. // $this->addError($attribute, '市场必须为指导老师'.$this->conUserName.'下第一市场');
  410. // }
  411. // return;
  412. // }
  413. // if($conUser['CON_NUM'] == 1 && $this->location == 3){
  414. // if($this->insertUserName){
  415. // $this->addError($attribute, '为'.$this->insertUserName.'报单,市场必须为指导老师'.$this->conUserName.'第二市场');
  416. // } else {
  417. // $this->addError($attribute, '市场必须为指导老师'.$this->conUserName.'第二市场');
  418. // }
  419. // return;
  420. // }
  421. $this->_tempParentUser[$this->conUserName]['CON_NUM'] += 1;
  422. $this->_tempParentUser[$this->conUserName]['LOCATION'.$this->location] = true;
  423. }
  424. }
  425. }
  426. /**
  427. * 判断会员是否可加入(通过redis结合数据库判断上级会员的情况)
  428. * @param $attribute
  429. */
  430. public function isCanAddUser($attribute){
  431. if($this->type == self::TYPE_ZC){
  432. // 从数据库查看接点会员是否在
  433. if(!isset($this->_tempParentUser[$this->conUserName])){
  434. $conUserTemp = UserInfo::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => $this->conUserName]);
  435. if(!$conUserTemp){
  436. $this->addError($attribute, '为'.$this->insertUserName.'报单,指导老师'.$this->conUserName.'不存在');
  437. return ;
  438. }
  439. $conUserTemp['CON_NUM'] = UserNetwork::firstFloorChildNum($conUserTemp['USER_ID']);
  440. $conUserTemp['REC_NUM'] = UserRelation::firstFloorChildNum($conUserTemp['USER_ID']);
  441. $this->_tempParentUser[$this->conUserName] = [
  442. 'USER_NAME' => $this->conUserName,
  443. //'ZC_PV' => $conUserTemp['ZC_PV'],
  444. 'CON_USER_NAME' => Info::getUserNameByUserId($conUserTemp['CON_UID']),
  445. 'REC_USER_NAME' => Info::getUserNameByUserId($conUserTemp['REC_UID']),
  446. 'CON_NUM' => $conUserTemp['CON_NUM'],
  447. 'REC_NUM' => $conUserTemp['REC_NUM'],
  448. 'SYSTEM_ID' => $conUserTemp['SYSTEM_ID'],
  449. 'LOCATION' => UserNetwork::getLocation($conUserTemp['USER_ID'], $conUserTemp['CON_UID']),
  450. ];
  451. // 把该会员下面的5个区是否存在会员都付上
  452. for($i=1;$i<=5;$i++){
  453. $this->_tempParentUser[$this->conUserName]["LOCATION$i"] = UserNetwork::issetUserInLocation($conUserTemp['USER_ID'], $i);
  454. }
  455. }
  456. $conUser = $this->_tempParentUser[$this->conUserName];
  457. // 开拓人
  458. if(!isset($this->_tempParentUser[$this->recUserName])){
  459. $recUserTemp = UserInfo::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => $this->recUserName]);
  460. if(!$recUserTemp){
  461. $this->addError($attribute, '为'.$this->insertUserName.'报单,开拓人'.$this->recUserName.'不存在');
  462. return ;
  463. }
  464. $recUserTemp['CON_NUM'] = UserNetwork::firstFloorChildNum($recUserTemp['USER_ID']);
  465. $recUserTemp['REC_NUM'] = UserRelation::firstFloorChildNum($recUserTemp['USER_ID']);
  466. $this->_tempParentUser[$this->recUserName] = [
  467. 'USER_NAME' => $this->recUserName,
  468. //'ZC_PV' => $recUserTemp['ZC_PV'],
  469. 'CON_USER_NAME' => Info::getUserNameByUserId($recUserTemp['CON_UID']),
  470. 'REC_USER_NAME' => Info::getUserNameByUserId($recUserTemp['REC_UID']),
  471. 'CON_NUM' => $recUserTemp['CON_NUM'],
  472. 'REC_NUM' => $recUserTemp['REC_NUM'],
  473. 'SYSTEM_ID' => $recUserTemp['SYSTEM_ID'],
  474. 'LOCATION' => UserNetwork::getLocation($recUserTemp['USER_ID'], $recUserTemp['CON_UID']),
  475. ];
  476. // 把该会员下面的5个区是否存在会员都付上
  477. for($i=1;$i<=5;$i++){
  478. $this->_tempParentUser[$this->recUserName]["LOCATION$i"] = UserNetwork::issetUserInLocation($recUserTemp['USER_ID'], $i);
  479. }
  480. }
  481. $recUser = $this->_tempParentUser[$this->recUserName];
  482. // 判断接点会员的下级会员是否大于三个,如果大于三个则不允许
  483. if($conUser['CON_NUM'] >= 3){
  484. $this->addError($attribute, '为'.$this->insertUserName.'报单,指导老师'.$this->conUserName.'下级点位已满');
  485. return;
  486. }
  487. // 判断指导老师相应的区位是否已满
  488. if($conUser['LOCATION'.$this->location]){
  489. $this->addError($attribute, '为'.$this->insertUserName.'报单,指导老师'.$this->conUserName.'该市场已存在会员');
  490. return;
  491. }
  492. // if($conUser['CON_NUM'] == 0 && $this->location != 1){
  493. // $this->addError($attribute, '为'.$this->insertUserName.'报单,市场必须为指导老师'.$this->conUserName.'下第一市场');
  494. // return;
  495. // }
  496. // 把指导老师的变量的相关数量增加数量
  497. // $this->_tempParentUser[$this->conUserName]['CON_NUM'] += 1;
  498. // $this->_tempParentUser[$this->conUserName]['LOCATION'.$this->location] = true;
  499. // $this->_tempParentUser[$this->recUserName]['REC_NUM'] += 1;
  500. // 把自己加入到临时上级会员数组中
  501. $this->_tempParentUser[$this->insertUserName] = [
  502. 'USER_NAME' => $this->insertUserName,
  503. //'ZC_PV' => $this->decPv,
  504. 'CON_USER_NAME' => $this->conUserName,
  505. 'REC_USER_NAME' => $this->recUserName,
  506. 'CON_NUM' => 0,
  507. 'REC_NUM' => 0,
  508. 'SYSTEM_ID' => $recUser['SYSTEM_ID'],
  509. 'LOCATION' => $this->location,
  510. 'LOCATION1' => 0,
  511. 'LOCATION2' => 0,
  512. 'LOCATION3' => 0,
  513. 'LOCATION4' => 0,
  514. 'LOCATION5' => 0,
  515. ];
  516. }
  517. }
  518. /**
  519. * 报单类型
  520. * @param $attribute
  521. * @param $params
  522. */
  523. public function isType($attribute, $params){
  524. if(array_key_exists($this->type, $this->_types)){
  525. if($this->type == self::TYPE_ZC){
  526. if($this->scenario == 'userDec'){
  527. if(!$this->insertUserName) $this->addError($attribute, '首购必须填写加入会员编号');
  528. }
  529. if(!$this->insertUserName) $this->addError($attribute, '首购必须填写加入会员编号');
  530. if(!$this->insertUserIdCard) $this->addError($attribute, '首购必须填写加入会员的身份证号');
  531. if(!$this->conUserName) $this->addError($attribute, '首购必须填写加入会员的指导老师编号');
  532. if(!$this->recUserName) $this->addError($attribute, '首购必须填写加入会员的开拓人编号');
  533. if(!$this->location) $this->addError($attribute, '首购必须填写加入会员的市场');
  534. }
  535. } else {
  536. $this->addError($attribute, '报单类型不正确');
  537. }
  538. }
  539. /**
  540. * 检验相同身份证 会员是否同一体系内(接点)
  541. * @param $attribute
  542. */
  543. public function isSameSystem($attribute){
  544. if(isset($this->_tempParentUser[$this->conUserName])){
  545. // 新加入会员的身份证号对应网内的其他会员
  546. $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]);
  547. if(count($otherUser)>=7){
  548. $this->addError($attribute, '同一个身份证限制只能报7单');
  549. }
  550. if($otherUser){
  551. $flipParent = array_flip(array_filter($this->_tempNetworkParentUser[$this->insertUserName]));
  552. $isExsit = false;
  553. foreach ($otherUser as $conUser){
  554. if(isset($flipParent[$conUser['USER_NAME']])){
  555. $isExsit = true;
  556. break;
  557. }
  558. }
  559. if (!$isExsit) {
  560. $this->addError($attribute, '为' . $this->insertUserName . '报单,身份证号码相同的会员'.$otherUser[0]['USER_NAME'].'不在' . $this->insertUserName . '的安置网上级中');
  561. return ;
  562. }
  563. }
  564. } else {
  565. $this->addError($attribute, '为'.$this->insertUserName.'报单,接点人'.$this->conUserName.'不存在');
  566. }
  567. }
  568. /**
  569. * 添加报单
  570. * @param $allData
  571. * @return bool|null
  572. * @throws Exception
  573. * @throws \yii\db\Exception
  574. */
  575. public function add($allData){
  576. if(!$this->validate()){
  577. return null;
  578. }
  579. // 首购单,需要添加会员操作
  580. if($this->type == self::TYPE_ZC){
  581. if (preg_match("/[\x7f-\xff]/", $this->insertUserName)) { //判断字符串中是否有中文
  582. throw new Exception('会员编号中不能含有汉字');
  583. }
  584. //报单商品及PV判断
  585. $decLevelConfig = Cache::getDecLevelConfig();
  586. $decLevel = $decLevelConfig[$this->decLv];
  587. $toDecLevel = $this->decLv;
  588. if(!$this->decLv){
  589. throw new Exception('请选择报单级别');
  590. }
  591. if($this->decWay==1) {
  592. $decPackage = DeclarationPackage::findOneAsArray('ID=:ID', [':ID'=>$this->packageId]);
  593. $this->_decAmount = $decPackage['AMOUNT'];
  594. $this->_decPv = $decPackage['PV'];
  595. $this->_orderGoods[] = [
  596. 'GOODS_ID' => $this->packageId,
  597. 'PRICE' => $this->_decAmount,
  598. 'REAL_PRICE' => $this->_decAmount,
  599. 'PV' => $this->_decPv,
  600. 'REAL_PV' => $this->_decPv,
  601. 'BUY_NUMS' => 1,
  602. 'SKU_CODE' => $decPackage['PACKAGE_NO'],
  603. 'GOODS_TITLE' => $decPackage['PACKAGE_NAME']
  604. ];
  605. }else{
  606. $ids = $this->goodsId;
  607. $totalAmount = 0;
  608. $totalPv = 0;
  609. foreach ($this->goodsNum as $k => $v) {
  610. if ($v) {
  611. $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1',[':ID'=> $ids[$k]]);
  612. if($goods['STORE_NUMS']>0){
  613. $totalAmount += $goods['SELL_PRICE'] * intval($v);
  614. $totalPv += $goods['PRICE_PV'] * intval($v);
  615. $this->_orderGoods[] = [
  616. 'GOODS_ID' => $goods['ID'],
  617. 'PRICE' => $goods['SELL_PRICE'],
  618. 'REAL_PRICE' => $goods['SELL_PRICE'],
  619. 'PV' => $goods['PRICE_PV'],
  620. 'REAL_PV' => $goods['PRICE_PV'],
  621. 'POINT' => $goods['POINT'],
  622. 'BUY_NUMS' => intval($v),
  623. 'SKU_CODE' => $goods['GOODS_NO'],
  624. 'GOODS_TITLE' => $goods['GOODS_NAME']
  625. ];
  626. }
  627. }
  628. }
  629. if($totalPv<$decLevel['PERF']){
  630. throw new Exception('自选商品总PV不能小于所选报单级别PV');
  631. }
  632. foreach ($decLevelConfig as $key=>$val){
  633. if($totalPv>=$val['PERF']){
  634. $toDecLevel = $key;
  635. }
  636. }
  637. if($this->decLv!=$toDecLevel){
  638. throw new Exception('自选商品总PV不能超过已选级别下一个级别的PV值');
  639. }
  640. $this->_decAmount = $totalAmount;
  641. $this->_decPv = $totalPv;
  642. }
  643. //看现金余额是否充足
  644. $loginUserId = \Yii::$app->user->id;
  645. if (Cash::getAvailableBalance($loginUserId) < $this->_decAmount){
  646. throw new Exception('报单人现金不足,无法完成报单');
  647. }
  648. if(!($zcResult = $this->addUser($allData))) {
  649. throw new Exception(Form::formatErrorsForApi($this->_userForm->getErrors()));
  650. }
  651. if(!($decResult = $this->addDecOrder())) {
  652. throw new Exception(Form::formatErrorsForApi($decResult->getErrors()));
  653. }
  654. }
  655. return true;
  656. }
  657. /**
  658. * 添加会员
  659. * @param $allData
  660. * @return bool|UserInfo|null
  661. * @throws \yii\db\Exception
  662. */
  663. public function addUser($allData){
  664. $period = Period::instance();
  665. // 增加会员
  666. $user = new User();
  667. $user->USER_NAME = $this->insertUserName;
  668. $user->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
  669. $user->PAY_PASSWORD = \Yii::$app->security->generatePasswordHash($this->payPassword);
  670. $user->NATION = 0;
  671. $user->REAL_NAME = $this->realName;
  672. $user->ID_CARD = $this->insertUserIdCard;
  673. $user->MOBILE = $this->mobile;
  674. $user->ADDRESS = $this->address ? $this->address : '无';
  675. $user->OPEN_BANK = $this->openBank;
  676. $user->BANK_ADDRESS = $this->bankAddress;
  677. $user->BANK_NO = $this->bankNo;
  678. $user->BANK_PROVINCE = $this->bankProvince ?? '0';
  679. $user->BANK_CITY = $this->bankCity ?? '0';
  680. $user->BANK_COUNTY = $this->bankCounty ?? '0';
  681. $user->CREATED_AT = Date::nowTime();
  682. $user->STATUS = 1;
  683. $user->DEC_LV = $this->decLv;
  684. $user->LAST_DEC_LV = $this->decLv;
  685. $user->EMP_LV = EmployLevel::getDefaultLevelId();
  686. $user->PROVINCE = $this->province ?? '0';
  687. $user->CITY = $this->city ?? '0';
  688. $user->COUNTY = $this->county ?? '0';
  689. $user->AVATAR = 'avatar/1.png';
  690. $user->IS_DEC = 0;
  691. $user->DEC_ID = $this->_decId ?? null;
  692. $user->DEC_ROLE_ID = $this->decRoleId ?? null;
  693. $user->PERIOD_AT = $period->getNowPeriodNum();
  694. $user->IS_DIRECT_SELLER = 0;
  695. $user->VERIFIED = 1;
  696. $user->VERIFIED_AT = Date::nowTime();
  697. if (!$user->save()) {
  698. throw new Exception(Form::formatErrorsForApi($user->getErrors()));
  699. }
  700. $this->_insertUserId = $user->ID;
  701. $userForm = new UserForm();
  702. $this->_userForm = $userForm;
  703. $userForm->scenario = 'addWithUserName';
  704. $userForm->userId = $this->_insertUserId;
  705. $userForm->userName = $this->insertUserName;
  706. $userForm->zcPv = $this->_decPv;
  707. $userForm->zcAmount = $this->_decAmount;
  708. $userForm->conUserName = $this->conUserName;
  709. $userForm->recUserName = $this->recUserName;
  710. $userForm->location = $this->location;
  711. $userForm->idCard = $this->insertUserIdCard;
  712. $userForm->allData = $allData;
  713. if(!$userForm->validate()){
  714. $this->addErrors($userForm->getErrors());
  715. return false;
  716. }
  717. if($result = $userForm->add()){
  718. return $result;
  719. } else {
  720. return false;
  721. }
  722. }
  723. /**
  724. * 添加报单订单
  725. * @return bool|UserInfo|null
  726. * @throws \yii\db\Exception
  727. */
  728. public function addDecOrder(){
  729. $periodObj = Period::instance();
  730. $nowPeriodNum = $periodObj->getNowPeriodNum();
  731. $nowCalcMonth = $periodObj->getYearMonth($nowPeriodNum);
  732. $ord = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 10);
  733. // 加入报单信息
  734. $decOrderModel = new DecOrder();
  735. $decOrderModel->DEC_SN = 'DS'.$ord;
  736. $decOrderModel->ORDER_SN = 'OS'.$ord;
  737. $decOrderModel->TYPE = $this->type;
  738. $decOrderModel->USER_ID = \Yii::$app->user->id;
  739. $decOrderModel->TO_USER_ID = $this->_insertUserId;
  740. $decOrderModel->DEC_AMOUNT = $this->_decAmount;
  741. $decOrderModel->DEC_PV = $this->_decPv;
  742. $decOrderModel->PERIOD_NUM = $nowPeriodNum;
  743. $decOrderModel->CALC_MONTH = $nowCalcMonth;
  744. $decOrderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  745. $decOrderModel->PAID_WALLET = 'cash';
  746. $decOrderModel->CON_USER_ID = Info::getUserIdByUserName($this->conUserName);
  747. $decOrderModel->REC_USER_ID = Info::getUserIdByUserName($this->recUserName);
  748. $decOrderModel->DEC_ID = $this->_decId;
  749. $decOrderModel->IS_DEL = 0;
  750. $decOrderModel->CREATED_AT = Date::nowTime();
  751. if(!$decOrderModel->save()){
  752. throw new Exception(Form::formatErrorsForApi($decOrderModel->getErrors()));
  753. }
  754. // 加入订单信息
  755. $warehouse = Region::getWarehouseByCode($this->province);//仓库
  756. if(!$warehouse){
  757. throw new Exception('地区暂时不支持配送,具体联系客服');
  758. }
  759. $orderModel = new Order();
  760. $orderModel->SN = 'OS'.$ord;
  761. $orderModel->DEC_SN = 'DS'.$ord;
  762. $orderModel->ORDER_TYPE = $this->type;
  763. $orderModel->USER_ID = $this->_insertUserId;
  764. $orderModel->USER_NAME = $this->insertUserName;
  765. $orderModel->ORDER_AMOUNT = $this->_decAmount;
  766. $orderModel->PV = $this->_decPv;
  767. $orderModel->PAY_AMOUNT = $this->_decAmount;
  768. $orderModel->PAY_PV = $this->_decPv;
  769. $orderModel->PAY_AT = Date::nowTime();
  770. $orderModel->PAY_TYPE = 'cash';
  771. $orderModel->PERIOD_NUM = $nowPeriodNum;
  772. $orderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  773. $orderModel->FREIGHT = 0;
  774. $orderModel->PAY_FREIGHT = 0;
  775. $orderModel->CONSIGNEE = $this->consignee;
  776. $orderModel->MOBILE = $this->acceptMobile;
  777. $orderModel->PROVINCE = $this->province;
  778. $orderModel->CITY = $this->city;
  779. $orderModel->COUNTY = $this->county;
  780. $orderModel->ADDRESS = $this->address;
  781. $orderModel->WAREHOUSE = $warehouse;
  782. $orderModel->STATUS = 1;
  783. $orderModel->CREATED_AT = Date::nowTime();
  784. $orderModel->CREATE_USER = Info::getUserNameByUserId(\Yii::$app->user->id);
  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. //写入收货地址信息
  795. $addressModel = new ReceiveAddress();
  796. $addressModel->USER_ID = $this->_insertUserId;
  797. $addressModel->USER_NAME = $this->insertUserName;
  798. $addressModel->CONSIGNEE = $this->consignee;
  799. $addressModel->MOBILE = $this->acceptMobile;
  800. $addressModel->PROVINCE = $this->province;
  801. $addressModel->CITY = $this->city;
  802. $addressModel->COUNTY = $this->county;
  803. $addressModel->ADDRESS = $this->address;
  804. $addressModel->IS_DEFAULT = 1;
  805. if(!$addressModel->save()){
  806. throw new Exception(Form::formatErrorsForApi($addressModel->getErrors()));
  807. }
  808. //扣报单人现金钱包
  809. Cash::changeUserCash(\Yii::$app->user->id, 'CASH', -abs($this->_decAmount), ['REMARK' =>'为'.$this->insertUserName.'报单']);
  810. return $addressModel;
  811. }
  812. /**
  813. * 删单
  814. * @return bool
  815. * @throws \yii\db\Exception
  816. */
  817. public function delete(){
  818. if(!$this->validate()){
  819. return false;
  820. }
  821. $transaction = \Yii::$app->db->beginTransaction();
  822. try {
  823. $oneOrder = $this->_oneOrder;
  824. // 首购单要删除会员
  825. if($this->type == self::TYPE_ZC){
  826. UserInfo::deleteUser($oneOrder['TO_USER_ID']);
  827. }
  828. // 如果是复销单的话,还需要考虑给会员的复销池减去金额
  829. elseif($this->type == self::TYPE_FX){
  830. Reconsume::changePoolPV($oneOrder['TO_USER_ID'], -abs($oneOrder['DEC_PV']), ['REMARK'=>'删单扣除', 'DEAL_TYPE'=>Reconsume::TYPE_AUDIT_PV]);
  831. }
  832. $transaction->commit();
  833. } catch (Exception $e) {
  834. $transaction->rollBack();
  835. $this->addError('delete', $e->getMessage());
  836. return false;
  837. }
  838. return true;
  839. }
  840. }