259 lines
11 KiB
PHP
Executable File
259 lines
11 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\M3\Console;
|
|
|
|
use Ayeo\Barcode;
|
|
use Cache;
|
|
use Config;
|
|
use DB;
|
|
use Dompdf\Dompdf;
|
|
use File;
|
|
use Illuminate\Console\Command;
|
|
use Log;
|
|
use PDF;
|
|
use Picqer\Barcode\BarcodeGeneratorPNG;
|
|
use Str;
|
|
|
|
class GenerateGS1Cmd extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'command:GenerateGS1Cmd {user?} {--gsarray=}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Generate GS1-128 Label';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
ini_set('max_execution_time', Config::get('arrays.PHPTimeOutCL'));
|
|
ini_set('max_input_time', Config::get('arrays.PHPTimeOutCL'));
|
|
ini_set('default_socket_timeout', Config::get('arrays.PHPTimeOutCL'));
|
|
ini_set('memory_limit', '1024M');
|
|
|
|
try {
|
|
$this->info('>>> Run GS1 Label creation Command...');
|
|
Log::channel('LabelCron')->info('>>> Run GS1 Label creation Command...');
|
|
|
|
// Get the Calling User
|
|
$userId = $this->argument('user');
|
|
$sHUSER = $userId ?? (m3_user() !== '' ? Str::upper(trim(m3_user())) : 'TWOSHIP');
|
|
$this->info('User: ' . $sHUSER);
|
|
|
|
// Get Manifest Array
|
|
$gSArray1 = $this->option('gsarray');
|
|
$gSArray = json_decode($gSArray1, true);
|
|
|
|
// MAK : Used for debug only
|
|
$gSArray = ['Macpkr' => '55555', 'Recipient_Address1' => '9571 Pan American DR', 'Recipient_Address2' => 'C/O AEE Solar', 'Recipient_City' => 'El Paso',
|
|
'Recipient_CompanyName' => 'SNAPRACK EL PASO PRODUCTION', 'Recipient_CountryCode' => 'US', 'Recipient_PostalCode' => '79927', 'Recipient_State' => 'TX',
|
|
'Sender_Address1' => '6800 Arnold Miller Pkwy', 'Sender_Address2' => '', 'Sender_City' => 'Solon',
|
|
'Sender_CompanyName' => 'Erico International', 'Sender_Country' => 'US', 'Sender_PostalCode' => '44139', 'Sender_State' => 'OH',
|
|
'CarrierName' => 'R&L Carriers', 'trackingno' => '306539838', 'BOLnumber' => '693103848064454', 'ShipmentPONumber' => '4523039092',
|
|
'PackagesArray' => ['package 1', 'package 2'],
|
|
];
|
|
|
|
if ($sHUSER != '') {
|
|
// Get User CO & WH from Table ICM428M
|
|
$iCM428M = DB::connection(config('m3.connection', 'pgsql'))->table('icm428m')
|
|
->select('R428US', 'R428CO', 'R428WH', 'R428PL')
|
|
->where('R428US', '=', $sHUSER)
|
|
->first();
|
|
|
|
$cOMPANY = isset($iCM428M->R428CO) ? $iCM428M->R428CO : ''; // Company
|
|
$wHS = isset($iCM428M->R428WH) ? $iCM428M->R428WH : ''; // WareHouse
|
|
|
|
$macpkr = $gSArray['Macpkr'];
|
|
$packageType = '0';
|
|
$manufactID = '0826528'; // MAK : get ID from Nvent and save to Config File
|
|
$total_length = 9;
|
|
|
|
$html = '
|
|
<html>
|
|
<head>
|
|
<style>
|
|
.border_btm {
|
|
border-bottom: 1px solid;
|
|
}
|
|
.border_rght {
|
|
border-right: 1px solid;
|
|
}
|
|
|
|
.margin_t {
|
|
padding-top : 10px;
|
|
}
|
|
|
|
.align_lft {
|
|
text-align: left;
|
|
}
|
|
.align_center {
|
|
text-align: center;
|
|
}
|
|
|
|
@page { margin: 5px; }
|
|
body { margin: 0px; }
|
|
</style>
|
|
</head>
|
|
<body style="font-family: Arial, sans-serif;">';
|
|
|
|
// Get number of Pallets
|
|
$packageCount = count($gSArray['PackagesArray']);
|
|
$packageNum = 1;
|
|
while ($packageNum <= $packageCount) {
|
|
$table_style = $packageNum < $packageCount ? ' style="page-break-after: always;" ' : '';
|
|
|
|
if (Cache::has('GS1SEQUENCE')) {
|
|
$gS1SEQUENCE = Cache::get('GS1SEQUENCE');
|
|
$gS1SEQUENCE++;
|
|
$gS1SEQUENCEID = str_pad($gS1SEQUENCE, $total_length, '0', STR_PAD_LEFT);
|
|
}
|
|
// Box Number Not Found Get it from DB
|
|
else {
|
|
$gS1SEQUENCE = 1;
|
|
$gS1SEQUENCEID = str_pad($gS1SEQUENCE, $total_length, '0', STR_PAD_LEFT);
|
|
}
|
|
Cache::forever('GS1SEQUENCE', $gS1SEQUENCEID);
|
|
$this->info('>>> GS1 sequence Id: ' . $gS1SEQUENCEID);
|
|
|
|
$gs1_128_bc_1 = '00' . $packageType . $manufactID . $gS1SEQUENCEID;
|
|
|
|
// Calculate Check Digit
|
|
$digits = str_split($gs1_128_bc_1);
|
|
$sum = 0;
|
|
$length = count($digits);
|
|
|
|
// Loop through each digit starting from the right
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$digit = $digits[$length - $i - 1];
|
|
// Assign a weight of 3 or 1 alternately
|
|
$weight = ($i % 2 == 0) ? 3 : 1;
|
|
// Add the product of the digit and its weight to the sum
|
|
$sum += $digit * $weight;
|
|
}
|
|
// Calculate the remainder when sum is divided by 10
|
|
$remainder = $sum % 10;
|
|
// If remainder is 0, check digit is 0; otherwise, subtract remainder from 10
|
|
$checkDigit = ($remainder == 0) ? 0 : 10 - $remainder;
|
|
|
|
$gs1_128_bc_desc = '(00)' . $packageType . $manufactID . $gS1SEQUENCEID . $checkDigit;
|
|
|
|
$data = [
|
|
'ship_to' => $gSArray['Recipient_CompanyName'] . '<br>' . $gSArray['Recipient_Address1'] . '<br>' .
|
|
$gSArray['Recipient_City'] . ',' . $gSArray['Recipient_State']
|
|
. '<br>' . $gSArray['Recipient_PostalCode'],
|
|
'from' => $gSArray['Sender_CompanyName'] . '<br>' . $gSArray['Sender_Address1'] . '<br>' .
|
|
$gSArray['Sender_City'] . ',' . $gSArray['Sender_State']
|
|
. '<br>' . $gSArray['Sender_PostalCode'],
|
|
'ship_to_lbl' => '420' . $gSArray['Recipient_PostalCode'],
|
|
'ship_to_desc' => '(420)' . $gSArray['Recipient_PostalCode'],
|
|
'pro' => '190071217845706',
|
|
'item_number' => '10000B',
|
|
'upc' => '098506095310',
|
|
'quantity' => '1',
|
|
'serial_shipping_container' => $gs1_128_bc_desc,
|
|
];
|
|
|
|
// Initialize barcode generator
|
|
$generator = new BarcodeGeneratorPNG;
|
|
|
|
// Generate barcodes
|
|
$shipToPostalCodeBarcode = $generator->getBarcode($data['ship_to_lbl'], $generator::TYPE_CODE_128);
|
|
$serialShippingContainerBarcode = $generator->getBarcode($data['serial_shipping_container'], $generator::TYPE_CODE_128, 2, 50);
|
|
|
|
// HTML content for the label
|
|
$html .= '
|
|
<table ' . $table_style . '>
|
|
<tr>
|
|
<td colspan="2" class="border_btm border_rght align_lft">
|
|
FROM:<br>' . $data['from'] . '
|
|
</td>
|
|
<td colspan="2" class="border_btm align_lft">
|
|
SHIP TO:<br>' . $data['ship_to'] . '
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td colspan="3" class="border_btm border_rght margin_t align_center">
|
|
<img src="data:image/png;base64,' . base64_encode($shipToPostalCodeBarcode) . '"> <br>
|
|
' . $data['ship_to_desc'] . '
|
|
</td>
|
|
<td class="border_btm margin_t align_lft">
|
|
Carrier: ' . $gSArray['CarrierName'] . ' <br>
|
|
PRO #: ' . $gSArray['ShipmentPONumber'] . ' <br>
|
|
B/L #: ' . $gSArray['BOLnumber'] . '
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td colspan="2" class="border_btm margin_t align_lft border_rght">
|
|
PURCHASE ORDER: ' . $gSArray['Macpkr'] . ' <br>
|
|
Vendor: ' . $manufactID . '
|
|
</td>
|
|
<td colspan="2" class="border_btm margin_t align_center">
|
|
Pallet # : ' . $packageNum . ' of ' . $packageCount . '
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td colspan="4" style="text-align: center;" class="margin_t">
|
|
<img src="data:image/png;base64,' . base64_encode($serialShippingContainerBarcode) . '"><br>
|
|
' . $data['serial_shipping_container'] . '
|
|
</td>
|
|
</tr>
|
|
</table>';
|
|
|
|
$packageNum++;
|
|
}
|
|
|
|
$html .= '</body> </html>';
|
|
|
|
ob_start();
|
|
|
|
// Initialize Dompdf
|
|
$dompdf = new Dompdf;
|
|
$dompdf->loadHtml($html);
|
|
$dompdf->setPaper('a6', 'portrait');
|
|
// Render the HTML as PDF
|
|
$dompdf->render();
|
|
|
|
// Output the generated PDF to Browser
|
|
// $dompdf->stream();
|
|
|
|
$pdfContent = $dompdf->output();
|
|
|
|
$labelsPath = Config::get('arrays.PdfPath');
|
|
// Put the PDF in labels/CarrierLabel
|
|
$gSFolder = 'GSLabel';
|
|
$carrierFileName = $gSFolder . '_' . $macpkr . '_' . $cOMPANY . '_' . $wHS;
|
|
file_put_contents($labelsPath . $gSFolder . '/' . $carrierFileName . '.pdf', $pdfContent);
|
|
|
|
// Check if the PDF was generated correctly
|
|
// if (!file_exists($tempPdfPath) || filesize($tempPdfPath) === 0) {
|
|
// throw new Exception('PDF generation failed.');
|
|
// }
|
|
|
|
ob_end_clean();
|
|
}
|
|
|
|
Log::channel('LabelCron')->info('<<< GS1-128...End of Label Creation');
|
|
$this->info('<<< GS1-128...End of Label Creation');
|
|
|
|
} catch (\Throwable $th) {
|
|
Log::channel('LabelCron')->error($th);
|
|
}
|
|
}
|
|
}
|