| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Order;
- use app\models\OrderDetail;
- use yii\base\Model;
- class OrderPriceForm extends Model
- {
- public $store_id;
- public $order_id;
- public $pay_price;
- public $express_price;
- public $express_type;
- public $order_type;
- public function rules()
- {
- return [
- [['pay_price', 'express_price'], 'number'],
- [['order_type'],'string'],
- [['order_id', 'express_type'],'integer']
- ];
- }
- public function save()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $order = Order::findOne([
- 'id' => $this->order_id,
- 'is_delete' => Order::IS_DELETE_FALSE,
- 'is_pay' => Order::IS_PAY_FALSE
- ]);
- if (!$order) {
- return [
- 'code' => 1,
- 'msg' => '订单错误'
- ];
- }
- $money = doubleval($order->pay_price);
- $express = doubleval($order->express_price);
- if ($this->express_type != OrderDetail::GOODS_DELIVERY_IM && $express != $this->express_price) {
- if ($this->express_price < 0) {
- return [
- 'code' => 1,
- 'msg' => '运费不能小于0'
- ];
- }
- $order->before_update_express = $express;
- $order->express_price = $this->express_price;
- }
- if ($this->pay_price && $money != $this->pay_price) {
- if ($this->pay_price < 0.01) {
- return [
- 'code' => 1,
- 'msg' => '订单金额不能小于0.01'
- ];
- }
- //改价时候修改每个商品的价格 便于退款售后
- $order_detail_arr = OrderDetail::find()->where(['order_id' => $order->id, 'is_delete' => 0])->select('total_price, num, id')->asArray()->all();
- $order_detail_total_price = array_sum(array_column($order_detail_arr, 'total_price'));
- $price = 0;
- foreach ($order_detail_arr as $order_detail_index => $item) {
- $order_detail = OrderDetail::findOne($item['id']);
- if ($order_detail_index === (count($order_detail_arr) - 1)) {
- $order_detail_price = bcsub($this->pay_price, $price, 2);
- } else {
- $profit = bcdiv($item['total_price'], $order_detail_total_price, 2);
- $order_detail_price = bcmul($profit, $this->pay_price, 2);
- $price = bcadd($price, $order_detail_price, 2);
- }
- $order_detail->total_price = $order_detail_price;
- $order_detail->save();
- }
- $order->before_update_price = $money;
- if ($this->express_type != OrderDetail::GOODS_DELIVERY_IM) {
- $order->pay_price = $this->express_price ? $this->pay_price + $this->express_price : $this->pay_price;
- } else {
- $order->pay_price = $this->pay_price;
- }
- }
- if ($order->save()) {
- \app\models\OrderAdminHandleLog::addHandleLog($order->id, \app\models\OrderAdminHandleLog::HANDLE_TYPE_EDIT_PRICE);
- // $orderDetailList = OrderDetail::findAll(['order_id' => $order->id, 'is_delete' => 0]);
- // $goodsTotalPrice = 0.00;
- // $goodsTotalPayPrice = $order->pay_price - $order->express_price;
- // foreach ($orderDetailList as $goods) {
- // $goodsTotalPrice += $goods->total_price;
- // }
- // foreach ($orderDetailList as $goods) {
- // if (in_array(get_plugin_type(), [0,2])) {
- // $goods->total_price = doubleval(sprintf('%.2f', $goodsTotalPayPrice * $goods->total_price / $goodsTotalPrice));
- // }
- // $goods->save();
- // }
- // TODO: 重新设置分销等
- // $this->setReturnData($order);
- return [
- 'code' => 0,
- 'msg' => '成功'
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- }
- // /**
- // * 设置佣金
- // */
- // private function setReturnData($order)
- // {
- // $form = new ShareMoneyForm();
- // $form->order = $order;
- // if ($this->order_type == 'ms') {
- // $form->order_type = 1;
- // } else {
- // $form->order_type = 0;
- // }
- // return $form->setData();
- // }
- }
|