| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\constants\OptionSetting;
- use app\librarys\Picqer\Barcode\BarcodeGeneratorJPG;
- use app\models\Admin;
- use app\models\AdminRole;
- use app\models\common\admin\order\CommonOrderSearch;
- use app\models\common\ExpressDetail;
- use app\models\DeliveryInfo;
- use app\models\Express;
- use app\models\FoodFlag;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\Md;
- use app\models\MdProfit;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\OrderForm;
- use app\models\OrderRefund;
- use app\models\OrderStateFlow;
- use app\models\SaasUser;
- use app\models\Salesman;
- use app\models\Shop;
- use app\models\Store;
- use app\models\StoreCloud;
- use app\models\User;
- use app\models\CloudGoodsBind;
- use app\models\OrderTransit;
- use app\models\Supplier;
- use app\models\VerifyCardSale;
- use app\models\VerifyCard;
- use app\models\VerifyCardAccount;
- use app\utils\Delivery\Delivery;
- use app\utils\Export;
- use app\utils\ExportList;
- use app\utils\QrCode;
- use app\utils\ShareQrcode;
- use app\utils\Tools;
- use yii\base\Model;
- use yii\db\Query;
- use yii\helpers\Json;
- use \app\models\AlipayAcitvityVoucherOrder;
- use app\models\AuthRole;
- use app\models\DeliveryMaiyatian;
- use app\models\DeliveryKeloop;
- use app\models\Worker;
- use app\models\WorkerGoodsExt;
- use app\models\WorkerOrderExt;
- use app\models\BookingOrderExt;
- use yii\helpers\VarDumper;
- class ReconciliationOrderForm extends Model
- {
- public $filters_is_pay;
- public $filters_order_types;
- public $sort;
- public $sort_type;
- public $selected_rows;
- public $selected_row_keys;
- public $name;
- public $export;
- public $store_id;
- public $user_id;
- public $keyword;
- public $status;
- public $status_ext;
- public $worker_name;
- public $worker_tel;
- public $page;
- public $limit;
- public $activity_type;
- public $md_name;
- public $order_no;
- public $goods_name;
- public $mobile;
- public $mobiles;
- public $openids;
- public $book_name;
- public $book_mobile;
- public $is_offline;
- public $clerk_id;
- public $parent_id;
- public $shop_id;
- public $order_type;
- public $pay_type;
- public $dateStart;
- public $dateEnd;
- public $express_type;
- public $keywordType;
- public $seller_comments;
- public $fields;
- public $type;
- public $mch_id;
- public $flag; // 是否导出
- public $supplier_id;
- public $supplier;
- public $user_name;
- public $salesman_id;
- public $province_id;
- public $city_id;
- public $district_id;
- public $order_origin;
- public $send_time;
- public $booking_time_start;
- public $booking_time_end;
- public $worker_id;
- public $admin;
- public $admin_id;
- public $change_admin_id;
- public $confirm_payment;
- /**
- * @var 所属平台
- */
- public $platform; //所属平台
- /**
- * @var bool 是否是配送员订单
- */
- public $is_delivery = false; //是否是配送员订单
- /**
- * @var bool 是否是配送员订单
- */
- public $is_same_city = false; //是否是配送员订单
- /**
- * @var 获取默写城市的订单 例如:["\u5357\u5b81","\u4e0a\u6d77"]
- */
- public $shop_list;
- public $order_id;
- public $is_wastore;
- //麦芽田
- public $maiyatian;
- public $maiyatian_delivery_status;
- public $maiyatian_order_status;
- public $maiyatian_logistic_tag;
- public $cat_id = 0;
- //快跑者
- public $keloop;
- public $keloop_delivery_status;
- public function rules()
- {
- return [
- [['keyword', 'flag', 'selected_row_keys'], 'trim'],
- [['status', 'page', 'limit', 'user_id', 'is_offline', 'clerk_id', 'shop_id', 'keywordType', 'platform', 'order_type', 'pay_type', 'salesman_id', 'order_origin', 'admin_id', 'change_admin_id'], 'integer'],
- [['status',], 'default', 'value' => -1],
- [['page',], 'default', 'value' => 1],
- [['dateStart', 'dateEnd', 'express_type', 'md_name', 'order_no', 'goods_name', 'mobile'], 'trim'],
- [['seller_comments', 'flag', 'user_name', 'send_time',], 'string'],
- [['export', 'activity_type', 'mobiles', 'openids'], 'safe'],
- [['fields', 'is_delivery', 'shop_list', 'book_name', 'book_mobile', 'name'], 'safe'],
- [['maiyatian', 'maiyatian_delivery_status', 'maiyatian_order_status', 'maiyatian_logistic_tag', 'province_id', 'city_id', 'district_id'], 'safe'],
- [['keloop', 'keloop_delivery_status'], 'safe'],
- [['status_ext', 'worker_name', 'worker_tel'], 'safe'],
- [['booking_time_start', 'booking_time_end', 'worker_id', 'is_wastore'], 'safe'],
- ['cat_id', 'integer'],
- ['cat_id', 'default', 'value' => 0]
- ];
- }
- public function search()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $addSelect = [];
- $query = Order::find()->alias('o')->where(['!=', 'o.order_type', Order::ORDER_TYPE_Adopt]);
- $query->andWhere(['o.store_id' => $this->store_id]);
- if (!empty($this->selected_row_keys)) {
- $query->andWhere(['in', 'o.id', $this->selected_row_keys]);
- }
- if ($this->order_type == Order::ORDER_TYPE_WORKER) {
- $query->leftJoin(['woe' => WorkerOrderExt::tableName()], 'o.id = woe.order_id');
- $query->leftJoin(['w' => Worker::tableName()], 'w.id = woe.worker_id');
- if ($this->status_ext > -1) {
- $this->status = -1;
- $query->andWhere([
- 'woe.status_ext' => $this->status_ext,
- ]);
- }
- if ($this->worker_name) {
- $query->andWhere(['like', 'w.name', $this->worker_name]);
- }
- if ($this->worker_tel) {
- $query->andWhere(['like', 'w.tel', $this->worker_tel]);
- }
- } else {
- $query->andWhere(['!=', 'o.order_type', Order::ORDER_TYPE_WORKER]);
- }
- if ($this->order_type == 2) {
- $query->leftJoin(['boe' => BookingOrderExt::tableName()], 'o.id = boe.order_id');
- $addSelect = array_merge($addSelect, ['boe.worker_id', 'boe.worker_name', 'boe.status_ext', 'boe.time_sys_confirm', 'boe.user_revoke', 'boe.user_revoke_reason']);
- $addSelect = array_merge($addSelect, ['IF(boe.booking_time_end < "' . date('Y-m-d H:i:s') . '" AND boe.status_ext < ' . BookingOrderExt::STATUS_EXT_START . ', 1, 0) booking_time_out']);
- if ($this->booking_time_start) {
- $query->andWhere(['>=', 'boe.booking_time_end', $this->booking_time_start]);
- }
- if ($this->booking_time_end) {
- $query->andWhere(['<=', 'boe.booking_time_start', $this->booking_time_end]);
- }
- if (isset($this->worker_id)) {
- $query->andWhere(['boe.worker_id' => $this->worker_id]);
- }
- if ($this->status_ext == 500) {
- //已超时
- $query->andWhere(['and', ['<', 'boe.status_ext', BookingOrderExt::STATUS_EXT_START], ['<', 'boe.booking_time_end', date('Y-m-d H:i:s')]]);
- } else if ($this->status_ext == 600) {
- //未分配
- $query->andWhere(['boe.worker_id' => 0]);
- } else {
- isset($this->status_ext) && $this->status_ext > -1 && $query->andWhere(['boe.status_ext' => $this->status_ext]);
- }
- }
- if (get_md_id()) {
- $query->andWhere(['o.md_id' => get_md_id()]);
- }
- if (!empty($this->order_type) && in_array($this->order_type, [0, 1, 2, 3, Order::ORDER_TYPE_WORKER, Order::ORDER_TYPE_INTEGRAL])) {
- $query->andWhere([
- 'o.order_type' => $this->order_type
- ]);
- }
- if (!empty($this->pay_type) && in_array($this->pay_type, [0, 1, 2, 3, 4, 5, 6, 11])) {
- $query->andWhere([
- 'o.pay_type' => $this->pay_type
- ]);
- }
- $query->leftJoin(['u' => User::tableName()], 'u.id = o.user_id')
- ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id');
- switch ($this->status) {
- case 0:
- // 排除已取消的订单,未删除且未支付
- $query->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['o.is_delete' => Order::IS_DELETE_FALSE, 'o.is_pay' => Order::IS_PAY_FALSE]);
- break;
- case 1:
- // 未发货状态,未删除,已支付或货到付款
- $query->andWhere([
- 'o.trade_status' => Order::ORDER_FLOW_NO_SEND,
- 'o.is_delete' => Order::IS_DELETE_FALSE
- ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]);
- break;
- case 2:
- // 已发货状态,未删除,已支付或货到付款
- $query->andWhere([
- 'o.trade_status' => Order::ORDER_FLOW_SEND,
- 'o.is_delete' => Order::IS_DELETE_FALSE
- ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]);
- break;
- case 3:
- // 已确认状态,未删除,已支付或货到付款
- $query->andWhere([
- 'o.trade_status' => Order::ORDER_FLOW_CONFIRM,
- 'o.is_delete' => Order::IS_DELETE_FALSE
- ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]);
- break;
- default:
- // 如果$order_id`为空,只过滤未删除的订单
- if (empty($this->order_id)) {
- $query->andWhere([
- 'o.is_delete' => Order::IS_DELETE_FALSE
- ]);
- }
- break;
- }
- if ($this->order_id && $this->order_id > 0) {
- $query->andWhere(['o.id' => $this->order_id]);
- }
- if (!$this->order_id) {
- if ($this->md_name) {
- $query->leftJoin(['md' => Md::tableName()], 'md.id=o.md_id')->andWhere(['like', 'md.name', $this->md_name]);
- }
- if ($this->order_no) {
- $query->andWhere(['like', 'o.order_no', $this->order_no]);
- }
- if ($this->mobile) {
- $query->andWhere([
- 'or',
- ['like', 'o.mobile', $this->mobile],
- ['like', 'u.binding', $this->mobile],
- ]);
- }
- if ($this->goods_name) {
- $query->andWhere(['like', 'od.goods_name', $this->goods_name]);
- }
- if ($this->user_name) {
- $query->andWhere(['like', 'u.nickname', $this->user_name]);
- }
- }
- if ($this->dateStart) {
- $query->andWhere(['>=', 'o.created_at', strtotime($this->dateStart)]);
- }
- if ($this->dateEnd) {
- $query->andWhere(['<=', 'o.created_at', strtotime($this->dateEnd)]);
- }
- if ($this->flag == Export::EXPORT) {
- $query_ex = clone $query;
- $list_ex = $query_ex;
- $export = new ExportList();
- $export->is_offline = $this->is_offline;
- $export->is_delivery = $this->is_delivery;
- $export->order_type = 0;
- $export->fields = $this->fields;
- $export->dataTransform_new($list_ex);
- }
- if ($this->platform !== null && $this->platform !== '') {
- $query->andWhere([
- 'u.platform' => $this->platform
- ]);
- }
- if (!empty($this->filters_is_pay)) {
- $query->andWhere(['o.is_pay' => $this->filters_is_pay]);
- }
- if (!empty($this->filters_order_types)) {
- foreach ($this->filters_order_types as $key => $value) {
- if ($value == 0) {
- $query->andWhere(['o.is_offline' => 0]);
- }
- if ($value == 1) {
- $query->andWhere(['o.is_offline' => 1]);
- }
- if ($value == 2) {
- $query->andWhere(['o.is_delivery' => 1]);
- }
- if ($value == 3) {
- $query->andWhere(['o.order_origin' => 4]);
- }
- }
- }
- $query->leftJoin(['a' => Admin::tableName()], 'a.id= o.admin_id ');
- $refundQuery1 = OrderRefund::find()->alias('or')
- ->select('or.status, or.order_id, or.created_at')
- ->where(['or.store_id' => $this->store_id, 'or.is_delete' => 0]);
- $refundQuery = (new Query())->from(['or' => $refundQuery1])->where('`or`.order_id=o.id')
- ->select('or.status')
- ->orderBy(['or.created_at' => SORT_DESC])
- ->limit(1);
- if ($this->mobiles || $this->openids) {
- if ($this->mobiles) {
- $_where = [
- 'or',
- ['o.mobile' => $this->mobiles],
- ['u.binding' => $this->mobiles],
- ];
- if ($this->openids) {
- $_where[] = [
- 'u.wechat_open_id' => $this->openids,
- ];
- }
- } else {
- $_where = [
- 'u.wechat_open_id' => $this->openids,
- ];
- }
- $query->andWhere($_where);
- }
- $query->orderBy('o.id DESC')
- ->select([
- 'o.*', 'u.nickname as de_name', 'u.platform', 'u.binding', 'u.avatar_url',
- 'refund' => $refundQuery, 'od.delivery_type', 'od.attr', 'o.name user_name', 'o.mobile user_mobile', 'o.address user_address', 'od.order_transit_id', 'a.name admin_name'
- ]);
- $query->addSelect($addSelect);
- $query->groupBy('o.id');
- if (!empty($this->sort) && !empty($this->sort_type)) {
- if ($this->sort_type == 'id') {
- $query->orderBy(['o.id' => $this->sort == 'asc' ? SORT_ASC : SORT_DESC,]);
- }
- if ($this->sort_type == 'sort') {
- $query->orderBy(['o.sort' => $this->sort == 'asc' ? SORT_ASC : SORT_DESC,]);
- }
- if ($this->sort_type == 'total_price') {
- $query->orderBy(['o.total_price' => $this->sort == 'asc' ? SORT_ASC : SORT_DESC,]);
- }
- if ($this->sort_type == 'pay_price') {
- $query->orderBy(['o.pay_price' => $this->sort == 'asc' ? SORT_ASC : SORT_DESC,]);
- }
- if ($this->sort_type == 'pay_datetime') {
- $query->orderBy(['o.pay_time' => $this->sort == 'asc' ? SORT_ASC : SORT_DESC,]);
- }
- } else {
- $query->orderBy(['o.id' => SORT_DESC]);
- }
- $sumQuery = clone $query;
- $sumPayPrice = $sumQuery->sum('pay_price');
- $sumDailyQuery = clone $query;
- $sumDailyQuery->andWhere([
- 'and',
- ['>=', 'o.created_at', strtotime(date('Y-m-d 00:00:00'))],
- ['<=', 'o.created_at', strtotime(date('Y-m-d 23:59:59'))],
- ]);
- $sumDaily = $sumDailyQuery->sum('pay_price');
- $pagination = pagination_make($query);
- $listArray = $pagination['list'];
- foreach ($listArray as $i => &$item) {
- if ($item['order_origin'] == 4) {
- $item['order_types'] = 3;
- } else if ($item['is_delivery'] == 1) {
- $item['order_types'] = 2;
- } else if ($item['is_offline'] == 1) {
- $item['order_types'] = 1;
- } else if ($item['is_offline'] == 0) {
- $item['order_types'] = 0;
- }
- $item['pay_datetime'] = date('Y-m-d H:i:s', $item['created_at']);;
- $item['goods_list'] = $this->getOrderGoodsList($item['id']);
- $item['avatar'] = '';
- if (empty($item['binding'])) {
- $item['nickname'] = $item['de_name'];
- $item['avatar'] = $item['avatar_url'];
- } else {
- $sass_user = SaasUser::findOne(['mobile' => $item['binding']]);
- if ($sass_user) {
- $item['nickname'] = $sass_user->name;
- $item['avatar'] = $sass_user->avatar;
- }
- }
- if ($item['order_type'] == 2) {
- $tmp_attr = Json::decode($item['attr']);
- $item['book']['date'] = $tmp_attr['date'] . ' ' . $tmp_attr['time'];
- }
- $rmList = [];
- if ($this->order_id && $this->order_id > 0) {
- $rmQuery = \app\models\OrderRefundMoney::find()->where(['is_delete' => 0, 'order_id' => $this->order_id]);
- $rmList = $rmQuery->orderBy('id DESC')->asArray()->all();
- foreach ($rmList as &$k) {
- $k['created_at'] = date('Y-m-d H:i:s', $k['created_at']);
- }
- }
- $item['rmList'] = $rmList;
- if (!empty($item['food_flag_id'])) {
- $food_flag = FoodFlag::findOne($item['food_flag_id']);
- $item['food_table_num'] = $food_flag->table_num;
- }
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['arrival_time_text'] = date('Y-m-d H:i:s', $item['arrival_time']);
- $item['goods_list'] = $this->getOrderGoodsList($item['id']);
- $item['clerk_name'] = '';
- $shop = Shop::findOne($item['shop_id']);
- if ($shop) {
- $user = User::findOne(Shop::findOne($item['shop_id'])->user_id);
- if ($user) {
- $item['clerk_name'] = $user->nickname;
- }
- }
- $item['integral'] = json_decode($item['integral'], true);
- if (isset($item['address_data'])) {
- $item['address_data'] = json_decode($item['address_data']);
- }
- $item['flag'] = 0;
- $item['delivery_info'] = [];
- // 同城配送配送信息
- if ($item['delivery_type'] == OrderDetail::GOODS_DELIVERY_IM) {
- $delivery_info = DeliveryInfo::findOne(['order_no' => $item['order_no']]);
- $item['delivery_info'] = [
- 'waybill_id' => $delivery_info->waybill_id,
- 'status' => Delivery::$validOrderStatus[$delivery_info->status],
- 'reason' => $delivery_info->reason_id > 0 ? Delivery::$validReason[$delivery_info->reason_id] : '',
- 'fee' => $delivery_info->fee,
- 'duct_fee' => $delivery_info->deduct_fee,
- 'delivery_type' => Delivery::$deliveryType[$delivery_info->delivery_type],
- 'rider_name' => $delivery_info->rider_name,
- 'rider_mobile' => $delivery_info->rider_mobile
- ];
- }
- if ($item['delivery_info']) {
- $item['pay_price'] > 0 && $item['express_price'] += $item['delivery_info']['fee'];
- }
- $item['md_info'] = null;
- if ($item['md_id']) {
- $item['md_info'] = Md::find()->where(['id' => $item['md_id']])
- ->select('id, name, address, cover_url, start_time, end_time, mobile, open_status, shop_time_type, is_single')->one();
- }
- $item['pay_user_info'] = null;
- if ($item['pay_user_id'] > 0) {
- $pay_saas_user = SaasUser::findOne(['mobile' => User::findOne($item['pay_user_id'])->binding]);
- $item['pay_user_info'] = [
- 'name' => $pay_saas_user->name,
- 'mobile' => $pay_saas_user->mobile
- ];
- }
- $item['all_discount'] = sprintf("%.2f", ($item['integral']['forehead'] + $item['coupon_sub_price']));
- $item['alipay_activity_voucher'] = AlipayAcitvityVoucherOrder::getByOrder($item['alipay_trade_no']);
- if ($item['store_id']) {
- // 增加查询商城的联系电话
- $item['store'] = Store::find()->where(['id' => $item['store_id']])->select('id, name, province_id, city_id, district_id, address,contact_tel')->one();
- }
- $diy_shop_name = Option::get(OptionSetting::DIY_SHOP_NAME, $item['store_id'], 'store', '自提配送')['value'] ?: '自提配送';
- $diy_shop_name = Option::get(OptionSetting::DIY_SHOP_NAME, $item['store_id'], 'pay', $diy_shop_name)['value'];
- $diy_express_name = Option::get(OptionSetting::DIY_EXPRESS_NAME, $item['store_id'], 'store', '快递配送')['value'] ?: '快递配送';
- $diy_express_name = Option::get(OptionSetting::DIY_EXPRESS_NAME, $item['store_id'], 'pay', $diy_express_name)['value'];
- $diy_delivery_name = Option::get(OptionSetting::DIY_DELIVERY_NAME, $item['store_id'], 'store', '同城配送')['value'] ?: '同城配送';
- $diy_delivery_name = Option::get(OptionSetting::DIY_DELIVERY_NAME, $item['store_id'], 'pay', $diy_delivery_name)['value'];
- $item['diy_express_name'] = intval($item['is_offline']) === 1 ? $diy_shop_name : (intval($item['is_delivery']) === 1 ? $diy_delivery_name : $diy_express_name);
- if (empty($item['address']) && (int)$item['is_offline']) {
- if (!empty($item['store'])) {
- $item['address'] = '配送到门店:' . $item['store']['name'];
- }
- if (!empty($item['md_info'])) {
- $item['address'] = '配送到门店:' . $item['md_info']['name'];
- }
- }
- $integral = 0;
- if ((int)$item['is_pay'] !== 0) {
- foreach ($item['goods_list'] as $goodskey => &$goods) {
- if ($goods['goods_id'] && $goods['attr']) {
- $cards = Goods::find()->where(['id' => $goods['goods_id']])->select('verify_card_id, integral')->one();
- if (!empty($cards->integral)) {
- $goods_integral = json_decode($cards->integral, true);
- $integral += (int)$goods_integral['give'];
- }
- $cloudGoodsInfo = CloudGoodsBind::find()->where(['goods_id' => $goods['goods_id'], 'is_delete' => 0])->one();
- if ($cloudGoodsInfo && isset($cloudGoodsInfo['cloud_goods_id'])) {
- $cloudInfo = [];
- $cloudInfo['cloud_goods_id'] = $cloudGoodsInfo['cloud_goods_id'];
- $cloudInfo['cloud_attr_id'] = $cloudGoodsInfo['cloud_attr_id'];
- $cloudInfo['cloud_supplier_id'] = $cloudGoodsInfo['cloud_supplier_id'];
- if ($cloudGoodsInfo['cloud_supplier_id'] > 0) {
- $supplierInfo = Supplier::find()->select('name,logo')->where(['cloud_supplier_id' => $cloudGoodsInfo['cloud_supplier_id'], 'is_delete' => 0])->one();
- if ($supplierInfo && $supplierInfo['name'] && $supplierInfo['logo']) {
- $cloudInfo['cloud_supplier_name'] = $supplierInfo['name'];
- $cloudInfo['cloud_supplier_logo'] = $supplierInfo['logo'];
- }
- }
- $listArray[$i]['goods_list'][$goodskey]['cloud_bind'] = json_encode($cloudInfo);
- $listArray[$i]['is_cloud_bind'] = (int)$item['trade_status'] === 0;
- }
- }
- }
- $item['integral'] = $integral;
- }
- if ($item['order_type'] == Order::ORDER_TYPE_WORKER) {
- $woe = WorkerOrderExt::findOne(['order_id' => $item['id']]);
- $item['woe'] = $woe;
- $item['w'] = $woe ? Worker::findOne($woe['worker_id']) : null;
- foreach ($item['goods_list'] as &$good) {
- $wge = WorkerGoodsExt::findOne(['goods_id' => $good['goods_id']]);
- $good['wge'] = $wge;
- }
- }
- if ($item['pt_order_id']) {
- $pt = \app\models\PtActivityOrder::findOne($item['pt_order_id']);
- if ($pt->pt_number) {
- $item['pt_order_id'] = $pt->pt_number;
- }
- }
- if ($item['confirm_time'] && !$item['send_time']) {
- $item['send_time'] = $item['confirm_time'];
- }
- }
- $adminPermission = AdminRole::find()->alias('ar')
- ->leftJoin(['ad' => AuthRole::tableName()], 'ad.id=ar.role_id')
- ->select(['ad.edit_data', 'ar.admin_id'])
- ->asArray()
- ->all();
- $has_admin = false;
- foreach ($adminPermission as $permission) {
- $data = json_decode($permission['edit_data'], true);
- if (in_array('PaymentStatement', (array)$data)) {
- $admins = Admin::find()
- ->where([
- 'or',
- ['id' => $permission['admin_id']],
- ['and', ['type' => ['admin', 'staff']], ['is_delete' => 0]]
- ])
- ->all();
- foreach ($admins as $admin) {
- if ($admin->id == $this->admin->id) {
- $has_admin = true;
- break;
- }
- }
- }
- }
- // 获取可导出数据
- $f = new ExportList();
- $exportList = $f->getList();
- if ($this->export) {
- return $this->export($listArray);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'q' => $query->createCommand()->getRawSql(),
- 'totalSum' => $sumPayPrice,
- 'has_admin' => $has_admin,
- 'admins' => $admins ?? [],
- 'admin' => $this->admin,
- 'sumDaily' => $sumDaily,
- 'export_list' => $exportList,
- 'data' => $listArray,
- 'pageNo' => $pagination['pageNo'],
- 'pageSize' => $pagination['pageSize'],
- 'totalCount' => $pagination['totalCount'],
- 'print_setting' => Option::getPrintOrderSetting()
- ],
- ];
- }
- private function export($list)
- {
- $rows = [[
- 'ID',
- '排单',
- '会员信息',
- '订单号',
- '订单状态',
- '总金额(元)',
- '运费',
- '实际支付(元)',
- '是否已付款',
- '收货人',
- '收货人电话',
- '收货人地址',
- '下单门店',
- '商品',
- '规格',
- '数量',
- '成本价',
- '售价',
- '供货商信息',
- '备注',
- '时间',
- ]];
- usort($list, function ($a, $b) {
- if ($a['sort'] == 0 && $b['sort'] == 0) {
- return 0;
- }
- if ($a['sort'] == 0) {
- return 1;
- }
- if ($b['sort'] == 0) {
- return -1;
- }
- return $a['sort'] <=> $b['sort'];
- });
- foreach ($list as $item) {
- $tradStatus = '';
- if ($item['order_type'] == 1) {
- if (in_array($item['trade_status'], [0, 2])) {
- $tradStatus = '待使用';
- }
- }
- if ($item['trade_status'] == 1) {
- $tradStatus = '已取消';
- }
- if ($item['is_pay'] == 0) {
- $tradStatus = '未支付';
- }
- if ($item['is_pay'] == 1 && $item['trade_status'] == 0) {
- $tradStatus = '待发货';
- }
- if ($item['is_pay'] == 1 && $item['trade_status'] == 2) {
- $tradStatus = '待收货';
- }
- if ($item['is_pay'] == 1 && $item['trade_status'] == 3) {
- $tradStatus = '已完成';
- }
- $goods = [];
- $supplier_info = '';
- $cost_price = 0;
- foreach ($item['goods_list'] as $i => $gitem) {
- $goods_info = json_decode($gitem['goods_info'], true);
- $goods_info || $goods_info = Goods::findOne($gitem['goods_id']);
- $supplier = Supplier::findOne(['cloud_supplier_id' => $goods_info['cloud_supplier_id']]);
- $supplier_info = $supplier->supplier_name;
- $attr = [];
- $order_attr = array_column($gitem['attr_list'], 'attr_id');
- sort($order_attr);
- $goods_attr = json_decode($goods_info['attr'], true);
- $attr_cost_price = '0.00';
- $attr_goods_price = '0.00';
- foreach ($goods_attr as $value) {
- $goods_attr_id = array_column($value['attr_list'], 'attr_id');
- sort($goods_attr_id);
- if (!array_diff($goods_attr_id, $order_attr)) {
- $attr_cost_price = $value['cost_price'] ?? $value['price'];
- $attr_goods_price = $value['price'] ?: 0.00;
- }
- }
- $attr_cost_price = $attr_cost_price <= 0 ? '暂无数据' : sprintf('%.2f', $attr_cost_price);
- $attr_goods_price = $attr_goods_price <= 0 ? '暂无数据' : sprintf('%.2f', $attr_goods_price);
- $cost_price = sprintf('%.2f', (doubleval($cost_price) + doubleval($attr_cost_price)));
- foreach ($gitem['attr_list'] as $atitem) {
- $attr[] = isset($atitem['attr_name']) ? $atitem['attr_name'] . isset($atitem['cost_price']) : '';
- }
- $goods[] = $gitem['name'] . '(' . implode(',', $attr) . ')' . '(' . $gitem['num'] . $gitem['unit'] . ')' . '(' . $gitem['total_price'] . '元)(成本价:' . $attr_cost_price . ')';
- $dateString = date('Y-m-d H:i:s', $item['created_at']);
- if ($i == 0) {
- $r = [
- $item['id'],
- $item['sort'],
- $item['nickname'],
- $item['order_no'],
- $tradStatus,
- $item['total_price'],
- $item['express_price'],
- $item['pay_price'],
- $item['is_pay'] ? '已付款' : '未付款',
- $item['name'],
- $item['mobile'],
- $item['address'],
- (($item['md_info'] && $item['md_info']['name']) ? $item['md_info']['name'] : '') . '-' . $item['store']['name'],
- $gitem['name'],
- implode(',', $attr),
- $gitem['num'] . $gitem['unit'],
- $attr_cost_price,
- $attr_goods_price,
- $supplier_info,
- $item['shop_remark'],
- $item['created_at'],
- ];
- $rows[] = $r;
- } else {
- $r = [
- '',
- '',
- '',
- '',
- '',
- '',
- '',
- '',
- '',
- '',
- '',
- '',
- '',
- '',
- '',
- '',
- $gitem['name'],
- implode(',', $attr),
- $gitem['num'] . $gitem['unit'],
- $attr_cost_price,
- $attr_goods_price,
- $supplier_info,
- '',
- ];
- $rows[] = $r;
- }
- }
- }
- $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
- ->addRows($rows)->toBrowser();
- }
- public function getOrderGoodsList($order_id)
- {
- $orderDetailList = OrderDetail::find()->alias('od')
- ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
- ->where([
- 'od.is_delete' => 0,
- 'od.order_id' => $order_id,
- ])->select(['od.num', 'od.total_price', 'g.goods_no', 'od.attr', 'od.food_ext_goods', 'od.is_level', 'od.batch_price_tips', 'name' => 'od.goods_name', 'od.pic', 'od.goods_info', 'od.delivery_type', 'od.shop_id', 'od.goods_id', 'g.attr attrs'])->asArray()->all();
- foreach ($orderDetailList as &$item) {
- $item['form'] = OrderForm::findAll(['goods_id' => $item['goods_id'], 'order_id' => $order_id]);
- $item['food_ext_goods'] = (array)json_decode($item['food_ext_goods'], true);
- $item['attr_list'] = json_decode($item['attr'], true);
- $item['unit'] = Json::decode($item['goods_info'])['unit'];
- $item['price'] = sprintf('%.2f', ($item['total_price'] / $item['num']));
- $oattr = json_decode($item['attr'], true);
- $oattrIds = array_column($oattr, 'attr_id');
- sort($oattrIds);
- $gattr = json_decode($item['goods_info'], true)['attr'];
- $gattr = json_decode($gattr, true);
- $goods_no = '';
- foreach ($gattr as $igattr) {
- $attrIds = array_column($igattr['attr_list'], 'attr_id');
- sort($attrIds);
- if (implode('_', $attrIds) === implode('_', $oattrIds)) {
- $goods_no = $igattr['no'];
- }
- }
- $item['goods_no'] = trim($goods_no ?: $item['goods_no']);
- $item['code_path'] = '';
- if ($item['goods_no']) {
- $generator = new BarcodeGeneratorJPG();
- $barcode = $generator->getBarcode($item['goods_no'], $generator::TYPE_CODE_128, 2, 30);
- $filename = md5('goods_no_' . $item['goods_no']);
- $code_path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
- $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
- file_put_contents($code_path, $barcode);
- $item['code_path'] = $pic_url;
- }
- $item['shop'] = [];
- if ($item['delivery_type'] == OrderDetail::GOODS_DELIVERY_SHOP) {
- $shop_info = Shop::find()->where(['id' => $item['shop_id']])->asArray()->one();
- if (!$shop_info) continue;
- $item['shop']['address'] = $shop_info['address'];
- $item['shop']['id'] = $shop_info['id'];
- $item['shop']['name'] = $shop_info['name'];
- $item['shop']['mobile'] = $shop_info['mobile'];
- }
- }
- return $orderDetailList;
- }
- public function confirmPayment()
- {
- if (!empty($this->confirm_payment) && !empty($this->change_admin_id)) {
- Order::updateAll(['confirm_payment' => 1, 'is_pay' => 1, 'admin_id' => $this->change_admin_id], ['id' => $this->confirm_payment]);
- }
- $admin = Admin::findOne($this->change_admin_id);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'admin_name' => $admin->name
- ],
- ];
- }
- }
|