A free open source IT asset/license management system
Added order to searchable relations
snipeaf6f41afeced
diff --git a/app/Models/Accessory.php b/app/Models/Accessory.phpindex 5b2447ae0a..94a78d53df 100755--- a/app/Models/Accessory.php+++ b/app/Models/Accessory.php@@ -73,10 +73,10 @@ class Accessory extends SnipeModel'manufacturer' => ['name'],'supplier' => ['name'],// Order numbers moved to a dedicated Orders / OrderItems data- // model when the parent order_number column was removed. Free-text- // search on "PO-123" walks the HasOrders trait's orders() through- // relation into orders.order_number so any accessory that was- // ever purchased under that PO still surfaces.+ // model when the parent order_number column was removed.+ // Free-text search on an order-number string walks the HasOrders+ // trait's orders() HasManyThrough into orders.order_number so+ // any accessory ever acquired under that order still surfaces.'orders' => ['order_number'],];diff --git a/app/Models/Asset.php b/app/Models/Asset.phpindex bd2d0b3072..81dec2bd83 100644--- a/app/Models/Asset.php+++ b/app/Models/Asset.php@@ -238,8 +238,8 @@ class Asset extends Depreciable'assigned_to' => ['name'],// Historical order_number lives on the Orders table via// order_items now (see HasOrders trait). Search hits it through- // the orders() HasManyThrough so "PO-123" still surfaces every- // asset ordered under that PO.+ // the orders() HasManyThrough so an order-number string still+ // surfaces every asset ever acquired under that order.'orders' => ['order_number'],];diff --git a/app/Models/Component.php b/app/Models/Component.phpindex c46cfc095f..c969e50b92 100644--- a/app/Models/Component.php+++ b/app/Models/Component.php@@ -121,7 +121,7 @@ class Component extends SnipeModel'adminuser' => ['first_name', 'last_name', 'display_name'],// See Accessory::$searchableRelations. Search hits order_number// through the HasOrders trait's orders() HasManyThrough into- // the Orders table so historical PO references still match.+ // the Orders table so historical order references still match.'orders' => ['order_number'],];diff --git a/app/Models/Consumable.php b/app/Models/Consumable.phpindex 6344fb7a63..1c7ec60124 100644--- a/app/Models/Consumable.php+++ b/app/Models/Consumable.php@@ -122,7 +122,7 @@ class Consumable extends SnipeModel'adminuser' => ['first_name', 'last_name', 'display_name'],// See Accessory::$searchableRelations. Search hits order_number// through the HasOrders trait's orders() HasManyThrough into- // the Orders table so historical PO references still match.+ // the Orders table so historical order references still match.'orders' => ['order_number'],];diff --git a/app/Models/License.php b/app/Models/License.phpindex 9ddaf3dcf3..f26e43a36c 100755--- a/app/Models/License.php+++ b/app/Models/License.php@@ -136,8 +136,8 @@ class License extends Depreciable'adminuser' => ['first_name', 'last_name', 'display_name'],// Historical order_number lives on the Orders table via// order_items now (see HasOrders trait). Search hits it through- // the orders() HasManyThrough so "PO-123" still surfaces every- // license ordered under that PO.+ // the orders() HasManyThrough so an order-number string still+ // surfaces every license ever acquired under that order.'orders' => ['order_number'],];diff --git a/app/Models/Order.php b/app/Models/Order.phpnew file mode 100644index 0000000000..fb5466f002--- /dev/null+++ b/app/Models/Order.php@@ -0,0 +1,67 @@+<?php++namespace App\Models;++use Illuminate\Database\Eloquent\Factories\HasFactory;+use Illuminate\Database\Eloquent\Relations\BelongsTo;+use Illuminate\Database\Eloquent\Relations\HasMany;+use Illuminate\Database\Eloquent\SoftDeletes;++/**+ * An acquisition record for inventory. One row per real-world order with+ * an optional order_number, supplier, purchase_date, and company scope.+ * Line items (the actual things acquired and their quantities / prices)+ * live on the OrderItem polymorphic pivot so a single Order can carry+ * accessories, consumables, components, assets, and license seats+ * side-by-side.+ *+ * Explicitly NOT a purchase-order-workflow model — no state machine,+ * approvals, or receiving flow. This is just the "what was acquired,+ * when, from whom" record that the adjust-quantity flow and importers+ * write to today. A richer workflow (or a dedicated PurchaseOrder model)+ * can layer on later without disrupting this data.+ *+ * File attachments (receipt / invoice PDFs) are not modeled here yet —+ * follow-up if / when it's actually needed.+ */+class Order extends SnipeModel+{+ use HasFactory;+ use SoftDeletes;++ protected $table = 'orders';++ protected $fillable = [+ 'order_number',+ 'supplier_id',+ 'company_id',+ 'purchase_date',+ 'created_by',+ 'notes',+ 'currency',+ ];++ protected $casts = [+ 'purchase_date' => 'date',+ ];++ public function orderItems(): HasMany+ {+ return $this->hasMany(OrderItem::class);+ }++ public function supplier(): BelongsTo+ {+ return $this->belongsTo(Supplier::class);+ }++ public function company(): BelongsTo+ {+ return $this->belongsTo(Company::class);+ }++ public function admin(): BelongsTo+ {+ return $this->belongsTo(User::class, 'created_by');+ }+}diff --git a/app/Models/OrderItem.php b/app/Models/OrderItem.phpnew file mode 100644index 0000000000..4e422f8508--- /dev/null+++ b/app/Models/OrderItem.php@@ -0,0 +1,45 @@+<?php++namespace App\Models;++use Illuminate\Database\Eloquent\Factories\HasFactory;+use Illuminate\Database\Eloquent\Model;+use Illuminate\Database\Eloquent\Relations\BelongsTo;+use Illuminate\Database\Eloquent\Relations\MorphTo;++/**+ * One line on an Order. Polymorphic to the inventory model that was+ * acquired (Accessory / Consumable / Component / Asset / License) so a+ * single Order can span multiple item types. qty is the number of units+ * on this line; price is the per-unit cost (matches the semantic that+ * the existing purchase_cost columns use across the codebase).+ */+class OrderItem extends Model+{+ use HasFactory;++ protected $table = 'order_items';++ protected $fillable = [+ 'order_id',+ 'item_type',+ 'item_id',+ 'qty',+ 'price',+ ];++ protected $casts = [+ 'qty' => 'integer',+ 'price' => 'decimal:4',+ ];++ public function order(): BelongsTo+ {+ return $this->belongsTo(Order::class);+ }++ public function item(): MorphTo+ {+ return $this->morphTo();+ }+}