134 lines
4.2 KiB
PHP
134 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\InvoiceValidation;
|
|
use App\Models\Plant;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use App\Mail\ProductionMail;
|
|
use App\Models\Line;
|
|
use App\Models\ProductionQuantity;
|
|
|
|
class SendProductionReport extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'send:production-report';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Command description';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities')
|
|
->where('rule_name', 'ProductionMail')
|
|
->get();
|
|
|
|
$emails = $mailRules->pluck('email')->unique()->toArray();
|
|
|
|
$lines = Line::all();
|
|
$fgLineIds = [];
|
|
$nonFgLineIds = [];
|
|
$lineNames = [];
|
|
|
|
foreach ($lines as $line) {
|
|
$lineNames[$line->id] = $line->name;
|
|
if ($line->type == 'FG Line') {
|
|
$fgLineIds[] = $line->id;
|
|
} else {
|
|
$nonFgLineIds[] = $line->id;
|
|
}
|
|
}
|
|
|
|
// 2. Set date range
|
|
$startDate = now()->setTime(8, 0, 0);
|
|
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
|
|
|
|
// 3. Get all plants
|
|
$plants = ProductionQuantity::select('plant_id')->distinct()->pluck('plant_id');
|
|
|
|
// 4. Get production quantities for FG and non-FG lines in one go
|
|
// $fgProduction = ProductionQuantity::select('line_id', \DB::raw('COUNT(*) as total_quantity'))
|
|
// ->whereBetween('created_at', [$startDate, $endDate])
|
|
// ->whereIn('line_id', $fgLineIds)
|
|
// ->groupBy('line_id')
|
|
// ->pluck('total_quantity', 'line_id')
|
|
// ->toArray();
|
|
|
|
$nonFgProduction = ProductionQuantity::select('line_id', \DB::raw('COUNT(*) as total_quantity'))
|
|
->whereBetween('created_at', [$startDate, $endDate])
|
|
->whereIn('line_id', $nonFgLineIds)
|
|
->groupBy('line_id')
|
|
->pluck('total_quantity', 'line_id')
|
|
->toArray();
|
|
|
|
$tableData = [];
|
|
$no = 1;
|
|
|
|
foreach ($plants as $plantId)
|
|
{
|
|
$plant = Plant::find($plantId);
|
|
$plantName = $plant ? $plant->name : $plantId;
|
|
|
|
//Get all unique line_ids for this plant
|
|
$lineIds = ProductionQuantity::where('plant_id', $plantId)
|
|
->select('line_id')
|
|
->distinct()
|
|
->pluck('line_id');
|
|
|
|
foreach ($lineIds as $lineId) {
|
|
$lineName = $lineNames[$lineId] ?? $lineId;
|
|
|
|
$targetQuantity = \App\Models\ProductionPlan::where('plant_id', $plantId)
|
|
->where('line_id', $lineId)
|
|
->whereBetween('created_at', [$startDate, $endDate])
|
|
->sum('plan_quantity');
|
|
|
|
// Use the correct production quantity array based on line type
|
|
if (in_array($lineId, $nonFgLineIds))
|
|
{
|
|
$productionQuantity = $nonFgProduction[$lineId] ?? 0;
|
|
}
|
|
// else
|
|
// {
|
|
// $productionQuantity = $nonFgProduction[$lineId] ?? 0;
|
|
// }
|
|
$tableData[] = [
|
|
'no' => $no++,
|
|
'plant' => $plantName,
|
|
'line' => $lineName,
|
|
'targetQuantity' => $targetQuantity,
|
|
'productionQuantity' => $productionQuantity,
|
|
];
|
|
}
|
|
}
|
|
//$this->info(json_encode($tableData));
|
|
|
|
if (!empty($emails))
|
|
{
|
|
// Mail::to($emails)->send(new test($tableData));
|
|
// $this->info('Invoice report email sent successfully to: ' . implode(', ', $emails));
|
|
foreach ($emails as $email)
|
|
{
|
|
Mail::to($email)->send(new ProductionMail($tableData));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$this->info('No recipients found for InvoiceMailAlert.');
|
|
}
|
|
//$this->info(implode(', ', $emails));
|
|
}
|
|
}
|