OrderPriceForm.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Order;
  9. use app\models\OrderDetail;
  10. use yii\base\Model;
  11. class OrderPriceForm extends Model
  12. {
  13. public $store_id;
  14. public $order_id;
  15. public $pay_price;
  16. public $express_price;
  17. public $express_type;
  18. public $order_type;
  19. public function rules()
  20. {
  21. return [
  22. [['pay_price', 'express_price'], 'number'],
  23. [['order_type'],'string'],
  24. [['order_id', 'express_type'],'integer']
  25. ];
  26. }
  27. public function save()
  28. {
  29. if (!$this->validate()) {
  30. return [
  31. 'code' => 1,
  32. 'msg' => $this->getErrorSummary(false)[0],
  33. ];
  34. }
  35. $order = Order::findOne([
  36. 'id' => $this->order_id,
  37. 'is_delete' => Order::IS_DELETE_FALSE,
  38. 'is_pay' => Order::IS_PAY_FALSE
  39. ]);
  40. if (!$order) {
  41. return [
  42. 'code' => 1,
  43. 'msg' => '订单错误'
  44. ];
  45. }
  46. $money = doubleval($order->pay_price);
  47. $express = doubleval($order->express_price);
  48. if ($this->express_type != OrderDetail::GOODS_DELIVERY_IM && $express != $this->express_price) {
  49. if ($this->express_price < 0) {
  50. return [
  51. 'code' => 1,
  52. 'msg' => '运费不能小于0'
  53. ];
  54. }
  55. $order->before_update_express = $express;
  56. $order->express_price = $this->express_price;
  57. }
  58. if ($this->pay_price && $money != $this->pay_price) {
  59. if ($this->pay_price < 0.01) {
  60. return [
  61. 'code' => 1,
  62. 'msg' => '订单金额不能小于0.01'
  63. ];
  64. }
  65. //改价时候修改每个商品的价格 便于退款售后
  66. $order_detail_arr = OrderDetail::find()->where(['order_id' => $order->id, 'is_delete' => 0])->select('total_price, num, id')->asArray()->all();
  67. $order_detail_total_price = array_sum(array_column($order_detail_arr, 'total_price'));
  68. $price = 0;
  69. foreach ($order_detail_arr as $order_detail_index => $item) {
  70. $order_detail = OrderDetail::findOne($item['id']);
  71. if ($order_detail_index === (count($order_detail_arr) - 1)) {
  72. $order_detail_price = bcsub($this->pay_price, $price, 2);
  73. } else {
  74. $profit = bcdiv($item['total_price'], $order_detail_total_price, 2);
  75. $order_detail_price = bcmul($profit, $this->pay_price, 2);
  76. $price = bcadd($price, $order_detail_price, 2);
  77. }
  78. $order_detail->total_price = $order_detail_price;
  79. $order_detail->save();
  80. }
  81. $order->before_update_price = $money;
  82. if ($this->express_type != OrderDetail::GOODS_DELIVERY_IM) {
  83. $order->pay_price = $this->express_price ? $this->pay_price + $this->express_price : $this->pay_price;
  84. } else {
  85. $order->pay_price = $this->pay_price;
  86. }
  87. }
  88. if ($order->save()) {
  89. \app\models\OrderAdminHandleLog::addHandleLog($order->id, \app\models\OrderAdminHandleLog::HANDLE_TYPE_EDIT_PRICE);
  90. // $orderDetailList = OrderDetail::findAll(['order_id' => $order->id, 'is_delete' => 0]);
  91. // $goodsTotalPrice = 0.00;
  92. // $goodsTotalPayPrice = $order->pay_price - $order->express_price;
  93. // foreach ($orderDetailList as $goods) {
  94. // $goodsTotalPrice += $goods->total_price;
  95. // }
  96. // foreach ($orderDetailList as $goods) {
  97. // if (in_array(get_plugin_type(), [0,2])) {
  98. // $goods->total_price = doubleval(sprintf('%.2f', $goodsTotalPayPrice * $goods->total_price / $goodsTotalPrice));
  99. // }
  100. // $goods->save();
  101. // }
  102. // TODO: 重新设置分销等
  103. // $this->setReturnData($order);
  104. return [
  105. 'code' => 0,
  106. 'msg' => '成功'
  107. ];
  108. } else {
  109. return [
  110. 'code' => 1,
  111. 'msg' => $this->getErrorSummary(false)[0],
  112. ];
  113. }
  114. }
  115. // /**
  116. // * 设置佣金
  117. // */
  118. // private function setReturnData($order)
  119. // {
  120. // $form = new ShareMoneyForm();
  121. // $form->order = $order;
  122. // if ($this->order_type == 'ms') {
  123. // $form->order_type = 1;
  124. // } else {
  125. // $form->order_type = 0;
  126. // }
  127. // return $form->setData();
  128. // }
  129. }