Skip to content

A free open source IT asset/license management system

Sigh. Missed a few spots for the supplier swap

sniped71e3b72d6d8
diff --git a/app/Http/Controllers/Reports/CustomComponentReportController.php b/app/Http/Controllers/Reports/CustomComponentReportController.php
index a93a79ca74..fa8cae3f21 100644
--- a/app/Http/Controllers/Reports/CustomComponentReportController.php
+++ b/app/Http/Controllers/Reports/CustomComponentReportController.php
@@ -313,18 +313,50 @@ class CustomComponentReportController extends Controller
// ie: quantity_start|quantity_end => qty
'quantity' => 'qty',
'min_quantity' => 'min_amt',
- 'unit_cost' => 'purchase_cost',
+ // unit_cost filters against the parent's "typical" cost
+ // (default_purchase_cost). Per-acquisition cost lives on
+ // OrderItem.price — a report filter that walks Orders would
+ // return components that were EVER purchased in the range,
+ // which is a different question. Filter here targets
+ // parent-level "what does this cost per unit today".
+ 'unit_cost' => 'default_purchase_cost',
]);
$query = $this->appendDateWindowBoundaries($query, $request, [
// formKey => column
// _start and _end will be appended to the key
- // ie: purchase_start|purchase_end => purchase_date
- 'purchase' => 'purchase_date',
'created' => 'created_at',
'last_updated' => 'updated_at',
]);
+ // purchase_start / purchase_end walk the Orders ledger — parent
+ // no longer holds purchase_date, and "was this component ever
+ // acquired between X and Y" is the correct question for a
+ // purchase-date filter. EXISTS lets a component match if any
+ // of its OrderItems point at an Order with purchase_date in
+ // range, without duplicating rows via a join.
+ if ($request->filled('purchase_start') || $request->filled('purchase_end')) {
+ $start = $request->filled('purchase_start')
+ ? Carbon::parse($request->input('purchase_start'))->startOfDay()
+ : null;
+ $end = $request->filled('purchase_end')
+ ? Carbon::parse($request->input('purchase_end'))->endOfDay()
+ : null;
+
+ $query->whereExists(function ($sub) use ($start, $end) {
+ $sub->from('order_items')
+ ->join('orders', 'orders.id', '=', 'order_items.order_id')
+ ->whereColumn('order_items.item_id', 'components.id')
+ ->where('order_items.item_type', Component::class);
+ if ($start) {
+ $sub->where('orders.purchase_date', '>=', $start);
+ }
+ if ($end) {
+ $sub->where('orders.purchase_date', '<=', $end);
+ }
+ });
+ }
+
$query = $this->appendBeforeDateBoundaries($query, $request, [
// formKey => column
'last_updated_before' => 'updated_at',
diff --git a/tests/Feature/Reporting/Custom/CustomComponentReportTest.php b/tests/Feature/Reporting/Custom/CustomComponentReportTest.php
index e952bc4ab1..b199468f87 100644
--- a/tests/Feature/Reporting/Custom/CustomComponentReportTest.php
+++ b/tests/Feature/Reporting/Custom/CustomComponentReportTest.php
@@ -285,8 +285,8 @@ class CustomComponentReportTest extends TestCase
Component::factory()
->count(2)
->sequence(
- ['supplier_id' => $supplierA->id, 'name' => 'Component for Supplier A'],
- ['supplier_id' => $supplierB->id, 'name' => 'Component for Supplier B'],
+ ['default_supplier_id' => $supplierA->id, 'name' => 'Component for Supplier A'],
+ ['default_supplier_id' => $supplierB->id, 'name' => 'Component for Supplier B'],
)
->create();
@@ -407,8 +407,11 @@ class CustomComponentReportTest extends TestCase
public function test_limiting_by_purchase_date_range()
{
- Component::factory()->create(['name' => 'Component A', 'purchase_date' => '2024-01-15']);
- Component::factory()->create(['name' => 'Component B', 'purchase_date' => '2024-06-15']);
+ // purchase_date lives on the initial OrderItem's Order now;
+ // withInitialAcquisition seeds that Order's date so the report's
+ // Orders-EXISTS subquery matches Component A but not Component B.
+ Component::factory()->withInitialAcquisition(null, null, '2024-01-15')->create(['name' => 'Component A']);
+ Component::factory()->withInitialAcquisition(null, null, '2024-06-15')->create(['name' => 'Component B']);
$this->sendRequest([
'component_name' => '1',
@@ -455,8 +458,11 @@ class CustomComponentReportTest extends TestCase
public function test_limiting_by_unit_cost_range()
{
- Component::factory()->create(['name' => 'Component A', 'purchase_cost' => 10.00]);
- Component::factory()->create(['name' => 'Component B', 'purchase_cost' => 500.00]);
+ // unit_cost filters against the parent's default_purchase_cost
+ // template (see CustomComponentReportController::buildQuery for
+ // the rationale on why we don't walk OrderItems here).
+ Component::factory()->create(['name' => 'Component A', 'default_purchase_cost' => 10.00]);
+ Component::factory()->create(['name' => 'Component B', 'default_purchase_cost' => 500.00]);
$this->sendRequest([
'component_name' => '1',