Files
WMS/Modules/M3/Console/ClosedOrderSaveXls.php
T
2026-09-16 17:14:26 +01:00

262 lines
14 KiB
PHP

<?php
namespace Modules\M3\Console;
use Illuminate\Console\Command;
use Modules\M3\Http\Controllers\Controller;
use DB;
use Config;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Log;
use Str;
use Excel;
use Storage;
class ClosedOrderSaveXls extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:ClosedOrderSaveXls {user?} {co?} {whs?} {pl?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Save Orders Closed Lines to Excel';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
//$this->info('Start CallCloseAPI...');
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 {
Log::channel('dailyCron')->info("Cron...Start Save Orders Closed Lines to Excel");
# Get the Calling User
$userId = $this->argument('user');
$SHUSER = isset($userId) ? $userId : (m3_user() !== '' ? Str::upper(trim(m3_user())) : '');
$CO = $this->argument('co');
$COMPANY = isset($CO) ? trim($CO) : '';
$PWHS = $this->argument('whs');
$WHS = isset($PWHS) ? trim($PWHS) : '';
$PPL = $this->argument('pl');
$PLANT = isset($PPL) ? trim($PPL) : '';
# Generate last Month Report
$date = new \DateTime('FIRST DAY OF THIS MONTH');
$date->modify('-1 month');
$fromPreviousMonth = $date->format('Y-m-d');
$toPreviousMonth = $date->modify('last day of this month')->format('Y-m-d');
$reportDate = \DateTime::createFromFormat('Y-m-d', $fromPreviousMonth )->format('Ymd');
$toreportDate = \DateTime::createFromFormat('Y-m-d', $toPreviousMonth )->format('Ymd');
$reportDateWD = $fromPreviousMonth;
$toreportDateWD = $toPreviousMonth;
if ($WHS == '321') {
$prevMonthFile = 'Closedorderlinesreport_'.$reportDateWD.'-'.$toreportDateWD.'.xlsx';
}
else {
$prevMonthFile = $WHS.'/Closedorderlinesreport_'.$reportDateWD.'-'.$toreportDateWD.'-'.$WHS.'.xlsx';
}
$this->info("Generate Last Month Report...");
$this->info("From Date: ".$reportDate);
$this->info("To Date: ".$toreportDate);
# Check that the Excel file Exist
$prevMonthFileExist = Storage::disk('publicCL')->exists($prevMonthFile) ? false: true ;
$this->info("Prev Month File Exist: " .$prevMonthFileExist);
if($reportDate!='' && $COMPANY!='' && $WHS!='' && $PLANT!='' && $prevMonthFileExist ){
# Case 729: Add Currency code A1SHCURR FROM ICM666M
$ICM688M2 = DB::connection(config('m3.connection', 'pgsql'))
->select("
SELECT (SHDT || ' ' || SHTIME) AS DATECLOSED, A5MACPKR, A5ONO,
A4OFSTRM,
A5PMPWHT*A5PACKED AS WEIGHT,
A4SCAC,
A4SHIPTYPE,
(SELECT A4REFORD FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) AS A4REFORD,
A5PART, A5RSVQTY, A5PACKED, A5RSVQTY - A5PACKED AS CALCULATED_COLUMN,
A5SLUNPR * A5PACKED AS ANOTHER_CALCULATED_COLUMN,
(SELECT A4SHSHNM FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) AS A4SHSHNM ,
CASE WHEN SHDT IS NULL THEN (SELECT SHDATA FROM ICM699M WHERE SHMACPKR=A5MACPKR AND SHTYPE='M3-Res-out' AND SHSOURCE='CloseShipmentCoChargeAPI' ORDER BY SHDT DESC, SHTIME DESC FETCH FIRST 1 ROWS ONLY) ELSE '' END AS REASON,
A1SHCURR,
CASE
WHEN (SELECT A4SHIPTYPE FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) = 'LTL'
THEN (SELECT A4RATETCP FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) * ".(100.00 - Config::get('arrays.misc.discountCarrierRateForCustomerPriceLTL'))/100 ."
ELSE (SELECT A4RATETCP FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) * ".(100.00 - Config::get('arrays.misc.discountCarrierRateForCustomerPriceParcel') )/100 ."
END AS FREIGHT
FROM ICM688M2
LEFT JOIN ICM699M ON SHMACPKR=A5MACPKR AND SHTYPE='M3-Res-out' AND SHDATA LIKE 'Success%' AND SHSOURCE='CloseShipmentCoChargeAPI'
LEFT JOIN ICM666M ON A1DELNO=A5MACPKR AND A1ONO=A5ONO AND A1PKLN=A5BRADLN
LEFT JOIN ICM688M1 ON A4MACPKR=A5MACPKR
WHERE A5MACPKR IN (
SELECT DISTINCT AYMACPKR
FROM ICM695M1
WHERE AYSTATUS IN ('Z','z') AND AYAPR8N002 BETWEEN $reportDate AND $toreportDate
)
AND A5CO=$COMPANY AND A5WHS=$WHS AND A5PLANT=$PLANT
");
// $newItem = ["A5MACPKR" => 'Delivery #', "A5ONO" => 'Order #', "A4REFORD" => 'Order Type',"A5PART" => 'Part #',"A5RSVQTY" => 'Reserved',
// "A5PACKED" => 'Packed',"CALCULATED_COLUMN" => 'Back Order Qty',"ANOTHER_CALCULATED_COLUMN" => 'Value Packed $',"A4SHSHNM" => 'Customer name',
// "SHDT" => 'Date',"SHTIME" => 'Time',"REASON" => 'Failure Reason','A1SHCURR'=> 'Currency Code','FREIGHT'=> 'Freight'];
$newItem = ["DATECLOSED" => 'Close Date',"A5MACPKR" => 'Delivery #', "A5ONO" => 'Order #',
"A4OFSTRM" => 'Terms',"WEIGHT" => 'Weight',"A4SCAC" => 'Carrier',"A4SHIPTYPE" => 'Mode (Parcel, Air, TL or LTL)',
"A4REFORD" => 'Order Type',"A5PART" => 'Part #',"A5RSVQTY" => 'Reserved',
"A5PACKED" => 'Packed',"CALCULATED_COLUMN" => 'Back Order Qty',"ANOTHER_CALCULATED_COLUMN" => 'Value Packed $',"A4SHSHNM" => 'Customer name',
"REASON" => 'Failure Reason','A1SHCURR'=> 'Currency Code','FREIGHT'=> 'Freight'];
$ICM688M2Col = collect($ICM688M2);
$ICM688M2Col->prepend($newItem);
# Count records before exporting to excel : Max 55K records are allowed
$ICM688M2Count = $ICM688M2Col->count();
# Set Max allowed records
$maxRecords = Config::get('arrays.maxRecCL');
if ($ICM688M2Count>$maxRecords) {
Log::channel('dailyCron')->info('Maximum allowed records for Excel export is: '.number_format($maxRecords).' current records: '.number_format($ICM688M2Count));
}
}
else{
$ICM688M2Col = collect();
}
if ($ICM688M2Col->count()>0) {
$this->info("Saving Last Month Excel Report to Disk");
Log::channel('dailyCron')->info("Saving Last Month Excel Report to Disk");
# Store the File to Storage
app(\Modules\M3\Services\SpreadsheetExport::class)->store($ICM688M2Col, $prevMonthFile, 'publicCL');
# Other File Formats : \Maatwebsite\Excel\Excel::CSV , ,\Maatwebsite\Excel\Excel::ODS
}
else{
Log::channel('dailyCron')->info("Monthly report, no records found: Nothing to save...");
}
# Generate current Month Report
$now = new \DateTime();
$first_day_of_month = $now->format('Y-m-01');
$current_date = $now->format('Y-m-d');
$reportDate2 = \DateTime::createFromFormat('Y-m-d', $first_day_of_month )->format('Ymd');
$toreportDate2 = \DateTime::createFromFormat('Y-m-d', $current_date )->format('Ymd');
if ($WHS == '321') {
$currentMonthFile = 'first_of_month_to_current_date.xlsx';
}
else{
$currentMonthFile = $WHS.'/first_of_month_to_current_date_'.$WHS.'.xlsx';
}
$this->info("Generate Current Month Report...");
$this->info("From Date: ".$reportDate2);
$this->info("To Date : ".$toreportDate2);
if($reportDate2!='' && $COMPANY!='' && $WHS!='' && $PLANT!=''){
$this->info(">>> Proceed...");
# Case 729: Add Currency code A1SHCURR FROM ICM666M
$ICM688M2 = DB::connection(config('m3.connection', 'pgsql'))
->select("
SELECT (SHDT || ' ' || SHTIME) AS DATECLOSED, A5MACPKR, A5ONO,
A4OFSTRM,
A5PMPWHT*A5PACKED AS WEIGHT,
A4SCAC,
A4SHIPTYPE,
(SELECT A4REFORD FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) AS A4REFORD,
A5PART, A5RSVQTY, A5PACKED, A5RSVQTY - A5PACKED AS CALCULATED_COLUMN,
A5SLUNPR * A5PACKED AS ANOTHER_CALCULATED_COLUMN,
(SELECT A4SHSHNM FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) AS A4SHSHNM ,
CASE WHEN SHDT IS NULL THEN (SELECT SHDATA FROM ICM699M WHERE SHMACPKR=A5MACPKR AND SHTYPE='M3-Res-out' AND SHSOURCE='CloseShipmentCoChargeAPI' ORDER BY SHDT DESC, SHTIME DESC FETCH FIRST 1 ROWS ONLY) ELSE '' END AS REASON,
A1SHCURR,
CASE
WHEN (SELECT A4SHIPTYPE FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) = 'LTL'
THEN (SELECT A4RATETCP FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) * ".(100.00 - Config::get('arrays.misc.discountCarrierRateForCustomerPriceLTL'))/100 ."
ELSE (SELECT A4RATETCP FROM ICM688M1 WHERE A4MACPKR=A5MACPKR) * ".(100.00 - Config::get('arrays.misc.discountCarrierRateForCustomerPriceParcel') )/100 ."
END AS FREIGHT
FROM ICM688M2
LEFT JOIN ICM699M ON SHMACPKR=A5MACPKR AND SHTYPE='M3-Res-out' AND SHDATA LIKE 'Success%' AND SHSOURCE='CloseShipmentCoChargeAPI'
LEFT JOIN ICM666M ON A1DELNO=A5MACPKR AND A1ONO=A5ONO AND A1PKLN=A5BRADLN
LEFT JOIN ICM688M1 ON A4MACPKR=A5MACPKR
WHERE A5MACPKR IN (
SELECT DISTINCT AYMACPKR
FROM ICM695M1
WHERE AYSTATUS IN ('Z','z') AND AYAPR8N002 BETWEEN $reportDate2 AND $toreportDate2
)
AND A5CO=$COMPANY AND A5WHS=$WHS AND A5PLANT=$PLANT
");
// $newItem2 = ["A5MACPKR" => 'Delivery #', "A5ONO" => 'Order #', "A4REFORD" => 'Order Type',"A5PART" => 'Part #',"A5RSVQTY" => 'Reserved',
// "A5PACKED" => 'Packed',"CALCULATED_COLUMN" => 'Back Order Qty',"ANOTHER_CALCULATED_COLUMN" => 'Value Packed $',"A4SHSHNM" => 'Customer name',
// "SHDT" => 'Date',"SHTIME" => 'Time',"REASON" => 'Failure Reason','A1SHCURR'=> 'Currency Code','FREIGHT'=> 'Freight'];
$newItem2 = ["DATECLOSED" => 'Close Date',"A5MACPKR" => 'Delivery #', "A5ONO" => 'Order #',
"A4OFSTRM" => 'Terms',"WEIGHT" => 'Weight',"A4SCAC" => 'Carrier',"A4SHIPTYPE" => 'Mode (Parcel, Air, TL or LTL)',
"A4REFORD" => 'Order Type',"A5PART" => 'Part #',"A5RSVQTY" => 'Reserved',
"A5PACKED" => 'Packed',"CALCULATED_COLUMN" => 'Back Order Qty',"ANOTHER_CALCULATED_COLUMN" => 'Value Packed $',"A4SHSHNM" => 'Customer name',
"REASON" => 'Failure Reason','A1SHCURR'=> 'Currency Code','FREIGHT'=> 'Freight'];
$ICM688M2Col2 = collect($ICM688M2);
$ICM688M2Col2->prepend($newItem2);
# Count records before exporting to excel : Max 55K records are allowed
$ICM688M2Count = $ICM688M2Col2->count();
# Set Max allowed records
$maxRecords = Config::get('arrays.maxRecCL');
if ($ICM688M2Count>$maxRecords) {
Log::channel('dailyCron')->info('Maximum allowed records for Excel export is: '.number_format($maxRecords).' current records: '.number_format($ICM688M2Count));
}
}
else{
$ICM688M2Col2 = collect();
}
if ($ICM688M2Col2->count()>0) {
$this->info("Saving Current Month Excel Report to Disk");
Log::channel('dailyCron')->info("Saving Current Month Excel Report to Disk");
# Store the File to Storage
app(\Modules\M3\Services\SpreadsheetExport::class)->store($ICM688M2Col2, $currentMonthFile, 'publicCL');
}
else{
Log::channel('dailyCron')->info("Daily Report, no records found: Nothing to save...");
}
Log::channel('dailyCron')->info("Cron...End Save Orders Closed Lines to Excel");
$this->info("Cron...End Save Orders Closed Lines to Excel");
}
catch (\Throwable $th) {
// Log::error($th);
Log::channel('dailyCron')->error($th);
}
}
}