ApproachDeclarationForm.php 43 KB

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