Files
pds/app/Http/Controllers/TestingPanelController.php
2025-07-01 14:04:34 +05:30

772 lines
30 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Item;
use App\Models\MotorTestingMaster;
use App\Models\Plant;
use App\Models\TestingPanelReading;
use DB;
use Filament\Notifications\Notification;
use Illuminate\Http\Request;
use Mpdf\Mpdf;
use chillerlan\QRCode\QROptions;
use chillerlan\QRCode\Output\QROutputInterface;
use Mpdf\QrCode\Output;
use Mpdf\QrCode\QrCode;
class TestingPanelController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
public function downloadQrPdf($palletNo)
{
// Generate QR code as PNG binary (adjust size as needed)
$qrCode = new QrCode($palletNo);
$output = new Output\Png();
$qrBinary = $output->output($qrCode, 100); // 100px size
$qrBase64 = base64_encode($qrBinary);
// Prepare HTML with embedded QR code
$html = '
<html>
<head>
<style>
body { margin: 0; padding: 0; width: 60mm; height: 14mm; font-size: 10pt; font-family: DejaVu Sans, sans-serif; }
.sticker-table { width: 60mm; height: 14mm; border-collapse: collapse; }
.qr-cell { width: 14mm; text-align: left; vertical-align: middle; padding-left: 5mm; padding-top: 2mm; }
.text-cell { text-align: left; vertical-align: middle; font-size: 12pt; padding-left: 7mm; padding-top: 2mm; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: bold; }
img.qr { width: 10mm; height: 10mm; display: block; }
</style>
</head>
<body>
<table class="sticker-table">
<tr>
<td class="qr-cell">
<img class="qr" src="data:image/png;base64,' . $qrBase64 . '" alt="QR" />
</td>
<td class="text-cell">
' . htmlspecialchars($palletNo) . '
</td>
</tr>
</table>
</body>
</html>
';
$mpdf = new Mpdf([
'mode' => 'utf-8',
'format' => [60, 14],
'margin_left' => 0,
'margin_right' => 0,
'margin_top' => 0,
'margin_bottom' => 0,
'tempDir' => '/var/www/storage/mpdf-tmp',
]);
$mpdf->WriteHTML($html);
$mpdf->Output('qr-label.pdf', 'I');
exit;
}
// public function downloadQrPdf($palletNo)
// {
// $qrBinary = QrCode::format('png')->size(70)->generate($palletNo); // Adjust size as needed
// $qrBase64 = base64_encode($qrBinary);
// $html = '
// <html>
// <head>
// <style>
// body {
// margin: 0;
// padding: 0;
// width: 60mm;
// height: 14mm;
// font-size: 10pt;
// font-family: DejaVu Sans, sans-serif;
// }
// .sticker-table {
// width: 60mm;
// height: 14mm;
// border-collapse: collapse;
// }
// .qr-cell {
// width: 14mm;
// text-align: left;
// vertical-align: middle;
// padding-left: 5mm;
// padding-top: 2mm;
// }
// .text-cell {
// text-align: left;
// vertical-align: middle;
// font-size: 12pt;
// padding-left: 7mm;
// padding-top: 2mm;
// white-space: nowrap;
// overflow: hidden;
// text-overflow: ellipsis;
// font-weight: bold;
// }
// img.qr {
// width: 10mm;
// height: 10mm;
// display: block;
// }
// </style>
// </head>
// <body>
// <table class="sticker-table">
// <tr>
// <td class="qr-cell">
// <img class="qr" src="data:image/png;base64,' . $qrBase64 . '" alt="QR" />
// </td>
// <td class="text-cell">
// ' . htmlspecialchars($palletNo) . '
// </td>
// </tr>
// </table>
// </body>
// </html>
// ';
// $mpdf = new Mpdf([
// 'mode' => 'utf-8',
// 'format' => [60, 14],
// 'margin_left' => 0,
// 'margin_right' => 0,
// 'margin_top' => 0,
// 'margin_bottom' => 0,
// ]);
// $mpdf->WriteHTML($html);
// $mpdf->Output(); // This directly flushes output as PDF
// exit;
// }
// public function downloadQrPdf($palletNo)
// {
// // 1. Generate TSPL commands
// $labelContent = <<<EOT
// SIZE 60 mm,14 mm
// GAP 2 mm,0 mm
// DIRECTION 1
// CLS
// QRCODE 20,20,L,5,A,0,"$palletNo"
// TEXT 160,30,"0",0,12,12,"$palletNo"
// PRINT 1
// EOT;
// $filePath = '/tmp/labelprint.txt';
// file_put_contents($filePath, $labelContent);
// //$printerShare = '//LASER-STANDBY/TSC TTP';
// $tempFile = '/tmp/labelprint.txt';
// $printerIp = '172.31.31.250'; // Printer server IP
// $username = 'admin'; // SMB username
// $password = 'admin'; // SMB password (can be empty, but better with password)
// $printerShare = "//$printerIp/TSC_TTP_244 Pro"; // SMB share path
// $tempFile = $filePath;
// $command = "smbclient \"$printerShare\" -U \"$username%$password\" -c \"put $tempFile labelprint.txt\"";
// exec($command, $output, $status);
// // $tempFile = '/tmp/labelprint.txt';
// // $printerName = 'TSC_TTP_244_Pro'; // Replace with your actual CUPS printer queue name
// // $command = "lp -d $printerName -o raw $tempFile";
// // exec($command, $output, $status);
// if ($status === 0) {
// return response("Print sent to printer.");
// } else {
// return response("Print failed. Output: " . implode("\n", $output));
// }
// }
// public function downloadQrPdf($palletNo)
// {
// $tspl = <<<EOT
// SIZE 40 mm,30 mm
// GAP 2 mm,0 mm
// CLS
// TEXT 50,50,"0",0,1,1,"Hello, TSC!"
// PRINT 1
// EOT;
// // Save TSPL to a temporary file
// $tmpFile = tempnam(sys_get_temp_dir(), 'tsc_') . '.tspl';
// file_put_contents($tmpFile, $tspl);
// // Print using CUPS (replace with your actual printer name)
// $printerName = "TSC TTP-244 Pro"; // Example, use the exact name from CUPS
// $command = "lp -d \"$printerName\" \"$tmpFile\" 2>&1";
// exec($command, $output, $return_var);
// $outputStr = implode("\n", $output);
// unlink($tmpFile);
// if ($return_var === 0) {
// return "Label sent to printer!";
// }
// else
// {
// return "Failed to send label to printer. Error: " . $outputStr;
// }
// }
// public function downloadQrPdf($palletNo)
// {
// // TSPL commands for test label
// $labelContent = <<<EOT
// SIZE 60 mm,14 mm
// GAP 3 mm,0 mm
// OFFSET 0 mm
// DIRECTION 1
// CLS
// QRCODE 35,21,L,4,A,0,"$palletNo"
// TEXT 180,50,"4",0,1,1,"$palletNo"
// PRINT 1
// EOT;
// //A : error correction level:
// $escapedContent = escapeshellarg($labelContent);
// $printerName = '4';
// $command = "echo $escapedContent | lp -d \"$printerName\" -o raw";
// exec($command, $output, $status);
// if ($status === 0) {
// return response("Test print sent to printer successfully.");
// } else {
// return response("Print failed. Output: " . implode("\n", $output));
// }
// }
// public function downloadQrPdf($palletNo)
// {
// // TSPL label content (no indentation in heredoc)
// $labelContent = <<<EOT
// SIZE 60 mm,14 mm
// GAP 2 mm,0 mm
// DIRECTION 1
// CLS
// TEXT 10,10,"1",0,6,6,"Test"
// PRINT 1
// EOT;
// $printerName = '4';
// //TEXT x, y, font, rotation, x_multiplication, y_multiplication, "text"
// // $command = "echo \"$labelContent\" | lp -d \"$printerName\" -o raw";
// // exec($command, $output, $status);
// // Save label content to a temp file
// $tempFile = sys_get_temp_dir() . '/label.txt';
// file_put_contents($tempFile, $labelContent);
// // Send the temp file to the printer
// $command = "cat " . escapeshellarg($tempFile) . " | lp -d " . escapeshellarg($printerName) . " -o raw";
// exec($command, $output, $status);
// // Delete the temp file
// unlink($tempFile);
// if ($status === 0) {
// return response("TSPL command sent to printer.");
// } else {
// return response("TSPL print failed. Output: " . implode("\n", $output));
// }
// }
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$expectedUser = env('API_AUTH_USER');
$expectedPw = env('API_AUTH_PW');
$header_auth = $request->header('Authorization');
$expectedToken = $expectedUser . ':' . $expectedPw;
if("Bearer " . $expectedToken !== $header_auth)
{
return response("Unauthorized", 403)
->header('Content-Type', 'text/plain');
}
$data = $request->all();
if ($data['plant_code'] == '' || !ctype_digit($data['plant_code'])) {
// return response("ERROR: Please provide a valid plant code.", 400)
// ->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Please provide a valid plant code.'
], 400);
}
$plant = Plant::where('code', $data['plant_code'])->first();
if (!$plant) {
//return response("Plant not found.", 400)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Plant not found.'
], 400);
}
$plantId = $plant->id;
$line = \App\Models\Line::where('name', $data['line_name'])->first();
if (!$line)
{
//return response("Line not found.", 400)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Line not found.'
], 400);
}
$line = \App\Models\Line::where('name', $data['line_name'])
->where('plant_id', $plantId)
->first();
if (!$line)
{
//return response( "Line not found for the specified plant : {$data['plant_code']}",400)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Line not found for the specified plant : {$data['plant_code']}."
], 400);
}
$lineId = $line->id;
$machine = \App\Models\Machine::where('name', $data['machine_name'])
->first();
if (!$machine)
{
// return response('Machine not found', 400)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Machine not found.'
], 400);
}
$machine = \App\Models\Machine::where('name', $data['machine_name'])
->where('plant_id', $plantId)
->first();
if (!$machine)
{
// return response("Machine not found for the specified plant : {$data['plant_code']}", 400)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Machine not found for the specified plant : {$data['plant_code']}."
], 400);
}
$machine = \App\Models\Machine::where('name', $data['machine_name'])
->where('line_id', $lineId)
->first();
if (!$machine)
{
// return response("Machine not found for the specified line : {$data['line_name']}", 400)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Machine not found for the specified line : {$data['line_name']}."
], 400);
}
$machine = \App\Models\Machine::where('name', $data['machine_name'])
->where('plant_id', $plantId)
->where('line_id', $lineId)
->first();
if (!$machine) {
// return response('Machine not found for the specified plant and line', 400)
// ->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Machine not found for the specified plant and line.'
], 400);
}
$machineId = $machine->id;
try
{
$insertedSerials = [];
$missedItemCodes = [];
$duplicateItemCodes = [];
$existSnoCount = [];
if (!empty($data['item_codes']) && is_array($data['item_codes']))
{
foreach ($data['item_codes'] as $item)
{
$code = $item['item_code'] ?? null;
// Check if item_code is present
if ($code == '')
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Item code cannot be empty.'
], 400);
}
// Collect duplicates
if (isset($itemCodeCounts[$code]))
{
$itemCodeCounts[$code]++;
// Only add to duplicates array once
if ($itemCodeCounts[$code] == 2) {
$duplicateItemCodes[] = $code;
}
}
else
{
$itemCodeCounts[$code] = 1;
}
$motorTestingMaster = MotorTestingMaster::whereHas('item', function ($query) use ($item) {
$query->where('code', $item['item_code']);
})->where('plant_id', $plantId)->first();
if (!$motorTestingMaster) {
$missedItemCodes[] = $item['item_code'];
}
if (!empty($item['serial_numbers']) && is_array($item['serial_numbers'])) {
foreach ($item['serial_numbers'] as $serial)
{
$existSnoCount[] = $serial['serial_number'];
}
}
}
// If any duplicates found, return error
if (!empty($duplicateItemCodes)) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Duplicate item codes found in request: ' . implode(', ', $duplicateItemCodes)
], 400);
}
$uniqueInvalidCodes = array_unique($missedItemCodes);
if (!empty($uniqueInvalidCodes)) {
// return response("Item codes : ". implode(', ', $uniqueInvalidCodes)." not found in motor testing master for the specified plant {$plant->name}", 400)
// ->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Item codes : ". implode(', ', $uniqueInvalidCodes)." not found in master for the specified plant : {$plant->name}"
], 400);
}
$insertedSnoCount = [];
foreach ($data['item_codes'] as $item)
{
$motorTestingMaster = MotorTestingMaster::whereHas('item', callback: function ($query) use ($item) {
$query->where('code', $item['item_code']);
})->where('plant_id', $plantId)->first();
$motorTestingMasterId = $motorTestingMaster->id;
if (!empty($item['serial_numbers']) && is_array($item['serial_numbers']))
{
foreach ($item['serial_numbers'] as $serial)
{
//For update_count calculation
$updateCount = [
'plant_id' => $plantId,
'line_id' => $lineId,
'machine_id' => $machineId,
'motor_testing_master_id' => $motorTestingMasterId,
'serial_number' => $serial['serial_number'] ?? null,
'rework_count' => $serial['rework_count'] ?? 0,
];
// Find the current max update_count for this composite key
// $maxUpdateCount = \App\Models\TestingPanelReading::where($updateCount)->max('update_count');
$maxUpdateCount = TestingPanelReading::where($updateCount)
->select(TestingPanelReading::raw('MAX(CAST(update_count AS INTEGER)) AS max_update_count'))
->value('max_update_count');
$newUpdateCount = ($maxUpdateCount === null || $maxUpdateCount === '') ? 0 : $maxUpdateCount + 1;
$updateCountString = (string) $newUpdateCount;
$row =
[
'plant_id' => $plantId,
'line_id' => $lineId,
'machine_id' => $machineId,
'motor_testing_master_id'=> $motorTestingMasterId,
'serial_number' => $serial['serial_number'] ?? null,
'output' => $serial['output'] ?? null,
'before_fr_volt' => $serial['before_fr_volt'] ?? null,
'before_fr_cur' => $serial['before_fr_cur'] ?? null,
'before_fr_pow' => $serial['before_fr_pow'] ?? null,
'before_fr_res_ry' => $serial['before_fr_res_ry'] ?? null,
'before_fr_res_yb' => $serial['before_fr_res_yb'] ?? null,
'before_fr_res_br' => $serial['before_fr_res_br'] ?? null,
'before_fr_ir' => $serial['before_fr_ir'] ?? null,
'before_fr_ir_r' => $serial['before_fr_ir_r'] ?? null,
'before_fr_ir_y' => $serial['before_fr_ir_y'] ?? null,
'before_fr_ir_b' => $serial['before_fr_ir_b'] ?? null,
'before_fr_freq' => $serial['before_fr_freq'] ?? null,
'before_fr_speed' => $serial['before_fr_speed'] ?? null,
'after_fr_vol' => $serial['after_fr_vol'] ?? null,
'after_fr_cur' => $serial['after_fr_cur'] ?? null,
'after_fr_pow' => $serial['after_fr_pow'] ?? null,
'after_fr_ir_hot' => $serial['after_fr_ir_hot'] ?? null,
'after_fr_ir_hot_r' => $serial['after_fr_ir_hot_r'] ?? null,
'after_fr_ir_hot_y' => $serial['after_fr_ir_hot_y'] ?? null,
'after_fr_ir_hot_b' => $serial['after_fr_ir_hot_b'] ?? null,
'after_fr_ir_cool' => $serial['after_fr_ir_cool'] ?? null,
'after_fr_ir_cool_r' => $serial['after_fr_ir_cool_r'] ?? null,
'after_fr_ir_cool_y' => $serial['after_fr_ir_cool_y'] ?? null,
'after_fr_ir_cool_b' => $serial['after_fr_ir_cool_b'] ?? null,
'after_fr_freq' => $serial['after_fr_freq'] ?? null,
'after_fr_speed' => $serial['after_fr_speed'] ?? null,
'after_fr_leak_cur' => $serial['after_fr_leak_cur'] ?? null,
'locked_rt_volt' => $serial['locked_rt_volt'] ?? null,
'locked_rt_cur' => $serial['locked_rt_cur'] ?? null,
'locked_rt_pow' => $serial['locked_rt_pow'] ?? null,
'no_load_pickup_volt' => $serial['no_load_pickup_volt'] ?? null,
'room_temperature' => $serial['room_temperature'] ?? null,
'hv_test' => $serial['hv_test'] ?? null,
'batch_number' => $serial['batch_number'] ?? null,
'batch_count' => $serial['batch_count'] ?? 0,
'result' => $serial['result'] ?? null,
'remark' => $serial['remark'] ?? null,
'rework_count' => $serial['rework_count'] ?? 0,
'output_flag' => $serial['output_flag'] ?? 0,
'tested_by' => $serial['tested_by'] ?? null,
'updated_by' => $serial['updated_by'] ?? null,
'created_at' => $serial['created_at'] ?? null,
'updated_at' => $serial['updated_at'] ?? $serial['created_at'],
'scanned_at' => $serial['scanned_at'] ?? null,
'update_count' => $updateCountString,
];
// Insert the new record
TestingPanelReading::create($row);
$insertedSerials[] = $serial['serial_number'] ?? '[unknown]';
$insertedSnoCount[] = $serial['serial_number'];
}
}
}
}
if (!empty($insertedSerials))
{
if(count($existSnoCount) == count($insertedSnoCount))
{
// $messages[] = "Inserted serial numbers are: " . implode(', ', $insertedSerials);
return response()->json([
'status_code' => 'SUCCESS',
'status_description' => 'Inserted serial numbers are: ' . implode(', ', $insertedSerials)
], 200);
}
else
{
// $missingSno = array_diff($existSnoCount,$insertedSnoCount);
// $messages[] = "Missed serial numbers are: " . implode(', ', $missingSno);
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Missed serial numbers are: " . implode(', ', $missingSno)'
], 400);
}
}
// return response("" . implode("\n", $messages), 200)
// ->header('Content-Type', 'text/plain');
}
catch (\Exception $e)
{
// return response($e->getMessage(), 500)->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Internal Sever Error : '.$e?->getCode()
], 500);
}
}
/**
* Display the specified resource.
*/
public function get_motor_master(Request $request)
{
$expectedUser = env('API_AUTH_USER');
$expectedPw = env('API_AUTH_PW');
$header_auth = $request->header('Authorization');
$expectedToken = $expectedUser . ':' . $expectedPw;
if("Bearer " . $expectedToken !== $header_auth)
{
return response("Unauthorized", 403)
->header('Content-Type', 'text/plain');
}
//$data = $request->all();
if ("Bearer " . $expectedToken !== $header_auth)
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Invalid authorization token'
], 403);
}
$plantCode = $request->header('plant-code');
$itemCode = $request->header('item-code');
// $description = $item ? $item->description : '';
// Fetch item by code
$item = Item::where('code', $itemCode)->first();
// Get description or empty string if not found
$description = $item ? $item->description : '';
$category = $item ? $item->category : '';
if ($plantCode === null || $plantCode === '')
{
// return response("ERROR: Plant Name can't be empty", 400)
// ->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Plant Name cannot be empty"
], 400);
}
else if($itemCode == null || $itemCode == '')
{
// return response("ERROR: OBD Number can't be empty", 400)
// ->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Item Code cannot be empty"
], 400);
}
$plant = Plant::where('code', $plantCode)->first();
if (!$plant) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Plant not found"
], 400);
}
$plantId = $plant->id;
$item = Item::where('code', $itemCode)->first();
if (!$item)
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Item Code not found in item table for the plant : $plant->name"
], 404);
}
$motorTestingMaster = MotorTestingMaster::where('plant_id', $plant->id)
->where('item_id', $item->id)
->first();
if (!$motorTestingMaster)
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Item Code not found in master table for the plant : $plant->name"
], 404);
}
$output = [
"mot_model_name" => $description ?? "",
"mot_non_isi_model" => $motorTestingMaster->isi_model ? "0" :"1",
"mot_phase" => $motorTestingMaster->phase ?? "",
"mot_hp" => $motorTestingMaster->hp ?? "",
"mot_kw" => $motorTestingMaster->kw ?? "",
"mot_volt" => $motorTestingMaster->volt ?? "",
"mot_cur" => $motorTestingMaster->current ?? "",
"mot_rpm" => $motorTestingMaster->rpm ?? "",
"mot_rate_torque_kg" => $motorTestingMaster->torque ?? "",
"mot_freq" => $motorTestingMaster->frequency ?? "",
"mot_conn" => $motorTestingMaster->connection ?? "",
"mot_ins_res_limit" => $motorTestingMaster->ins_res_limit ?? "",
"mot_ins_res_type" => $motorTestingMaster->ins_res_type ?? "",
"mot_category" => $category ?? "",
"mot_routine_test_time" => $motorTestingMaster->routine_test_time ?? "",
"mot_res_ry_ll" => $motorTestingMaster->res_ry_ll ?? "",
"mot_res_ry_ul" => $motorTestingMaster->res_ry_ul ?? "",
"mot_res_yb_ll" => $motorTestingMaster->res_yb_ll ?? "",
"mot_res_yb_ul" => $motorTestingMaster->res_yb_ul ?? "",
"mot_res_br_ll" => $motorTestingMaster->res_br_ll ?? "",
"mot_res_br_ul" => $motorTestingMaster->res_br_ul ?? "",
"mot_lock_volt_limit" => $motorTestingMaster->lock_volt_limit ?? "",
"mot_leak_cur_limit" => $motorTestingMaster->leak_cur_limit ?? "",
"mot_lock_cur_ll" => $motorTestingMaster->lock_cur_ll ?? "",
"mot_lock_cur_ul" => $motorTestingMaster->lock_cur_ul ?? "",
"mot_noload_cur_ll" => $motorTestingMaster->noload_cur_ll ?? "",
"mot_noload_cur_ul" => $motorTestingMaster->noload_cur_ul ?? "",
"mot_noload_pow_ll" => $motorTestingMaster->noload_pow_ll ?? "",
"mot_noload_pow_ul" => $motorTestingMaster->noload_pow_ul ?? "",
"mot_noload_spd_ll" => $motorTestingMaster->noload_spd_ll ?? "",
"mot_noload_spd_ul" => $motorTestingMaster->noload_spd_ul ?? "",
];
return response()->json($output, 200);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}