A free open source IT asset/license management system
Check for consistent suplier for info panel
snipe7d6617571d94
diff --git a/app/Models/Accessory.php b/app/Models/Accessory.phpindex 1feb90e895..490c64c51d 100755--- a/app/Models/Accessory.php+++ b/app/Models/Accessory.php@@ -464,24 +464,35 @@ class Accessory extends SnipeModel*/public function totalCostSumByCurrency(): array{- $totals = $this->orderItems()- ->with('order:id,currency')- ->get()- ->reduce(function (array $carry, OrderItem $line) {- if ($line->price === null) {- return $carry;- }- $currency = $line->order?->currency- ?? Setting::getSettings()?->default_currency- ?? '';- $carry[$currency] = ($carry[$currency] ?? 0) + ($line->qty * (float) $line->price);+ $lines = $this->orderItems()->with('order:id,currency')->get();+ $totals = $lines->reduce(function (array $carry, OrderItem $line) {+ if ($line->price === null) {return $carry;- }, []);-- if ($totals === [] && $this->purchase_cost !== null) {- $default = Setting::getSettings()?->default_currency ?? '';- $totals[$default] = $this->qty * (float) $this->purchase_cost;+ }+ $currency = $line->order?->currency+ ?? Setting::getSettings()?->default_currency+ ?? '';+ $carry[$currency] = ($carry[$currency] ?? 0) + ($line->qty * (float) $line->price);++ return $carry;+ }, []);++ // Account for units that predate the Orders flow. Only+ // Asset::created writes an OrderItem on creation — accessory /+ // consumable / component observers don't, so the "10 units+ // created at purchase_cost" pre-adjust history has no matching+ // OrderItem line. Add the unaccounted qty * parent.purchase_cost+ // under location.currency (or default_currency if the location+ // has none) so this line's currency matches how unit_cost is+ // rendered in the info-panel.+ $allocatedQty = (int) $lines->sum('qty');+ $unaccountedQty = max(0, (int) $this->qty - $allocatedQty);+ if ($unaccountedQty > 0 && $this->purchase_cost !== null) {+ $fallbackCurrency = ($this->location && $this->location->currency !== '' && $this->location->currency !== null)+ ? $this->location->currency+ : (Setting::getSettings()?->default_currency ?? '');+ $totals[$fallbackCurrency] = ($totals[$fallbackCurrency] ?? 0) + ($unaccountedQty * (float) $this->purchase_cost);}return $totals;@@ -497,6 +508,32 @@ class Accessory extends SnipeModelreturn array_sum($this->totalCostSumByCurrency()) ?: null;}+ /**+ * True when every recorded acquisition for this item came from the+ * same supplier — parent.supplier_id plus every Order linked via+ * OrderItems. The info-panel's supplier row hides itself when this+ * returns false: displaying a single supplier name would misrepresent+ * an item that was replenished from multiple suppliers over time.+ */+ public function hasConsistentSupplier(): bool+ {+ $orderSupplierIds = $this->orderItems()+ ->with('order:id,supplier_id')+ ->get()+ ->map(fn (OrderItem $line) => $line->order?->supplier_id)+ ->filter()+ ->unique()+ ->values()+ ->all();++ $known = array_values(array_unique(array_filter(array_merge(+ [$this->supplier_id],+ $orderSupplierIds,+ ))));++ return count($known) <= 1;+ }+/*** -----------------------------------------------* BEGIN MUTATORSdiff --git a/app/Models/AssetModel.php b/app/Models/AssetModel.phpindex 13822079f2..1e7614b456 100755--- a/app/Models/AssetModel.php+++ b/app/Models/AssetModel.php@@ -153,6 +153,23 @@ class AssetModel extends SnipeModelreturn $this->hasMany(Asset::class, 'model_id')->Deployed();}+ /**+ * How many distinct Orders this asset model has appeared on across+ * all of its Asset instances. Since each Asset is 1:1 with a+ * transaction via AssetObserver::created, this is the count of+ * distinct order_ids on order_items linked to any of this model's+ * assets. Useful for the info-panel "how many times has this model+ * been ordered" hint.+ */+ public function ordersCount(): int+ {+ return (int) OrderItem::query()+ ->where('item_type', Asset::class)+ ->whereIn('item_id', $this->assets()->select('id'))+ ->distinct()+ ->count('order_id');+ }+public function archivedAssets(){return $this->hasMany(Asset::class, 'model_id')->Archived();diff --git a/app/Models/Component.php b/app/Models/Component.phpindex 327957c267..f8f65060ec 100644--- a/app/Models/Component.php+++ b/app/Models/Component.php@@ -416,24 +416,33 @@ class Component extends SnipeModel*/public function totalCostSumByCurrency(): array{- $totals = $this->orderItems()- ->with('order:id,currency')- ->get()- ->reduce(function (array $carry, OrderItem $line) {- if ($line->price === null) {- return $carry;- }- $currency = $line->order?->currency- ?? Setting::getSettings()?->default_currency- ?? '';- $carry[$currency] = ($carry[$currency] ?? 0) + ($line->qty * (float) $line->price);+ $lines = $this->orderItems()->with('order:id,currency')->get();+ $totals = $lines->reduce(function (array $carry, OrderItem $line) {+ if ($line->price === null) {return $carry;- }, []);-- if ($totals === [] && $this->purchase_cost !== null) {- $default = Setting::getSettings()?->default_currency ?? '';- $totals[$default] = $this->qty * (float) $this->purchase_cost;+ }+ $currency = $line->order?->currency+ ?? Setting::getSettings()?->default_currency+ ?? '';+ $carry[$currency] = ($carry[$currency] ?? 0) + ($line->qty * (float) $line->price);++ return $carry;+ }, []);++ // Account for units created before the Orders flow. Component+ // creation doesn't write an OrderItem (unlike Asset::created),+ // so the initial N units at parent.purchase_cost never land in+ // the OrderItem ledger. Add them here under location.currency+ // (or default_currency if the location has none) so this line's+ // currency matches how unit_cost is rendered in the info-panel.+ $allocatedQty = (int) $lines->sum('qty');+ $unaccountedQty = max(0, (int) $this->qty - $allocatedQty);+ if ($unaccountedQty > 0 && $this->purchase_cost !== null) {+ $fallbackCurrency = ($this->location && $this->location->currency !== '' && $this->location->currency !== null)+ ? $this->location->currency+ : (Setting::getSettings()?->default_currency ?? '');+ $totals[$fallbackCurrency] = ($totals[$fallbackCurrency] ?? 0) + ($unaccountedQty * (float) $this->purchase_cost);}return $totals;@@ -448,6 +457,30 @@ class Component extends SnipeModel{return array_sum($this->totalCostSumByCurrency()) ?: null;}++ /**+ * True when every recorded acquisition for this item came from the+ * same supplier. Info-panel supplier row hides when false so a+ * single supplier name doesn't misrepresent multi-supplier history.+ */+ public function hasConsistentSupplier(): bool+ {+ $orderSupplierIds = $this->orderItems()+ ->with('order:id,supplier_id')+ ->get()+ ->map(fn (OrderItem $line) => $line->order?->supplier_id)+ ->filter()+ ->unique()+ ->values()+ ->all();++ $known = array_values(array_unique(array_filter(array_merge(+ [$this->supplier_id],+ $orderSupplierIds,+ ))));++ return count($known) <= 1;+ }/*** -----------------------------------------------* BEGIN MUTATORSdiff --git a/app/Models/Consumable.php b/app/Models/Consumable.phpindex e1b1bca1f0..fd54f55d87 100644--- a/app/Models/Consumable.php+++ b/app/Models/Consumable.php@@ -390,24 +390,33 @@ class Consumable extends SnipeModel*/public function totalCostSumByCurrency(): array{- $totals = $this->orderItems()- ->with('order:id,currency')- ->get()- ->reduce(function (array $carry, OrderItem $line) {- if ($line->price === null) {- return $carry;- }- $currency = $line->order?->currency- ?? Setting::getSettings()?->default_currency- ?? '';- $carry[$currency] = ($carry[$currency] ?? 0) + ($line->qty * (float) $line->price);+ $lines = $this->orderItems()->with('order:id,currency')->get();+ $totals = $lines->reduce(function (array $carry, OrderItem $line) {+ if ($line->price === null) {return $carry;- }, []);-- if ($totals === [] && $this->purchase_cost !== null) {- $default = Setting::getSettings()?->default_currency ?? '';- $totals[$default] = $this->qty * (float) $this->purchase_cost;+ }+ $currency = $line->order?->currency+ ?? Setting::getSettings()?->default_currency+ ?? '';+ $carry[$currency] = ($carry[$currency] ?? 0) + ($line->qty * (float) $line->price);++ return $carry;+ }, []);++ // Account for units created before the Orders flow. Consumable+ // creation doesn't write an OrderItem (unlike Asset::created),+ // so the initial N units at parent.purchase_cost never land in+ // the OrderItem ledger. Add them here under location.currency+ // (or default_currency if the location has none) so this line's+ // currency matches how unit_cost is rendered in the info-panel.+ $allocatedQty = (int) $lines->sum('qty');+ $unaccountedQty = max(0, (int) $this->qty - $allocatedQty);+ if ($unaccountedQty > 0 && $this->purchase_cost !== null) {+ $fallbackCurrency = ($this->location && $this->location->currency !== '' && $this->location->currency !== null)+ ? $this->location->currency+ : (Setting::getSettings()?->default_currency ?? '');+ $totals[$fallbackCurrency] = ($totals[$fallbackCurrency] ?? 0) + ($unaccountedQty * (float) $this->purchase_cost);}return $totals;@@ -423,6 +432,30 @@ class Consumable extends SnipeModelreturn array_sum($this->totalCostSumByCurrency()) ?: null;}+ /**+ * True when every recorded acquisition for this item came from the+ * same supplier. Info-panel supplier row hides when false so a+ * single supplier name doesn't misrepresent multi-supplier history.+ */+ public function hasConsistentSupplier(): bool+ {+ $orderSupplierIds = $this->orderItems()+ ->with('order:id,supplier_id')+ ->get()+ ->map(fn (OrderItem $line) => $line->order?->supplier_id)+ ->filter()+ ->unique()+ ->values()+ ->all();++ $known = array_values(array_unique(array_filter(array_merge(+ [$this->supplier_id],+ $orderSupplierIds,+ ))));++ return count($known) <= 1;+ }+/*** Get the list of checkouts for this consumable*