Merge pull request 'corrected logic in wire print page' (#800) from ranjith-dev into master
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 44s

Reviewed-on: #800
This commit was merged in pull request #800.
This commit is contained in:
2026-07-06 06:21:33 +00:00
2 changed files with 220 additions and 216 deletions

View File

@@ -15,6 +15,7 @@ use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select; use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput; use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification; use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Log;
class WireMasterPrint extends Page class WireMasterPrint extends Page
{ {
@@ -77,41 +78,66 @@ class WireMasterPrint extends Page
return []; return [];
} }
return CustomerPoMaster::where('plant_id', $plantId)->pluck('customer_po', 'id'); return CustomerPoMaster::where('plant_id', $plantId)->distinct()->pluck('customer_po', 'customer_po'); //->pluck('customer_po', 'id'); ->distinct()
}) })
->required(), ->required(),
select::make('scan_pallet_no') Select::make('scan_pallet_no')
->label('Scan Pallet No') ->label('Scan Pallet No')
->reactive() ->reactive()
->searchable()
->options(function ($get) { ->options(function ($get) {
$plantId = $get('plant_id'); // $plantId = $get('plant_id');
$customerPoId = $get('customer_po_master_id'); // $customerPoId = $get('customer_po_master_id');
if (! $plantId || ! $customerPoId) { // if (! $plantId || ! $customerPoId) {
// return [];
// }
// $palletNumbers = WireMasterPacking::query()
// ->select('wire_packing_number')
// ->where('plant_id', $plantId)
// ->where('customer_po_master_id', $customerPoId)
// ->whereNotNull('wire_packing_number')
// ->groupBy('wire_packing_number')
// ->havingRaw('COUNT(*) = COUNT(wire_packing_status)')
// ->havingRaw("SUM(CASE WHEN TRIM(wire_packing_status) = '' THEN 1 ELSE 0 END) = 0")
// ->orderBy('wire_packing_number', 'asc')
// ->pluck('wire_packing_number')
// ->toArray();
// return collect($palletNumbers)
// ->mapWithKeys(fn ($number) => [$number => $number])
// ->toArray();
//..New Logic
$plantId = $get('plant_id');
$customerPo = $get('customer_po_master_id');
if (! $plantId || ! $customerPo)
{
return []; return [];
} }
$poIds = CustomerPoMaster::where('plant_id', $plantId)->where('customer_po', $customerPo)->pluck('id');
$palletNumbers = WireMasterPacking::query() $palletNumbers = WireMasterPacking::query()
->select('wire_packing_number')
->where('plant_id', $plantId) ->where('plant_id', $plantId)
->where('customer_po_master_id', $customerPoId) ->whereIn('customer_po_master_id', $poIds)
->whereNotNull('wire_packing_number') ->whereNotNull('wire_packing_number')
->groupBy('wire_packing_number') ->groupBy('wire_packing_number')
->havingRaw('COUNT(*) = COUNT(wire_packing_status)') ->havingRaw('COUNT(*) = COUNT(wire_packing_status)')
->havingRaw("SUM(CASE WHEN TRIM(wire_packing_status) = '' THEN 1 ELSE 0 END) = 0") ->havingRaw("SUM(CASE WHEN TRIM(wire_packing_status) = '' THEN 1 ELSE 0 END) = 0")
->orderBy('wire_packing_number', 'asc') ->orderBy('wire_packing_number')
->pluck('wire_packing_number') ->pluck('wire_packing_number')
->toArray(); ->toArray();
return collect($palletNumbers) ->mapWithKeys(fn ($number) => [$number => $number]) ->toArray();
return collect($palletNumbers)
->mapWithKeys(fn ($number) => [$number => $number])
->toArray();
}) })
->afterStateUpdated(function ($state, callable $set, $get) { ->afterStateUpdated(function ($state, callable $set, $get) {
$palletNo = $state; $palletNo = $state;
$plantId = $get('plant_id'); $plantId = $get('plant_id');
$this->dispatch('loadData', $palletNo, $plantId); $this->dispatch('loadData', $palletNo, $plantId);
}) })
->extraAttributes([ ->extraAttributes([

View File

@@ -19,168 +19,9 @@ 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 + 3.050,
'netWeight' => $grossWeight,
// '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) // public function print(Request $request, $pallet, $plant)
// { // {
// $customerId = $request->query('customer'); // $customerId = $request->query('customer');
// $items = WireMasterPacking::with('item') // $items = WireMasterPacking::with('item')
@@ -205,6 +46,29 @@ class PalletPrintController extends Controller
// $customerCode = $customer->customer_po ?? ''; // $customerCode = $customer->customer_po ?? '';
// $customerName = $customer->customer_name ?? ''; // $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) // $totalBoxes = WireMasterPacking::where('plant_id', $plant)
// ->where('customer_po_master_id', $customerId) // ->where('customer_po_master_id', $customerId)
// ->distinct('wire_packing_number') // ->distinct('wire_packing_number')
@@ -250,43 +114,157 @@ class PalletPrintController extends Controller
// $plantAddress = Plant::where('id', $plant)->value('address'); // $plantAddress = Plant::where('id', $plant)->value('address');
// $qrBase64 = $this->generateQrDataUri($pallet); // $pdf = Pdf::loadView('pdf.wire-pallet', [
// $mpdf = new Mpdf([
// 'format' => [85, 100],
// 'margin_left' => 0,
// 'margin_right' => 0,
// 'margin_top' => 0,
// 'margin_bottom' => 0,
// 'tempDir' => '/var/www/tmp/mpdf'
// ]);
// $html = view('pdf.wire-pallet', [
// 'product' => 'Submersible Winding Wire', // 'product' => 'Submersible Winding Wire',
// 'plantName' => $plantName, // 'plantName' => $plantName,
// 'plantAddress' => $plantAddress, // 'plantAddress' => $plantAddress,
// 'monthYear' => now()->format('M-y'), // 'monthYear' => now()->format('M-y'),
// 'branch' => '',
// 'customerCode' => $customerCode, // 'customerCode' => $customerCode,
// 'customerName' => $customerName, // 'customerName' => $customerName,
// 'masterBox' => $pallet, // 'masterBox' => $boxLabel,
// 'items' => $items, // 'items' => $items,
// 'grossWeight' => $grossWeight, // 'grossWeight' => $grossWeight + 3.050,
// 'netWeight' => $grossWeight - 3.05, // 'netWeight' => $grossWeight,
// // 'grossWeight' => $grossWeight,
// // 'netWeight' => $grossWeight - 3.05,
// 'pallet' => $pallet, // 'pallet' => $pallet,
// 'qrBase64' => $qrBase64, // ])->setPaper([0, 0, $widthPt, $heightPt], 'portrait');
// ])->render();
// $mpdf->WriteHTML($html); // return $pdf->stream("Pallet-{$pallet}.pdf");
// return $mpdf->Output("Pallet-{$pallet}.pdf", 'I'); // 'I' = inline view in browser // // $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);
// // }
// } // }
// private function generateQrDataUri(string $data): string /**
// { * Store a newly created resource in storage.
// $qr = new \Mpdf\QrCode\QrCode($data, 'H'); */
// $output = new \Mpdf\QrCode\Output\Png();
// $pngData = $output->output($qr, 80, [255, 255, 255], [0, 0, 0]); public function print(Request $request, $pallet, $plant)
// return 'data:image/png;base64,' . base64_encode($pngData); {
// }
$customerPo = $request->query('customer');
$itemId = WireMasterPacking::where('plant_id', $plant)
->where('wire_packing_number', $pallet)
->value('item_id');
$customerPoIds = CustomerPoMaster::where('plant_id', $plant)
->where('customer_po', $customerPo)
->where('item_id', $itemId)
->pluck('id');
$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 ?? '';
// $totalBoxes = WireMasterPacking::where('plant_id', $plant)
// ->where('customer_po_master_id', $customerId)
// ->distinct('wire_packing_number')
// ->count('wire_packing_number');
$totalBoxes = WireMasterPacking::where('plant_id', $plant)
->whereIn('customer_po_master_id', $customerPoIds)
->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();
$completedPallets = WireMasterPacking::where('plant_id', $plant)
->whereIn('customer_po_master_id', $customerPoIds)
->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 : 1;
$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 + 3.050,
'netWeight' => $grossWeight,
'pallet' => $pallet,
])->setPaper([0, 0, $widthPt, $heightPt], 'portrait');
return $pdf->stream("Pallet-{$pallet}.pdf");
}
public function store(Request $request) public function store(Request $request)
{ {