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-07-26 13:58:52 +08:00
|
|
|
use App\Models\BusinessOrderItem;
|
|
|
|
|
use App\Models\GoodsSku;
|
2023-04-04 15:28:56 +08:00
|
|
|
use App\Models\Shop;
|
2023-07-28 15:35:47 +08:00
|
|
|
use App\Services\Ship\WayBillService;
|
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-07-26 13:58:52 +08:00
|
|
|
use Illuminate\Support\Facades\DB;
|
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)
|
|
|
|
|
{
|
2023-07-27 18:30:09 +08:00
|
|
|
$shopIds = Shop::query()
|
|
|
|
|
->where('plat_id', Shop::$PLAT_KTT)
|
|
|
|
|
->pluck('id');
|
2023-07-26 13:58:52 +08:00
|
|
|
$builder = BusinessOrder::query()
|
2023-04-03 21:42:57 +08:00
|
|
|
->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
|
|
|
])
|
2023-07-27 18:30:09 +08:00
|
|
|
->whereIn('shop_id', $shopIds)
|
2023-07-26 13:58:52 +08:00
|
|
|
->filter();
|
|
|
|
|
$externalSkuIds = $request->get('external_sku_ids');
|
|
|
|
|
if ($externalSkuIds) {
|
|
|
|
|
$ids = BusinessOrderItem::query()->whereIn('external_sku_id', $externalSkuIds)->pluck('business_order_id');
|
|
|
|
|
$builder->whereIn('id', $ids);
|
|
|
|
|
}
|
|
|
|
|
$businessOrders = $builder->orderByDesc('confirm_at')
|
2023-04-03 15:41:14 +08:00
|
|
|
->paginate($request->get('per_page'));
|
|
|
|
|
|
|
|
|
|
return BusinessOrderResource::collection($businessOrders);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 13:58:52 +08:00
|
|
|
private function getOrderIdsWitmGoodsNum($goodsSkuNum, $orders)
|
|
|
|
|
{
|
|
|
|
|
$map = [
|
|
|
|
|
1 => [
|
|
|
|
|
'min' => 1,
|
|
|
|
|
'max' => 1
|
|
|
|
|
],
|
|
|
|
|
2 => [
|
|
|
|
|
'min' => 2,
|
|
|
|
|
'max' => 5
|
|
|
|
|
],
|
|
|
|
|
6 => [
|
|
|
|
|
'min' => 6,
|
|
|
|
|
'max' => 999999
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
$numMap = $map[$goodsSkuNum];
|
|
|
|
|
|
|
|
|
|
// 获取订单商品编码
|
|
|
|
|
$externalSkuIds = [];
|
|
|
|
|
foreach ($orders as $order) {
|
|
|
|
|
foreach ($order->items as $item) {
|
|
|
|
|
if ($item['external_sku_id']) {
|
|
|
|
|
$externalSkuIds [] = $item['external_sku_id'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$goodsSkus = GoodsSku::query()
|
|
|
|
|
->with('combinationGoods')
|
|
|
|
|
->whereIn('external_sku_id', $externalSkuIds)
|
|
|
|
|
->get('external_sku_id');
|
|
|
|
|
$goodsSkuItems = [];
|
|
|
|
|
foreach ($goodsSkus as $goodsSku) {
|
|
|
|
|
$goodsSkuItems[$goodsSku['external_sku_id']] = $goodsSku->combinationGoods->count() ?: 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ids = [];
|
|
|
|
|
foreach ($orders as $order) {
|
|
|
|
|
$itemNum = 0;
|
|
|
|
|
foreach ($order->items as $item) {
|
|
|
|
|
if (0 === $item['cancel_status']) {
|
|
|
|
|
$itemNum = isset($goodsSkuItems[$item['external_sku_id']]) ? $itemNum + $goodsSkuItems[$item['external_sku_id']] : $itemNum + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($itemNum >= $numMap['min'] && $itemNum <= $numMap['max']) {
|
|
|
|
|
$ids[] = $order['id'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ids;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-03 15:41:14 +08:00
|
|
|
public function exportOrderBlank(Request $request)
|
|
|
|
|
{
|
2023-07-28 19:13:51 +08:00
|
|
|
$orderIds = $request->get('order_ids');
|
2023-04-03 20:25:57 +08:00
|
|
|
$orders = BusinessOrder::query()
|
|
|
|
|
->with([
|
2023-04-08 16:20:13 +08:00
|
|
|
'items:id,business_order_id,external_sku_id,goods_number,goods_name,already_cancel_number',
|
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
|
|
|
])
|
2023-07-28 19:13:51 +08:00
|
|
|
->whereIn('id', $orderIds)
|
|
|
|
|
->get(['id']);
|
2023-04-03 20:25:57 +08:00
|
|
|
$distribution = [];
|
|
|
|
|
$no = [];
|
|
|
|
|
foreach ($orders as $key => $order) {
|
2023-04-04 21:16:13 +08:00
|
|
|
$index = $key + 1;
|
2023-07-28 19:13:51 +08:00
|
|
|
$no[] = 'P' . $index;
|
2023-04-03 20:25:57 +08:00
|
|
|
foreach ($order->items as $item) {
|
2023-04-08 16:20:13 +08:00
|
|
|
$num = $item['goods_number'] - $item['already_cancel_number'];
|
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-08 16:20:13 +08:00
|
|
|
$index = "P{$index}*{$num}";
|
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-08 16:20:13 +08:00
|
|
|
$distribution[$item['external_sku_id']]['num'] += $num;
|
2023-04-03 20:25:57 +08:00
|
|
|
} else {
|
|
|
|
|
$distribution[$item['external_sku_id']] = [
|
2023-04-04 15:28:56 +08:00
|
|
|
'p' => [$index],
|
|
|
|
|
'local' => $local,
|
2023-04-08 16:20:13 +08:00
|
|
|
'num' => $num,
|
2023-04-04 15:28:56 +08:00
|
|
|
'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-07-28 19:13:51 +08:00
|
|
|
$fileName = time();
|
2023-04-04 15:28:56 +08:00
|
|
|
ob_end_clean();
|
|
|
|
|
|
2023-07-28 19:13:51 +08:00
|
|
|
return Excel::download(new OrderBlankExport($fileName, $no, $distribution), $fileName . 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
|
|
|
}
|
2023-07-28 15:35:47 +08:00
|
|
|
|
|
|
|
|
public function print(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$shopIds = Shop::query()
|
|
|
|
|
->where('plat_id', Shop::$PLAT_KTT)
|
|
|
|
|
->pluck('id');
|
|
|
|
|
$builder = BusinessOrder::query()
|
|
|
|
|
->with('items')
|
|
|
|
|
->whereIn('shop_id', $shopIds)
|
|
|
|
|
->filter();
|
|
|
|
|
$externalSkuIds = $request->get('external_sku_ids');
|
|
|
|
|
if ($externalSkuIds) {
|
|
|
|
|
$ids = BusinessOrderItem::query()->whereIn('external_sku_id', $externalSkuIds)->pluck('business_order_id');
|
|
|
|
|
$builder->whereIn('id', $ids);
|
|
|
|
|
}
|
|
|
|
|
$businessOrders = $builder->get();
|
|
|
|
|
$waybill = new WayBillService();
|
|
|
|
|
$waybill->setOrders($businessOrders);
|
2023-07-28 19:13:51 +08:00
|
|
|
$contents = $waybill->getContents();
|
|
|
|
|
// 待打印数据
|
|
|
|
|
[$documents, $orderIds] = $waybill->getDocumentsAndOrderIds($contents);
|
|
|
|
|
|
|
|
|
|
return response([
|
2023-07-29 17:57:36 +08:00
|
|
|
'documents' => $this->combinationPrintDocuments($documents),
|
|
|
|
|
'order_ids' => implode(',', $orderIds),
|
2023-07-28 19:13:51 +08:00
|
|
|
]);
|
2023-07-28 15:35:47 +08:00
|
|
|
}
|
2023-07-29 17:57:36 +08:00
|
|
|
|
|
|
|
|
private function combinationPrintDocuments($documents)
|
|
|
|
|
{
|
|
|
|
|
$documentData = [
|
|
|
|
|
'data' => [
|
|
|
|
|
'height' => 240,
|
|
|
|
|
'list' => [
|
|
|
|
|
[
|
|
|
|
|
'fontSize' => 31.2,
|
|
|
|
|
'height' => 45.68,
|
|
|
|
|
'left' => 2.08,
|
|
|
|
|
'text' => '', // 备注
|
|
|
|
|
'top' => 2.08,
|
|
|
|
|
'width' => 413.52
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'waterdata' => [
|
|
|
|
|
'text' => ''
|
|
|
|
|
],
|
|
|
|
|
'width' => 560
|
|
|
|
|
],
|
|
|
|
|
'templateURL' => 'http://pinduoduoimg.yangkeduo.com/logistics/2019-07-14/5d7e8b5969d954539fcfba3268bbeb3a.xml'
|
|
|
|
|
];
|
|
|
|
|
$data = [];
|
|
|
|
|
foreach ($documents as &$document) {
|
2023-08-15 16:33:36 +08:00
|
|
|
$documentData['data']['list'][0]['text'] = '[跟团号: ' . $document['participate_no'] . '] ';
|
2023-07-29 17:57:36 +08:00
|
|
|
$documentID = $document['documentID'];
|
2023-08-15 16:33:36 +08:00
|
|
|
$count = 0;
|
2023-08-09 17:45:20 +08:00
|
|
|
foreach ($document['items'] as $item) {
|
2023-08-15 16:33:36 +08:00
|
|
|
$count += $item['count'];
|
2023-08-09 17:45:20 +08:00
|
|
|
$documentData['data']['list'][0]['text'] .= $item['name'] . ' ' . $item['count'] . '件;';
|
|
|
|
|
}
|
2023-08-15 16:33:36 +08:00
|
|
|
$documentData['data']['list'][0]['text'] .= ' 总计: ' . $count . '件';
|
|
|
|
|
if ($document['note']) {
|
|
|
|
|
$documentData['data']['list'][0]['text'] .= ' 备注:' . $document['note'];
|
|
|
|
|
}
|
2023-08-16 15:50:41 +08:00
|
|
|
$documentData['data']['templateURL'] = $document['templateUrl'];
|
2023-08-16 15:19:01 +08:00
|
|
|
unset($document['documentID'], $document['items'], $document['order_id'], $document['participate_no'], $document['note']);
|
2023-07-29 17:57:36 +08:00
|
|
|
$data[] = [
|
|
|
|
|
'documentID' => $documentID,
|
|
|
|
|
'contents' => [
|
|
|
|
|
$document,
|
|
|
|
|
$documentData
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function printSuccess(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$orderIds = $request->input('order_ids');
|
|
|
|
|
$orderIds = explode(',', $orderIds);
|
|
|
|
|
BusinessOrder::query()
|
|
|
|
|
->where('id', $orderIds)
|
|
|
|
|
->increment('print_status');
|
|
|
|
|
|
|
|
|
|
return response(['message' => 'success']);
|
|
|
|
|
}
|
2023-04-03 15:41:14 +08:00
|
|
|
}
|