diff --git a/app/Http/Controllers/Business/BusinessOrderController.php b/app/Http/Controllers/Business/BusinessOrderController.php index 2cc1057..8424e59 100644 --- a/app/Http/Controllers/Business/BusinessOrderController.php +++ b/app/Http/Controllers/Business/BusinessOrderController.php @@ -95,61 +95,21 @@ class BusinessOrderController extends Controller public function exportOrderBlank(Request $request) { - $shopId = $request->get('shop_id'); - $startNo = $request->get('start_no'); - $endNo = $request->get('end_no'); - $isSupplier = $request->get('is_supplier'); - $field = 'participate_no'; - if ($isSupplier) { - $field = 'supply_participate_no'; - } - $startTime = BusinessOrder::query() - ->where('shop_id', $shopId) - ->where('is_supplier', $isSupplier) - ->where($field, $startNo) - ->orderByDesc('id') - ->value('confirm_at'); - if (empty($startTime)) { - exit('开始跟团号订单未查询到或正在同步中,请稍后再次获取'); - } - $startTime = DateTimeUtils::getMicroTime($startTime); - $endTime = BusinessOrder::query() - ->where('shop_id', $shopId) - ->where('is_supplier', $isSupplier) - ->where($field, $endNo) - ->orderByDesc('id') - ->value('confirm_at'); - if (empty($endTime)) { - exit('结束跟团号订单未查询到或正在同步中,请稍后再次获取'); - } - $endTime = DateTimeUtils::getMicroTime($endTime); - if ($startTime > $endTime) { - exit('开始跟团号订单 成交时间 大于 结束跟团号订单时间,请查验后再试!'); - } + $orderIds = $request->get('order_ids'); $orders = BusinessOrder::query() ->with([ 'items:id,business_order_id,external_sku_id,goods_number,goods_name,already_cancel_number', 'items.goodsSkuLocation:id,external_sku_id,location,goods_name' ]) - ->where('shop_id', $shopId) - ->where('confirm_at', '>=', $startTime) - ->where('confirm_at', '<=', $endTime) - ->where('after_sales_status', 0) - ->where('cancel_status', 0) - ->where('is_supplier', $isSupplier) - ->orderByDesc('confirm_at') - ->get(['id', $field]); + ->whereIn('id', $orderIds) + ->get(['id']); $distribution = []; $no = []; foreach ($orders as $key => $order) { $index = $key + 1; - $no[] = 'P' . $index . '(' . $order->$field . ')'; + $no[] = 'P' . $index; foreach ($order->items as $item) { - $item = $item->toArray(); $num = $item['goods_number'] - $item['already_cancel_number']; - if (empty($item['external_sku_id']) || empty($num)) { - continue; - } $local = $item['goods_sku_location'] ? $item['goods_sku_location']['location'] : '无'; $index = $key + 1; $index = "P{$index}*{$num}"; @@ -169,10 +129,10 @@ class BusinessOrderController extends Controller } } } - $shopName = Shop::query()->where('id', $shopId)->value('name'); + $fileName = time(); ob_end_clean(); - return Excel::download(new OrderBlankExport($shopName, $no, $distribution), $shopName . date('Y-m-d H:i:s') . '.xlsx'); + return Excel::download(new OrderBlankExport($fileName, $no, $distribution), $fileName . date('Y-m-d H:i:s') . '.xlsx'); } public function groupActivity(Request $request, $shopId) @@ -205,6 +165,17 @@ class BusinessOrderController extends Controller $businessOrders = $builder->get(); $waybill = new WayBillService(); $waybill->setOrders($businessOrders); - $waybill->get(); + $contents = $waybill->getContents(); + // 待打印数据 + [$documents, $orderIds] = $waybill->getDocumentsAndOrderIds($contents); + // 只有订单商品种类2-5的需要配货单 + if (2 !== $request->get('goods_sku_num')) { + $orderIds = []; + } + + return response([ + 'documents' => $documents, + 'order_ids' => $orderIds, + ]); } } diff --git a/app/Services/Ship/WayBillService.php b/app/Services/Ship/WayBillService.php index 302619e..92107c2 100644 --- a/app/Services/Ship/WayBillService.php +++ b/app/Services/Ship/WayBillService.php @@ -13,9 +13,14 @@ class WayBillService public $objectId; public $templateUrl = 'https://file-link.pinduoduo.com/sf_one'; - public function get() + public function getContents() { + $contents = []; foreach ($this->orders as $shopId => $order) { + // 花落测试; + if ($shopId !== 6) { + continue; + } // 订单取消的情况暂不处理 $shop = $this->getShop($shopId); $faceSheet = new FaceSheet(); @@ -34,9 +39,54 @@ class WayBillService $waybill->ver = $printData['ver']; $waybill->waybill_code = $data['waybill_code']; $waybill->save(); + // 返回待打印内容 + $contents[$waybill->id] = [ + 'addData' => [ + 'sender' => $sender, + ], + 'encryptedData' => $printData['encryptedData'], + 'signature' => $printData['signature'], + 'templateUrl' => $printData['templateUrl'], + 'ver' => $printData['ver'], + 'userid' => $waybill->user_id, + 'items' => $item['items'], + 'documentID' => $waybill->id, + 'order_id' => $item['id'] + ]; + + return $contents; //测试 } } } + + return $contents; + } + + public function getDocumentsAndOrderIds($contents) + { + // 打印单,根据商品排序 + $items = []; + foreach ($contents as $docId => $content) { + foreach ($content['items'] as $item) { + if ($item['is_single']) { + $items[$item['external_sku_id']][] = $docId; + } + } + } + ksort($items); + $documents = $orderIds = $hasIds = []; + foreach ($items as $docIds) { + $docIds = array_unique($docIds); + $docIds = array_diff($docIds, $hasIds); + $hasIds = array_merge($hasIds, $docIds); + foreach ($docIds as $docId) { + $orderIds[] = $contents[$docId]['order_id']; + unset($contents[$docId]['items'], $contents[$docId]['order_id']); + $documents[] = $contents[$docId]; + } + } + + return [$documents, $orderIds]; } private function saveWayBill($order, $shop)