DeclarationForm.php 44 KB

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