DeclarationForm.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Cache;
  5. use common\helpers\Date;
  6. use common\helpers\Form;
  7. use common\helpers\user\Cash;
  8. use common\helpers\user\Reconsume;
  9. use common\helpers\user\Info;
  10. use common\models\BaUser;
  11. use common\models\DeclarationPackage;
  12. use common\models\DecOrder;
  13. use common\models\EmployLevel;
  14. use common\models\Order;
  15. use common\models\OrderGoods;
  16. use common\models\Period;
  17. use common\models\ReceiveAddress;
  18. use common\models\ReconsumePool;
  19. use common\models\ReconsumePoolFlow;
  20. use common\models\DeclarationLevel;
  21. use common\models\Region;
  22. use common\models\ShopGoods;
  23. use common\models\User;
  24. use common\models\UserInfo;
  25. use common\models\UserNetwork;
  26. use common\models\UserRelation;
  27. use Yii;
  28. use yii\base\Exception;
  29. /**
  30. * Login form
  31. */
  32. class DeclarationForm extends Model
  33. {
  34. public $type;
  35. public $decLv;
  36. public $decWay;
  37. public $packageId;
  38. public $goodsId;
  39. public $goodsNum;
  40. public $insertUserName;
  41. public $password;
  42. public $payPassword;
  43. public $realName;
  44. public $insertUserIdCard;
  45. public $mobile;
  46. public $email;
  47. public $address;
  48. public $openBank;
  49. public $bankAddress;
  50. public $bankNo;
  51. public $bankProvince;
  52. public $bankCity;
  53. public $bankCounty;
  54. public $conUserName;
  55. public $recUserName;
  56. public $decUserName;
  57. public $location;
  58. public $consignee;
  59. public $acceptMobile;
  60. public $province;
  61. public $city;
  62. public $county;
  63. public $cityName;
  64. public $lgaName;
  65. public $decType;
  66. // 传过来的全部数据
  67. public $allData;
  68. private $_decId;
  69. public $_insertUserId;
  70. private $_decAmount;
  71. private $_decPv;
  72. private $_orderGoods;
  73. private $_standardAmount;
  74. private $_decAmountStandard;
  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' => 'Stockist user name', // 报单中心用户名
  153. 'conUid' => 'Instructor Member ID', // 指导老师会员ID
  154. 'recUid' => 'Sponsor Member ID', // 开拓人会员ID
  155. 'location' => 'market', // 市场
  156. 'consignee' => 'Recipient', // 收货人
  157. 'acceptMobile' => 'Recipient 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, Yii::t('app', 'stockistDoesNotExist'));
  185. return false;
  186. } else {
  187. if ($this->decType !== 'ba') {
  188. // 判断报单中心是否在新加入会员的安置网上级中
  189. $this->loopFindParentToNetwork($this->insertUserName);
  190. //反转数组,in_array搜索错误
  191. //in_array($this->decUserName, $this->_tempNetworkParentUser[$this->insertUserName]);
  192. $flipParent = array_flip(array_filter($this->_tempNetworkParentUser[$this->insertUserName]));
  193. // var_dump($flipParent);
  194. // echo $this->insertUserName.'=='.$this->decUserName;
  195. // exit;
  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->insertUserName) {
  313. if ($this->decType !== 'ba') {
  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. // 把自己加入到临时上级会员数组中
  323. $this->_tempParentUser[$this->insertUserName] = [
  324. 'USER_NAME' => $this->insertUserName,
  325. //'ZC_PV' => $this->decPv,
  326. 'CON_USER_NAME' => $this->conUserName,
  327. 'REC_USER_NAME' => $this->recUserName,
  328. 'CON_NUM' => 0,
  329. 'REC_NUM' => 0,
  330. 'SYSTEM_ID' => $recUser['SYSTEM_ID'],
  331. 'LOCATION' => $this->location,
  332. 'LOCATION1' => 0,
  333. 'LOCATION2' => 0,
  334. 'LOCATION3' => 0,
  335. 'LOCATION4' => 0,
  336. 'LOCATION5' => 0,
  337. ];
  338. }
  339. }
  340. /**
  341. * 循环把所有新加入的会员的上级加入到临时变量中
  342. * @param null $conUserName
  343. * @return bool
  344. */
  345. private function loopFindParentToNetwork($conUserName = null) {
  346. if($conUserName == null ){
  347. $conUserName = $this->insertUserName;
  348. }
  349. $findUserKey = array_search($conUserName, array_column($this->allData, 'insertUserName'));
  350. // 如果有这个新加入的会员,则查找其上级接点
  351. if($findUserKey !== false){
  352. if(strtoupper($this->allData[$findUserKey]['type']) == 'ZC'){
  353. $this->_tempNetworkParentUser[$this->insertUserName][] = $this->allData[$findUserKey]['conUserName'];
  354. $this->loopFindParentToNetwork($this->allData[$findUserKey]['conUserName']);
  355. }
  356. }
  357. // 如果没有这个新加入的接点,
  358. else {
  359. if($this->insertUserName == $conUserName){
  360. // $this->addError('recUserName', '新加入的会员不存在');
  361. $this->addError('recUserName', Yii::t('app', 'newMemberDoesNotExist'));
  362. return false;
  363. }
  364. // 去数据库里查找这个会员的所有上级
  365. // $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();
  366. $baseUser = Info::getBaseUserByUserName($conUserName);
  367. $userNetworkInfo = UserNetwork::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $baseUser['ID']]);
  368. unset($baseUser);
  369. $allParentUserIdsArr = explode(',', $userNetworkInfo['PARENT_UIDS']);
  370. unset($userNetworkInfo);
  371. $allParentUserIds = array_reverse($allParentUserIdsArr);
  372. unset($allParentUserIdsArr);
  373. if($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. 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, Yii::t('app', 'pleaseSelectMarket'));
  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. //市场顺序判断
  421. // if($conUser['CON_NUM'] == 0 && $this->location != 1){
  422. // if($this->insertUserName){
  423. // $this->addError($attribute, '为'.$this->insertUserName.'报单,市场必须为指导老师'.$this->conUserName.'下第一市场');
  424. // } else {
  425. // $this->addError($attribute, '市场必须为指导老师'.$this->conUserName.'下第一市场');
  426. // }
  427. // return;
  428. // }
  429. // if($conUser['CON_NUM'] == 1 && $this->location == 3){
  430. // if($this->insertUserName){
  431. // $this->addError($attribute, '为'.$this->insertUserName.'报单,市场必须为指导老师'.$this->conUserName.'第二市场');
  432. // } else {
  433. // $this->addError($attribute, '市场必须为指导老师'.$this->conUserName.'第二市场');
  434. // }
  435. // return;
  436. // }
  437. $this->_tempParentUser[$this->conUserName]['CON_NUM'] += 1;
  438. $this->_tempParentUser[$this->conUserName]['LOCATION'.$this->location] = true;
  439. }
  440. }
  441. }
  442. /**
  443. * 判断会员是否可加入(通过redis结合数据库判断上级会员的情况)
  444. * @param $attribute
  445. */
  446. public function isCanAddUser($attribute){
  447. if($this->type == self::TYPE_ZC){
  448. // 从数据库查看接点会员是否在
  449. if(!isset($this->_tempParentUser[$this->conUserName])){
  450. $conUserTemp = UserInfo::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => $this->conUserName]);
  451. if(!$conUserTemp){
  452. // $this->addError($attribute, '为'.$this->insertUserName.'报单,指导老师'.$this->conUserName.'不存在');
  453. $this->addError($attribute, 'To'.$this->insertUserName.'Entry,Instructor'.$this->conUserName.'not in exist');
  454. return ;
  455. }
  456. $conUserTemp['CON_NUM'] = UserNetwork::firstFloorChildNum($conUserTemp['USER_ID']);
  457. $conUserTemp['REC_NUM'] = UserRelation::firstFloorChildNum($conUserTemp['USER_ID']);
  458. $this->_tempParentUser[$this->conUserName] = [
  459. 'USER_NAME' => $this->conUserName,
  460. //'ZC_PV' => $conUserTemp['ZC_PV'],
  461. 'CON_USER_NAME' => Info::getUserNameByUserId($conUserTemp['CON_UID']),
  462. 'REC_USER_NAME' => Info::getUserNameByUserId($conUserTemp['REC_UID']),
  463. 'CON_NUM' => $conUserTemp['CON_NUM'],
  464. 'REC_NUM' => $conUserTemp['REC_NUM'],
  465. 'SYSTEM_ID' => $conUserTemp['SYSTEM_ID'],
  466. 'LOCATION' => UserNetwork::getLocation($conUserTemp['USER_ID'], $conUserTemp['CON_UID']),
  467. ];
  468. // 把该会员下面的5个区是否存在会员都付上
  469. for($i=1;$i<=5;$i++){
  470. $this->_tempParentUser[$this->conUserName]["LOCATION$i"] = UserNetwork::issetUserInLocation($conUserTemp['USER_ID'], $i);
  471. }
  472. }
  473. $conUser = $this->_tempParentUser[$this->conUserName];
  474. // 开拓人
  475. if(!isset($this->_tempParentUser[$this->recUserName])){
  476. $recUserTemp = UserInfo::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => $this->recUserName]);
  477. if(!$recUserTemp){
  478. // $this->addError($attribute, '为'.$this->insertUserName.'报单,开拓人'.$this->recUserName.'不存在');
  479. $this->addError($attribute, 'To'.$this->insertUserName.'Entry, Sponsor'.$this->recUserName.'not in exist');
  480. return ;
  481. }
  482. $recUserTemp['CON_NUM'] = UserNetwork::firstFloorChildNum($recUserTemp['USER_ID']);
  483. $recUserTemp['REC_NUM'] = UserRelation::firstFloorChildNum($recUserTemp['USER_ID']);
  484. $this->_tempParentUser[$this->recUserName] = [
  485. 'USER_NAME' => $this->recUserName,
  486. //'ZC_PV' => $recUserTemp['ZC_PV'],
  487. 'CON_USER_NAME' => Info::getUserNameByUserId($recUserTemp['CON_UID']),
  488. 'REC_USER_NAME' => Info::getUserNameByUserId($recUserTemp['REC_UID']),
  489. 'CON_NUM' => $recUserTemp['CON_NUM'],
  490. 'REC_NUM' => $recUserTemp['REC_NUM'],
  491. 'SYSTEM_ID' => $recUserTemp['SYSTEM_ID'],
  492. 'LOCATION' => UserNetwork::getLocation($recUserTemp['USER_ID'], $recUserTemp['CON_UID']),
  493. ];
  494. // 把该会员下面的5个区是否存在会员都付上
  495. for($i=1;$i<=5;$i++){
  496. $this->_tempParentUser[$this->recUserName]["LOCATION$i"] = UserNetwork::issetUserInLocation($recUserTemp['USER_ID'], $i);
  497. }
  498. }
  499. $recUser = $this->_tempParentUser[$this->recUserName];
  500. // 判断接点会员的下级会员是否大于三个,如果大于三个则不允许
  501. if($conUser['CON_NUM'] >= 3){
  502. // $this->addError($attribute, '为'.$this->insertUserName.'报单,指导老师'.$this->conUserName.'下级点位已满');
  503. $this->addError($attribute, 'To'.$this->insertUserName.'Entry, Sponsor'.$this->conUserName.'lower level is full');
  504. return;
  505. }
  506. // 判断指导老师相应的区位是否已满
  507. if($conUser['LOCATION'.$this->location]){
  508. // $this->addError($attribute, '为'.$this->insertUserName.'报单,指导老师'.$this->conUserName.'该市场已存在会员');
  509. $this->addError($attribute, 'To'.$this->insertUserName.'Entry, Sponsor'.$this->conUserName.'Members already exist in this market');
  510. return;
  511. }
  512. // if($conUser['CON_NUM'] == 0 && $this->location != 1){
  513. // $this->addError($attribute, '为'.$this->insertUserName.'报单,市场必须为指导老师'.$this->conUserName.'下第一市场');
  514. // return;
  515. // }
  516. // 把指导老师的变量的相关数量增加数量
  517. // $this->_tempParentUser[$this->conUserName]['CON_NUM'] += 1;
  518. // $this->_tempParentUser[$this->conUserName]['LOCATION'.$this->location] = true;
  519. // $this->_tempParentUser[$this->recUserName]['REC_NUM'] += 1;
  520. // 把自己加入到临时上级会员数组中
  521. $this->_tempParentUser[$this->insertUserName] = [
  522. 'USER_NAME' => $this->insertUserName,
  523. //'ZC_PV' => $this->decPv,
  524. 'CON_USER_NAME' => $this->conUserName,
  525. 'REC_USER_NAME' => $this->recUserName,
  526. 'CON_NUM' => 0,
  527. 'REC_NUM' => 0,
  528. 'SYSTEM_ID' => $recUser['SYSTEM_ID'],
  529. 'LOCATION' => $this->location,
  530. 'LOCATION1' => 0,
  531. 'LOCATION2' => 0,
  532. 'LOCATION3' => 0,
  533. 'LOCATION4' => 0,
  534. 'LOCATION5' => 0,
  535. ];
  536. }
  537. }
  538. /**
  539. * 报单类型
  540. * @param $attribute
  541. * @param $params
  542. */
  543. public function isType($attribute, $params){
  544. if(array_key_exists($this->type, $this->_types)){
  545. if($this->type == self::TYPE_ZC){
  546. if($this->scenario == 'userDec'){
  547. if(!$this->insertUserName) $this->addError($attribute, Yii::t('app', 'MembershipNumberFilledInitialPurchase'));
  548. }
  549. if(!$this->insertUserName) $this->addError($attribute, Yii::t('app', 'MembershipNumberFilledInitialPurchase'));
  550. // if(!$this->insertUserIdCard) $this->addError($attribute, 'For the first purchase, the ID number of the member must be filled in');//首购必须填写加入会员的身份证号
  551. if(!$this->conUserName) $this->addError($attribute, Yii::t('app', 'fillTheInstructorNumberTheMember'));
  552. if(!$this->recUserName) $this->addError($attribute, Yii::t('app', 'sponsorNumberMustBeFilled'));
  553. if(!$this->location) $this->addError($attribute, Yii::t('app', 'beFilledTheMarketMember'));
  554. }
  555. } else {
  556. $this->addError($attribute, Yii::t('app', 'incorrectEntryType'));
  557. }
  558. }
  559. /**
  560. * 检验相同身份证 会员是否同一体系内(接点)
  561. * @param $attribute
  562. */
  563. // public function isSameSystem($attribute){
  564. // if(isset($this->_tempParentUser[$this->conUserName])){
  565. // // 新加入会员的身份证号对应网内的其他会员
  566. // $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]);
  567. // if(count($otherUser)>=7){
  568. // $this->addError($attribute, 'Only 7 documents can be reported for the same ID card');//同一个身份证限制只能报7单
  569. // }
  570. // if($otherUser){
  571. // $flipParent = array_flip(array_filter($this->_tempNetworkParentUser[$this->insertUserName]));
  572. // $isExsit = false;
  573. // foreach ($otherUser as $conUser){
  574. // if(isset($flipParent[$conUser['USER_NAME']])){
  575. // $isExsit = true;
  576. // break;
  577. // }
  578. // }
  579. // if (!$isExsit) {
  580. //// $this->addError($attribute, '为' . $this->insertUserName . '报单,身份证号码相同的会员'.$otherUser[0]['USER_NAME'].'不在' . $this->insertUserName . '的安置网上级中');
  581. // $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');
  582. // return ;
  583. // }
  584. // }
  585. // } else {
  586. //// $this->addError($attribute, '为'.$this->insertUserName.'报单,接点人'.$this->conUserName.'不存在');
  587. // $this->addError($attribute, 'To'.$this->insertUserName.'Entry,Contact person'.$this->conUserName.'not in exist');
  588. // }
  589. // }
  590. /**
  591. * 添加报单
  592. * @param $allData
  593. * @return bool|null
  594. * @throws Exception
  595. * @throws \yii\db\Exception
  596. */
  597. public function add($allData){
  598. if(!$this->validate()){
  599. return null;
  600. }
  601. // 首购单,需要添加会员操作
  602. if($this->type == self::TYPE_ZC){
  603. if (preg_match("/[\x7f-\xff]/", $this->insertUserName)) { //判断字符串中是否有中文
  604. throw new Exception(Yii::t('app', 'memberNumberCanNotContainChineseCharacters'));
  605. }
  606. //报单商品及PV判断
  607. $decLevelConfig = Cache::getDecLevelConfig();
  608. $decLevel = $decLevelConfig[$this->decLv];
  609. $toDecLevel = $this->decLv;
  610. if(!$this->decLv){
  611. throw new Exception(Yii::t('app', 'pleaseSelectTheEntryLevel'));
  612. }
  613. if($this->decWay==1) {
  614. $decPackage = DeclarationPackage::findOneAsArray('ID=:ID', [':ID'=>$this->packageId]);
  615. $this->_decAmount = $decPackage['AMOUNT'];
  616. $this->_decPv = $decPackage['PV'];
  617. $this->_orderGoods[] = [
  618. 'GOODS_ID' => $this->packageId,
  619. 'PRICE' => $this->_decAmount,
  620. 'REAL_PRICE' => $this->_decAmount,
  621. 'PV' => $this->_decPv,
  622. 'REAL_PV' => $this->_decPv,
  623. 'BUY_NUMS' => 1,
  624. 'SKU_CODE' => $decPackage['PACKAGE_NO'],
  625. 'GOODS_TITLE' => $decPackage['PACKAGE_NAME'],
  626. 'EMAIL' => $this->email
  627. ];
  628. // 更改库存和状态
  629. $data = DeclarationPackage::find()->where(['ID' => $decPackage['ID']])->one();
  630. $data->STORE_NUMS = $data->STORE_NUMS - 1;
  631. if($data->STORE_NUMS <= 0){
  632. $data->STATUS = 0;
  633. $data->UPDATED_AT = Date::nowTime();
  634. }
  635. $data->update();
  636. }else{
  637. $ids = $this->goodsId;
  638. $totalAmount = 0;
  639. $totalAmountStandard = 0;
  640. $totalPv = 0;
  641. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0); // 汇率
  642. foreach ($this->goodsNum as $k => $v) {
  643. if ($v) {
  644. $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1',[':ID'=> $ids[$k]]);
  645. if($goods['STORE_NUMS']>0){
  646. $totalAmount += $goods['SELL_PRICE'] * intval($v);
  647. $totalAmountStandard += $goods['SELL_PRICE_STANDARD'] * intval($v);
  648. $realPriceStandard = $goods['SELL_PRICE_STANDARD'];
  649. $totalPv += $goods['PRICE_PV'] * intval($v);
  650. $this->_orderGoods[] = [
  651. 'GOODS_ID' => $goods['ID'],
  652. 'PRICE' => $goods['SELL_PRICE'],
  653. 'REAL_PRICE' => $goods['SELL_PRICE'],
  654. 'PV' => $goods['PRICE_PV'],
  655. 'REAL_PV' => $goods['PRICE_PV'],
  656. 'POINT' => $goods['POINT'],
  657. 'BUY_NUMS' => intval($v),
  658. 'SKU_CODE' => $goods['GOODS_NO'],
  659. 'GOODS_TITLE' => $goods['GOODS_NAME'],
  660. 'EMAIL' => $this->email,
  661. 'TAX_RATE' => $goods['TAX_RATE'],
  662. 'STANDARD_PRICE' => $goods['SELL_PRICE_STANDARD'],
  663. 'REAL_STANDARD_PRICE' => $realPriceStandard,
  664. 'EXCHANGE_RATE' => $exchangeRate,
  665. ];
  666. // 更改库存和状态
  667. $data = ShopGoods::find()->where(['ID' => $goods['ID']])->one();
  668. $data->STORE_NUMS = $data->STORE_NUMS - intval($v);
  669. if($data->STORE_NUMS <= 0){
  670. $data->STATUS = 0;
  671. $data->UPDATED_AT = Date::nowTime();
  672. }
  673. $data->update();
  674. }
  675. }
  676. }
  677. if($totalPv<$decLevel['PERF']){
  678. throw new Exception(Yii::t('app', 'totalBVCanNotLessThanSelectedBV'));
  679. }
  680. foreach ($decLevelConfig as $key=>$val){
  681. if($totalPv>=$val['PERF']){
  682. $toDecLevel = $key;
  683. }
  684. }
  685. if($this->decLv!=$toDecLevel){
  686. throw new Exception(Yii::t('app', 'totalBVCanNotLessThanNextSelectedLevelBV'));
  687. }
  688. $this->_decAmount = $totalAmount;
  689. $this->_decPv = $totalPv;
  690. $this->_decAmountStandard = $totalAmountStandard;
  691. $this->_standardAmount = $this->_decAmountStandard;
  692. }
  693. //看现金余额是否充足
  694. $loginUserId = \Yii::$app->user->id;
  695. if (Cash::getAvailableBalance($loginUserId) < $this->_decAmount){
  696. throw new Exception(Yii::t('app', 'applicantCashShort'));
  697. }
  698. if(!($zcResult = $this->addUser($allData))) {
  699. throw new Exception(Form::formatErrorsForApi($this->_userForm->getErrors()));
  700. }
  701. if(!($decResult = $this->addDecOrder())) {
  702. throw new Exception(Form::formatErrorsForApi($decResult->getErrors()));
  703. }
  704. }
  705. return true;
  706. }
  707. /**
  708. * 添加会员
  709. * @param $allData
  710. * @return bool|UserInfo|null
  711. * @throws \yii\db\Exception
  712. */
  713. public function addUser($allData){
  714. $period = Period::instance();
  715. // 增加会员
  716. $user = new User();
  717. $user->USER_NAME = $this->insertUserName;
  718. $user->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
  719. $user->PAY_PASSWORD = \Yii::$app->security->generatePasswordHash($this->payPassword);
  720. $user->NATION = 0;
  721. $user->REAL_NAME = $this->realName;
  722. $user->ID_CARD = $this->mobile;
  723. $user->MOBILE = $this->mobile;
  724. $user->EMAIL = $this->email;
  725. $user->ADDRESS = $this->address ? $this->address : 'nothing';//无
  726. $user->OPEN_BANK = $this->openBank;
  727. $user->BANK_ADDRESS = $this->bankAddress;
  728. $user->BANK_NO = $this->bankNo;
  729. $user->BANK_PROVINCE = $this->bankProvince ?? 0;
  730. $user->BANK_CITY = $this->bankCity ?? 0;
  731. $user->BANK_COUNTY = $this->bankCounty ?? 0;
  732. $user->CREATED_AT = Date::nowTime();
  733. $user->STATUS = 1;
  734. $user->DEC_LV = $this->decLv;
  735. $user->LAST_DEC_LV = $this->decLv;
  736. $user->EMP_LV = EmployLevel::getDefaultLevelId();
  737. $user->PROVINCE = $this->province ?? 0;
  738. $user->LGA_NAME = $this->lgaName;
  739. $user->CITY_NAME = $this->cityName;
  740. // $user->CITY = $this->city ?? 0;
  741. // $user->COUNTY = intval($this->county) ?? 0;
  742. $user->AVATAR = 'avatar/1.png';
  743. $user->IS_DEC = 0;
  744. $user->DEC_ID = $this->_decId ?? null;
  745. $user->DEC_ROLE_ID = $this->decRoleId ?? null;
  746. $user->PERIOD_AT = $period->getNowPeriodNum();
  747. $user->IS_DIRECT_SELLER = 0;
  748. $user->VERIFIED = 1;
  749. $user->VERIFIED_AT = Date::nowTime();
  750. if (!$user->save()) {
  751. throw new Exception(Form::formatErrorsForApi($user->getErrors()));
  752. }
  753. $this->_insertUserId = $user->ID;
  754. $userForm = new UserForm();
  755. $this->_userForm = $userForm;
  756. $userForm->scenario = 'addWithUserName';
  757. $userForm->userId = $this->_insertUserId;
  758. $userForm->userName = $this->insertUserName;
  759. $userForm->zcPv = $this->_decPv;
  760. $userForm->zcAmount = $this->_decAmount;
  761. $userForm->conUserName = $this->conUserName;
  762. $userForm->recUserName = $this->recUserName;
  763. $userForm->location = $this->location;
  764. $userForm->idCard = $this->mobile;
  765. $userForm->allData = $allData;
  766. if(!$userForm->validate()){
  767. $this->addErrors($userForm->getErrors());
  768. return false;
  769. }
  770. if($result = $userForm->add()){
  771. return $result;
  772. } else {
  773. return false;
  774. }
  775. }
  776. /**
  777. * 添加报单订单
  778. * @return bool|UserInfo|null
  779. * @throws \yii\db\Exception
  780. */
  781. public function addDecOrder(){
  782. $periodObj = Period::instance();
  783. $nowPeriodNum = $periodObj->getNowPeriodNum();
  784. $nowCalcMonth = $periodObj->getYearMonth($nowPeriodNum);
  785. $ord = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 10);
  786. // 加入报单信息
  787. $decOrderModel = new DecOrder();
  788. $decOrderModel->DEC_SN = 'DS'.$ord;
  789. $decOrderModel->ORDER_SN = 'OS'.$ord;
  790. $decOrderModel->TYPE = $this->type;
  791. $decOrderModel->USER_ID = \Yii::$app->user->id;
  792. $decOrderModel->TO_USER_ID = $this->_insertUserId;
  793. $decOrderModel->DEC_AMOUNT = $this->_decAmount;
  794. $decOrderModel->DEC_PV = $this->_decPv;
  795. $decOrderModel->PERIOD_NUM = $nowPeriodNum;
  796. $decOrderModel->CALC_MONTH = $nowCalcMonth;
  797. $decOrderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  798. $decOrderModel->PAID_WALLET = 'cash';
  799. $decOrderModel->CON_USER_ID = Info::getUserIdByUserName($this->conUserName);
  800. $decOrderModel->REC_USER_ID = Info::getUserIdByUserName($this->recUserName);
  801. $decOrderModel->DEC_ID = $this->_decId;
  802. $decOrderModel->IS_DEL = 0;
  803. $decOrderModel->CREATED_AT = Date::nowTime();
  804. if(!$decOrderModel->save()){
  805. throw new Exception(Form::formatErrorsForApi($decOrderModel->getErrors()));
  806. }
  807. // 加入订单信息
  808. if($this->province!=1){
  809. $warehouse = Region::getWarehouseByCode($this->province);//仓库
  810. if(!$warehouse){
  811. throw new Exception(Yii::t('app', 'deliveryTemporarilyNotSupported'));
  812. }
  813. }else{
  814. $warehouse = '01';
  815. }
  816. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0);
  817. $orderModel = new Order();
  818. $orderModel->SN = 'OS'.$ord;
  819. $orderModel->DEC_SN = 'DS'.$ord;
  820. $orderModel->ORDER_TYPE = $this->type;
  821. $orderModel->USER_ID = $this->_insertUserId;
  822. $orderModel->USER_NAME = $this->insertUserName;
  823. $orderModel->ORDER_AMOUNT = $this->_decAmount;
  824. $orderModel->PV = $this->_decPv;
  825. $orderModel->PAY_AMOUNT = $this->_decAmount;
  826. $orderModel->PAY_PV = $this->_decPv;
  827. $orderModel->PAY_AT = Date::nowTime();
  828. $orderModel->PAY_TYPE = 'cash';
  829. $orderModel->PERIOD_NUM = $nowPeriodNum;
  830. $orderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  831. $orderModel->FREIGHT = 0;
  832. $orderModel->PAY_FREIGHT = 0;
  833. $orderModel->CONSIGNEE = $this->consignee;
  834. $orderModel->MOBILE = $this->acceptMobile;
  835. $orderModel->EMAIL = $this->email;
  836. $orderModel->PROVINCE = $this->province;
  837. $orderModel->LGA_NAME = $this->lgaName;
  838. $orderModel->CITY_NAME = $this->cityName;
  839. // $orderModel->CITY = $this->city;
  840. // $orderModel->COUNTY = intval($this->county) ?? 0;
  841. $orderModel->ADDRESS = $this->address;
  842. $orderModel->WAREHOUSE = $warehouse;
  843. $orderModel->STATUS = 1;
  844. $orderModel->CREATED_AT = Date::nowTime();
  845. $orderModel->CREATE_USER = Info::getUserNameByUserId(\Yii::$app->user->id);
  846. $orderModel->ORDER_AMOUNT_STANDARD = $this->_decAmountStandard;
  847. $orderModel->PAY_AMOUNT_STANDARD = $this->_standardAmount;
  848. $orderModel->EXCHANGE_RATE = $exchangeRate;
  849. if($this->province==1){
  850. $orderModel->EXPRESS_TYPE = 1;
  851. }
  852. if(!$orderModel->save()){
  853. throw new Exception(Form::formatErrorsForApi($orderModel->getErrors()));
  854. }
  855. // 加入商品到订单商品表
  856. foreach($this->_orderGoods as $key=>$value){
  857. $this->_orderGoods[$key]['ORDER_SN'] = $orderModel->SN;
  858. $this->_orderGoods[$key]['P_CALC_MONTH'] = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  859. }
  860. OrderGoods::batchInsert($this->_orderGoods);
  861. //写入收货地址信息,判断province字段,当不为1时,插入收货地址信息
  862. if($this->province!=1){
  863. $addressModel = new ReceiveAddress();
  864. $addressModel->USER_ID = $this->_insertUserId;
  865. $addressModel->USER_NAME = $this->insertUserName;
  866. $addressModel->CONSIGNEE = $this->consignee;
  867. $addressModel->MOBILE = $this->acceptMobile;
  868. $addressModel->PROVINCE = $this->province;
  869. // $addressModel->CITY = $this->city;
  870. // $addressModel->COUNTY = intval($this->county) ?? 0;
  871. $addressModel->CITY_NAME = $this->cityName;
  872. $addressModel->LGA_NAME = $this->lgaName;
  873. $addressModel->ADDRESS = $this->address;
  874. $addressModel->IS_DEFAULT = 1;
  875. if(!$addressModel->save()){
  876. throw new Exception(Form::formatErrorsForApi($addressModel->getErrors()));
  877. }
  878. //扣报单人现金钱包
  879. // Cash::changeUserCash(\Yii::$app->user->id, 'CASH', -abs($this->_decAmount), ['REMARK' =>'为'.$this->insertUserName.'报单']);
  880. Cash::changeUserCash(\Yii::$app->user->id, 'CASH', -abs($this->_decAmount), ['REMARK' =>'To'.$this->insertUserName.'Entry']);
  881. return $addressModel;
  882. }else{
  883. $addressModel = new ReceiveAddress();
  884. Cash::changeUserCash(\Yii::$app->user->id, 'CASH', -abs($this->_decAmount), ['REMARK' =>'To'.$this->insertUserName.'Entry']);
  885. return $addressModel;
  886. }
  887. }
  888. /**
  889. * 删单
  890. * @return bool
  891. * @throws \yii\db\Exception
  892. */
  893. public function delete(){
  894. if(!$this->validate()){
  895. return false;
  896. }
  897. $transaction = \Yii::$app->db->beginTransaction();
  898. try {
  899. $oneOrder = $this->_oneOrder;
  900. // 首购单要删除会员
  901. if($this->type == self::TYPE_ZC){
  902. UserInfo::deleteUser($oneOrder['TO_USER_ID']);
  903. }
  904. // 如果是复销单的话,还需要考虑给会员的复销池减去金额
  905. elseif($this->type == self::TYPE_FX){
  906. // Reconsume::changePoolPV($oneOrder['TO_USER_ID'], -abs($oneOrder['DEC_PV']), ['REMARK'=>'删单扣除', 'DEAL_TYPE'=>Reconsume::TYPE_AUDIT_PV]);
  907. Reconsume::changePoolPV($oneOrder['TO_USER_ID'], -abs($oneOrder['DEC_PV']), ['REMARK'=>'Deletion deduction', 'DEAL_TYPE'=>Reconsume::TYPE_AUDIT_PV]);
  908. }
  909. $transaction->commit();
  910. } catch (Exception $e) {
  911. $transaction->rollBack();
  912. $this->addError('delete', $e->getMessage());
  913. return false;
  914. }
  915. return true;
  916. }
  917. }