| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786 |
- <?php
- namespace addons\Community\common\service;
- use addons\Community\common\enums\CommunityEnum;
- use addons\Community\common\helper\CommunityHelper;
- use addons\Community\common\models\CommunityGoodsSetting;
- use addons\Community\common\models\CommunityHead;
- use addons\Community\common\models\CommunityHeadCommission;
- use addons\Community\common\models\CommunityHeadOrder;
- use addons\SmartForm\common\service\SmsNoticeService;
- use common\enums\AddonsEnum;
- use common\enums\ClerkEnum;
- use common\enums\GlobalFuncEnum;
- use common\enums\MarketingTypeEnum;
- use common\enums\OrderSourceEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\PayTypeEnum;
- use common\enums\ShippingTypeEnum;
- use common\enums\StatusEnum;
- use common\globals\GlobalInvoke;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\order\OrderMarketing;
- use common\models\user\User;
- use Yii;
- use yii\helpers\Json;
- class OrderService
- {
- /**
- * 订单列表 - 后台
- * @param int $mall_id
- * @param array $params
- * @return array|false|mixed|string|null
- */
- public function page(int $mall_id, array $params)
- {
- // 组织条件
- $user_ids = [];
- if (!empty($params["user_id"])) {
- $user_ids[] = $params["user_id"];
- }
- if (!empty($params["member"])) {
- $user_or[] = 'or';
- $user_or[] = ['like', 'mobile', $params['member']];
- $user_or[] = ['like', 'nickname', $params['member']];
- $u_ids = User::find()->where(['mall_id' => $mall_id])->andWhere($user_or)->column();
- if (!empty($u_ids)) {
- $user_ids = array_merge($user_ids, $u_ids);
- }
- }
- $where = [
- ['mall_id' => $mall_id]
- ];
- // 卖家查询
- if (!empty($params['head_member'])) {
- $head_member_or[] = 'or';
- $head_member_or[] = ['like', 'mobile', $params['head_member']];
- $head_member_or[] = ['like', 'nickname', $params['head_member']];
- $u_ids = User::find()->where(['mall_id' => $mall_id])->andWhere($head_member_or)->column();
- if (!empty($u_ids)) {
- $where[] = ['in', 'user_id', $u_ids];
- }
- }
- if (!empty($params['status'])) {
- $where[] = ['apply_status' => $params['status']];
- }
- // 团长检索
- if (!empty($params['name'])) {
- $h_ids = CommunityHead::find()->where(['mall_id' => $mall_id])->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['like', 'name', trim($params['name'])])->select('id')->column();
- if (!empty($h_ids)) $where[] = ['in', 'head_id', $h_ids];
- }
- if (!empty($params['date_start']) && !empty($params['date_end'])) {
- $s = strtotime($params['date_start']);
- $e = strtotime($params['date_end']);
- $where[] = ['between', 'created_at', $s, $e];
- } elseif (!empty($params['date_start'])) {
- $s = strtotime($params['date_start']);
- $where[] = ['<=', 'created_at', $s];
- } elseif (!empty($params['date_end'])) {
- $e = strtotime($params['date_end']);
- $where[] = ['>=', 'created_at', $e];
- }
- // 买家查询
- if (!empty($user_ids)) $where[] = ['in', 'buyer_user_id', $user_ids];
- if (isset($params['order_status']) && is_numeric($params['order_status'])) {
- $where[] = [
- 'order_id' => Order::find()
- ->where([
- 'id' => CommunityHeadOrder::find()
- ->where([
- 'status' => StatusEnum::ENABLED,
- 'mall_id' => $mall_id
- ])
- ->select('order_id')
- ->column(),
- 'order_status' => $params['order_status'],
- 'mall_id' => $mall_id,
- 'status' => StatusEnum::ENABLED
- ])
- ->select('id')
- ->column()
- ];
- }
- $map['where'] = $where;
- $map['with'] = [
- 'head' => function($query) {
- $query->select('id, name, logo, contacts, mobile');
- },
- 'order'=> function($query) {
- $query->with('detail');
- },
- 'headUser', 'buyer'
- ];
- $map['order'] = 'id desc';
- $map["limit"] = $params["limit"];
- $map["page"] = $params["page"];
- $ret = CommunityHeadOrder::listPage($map);
- if (!empty(CommunityHeadOrder::$static_error)) return CommunityHeadOrder::$static_error;
- if (!empty($ret['list'])) {
- foreach ($ret['list'] as &$item) {
- if ($item['order']['consign_time'] > 0) { // 只有已经发货的才有预计提货时间
- $item['expected_receive_at'] = CommunityHelper::getPickUpAt($item['mall_id'], $item['order']['consign_time']);
- } else {
- $item['expected_receive_at'] = '-';
- }
- }
- }
- return $ret;
- }
- /**
- * 创建订单
- * @param Order $order
- * @param int $head_id
- * @return bool
- */
- public function create(Order $order, int $head_id)
- {
- $head_user_id = CommunityHead::find()->where(['mall_id' => $order->mall_id])
- ->andWhere(['id' => $head_id])->select('user_id')->scalar();
- if (!$head_user_id) {
- \Yii::error("同步订单 {$order->id} 至自提点 {$head_id} 获取不到团长ID");
- return false;
- }
- $model = new CommunityHeadOrder();
- $model->mall_id = $order->mall_id;
- $model->buyer_user_id = $order->user_id;
- $model->head_id = $head_id;
- $model->user_id = $head_user_id;
- $model->order_id = $order->id;
- $model->order_no = $order->order_no;
- $model->order_status = OrderStatusEnum::NOT_PAY;
- if (!$model->save()) {
- \Yii::error("同步订单 {$order->id} 至自提点 {$head_id} 失败 {$model->getErrorMessage()}");
- return false;
- }
- return true;
- }
- /**
- * @param Order $order
- * @param int $status
- * @param \Closure|null $callback
- * @return bool
- */
- public function changeStatus(Order $order, int $status, \Closure $callback = null)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $data = CommunityHeadOrder::getOne([
- ['order_id' => $order->id],
- ['mall_id' => $order->mall_id]
- ]);
- if (empty($data)) {
- return false;
- }
- // $status = $status == OrderStatusEnum::SHIPMENTS ? OrderStatusEnum::TAKE : $status;
- $data->order_status = $status;
- if (!$data->save()) {
- \Yii::error("同步订单 {$order->id} 状态 {$status} 至自提点 {$data->head_id} 失败 {$data->getErrorMessage()}");
- throw new \Exception($data->getErrorMessage());
- }
- if ($status == OrderStatusEnum::TAKE) {
- // 更新订单状态 - 需要将平台订单状态修改为 6 ?等待用户自提 - 如果长时间没消费怎么办?
- $order->order_status = $status;
- if (!$order->save()) {
- \Yii::error("同步订单 更新订单 {$order->id} 状态 {$status} 至自提点 {$data->head_id} 失败 {$data->getErrorMessage()}");
- throw new \Exception($order->getErrorMessage());
- }
- // 订单详情
- if (!OrderDetail::updateAll(['order_status' => $status], ['order_status' => OrderStatusEnum::SHIPMENTS, 'order_id' => $order->id])) {
- \Yii::error("同步订单 更新 orderDetail {$order->id} 状态 {$status} 至自提点 {$data->head_id} 失败 {$data->getErrorMessage()}");
- throw new \Exception(OrderDetail::getStaticError());
- }
- }
- // 闭包处理
- if (!empty($callback)) {
- $callback->call($this);
- }
- $t->commit();
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- \Yii::error(Json::encode([
- 'file' => $e->getFile(),
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage()
- ]));
- return false;
- }
- }
- public function frontPage(int $mall_id, array $params, array $mall, string $platform, int $user_id)
- {
- // 通过用户ID去取数据
- $head_where = [
- ['mall_id' => $mall_id],
- ['user_id' => $user_id],
- ['status' => StatusEnum::ENABLED]
- ];
- if (isset($params['order_status']) && $params['order_status'] != 999) {
- if ($params['order_status'] == OrderStatusEnum::SING) { // 如果筛选的是已收货的订单那么就将已提货的订单一起取出来
- $params['order_status'] = [OrderStatusEnum::SING, OrderStatusEnum::TAKE_FINISH];
- }
- $head_where[] = ['order_status' => $params['order_status']];
- } else {
- if (!empty($params['wait_pending'])) {
- $head_where[] = ['not in', 'order_status', [OrderStatusEnum::ACCOMPLISH, OrderStatusEnum::REPEAL,
- OrderStatusEnum::TAKE_FINISH, OrderStatusEnum::SING]]; // 排除 已完成, 已关闭, 已自提, 已签收
- }
- }
- if (!empty($params['keyword'])) {
- $u_ids = User::find()->where(['mall_id' => $mall_id])
- ->andWhere(['or', ['like', 'mobile', $params['keyword']], ['like', 'nickname', $params['keyword']]])
- ->select('id')
- ->column();
- if (!empty($u_ids)) {
- $head_where[] = ['buyer_user_id' => $u_ids];
- } else {
- $head_where[] = ['buyer_user_id' => 0];
- }
- }
- $map['where'] = $head_where;
- $map['select'] = 'order_id';
- $map['limit'] = $params['limit'] ?? 5;
- $map['page'] = $params['page'] ?? 1;
- $map['order'] = 'id desc';
- $ret = CommunityHeadOrder::listPage($map);
- if (!empty($ret['list'])) {
- $order_ids = array_column($ret['list'], 'order_id');
- $where[] = ['in', 'id', $order_ids];
- $where[] = ['is_feedback' => OrderStatusEnum::NOT_FEEDBACK];
- $where[] = ['!=', 'order_source', OrderSourceEnum::SOURCE_SELF_MALL];
- if ($params['shipping_type']) {// 判断订单配送类型
- $where[] = ['shipping_type' => $params['shipping_type']];
- }
- // $select = 'id,pay_money,order_status,shipping_type,is_recycle,marketing_type,extra_info,order_source, consign_time';
- // $params['order'] = 'id desc';
- // if(!empty($params['keyword'])){
- // $goods_list = Goods::lists(['where'=>[
- // ['mall_id'=>$mall_id],
- // ['like','goods_name',trim($params['keyword']).'%',false]
- // ],'select'=>'id']);
- //
- // $goods_ids = array_column($goods_list,'id');
- // $order_detail_list = OrderDetail::lists(['where'=>[
- // ['mall_id'=>$mall_id],
- // ['in','goods_id',$goods_ids]
- // ],'select'=>'order_id']);
- // $order_ids = array_column($order_detail_list,'order_id');
- // $where[]=[
- // 'or',
- // ['id'=>$order_ids],
- // ['like','order_no',trim($params['keyword']).'%',false]
- // ];
- // }
- $select = 'id,pay_money,order_status,shipping_type,is_recycle,marketing_type,extra_info,order_source,store_id,mch_id,receiver_name,mobile, consign_time, mall_id';
- $params['order'] = 'pay_time desc';
- $params['select'] = $select;
- $params['where'] = $where;
- $params['with'] = ['detail' => function ($query) {
- $query->select('id,order_id,goods_id,goods_name,pic_url,goods_attr_id,num,price,is_feedback,sign_id,goods_attr_name,extra_info,adjust_money');
- }];
- $params['limit'] = 0;
- // $params['page'] = 0;
- $order_list = Order::lists($params);
- $ret['mall_name'] = $mall['name'];
- $ret['mall_logo'] = Yii::$app->services->setting->mall->get($mall_id, 'basic.logo');
- if (!empty($order_list)) {
- foreach ($order_list as $key => $val) {
- array_map(function ($arg) use (&$val) {
- if (!isset($val['num'])) $val['num'] = 0;
- $val['num'] += $arg['num'];
- }, $val['detail']);
- // 剩余库存
- $stock = [];
- if($val['detail']){
- foreach ($val['detail'] as $k => $v){
- $order_list[$key]['detail'][$k]['price'] = $order_list[$key]['detail'][$k]['price'] + $order_list[$key]['detail'][$k]['adjust_money'];
- $order_list[$key]['detail'][$k]['price'] = round($order_list[$key]['detail'][$k]['price'],2);
- $goods_unit = Goods::getOne([['=','id',$v['goods_id']]],false,'','','unit')['unit'];
- $val['detail'][$k]['unit'] = $goods_unit;
- // 当订单关闭时可以直接再次购买
- if ($val['order_status'] == OrderStatusEnum::REPEAL) {
- // 查询商品是否有货
- $stock[] = [
- 'num' => $v['num'],
- 'store_id' => $v['store_id']??0,
- 'goods_id' => $v['goods_id'],
- 'attr_id' => $v['goods_attr_id'],
- 'stock' => GoodsAttr::find()->where(['id' => $v['goods_attr_id']])->select('stock')->scalar(),
- ];
- }
- }
- }
- // 获取支付金额
- $order_list[$key] = GlobalInvoke::exec(GlobalInvoke::ORDER_DETAIL, $mall_id, [
- 'order' => $val,
- 'mall_id' => $mall_id,
- ]) ?: $val;
- $order_list[$key]['mall_name'] = $mall['name'];
- $order_list[$key]['mall_logo'] = Yii::$app->services->setting->mall->get($mall_id, 'basic.logo');
- // 判断是否可以再次购买
- $is_agent = empty($stock) ? 0 : 1;
- foreach ($stock as $v) {
- // 当库存小于购买数量 不可再次购买
- if ($v['stock'] < $v['num']) {
- $is_agent = 0;
- }
- }
- $order_list[$key]['is_agent'] = $is_agent;
- $order_list[$key]['is_agent_data'] = $is_agent ? $stock : [];
- // 计算预计取货时间
- $order_list[$key]['expected_receive_at'] = "";
- if (!empty($val['consign_time']) && $val['order_status'] == OrderStatusEnum::SHIPMENTS) {
- $order_list[$key]['expected_receive_at'] = CommunityHelper::getPickUpAt($mall_id, $val['consign_time']);
- }
- }
- $order_list = GlobalInvoke::exec(GlobalFuncEnum::ORDER_LIST, $mall_id, [
- 'order' => $order_list, 'mall_id' => $mall_id
- ]) ?: $order_list;
- $ret['shipping_type_text'] = ShippingTypeEnum::getMap();
- $ret['order_status_text'] = OrderStatusEnum::getMap();
- $ret['order_status_text'][OrderStatusEnum::SHIPMENTS] = '待取货';
- $ret['is_feedback_text'] = OrderStatusEnum::getFeedbackMap();
- }
- $ret['pay_type_list'] = \Yii::$app->services->pay->getEnablePayment($mall_id, $platform);
- $ret['marketing_type_map'] = MarketingTypeEnum::getMap();
- $ret['list'] = $order_list;
- }
- return $ret;
- }
- /**
- * 订单详情
- * @param int $mall_id
- * @param int|string $order_id
- * @param string $filed_key order_id | order_no
- * @param int $user_id
- * @param string $platform
- * @return array|mixed
- */
- public function detail(int $mall_id, $order_id, string $filed_key, int $user_id, string $platform)
- {
- $order_id = CommunityHeadOrder::find()
- ->where(['mall_id' => $mall_id])
- ->andWhere(['user_id' => $user_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere([$filed_key => $order_id])
- ->select('order_id')->scalar();
- if (empty($order_id)) return '订单不存在.';
- $select = 'id,mall_id,user_id,store_id,order_no,receiver_name,mobile,pay_money,shipping_money,original_goods_price,coupon_money,goods_price,order_status,pay_status,
- shipping_status,review_status,is_feedback,address,created_at,pay_time,region_name,remark,consign_time,finish_time,sign_time,shipping_type,payment_type,score,score_money,marketing_id,marketing_type,extra_info,order_source';
- $where = [];
- $where[] = ['mall_id' => $mall_id];
- $where[] = ['id' => $order_id];
- $order = Order::getOne($where, true, ['detail' => function ($query) {
- $query->select('id,order_id,goods_id,goods_name,pic_url,num,price,is_feedback,sign_id,goods_attr_name,order_status,adjust_money,extra_info');
- }], false, $select);
- if (empty($order)) return '该订单不存在';
- $marketing = OrderMarketing::getMarketingInfo($order);
- $order['order_status_text'] = OrderStatusEnum::getMap();
- $order['order_status_text'][OrderStatusEnum::SHIPMENTS] = $order['shipping_type'] == ShippingTypeEnum::MERCHANT_DISTRIBUTION ? '待配送' : '待取货';
- $order['is_feedback_text'] = OrderStatusEnum::getFeedbackMap();
- $order['shipping_type_text'] = ShippingTypeEnum::getMap();
- $order['payment_type_text'] = PayTypeEnum::getMap();
- $order['pay_type_list'] = \Yii::$app->services->pay->getEnablePayment($mall_id, $platform);
- $order['change_price'] = array_sum(array_column($order['detail'],'adjust_money'));//预留字段
- $order['marketing'] = $marketing;
- $order['marketing_type_map'] =MarketingTypeEnum::getMap();
- $order_setting = Yii::$app->services->setting->mall->get($mall_id, 'order');
- $order['payment_expire_time'] = $order_setting['payment_expire_time']??30;
- $order['auto_receiving_time'] = $order_setting['auto_receiving_time']??14;
- //查询商品单位
- $goods_ids = [];
- if(!empty($order['detail'])){
- foreach ($order['detail'] as $k => $v){
- $goods_ids[] = $v['goods_id'];
- $goods_unit = Goods::getOne([['=','id',$v['goods_id']]],false,'','','unit')['unit'];
- $order['detail'][$k]['unit'] = $goods_unit;
- }
- }
- // 获取订单详情的状态文字描述
- $order['status_text'] = OrderStatusEnum::getApiStatus($order['status'], $order['shipping_type']);
- $order['status'] == OrderStatusEnum::NOT_PAY && $order['status_text']['desc'] = $order['payment_expire_time'] . $order['status_text']['desc'];
- $order['status'] == OrderStatusEnum::SHIPMENTS && $order['status_text']['desc'] = $order['auto_receiving_time'] . $order['status_text']['desc'];
- $order['expected_receive_at'] = ""; // 预计取货时间
- $order['is_show_receipt_btn'] = StatusEnum::ENABLED; // 是否显示收货按钮
- $order['shipping_money'] = $order['shipping_money'] ?? 0;
- $config = Yii::$app->services->setting->addons->get($mall_id, CommunityEnum::ADDONS_NAME, 'basic');
- $is_enable_write_off = !empty($config['is_enable_write_off']) ? StatusEnum::ENABLED : StatusEnum::DISABLED; // 是否有开启核销
- if (!empty($is_enable_write_off) || $order['order_status'] != OrderStatusEnum::SHIPMENTS) { // 开启了核销
- $order['is_show_receipt_btn'] = StatusEnum::DISABLED;
- }
- if (!empty($order['consign_time']) && $order['order_status'] == OrderStatusEnum::SHIPMENTS) {
- $order['expected_receive_at'] = CommunityHelper::getPickUpAt($mall_id, $order['consign_time']);
- }
- // 判断是否有智能表单
- if (!empty($goods_ids) && MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_SMART_FORM)) {
- // 如果有安装
- $goods_ids = array_unique($goods_ids);
- $order['smart_form'] = SmsNoticeService::findByUserIdAndGoodsIds($order['user_id'], $goods_ids, $order['id']);
- }
- // 公共调用
- return GlobalInvoke::exec(GlobalInvoke::ORDER_DETAIL, $mall_id, [
- 'order' => $order,
- 'mall_id' => $mall_id,
- ]) ?: $order;
- }
- /**
- * 公共调用订单详情
- * @param $params
- * @return mixed
- */
- public function globalDetail($params)
- {
- // $params['order'] // 订单主体信息
- // 如果是站点自提
- if (in_array($params['order']['shipping_type'], [ShippingTypeEnum::PICKUP, ShippingTypeEnum::MERCHANT_DISTRIBUTION]) && !empty($params['order']['extra_info'])) {
- try {
- $ext = Json::decode($params['order']['extra_info']);
- } catch (\Exception $exception) {
- return $params['order'];
- }
- if (!empty($ext['head_id']) && !empty($params['order']['mall_id'])) { // 确定为站点自提
- // 返回 是否是社区团购自提订单
- if (!empty($params['order']['id'])) {
- $params['order']['is_community_order'] = (bool)CommunityHeadOrder::find()
- ->where(['order_id' => $params['order']['id']])
- ->count();
- }
- // 返回自提订单是否允许退款,如果不是社区团购自提订单也没必要返回是否允许退款的配置了
- if (!empty($params['order']['detail']) && !empty($params['order']['is_community_order'])) {
- $goodsIds = array_column($params['order']['detail'], 'goods_id');
- $where = ['goods_id' => $goodsIds, 'status' => StatusEnum::ENABLED, 'enable' => StatusEnum::ENABLED];
- $goodsSettingList = CommunityGoodsSetting::find()
- ->where($where)
- ->select('goods_id, enable_refund')
- ->asArray()
- ->indexBy('goods_id')
- ->all();
- array_walk($params['order']['detail'], function (&$item) use ($goodsSettingList) {
- $item['community_goods_setting'] = $goodsSettingList[$item['goods_id']] ?? [];
- if (isset($item['community_goods_setting']['enable_refund'])) {
- // 返回前端是否要显示售后按钮,前端状态 1待发货 2已发货 才能申请
- $item['community_goods_setting']['enable_refund_1'] = $item['community_goods_setting']['enable_refund'] &&
- in_array($item['order_status'], [OrderStatusEnum::PAY, OrderStatusEnum::SHIPMENTS]);
- }
- });
- }
- $head_info = (new HeadService())->pickupInfoOrder($params['order']['mall_id'], $ext['head_id']);
- if (empty($head_info)) {
- Yii::error("获取团长信息失败: {$params['order']['id']}, {$params['order']['extra_info']}");
- return $params['order'];
- }
- $config = Yii::$app->services->setting->addons->get($params['order']['mall_id'], CommunityEnum::ADDONS_NAME, 'basic');
- $head_info['tips'] = '具体取货时间可联系自提点';
- if ($config['pick_up_time_range_type'] == StatusEnum::DISABLED) {
- $head_info['business_hours'] = '全天';
- } else {
- $head_info['business_hours'] = implode(' - ', Json::decode($config['pick_up_time_range_custom_at']));
- }
- $head_info['pickup_at'] = '';
- if (!empty($params['order']['consign_time']) && !empty($params['order']['mall_id'])) {
- $head_info['pickup_at'] = CommunityHelper::getPickUpAt($params['order']['mall_id'], $params['order']['consign_time']);
- }
- $is_enable_write_off = $config['is_enable_write_off'] ?? StatusEnum::DISABLED;
- // 如果订单不为用户自提那么就不能走核销码,如果订单为用户自提那么只能在已支付、已发货情况下进行核销
- // 如果是自提,那么就有核销码,如果是送货上门,那么就没有核销码
- if (!in_array($params['order']['order_status'], [OrderStatusEnum::PAY, OrderStatusEnum::SHIPMENTS]) ||
- $params['order']['shipping_type'] != ShippingTypeEnum::PICKUP) {
- $is_enable_write_off = StatusEnum::DISABLED;
- }
- $params['order']['pickup_info'] = [
- 'is_enable_write_off' => $is_enable_write_off,
- 'head_info' => $head_info,
- 'pickup_person' => [ // 提货人
- 'contact_name' => !empty($ext['contact_name']) ? $ext['contact_name'] : $params['order']['receiver_name'],
- 'contact_phone' => !empty($ext['contact_phone']) ? $ext['contact_phone'] : $params['order']['mobile']
- ],
- ];
- }
- $params['order']['is_community_pickup'] = StatusEnum::ENABLED; // 是否为社区自提
- } else {
- $params['order']['is_community_pickup'] = StatusEnum::DISABLED;
- }
- return $params['order'];
- }
- /**
- * 公共调用 - 订单列表
- * @param $params
- * @return mixed
- */
- public function globalList($params)
- {
- if (!empty($params['order'])) {
- $config = Yii::$app->services->setting->addons->get($params['mall_id'], CommunityEnum::ADDONS_NAME, 'basic');
- foreach ($params['order'] as &$item) {
- $item['order_status_txt'] = OrderStatusEnum::getValue($item['order_status']);
- $item['community_addons_data'] = [
- 'is_community_pickup' => StatusEnum::DISABLED,
- 'pickup_tips' => '',
- 'pickup_at' => '',
- 'is_enable_write_off' => StatusEnum::DISABLED,
- ];
- if (in_array($item['shipping_type'], [ShippingTypeEnum::PICKUP, ShippingTypeEnum::MERCHANT_DISTRIBUTION]) && !empty($item['extra_info'])) {
- try {
- $ext = Json::decode($item['extra_info']);
- } catch (\Exception $e) {
- return $params['order'];
- }
- if (!empty($ext['head_id'])) {
- // 这个是用来自提的
- $is_enable_write_off = $config['is_enable_write_off'] ?? StatusEnum::DISABLED;
- // 如果订单不为用户自提那么就不能走核销码,如果订单为用户自提那么只能在已支付、已发货情况下进行核销
- if (!in_array($item['order_status'], [OrderStatusEnum::PAY, OrderStatusEnum::SHIPMENTS]) ||
- $item['shipping_type'] != ShippingTypeEnum::PICKUP) {
- $is_enable_write_off = StatusEnum::DISABLED;
- }
- $item['community_addons_data']['is_community_pickup'] = StatusEnum::ENABLED;
- $item['community_addons_data']['pickup_tips'] = '站点自提';
- $item['community_addons_data']['is_enable_write_off'] = $is_enable_write_off;
- if ($item['order_status'] == OrderStatusEnum::SHIPMENTS) {
- $item['order_status_txt'] = $item['shipping_type'] == ShippingTypeEnum::MERCHANT_DISTRIBUTION ? '待配送' : '待自提';
- }
- // 需要取出信息
- if (!empty($item['consign_time']) && !empty($item['mall_id'])) {
- $item['community_addons_data']['pickup_at'] = CommunityHelper::getPickUpAt($item['mall_id'], $item['consign_time']);
- }
- }
- }
- }
- }
- return $params['order'];
- }
- /**
- * 公共调用 - 总后台订单列表
- * @param $params
- * @return mixed
- */
- public function globalBackendLists($params)
- {
- foreach ($params['order'] as &$order) {
- // 送货上门
- // 物流配送
- // 自提点自提
- $txt = "";
- $type = "";
- switch ($order['shipping_type']) {
- case '1': // 物流
- $txt = '快递配送';
- $type = "primary";
- break;
- case '2': // 自提点自提
- $txt = '自提点自提';
- $type = "success";
- break;
- case '3': // 送货上门
- $txt = '送货上门';
- $type = "danger";
- break;
- }
- $order['community_shipping_type'] = [
- 'title' => $txt,
- 'type' => $type
- ];
- if (!empty($order['mall_id']) && !empty($order['extra_info'])) {
- try {
- $ext = Json::decode($order['extra_info']);
- } catch (\Exception $e) {
- return $params['order'];
- }
- if (!empty($ext['head_id'])) {
- $order['community_addons_data']['is_community_pickup'] = StatusEnum::ENABLED;
- $order['community_addons_data']['head_info'] = (new HeadService())->pickupInfo($order['mall_id'], $ext['head_id']);
- }
- }
- }
- return $params['order'];
- }
- public function getClerkCode($mall_id)
- {
- while (true) {
- $clerk_code = ClerkEnum::getCode(ClerkEnum::COMMUNITY_CLERK_CODE_PRE);
- $res = Order::findOne(['clerk_code' => $clerk_code]);
- if (empty($res)) break;
- }
- if (empty($clerk_code)) throw new \Exception('核销码生成失败');
- return $clerk_code;
- }
- public function updateCode()
- {
- $i = 1;
- $page_size = 1000;
- while ($list = CommunityHeadOrder::find()
- ->andWhere(['>', 'id', 0])
- ->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
- foreach ($list as $item) {
- $code = $this->getClerkCode($item['mall_id']);
- $order = Order::find()->where(['order_no'=>$item['order_no'],'sign_time'=>0,'shipping_type'=>2,'store_id'=>0,'mch_id'=>0])->one();
- if($order){
- $order_no = $order['order_no'];
- $sql = "UPDATE `qimall_order` SET `clerk_code`='{$code}' WHERE `order_no`='{$order_no}'";
- $res = Yii::$app->db->createCommand($sql)->execute();
- var_dump($res);
- }
- }
- $i++;
- }
- }
- /**
- * 公共调用 - 订单详情设置是否是自提订单 并且该商品是否允许退款字段
- * @param $params
- * @return mixed
- */
- public function globalOrderDetailsItem($params)
- {
- if (empty($params['order_detail']['order_id'])) {
- return $params['order_detail'];
- }
- // 是否是自提订单
- $params['order_detail']['is_community_order'] = (bool)CommunityHeadOrder::find()
- ->where(['order_id' => $params['order_detail']['order_id']])
- ->count();
- if ($params['order_detail']['is_community_order'] && !empty($params['order_detail']['goods_id'])) {
- $params['order_detail']['community_goods_setting'] = CommunityGoodsSetting::getOne([
- ['mall_id' => $params['mall_id']],
- ['goods_id' => $params['order_detail']['goods_id']],
- ['status' => StatusEnum::ENABLED],
- ['enable' => StatusEnum::ENABLED]
- ], true, [], false, 'enable_refund');
- if (isset($params['order_detail']['community_goods_setting']['enable_refund'])) {
- // 返回前端是否要显示售后按钮,前端状态 1待发货 2已发货 才能申请
- $params['order_detail']['community_goods_setting']['enable_refund_1'] = $params['order_detail']['community_goods_setting']['enable_refund'] &&
- in_array($params['order_detail']['order_status'], [OrderStatusEnum::PAY, OrderStatusEnum::SHIPMENTS]);
- }
- }
- return $params['order_detail'];
- }
- /**
- * 我的订单页面所需数据
- *
- * @param string $mall_id
- * @param string $user_id
- *
- * @return array
- */
- public function myOrder(string $mall_id, string $user_id): array
- {
- // 通过用户ID去取数据
- $head_where = [
- ['mall_id' => $mall_id],
- ['user_id' => $user_id],
- ['status' => StatusEnum::ENABLED]
- ];
- $order_status = [
- 'pending_shipment' => [
- 'order_status' => 1,
- ],
- 'pending_pickup' => [
- 'order_status' => 2,
- 'shipping_type' => 2,
- ],
- 'pending_delivery' => [
- 'order_status' => 2,
- 'shipping_type' => 3,
- ],
- ];
- $tabCount = [
- 'pending_shipment' => 0,
- 'pending_pickup' => 0,
- 'pending_delivery' => 0,
- ];
- foreach ($order_status as $k => $item) {
- $ret = CommunityHeadOrder::lists([
- 'where' => array_merge($head_where, [['order_status' => $item['order_status']]]),
- 'select' => 'order_id'
- ]);
- if (!empty($ret)) {
- $order_ids = array_column($ret, 'order_id');
- $where = [
- ['in', 'id', $order_ids],
- ['is_feedback' => OrderStatusEnum::NOT_FEEDBACK],
- ['!=', 'order_source', OrderSourceEnum::SOURCE_SELF_MALL]
- ];
- if (isset($item['shipping_type'])) {// 判断订单配送类型
- $where[] = ['shipping_type' => $item['shipping_type']];
- }
- $orderFind = Order::find();
- Order::paramPack([
- 'where' => $where
- ], $orderFind);
- $tabCount[$k] = $orderFind->count();
- }
- }
- // 返回待发货、待自提、待配送数量
- return ['tab_counts' => $tabCount];
- }
- }
|