model()->orderBy('id', 'desc'); $grid->column('id')->sortable()->if(function () { return Admin::user()->can('dcat.admin.offline_orders.show'); })->then(function (Column $column) { $column->link(function ($value) { return admin_route('offline_orders.show', ['offline_order' => $value]); }); }); $grid->column('sn')->copyable(); $grid->column('user_id')->display(function () { $nickname = $this->userInfo?->nickname ?? '---'; $avatar = $this->userInfo?->avatar ?? 'https://via.placeholder.com/45x45.png'; $phone = $this->user?->phone; return << {$nickname} {$phone} HTML; }); $grid->column('store_id')->display(fn() => $this->store?->title); $grid->column('staff_id')->display(function () { if (is_null($this->staff_id)) { return ''; } $nickname = $this->staffInfo?->nickname ?? '---'; $avatar = $this->staffInfo?->avatar ?? 'https://via.placeholder.com/45x45.png'; $phone = $this->staff?->phone; return << {$nickname} {$phone} HTML; }); $grid->column('items')->display(function ($items) { return $items->map(function ($item) { $color = $item->productCategory?->color ?? '#586cb1'; $category = $item->productCategory?->name; if ($discount = $item->getDiscount()) { $category .= " {$discount}折"; } return "{$category}"; })->join(''); }); $grid->column('products_total_amount')->display(fn($v) => bcdiv($v, 100, 2))->prepend('¥'); $grid->column('discount_reduction_amount')->display(fn($v) => bcdiv($v, 100, 2))->prepend('¥'); $grid->column('points_deduction_amount')->display(fn($v) => bcdiv($v, 100, 2))->prepend('¥'); $grid->column('payment_amount')->display(fn($v) => bcdiv($v, 100, 2))->prepend('¥'); $grid->column('status')->display(fn($v) => $v->dot()); $grid->column('payment_method')->display(fn($v) => $v?->dot()); $grid->column('payment_time'); $grid->column('created_at'); $grid->filter(function (Grid\Filter $filter) { $filter->panel(); $filter->like('sn')->width(3); $filter->where('user_id', function ($q) { $q->where(function ($q) { $q->whereHas('user', fn($q) => $q->where('phone', 'like', '%'.$this->input.'%')) ->orWhereHas('userInfo', fn($q) => $q->where('nickname', 'like', '%'.$this->input.'%')); }); })->width(3)->placeholder('昵称/手机号'); $filter->like('payment_sn')->width(3); $filter->like('out_trade_no')->width(3); $filter->equal('store_id')->select(Store::pluck('title', 'id'))->width(3); $filter->where('product_category_id', function ($q) { $q->where(function ($q) { $q->whereHas('items', fn($q) => $q->where('product_category_id', $this->input)); }); }, '品类')->select(OfflineProductCategory::pluck('name', 'id'))->width(3); $filter->where('staff_id', function ($q) { $q->where(function ($q) { $q->whereHas('staff', fn($q) => $q->where('phone', 'like', '%'.$this->input.'%')) ->orWhereHas('staffInfo', fn($q) => $q->where('nickname', 'like', '%'.$this->input.'%')); }); })->width(3)->placeholder('昵称/手机号'); $filter->where('type', function ($builder) { if ($this->input == 1) { $builder->where('points_deduction_amount', '>', 0); } else { $builder->where('points_deduction_amount', 0); } }, '类型')->select([1 => '积分订单', 2 => '其它订单'])->width(3); $filter->in('status')->multipleSelect(OfflineOrderStatus::options())->width(3); $filter->equal('payment_method')->select([ PayWay::WxpayMiniProgram->value => PayWay::WxpayMiniProgram->text(), PayWay::None->value => PayWay::None->text(), ])->width(3); $filter->between('created_at')->dateTime()->width(6); }); $user = Admin::user(); $grid->actions(function (Grid\Displayers\Actions $actions) use ($user) { if ($user->can('dcat.admin.offline_orders.show')) { $actions->disableView(false); } if ($user->can('dcat.admin.offline_orders.revoke') && $actions->row->isPending()) { $actions->append(new OfflineOrderRevoke()); } }); $grid->header(function ($collection) use ($grid) { return tap(new Row(), function ($row) use ($grid) { $query = OfflineOrder::select([ DB::raw('sum(products_total_amount) as products_total_amount'), DB::raw('sum(discount_reduction_amount) as discount_reduction_amount'), DB::raw('sum(points_deduction_amount) as points_deduction_amount'), DB::raw('sum(payment_amount) as payment_amount'), ]); $grid->model()->getQueries()->unique()->each(function ($value) use (&$query) { if (in_array($value['method'], ['paginate', 'get', 'orderBy', 'orderByDesc'], true)) { return; } $query = call_user_func_array([$query, $value['method']], $value['arguments'] ?? []); }); $results = $query->first(); $row->column(3, new InfoBox('订单总额', bcdiv($results['products_total_amount'] ?? 0, 100, 2), 'fa fa-ticket')); $row->column(3, new InfoBox('折扣优惠', bcdiv($results['discount_reduction_amount'] ?? 0, 100, 2), 'fa fa-ticket')); $row->column(3, new InfoBox('积分抵扣', bcdiv($results['points_deduction_amount'] ?? 0, 100, 2), 'fa fa-ticket')); $row->column(3, new InfoBox('实付金额', bcdiv($results['payment_amount'] ?? 0, 100, 2), 'fa fa-ticket')); }); }); return $grid; } protected function detail($id) { return function (Row $row) use ($id) { $row->column(5, function ($column) use ($id) { $builder = OfflineOrderRepository::with(['user', 'userInfo', 'store', 'staff', 'staffInfo']); $column->row(Show::make($id, $builder, function (Show $show) { $show->row(function (Show\Row $show) { $show->width(6)->field('sn'); $show->width(6) ->field('phone') ->as(fn () => $this->user?->phone); $show->field('store_id') ->as(fn () => $this->store?->title); $show->field('staff_id') ->as(fn () => $this->staffInfo?->nickname ?? $this->staff?->phone); $show->field('products_total_amount') ->as(fn ($v) => bcdiv($v, 100, 2)) ->prepend('¥'); $show->field('discount_reduction_amount') ->as(fn ($v) => bcdiv($v, 100, 2)) ->prepend('-¥'); $show->field('points_deduction_amount') ->as(fn ($v) => bcdiv($v, 100, 2)) ->prepend('-¥'); $show->field('payment_amount') ->as(fn ($v) => bcdiv($v, 100, 2)) ->prepend('¥'); $show->field('status') ->escape(false) ->as(fn () => $this->status->dot()); $show->field('payment_method') ->escape(false) ->as(fn () => $this->payment_method?->dot()); $show->field('payment_time'); $show->field('payment_sn'); $show->field('out_trade_no'); $show->field('created_at'); $show->field('revoked_at'); }); $show->panel()->tools(function (Show\Tools $tools) use ($show) { $tools->disableEdit(); $tools->disableDelete(); }); })); }); $row->column(7, function ($column) use ($id) { $orderItemGrid = Grid::make(OfflineOrderItem::with(['productCategory'])->where('order_id', $id), function (Grid $grid) { $grid->column('product_category_name') ->display(fn() => $this->productCategory?->name ?: 'Unknown'); $grid->column('products_total_amount', '商品总额') ->display(fn ($v) => bcdiv($v, 100, 2))->prepend('¥'); $grid->column('discount_reduction_amount', '折扣优惠') ->display(fn ($v) => bcdiv($v, 100, 2))->prepend('¥'); $grid->column('points_deduction_amount', '积分抵扣') ->display(fn ($v) => bcdiv($v, 100, 2))->prepend('¥'); $grid->column('payment_amount', '实付金额') ->display(fn ($v) => bcdiv($v, 100, 2))->prepend('¥'); $grid->disableActions(); $grid->disablePagination(); $grid->disableRefreshButton(); }); $column->row(Box::make('品类', $orderItemGrid)); }); }; } }