diff --git a/app/Filament/Exports/CustomerPoMasterExporter.php b/app/Filament/Exports/CustomerPoMasterExporter.php new file mode 100644 index 0000000..acd56e9 --- /dev/null +++ b/app/Filament/Exports/CustomerPoMasterExporter.php @@ -0,0 +1,58 @@ +label('NO') + ->state(function ($record) use (&$rowNumber) { + // Increment and return the row number + return ++$rowNumber; + }), + ExportColumn::make('plant.code') + ->label('PLANT CODE'), + ExportColumn::make('item.code') + ->label('ITEM CODE'), + ExportColumn::make('customer_po') + ->label('CUSTOMER PO'), + ExportColumn::make('customer_name') + ->label('CUSTOMER NAME'), + ExportColumn::make('quantity') + ->label('QUANTITY'), + ExportColumn::make('created_at') + ->label('CREATED AT'), + ExportColumn::make('updated_at') + ->label('UPDATED AT'), + ExportColumn::make('created_by') + ->label('CREATED BY'), + ExportColumn::make('updated_by') + ->label('UPDATED BY'), + ExportColumn::make('deleted_at') + ->label('DELETED AT') + ->enabledByDefault(false), + ]; + } + + public static function getCompletedNotificationBody(Export $export): string + { + $body = 'Your customer po master export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.'; + + if ($failedRowsCount = $export->getFailedRowsCount()) { + $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.'; + } + + return $body; + } +} diff --git a/app/Filament/Imports/CustomerPoMasterImporter.php b/app/Filament/Imports/CustomerPoMasterImporter.php new file mode 100644 index 0000000..430f81b --- /dev/null +++ b/app/Filament/Imports/CustomerPoMasterImporter.php @@ -0,0 +1,140 @@ +requiredMapping() + ->exampleHeader('Plant Code') + ->example('1000') + ->label('Plant Code') + ->relationship(resolveUsing: 'code') + ->rules(['required']), + ImportColumn::make('item') + ->requiredMapping() + ->exampleHeader('Item Code') + ->example('630214') + ->label('Item Code') + ->relationship(resolveUsing: 'code') + ->rules(['required']), + ImportColumn::make('customer_po') + ->requiredMapping() + ->exampleHeader('Customer PO') + ->example('1JA0029512') + ->label('Customer PO') + ->rules(['required']), + ImportColumn::make('customer_name') + ->requiredMapping() + ->exampleHeader('Customer Name') + ->example('KANKARIA MACHINERY STORE') + ->label('Customer Name') + ->rules(['required']), + ImportColumn::make('quantity') + ->requiredMapping() + ->exampleHeader('Quantity') + ->example('5') + ->label('Quantity') + ->rules(['required']), + // ImportColumn::make('created_by') + // ->requiredMapping() + // ->exampleHeader('Created By') + // ->example('Admin') + // ->label('Created By') + // ->rules(['required']), + ]; + } + + public function resolveRecord(): ?CustomerPoMaster + { + $warnMsg = []; + $plantCod = $this->data['plant']; + $plant = null; + $item = null; + + if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) { + $warnMsg[] = 'Invalid plant code found'; + } else { + $plant = Plant::where('code', $plantCod)->first(); + if (! $plant) { + $warnMsg[] = 'Plant not found'; + } else { + $item = Item::where('code', $this->data['item'])->where('plant_id', $plant->id)->first(); + } + if (! $item) { + $warnMsg[] = 'Item not found'; + } + } + + if (Str::length($this->data['customer_po']) < 9) { + $warnMsg[] = 'Invalid Customer PO number found'; + } + + if (empty($this->data['customer_po'])) { + $warnMsg[] = 'Customer PO cannot be empty.'; + } + + // $user = User::where('name', $this->data['created_by'])->first(); + // if (! $user) { + // $warnMsg[] = 'User not found'; + // } + + $user = Filament::auth()->user(); + + $operatorName = $user->name; + + if (! empty($warnMsg)) { + throw new RowImportFailedException(implode(', ', $warnMsg)); + } + //else { // if (empty($warnMsg)) + // $grMaster = GrMaster::where('plant_id', $plant->id) + // ->where('serial_number', $this->data['serial_number']) + // ->latest() + // ->first(); + + // if ($grMaster) { + // throw new RowImportFailedException('Serial number already exist!'); + // } + // } + + CustomerPoMaster::updateOrCreate([ + 'plant_id' => $plant->id, + 'item_id' => $item->id, + 'customer_po' => $this->data['customer_po'], + 'customer_name' => $this->data['customer_name'], + 'quantity' => $this->data['quantity'] ?? null, + 'created_by' => $operatorName, + ]); + + return null; + + // return new CustomerPoMaster(); + } + + public static function getCompletedNotificationBody(Import $import): string + { + $body = 'Your customer po master import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; + + if ($failedRowsCount = $import->getFailedRowsCount()) { + $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.'; + } + + return $body; + } +}