DeclarationForm.php 46 KB

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