A free open source IT asset/license management system
Updated observers to only write to orders when delta is positive
snipe55ce28027a94
diff --git a/app/Observers/AccessoryObserver.php b/app/Observers/AccessoryObserver.phpindex 9a719e065b..4c6c82bc64 100644--- a/app/Observers/AccessoryObserver.php+++ b/app/Observers/AccessoryObserver.php@@ -51,39 +51,46 @@ class AccessoryObserverpublic function created(Accessory $accessory){$attrs = $accessory->getAttributes();- $qty = max(1, (int) ($attrs['qty'] ?? 0));+ $initialQty = (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;+ // Only write an Order + OrderItem when the create carried real+ // stock (qty > 0). qty=0/null means "container-only" — a bare+ // accessory row without an initial acquisition — and there's no+ // transaction to record. Later adjust-quantity events write+ // their own Order + OrderItem when stock actually arrives.+ //+ // For qty > 0, the Order + OrderItem written here are+ // placeholders. Transaction metadata (supplier_id, purchase_date,+ // purchase_cost, currency, order_number) only lives on Orders /+ // OrderItems now — form-driven creates enrich it via the+ // controller's enrichInitialOrderFromRequest(), and factory /+ // seeder paths fill it via an afterCreating hook.+ $orderItem = null;+ if ($initialQty > 0) {+ $currency = ($accessory->location && $accessory->location->currency !== '' && $accessory->location->currency !== null)+ ? $accessory->location->currency+ : Setting::getSettings()?->default_currency;- $order = new Order([- 'order_number' => null,- 'supplier_id' => $accessory->supplier_id,- 'company_id' => $accessory->company_id,- 'purchase_date' => $accessory->purchase_date,- 'currency' => $currency,- ]);- $order->created_by = $accessory->created_by ?? auth()->id();- $order->save();+ $order = new Order([+ 'order_number' => null,+ 'supplier_id' => null,+ 'company_id' => $accessory->company_id,+ 'purchase_date' => null,+ 'currency' => $currency,+ ]);+ $order->created_by = $accessory->created_by ?? auth()->id();+ $order->save();- $orderItem = new OrderItem([- 'order_id' => $order->id,- 'item_type' => Accessory::class,- 'item_id' => $accessory->id,- 'qty' => $qty,- 'price' => $accessory->purchase_cost,- ]);- $orderItem->created_by = $accessory->created_by ?? auth()->id();- $orderItem->save();+ $orderItem = new OrderItem([+ 'order_id' => $order->id,+ 'item_type' => Accessory::class,+ 'item_id' => $accessory->id,+ 'qty' => $initialQty,+ 'price' => null,+ ]);+ $orderItem->created_by = $accessory->created_by ?? auth()->id();+ $orderItem->save();+ }$logAction = new Actionlog;$logAction->item_type = Accessory::class;@@ -93,11 +100,11 @@ class AccessoryObserver// 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;+ $logAction->quantity = $initialQty;+ // Link the create log to the OrderItem when one exists. For+ // container-only creates (qty=0) the log has no OrderItem to+ // point at.+ $logAction->order_item_id = $orderItem?->id;if ($accessory->imported) {$logAction->setActionSource('importer');}diff --git a/app/Observers/ComponentObserver.php b/app/Observers/ComponentObserver.phpindex 251a511230..b21903d80d 100644--- a/app/Observers/ComponentObserver.php+++ b/app/Observers/ComponentObserver.php@@ -55,35 +55,36 @@ class ComponentObserverpublic function created(Component $component){$attrs = $component->getAttributes();- $qty = max(1, (int) ($attrs['qty'] ?? 0));+ $initialQty = (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;+ // Skip Order + OrderItem when qty=0/null (container-only).+ // See AccessoryObserver::created for the full rationale.+ $orderItem = null;+ if ($initialQty > 0) {+ $currency = ($component->location && $component->location->currency !== '' && $component->location->currency !== null)+ ? $component->location->currency+ : Setting::getSettings()?->default_currency;- $order = new Order([- 'order_number' => null,- 'supplier_id' => $component->supplier_id,- 'company_id' => $component->company_id,- 'purchase_date' => $component->purchase_date,- 'currency' => $currency,- ]);- $order->created_by = $component->created_by ?? auth()->id();- $order->save();+ $order = new Order([+ 'order_number' => null,+ 'supplier_id' => null,+ 'company_id' => $component->company_id,+ 'purchase_date' => null,+ 'currency' => $currency,+ ]);+ $order->created_by = $component->created_by ?? auth()->id();+ $order->save();- $orderItem = new OrderItem([- 'order_id' => $order->id,- 'item_type' => Component::class,- 'item_id' => $component->id,- 'qty' => $qty,- 'price' => $component->purchase_cost,- ]);- $orderItem->created_by = $component->created_by ?? auth()->id();- $orderItem->save();+ $orderItem = new OrderItem([+ 'order_id' => $order->id,+ 'item_type' => Component::class,+ 'item_id' => $component->id,+ 'qty' => $initialQty,+ 'price' => null,+ ]);+ $orderItem->created_by = $component->created_by ?? auth()->id();+ $orderItem->save();+ }$logAction = new Actionlog;$logAction->item_type = Component::class;@@ -91,11 +92,8 @@ class ComponentObserver$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();- // 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;+ $logAction->quantity = $initialQty;+ $logAction->order_item_id = $orderItem?->id;if ($component->imported) {$logAction->setActionSource('importer');}diff --git a/app/Observers/ConsumableObserver.php b/app/Observers/ConsumableObserver.phpindex b9916093b6..0a83ef3f7c 100644--- a/app/Observers/ConsumableObserver.php+++ b/app/Observers/ConsumableObserver.php@@ -52,47 +52,44 @@ class ConsumableObserverpublic 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 = new Order([- 'order_number' => null,- 'supplier_id' => $consumable->supplier_id,- 'company_id' => $consumable->company_id,- 'purchase_date' => $consumable->purchase_date,- 'currency' => $currency,- ]);- $order->created_by = $consumable->created_by ?? auth()->id();- $order->save();-- $orderItem = new OrderItem([- 'order_id' => $order->id,- 'item_type' => Consumable::class,- 'item_id' => $consumable->id,- 'qty' => $qty,- 'price' => $consumable->purchase_cost,- ]);- $orderItem->created_by = $consumable->created_by ?? auth()->id();- $orderItem->save();+ $initialQty = (int) ($attrs['qty'] ?? 0);++ // Skip Order + OrderItem when qty=0/null (container-only).+ // See AccessoryObserver::created for the full rationale.+ $orderItem = null;+ if ($initialQty > 0) {+ $currency = ($consumable->location && $consumable->location->currency !== '' && $consumable->location->currency !== null)+ ? $consumable->location->currency+ : Setting::getSettings()?->default_currency;++ $order = new Order([+ 'order_number' => null,+ 'supplier_id' => null,+ 'company_id' => $consumable->company_id,+ 'purchase_date' => null,+ 'currency' => $currency,+ ]);+ $order->created_by = $consumable->created_by ?? auth()->id();+ $order->save();++ $orderItem = new OrderItem([+ 'order_id' => $order->id,+ 'item_type' => Consumable::class,+ 'item_id' => $consumable->id,+ 'qty' => $initialQty,+ 'price' => null,+ ]);+ $orderItem->created_by = $consumable->created_by ?? auth()->id();+ $orderItem->save();+ }$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();- // 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;+ $logAction->quantity = $initialQty;+ $logAction->order_item_id = $orderItem?->id;if ($consumable->imported) {$logAction->setActionSource('importer');}