ApproachDeclarationForm.php 43 KB

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