DeclarationForm.php 46 KB

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