Skip to content

A free open source IT asset/license management system

Added lastOrderPrefill for cloning

snipe5adabfccaa71
diff --git a/app/Http/Controllers/Accessories/AccessoriesController.php b/app/Http/Controllers/Accessories/AccessoriesController.php
index 23db43705b..cc19a704d0 100755
--- a/app/Http/Controllers/Accessories/AccessoriesController.php
+++ b/app/Http/Controllers/Accessories/AccessoriesController.php
@@ -151,6 +151,20 @@ class AccessoriesController extends Controller
$cloned->id = null;
$cloned->deleted_at = '';
+ // Restore the pre-Orders clone-as-fast-entry workflow: carry
+ // the source item's most recent acquisition context onto the
+ // cloned entry's create form so operators cloning a stock row
+ // to restock don't have to copy-paste supplier / order # /
+ // date / price from another tab. Field names match the
+ // create-form input names so enrichInitialOrderFromRequest
+ // in store() picks them up on save and writes them onto the
+ // observer-created initial Order + OrderItem for the new row.
+ foreach ($accessory->lastOrderPrefill() as $field => $value) {
+ if ($value !== null) {
+ $cloned->{$field} = $value;
+ }
+ }
+
return view('accessories/edit')
->with('cloned_model', $accessory_to_clone)
->with('item', $cloned);
diff --git a/app/Http/Controllers/Components/ComponentsController.php b/app/Http/Controllers/Components/ComponentsController.php
index 0968cdfda9..38c1947f37 100644
--- a/app/Http/Controllers/Components/ComponentsController.php
+++ b/app/Http/Controllers/Components/ComponentsController.php
@@ -252,6 +252,15 @@ class ComponentsController extends Controller
$cloned_component->id = null;
$cloned_component->deleted_at = null;
+ // See AccessoriesController::getClone — same rationale for
+ // carrying the source item's most recent acquisition context
+ // onto the cloned create form.
+ foreach ($component->lastOrderPrefill() as $field => $value) {
+ if ($value !== null) {
+ $cloned_component->{$field} = $value;
+ }
+ }
+
// Show the page
return view('components/edit')
->with('item', $cloned_component)
diff --git a/app/Http/Controllers/Consumables/ConsumablesController.php b/app/Http/Controllers/Consumables/ConsumablesController.php
index 71056f3d1b..4cb328c9c3 100644
--- a/app/Http/Controllers/Consumables/ConsumablesController.php
+++ b/app/Http/Controllers/Consumables/ConsumablesController.php
@@ -255,6 +255,15 @@ class ConsumablesController extends Controller
$consumable->id = null;
$consumable->created_by = null;
+ // See AccessoriesController::getClone — same rationale for
+ // carrying the source item's most recent acquisition context
+ // onto the cloned create form.
+ foreach ($consumable_to_close->lastOrderPrefill() as $field => $value) {
+ if ($value !== null) {
+ $consumable->{$field} = $value;
+ }
+ }
+
return view('consumables/edit')
->with('cloned_model', $consumable_to_close)
->with('item', $consumable);
diff --git a/app/Models/Traits/HasOrders.php b/app/Models/Traits/HasOrders.php
index 9983408e89..3d372f0f7e 100644
--- a/app/Models/Traits/HasOrders.php
+++ b/app/Models/Traits/HasOrders.php
@@ -139,6 +139,44 @@ trait HasOrders
];
}
+ /**
+ * Prefill values for the create / clone form's initial-acquisition
+ * fields. Distinct from lastOrderDefaults() because this shape
+ * includes `order_number` (per-shipment, not a "default" concept)
+ * and matches the request keys the create form posts back, so a
+ * controller can loop the return array to assign values directly
+ * onto a cloned model's attributes.
+ *
+ * Used by getClone() on Accessories / Consumables / Components to
+ * carry the source item's most recent acquisition context onto the
+ * cloned entry's create form, restoring the pre-Orders workflow
+ * where cloning a stock entry pre-filled supplier / order # / date
+ * / price for a fast restock. Items with no order history return
+ * an all-null array.
+ *
+ * @return array{
+ * supplier_id: ?int,
+ * purchase_date: ?string,
+ * purchase_cost: ?string,
+ * order_number: ?string,
+ * }
+ */
+ public function lastOrderPrefill(): array
+ {
+ $line = $this->orderItems()
+ ->with('order:id,order_number,supplier_id,purchase_date')
+ ->latest('id')
+ ->first();
+ $order = $line?->order;
+
+ return [
+ 'supplier_id' => $order?->supplier_id,
+ 'purchase_date' => $order?->purchase_date?->toDateString(),
+ 'purchase_cost' => $line?->price !== null ? (string) $line->price : null,
+ 'order_number' => $order?->order_number,
+ ];
+ }
+
/**
* Resolve a Supplier for the "last acquisition" view (transformers,
* info-panel, report callbacks). Same fallback ladder as