DeclarationForm.php 46 KB

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