BaApproachDeclarationForm.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Cache;
  5. use common\helpers\Date;
  6. use common\helpers\Form;
  7. use common\helpers\LoggerTool;
  8. use common\helpers\user\Cash;
  9. use common\helpers\user\Reconsume;
  10. use common\helpers\user\Info;
  11. use common\models\ApproachDecOrder;
  12. use common\models\ApproachOrder;
  13. use common\models\ApproachOrderGoods;
  14. use common\models\BaApproachDecOrder;
  15. use common\models\BaApproachOrder;
  16. use common\models\BaApproachOrderGoods;
  17. use common\models\BaReceiveAddress;
  18. use common\models\BaUser;
  19. use common\models\DeclarationPackage;
  20. use common\models\DecOrder;
  21. use common\models\EmployLevel;
  22. use common\models\Order;
  23. use common\models\OrderGoods;
  24. use common\models\Period;
  25. use common\models\ReceiveAddress;
  26. use common\models\ReconsumePool;
  27. use common\models\ReconsumePoolFlow;
  28. use common\models\DeclarationLevel;
  29. use common\models\Region;
  30. use common\models\ShopGoods;
  31. use common\models\User;
  32. use common\models\UserInfo;
  33. use common\models\UserNetwork;
  34. use common\models\UserRelation;
  35. use yii\base\Exception;
  36. /**
  37. * Login form
  38. */
  39. class BaApproachDeclarationForm extends Model
  40. {
  41. public $type;
  42. public $decLv;
  43. public $decWay;
  44. public $packageId;
  45. public $goodsId;
  46. public $goodsNum;
  47. public $insertUserName;
  48. public $password;
  49. public $payPassword;
  50. public $realName;
  51. public $insertUserIdCard;
  52. public $mobile;
  53. public $email;
  54. public $address;
  55. public $openBank;
  56. public $bankAddress;
  57. public $bankNo;
  58. public $bankProvince;
  59. public $bankCity;
  60. public $bankCounty;
  61. public $conUserName;
  62. public $recUserName;
  63. public $decUserName;
  64. public $location;
  65. public $consignee;
  66. public $acceptMobile;
  67. public $province;
  68. public $city;
  69. public $county;
  70. public $cityName;
  71. public $lgaName;
  72. public $payType;
  73. // 传过来的全部数据
  74. public $allData;
  75. private $_decId;
  76. public $_insertUserId;
  77. private $_decAmount;
  78. private $_decPv;
  79. private $_orderGoods;
  80. private $_standardAmount;
  81. private $_decAmountStandard;
  82. // 批量报单时添加会员的REDIS里面缓存的添加的会员资料
  83. const REDIS_WAIT_ADD_USER = 'user:dec:waitAdd';
  84. const TYPE_ZC = 'ZC';
  85. // const TYPE_YH = 'YH';
  86. const TYPE_ZG = 'ZG';
  87. const TYPE_LS = 'LS';
  88. const TYPE_FX = 'FX';
  89. // private $_modelClass = null;
  90. private $_oneOrder = null;
  91. private $_userForm = null;
  92. // 全部的上级(安置网和开拓网)
  93. private $_tempParentUser = [];
  94. // 全部的安置网上级
  95. private $_tempNetworkParentUser = [];
  96. // 全部的开拓网上级
  97. private $_tempRelationParentUser = [];
  98. private $_types = [
  99. self::TYPE_ZC => [
  100. 'name' => '首购单',
  101. ],
  102. // self::TYPE_YH => [
  103. // 'class' => DeclarationYH::class,
  104. // 'table' => '{{%DECLARATION_YH}}',
  105. // 'name' => '优惠单',
  106. // ],
  107. self::TYPE_ZG => [
  108. 'name' => '升级增购单',
  109. ],
  110. self::TYPE_LS => [
  111. 'name' => '零售单',
  112. ],
  113. self::TYPE_FX => [
  114. 'name' => '复销单',
  115. ],
  116. ];
  117. /**
  118. * @inheritdoc
  119. */
  120. public function rules()
  121. {
  122. return [
  123. [['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'],
  124. [['type','decLv','decWay','insertUserName',/* 'insertUserIdCard',*/'password','payPassword','decUserName'], 'required'],
  125. ];
  126. }
  127. public function attributeLabels()
  128. {
  129. return [
  130. 'type' => 'Entry type',//报单类型
  131. 'decLv' => 'Entry level',//报单级别
  132. //'decPv' => '报单PV',
  133. 'decWay' => 'Entry method',//报单方式
  134. 'packageId' => 'Entry Package',//报单套餐
  135. 'goodsId' => 'product ID',//商品ID
  136. 'goodsNum' => 'Product quantity',//商品数量
  137. //'addType' => '新增会员方式',
  138. 'insertUserName' => 'Member user name',//会员用户名
  139. 'password' => 'Login password',//登陆密码
  140. 'payPassword' => 'Payment password',//支付密码
  141. 'realName' => 'Member Name',//会员姓名
  142. 'insertUserIdCard' => 'ID',//身份证号
  143. 'mobile' => 'Phone Number',//手机号
  144. 'openBank' => 'openBank',//开户行
  145. 'bankAddress' => 'bankAddress',//开户支行
  146. 'bankNo' => 'bankNo',//银行账号
  147. 'bankProvince' => 'bankProvince',//银行省份
  148. 'bankCity' => 'bankCity',//银行城市
  149. 'bankCounty' => 'bankCounty',//银行县区
  150. 'conUserName' => 'Instructor user name',//指导老师用户名
  151. 'recUserName' => 'Developer user name',//开拓人用户名
  152. 'decUserName' => 'Stockist user name',//报单中心用户名
  153. 'conUid' => 'Instructor member ID',//指导老师会员ID
  154. 'recUid' => 'Sponsor member ID',//'开拓人会员ID
  155. 'location' => 'market',//市场
  156. 'consignee' => 'consignee',//收货人
  157. 'acceptMobile' => 'Recipient Phone Number',//收货人手机
  158. 'province' => 'Receiving Province',//收货省
  159. 'city' => 'Receiving City',//收货市
  160. 'county' => 'Receiving area / county',//收货区县
  161. 'address' => 'Receiving detailed address',//收货详细地址
  162. ];
  163. }
  164. /**
  165. * 指定场景
  166. * @return array
  167. */
  168. public function scenarios()
  169. {
  170. $parentScenarios = parent::scenarios();
  171. $customScenarios = [
  172. 'userDec' => ['type','allData', /*'decLv','decWay',*/'insertUserName','password','payPassword', 'realName','mobile', 'address','consignee','acceptMobile','province','conUserName', /*'recUserName','decUserName',*/ 'location', 'email'],
  173. 'canDec' => ['type', 'insertUserName',/* 'insertUserIdCard',*/ 'conUserName', 'recUserName', 'location'],
  174. 'notFull' => ['type', 'insertUserName', 'conUserName', 'recUserName', 'location'],
  175. ];
  176. return array_merge($parentScenarios, $customScenarios);
  177. }
  178. /**
  179. * 判断报单中心是否存在
  180. * @param $attribute
  181. * @return false|void
  182. */
  183. public function issetDec($attribute){
  184. if (!User::find()->select('ID')->where('IS_DEC=1 AND USER_NAME=:USER_NAME', [':USER_NAME' => $this->decUserName])->asArray()->one()) {
  185. $this->addError($attribute, 'Stockist does not exist');//报单中心不存在
  186. return false;
  187. }
  188. return true;
  189. }
  190. /**
  191. * 判断开拓人
  192. * @param $attribute
  193. */
  194. public function isRecUserName($attribute){
  195. if($this->type == self::TYPE_ZC){
  196. // 开拓人
  197. if(!isset($this->_tempParentUser[$this->recUserName])){
  198. $recUserTemp = UserInfo::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => $this->recUserName]);
  199. if(!$recUserTemp){
  200. if($this->insertUserName){
  201. //$this->addError($attribute, '为'.$this->insertUserName.'报单,开拓人'.$this->recUserName.'不存在');
  202. $this->addError($attribute, 'To'.$this->insertUserName.'Entry, Sponsor'.$this->recUserName.'not in exist');
  203. } else {
  204. // $this->addError($attribute, '开拓人'.$this->recUserName.'不存在');
  205. $this->addError($attribute, 'Sponsor'.$this->recUserName.'not in exist');
  206. }
  207. return ;
  208. }
  209. $recUserTemp['CON_NUM'] = UserNetwork::firstFloorChildNum($recUserTemp['USER_ID']);
  210. $recUserTemp['REC_NUM'] = UserRelation::firstFloorChildNum($recUserTemp['USER_ID']);
  211. $this->_tempParentUser[$this->recUserName] = [
  212. 'USER_NAME' => $this->recUserName,
  213. 'ZC_PV' => $recUserTemp['ZC_PV'],
  214. 'CON_USER_NAME' => Info::getUserNameByUserId($recUserTemp['CON_UID']),
  215. 'REC_USER_NAME' => Info::getUserNameByUserId($recUserTemp['REC_UID']),
  216. 'CON_NUM' => $recUserTemp['CON_NUM'],
  217. 'REC_NUM' => $recUserTemp['REC_NUM'],
  218. 'SYSTEM_ID' => $recUserTemp['SYSTEM_ID'],
  219. 'LOCATION' => UserNetwork::getLocation($recUserTemp['USER_ID'], $recUserTemp['CON_UID']),
  220. ];
  221. // 把该会员下面的5个区是否存在会员都付上
  222. for($i=1;$i<=5;$i++){
  223. $this->_tempParentUser[$this->recUserName]["LOCATION$i"] = UserNetwork::issetUserInLocation($recUserTemp['USER_ID'], $i);
  224. }
  225. }
  226. $recUser = $this->_tempParentUser[$this->recUserName];
  227. $this->_tempParentUser[$this->recUserName]['REC_NUM'] += 1;
  228. // 存在新加入会员时查看开拓人是否在新加入会员的安置网上级中
  229. if($this->insertUserName) {
  230. $this->loopFindParentToNetwork($this->insertUserName);
  231. if (!$this->recUserIsInNetworkParent()) {
  232. // $this->addError($attribute, '为' . $this->insertUserName . '报单,开拓人' . $this->recUserName . '不在' . $this->insertUserName . '的安置网上级中');
  233. $this->addError($attribute, 'To' . $this->insertUserName . 'Entry, Sponsor' . $this->recUserName . 'not in exist' . $this->insertUserName . 'in the placement superiors');
  234. return ;
  235. }
  236. }
  237. // 把自己加入到临时上级会员数组中
  238. $this->_tempParentUser[$this->insertUserName] = [
  239. 'USER_NAME' => $this->insertUserName,
  240. //'ZC_PV' => $this->decPv,
  241. 'CON_USER_NAME' => $this->conUserName,
  242. 'REC_USER_NAME' => $this->recUserName,
  243. 'CON_NUM' => 0,
  244. 'REC_NUM' => 0,
  245. 'SYSTEM_ID' => $recUser['SYSTEM_ID'],
  246. 'LOCATION' => $this->location,
  247. 'LOCATION1' => 0,
  248. 'LOCATION2' => 0,
  249. 'LOCATION3' => 0,
  250. 'LOCATION4' => 0,
  251. 'LOCATION5' => 0,
  252. ];
  253. }
  254. }
  255. /**
  256. * 报单类型
  257. * @param $attribute
  258. * @param $params
  259. */
  260. public function isType($attribute, $params){
  261. if(array_key_exists($this->type, $this->_types)){
  262. if($this->type == self::TYPE_ZC){
  263. if($this->scenario == 'userDec'){
  264. if(!$this->insertUserName) $this->addError($attribute, 'Membership number must be filled in for initial purchase');//首购必须填写加入会员编号
  265. }
  266. if(!$this->insertUserName) $this->addError($attribute, 'Membership number must be filled in for initial purchase');//首购必须填写加入会员编号
  267. if(!$this->conUserName) $this->addError($attribute, 'For the first purchase, you must fill in the instructor number of the member');//首购必须填写加入会员的指导老师编号
  268. if(!$this->recUserName) $this->addError($attribute, 'For the first purchase, the Sponsor number of the member must be filled in');//首购必须填写加入会员的开拓人编号
  269. if(!$this->location) $this->addError($attribute, 'The first purchase must be filled in the market of the member');//首购必须填写加入会员的市场
  270. }
  271. } else {
  272. $this->addError($attribute, 'Incorrect entry type');//报单类型不正确
  273. }
  274. }
  275. /**
  276. * 添加报单
  277. * @param $allData
  278. * @return array|bool|BaApproachOrder
  279. * @throws Exception
  280. */
  281. public function add($allData){
  282. if(!$this->validate()){
  283. return $this->getErrors();
  284. }
  285. // 首购单,需要添加会员操作
  286. if($this->type == self::TYPE_ZC) {
  287. try {
  288. if (preg_match("/[\x7f-\xff]/", $this->insertUserName)) { //判断字符串中是否有中文
  289. throw new Exception('Member number cannot contain Chinese characters');//会员编号中不能含有汉字
  290. }
  291. if ($this->decWay == 1) {
  292. $decPackage = DeclarationPackage::findOneAsArray('ID=:ID', [':ID' => $this->packageId]);
  293. $this->_decAmount = $decPackage['AMOUNT'];
  294. $this->_decPv = $decPackage['PV'];
  295. $this->_orderGoods[] = [
  296. 'GOODS_ID' => $this->packageId,
  297. 'PRICE' => $this->_decAmount,
  298. 'REAL_PRICE' => $this->_decAmount,
  299. 'PV' => $this->_decPv,
  300. 'REAL_PV' => $this->_decPv,
  301. 'BUY_NUMS' => 1,
  302. 'SKU_CODE' => $decPackage['PACKAGE_NO'],
  303. 'GOODS_TITLE' => $decPackage['PACKAGE_NAME'],
  304. 'EMAIL' => $this->email
  305. ];
  306. } else {
  307. $ids = $this->goodsId;
  308. $totalAmount = 0;
  309. $totalAmountStandard = 0;
  310. $totalPv = 0;
  311. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0); // 汇率
  312. foreach ($this->goodsNum as $k => $v) {
  313. if ($v) {
  314. $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1', [':ID' => $ids[$k]]);
  315. if ($goods['STORE_NUMS'] > 0) {
  316. $totalAmount += $goods['SELL_PRICE'] * intval($v);
  317. $totalAmountStandard += $goods['SELL_PRICE_STANDARD'] * intval($v);
  318. $realPriceStandard = $goods['SELL_PRICE_STANDARD'];
  319. $totalPv += $goods['PRICE_PV'] * intval($v);
  320. $this->_orderGoods[] = [
  321. 'GOODS_ID' => $goods['ID'],
  322. 'PRICE' => $goods['SELL_PRICE'],
  323. 'REAL_PRICE' => $goods['SELL_PRICE'],
  324. 'PV' => $goods['PRICE_PV'],
  325. 'REAL_PV' => $goods['PRICE_PV'],
  326. 'POINT' => $goods['POINT'],
  327. 'BUY_NUMS' => intval($v),
  328. 'SKU_CODE' => $goods['GOODS_NO'],
  329. 'GOODS_TITLE' => $goods['GOODS_NAME'],
  330. 'EMAIL' => $this->email,
  331. 'STANDARD_PRICE' => $goods['SELL_PRICE_STANDARD'],
  332. 'REAL_STANDARD_PRICE' => $realPriceStandard,
  333. 'EXCHANGE_RATE' => $exchangeRate,
  334. 'TAX_RATE' => $goods['TAX_RATE'],
  335. ];
  336. }
  337. }
  338. }
  339. $this->_decAmount = $totalAmount;
  340. $this->_decPv = $totalPv;
  341. $this->_decAmountStandard = $totalAmountStandard;
  342. $this->_standardAmount = $this->_decAmountStandard;
  343. }
  344. if (!($this->addUser($allData))) {
  345. throw new Exception(Form::formatErrorsForApi($this->_userForm->getErrors()));
  346. }
  347. $decResult = $this->addDecOrder();
  348. if (!$decResult) {
  349. throw new Exception(Form::formatErrorsForApi($decResult->getErrors()));
  350. }
  351. return $decResult;
  352. } catch (\Exception $e) {
  353. throw new Exception(Form::formatErrorsForApi($e->getFile() . ' ' . $e->getLine() . ' ' . $e->getMessage()));
  354. }
  355. }
  356. return true;
  357. }
  358. /**
  359. * 添加会员
  360. * @param $allData
  361. * @return bool|UserInfo|null
  362. * @throws \yii\db\Exception|Exception
  363. */
  364. public function addUser($allData){
  365. $period = Period::instance();
  366. // 增加会员
  367. $user = new BaUser();
  368. $user->USER_NAME = $this->insertUserName;
  369. $user->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
  370. $user->PAY_PASSWORD = \Yii::$app->security->generatePasswordHash($this->payPassword);
  371. $user->NATION = 0;
  372. $user->REAL_NAME = $this->realName;
  373. $user->ID_CARD = $this->mobile;
  374. $user->MOBILE = $this->mobile;
  375. $user->EMAIL = $this->email;
  376. $user->ADDRESS = $this->address ? $this->address : 'nothing';//无
  377. $user->OPEN_BANK = $this->openBank;
  378. $user->BANK_ADDRESS = $this->bankAddress;
  379. $user->BANK_NO = $this->bankNo;
  380. $user->BANK_PROVINCE = $this->bankProvince ?? 0;
  381. $user->BANK_CITY = $this->bankCity ?? 0;
  382. $user->BANK_COUNTY = $this->bankCounty ?? 0;
  383. $user->CREATED_AT = Date::nowTime();
  384. $user->STATUS = 0;
  385. $user->DEC_LV = $this->decLv;
  386. $user->LAST_DEC_LV = $this->decLv;
  387. $user->EMP_LV = EmployLevel::getDefaultLevelId();
  388. $user->PROVINCE = $this->province ?? 0;
  389. $user->LGA_NAME = $this->lgaName;
  390. $user->CITY_NAME = $this->cityName;
  391. $user->AVATAR = 'avatar/1.png';
  392. $user->IS_DEC = 0;
  393. $user->DEC_ID = $this->_decId ?? '';
  394. $user->DEC_ROLE_ID = $this->decRoleId ?? '';
  395. $user->PERIOD_AT = $period->getNowPeriodNum();
  396. $user->IS_DIRECT_SELLER = 0;
  397. $user->VERIFIED = 1;
  398. $user->VERIFIED_AT = Date::nowTime();
  399. if (!$user->save()) {
  400. throw new Exception(Form::formatErrorsForApi($user->getErrors()));
  401. }
  402. $this->_insertUserId = $user->ID;
  403. $userForm = new BaUserForm();
  404. $this->_userForm = $userForm;
  405. $userForm->scenario = 'addWithUserName';
  406. $userForm->userId = $this->_insertUserId;
  407. $userForm->userName = $this->insertUserName;
  408. $userForm->zcPv = $this->_decPv;
  409. $userForm->zcAmount = $this->_decAmount;
  410. $userForm->conUserName = $this->conUserName ?? '';
  411. $userForm->recUserName = $this->recUserName;
  412. $userForm->location = $this->location;
  413. $userForm->idCard = $this->mobile;
  414. $userForm->allData = $allData;
  415. if(!$userForm->validate()){
  416. $this->addErrors($userForm->getErrors());
  417. return false;
  418. }
  419. if($result = $userForm->add()){
  420. return $result;
  421. } else {
  422. return false;
  423. }
  424. }
  425. /**
  426. * 添加报单订单
  427. * @throws \yii\db\Exception
  428. * @throws Exception
  429. */
  430. public function addDecOrder(){
  431. $periodObj = Period::instance();
  432. $nowPeriodNum = $periodObj->getNowPeriodNum();
  433. $nowCalcMonth = $periodObj->getYearMonth($nowPeriodNum);
  434. $ord = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 10);
  435. // 加入报单信息
  436. $decOrderModel = new BaApproachDecOrder();
  437. $decOrderModel->DEC_SN = 'DS' . $ord;
  438. $decOrderModel->ORDER_SN = 'OS' . $ord;
  439. $decOrderModel->TYPE = $this->type;
  440. $decOrderModel->USER_ID = \Yii::$app->user->id;
  441. $decOrderModel->TO_USER_ID = $this->_insertUserId;
  442. $decOrderModel->DEC_AMOUNT = $this->_decAmount;
  443. $decOrderModel->DEC_PV = $this->_decPv;
  444. $decOrderModel->PERIOD_NUM = $nowPeriodNum;
  445. $decOrderModel->CALC_MONTH = $nowCalcMonth;
  446. $decOrderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  447. $decOrderModel->PAID_WALLET = $this->payType;
  448. $decOrderModel->CON_USER_ID = Info::getUserIdByUserName($this->conUserName) ?? '';
  449. $decOrderModel->REC_USER_ID = Info::getUserIdByUserName($this->recUserName) ?? '';
  450. $decOrderModel->DEC_ID = $this->_decId ?? '';
  451. $decOrderModel->IS_DEL = 0;
  452. $decOrderModel->CREATED_AT = Date::nowTime();
  453. if(!$decOrderModel->save()){
  454. throw new Exception(Form::formatErrorsForApi($decOrderModel->getErrors()));
  455. }
  456. // 加入订单信息
  457. if ($this->province!=1) {
  458. // 仓库
  459. $warehouse = Region::getWarehouseByCode($this->province);
  460. if (!$warehouse) {
  461. // 地区暂时不支持配送,具体联系客服
  462. throw new Exception('Delivery is temporarily not supported in the region. Contact customer service for details');
  463. }
  464. } else {
  465. $warehouse = '01';
  466. }
  467. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0);
  468. $orderModel = new BaApproachOrder();
  469. $orderModel->SN = 'OS' . $ord;
  470. $orderModel->DEC_SN = 'DS' . $ord;
  471. $orderModel->ORDER_TYPE = $this->type;
  472. $orderModel->USER_ID = $this->_insertUserId;
  473. $orderModel->USER_NAME = $this->insertUserName;
  474. $orderModel->ORDER_AMOUNT = $this->_decAmount;
  475. $orderModel->PV = $this->_decPv;
  476. $orderModel->PAY_AMOUNT = $this->_decAmount;
  477. $orderModel->PAY_PV = $this->_decPv;
  478. $orderModel->PAY_AT = 0;
  479. $orderModel->PAY_TYPE = $this->payType;
  480. $orderModel->PERIOD_NUM = $nowPeriodNum;
  481. $orderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  482. $orderModel->FREIGHT = 0;
  483. $orderModel->PAY_FREIGHT = 0;
  484. $orderModel->CONSIGNEE = $this->consignee;
  485. $orderModel->MOBILE = $this->acceptMobile;
  486. $orderModel->EMAIL = $this->email ?: $this->insertUserName . '@elken.net';
  487. $orderModel->PROVINCE = $this->province;
  488. $orderModel->LGA_NAME = $this->lgaName;
  489. $orderModel->CITY_NAME = $this->cityName;
  490. $orderModel->ADDRESS = $this->address;
  491. $orderModel->WAREHOUSE = $warehouse;
  492. $orderModel->STATUS = \Yii::$app->params['orderStatus']['notPaid']['value'];
  493. $orderModel->CREATED_AT = Date::nowTime();
  494. $orderModel->CREATE_USER = Info::getUserNameByUserId(\Yii::$app->user->id);
  495. $orderModel->ORDER_AMOUNT_STANDARD = $this->_decAmountStandard;
  496. $orderModel->PAY_AMOUNT_STANDARD = $this->_standardAmount;
  497. $orderModel->EXCHANGE_RATE = $exchangeRate;
  498. if ($this->province==1) {
  499. $orderModel->EXPRESS_TYPE = 1;
  500. }
  501. if (!$orderModel->save()) {
  502. throw new Exception(Form::formatErrorsForApi($orderModel->getErrors()));
  503. }
  504. // 加入商品到订单商品表
  505. foreach($this->_orderGoods as $key=>$value){
  506. $this->_orderGoods[$key]['ORDER_SN'] = $orderModel->SN;
  507. $this->_orderGoods[$key]['P_CALC_MONTH'] = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  508. }
  509. BaApproachOrderGoods::batchInsert($this->_orderGoods);
  510. // 写入收货地址信息,判断province字段,当不为1时,插入收货地址信息
  511. $addressModel = new BaReceiveAddress();
  512. if ($this->province != 1) {
  513. $addressModel->USER_ID = $this->_insertUserId;
  514. $addressModel->USER_NAME = $this->insertUserName;
  515. $addressModel->CONSIGNEE = $this->consignee;
  516. $addressModel->MOBILE = $this->acceptMobile;
  517. $addressModel->PROVINCE = $this->province;
  518. $addressModel->CITY_NAME = $this->cityName;
  519. $addressModel->LGA_NAME = $this->lgaName;
  520. $addressModel->ADDRESS = $this->address;
  521. $addressModel->IS_DEFAULT = 1;
  522. if (!$addressModel->save()) {
  523. throw new Exception(Form::formatErrorsForApi($addressModel->getErrors()));
  524. }
  525. }
  526. return $orderModel;
  527. }
  528. }