Compare commits
2 Commits
e6cc03aeef
...
64e74b8d67
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
64e74b8d67 | ||
|
|
e6438cf46d |
@@ -2,10 +2,19 @@
|
|||||||
|
|
||||||
namespace App\Filament\Imports;
|
namespace App\Filament\Imports;
|
||||||
|
|
||||||
|
use App\Models\Block;
|
||||||
|
use App\Models\Line;
|
||||||
|
use App\Models\Plant;
|
||||||
use App\Models\ProductionPlan;
|
use App\Models\ProductionPlan;
|
||||||
|
use App\Models\Shift;
|
||||||
|
use App\Models\User;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
||||||
use Filament\Actions\Imports\ImportColumn;
|
use Filament\Actions\Imports\ImportColumn;
|
||||||
use Filament\Actions\Imports\Importer;
|
use Filament\Actions\Imports\Importer;
|
||||||
use Filament\Actions\Imports\Models\Import;
|
use Filament\Actions\Imports\Models\Import;
|
||||||
|
use Filament\Facades\Filament;
|
||||||
|
use Str;
|
||||||
|
|
||||||
class ProductionPlanImporter extends Importer
|
class ProductionPlanImporter extends Importer
|
||||||
{
|
{
|
||||||
@@ -17,54 +26,60 @@ class ProductionPlanImporter extends Importer
|
|||||||
ImportColumn::make('created_at')
|
ImportColumn::make('created_at')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Created DateTime')
|
->exampleHeader('Created DateTime')
|
||||||
->example('30-01-2025 16:44:00')
|
->example(['01-01-2025 08:00:00', '01-01-2025 19:30:00'])
|
||||||
->label('Created DateTime')
|
->label('Created DateTime')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
ImportColumn::make('plan_quantity')
|
ImportColumn::make('plan_quantity')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Plan Quantity')
|
->exampleHeader('Plan Quantity')
|
||||||
->example('500')
|
->example(['500', '450'])
|
||||||
->label('Plan Quantity')
|
->label('Plan Quantity')
|
||||||
->numeric()
|
->numeric()
|
||||||
->rules(['required', 'integer']),
|
->rules(['required', 'integer']),
|
||||||
ImportColumn::make('production_quantity')
|
ImportColumn::make('production_quantity')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Production Quantity')
|
->exampleHeader('Production Quantity')
|
||||||
->example('501')
|
->example(['0', '0'])
|
||||||
->label('Production Quantity')
|
->label('Production Quantity')
|
||||||
->numeric()
|
->numeric()
|
||||||
->rules(['required', 'integer']),
|
->rules(['required', 'integer']),
|
||||||
ImportColumn::make('line')
|
ImportColumn::make('line')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Line Name')
|
->exampleHeader('Line Name')
|
||||||
->example('4 inch pump line')
|
->example(['4 inch pump line', '4 inch pump line'])
|
||||||
->label('Line Name')
|
->label('Line Name')
|
||||||
->relationship(resolveUsing:'name')
|
->relationship(resolveUsing:'name')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
|
ImportColumn::make('block_reference')
|
||||||
|
->requiredMapping() // Or optionalMapping() if not always present
|
||||||
|
->exampleHeader('Block Name')
|
||||||
|
->example(['Block A', 'Block A'])
|
||||||
|
->label('Block Name')
|
||||||
|
->rules(['required']), // Or remove if not required
|
||||||
ImportColumn::make('shift')
|
ImportColumn::make('shift')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Shift Name')
|
->exampleHeader('Shift Name') //ID
|
||||||
->example('Day')
|
->example(['Day', 'Night']) //'2', '7'
|
||||||
->label('Shift Name')
|
->label('Shift Name') // ID
|
||||||
->relationship(resolveUsing:'name')
|
->relationship(resolveUsing: 'name')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
ImportColumn::make('plant')
|
ImportColumn::make('plant')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Plant Name')
|
->exampleHeader('Plant Name')
|
||||||
->example('Ransar Industries-I')
|
->example(['Ransar Industries-I', 'Ransar Industries-I'])
|
||||||
->label('Plant Name')
|
->label('Plant Name')
|
||||||
->relationship(resolveUsing:'name')
|
->relationship(resolveUsing:'name')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
ImportColumn::make('updated_at')
|
ImportColumn::make('updated_at')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Updated DateTime')
|
->exampleHeader('Updated DateTime')
|
||||||
->example('30-01-2025 19:11:00')
|
->example(['01-01-2025 08:00:00', '01-01-2025 19:30:00'])
|
||||||
->label('Updated DateTime')
|
->label('Updated DateTime')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
ImportColumn::make('operator_id')
|
ImportColumn::make('operator_id')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Operator ID')
|
->exampleHeader('Operator ID')
|
||||||
->example('admin')
|
->example([Filament::auth()->user()->name, Filament::auth()->user()->name])
|
||||||
->label('Operator ID')
|
->label('Operator ID')
|
||||||
->rules(['required']),
|
->rules(['required']),
|
||||||
];
|
];
|
||||||
@@ -72,12 +87,141 @@ class ProductionPlanImporter extends Importer
|
|||||||
|
|
||||||
public function resolveRecord(): ?ProductionPlan
|
public function resolveRecord(): ?ProductionPlan
|
||||||
{
|
{
|
||||||
|
$warnMsg = [];
|
||||||
|
$plant = Plant::where('name', $this->data['plant'])->first();
|
||||||
|
if (!$plant) {
|
||||||
|
$warnMsg[] = "Plant not found";
|
||||||
|
}
|
||||||
|
$line = Line::where('name', $this->data['line'])->where('plant_id', $plant->id)->first();
|
||||||
|
if (!$line) {
|
||||||
|
$warnMsg[] = "Line not found";
|
||||||
|
}
|
||||||
|
//block_reference
|
||||||
|
$block = Block::where('name', $this->data['block_reference'])->where('plant_id', $plant->id)->first();
|
||||||
|
$shift = null;
|
||||||
|
if (!$block) {
|
||||||
|
$warnMsg[] = "Block not found";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$shift = Shift::where('name', $this->data['shift'])->where('plant_id', $plant->id)->where('block_id', $block->id)->first();
|
||||||
|
}
|
||||||
|
//$shift = Shift::where('id', $this->data['shift'])->where('plant_id', $plant->id)->first();
|
||||||
|
if (!$shift) {
|
||||||
|
$warnMsg[] = "Shift not found";
|
||||||
|
}
|
||||||
|
if (Str::length($this->data['plan_quantity']) < 0 || !is_numeric($this->data['plan_quantity']) || $this->data['plan_quantity'] <= 0) {
|
||||||
|
$warnMsg[] = "Invalid plan quantity found";
|
||||||
|
}
|
||||||
|
if (Str::length($this->data['production_quantity']) < 0 || !is_numeric($this->data['production_quantity']) || $this->data['production_quantity'] < 0) {
|
||||||
|
$warnMsg[] = "Invalid production quantity found";
|
||||||
|
}
|
||||||
|
|
||||||
|
$fromDate = $this->data['created_at'];
|
||||||
|
$toDate = $this->data['updated_at'];
|
||||||
|
|
||||||
|
$formats = ['d-m-Y H:i', 'd-m-Y H:i:s']; //'07-05-2025 08:00' or '07-05-2025 08:00:00'
|
||||||
|
|
||||||
|
$fdateTime = null;
|
||||||
|
$tdateTime = null;
|
||||||
|
// Try parsing with multiple formats
|
||||||
|
foreach ($formats as $format) {
|
||||||
|
try {
|
||||||
|
$fdateTime = Carbon::createFromFormat($format, $fromDate);
|
||||||
|
break;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// Optionally collect warning messages
|
||||||
|
// $warnMsg[] = "Date format mismatch with format: $format";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($formats as $format) {
|
||||||
|
try {
|
||||||
|
$tdateTime = Carbon::createFromFormat($format, $toDate);
|
||||||
|
break;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// Optionally collect warning messages
|
||||||
|
// $warnMsg[] = "Date format mismatch with format: $format";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$fDateOnly = '';
|
||||||
|
if (!isset($fdateTime)) {
|
||||||
|
// throw new \Exception('Invalid date time format');
|
||||||
|
$warnMsg[] = "Invalid 'Created DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$fDateOnly = $fdateTime->toDateString();
|
||||||
|
}
|
||||||
|
if (!isset($tdateTime)) {
|
||||||
|
$warnMsg[] = "Invalid 'Updated DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($fdateTime) && isset($tdateTime)) {
|
||||||
|
if ($fdateTime->greaterThan($tdateTime)) {
|
||||||
|
$warnMsg[] = "'Created DataTime' is greater than 'Updated DateTime'.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (!$fromDate) {
|
||||||
|
// $warnMsg[] = "Invalid 'Created DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
|
||||||
|
// }
|
||||||
|
// else if (!$toDate) {
|
||||||
|
// $warnMsg[] = "Invalid 'Updated DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
|
||||||
|
// }
|
||||||
|
|
||||||
|
$user = User::where('name', $this->data['operator_id'])->first();
|
||||||
|
if (!$user) {
|
||||||
|
$warnMsg[] = "Operator ID not found";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($warnMsg)) {
|
||||||
|
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||||
|
}
|
||||||
|
else { //if (empty($warnMsg))
|
||||||
|
$productionPlan = ProductionPlan::where('plant_id', $plant->id)
|
||||||
|
->where('shift_id', $shift->id)
|
||||||
|
->where('line_id', $line->id)
|
||||||
|
->whereDate('created_at', $fDateOnly)
|
||||||
|
// ->where('plan_quantity', $productionQuantity->plan_quantity)
|
||||||
|
->latest()
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($productionPlan) {
|
||||||
|
// if($productionPlan->production_quantity)
|
||||||
|
// {
|
||||||
|
// throw new RowImportFailedException("{$productionPlan->created_at}, {$productionPlan->production_quantity}");
|
||||||
|
// }
|
||||||
|
// $warnMsg[] = "Production plan already exist on '{$fDateOnly}'!";
|
||||||
|
|
||||||
|
$productionPlan->update([
|
||||||
|
'plan_quantity' => $this->data['plan_quantity'],
|
||||||
|
// 'production_quantity' => $productionPlan->production_quantity,
|
||||||
|
// 'created_at' => $productionPlan->created_at,//$fdateTime->format('Y-m-d H:i:s'),
|
||||||
|
// 'updated_at' => $tdateTime->format('Y-m-d H:i:s'),
|
||||||
|
'operator_id' => $this->data['operator_id'],
|
||||||
|
]);
|
||||||
|
$productionPlan->save();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ProductionPlan::updateOrCreate([
|
||||||
|
'plant_id' => $plant->id,
|
||||||
|
'line_id' => $line->id,
|
||||||
|
'shift_id' => $shift->id,
|
||||||
|
'plan_quantity' => $this->data['plan_quantity'],
|
||||||
|
'production_quantity' => $this->data['production_quantity'],
|
||||||
|
'created_at' => $fdateTime->format('Y-m-d H:i:s'),//$this->data['created_at'],
|
||||||
|
'updated_at' => $tdateTime->format('Y-m-d H:i:s'),//$this->data['updated_at'],
|
||||||
|
'operator_id' => $this->data['operator_id'],
|
||||||
|
]);
|
||||||
|
return null;
|
||||||
// return ProductionPlan::firstOrNew([
|
// return ProductionPlan::firstOrNew([
|
||||||
// // Update existing records, matching them by `$this->data['column_name']`
|
// // Update existing records, matching them by `$this->data['column_name']`
|
||||||
// 'email' => $this->data['email'],
|
// 'email' => $this->data['email'],
|
||||||
// ]);
|
// ]);
|
||||||
|
|
||||||
return new ProductionPlan();
|
// return new ProductionPlan();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getCompletedNotificationBody(Import $import): string
|
public static function getCompletedNotificationBody(Import $import): string
|
||||||
|
|||||||
@@ -30,15 +30,15 @@ class InvoiceChart extends ChartWidget
|
|||||||
// ];
|
// ];
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// if ($activeFilter === 'yesterday') {
|
// if ($activeFilter == 'yesterday') {
|
||||||
// $startDate = now()->subDay()->setTime(8, 0, 0);
|
// $startDate = now()->subDay()->setTime(8, 0, 0);
|
||||||
// $endDate = now()->setTime(8, 0, 0);
|
// $endDate = now()->setTime(8, 0, 0);
|
||||||
// $groupBy = 'none'; // No grouping by hour
|
// $groupBy = 'none'; // No grouping by hour
|
||||||
// } elseif ($activeFilter === 'this_week') {
|
// } elseif ($activeFilter == 'this_week') {
|
||||||
// $startDate = now()->startOfWeek()->setTime(8, 0, 0);
|
// $startDate = now()->startOfWeek()->setTime(8, 0, 0);
|
||||||
// $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0);
|
// $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0);
|
||||||
// $groupBy = 'day_of_week';
|
// $groupBy = 'day_of_week';
|
||||||
// } elseif ($activeFilter === 'this_month') {
|
// } elseif ($activeFilter == 'this_month') {
|
||||||
// $startDate = now()->startOfMonth();
|
// $startDate = now()->startOfMonth();
|
||||||
// $endDate = now()->endOfMonth();
|
// $endDate = now()->endOfMonth();
|
||||||
// $groupBy = 'week_of_month';
|
// $groupBy = 'week_of_month';
|
||||||
@@ -49,7 +49,7 @@ class InvoiceChart extends ChartWidget
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// // Get fully scanned invoices
|
// // Get fully scanned invoices
|
||||||
// if ($selectedInvoice === 'individual_material') {
|
// if ($selectedInvoice == 'individual_material') {
|
||||||
|
|
||||||
// $allInvoiceNumbers = DB::table('invoice_validations')
|
// $allInvoiceNumbers = DB::table('invoice_validations')
|
||||||
// ->select('invoice_number')
|
// ->select('invoice_number')
|
||||||
@@ -66,7 +66,7 @@ class InvoiceChart extends ChartWidget
|
|||||||
// ->havingRaw("SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END) = COUNT(*)")
|
// ->havingRaw("SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END) = COUNT(*)")
|
||||||
// ->pluck('invoice_number');
|
// ->pluck('invoice_number');
|
||||||
// }
|
// }
|
||||||
// else if($selectedInvoice === 'serial_invoice') {
|
// else if($selectedInvoice == 'serial_invoice') {
|
||||||
// $allInvoiceNumbers = DB::table('invoice_validations')
|
// $allInvoiceNumbers = DB::table('invoice_validations')
|
||||||
// ->select('invoice_number')
|
// ->select('invoice_number')
|
||||||
// ->where('plant_id', $selectedPlant)
|
// ->where('plant_id', $selectedPlant)
|
||||||
@@ -81,7 +81,7 @@ class InvoiceChart extends ChartWidget
|
|||||||
// ->havingRaw("SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END) = COUNT(*)")
|
// ->havingRaw("SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END) = COUNT(*)")
|
||||||
// ->pluck('invoice_number');
|
// ->pluck('invoice_number');
|
||||||
// }
|
// }
|
||||||
// if ($selectedInvoice === 'bundle_material') {
|
// if ($selectedInvoice == 'bundle_material') {
|
||||||
|
|
||||||
// //Get all invoice numbers that contain bundle materials
|
// //Get all invoice numbers that contain bundle materials
|
||||||
// $allInvoiceNumbers = DB::table('invoice_validations')
|
// $allInvoiceNumbers = DB::table('invoice_validations')
|
||||||
@@ -193,12 +193,12 @@ class InvoiceChart extends ChartWidget
|
|||||||
// // }
|
// // }
|
||||||
|
|
||||||
// // // Build x-axis labels and data arrays
|
// // // Build x-axis labels and data arrays
|
||||||
// // if ($groupBy === 'hour') {
|
// // if ($groupBy == 'hour') {
|
||||||
// // $shiftedKeys = array_merge(range(8, 23), range(0, 7));
|
// // $shiftedKeys = array_merge(range(8, 23), range(0, 7));
|
||||||
// // $labels = array_map(fn($h) => date("g A", strtotime("$h:00")), $shiftedKeys);
|
// // $labels = array_map(fn($h) => date("g A", strtotime("$h:00")), $shiftedKeys);
|
||||||
// // $scannedData = array_map(fn($h) => $scannedGrouped[$h] ?? 0, $shiftedKeys);
|
// // $scannedData = array_map(fn($h) => $scannedGrouped[$h] ?? 0, $shiftedKeys);
|
||||||
// // $notScannedData = array_map(fn($h) => $notScannedGrouped[$h] ?? 0, $shiftedKeys);
|
// // $notScannedData = array_map(fn($h) => $notScannedGrouped[$h] ?? 0, $shiftedKeys);
|
||||||
// // } elseif ($groupBy === 'day_of_week') {
|
// // } elseif ($groupBy == 'day_of_week') {
|
||||||
// // $labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
// // $labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
||||||
// // $scannedData = array_fill(0, 7, 0);
|
// // $scannedData = array_fill(0, 7, 0);
|
||||||
// // $notScannedData = array_fill(0, 7, 0);
|
// // $notScannedData = array_fill(0, 7, 0);
|
||||||
@@ -312,15 +312,15 @@ class InvoiceChart extends ChartWidget
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Define the date range based on the active filter
|
// Define the date range based on the active filter
|
||||||
if ($activeFilter === 'yesterday') {
|
if ($activeFilter == 'yesterday') {
|
||||||
$startDate = now()->subDay()->setTime(8, 0, 0);
|
$startDate = now()->subDay()->setTime(8, 0, 0);
|
||||||
$endDate = now()->setTime(8, 0, 0);
|
$endDate = now()->setTime(8, 0, 0);
|
||||||
$groupBy = 'none'; // No grouping by hour
|
$groupBy = 'none'; // No grouping by hour
|
||||||
} elseif ($activeFilter === 'this_week') {
|
} elseif ($activeFilter == 'this_week') {
|
||||||
$startDate = now()->startOfWeek()->setTime(8, 0, 0);
|
$startDate = now()->startOfWeek()->setTime(8, 0, 0);
|
||||||
$endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0);
|
$endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0);
|
||||||
$groupBy = 'day_of_week';
|
$groupBy = 'day_of_week';
|
||||||
} elseif ($activeFilter === 'this_month') {
|
} elseif ($activeFilter == 'this_month') {
|
||||||
$startDate = now()->startOfMonth()->setTime(8, 0, 0);
|
$startDate = now()->startOfMonth()->setTime(8, 0, 0);
|
||||||
$endDate = now()->endOfMonth()->setTime(8, 0, 0);
|
$endDate = now()->endOfMonth()->setTime(8, 0, 0);
|
||||||
$groupBy = 'week_of_month';
|
$groupBy = 'week_of_month';
|
||||||
@@ -331,7 +331,7 @@ class InvoiceChart extends ChartWidget
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the counts for Imported Invoices (unique invoice numbers) and Completed Invoices
|
// Get the counts for Imported Invoices (unique invoice numbers) and Completed Invoices
|
||||||
if ($selectedInvoice === 'individual_material') {
|
if ($selectedInvoice == 'individual_material') {
|
||||||
$importedInvoicesCount = DB::table('invoice_validations')
|
$importedInvoicesCount = DB::table('invoice_validations')
|
||||||
->where('plant_id', $selectedPlant)
|
->where('plant_id', $selectedPlant)
|
||||||
->where('quantity', 1)
|
->where('quantity', 1)
|
||||||
@@ -339,29 +339,44 @@ class InvoiceChart extends ChartWidget
|
|||||||
->distinct('invoice_number') // Only distinct invoice numbers
|
->distinct('invoice_number') // Only distinct invoice numbers
|
||||||
->count('invoice_number');
|
->count('invoice_number');
|
||||||
|
|
||||||
$completedInvoicesCount = DB::table('invoice_validations')
|
$completedInvoicesCount = DB::table('invoice_validations')
|
||||||
|
->select('invoice_number')
|
||||||
->where('plant_id', $selectedPlant)
|
->where('plant_id', $selectedPlant)
|
||||||
->where('quantity', 1)
|
->where('quantity', 1)
|
||||||
->whereNotNull('serial_number') // assuming serial_number presence means completed
|
->whereBetween('updated_at', [$startDate, $endDate])
|
||||||
->whereBetween('updated_at', [$startDate, $endDate]) // Filter by date range
|
->groupBy('invoice_number')
|
||||||
->distinct('invoice_number') // Only distinct invoice numbers
|
->havingRaw(
|
||||||
->count('invoice_number');
|
"COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"
|
||||||
} elseif ($selectedInvoice === 'serial_invoice') {
|
)
|
||||||
|
->count();
|
||||||
|
|
||||||
|
} elseif ($selectedInvoice == 'serial_invoice') {
|
||||||
$importedInvoicesCount = DB::table('invoice_validations')
|
$importedInvoicesCount = DB::table('invoice_validations')
|
||||||
->where('plant_id', $selectedPlant)
|
->where('plant_id', $selectedPlant)
|
||||||
->whereNull('quantity')
|
->whereNull('quantity')
|
||||||
->whereBetween('created_at', [$startDate, $endDate]) // Filter by date range
|
->whereBetween('created_at', [$startDate, $endDate])
|
||||||
->distinct('invoice_number') // Only distinct invoice numbers
|
->distinct('invoice_number')
|
||||||
->count('invoice_number');
|
->count('invoice_number');
|
||||||
|
|
||||||
$completedInvoicesCount = DB::table('invoice_validations')
|
// $completedInvoicesCount = DB::table('invoice_validations')
|
||||||
|
// ->where('plant_id', $selectedPlant)
|
||||||
|
// ->whereNull('quantity')
|
||||||
|
// ->where('scanned_status', 'Scanned') // assuming scanned_status means completed
|
||||||
|
// ->whereBetween('updated_at', [$startDate, $endDate]) // Filter by date range
|
||||||
|
// ->distinct('invoice_number') // Only distinct invoice numbers
|
||||||
|
// ->count('invoice_number');
|
||||||
|
$completedInvoicesCount = DB::table('invoice_validations')
|
||||||
|
->select('invoice_number')
|
||||||
->where('plant_id', $selectedPlant)
|
->where('plant_id', $selectedPlant)
|
||||||
->whereNull('quantity')
|
->whereNull('quantity')
|
||||||
->where('scanned_status', 'Scanned') // assuming scanned_status means completed
|
->whereBetween('updated_at', [$startDate, $endDate])
|
||||||
->whereBetween('updated_at', [$startDate, $endDate]) // Filter by date range
|
->groupBy('invoice_number')
|
||||||
->distinct('invoice_number') // Only distinct invoice numbers
|
->havingRaw(
|
||||||
->count('invoice_number');
|
"COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"
|
||||||
} elseif ($selectedInvoice === 'bundle_material') {
|
)
|
||||||
|
->count();
|
||||||
|
|
||||||
|
} elseif ($selectedInvoice == 'bundle_material') {
|
||||||
$importedInvoicesCount = DB::table('invoice_validations')
|
$importedInvoicesCount = DB::table('invoice_validations')
|
||||||
->where('plant_id', $selectedPlant)
|
->where('plant_id', $selectedPlant)
|
||||||
->where('quantity', '>', 1)
|
->where('quantity', '>', 1)
|
||||||
@@ -369,20 +384,31 @@ class InvoiceChart extends ChartWidget
|
|||||||
->distinct('invoice_number') // Only distinct invoice numbers
|
->distinct('invoice_number') // Only distinct invoice numbers
|
||||||
->count('invoice_number');
|
->count('invoice_number');
|
||||||
|
|
||||||
|
// $completedInvoicesCount = DB::table('invoice_validations')
|
||||||
|
// ->where('plant_id', $selectedPlant)
|
||||||
|
// ->where('quantity', '>', 1)
|
||||||
|
// ->whereNotNull('serial_number') // assuming serial_number presence means completed
|
||||||
|
// ->whereBetween('updated_at', [$startDate, $endDate]) // Filter by date range
|
||||||
|
// ->distinct('invoice_number') // Only distinct invoice numbers
|
||||||
|
// ->count('invoice_number');
|
||||||
$completedInvoicesCount = DB::table('invoice_validations')
|
$completedInvoicesCount = DB::table('invoice_validations')
|
||||||
|
->select('invoice_number')
|
||||||
->where('plant_id', $selectedPlant)
|
->where('plant_id', $selectedPlant)
|
||||||
->where('quantity', '>', 1)
|
->where('quantity', '>', 1)
|
||||||
->whereNotNull('serial_number') // assuming serial_number presence means completed
|
->whereBetween('updated_at', [$startDate, $endDate])
|
||||||
->whereBetween('updated_at', [$startDate, $endDate]) // Filter by date range
|
->groupBy('invoice_number')
|
||||||
->distinct('invoice_number') // Only distinct invoice numbers
|
->havingRaw(
|
||||||
->count('invoice_number');
|
"COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"
|
||||||
|
)
|
||||||
|
->count();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare the chart data
|
// Prepare the chart data
|
||||||
$labels = []; // Labels for each bar
|
$labels = []; // Labels for each bar
|
||||||
$datasets = []; // Datasets for the chart
|
$datasets = []; // Datasets for the chart
|
||||||
|
|
||||||
if (in_array($activeFilter, ['yesterday', 'today'])) {
|
if (in_array($activeFilter, ['yesterday'])) {
|
||||||
$labels = ['Imported Invoice', 'Completed Invoice'];
|
$labels = ['Imported Invoice', 'Completed Invoice'];
|
||||||
$datasets = [[
|
$datasets = [[
|
||||||
'label' => 'Invoices',
|
'label' => 'Invoices',
|
||||||
@@ -391,37 +417,61 @@ class InvoiceChart extends ChartWidget
|
|||||||
'fill' => false,
|
'fill' => false,
|
||||||
]];
|
]];
|
||||||
}
|
}
|
||||||
elseif ($activeFilter === 'this_week') {
|
|
||||||
|
elseif ($activeFilter == 'this_week') {
|
||||||
$daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
$daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
||||||
$importedInvoicesPerDay = array_fill(0, 7, 0);
|
$importedInvoicesPerDay = array_fill(0, 7, 0);
|
||||||
$completedInvoicesPerDay = array_fill(0, 7, 0);
|
$completedInvoicesPerDay = array_fill(0, 7, 0);
|
||||||
|
|
||||||
foreach ($daysOfWeek as $index => $day) {
|
for ($index = 0; $index < 7; $index++) {
|
||||||
$dayStart = now()->startOfWeek()->addDays($index)->setTime(8, 0, 0);
|
$dayStart = now()->startOfWeek()->addDays($index)->setTime(8, 0, 0);
|
||||||
$dayEnd = $dayStart->copy()->addDay()->setTime(8, 0, 0);
|
$dayEnd = $dayStart->copy()->addDay()->setTime(8, 0, 0);
|
||||||
|
|
||||||
$query = DB::table('invoice_validations')->where('plant_id', $selectedPlant);
|
$query = DB::table('invoice_validations')->where('plant_id', $selectedPlant);
|
||||||
$completedQuery = DB::table('invoice_validations')->where('plant_id', $selectedPlant);
|
|
||||||
|
|
||||||
if ($selectedInvoice === 'individual_material') {
|
// Build completedQuery with only 'invoice_number' in select
|
||||||
|
$completedQuery = DB::table('invoice_validations')
|
||||||
|
->select('invoice_number')
|
||||||
|
->where('plant_id', $selectedPlant);
|
||||||
|
|
||||||
|
if ($selectedInvoice == 'individual_material')
|
||||||
|
{
|
||||||
$query->where('quantity', 1)->whereBetween('created_at', [$dayStart, $dayEnd]);
|
$query->where('quantity', 1)->whereBetween('created_at', [$dayStart, $dayEnd]);
|
||||||
|
|
||||||
$completedQuery->where('quantity', 1)
|
$completedQuery->where('quantity', 1)
|
||||||
->whereNotNull('serial_number')
|
->whereBetween('updated_at', [$dayStart, $dayEnd])
|
||||||
->whereBetween('updated_at', [$dayStart, $dayEnd]);
|
->groupBy('invoice_number')
|
||||||
} elseif ($selectedInvoice === 'serial_invoice') {
|
->havingRaw(
|
||||||
|
"COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
elseif ($selectedInvoice == 'serial_invoice')
|
||||||
|
{
|
||||||
$query->whereNull('quantity')->whereBetween('created_at', [$dayStart, $dayEnd]);
|
$query->whereNull('quantity')->whereBetween('created_at', [$dayStart, $dayEnd]);
|
||||||
|
|
||||||
$completedQuery->whereNull('quantity')
|
$completedQuery->whereNull('quantity')
|
||||||
->where('scanned_status', 'Scanned')
|
->whereBetween('updated_at', [$dayStart, $dayEnd])
|
||||||
->whereBetween('updated_at', [$dayStart, $dayEnd]);
|
->groupBy('invoice_number')
|
||||||
} elseif ($selectedInvoice === 'bundle_material') {
|
->havingRaw(
|
||||||
|
"COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
elseif ($selectedInvoice == 'bundle_material')
|
||||||
|
{
|
||||||
$query->where('quantity', '>', 1)->whereBetween('created_at', [$dayStart, $dayEnd]);
|
$query->where('quantity', '>', 1)->whereBetween('created_at', [$dayStart, $dayEnd]);
|
||||||
|
|
||||||
$completedQuery->where('quantity', '>', 1)
|
$completedQuery->where('quantity', '>', 1)
|
||||||
->whereNotNull('serial_number')
|
->whereBetween('updated_at', [$dayStart, $dayEnd])
|
||||||
->whereBetween('updated_at', [$dayStart, $dayEnd]);
|
->groupBy('invoice_number')
|
||||||
|
->havingRaw(
|
||||||
|
"COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Imported invoices count (distinct invoice_number)
|
||||||
$importedInvoicesPerDay[$index] = $query->distinct('invoice_number')->count('invoice_number');
|
$importedInvoicesPerDay[$index] = $query->distinct('invoice_number')->count('invoice_number');
|
||||||
$completedInvoicesPerDay[$index] = $completedQuery->distinct('invoice_number')->count('invoice_number');
|
|
||||||
|
$completedInvoicesPerDay[$index] = $completedQuery->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
$labels = $daysOfWeek;
|
$labels = $daysOfWeek;
|
||||||
@@ -438,7 +488,9 @@ class InvoiceChart extends ChartWidget
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
elseif ($activeFilter === 'this_month') {
|
|
||||||
|
elseif ($activeFilter == 'this_month')
|
||||||
|
{
|
||||||
$startOfMonth = now()->startOfMonth();
|
$startOfMonth = now()->startOfMonth();
|
||||||
$endOfMonth = now()->endOfMonth();
|
$endOfMonth = now()->endOfMonth();
|
||||||
$monthName = $startOfMonth->format('M');
|
$monthName = $startOfMonth->format('M');
|
||||||
@@ -464,15 +516,20 @@ class InvoiceChart extends ChartWidget
|
|||||||
$queryImported = DB::table('invoice_validations')->where('plant_id', $selectedPlant)->whereBetween('created_at', [$weekStart, $weekEnd]);
|
$queryImported = DB::table('invoice_validations')->where('plant_id', $selectedPlant)->whereBetween('created_at', [$weekStart, $weekEnd]);
|
||||||
$queryCompleted = DB::table('invoice_validations')->where('plant_id', $selectedPlant)->whereBetween('updated_at', [$weekStart, $weekEnd]);
|
$queryCompleted = DB::table('invoice_validations')->where('plant_id', $selectedPlant)->whereBetween('updated_at', [$weekStart, $weekEnd]);
|
||||||
|
|
||||||
if ($selectedInvoice === 'individual_material') {
|
if ($selectedInvoice == 'individual_material') {
|
||||||
$queryImported->where('quantity', 1);
|
$queryImported->where('quantity', 1);
|
||||||
$queryCompleted->where('quantity', 1)->whereNotNull('serial_number');
|
$queryCompleted->where('quantity', 1)
|
||||||
} elseif ($selectedInvoice === 'serial_invoice') {
|
->whereNotNull('serial_number')
|
||||||
|
->where('serial_number','!=', '');
|
||||||
|
} elseif ($selectedInvoice == 'serial_invoice') {
|
||||||
$queryImported->whereNull('quantity');
|
$queryImported->whereNull('quantity');
|
||||||
$queryCompleted->whereNull('quantity')->where('scanned_status', 'Scanned');
|
$queryCompleted->whereNull('quantity')
|
||||||
} elseif ($selectedInvoice === 'bundle_material') {
|
->where('scanned_status', 'Scanned');
|
||||||
|
} elseif ($selectedInvoice == 'bundle_material') {
|
||||||
$queryImported->where('quantity', '>', 1);
|
$queryImported->where('quantity', '>', 1);
|
||||||
$queryCompleted->where('quantity', '>', 1)->whereNotNull('serial_number');
|
$queryCompleted->where('quantity', '>', 1)
|
||||||
|
->whereNotNull('serial_number')
|
||||||
|
->where('serial_number','!=', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
$importedInvoicesPerWeek[$index] = $queryImported->distinct('invoice_number')->count('invoice_number');
|
$importedInvoicesPerWeek[$index] = $queryImported->distinct('invoice_number')->count('invoice_number');
|
||||||
@@ -492,7 +549,15 @@ class InvoiceChart extends ChartWidget
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
$labels = ['Imported Invoice', 'Completed Invoice'];
|
||||||
|
$datasets = [[
|
||||||
|
'label' => 'Invoices',
|
||||||
|
'data' => [$importedInvoicesCount, $completedInvoicesCount],
|
||||||
|
'backgroundColor' => ['rgba(75, 192, 192, 1)', 'rgba(23, 211, 80, 1)'],
|
||||||
|
'fill' => false,
|
||||||
|
]];
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'datasets' => $datasets,
|
'datasets' => $datasets,
|
||||||
|
|||||||
Reference in New Issue
Block a user