ApproachDeclarationForm.php 44 KB

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