removed unwanted logic from pallet print controller #393

Merged
jothi merged 1 commits from ranjith-dev into master 2026-02-25 04:23:14 +00:00
2 changed files with 237 additions and 144 deletions

View File

@@ -7,6 +7,7 @@ use App\Models\Plant;
use App\Models\WireMasterPacking; use App\Models\WireMasterPacking;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf; use Barryvdh\DomPDF\Facade\Pdf;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class PalletPrintController extends Controller class PalletPrintController extends Controller
@@ -19,164 +20,256 @@ 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,
// 'qrHtml' => '<barcode code="' . $pallet . '" type="QRCODE,H" class="barcode" />'
// ])->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) public function print(Request $request, $pallet, $plant)
{ {
$customerId = $request->query('customer');
$customerId = $request->query('customer'); // Fetch items
$items = WireMasterPacking::with('item')
// $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('plant_id', $plant)
->where('wire_packing_number', $pallet) ->where('wire_packing_number', $pallet)
->get() ->get()
->map(function ($row) { ->map(function ($row) {
return (object) [ return (object) [
'code' => $row->item->code, 'code' => $row->item->code,
'description' => $row->item->description, 'description' => $row->item->description,
'box_count' => 1, // each row = one box 'box_count' => 1,
'weight' => $row->weight, 'weight' => $row->weight,
]; ];
}); });
$masterBox = WireMasterPacking::where('plant_id', $plant) // Fetch master box & customer info
->where('wire_packing_number', $pallet) $masterBox = WireMasterPacking::where('plant_id', $plant)
->value('customer_po_master_id'); ->where('wire_packing_number', $pallet)
->value('customer_po_master_id');
$customer = CustomerPoMaster::find($masterBox); $customer = CustomerPoMaster::find($masterBox);
$customerCode = $customer->customer_po ?? '';
$customerName = $customer->customer_name ?? '';
$customerCode = $customer->customer_po ?? ''; // Calculate total boxes & current pallet number
$customerName = $customer->customer_name ?? ''; $totalBoxes = WireMasterPacking::where('plant_id', $plant)
->where('customer_po_master_id', $customerId)
->distinct('wire_packing_number')
->count('wire_packing_number');
// $masterBox = WireMasterPacking::where('plant_id', $plant) $completedPallets = WireMasterPacking::where('plant_id', $plant)
// ->where('wire_packing_number', $pallet) ->where('customer_po_master_id', $customerId)
// ->distinct('customer_po') ->select('wire_packing_number')
// ->count('customer_po'); ->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();
// $pallets = WireMasterPacking::where('plant_id', $plant) $index = $completedPallets->search($pallet);
// ->select('wire_packing_number', 'updated_at') $currentPalletNo = ($index !== false) ? $index + 1 : 0;
// ->distinct('wire_packing_number') $boxLabel = $currentPalletNo . '/' . $totalBoxes;
// ->orderBy('wire_packing_number')
// ->orderBy('updated_at', 'asc')
// ->get()
// ->pluck('wire_packing_number')
// ->values();
// $currentPalletNo = $pallets->search($pallet) + 1; // Calculate gross weight
$grossWeight = $items->sum('weight');
// $totalBoxes = WireMasterPacking::where('plant_id', $plant) // Page dimensions in points for DomPDF
// // ->where('wire_packing_number', $pallet) $widthPt = 85 * 2.83465; // 85mm
// ->distinct() $heightPt = 100 * 2.83465; // 100mm
// ->count('customer_po');
// $boxLabel = $currentPalletNo . '/' . $totalBoxes; // Plant info
$plantName = Plant::where('id', $plant)->value('name');
$plantAddress = Plant::where('id', $plant)->value('address');
$totalBoxes = WireMasterPacking::where('plant_id', $plant) // Generate QR code using GD (no Imagick required)
->where('customer_po_master_id', $customerId) $qrBase64 = 'data:image/png;base64,' . base64_encode(
->distinct('wire_packing_number') QrCode::format('png')
->count('wire_packing_number'); ->size(120) // ~12mm
->margin(0)
->errorCorrection('H')
->generate($pallet)
);
$completedPallets = WireMasterPacking::where('plant_id', $plant) // Load Blade view with data
->where('customer_po_master_id', $customerId) $pdf = Pdf::loadView('pdf.wire-pallet', [
->select('wire_packing_number') 'product' => 'Submersible Winding Wire',
->groupBy('wire_packing_number') 'plantName' => $plantName,
->havingRaw( 'plantAddress' => $plantAddress,
'COUNT(*) = COUNT(CASE WHEN wire_packing_status = ? THEN 1 END)', 'monthYear' => now()->format('M-y'),
['Completed'] 'branch' => '',
) 'customerCode' => $customerCode,
->orderBy('wire_packing_number') 'customerName' => $customerName,
->pluck('wire_packing_number') 'masterBox' => $boxLabel,
->values(); 'items' => $items,
'grossWeight' => $grossWeight,
'netWeight' => $grossWeight - 3.05,
'pallet' => $pallet,
'qrBase64' => $qrBase64
])->setPaper([0, 0, $widthPt, $heightPt], 'portrait');
return $pdf->stream("Pallet-{$pallet}.pdf");
}
$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 store(Request $request) public function store(Request $request)
{ {
// //

View File

@@ -84,13 +84,13 @@
// ->size(120) // 12mm ~ 120px // ->size(120) // 12mm ~ 120px
// ->margin(0) // ->margin(0)
// ->generate($pallet) // ->generate($pallet)
$qrBase64 = 'data:image/png;base64,' . base64_encode( // $qrBase64 = 'data:image/png;base64,' . base64_encode(
QrCode::format('png') // QrCode::format('png')
->size(120) // ->size(120)
->margin(0) // ->margin(0)
->errorCorrection('H') // ->errorCorrection('H')
->generate($pallet) // ->generate($pallet)
); // );
@endphp @endphp
<style> <style>
@@ -298,7 +298,7 @@
max-width: <?php echo $isilogoMaxWidth; ?>mm; max-width: <?php echo $isilogoMaxWidth; ?>mm;
left: 71mm;"> --}} left: 71mm;"> --}}
<img src="{{ $qrBase64 }}" <img src="{{ $qrBase64 }}"
style="position: absolute; bottom: 1.2mm; right: 2mm; width: 8mm; height: 7.2mm;"> style="position: absolute; bottom: 1.2mm; right: 2mm; width: 8mm; height: 7.2mm;">
</td> </td>
</tr> </tr>