diff --git a/app/Http/Controllers/PalletPrintController.php b/app/Http/Controllers/PalletPrintController.php index f0f2a93..e580eda 100644 --- a/app/Http/Controllers/PalletPrintController.php +++ b/app/Http/Controllers/PalletPrintController.php @@ -5,9 +5,9 @@ namespace App\Http\Controllers; use App\Models\CustomerPoMaster; use App\Models\Plant; use App\Models\WireMasterPacking; -use Illuminate\Http\Request; use Barryvdh\DomPDF\Facade\Pdf; -use SimpleSoftwareIO\QrCode\Facades\QrCode; +use Illuminate\Http\Request; + class PalletPrintController extends Controller @@ -20,9 +20,168 @@ class PalletPrintController extends Controller // } + public function print(Request $request, $pallet, $plant) + { + + $customerId = $request->query('customer'); + + // $customerName = $request->query('customer_name'); + + // $items = WireMasterPacking::with('item') + // ->where('plant_id', $plant) + // ->where('wire_packing_number', $pallet) + // ->get() + // ->groupBy('item_id') + // ->map(function ($rows) { + // $first = $rows->first(); + + // return (object) [ + // 'code' => $first->item->code, + // 'description' => $first->item->description, + // 'box_count' => $rows->count(), + // 'weight' => $rows->sum('weight'), + // ]; + // }) + // ->values(); + + $items = WireMasterPacking::with('item') + ->where('plant_id', $plant) + ->where('wire_packing_number', $pallet) + ->get() + ->map(function ($row) { + return (object) [ + 'code' => $row->item->code, + 'description' => $row->item->description, + 'box_count' => 1, // each row = one box + 'weight' => $row->weight, + ]; + }); + + $masterBox = WireMasterPacking::where('plant_id', $plant) + ->where('wire_packing_number', $pallet) + ->value('customer_po_master_id'); + + $customer = CustomerPoMaster::find($masterBox); + + $customerCode = $customer->customer_po ?? ''; + $customerName = $customer->customer_name ?? ''; + + // $masterBox = WireMasterPacking::where('plant_id', $plant) + // ->where('wire_packing_number', $pallet) + // ->distinct('customer_po') + // ->count('customer_po'); + + // $pallets = WireMasterPacking::where('plant_id', $plant) + // ->select('wire_packing_number', 'updated_at') + // ->distinct('wire_packing_number') + // ->orderBy('wire_packing_number') + // ->orderBy('updated_at', 'asc') + // ->get() + // ->pluck('wire_packing_number') + // ->values(); + + // $currentPalletNo = $pallets->search($pallet) + 1; + + // $totalBoxes = WireMasterPacking::where('plant_id', $plant) + // // ->where('wire_packing_number', $pallet) + // ->distinct() + // ->count('customer_po'); + + // $boxLabel = $currentPalletNo . '/' . $totalBoxes; + + $totalBoxes = WireMasterPacking::where('plant_id', $plant) + ->where('customer_po_master_id', $customerId) + ->distinct('wire_packing_number') + ->count('wire_packing_number'); + + $completedPallets = WireMasterPacking::where('plant_id', $plant) + ->where('customer_po_master_id', $customerId) + ->select('wire_packing_number') + ->groupBy('wire_packing_number') + ->havingRaw( + 'COUNT(*) = COUNT(CASE WHEN wire_packing_status = ? THEN 1 END)', + ['Completed'] + ) + ->orderBy('wire_packing_number') + ->pluck('wire_packing_number') + ->values(); + + + $index = $completedPallets->search($pallet); + + $currentPalletNo = ($index !== false) ? $index + 1 : 0; + + $boxLabel = $currentPalletNo . '/' . $totalBoxes; + + // $completedPallets = WireMasterPacking::where('plant_id', $plant) + // ->select('wire_packing_number') + // ->groupBy('wire_packing_number') + // ->havingRaw('COUNT(*) = COUNT(CASE WHEN wire_packing_status = ? THEN 1 END)', ['Completed']) + // ->orderBy('wire_packing_number') + // ->pluck('wire_packing_number') + // ->values(); + + // $currentPalletNo = $completedPallets->search($pallet) != false + // ? $completedPallets->search($pallet) + 1 + // : 0; + + // $boxLabel = $currentPalletNo . '/' . $totalBoxes; + + $grossWeight = $items->sum('weight'); + $widthPt = 85 * 2.83465; // 85mm → points + $heightPt = 100 * 2.83465; // 100mm → points + + $plantName = Plant::where('id', $plant)->value('name'); + + $plantAddress = Plant::where('id', $plant)->value('address'); + + $pdf = Pdf::loadView('pdf.wire-pallet', [ + 'product' => 'Submersible Winding Wire', + 'plantName' => $plantName, + 'plantAddress' => $plantAddress, + 'monthYear' => now()->format('M-y'), + 'branch' => '', + 'customerCode' => $customerCode, + 'customerName' => $customerName, + 'masterBox' => $boxLabel, + 'items' => $items, + 'grossWeight' => $grossWeight, + 'netWeight' => $grossWeight - 3.05, + 'pallet' => $pallet, + ])->setPaper([0, 0, $widthPt, $heightPt], 'portrait'); + + return $pdf->stream("Pallet-{$pallet}.pdf"); + // $pdfPath = storage_path("app/public/Pallet-{$pallet}.pdf"); + // $pdf->save($pdfPath); + + // $printerName = 'Tsc'; + // $output = []; + // $returnVar = 0; + + // exec("lp -d {$printerName} " . escapeshellarg($pdfPath), $output, $returnVar); + + // if ($returnVar == 0) { + // return response()->json([ + // 'status' => 'success', + // 'message' => "PDF sent to printer $printerName successfully." + // ]); + // } else { + // return response()->json([ + // 'status' => 'error', + // 'message' => "Failed to send PDF to printer $printerName.", + // 'output' => $output, + // 'code' => $returnVar + // ], 500); + // } + } + + /** + * Store a newly created resource in storage. + */ + + // public function print(Request $request, $pallet, $plant) // { - // $customerId = $request->query('customer'); // // $customerName = $request->query('customer_name'); @@ -135,141 +294,34 @@ class PalletPrintController extends Controller // $plantAddress = Plant::where('id', $plant)->value('address'); - // $pdf = Pdf::loadView('pdf.wire-pallet', [ - // 'product' => 'Submersible Winding Wire', - // 'plantName' => $plantName, - // 'plantAddress' => $plantAddress, - // 'monthYear' => now()->format('M-y'), - // 'branch' => '', - // 'customerCode' => $customerCode, - // 'customerName' => $customerName, - // 'masterBox' => $boxLabel, - // 'items' => $items, - // 'grossWeight' => $grossWeight, - // 'netWeight' => $grossWeight - 3.05, + + // // Generate PDF with mPDF + // $mpdf = new Mpdf([ + // 'format' => [85, 100], + // 'margin_left' => 0, + // 'margin_right' => 0, + // 'margin_top' => 0, + // 'margin_bottom' => 0, + // ]); + + // $html = view('pdf.wire-pallet', [ + // 'product' => 'Submersible Winding Wire', + // 'plantName' => $plantName, + // 'plantAddress' => $plantAddress, + // 'monthYear' => now()->format('M-y'), + // 'customerCode' => $customerCode, + // 'customerName' => $customerName, + // 'masterBox' => $pallet, + // 'items' => $items, + // 'grossWeight' => $grossWeight, + // 'netWeight' => $grossWeight - 3.05, // 'pallet' => $pallet, - // 'qrHtml' => '' - // ])->setPaper([0, 0, $widthPt, $heightPt], 'portrait'); + // ])->render(); - // return $pdf->stream("Pallet-{$pallet}.pdf"); - // // $pdfPath = storage_path("app/public/Pallet-{$pallet}.pdf"); - // // $pdf->save($pdfPath); - - // // $printerName = 'Tsc'; - // // $output = []; - // // $returnVar = 0; - - // // exec("lp -d {$printerName} " . escapeshellarg($pdfPath), $output, $returnVar); - - // // if ($returnVar == 0) { - // // return response()->json([ - // // 'status' => 'success', - // // 'message' => "PDF sent to printer $printerName successfully." - // // ]); - // // } else { - // // return response()->json([ - // // 'status' => 'error', - // // 'message' => "Failed to send PDF to printer $printerName.", - // // 'output' => $output, - // // 'code' => $returnVar - // // ], 500); - // // } + // $mpdf->WriteHTML($html); + // return $mpdf->Output("Pallet-{$pallet}.pdf", 'I'); // 'I' = inline view in browser // } - /** - * Store a newly created resource in storage. - */ - - - public function print(Request $request, $pallet, $plant) -{ - $customerId = $request->query('customer'); - - // Fetch items - $items = WireMasterPacking::with('item') - ->where('plant_id', $plant) - ->where('wire_packing_number', $pallet) - ->get() - ->map(function ($row) { - return (object) [ - 'code' => $row->item->code, - 'description' => $row->item->description, - 'box_count' => 1, - 'weight' => $row->weight, - ]; - }); - - // Fetch master box & customer info - $masterBox = WireMasterPacking::where('plant_id', $plant) - ->where('wire_packing_number', $pallet) - ->value('customer_po_master_id'); - - $customer = CustomerPoMaster::find($masterBox); - $customerCode = $customer->customer_po ?? ''; - $customerName = $customer->customer_name ?? ''; - - // Calculate total boxes & current pallet number - $totalBoxes = WireMasterPacking::where('plant_id', $plant) - ->where('customer_po_master_id', $customerId) - ->distinct('wire_packing_number') - ->count('wire_packing_number'); - - $completedPallets = WireMasterPacking::where('plant_id', $plant) - ->where('customer_po_master_id', $customerId) - ->select('wire_packing_number') - ->groupBy('wire_packing_number') - ->havingRaw( - 'COUNT(*) = COUNT(CASE WHEN wire_packing_status = ? THEN 1 END)', - ['Completed'] - ) - ->orderBy('wire_packing_number') - ->pluck('wire_packing_number') - ->values(); - - $index = $completedPallets->search($pallet); - $currentPalletNo = ($index !== false) ? $index + 1 : 0; - $boxLabel = $currentPalletNo . '/' . $totalBoxes; - - // Calculate gross weight - $grossWeight = $items->sum('weight'); - - // Page dimensions in points for DomPDF - $widthPt = 85 * 2.83465; // 85mm - $heightPt = 100 * 2.83465; // 100mm - - // Plant info - $plantName = Plant::where('id', $plant)->value('name'); - $plantAddress = Plant::where('id', $plant)->value('address'); - - // Generate QR code using GD (no Imagick required) - $qrBase64 = 'data:image/png;base64,' . base64_encode( - QrCode::format('png') - ->size(120) // ~12mm - ->margin(0) - ->errorCorrection('H') - ->generate($pallet) - ); - - // Load Blade view with data - $pdf = Pdf::loadView('pdf.wire-pallet', [ - 'product' => 'Submersible Winding Wire', - 'plantName' => $plantName, - 'plantAddress' => $plantAddress, - 'monthYear' => now()->format('M-y'), - 'branch' => '', - 'customerCode' => $customerCode, - 'customerName' => $customerName, - 'masterBox' => $boxLabel, - 'items' => $items, - 'grossWeight' => $grossWeight, - 'netWeight' => $grossWeight - 3.05, - 'pallet' => $pallet, - 'qrBase64' => $qrBase64 - ])->setPaper([0, 0, $widthPt, $heightPt], 'portrait'); - - return $pdf->stream("Pallet-{$pallet}.pdf"); -} - public function store(Request $request) { // diff --git a/resources/views/pdf/wire-pallet.blade.php b/resources/views/pdf/wire-pallet.blade.php index 97f08f8..9622898 100644 --- a/resources/views/pdf/wire-pallet.blade.php +++ b/resources/views/pdf/wire-pallet.blade.php @@ -38,7 +38,7 @@ // Total fixed space $fixedSpace = $titleHeight + - (5 * $headerRowHeight) + // 6 header rows + (5 * $headerRowHeight) + $itemHeaderHeight + $grossWeightHeight + $netWeightHeight + @@ -51,7 +51,7 @@ $itemRowHeight = floor(($spaceForItemsOnly / $numItems) * 10) / 10; - // $itemRowHeight -= 0.5; + $itemRowHeight -= 0.5; if ($numItems == 1) { $itemRowHeight -= 0.5; } @@ -79,24 +79,26 @@ $compensatedNetHeight = $netWeightHeight - 0.6; $compensatedLicenseHeight = $licenseHeight - 0.6; $compensatedCompanyHeight = $companyInfoHeight - 0.6; + // $qrBase64 = 'data:image/png;base64,' . base64_encode( // QrCode::format('png') // ->size(120) // 12mm ~ 120px // ->margin(0) // ->generate($pallet) - // $qrBase64 = 'data:image/png;base64,' . base64_encode( - // QrCode::format('png') - // ->size(120) - // ->margin(0) - // ->errorCorrection('H') - // ->generate($pallet) - // ); + $qrBase64 = 'data:image/png;base64,' . base64_encode( + \SimpleSoftwareIO\QrCode\Facades\QrCode::format('png') + ->size(120) + ->margin(0) + ->errorCorrection('H') + ->generate($pallet) + ); @endphp