erp/app/Http/Controllers/Business/BusinessOrderController.php

100 lines
3.9 KiB
PHP
Raw Normal View History

2023-04-03 15:41:14 +08:00
<?php
namespace App\Http\Controllers\Business;
2023-04-04 15:28:56 +08:00
use App\Exports\OrderBlankExport;
2023-04-03 15:41:14 +08:00
use App\Http\Controllers\Controller;
use App\Models\BusinessOrder;
2023-04-04 15:28:56 +08:00
use App\Models\Shop;
2023-04-03 20:25:57 +08:00
use App\Utils\DateTimeUtils;
use Carbon\Carbon;
2023-04-03 15:41:14 +08:00
use Illuminate\Http\Request;
use App\Http\Resources\BusinessOrderResource;
2023-04-04 15:28:56 +08:00
use Maatwebsite\Excel\Facades\Excel;
2023-04-03 15:41:14 +08:00
class BusinessOrderController extends Controller
{
public function index(Request $request)
{
$businessOrders = BusinessOrder::query()
2023-04-03 21:42:57 +08:00
->where('shop_id', '<>', 8)
->with([
'shop:id,name',
2023-04-04 15:28:56 +08:00
'items:id,business_order_id,goods_name,goods_number,external_sku_id'
2023-04-03 21:42:57 +08:00
])
->orderByDesc('confirm_at')
2023-04-03 15:41:14 +08:00
->filter()
->paginate($request->get('per_page'));
return BusinessOrderResource::collection($businessOrders);
}
public function exportOrderBlank(Request $request)
{
2023-04-03 20:25:57 +08:00
$shopId = $request->get('shop_id');
$startNo = $request->get('start_no');
$endNo = $request->get('end_no');
2023-04-04 15:28:56 +08:00
$startTime = BusinessOrder::query()->where('shop_id', $shopId)->where('participate_no', $startNo)->value('confirm_at');
$endTime = BusinessOrder::query()->where('shop_id', $shopId)->where('participate_no', $endNo)->value('confirm_at');
2023-04-03 20:25:57 +08:00
$orders = BusinessOrder::query()
->with([
'items:id,business_order_id,external_sku_id,goods_number,goods_name',
2023-04-04 15:28:56 +08:00
'items.goodsSkuLocation:id,external_sku_id,location,goods_name'
2023-04-03 20:25:57 +08:00
])
->where('shop_id', $shopId)
2023-04-04 15:28:56 +08:00
->where('confirm_at', '>=', DateTimeUtils::getMicroTime($startTime))
->where('confirm_at', '<=', DateTimeUtils::getMicroTime($endTime))
2023-04-03 20:25:57 +08:00
->where('after_sales_status', 0)
->where('cancel_status', 0)
->where('is_supplier', 1)
->orderByDesc('confirm_at')
->get(['id', 'participate_no']);
$distribution = [];
$no = [];
foreach ($orders as $key => $order) {
2023-04-04 21:16:13 +08:00
$index = $key + 1;
$no[] = 'P' . $index . '(' . $order->participate_no . ')';
2023-04-03 20:25:57 +08:00
foreach ($order->items as $item) {
2023-04-04 15:28:56 +08:00
$item = $item->toArray();
if (empty($item['external_sku_id'])) {
continue;
}
2023-04-04 21:16:13 +08:00
$local = $item['goods_sku_location'] ? $item['goods_sku_location']['location'] : '无';
2023-04-04 15:28:56 +08:00
$index = $key + 1;
2023-04-04 21:16:13 +08:00
$index = "P{$index}*{$item['goods_number']}";
2023-04-04 15:28:56 +08:00
[$goodsCode, $skuCode] = explode('_', $item['external_sku_id']);
2023-04-03 20:25:57 +08:00
if (isset($distribution[$item['external_sku_id']])) {
2023-04-04 15:28:56 +08:00
$distribution[$item['external_sku_id']]['p'][] = $index;
2023-04-03 20:25:57 +08:00
$distribution[$item['external_sku_id']]['num'] += $item['goods_number'];
} else {
$distribution[$item['external_sku_id']] = [
2023-04-04 15:28:56 +08:00
'p' => [$index],
'local' => $local,
'num' => $item['goods_number'],
'goods_name' => $item['goods_name'],
'goods_code' => $goodsCode,
2023-04-04 21:16:13 +08:00
'sku_code' => $skuCode,
2023-04-03 20:25:57 +08:00
];
}
}
}
2023-04-04 15:28:56 +08:00
$shopName = Shop::query()->where('id', $shopId)->value('name');
ob_end_clean();
return Excel::download(new OrderBlankExport($shopName, $no, $distribution), $shopName . date('Y-m-d H:i:s') . '.xlsx');
2023-04-03 20:25:57 +08:00
}
public function groupActivity(Request $request, $shopId)
{
$todayTime = Carbon::today()->timestamp;
$todayTime = DateTimeUtils::getMicroTime($todayTime);
2023-04-03 15:41:14 +08:00
2023-04-03 20:25:57 +08:00
return BusinessOrder::query()
->where('shop_id', $shopId)
->where('confirm_at', '>=', $todayTime)
2023-04-04 15:28:56 +08:00
->groupBy(['activity_no', 'activity_title'])
->get(['activity_title', 'activity_no'])
2023-04-03 20:25:57 +08:00
->toArray();
2023-04-03 15:41:14 +08:00
}
}