1
0
forked from poc/pds
Files
poc-pds1/app/Console/Commands/SendProductionReport.php

171 lines
5.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\ProductionPlan;
use App\Models\ProductionQuantity;
use DB;
class SendProductionReport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
//protected $signature = 'send:production-report';
protected $signature = 'send:production-report {schedule_type} {plant}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
// public function handle()
// {
// $scheduleType = $this->argument('schedule_type');
// //$plant = $this->argument('plant');
// $plant = (int) $this->argument('plant');
// $mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities')
// ->where('rule_name', 'ProductionMail')
// ->where('plant', $plant)
// ->where('schedule_type', $scheduleType)
// ->get();
// $emails = $mailRules->pluck('email')->unique()->toArray();
// $plants = Plant::all()->keyBy('id');
// $lines = Line::all()->groupBy('plant_id'); // Group lines by plant for easy lookup
// $startDate = now()->setTime(8, 0, 0);
// $endDate = now()->copy()->addDay()->setTime(8, 0, 0);
// $tableData = [];
// $no = 1;
// foreach ($plants as $plantId => $plant) {
// $plantLines = $lines->get($plantId, collect());
// foreach ($plantLines as $line) {
// $lineId = $line->id;
// $lineName = $line->name;
// $targetQuantity = ProductionPlan::where('plant_id', $plant)
// ->where('line_id', $lineId)
// ->whereBetween('created_at', [$startDate, $endDate])
// ->sum('plan_quantity');
// $productionQuantity = ProductionQuantity::where('plant_id', $plant)
// ->where('line_id', $lineId)
// ->whereBetween('created_at', [$startDate, $endDate])
// ->count();
// $tableData[] = [
// 'no' => $no++,
// 'plant' => $plant->name,
// 'line' => $lineName,
// 'targetQuantity' => $targetQuantity,
// 'productionQuantity' => $productionQuantity,
// ];
// }
// }
// $this->table(
// ['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'],
// $tableData
// );
// if (!empty($emails))
// {
// foreach ($emails as $email)
// {
// Mail::to($email)->send(new ProductionMail($tableData));
// }
// }
// else
// {
// $this->info('No recipients found for ProductionMailAlert.');
// }
// }
public function handle()
{
$scheduleType = $this->argument('schedule_type');
$plantId = (int) $this->argument('plant'); // Only one plant
$mailRules = \App\Models\AlertMailRule::where('module', 'ProductionQuantities')
->where('rule_name', 'ProductionMail')
->where('plant', $plantId)
->where('schedule_type', $scheduleType)
->get();
$emails = $mailRules->pluck('email')->unique()->toArray();
$plant = Plant::find($plantId);
if (!$plant) {
$this->error("Invalid plant ID: $plantId");
return;
}
$lines = Line::where('plant_id', $plantId)->get();
$startDate = now()->setTime(8, 0, 0);
$endDate = now()->copy()->addDay()->setTime(8, 0, 0);
$PlanstartDate = now()->setTime(8, 0, 0);
$planendDate = now()->copy()->addDay()->setTime(7, 59, 00);
$tableData = [];
$no = 1;
foreach ($lines as $line) {
$lineId = $line->id;
$lineName = $line->name;
$targetQuantity = ProductionPlan::where('plant_id', $plantId)
->where('line_id', $lineId)
->whereBetween('created_at', [$PlanstartDate, $planendDate])
->sum('plan_quantity');
$productionQuantity = ProductionQuantity::where('plant_id', $plantId)
->where('line_id', $lineId)
->whereBetween('created_at', [$startDate, $endDate])
->count();
$tableData[] = [
'no' => $no++,
'plant' => $plant->name,
'line' => $lineName,
'targetQuantity' => $targetQuantity,
'productionQuantity' => $productionQuantity,
];
}
$this->table(
['No', 'Plant', 'Line', 'Target Quantity', 'Production Quantity'],
$tableData
);
if (!empty($emails)) {
foreach ($emails as $email) {
Mail::to($email)->send(new ProductionMail($tableData));
}
} else {
$this->info('No recipients found for ProductionMailAlert.');
}
}
}