Skip to content

A free open source IT asset/license management system

Updated observers to create the items in order_items

snipe14141a0f6401
diff --git a/app/Observers/AccessoryObserver.php b/app/Observers/AccessoryObserver.php
index e95a051eea..46e829c9cf 100644
--- a/app/Observers/AccessoryObserver.php
+++ b/app/Observers/AccessoryObserver.php
@@ -4,6 +4,9 @@ namespace App\Observers;
use App\Models\Accessory;
use App\Models\Actionlog;
+use App\Models\Order;
+use App\Models\OrderItem;
+use App\Models\Setting;
class AccessoryObserver
{
@@ -47,18 +50,51 @@ class AccessoryObserver
*/
public function created(Accessory $accessory)
{
+ $attrs = $accessory->getAttributes();
+ $qty = max(1, (int) ($attrs['qty'] ?? 0));
+
+ // Every accessory creation IS an acquisition transaction. Write
+ // the initial Order + OrderItem using what's on the model. The
+ // order_number stays null since the parent has no column for it
+ // any more — controllers doing form-driven creates enrich the
+ // Order with the form-supplied order_number / currency after
+ // save. Factories, seeders, importers, and any other callers
+ // just get the parent-attribute defaults, which keeps their
+ // rows discoverable via the Orders ledger.
+ $currency = ($accessory->location && $accessory->location->currency !== '' && $accessory->location->currency !== null)
+ ? $accessory->location->currency
+ : Setting::getSettings()?->default_currency;
+
+ $order = Order::create([
+ 'order_number' => null,
+ 'supplier_id' => $accessory->supplier_id,
+ 'company_id' => $accessory->company_id,
+ 'purchase_date' => $accessory->purchase_date,
+ 'currency' => $currency,
+ 'created_by' => $accessory->created_by ?? auth()->id(),
+ ]);
+
+ $orderItem = OrderItem::create([
+ 'order_id' => $order->id,
+ 'item_type' => Accessory::class,
+ 'item_id' => $accessory->id,
+ 'qty' => $qty,
+ 'price' => $accessory->purchase_cost,
+ ]);
+
$logAction = new Actionlog;
$logAction->item_type = Accessory::class;
$logAction->item_id = $accessory->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->created_by = auth()->id();
- $attrs = $accessory->getAttributes();
- // order_number moved off Accessory to the Orders / OrderItems
- // data model — the create log no longer captures it directly.
// Capture the initial on-hand qty so the create log gives auditors
// a "started with N units" anchor point. Subsequent QuantityAdjust
// logs record deltas, not running totals.
$logAction->quantity = (int) ($attrs['qty'] ?? 0);
+ // Link the create log to the OrderItem so the history-tab
+ // order-number column resolves to the initial Order (matches
+ // how QuantityAdjust logs surface later replenishments).
+ $logAction->order_item_id = $orderItem->id;
if ($accessory->imported) {
$logAction->setActionSource('importer');
}
diff --git a/app/Observers/ComponentObserver.php b/app/Observers/ComponentObserver.php
index 41b39d2003..4d370e91ef 100644
--- a/app/Observers/ComponentObserver.php
+++ b/app/Observers/ComponentObserver.php
@@ -4,6 +4,9 @@ namespace App\Observers;
use App\Models\Actionlog;
use App\Models\Component;
+use App\Models\Order;
+use App\Models\OrderItem;
+use App\Models\Setting;
class ComponentObserver
{
@@ -51,19 +54,45 @@ class ComponentObserver
*/
public function created(Component $component)
{
+ $attrs = $component->getAttributes();
+ $qty = max(1, (int) ($attrs['qty'] ?? 0));
+
+ // Every component creation IS an acquisition transaction.
+ // See AccessoryObserver::created for the full rationale — same
+ // pattern: initial Order + OrderItem from parent attributes,
+ // controllers enrich with form-supplied fields after save.
+ $currency = ($component->location && $component->location->currency !== '' && $component->location->currency !== null)
+ ? $component->location->currency
+ : Setting::getSettings()?->default_currency;
+
+ $order = Order::create([
+ 'order_number' => null,
+ 'supplier_id' => $component->supplier_id,
+ 'company_id' => $component->company_id,
+ 'purchase_date' => $component->purchase_date,
+ 'currency' => $currency,
+ 'created_by' => $component->created_by ?? auth()->id(),
+ ]);
+
+ $orderItem = OrderItem::create([
+ 'order_id' => $order->id,
+ 'item_type' => Component::class,
+ 'item_id' => $component->id,
+ 'qty' => $qty,
+ 'price' => $component->purchase_cost,
+ ]);
+
$logAction = new Actionlog;
$logAction->item_type = Component::class;
$logAction->item_id = $component->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->action_date = date('Y-m-d H:i:s');
$logAction->created_by = auth()->id();
- $attrs = $component->getAttributes();
- // order_number moved off Component to the Orders / OrderItems
- // data model — the create log no longer captures it directly.
// Capture the initial on-hand qty so the create log gives auditors
// a "started with N units" anchor point. Subsequent QuantityAdjust
// logs record deltas, not running totals.
$logAction->quantity = (int) ($attrs['qty'] ?? 0);
+ $logAction->order_item_id = $orderItem->id;
if ($component->imported) {
$logAction->setActionSource('importer');
}
diff --git a/app/Observers/ConsumableObserver.php b/app/Observers/ConsumableObserver.php
index 53445c3ead..993f99cd4b 100644
--- a/app/Observers/ConsumableObserver.php
+++ b/app/Observers/ConsumableObserver.php
@@ -4,6 +4,9 @@ namespace App\Observers;
use App\Models\Actionlog;
use App\Models\Consumable;
+use App\Models\Order;
+use App\Models\OrderItem;
+use App\Models\Setting;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
@@ -48,18 +51,45 @@ class ConsumableObserver
*/
public function created(Consumable $consumable)
{
+ $attrs = $consumable->getAttributes();
+ $qty = max(1, (int) ($attrs['qty'] ?? 0));
+
+ // Every consumable creation IS an acquisition transaction.
+ // See AccessoryObserver::created for the full rationale — same
+ // pattern: write the initial Order + OrderItem using parent
+ // attributes, controllers enrich with form-supplied fields after
+ // save, factories / seeders / importers get parent-only data.
+ $currency = ($consumable->location && $consumable->location->currency !== '' && $consumable->location->currency !== null)
+ ? $consumable->location->currency
+ : Setting::getSettings()?->default_currency;
+
+ $order = Order::create([
+ 'order_number' => null,
+ 'supplier_id' => $consumable->supplier_id,
+ 'company_id' => $consumable->company_id,
+ 'purchase_date' => $consumable->purchase_date,
+ 'currency' => $currency,
+ 'created_by' => $consumable->created_by ?? auth()->id(),
+ ]);
+
+ $orderItem = OrderItem::create([
+ 'order_id' => $order->id,
+ 'item_type' => Consumable::class,
+ 'item_id' => $consumable->id,
+ 'qty' => $qty,
+ 'price' => $consumable->purchase_cost,
+ ]);
+
$logAction = new Actionlog;
$logAction->item_type = Consumable::class;
$logAction->item_id = $consumable->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->created_by = auth()->id();
- $attrs = $consumable->getAttributes();
- // order_number moved off Consumable to the Orders / OrderItems
- // data model — the create log no longer captures it directly.
// Capture the initial on-hand qty so the create log gives auditors
// a "started with N units" anchor point. Subsequent QuantityAdjust
// logs record deltas, not running totals.
$logAction->quantity = (int) ($attrs['qty'] ?? 0);
+ $logAction->order_item_id = $orderItem->id;
if ($consumable->imported) {
$logAction->setActionSource('importer');
}