OrderForm.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Cache;
  4. use common\helpers\Date;
  5. use common\components\Model;
  6. use common\helpers\Form;
  7. use common\helpers\user\Balance;
  8. use common\helpers\user\Cash;
  9. use common\helpers\user\Info;
  10. use common\libs\logging\operate\AdminOperate;
  11. use common\models\DealType;
  12. use common\models\Order;
  13. use common\models\OrderGoods;
  14. use common\models\Period;
  15. use common\models\ReceiveAddress;
  16. use common\models\Region;
  17. use common\models\ShopGoods;
  18. use common\models\User;
  19. use common\models\UserNetwork;
  20. use yii\base\Exception;
  21. /**
  22. * Login form
  23. */
  24. class OrderForm extends Model
  25. {
  26. public $sn;
  27. public $expressCompany;
  28. public $orderTrackNo;
  29. public $status;
  30. public $remark;
  31. public $type;
  32. public $addressId;
  33. public $payType;
  34. public $goodsId;
  35. public $goodsNum;
  36. public $payPassword;
  37. public $userName;
  38. public $consignee;
  39. public $acceptMobile;
  40. public $province;
  41. public $city;
  42. public $county;
  43. public $detailaddress;
  44. private $_address;
  45. private $_decAmount;
  46. private $_decPv;
  47. private $_freight;
  48. private $_payAmount;
  49. private $_orderGoods;
  50. /**
  51. * @var Order
  52. */
  53. private $_model;
  54. public function init() {
  55. parent::init();
  56. $this->adminOperateLogger = new AdminOperate([
  57. 'fetchClass' => Order::class,
  58. ]);
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public function rules()
  64. {
  65. return [
  66. [['sn', 'expressCompany', 'orderTrackNo', 'status', 'remark','type','addressId','payType','goodsId','goodsNum', 'payPassword','userName','consignee','acceptMobile','province','city','county','detailaddress'], 'trim'],
  67. [['sn', 'expressCompany', 'orderTrackNo', 'status', 'remark','type','addressId','payType','goodsId','goodsNum', 'payPassword','userName','consignee','acceptMobile','province','city','county','detailaddress'], 'required'],
  68. [['status'], 'isStatus'],
  69. [['addressId'], 'isAddress'],
  70. [['payType'], 'isPayType'],
  71. [['payPassword'], 'validatePassword'],
  72. ];
  73. }
  74. public function attributeLabels()
  75. {
  76. return [
  77. 'sn' => '订单号',
  78. 'expressCompany' => '快递公司',
  79. 'orderTrackNo' => '快递单号',
  80. 'status' => '状态',
  81. 'remark' => '备注',
  82. 'type' => '订单类型',
  83. 'addressId' => '收货地址',
  84. 'payType' => '支付方式',
  85. 'goodsId' => '商品ID',
  86. 'goodsNum' => '商品数量',
  87. 'userName' => '复消会员编号',
  88. 'consignee' => '收货人',
  89. 'acceptMobile' => '收货电话',
  90. 'province' => '省',
  91. 'city' => '市',
  92. 'county' => '区',
  93. 'detailaddress' => '收货详细地址',
  94. ];
  95. }
  96. /**
  97. * 指定校验场景
  98. * @return array
  99. */
  100. public function scenarios()
  101. {
  102. $parentScenarios = parent::scenarios();
  103. $customScenarios = [
  104. // 管理员发货
  105. 'adminDelivery' => ['sn', 'expressCompany', 'orderTrackNo'],
  106. // 会员确认收货
  107. 'userConfirm' => ['sn', 'expressCompany', 'orderTrackNo'],
  108. // 管理员修改订单状态
  109. 'adminStatus' => ['sn', 'status'],
  110. // 管理员修改备注
  111. 'adminRemark' => ['sn', 'remark'],
  112. // 会员下单
  113. 'userOrder' => ['type','addressId', 'payType','goodsId','goodsNum', 'remark', 'payPassword'],
  114. // 帮会员复消下单
  115. 'reconsumeOrder' => ['type','userName', 'payType','goodsId','goodsNum', 'remark', 'payPassword','consignee','acceptMobile','province','city','county','detailaddress'],
  116. ];
  117. return array_merge($parentScenarios, $customScenarios);
  118. }
  119. /**
  120. * 校验之前
  121. * @return bool
  122. */
  123. public function beforeValidate()
  124. {
  125. $parentValidate = parent::beforeValidate();
  126. if ($this->sn) {
  127. $this->_model = Order::findOne(['SN'=>$this->sn]);
  128. if (!$this->_model){
  129. $this->addError('sn', '订单不存在');
  130. return false;
  131. }
  132. }
  133. if ($this->scenario == 'adminDelivery'){
  134. if($this->_model['STATUS'] == \Yii::$app->params['orderStatus']['cancel']) {
  135. $this->addError('sn', '订单已取消不能发货');
  136. return false;
  137. }
  138. if($this->_model['STATUS'] == \Yii::$app->params['orderStatus']['del']) {
  139. $this->addError('sn', '订单已删除不能发货');
  140. return false;
  141. }
  142. }
  143. return $parentValidate;
  144. }
  145. /**
  146. * 校验支付密码
  147. * @param $attribute
  148. * @param $params
  149. */
  150. public function validatePassword($attribute, $params) {
  151. if (!User::validatePayPassword(\Yii::$app->user->id, $this->payPassword)) {
  152. $this->addError($attribute, '支付密码不正确');
  153. }
  154. }
  155. /**
  156. * 判断收货地址是否存在
  157. * @param $attribute
  158. */
  159. public function isAddress($attribute){
  160. if (!$receiveAddress = ReceiveAddress::find()->where(' ID=:ID', [':ID' => $this->addressId])->asArray()->one()) {
  161. $this->addError($attribute, '收货地址不存在');
  162. } else {
  163. $this->_address = $receiveAddress;
  164. }
  165. }
  166. /**
  167. * 判断支付方式
  168. * @param $attribute
  169. */
  170. public function isPayType($attribute){
  171. if(!array_key_exists($this->payType, ShopGoods::payTypes())){
  172. $this->addError($attribute, '支付方式错误');
  173. return;
  174. }
  175. }
  176. /**
  177. * 校验类型
  178. * @param $attribute
  179. */
  180. public function isStatus($attribute){
  181. if(!in_array($this->type, \Yii::$app->params['orderStatus'])){
  182. $this->addError($attribute, '类型错误');
  183. return ;
  184. }
  185. if ($this->scenario == 'adminStatus'){
  186. if ($this->status == $this->_model['STATUS']) {
  187. $this->addError($attribute, '订单状态没有改变');
  188. return ;
  189. }
  190. if($this->status == \Yii::$app->params['orderStatus']['notPaid'] && $this->_model['STATUS'] >= \Yii::$app->params['orderStatus']['delivery']) {
  191. $this->addError($attribute, '订单已经进入物流状态不能改为未支付');
  192. return ;
  193. }
  194. elseif($this->status == \Yii::$app->params['orderStatus']['paid'] && $this->_model['STATUS'] >= \Yii::$app->params['orderStatus']['cancel']) {
  195. $this->addError($attribute, '订单已失效不能处理');
  196. return ;
  197. }
  198. elseif($this->status == \Yii::$app->params['orderStatus']['delivery']) {
  199. $this->addError($attribute, '订单不能单独处理为物流状态');
  200. return ;
  201. }
  202. elseif($this->status == \Yii::$app->params['orderStatus']['complete'] && $this->_model['STATUS'] > \Yii::$app->params['orderStatus']['cancel']) {
  203. $this->addError($attribute, '订单已失效不能处理');
  204. return ;
  205. }
  206. elseif($this->status == \Yii::$app->params['orderStatus']['cancel']) {
  207. if($this->_model['STATUS'] == \Yii::$app->params['orderStatus']['complete']) {
  208. $this->addError($attribute, '订单已完成不能取消');
  209. return ;
  210. }
  211. if($this->_model['STATUS'] == \Yii::$app->params['orderStatus']['del']) {
  212. $this->addError($attribute, '订单已删除不能取消');
  213. return ;
  214. }
  215. }
  216. elseif($this->status == \Yii::$app->params['orderStatus']['del']) {
  217. if($this->_model['STATUS'] == \Yii::$app->params['orderStatus']['complete']) {
  218. $this->addError($attribute, '订单已完成不能删除');
  219. return ;
  220. }
  221. }
  222. }
  223. }
  224. /**
  225. * 管理员发货
  226. * @return Order|null
  227. * @throws \yii\db\Exception
  228. */
  229. public function adminDelivery(){
  230. if(!$this->validate()){
  231. return null;
  232. }
  233. $db = \Yii::$app->db;
  234. $transaction = $db->beginTransaction();
  235. try {
  236. $period = Period::instance();
  237. $this->_model->DELIVERY_STATUS = \Yii::$app->params['deliveryStatus']['delivered']['value'];
  238. $this->_model->DELIVERY_PERIOD = $period->getNowPeriodNum();
  239. $this->_model->DELIVERY_AT = Date::nowTime();
  240. $this->_model->EXPRESS_COMPANY = $this->expressCompany;
  241. $this->_model->ORDER_TRACK_NO = $this->orderTrackNo;
  242. $this->_model->STATUS = \Yii::$app->params['orderStatus']['delivery']['value'];
  243. if(!$this->_model->save()){
  244. throw new Exception(Form::formatErrorsForApi($this->_model->getErrors()));
  245. }
  246. $transaction->commit();
  247. } catch (Exception $e) {
  248. $transaction->rollBack();
  249. $this->addError('edit', $e->getMessage());
  250. return null;
  251. }
  252. return $this->_model;
  253. }
  254. /**
  255. * 复销
  256. * @return bool|null
  257. * @throws Exception
  258. * @throws \yii\db\Exception
  259. */
  260. public function add(){
  261. if(!$this->validate()){
  262. return null;
  263. }
  264. $ids = $this->goodsId;
  265. $totalAmount = 0;
  266. $totalPv = 0;
  267. foreach ($this->goodsNum as $k => $v) {
  268. if ($v) {
  269. $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1',[':ID'=> $ids[$k]]);
  270. if($goods['STORE_NUMS']>0){
  271. $discount = $goods['SELL_DISCOUNT'];
  272. $realPrice = $goods['SELL_PRICE'] * $discount;
  273. $realPv = $goods['PRICE_PV'] * $discount;
  274. $totalAmount += $realPrice * intval($v);
  275. $totalPv += $realPv * intval($v);
  276. $this->_orderGoods[] = [
  277. 'GOODS_ID' => $goods['ID'],
  278. 'PRICE' => $goods['SELL_PRICE'],
  279. 'PV' => $goods['PRICE_PV'],
  280. 'REAL_PRICE' => $realPrice,
  281. 'REAL_PV' => $realPv,
  282. 'POINT' => $goods['POINT'],
  283. 'BUY_NUMS' => intval($v),
  284. 'SKU_CODE' => $goods['GOODS_NO'],
  285. 'GOODS_TITLE' => $goods['GOODS_NAME']
  286. ];
  287. }
  288. }
  289. }
  290. $this->_decAmount = $totalAmount;
  291. $this->_decPv = $totalPv;
  292. $this->_freight = ($totalAmount>=300) ? 0 : 15;
  293. $this->_payAmount = $this->_decAmount + $this->_freight;
  294. $db = \Yii::$app->db;
  295. $transaction = $db->beginTransaction();
  296. try {
  297. //判断用户余额是否充足
  298. $loginUserId = \Yii::$app->user->id;
  299. if($this->payType=='cash') {
  300. if (Cash::getAvailableBalance($loginUserId) < $this->_payAmount) {
  301. throw new Exception('余额不足,无法购买商品');
  302. }
  303. } else if ($this->payType =='exchange') {
  304. if ($this->_payAmount > Balance::getBalanceExchangePoints($loginUserId)) {
  305. throw new Exception('兑换点数不足,无法购买商品');
  306. }
  307. } else{
  308. if ($this->_payAmount > Balance::getBalanceReconsumePoints($loginUserId)) {
  309. throw new Exception('复销点数不足,无法购买商品');
  310. }
  311. }
  312. /**
  313. * 2022-04-28
  314. * York
  315. * 支付后减少库存
  316. */
  317. foreach ($this->goodsNum as $k => $v){
  318. if ($v){
  319. $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1',[':ID'=> $ids[$k]]);
  320. if ($goods['STORE_NUMS'] >= $this->goodsNum[$k]){
  321. $data = ShopGoods::find()->where(['ID' => $ids[$k]])->one();
  322. $goods_store_nums = $data->STORE_NUMS - $this->goodsNum[$k];
  323. $data->STORE_NUMS = $goods_store_nums;
  324. $data->update();
  325. //下单后库存小于等于0 商品下架
  326. if($goods_store_nums <= 0){
  327. $data->STATUS = 0;
  328. $data->UPDATED_AT = Date::nowTime();
  329. $data->update();
  330. }
  331. }else{
  332. throw new Exception($goods['GOODS_NAME'].'库存不足,无法购买商品');
  333. }
  334. }
  335. }
  336. // exit();
  337. //写入订单
  338. if (!$orderResult = $this->addOrder()) {
  339. throw new Exception(Form::formatErrorsForApi($orderResult->getErrors()));
  340. }
  341. $transaction->commit();
  342. }catch (\Exception $e){
  343. $transaction->rollBack();
  344. $this->addError('add', $e->getMessage());
  345. return null;
  346. }
  347. return true;
  348. }
  349. /**
  350. * 复销订单
  351. */
  352. public function addOrder(){
  353. $periodObj = Period::instance();
  354. $nowPeriodNum = $periodObj->getNowPeriodNum();
  355. $nowCalcMonth = $periodObj->getYearMonth($nowPeriodNum);
  356. $userId = \Yii::$app->user->id;
  357. $userName = Info::getUserNameByUserId($userId);
  358. // 加入订单信息
  359. $warehouse = Region::getWarehouseByCode($this->_address['PROVINCE']);//仓库
  360. if(!$warehouse){
  361. throw new Exception('地区暂时不支持配送,具体联系客服');
  362. }
  363. $ordNo = $this->_generateSn();
  364. $orderModel = new Order();
  365. $orderModel->SN = 'OS'.$ordNo;
  366. $orderModel->DEC_SN = 'DS'.$ordNo;
  367. $orderModel->ORDER_TYPE = $this->type;
  368. $orderModel->USER_ID = $userId;
  369. $orderModel->USER_NAME = $userName;
  370. $orderModel->ORDER_AMOUNT = $this->_decAmount;
  371. $orderModel->PV = $this->payType =='exchange' ? 0 : $this->_decPv;
  372. $orderModel->PAY_AMOUNT = $this->_payAmount;
  373. $orderModel->PAY_PV = $this->payType =='exchange' ? 0 : $this->_decPv; // 兑换积分不能算业绩
  374. $orderModel->PAY_AT = Date::nowTime();
  375. $orderModel->PAY_TYPE = $this->payType;
  376. $orderModel->PERIOD_NUM = $nowPeriodNum;
  377. $orderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  378. $orderModel->FREIGHT = $this->_freight;
  379. $orderModel->PAY_FREIGHT = $this->_freight;
  380. $orderModel->CONSIGNEE = $this->_address['CONSIGNEE'];
  381. $orderModel->MOBILE = $this->_address['MOBILE'];
  382. $orderModel->PROVINCE = $this->_address['PROVINCE'];
  383. $orderModel->CITY = $this->_address['CITY'];
  384. $orderModel->COUNTY = $this->_address['COUNTY'];
  385. $orderModel->ADDRESS = $this->_address['ADDRESS'];
  386. $orderModel->FRONT_REMARK = $this->remark;
  387. $orderModel->WAREHOUSE = $warehouse;
  388. $orderModel->STATUS = 1;
  389. $orderModel->CREATED_AT = Date::nowTime();
  390. $orderModel->CREATE_USER = $userName;
  391. if(!$orderModel->save()){
  392. $this->addErrors($orderModel->getErrors());
  393. return false;
  394. }
  395. // 加入商品到订单商品表
  396. foreach($this->_orderGoods as $key=>$value){
  397. // 增加判断,如果订单是兑换券购买,则AR_ORDER_GOODS中的REAL_PV真实PV应该是0
  398. if ($orderModel->PAY_TYPE == 'exchange') {
  399. $this->_orderGoods[$key]['REAL_PV'] = 0;
  400. }
  401. $this->_orderGoods[$key]['ORDER_SN'] = $orderModel->SN;
  402. $this->_orderGoods[$key]['P_CALC_MONTH'] = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  403. }
  404. OrderGoods::batchInsert($this->_orderGoods);
  405. //扣除会员余额/积分
  406. if($this->payType=='cash') {
  407. Cash::changeUserCash(\Yii::$app->user->id, 'CASH', -abs($this->_payAmount), ['REMARK' => '会员复销余额支付']);
  408. } else if ($this->payType=='exchange') {
  409. Balance::changeUserBonus(\Yii::$app->user->id,'exchange_points', -abs($this->_payAmount),['DEAL_TYPE_ID' => DealType::EXCHANGE_POINTS_EXCHANGE,'REMARK' => '会员兑换积分兑换']);
  410. } else{
  411. Balance::changeUserBonus(\Yii::$app->user->id,'reconsume_points', -abs($this->_payAmount),['DEAL_TYPE_ID' => DealType::RECONSUME_POINTS_EXCHANGE,'REMARK' => '会员复销点数兑换']);
  412. }
  413. return $orderModel;
  414. }
  415. /**
  416. * 帮会员复销
  417. * @return bool|null
  418. * @throws Exception
  419. * @throws \yii\db\Exception
  420. */
  421. public function reconsumeAdd(){
  422. if(!$this->validate()){
  423. return null;
  424. }
  425. $ids = $this->goodsId;
  426. $totalAmount = 0;
  427. $totalPv = 0;
  428. foreach ($this->goodsNum as $k => $v) {
  429. if ($v) {
  430. $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1',[':ID'=> $ids[$k]]);
  431. if($goods['STORE_NUMS']>0){
  432. $discount = $goods['SELL_DISCOUNT'];
  433. $realPrice = $goods['SELL_PRICE'] * $discount;
  434. $realPv = $goods['PRICE_PV'] * $discount;
  435. $totalAmount += $realPrice * intval($v);
  436. $totalPv += $realPv * intval($v);
  437. $this->_orderGoods[] = [
  438. 'GOODS_ID' => $goods['ID'],
  439. 'PRICE' => $goods['SELL_PRICE'],
  440. 'PV' => $goods['PRICE_PV'],
  441. 'REAL_PRICE' => $realPrice,
  442. 'REAL_PV' => $realPv,
  443. 'POINT' => $goods['POINT'],
  444. 'BUY_NUMS' => intval($v),
  445. 'SKU_CODE' => $goods['GOODS_NO'],
  446. 'GOODS_TITLE' => $goods['GOODS_NAME']
  447. ];
  448. }
  449. }
  450. }
  451. $this->_decAmount = $totalAmount;
  452. $this->_decPv = $totalPv;
  453. $this->_freight = ($totalAmount>=300) ? 0 : 15;
  454. $this->_payAmount = $this->_decAmount + $this->_freight;
  455. $db = \Yii::$app->db;
  456. $transaction = $db->beginTransaction();
  457. try {
  458. $loginUserId = \Yii::$app->user->id;
  459. //是否开启伞下会员限制
  460. $isResaleUmbrella = Cache::getSystemConfig()['isResaleUmbrella']['VALUE'];
  461. if($isResaleUmbrella){
  462. $userId = Info::getUserIdByUserName($this->userName);
  463. $userNetwork = UserNetwork::find()->where("USER_ID=:USER_ID AND INSTR(PARENT_UIDS,'{$loginUserId}')>0", ['USER_ID'=>$userId])->count();
  464. if(!$userNetwork){
  465. throw new Exception($this->userName.'不是您的伞下会员,不能为其复消!');
  466. }
  467. }
  468. //判断用户余额是否充足
  469. if($this->payType=='cash') {
  470. if (Cash::getAvailableBalance($loginUserId) < $this->_payAmount) {
  471. throw new Exception('余额不足,无法购买商品');
  472. }
  473. }else{
  474. if ($this->_payAmount > Balance::getBalanceReconsumePoints($loginUserId)) {
  475. throw new Exception('复销点数不足,无法购买商品');
  476. }
  477. }
  478. /**
  479. * 2022-04-28
  480. * York
  481. * 支付后减少库存
  482. */
  483. foreach ($this->goodsNum as $k => $v){
  484. if ($v){
  485. $goods = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1',[':ID'=> $ids[$k]]);
  486. if ($goods['STORE_NUMS'] >= $this->goodsNum[$k]){
  487. $data = ShopGoods::find()->where(['ID' => $ids[$k]])->one();
  488. $goods_store_nums = $data->STORE_NUMS - $this->goodsNum[$k];
  489. $data->STORE_NUMS = $goods_store_nums;
  490. $data->update();
  491. //下单后库存小于等于0 商品下架
  492. if($goods_store_nums <= 0){
  493. $data->STATUS = 0;
  494. $data->UPDATED_AT = Date::nowTime();
  495. $data->update();
  496. }
  497. }else{
  498. throw new Exception($goods['GOODS_NAME'].'库存不足,无法购买商品');
  499. }
  500. }
  501. }
  502. // exit();
  503. //写入订单
  504. if (!$orderResult = $this->addUserOrder()) {
  505. throw new Exception(Form::formatErrorsForApi($orderResult->getErrors()));
  506. }
  507. $transaction->commit();
  508. }catch (\Exception $e){
  509. $transaction->rollBack();
  510. $this->addError('add', $e->getMessage());
  511. return null;
  512. }
  513. return true;
  514. }
  515. /**
  516. * 帮会员复消的订单
  517. */
  518. public function addUserOrder(){
  519. $periodObj = Period::instance();
  520. $nowPeriodNum = $periodObj->getNowPeriodNum();
  521. $nowCalcMonth = $periodObj->getYearMonth($nowPeriodNum);
  522. //帮复消会员Id(登陆会员)
  523. $loginUserId = \Yii::$app->user->id;
  524. $loginUserName = Info::getUserNameByUserId($loginUserId);
  525. //订单会员Id
  526. $userId = Info::getUserIdByUserName($this->userName);
  527. // 加入订单信息
  528. $warehouse = Region::getWarehouseByCode($this->province);//仓库
  529. if(!$warehouse){
  530. throw new Exception('地区暂时不支持配送,具体联系客服');
  531. }
  532. $ordNo = $this->_generateSn();
  533. $orderModel = new Order();
  534. $orderModel->SN = 'OS'.$ordNo;
  535. $orderModel->DEC_SN = 'DS'.$ordNo;
  536. $orderModel->ORDER_TYPE = $this->type;
  537. $orderModel->USER_ID = $userId;
  538. $orderModel->USER_NAME = $this->userName;
  539. $orderModel->ORDER_AMOUNT = $this->_decAmount;
  540. $orderModel->PV = $this->_decPv;
  541. $orderModel->PAY_AMOUNT = $this->_payAmount;
  542. $orderModel->PAY_PV = $this->_decPv;
  543. $orderModel->PAY_AT = Date::nowTime();
  544. $orderModel->PAY_TYPE = $this->payType;
  545. $orderModel->PERIOD_NUM = $nowPeriodNum;
  546. $orderModel->P_CALC_MONTH = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  547. $orderModel->FREIGHT = $this->_freight;
  548. $orderModel->PAY_FREIGHT = $this->_freight;
  549. $orderModel->CONSIGNEE = $this->consignee;
  550. $orderModel->MOBILE = $this->acceptMobile;
  551. $orderModel->PROVINCE = $this->province;
  552. $orderModel->CITY = $this->city;
  553. $orderModel->COUNTY = $this->county;
  554. $orderModel->ADDRESS = $this->detailaddress;
  555. $orderModel->FRONT_REMARK = $this->remark;
  556. $orderModel->WAREHOUSE = $warehouse;
  557. $orderModel->STATUS = 1;
  558. $orderModel->CREATED_AT = Date::nowTime();
  559. $orderModel->CREATE_USER = $loginUserName;
  560. if(!$orderModel->save()){
  561. $this->addErrors($orderModel->getErrors());
  562. return false;
  563. }
  564. // 加入商品到订单商品表
  565. foreach($this->_orderGoods as $key=>$value){
  566. $this->_orderGoods[$key]['ORDER_SN'] = $orderModel->SN;
  567. $this->_orderGoods[$key]['P_CALC_MONTH'] = Date::ociToDate($nowCalcMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  568. }
  569. OrderGoods::batchInsert($this->_orderGoods);
  570. //扣除会员余额/积分
  571. if($this->payType=='cash') {
  572. Cash::changeUserCash($loginUserId, 'CASH', -abs($this->_payAmount), ['REMARK' => '会员复销余额支付']);
  573. }else{
  574. Balance::changeUserBonus($loginUserId,'reconsume_points', -abs($this->_payAmount),['DEAL_TYPE_ID' => DealType::RECONSUME_POINTS_EXCHANGE, 'REMARK' => '会员复销点数兑换']);
  575. }
  576. return $orderModel;
  577. }
  578. /**
  579. * 生成流水号
  580. * @return string
  581. */
  582. private function _generateSn() {
  583. return Date::today('Ymd') . $this->_random(10, 1);
  584. }
  585. /**
  586. * 生成随机数
  587. * @param $length
  588. * @param int $numeric
  589. * @return string
  590. */
  591. private function _random($length, $numeric = 0) {
  592. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  593. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  594. $hash = '';
  595. $max = strlen($seed) - 1;
  596. for ($i = 0; $i < $length; $i++) {
  597. $hash .= $seed[mt_rand(0, $max)];
  598. }
  599. return $hash;
  600. }
  601. }