CostOrderSubmitForm.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\adopt\models\client;
  8. use app\models\Address;
  9. use app\models\Cart;
  10. use app\models\Goods;
  11. use app\models\UserCoupon;
  12. use app\plugins\adopt\models\AdoptCostOrder;
  13. use app\plugins\adopt\models\AdoptOrderInfo;
  14. use app\plugins\scanCodePay\models\Order;
  15. use app\plugins\scanCodePay\models\OrderDetail;
  16. // use app\utils\CloudPrint;
  17. use app\utils\Notice\NoticeSend;
  18. use app\utils\OrderNo;
  19. use yii\base\Model;
  20. use yii\helpers\Json;
  21. class CostOrderSubmitForm extends Model
  22. {
  23. public $user;
  24. public $payment;
  25. public $use_integral;
  26. public $order_id;
  27. public $address_id;
  28. public $user_id;
  29. public $store_id;
  30. public $weight;
  31. public $order_type;
  32. public $address;
  33. public function rules()
  34. {
  35. $rules = [
  36. [['payment', 'use_integral', 'order_id', 'address_id', 'user_id', 'store_id', 'order_type'], 'integer'],
  37. [[ 'weight'], 'number']
  38. ];
  39. return $rules;
  40. }
  41. public function save()
  42. {
  43. if (!$this->validate())
  44. return [
  45. 'code' => 1,
  46. 'msg' => $this->getErrorSummary(false)[0],
  47. ];
  48. /**
  49. * @var $address Address
  50. */
  51. $order_submit_preview1 = new CostOrderSubmitPreviewForm();
  52. $order_submit_preview1->order_type = $this->order_type;
  53. $order_submit_preview1->order_id = $this->order_id;
  54. $order_submit_preview1->weight = $this->weight;
  55. $order_submit_preview1->store_id = $this->store_id;
  56. $order_submit_preview1->user_id = $this->user_id;
  57. $order_submit_preview1->address_id = $this->address_id;
  58. $this->address = $order_submit_preview1->getAddressData();
  59. $address = (object)($this->address);
  60. $order_id_list = [];
  61. $t = \Yii::$app->db->beginTransaction();
  62. // $order_submit_preview = new CostOrderSubmitPreviewForm();
  63. // $order_submit_preview->order_type = $this->order_type;
  64. // $order_submit_preview->order_id = $this->order_id;
  65. // $order_submit_preview->weight = $this->weight;
  66. // $order_submit_preview->store_id = $this->store_id;
  67. // $order_submit_preview->user_id = $this->user_id;
  68. $cost_detail = $order_submit_preview1->getCostDetail();
  69. $order = new AdoptCostOrder();
  70. $order->store_id = $this->store_id;
  71. $order->user_id = $this->user_id;
  72. $order->order_id = $this->order_id;
  73. $order->order_no = OrderNo::getOrderNo(OrderNo::ADOPT_COST_ORDER);
  74. $order->pay_price = $cost_detail['pay_price'];
  75. $order->express_price = $cost_detail['express_price'];
  76. $order->total_price = $cost_detail['pay_price'];
  77. $order->weight = $this->weight;
  78. $order->cost_detail = json_encode($cost_detail['cost_list']);
  79. $order->order_type = $this->order_type;
  80. // 是否是平台小程序订单
  81. if (is_platform()) {
  82. $order->is_platform = 1;
  83. }
  84. if (get_md_id()) {
  85. $order->md_id = get_md_id();
  86. }
  87. $order->integral = json_encode(['forehead' => 0, 'forehead_integral' => 0], JSON_UNESCAPED_UNICODE);
  88. $order->created_at = time();
  89. $order->content = '';
  90. $order->is_offline = 0;
  91. $order->version = cyy_version();
  92. $order->mch_id = 0;
  93. $order->discount = 10; // 当面付订单不享受会员折扣
  94. $order->remark = isset($mch['remark']) ?: '' ;
  95. if ($this->payment == 2) {
  96. $order->pay_type = 2;
  97. $order->is_pay = 0;
  98. }
  99. if ($this->payment == 3) {
  100. $order->pay_type = 3;
  101. $order->is_pay = 0;
  102. }
  103. $order->address = $address->province . $address->city . $address->district . $address->detail;
  104. $order->mobile = $address->mobile;
  105. $order->name = $address->name;
  106. $order->province_id = $address->province_id;
  107. $order->city_id = $address->city_id;
  108. $order->district_id = $address->district_id;
  109. $order->address_data = json_encode([
  110. 'province' => trim($address->province),
  111. 'city' => trim($address->city),
  112. 'district' => trim($address->district),
  113. 'detail' => trim($address->detail),
  114. 'latitude' => $address->latitude,
  115. 'longitude' => $address->longitude,
  116. ], JSON_UNESCAPED_UNICODE);
  117. if ($order->save()) {
  118. $adopt_order = AdoptOrderInfo::find()->where(['store_id' => $this->store_id, 'order_id' => $this->order_id])->one();
  119. $adopt_order->harvest_weight -= $this->weight;
  120. if (!$adopt_order->save()){
  121. $t->rollBack();
  122. return [
  123. 'code' => 0,
  124. 'msg' => $adopt_order->getErrorSummary(false)[0],
  125. 'data' => $adopt_order->getErrors(),
  126. ];
  127. };
  128. // 订单提交完成发送消息
  129. NoticeSend::OrderSubmit($this->user_id, $order->mobile, $order->order_no, $order->pay_price, '认养费用',2, $order->store_id);
  130. $order_id_list[] = $order->id;
  131. } else {
  132. $t->rollBack();
  133. return [
  134. 'code' => 0,
  135. 'msg' => $order->getErrorSummary(false)[0],
  136. 'data' => $order->getErrors(),
  137. ];
  138. }
  139. if (count($order_id_list) > 0) {
  140. $t->commit();
  141. $order_id = $order_id_list[0];
  142. // CloudPrint::doPrint($order_id, 0, $this->store_id);
  143. return [
  144. 'code' => 0,
  145. 'msg' => '订单提交成功',
  146. 'data' => (object)[
  147. 'order_id' => $order_id_list[0],
  148. 'add_time' => $order->created_at
  149. ],
  150. ];
  151. } else {
  152. $t->rollBack();
  153. return [
  154. 'order_list' => $order_id_list
  155. ];
  156. }
  157. }
  158. }