mirror of
https://github.com/snipe/snipe-it.git
synced 2026-08-18 11:15:42 +00:00
Added lastOrderPrefill for cloning
This commit is contained in:
@ -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);
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user