Raven
Raven
FFilament
Created by Raven on 12/2/2024 in #❓┊help
help with repeaters and updating data pls
Hello. I've got a Purchase Order resource with a repeater field on it. The repeater field is for items, and each item can have a name, quantity, and received quantity. When creating a purchase order, a user can add as many items as they like and set a quantity. The received quantity defaults to 0. The user can then upload invoices associated with the purchase order. In so doing, here's what I want to happen: 1. The user should be able to click an "Update Inventory" button on the invoice record to open a dialog with all of the existing items on that purchase order. 2. The items will be shown with their name and quantity disabled and unable to be edited, but the received quantity will be editable. 3. The user can edit the received quantity ONLY to a max of the quantity. The quantity is the upper limit. 4. On submit, it should increment or decrement the item's quantity in the repeater field and save the updated record. 5. On submit, it should also find each of the items in the Products table and increment or decrement their quantity accordingly. I've been losing my mind over this for the past hour or so because I can't figure out how to do it. I've got this so far:
DB::transaction(function () use ($record, $data) {
// Get the updated items from the modal
$updatedItems = $data['items'];
// Get the original items from the purchase order
$originalItems = $record->purchaseOrder->items;

// Initialize an array to hold the updated items for saving
$updatedItemsArray = [];

// Process the received quantities
foreach ($updatedItems as $updatedItem) {
// Find the matching original item in the purchase order
$matchingItem = collect($originalItems)->firstWhere('name', $updatedItem['name']);
if ($matchingItem) {
// Calculate the difference in received quantity
$previousReceivedQuantity = $matchingItem['received_quantity'];
$newReceivedQuantity = $updatedItem['received_quantity'];
$difference = $newReceivedQuantity - $previousReceivedQuantity;

// Update the received quantity in the original item
$matchingItem['received_quantity'] = $newReceivedQuantity;

// Increment or decrement inventory based on the difference
$product = Product::where('name', $updatedItem['name'])->first();
if ($product) {
if ($difference > 0) {
// If the received quantity increased, increment the inventory
$product->increment('inventory', $difference);
} elseif ($difference < 0) {
// If the received quantity decreased, decrement the inventory
$product->decrement('inventory', abs($difference));
}
}

// Add the updated item to the array
$updatedItemsArray[] = $matchingItem;
}
}

// Step 4: Save the updated items back to the purchase order
$record->purchaseOrder->items = $updatedItemsArray;
$record->purchaseOrder->save();
});
DB::transaction(function () use ($record, $data) {
// Get the updated items from the modal
$updatedItems = $data['items'];
// Get the original items from the purchase order
$originalItems = $record->purchaseOrder->items;

// Initialize an array to hold the updated items for saving
$updatedItemsArray = [];

// Process the received quantities
foreach ($updatedItems as $updatedItem) {
// Find the matching original item in the purchase order
$matchingItem = collect($originalItems)->firstWhere('name', $updatedItem['name']);
if ($matchingItem) {
// Calculate the difference in received quantity
$previousReceivedQuantity = $matchingItem['received_quantity'];
$newReceivedQuantity = $updatedItem['received_quantity'];
$difference = $newReceivedQuantity - $previousReceivedQuantity;

// Update the received quantity in the original item
$matchingItem['received_quantity'] = $newReceivedQuantity;

// Increment or decrement inventory based on the difference
$product = Product::where('name', $updatedItem['name'])->first();
if ($product) {
if ($difference > 0) {
// If the received quantity increased, increment the inventory
$product->increment('inventory', $difference);
} elseif ($difference < 0) {
// If the received quantity decreased, decrement the inventory
$product->decrement('inventory', abs($difference));
}
}

// Add the updated item to the array
$updatedItemsArray[] = $matchingItem;
}
}

// Step 4: Save the updated items back to the purchase order
$record->purchaseOrder->items = $updatedItemsArray;
$record->purchaseOrder->save();
});
This works except I need to make the name and quantity fields disabled and dehydrated(false) because otherwise the user can edit them. However, when I do that, they're not in the $data array and I'm not able to match it.
25 replies
FFilament
Created by Raven on 12/1/2024 in #❓┊help
How do I generate a random reference?
I've got a resource with a reference field that I want to automatically generate a random identifier for. The user opens the record creation page, and the default value is "INV-0001" or "INV-4829" (haven't decided if I want it to be random or incremented yet). How do I do that?
10 replies
FFilament
Created by Raven on 12/1/2024 in #❓┊help
Reactive Fields
I've got two fields, purchase order and vendor. A purchase order object also has a vendor attached to it. I want it so that if the user selects a purchase order, it automatically selects the vendor and disables the field. If the purchase order gets unselected, the vendor field is cleared and enabled.
6 replies
FFilament
Created by Raven on 11/30/2024 in #❓┊help
How do I integrate `moneyphp/money` with text inputs and columns?
Hello! I want to use moneyphp/money such that: 1. On record creation, the user will input in major currency, dollars for example. 2. On save, moneyphp/money will convert the dollars into minor currency, cents in this case. 3. On edit, the user will be shown major currency. 4. On view in tables, the user will also be shown major currency. I should note that the field I'm storing the currency in is NOT a direct attribute on the table. It's in a JSON column using a repeater.
5 replies
FFilament
Created by Raven on 11/30/2024 in #❓┊help
What's a better way to handle this?
TextInput::make('quantity')->numeric()->required()->live(onBlur: true)
->afterStateUpdated(fn(Set $set, Get $get, ?int $state) => $set('total_cost',
$state * $get('quantity') * (1 + $get('tax') / 100))),
TextInput::make('tax')->numeric()->suffix('%')->required()->live(onBlur: true)
->afterStateUpdated(fn(Set $set, Get $get, ?int $state) => $set('total_cost',
$state * $get('quantity') * (1 + $get('tax') / 100))),
TextInput::make('unit_cost')->numeric()->suffix('USD')->required()->live(onBlur: true)
->afterStateUpdated(fn(Set $set, Get $get, ?int $state) => $set('total_cost',
$state * $get('quantity') * (1 + $get('tax') / 100))),
TextInput::make('total_cost')->numeric()->suffix('USD')->required()->disabled(),
TextInput::make('quantity')->numeric()->required()->live(onBlur: true)
->afterStateUpdated(fn(Set $set, Get $get, ?int $state) => $set('total_cost',
$state * $get('quantity') * (1 + $get('tax') / 100))),
TextInput::make('tax')->numeric()->suffix('%')->required()->live(onBlur: true)
->afterStateUpdated(fn(Set $set, Get $get, ?int $state) => $set('total_cost',
$state * $get('quantity') * (1 + $get('tax') / 100))),
TextInput::make('unit_cost')->numeric()->suffix('USD')->required()->live(onBlur: true)
->afterStateUpdated(fn(Set $set, Get $get, ?int $state) => $set('total_cost',
$state * $get('quantity') * (1 + $get('tax') / 100))),
TextInput::make('total_cost')->numeric()->suffix('USD')->required()->disabled(),
5 replies