| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\constants\OptionSetting;
- use app\models\Goods;
- use app\models\GoodsBook;
- use app\models\Mch;
- use app\models\Md;
- use app\models\Option;
- use app\models\Order;
- use app\models\Store;
- use app\models\OrderDetail;
- use app\models\OrderRefund;
- use app\models\User;
- use app\models\UserCoupon;
- use app\models\SaasCoupon;
- use app\modules\alliance\models\order\OrderForm;
- use app\utils\Delivery\Delivery;
- use yii\base\BaseObject;
- use yii\data\Pagination;
- use app\utils\Notice\NoticeAction;
- use yii\helpers\Json;
- use app\models\DeliveryInfo;
- class OrderListForm extends OrderForm
- {
- public $store_id;
- public $user_id;
- public $saas_id;
- public $status;
- public $delivery_status;
- public $delivery_type;
- public $page;
- public $limit = 20;
- public $order_id;
- public $order_no;
- public $key;
- public $nowlatitude;
- public $nowlongitude;
- public function rules()
- {
- return [
- [['page', 'limit', 'status', 'delivery_type','saas_id'], 'integer'],
- [['page'], 'default', 'value' => 1],
- [['limit'], 'default', 'value' => 20],
- [['order_no'], 'default', 'value' => 0],
- [['order_id', 'key'],'trim'],
- [['nowlongitude', 'nowlatitude'], 'safe'],
- ];
- }
- public function search()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $this->saas_id = get_saas_user_id();
- $query = Order::find()->where([
- 'is_delete' => 0,
- //'store_id' => $this->store_id,
- 'saas_id' => $this->saas_id,
- 'user_delete' => 0
- ]);
- if (isset($this->status) && $this->status > -1) {
- $query->andWhere([
- '<>', 'trade_status', Order::ORDER_FLOW_CANCEL
- ]);
- }
- if ($this->status == 0) {//待付款
- $query->andWhere([
- 'is_pay' => Order::IS_PAY_FALSE,
- ])->andWhere(['!=', 'pay_type' , 2])->andWhere(['!=', 'trade_status', 1]);
- }
- if ($this->status == 1) {//待发货
- $query->andWhere([
- 'or',
- ['trade_status' => Order::ORDER_FLOW_NO_SEND],
- ['pay_type' => 2, 'trade_status' => Order::ORDER_FLOW_DEFAULT]
- ])->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]]);
- }
- if ($this->status == 2) {//待收货
- $query->andWhere([
- 'trade_status' => Order::ORDER_FLOW_SEND,
- ]);
- }
- if ($this->status == 3) {//已完成
- $query->andWhere([
- 'trade_status' => Order::ORDER_FLOW_CONFIRM,
- ]);
- }
- if ($this->status == 4) {//售后订单
- return $this->getRefundList();
- }
- $query->andWhere(['is_recycle' => 0]);
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
- /* @var Order[] $list */
- $list = $query->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->all();
- $new_list = [];
- $pay_type_list = OrderForm::getPayType();
- foreach ($list as $order) {
- // 预约订单超过15分钟未支付自动取消
- if (time() - $order->created_at > 15 * 60 && $order->trade_status != Order::ORDER_FLOW_CANCEL && $order->is_pay == 0 && $order->pay_type != 2) {
- $order->trade_status = Order::ORDER_FLOW_CANCEL;
- $order->updated_at = time();
- $order->save();
- SaasCoupon::updateAll(['is_use' => 0], ['id' => $order->user_coupon_id]);
- }
- $order_detail_list = OrderDetail::findAll(['order_id' => $order->id, 'is_delete' => 0]);
- $goods_list = [];
- foreach ($order_detail_list as $order_detail) {
- $goods = Json::encode($order_detail->goods_info);
- if (!$goods) {
- continue;
- }
- $goods_pic = isset($order_detail->pic) ? $order_detail->pic : $goods['cover_pic'];
- $goods_list[] = (object)[
- 'goods_id' => isset($goods['id']) ? $goods['id'] : $order_detail->goods_id,
- 'goods_pic' => $goods_pic,
- 'goods_name' => $order_detail->goods_name,
- 'num' => $order_detail->num,
- 'price' => $order_detail->total_price,
- 'attr_list' => json_decode($order_detail->attr),
- ];
- }
- $qrcode = null;
- $mch = [
- 'id' => 0,
- 'name' => '平台自营',
- 'logo' => '',
- ];
- $orderRefund = OrderRefund::find()->where(['store_id' => $order->store_id, 'order_id' => $order->id])->exists();
- $deliveryInfo = null;
- if ($order->is_delivery == 1) {
- $deliveryInfo = DeliveryInfo::find()->where(['order_no' => $order->order_no])->one();
- }
- $is_order_refund = $order_refund_enable = 0;
- if ($orderRefund) {
- $is_order_refund = 1;
- } else {
- $is_order_refund = 0;
- }
- if ($order->is_pay == 1 && ($order->trade_status == Order::ORDER_FLOW_SEND || $order->trade_status == Order::ORDER_FLOW_CONFIRM)) {
- $order_refund_enable = 1;
- } else {
- $order_refund_enable = 0;
- }
- if ($order->trade_status == Order::ORDER_FLOW_CONFIRM && intval($order->is_sale) === 0) {
- // $after_sale_time = Option::get(OptionSetting::STORE_AFTER_SALE_TIME, $this->store_id);
- // if ((time() - $order->confirm_time) > $after_sale_time['value'] * 86400) {//超过可售后时间
- $order_refund_enable = 1;
- // }
- }
- $food_pay_type = [];
- $store = Store::findOne($order->store_id);
- if (!empty($store)) {
- if ($order->md_id > 0) {
- $md = Md::findOne($order->md_id);
- $food_payment = $md->food_payment;
- } else {
- $store = Store::findOne($order->store_id);
- $food_payment = $store->food_payment;
- }
- $food_pay_list = [];
- if (!empty($food_payment)) {
- $food_pay_type = Json::decode($food_payment);
- foreach ($food_pay_type as $key => &$value) {
- if ($value['key'] == 'friend') {
- $value['payment'] = 7;
- if(!$value['name']){
- $value['name'] = "朋友代付";
- }
- }
- if ($value['key'] == 'alipay') {
- $value['payment'] = 4;
- if(!$value['name']){
- $value['name'] = "支付宝支付";
- }
- }
- if ($value['key'] == 'wechat') {
- $value['payment'] = 1;
- if(!$value['name']){
- $value['name'] = "微信支付";
- }
- }
- if ($value['key'] == 'huodao') {
- $value['payment'] = 2;
- if(!$value['name']){
- $value['name'] = "货到付款";
- }
- // 商盟去掉货到付款
- unset($food_pay_type[$key]);
- }
- }
- $food_pay_list = $food_pay_type;
- }
- foreach ($food_pay_list as $k => $val) {
- if (($val['payment'] == 4 && is_wechat_platform()) || ($val['payment'] == 4 && intval($val['value']) === 0)) {
- unset($food_pay_list[$k]);
- }
- if (($val['payment'] == 1 && is_alipay_platform()) || ($val['payment'] == 1 && intval($val['value']) === 0)) {
- unset($food_pay_list[$k]);
- }
- if ($val['payment'] == 7 && intval($val['value']) === 0) {
- unset($food_pay_list[$k]);
- }
- }
- $food_pay_list = array_values($food_pay_list);
- // 点餐支付方式
- $food_pay_type = $food_pay_list;
- }
- $storeInfo = Store::find()->where(['id' => $order->store_id])->asArray()->one();
- $new_list[] = (object)[
- 'is_order_refund' => $is_order_refund,
- 'order_refund_enable' => $order_refund_enable,
- 'order_id' => $order->id,
- 'order_no' => $order->order_no,
- 'add_time' => $order->created_at,
- 'goods_list' => $goods_list,
- 'total_price' => $order->total_price,
- 'pay_price' => sprintf("%1.2f", $order->pay_price),
- 'is_pay' => $order->is_pay,
- 'is_comment' => $order->is_comment,
- 'is_offline' => $order->is_offline,
- 'qrcode' => $qrcode,
- 'offline_qrcode' => $order->offline_qrcode,
- 'express' => $order->express,
- 'mch' => $mch,
- 'type' => $order->type,
- 'pay_type' => $order->pay_type,
- 'refund' => $orderRefund,
- 'apply_delete' => $order->apply_delete,
- 'trade_status' => $order->trade_status,
- 'delivery_fee' => $deliveryInfo ? $deliveryInfo->fee : 0,
- 'is_delivery' => $order->is_delivery,
- 'order_type' => $order->order_type,
- 'store_id' => $storeInfo['id'],
- 'store_name' => $storeInfo['name'] ?: '平台自营',
- 'store_logo' => $storeInfo['logo'],
- 'pay_type_list' => $food_pay_type ?: $pay_type_list
- ];
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $new_list,
- 'pay_type_list' => $pay_type_list,
- 'revoke_tpl'=>'',
- 'template_id' => NoticeAction::getSendTamplateId(['order_cancel']),
- 'cancel_reason' => Delivery::$validReason
- ],
- ];
- }
- private function getRefundList()
- {
- $query = OrderRefund::find()->alias('or')
- ->leftJoin(['od' => OrderDetail::tableName()], 'od.id=or.order_detail_id')
- ->leftJoin(['o' => Order::tableName()], 'o.id=or.order_id')
- ->where([
- 'or.store_id' => $this->store_id,
- 'or.user_id' => $this->user_id,
- 'or.is_delete' => 0,
- 'o.is_delete' => 0,
- 'od.is_delete' => 0,
- ]);
- if($this->order_id){
- $query->andWhere(['o.id' => $this->order_id]);
- }
- $count = $query->count();
- $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
- $list = $query->select('o.id AS order_id,o.order_no,or.id AS order_refund_id,od.goods_id,or.created_at,od.num,od.total_price,od.attr,or.refund_price,or.type,or.status,or.is_agree,or.is_user_send,od.goods_info, od.goods_name, od.pic')
- ->limit($pagination->limit)->offset($pagination->offset)->orderBy('or.id DESC')->asArray()->all();
- $new_list = [];
- foreach ($list as $item) {
- $goods = Json::decode($item['goods_info']) ?:Goods::find()->where(['id' => $item['goods_id']])->asArray()->one();
- if (!$goods) {
- continue;
- }
- $new_list[] = (object)[
- 'order_id' => intval($item['order_id']),
- 'order_no' => $item['order_no'],
- 'goods_list' => [(object)[
- 'goods_id' => intval($goods->id),
- 'goods_pic' => $item['pic'] ?: $goods['cover_pic'],
- 'goods_name' => $item['goods_name'],
- 'num' => intval($item['num']),
- 'price' => doubleval(sprintf('%.2f', $item['total_price'])),
- 'attr_list' => json_decode($item['attr']),
- ]],
- 'addtime' => date('Y-m-d H:i', $item['created_at']),
- 'refund_price' => doubleval(sprintf('%.2f', $item['refund_price'])),
- 'refund_type' => $item['type'],
- 'refund_status' => $item['status'],
- 'order_refund_id' => $item['order_refund_id'],
- 'is_agree' => $item['is_agree'],
- 'is_user_send' => $item['is_user_send'],
- ];
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'row_count' => $count,
- 'page_count' => $pagination->pageCount,
- 'list' => $new_list,
- ],
- ];
- }
- public function getNewRefundList()
- {
- $after_sale_time = Option::get(OptionSetting::STORE_AFTER_SALE_TIME, $this->store_id);
- $new_list = [];
- if (!$this->status) {
- $query = Order::find()->alias('o')
- ->where([
- //'o.store_id' => $this->store_id,
- 'o.saas_id' => $this->saas_id,
- 'o.is_delete' => 0,
- ]);
- // if (get_md_id()) {
- // $query->andWhere(['o.md_id' => get_md_id()]);
- // }
- $query->andWhere(['o.trade_status' => Order::ORDER_FLOW_CONFIRM]);
- //$query->andWhere(['>', 'o.confirm_time', time() - $after_sale_time['value'] * 86400]); ??
- $list = $query->select('o.pay_price, o.confirm_time, o.id AS order_id,o.order_no,o.created_at')->orderBy('o.id DESC')->asArray()->all();
- foreach ($list as $item) {
- $detail_goods = OrderDetail::find()->where(['order_id' => $item['order_id'], 'is_refund' => 0])->asArray()->all();
- if (empty($detail_goods)) {
- continue;
- }
- $detail_goods = OrderDetail::find()->where(['order_id' => $item['order_id']])->asArray()->all();
- $order_data = [
- 'order_id' => intval($item['order_id']),
- 'order_no' => $item['order_no'],
- 'pay_price' => $item['pay_price'],
- 'goods_list' => [],
- 'addtime' => date('Y-m-d H:i', $item['created_at']),
- 'refund_price' => isset($item['refund_price']) ? doubleval(sprintf('%.2f', $item['refund_price'])) : 0,
- 'refund_type' => isset($item['type']) ? $item['type'] : 0,
- 'refund_status' => isset($item['status']) ? $item['status'] : 0,
- 'order_refund_id' => isset($item['order_refund_id']) ? $item['order_refund_id'] : 0,
- 'is_agree' => isset($item['is_agree']) ? $item['is_agree'] : 0,
- 'is_user_send' => isset($item['is_user_send']) ? $item['is_user_send'] : 0,
- ];
- $goods_list = [];
- foreach ($detail_goods as $detail) {
- $goods_list[] = [
- 'goods_id' => intval($detail['goods_id']),
- 'goods_pic' => $detail['pic'],
- 'goods_name' => $detail['goods_name'],
- 'num' => intval($detail['num']),
- 'price' => doubleval(sprintf('%.2f', $detail['total_price'])),
- 'attr_list' => json_decode($detail['attr']),
- ];
- }
- $order_data['goods_list'] = $goods_list;
- $new_list[] = $order_data;
- }
- }
- // 处理中 或者 已完成
- if ($this->status == 1 || $this->status == 2) {
- $query = OrderRefund::find()->alias('or')
- ->leftJoin(['o' => Order::tableName()], 'o.id=or.order_id')
- ->where([
- //'or.store_id' => $this->store_id,
- 'or.user_id' => $this->saas_id,
- 'or.is_delete' => 0,
- 'o.is_delete' => 0,
- 'or.user_delete' => 0
- ]);
- // if (get_md_id()) {
- // $query->andWhere(['or.md_id' => get_md_id()]);
- // }
- if ($this->status == 1) {
- $query->andWhere(['or.status' => OrderRefund::STATUS_IN, 'or.is_user_cancel' => 0]);
- }
- if ($this->status == 2) {
- $query->andWhere(['or', ['or.is_user_cancel' => 1], ['in', 'or.status', OrderRefund::$is_handle_status]]);
- }
- $list = $query->select('or.is_user_cancel, or.order_detail_id,o.pay_price, o.confirm_time, o.id AS order_id,o.order_no,or.id AS order_refund_id,or.created_at,or.refund_price,or.type,or.status,or.is_agree,or.is_user_send')->orderBy('or.id DESC')->asArray()->all();
- if (!empty($list)) {
- foreach ($list as $item) {
- $order_data = [
- 'is_user_cancel' => intval($item['is_user_cancel']),
- 'order_id' => intval($item['order_id']),
- 'order_no' => $item['order_no'],
- 'pay_price' => $item['pay_price'],
- 'goods_list' => [],
- 'addtime' => date('Y-m-d H:i', $item['created_at']),
- 'refund_price' => isset($item['refund_price']) ? doubleval(sprintf('%.2f', $item['refund_price'])) : 0,
- 'refund_type' => isset($item['type']) ? $item['type'] : 0,
- 'refund_status' => isset($item['status']) ? $item['status'] : 0,
- 'order_refund_id' => isset($item['order_refund_id']) ? $item['order_refund_id'] : 0,
- 'is_agree' => isset($item['is_agree']) ? $item['is_agree'] : 0,
- 'is_user_send' => isset($item['is_user_send']) ? $item['is_user_send'] : 0,
- ];
- $detail_id = Json::decode($item['order_detail_id']);
- $detail_goods = OrderDetail::find()->where(['order_id' => $item['order_id'], 'is_refund' => 1])->andWhere(['in', 'id', $detail_id])->asArray()->all();
- $goods_list = [];
- $price = 0;
- foreach ($detail_goods as $detail) {
- $goods_list[] = [
- 'goods_id' => intval($detail['goods_id']),
- 'goods_pic' => $detail['pic'],
- 'goods_name' => $detail['goods_name'],
- 'num' => intval($detail['num']),
- 'price' => doubleval(sprintf('%.2f', $detail['total_price'])),
- 'attr_list' => json_decode($detail['attr']),
- 'is_refund' => $detail['is_refund']
- ];
- $price += $detail['total_price'];
- }
- $order_data['pay_price'] = $price;
- $order_data['goods_list'] = $goods_list;
- $new_list[] = $order_data;
- }
- }
- }
- $data = array_slice($new_list, ($this->page - 1) * $this->limit, $this->limit);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'row_count' => count($new_list),
- 'page_count' => ceil(count($new_list) / $this->limit),
- 'list' => $data,
- ],
- ];
- }
- public static function getCountData($saas_id)
- {
- $form = new OrderListForm();
- $form->limit = 1;
- //$form->store_id = $store_id;
- $form->saas_id = $saas_id;
- $data = [];
- $form->status = -1;
- $res = $form->searchOrderCount();
- $data['all'] = $res;
- $form->status = 0;
- $res = $form->searchOrderCount();
- $data['status_0'] = $res;
- $form->status = 1;
- $res = $form->searchOrderCount();
- $data['status_1'] = $res;
- $form->status = 2;
- $res = $form->searchOrderCount();
- $data['status_2'] = $res;
- $form->status = 3;
- $res = $form->searchOrderCount();
- $data['status_3'] = $res;
- return $data;
- }
- /**
- * 获取订单数方法
- * @return array|int|string
- */
- public function searchOrderCount()
- {
- $query = Order::find()->where([
- 'is_delete' => 0,
- 'user_delete' => 0,
- //'store_id' => $this->store_id,
- 'saas_id' => $this->saas_id,
- ]);
- if (isset($this->status) && $this->status > -1) {
- $query->andWhere([
- '<>', 'trade_status', Order::ORDER_FLOW_CANCEL
- ]);
- }
- if ($this->status == 0) {//待付款
- $query->andWhere([
- 'is_pay' => Order::IS_PAY_FALSE,
- ])->andWhere(['!=', 'pay_type' , 2]);
- }
- if ($this->status == 1) {//待发货
- $query->andWhere([
- 'trade_status' => Order::ORDER_FLOW_NO_SEND,
- ])->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]]);
- }
- if ($this->status == 2) {//待收货
- $query->andWhere([
- 'trade_status' => Order::ORDER_FLOW_SEND,
- ]);
- }
- if ($this->status == 3) {//已完成
- $query->andWhere([
- 'trade_status' => Order::ORDER_FLOW_CONFIRM,
- ]);
- }
- $query->andWhere(['is_recycle' => 0]);
- return $query->count();
- }
- //获取积分兑换商品列表
- public function getIntegralList(){
- $store_id = $this->store_id;
- if ($store_id <= 0) {
- $store_id = [0, -1];
- }
- $query = Order::find()->alias('o')->leftJoin(["d"=>OrderDetail::tablename()],'o.id = d.order_id');
- $query->where([
- 'o.is_delete' => 0,
- 'o.user_delete' => 0,
- 'o.store_id' => $store_id,
- 'o.saas_id' => $this->user_id,
- 'o.order_type' => 7,
- 'o.is_pay' => 1
- ]);
- $query->select("o.id,o.pay_price,o.order_no,o.integral_price,o.trade_status,d.goods_name,d.goods_id,d.num,d.total_price,d.integral_price,d.pic,d.goods_info");
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach($list as &$val){
- $val['goods_info'] = json_decode($val['goods_info'],true);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ],
- ];
- }
- }
|