mirror of
https://github.com/snipe/snipe-it.git
synced 2026-08-18 11:15:42 +00:00
Update controllers to use the relationship query for order_number
This commit is contained in:
@ -70,7 +70,10 @@ class AccessoriesController extends Controller
|
||||
$accessory->location_id = request('location_id');
|
||||
$accessory->min_amt = request('min_amt');
|
||||
$accessory->company_id = Company::getIdForCurrentUser(request('company_id'));
|
||||
$accessory->order_number = request('order_number');
|
||||
// order_number moved off the parent Accessory column to the Orders /
|
||||
// OrderItems data model. A create-time order_number in the request
|
||||
// body silently drops here; acquisition tracking happens through
|
||||
// the adjust-quantity flow or the importer's Order helper.
|
||||
$accessory->manufacturer_id = request('manufacturer_id');
|
||||
$accessory->model_number = request('model_number');
|
||||
$accessory->purchase_date = request('purchase_date');
|
||||
|
||||
@ -64,7 +64,6 @@ class AccessoriesController extends Controller
|
||||
'notes',
|
||||
'checkouts_count',
|
||||
'image',
|
||||
'order_number',
|
||||
'qty',
|
||||
// These are *relationships* so we wouldn't normally include them in this array,
|
||||
// since they would normally create a `column not found` error,
|
||||
@ -97,7 +96,12 @@ class AccessoriesController extends Controller
|
||||
}
|
||||
|
||||
if ($request->filled('order_number')) {
|
||||
$accessories->where('accessories.order_number', '=', $request->input('order_number'));
|
||||
// Reroute through the HasOrders orders() HasManyThrough since
|
||||
// the parent accessories.order_number column no longer exists.
|
||||
$orderNumber = $request->input('order_number');
|
||||
$accessories->whereHas('orders', function ($query) use ($orderNumber) {
|
||||
$query->where('orders.order_number', '=', $orderNumber);
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->filled('category_id')) {
|
||||
@ -278,12 +282,12 @@ class AccessoriesController extends Controller
|
||||
|
||||
// Payload shape is preserved for API back-compat: `qty`,
|
||||
// `order_number`, and `supplier_id` all remain accepted keys.
|
||||
// supplier_id flows through fill() like any other field now that
|
||||
// the model accessor is gone. `qty` gets pulled off the fill and
|
||||
// routed through adjustQuantity() below so any change writes a
|
||||
// QuantityAdjust action_log entry rather than silently overwriting.
|
||||
// `order_number` on the parent stays hidden by the accessor, but
|
||||
// rides along on the QuantityAdjust log when the qty also changed.
|
||||
// supplier_id flows through fill() like any other field. `qty` gets
|
||||
// pulled off the fill and routed through adjustQuantity() below so
|
||||
// any change writes a QuantityAdjust action_log entry rather than
|
||||
// silently overwriting. `order_number` no longer lives on the
|
||||
// parent — resolveOrderForAdjustment turns it into an Order +
|
||||
// OrderItem pair and passes the Order's id to the trait.
|
||||
$qtyBefore = (int) $accessory->qty;
|
||||
$qtyRequested = $request->has('qty') ? (int) $request->input('qty') : $qtyBefore;
|
||||
$qtyDelta = $qtyRequested - $qtyBefore;
|
||||
@ -297,11 +301,12 @@ class AccessoriesController extends Controller
|
||||
}
|
||||
|
||||
if ($qtyDelta !== 0) {
|
||||
$orderId = $this->resolveOrderForAdjustment($request, $accessory, $qtyDelta);
|
||||
try {
|
||||
$accessory->adjustQuantity(
|
||||
$qtyDelta,
|
||||
$request->input('note') ?: "API qty change: {$qtyBefore} → {$qtyRequested}",
|
||||
$request->input('order_number'),
|
||||
$orderId,
|
||||
);
|
||||
} catch (DomainException) {
|
||||
return response()->json(
|
||||
|
||||
@ -104,7 +104,6 @@ class AssetsController extends Controller
|
||||
'last_checkin',
|
||||
'notes',
|
||||
'expected_checkin',
|
||||
'order_number',
|
||||
'image',
|
||||
'assigned_to',
|
||||
'created_at',
|
||||
@ -375,7 +374,10 @@ class AssetsController extends Controller
|
||||
}
|
||||
|
||||
if ($request->filled('order_number')) {
|
||||
$assets->where('assets.order_number', '=', strval($request->input('order_number')));
|
||||
$orderNumber = strval($request->input('order_number'));
|
||||
$assets->whereHas('orders', function ($query) use ($orderNumber) {
|
||||
$query->where('orders.order_number', '=', $orderNumber);
|
||||
});
|
||||
}
|
||||
|
||||
foreach ($all_custom_fields as $field) {
|
||||
@ -423,6 +425,12 @@ class AssetsController extends Controller
|
||||
case 'supplier':
|
||||
$assets->OrderSupplier($order);
|
||||
break;
|
||||
case 'order_number':
|
||||
// Handled through the HasOrders scope rather than a raw
|
||||
// assets.order_number sort — that column moved off Asset
|
||||
// when Orders became the acquisition data model.
|
||||
$assets->orderByOrderNumber($order);
|
||||
break;
|
||||
case 'assigned_to':
|
||||
$assets->OrderAssigned($order);
|
||||
break;
|
||||
|
||||
@ -101,7 +101,12 @@ class ComponentsController extends Controller
|
||||
}
|
||||
|
||||
if ($request->filled('order_number')) {
|
||||
$components->where('components.order_number', '=', $request->input('order_number'));
|
||||
// Reroute through the HasOrders orders() HasManyThrough since
|
||||
// the parent components.order_number column no longer exists.
|
||||
$orderNumber = $request->input('order_number');
|
||||
$components->whereHas('orders', function ($query) use ($orderNumber) {
|
||||
$query->where('orders.order_number', '=', $orderNumber);
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->filled('category_id')) {
|
||||
@ -238,11 +243,12 @@ class ComponentsController extends Controller
|
||||
}
|
||||
|
||||
if ($qtyDelta !== 0) {
|
||||
$orderId = $this->resolveOrderForAdjustment($request, $component, $qtyDelta);
|
||||
try {
|
||||
$component->adjustQuantity(
|
||||
$qtyDelta,
|
||||
$request->input('note') ?: "API qty change: {$qtyBefore} → {$qtyRequested}",
|
||||
$request->input('order_number'),
|
||||
$orderId,
|
||||
);
|
||||
} catch (DomainException) {
|
||||
return response()->json(
|
||||
|
||||
@ -45,7 +45,6 @@ class ConsumablesController extends Controller
|
||||
$allowed_columns = [
|
||||
'id',
|
||||
'name',
|
||||
'order_number',
|
||||
'min_amt',
|
||||
'purchase_date',
|
||||
'purchase_cost',
|
||||
@ -88,7 +87,12 @@ class ConsumablesController extends Controller
|
||||
}
|
||||
|
||||
if ($request->filled('order_number')) {
|
||||
$consumables->where('consumables.order_number', '=', $request->input('order_number'));
|
||||
// Reroute through the HasOrders orders() HasManyThrough since
|
||||
// the parent consumables.order_number column no longer exists.
|
||||
$orderNumber = $request->input('order_number');
|
||||
$consumables->whereHas('orders', function ($query) use ($orderNumber) {
|
||||
$query->where('orders.order_number', '=', $orderNumber);
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->filled('category_id')) {
|
||||
@ -226,11 +230,12 @@ class ConsumablesController extends Controller
|
||||
}
|
||||
|
||||
if ($qtyDelta !== 0) {
|
||||
$orderId = $this->resolveOrderForAdjustment($request, $consumable, $qtyDelta);
|
||||
try {
|
||||
$consumable->adjustQuantity(
|
||||
$qtyDelta,
|
||||
$request->input('note') ?: "API qty change: {$qtyBefore} → {$qtyRequested}",
|
||||
$request->input('order_number'),
|
||||
$orderId,
|
||||
);
|
||||
} catch (DomainException) {
|
||||
return response()->json(
|
||||
|
||||
@ -75,7 +75,12 @@ class LicensesController extends Controller
|
||||
}
|
||||
|
||||
if ($request->filled('order_number')) {
|
||||
$licenses->where('order_number', '=', $request->input('order_number'));
|
||||
// Reroute through the HasOrders orders() HasManyThrough since
|
||||
// the parent licenses.order_number column no longer exists.
|
||||
$orderNumber = $request->input('order_number');
|
||||
$licenses->whereHas('orders', function ($query) use ($orderNumber) {
|
||||
$query->where('orders.order_number', '=', $orderNumber);
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->filled('purchase_order')) {
|
||||
@ -169,6 +174,12 @@ class LicensesController extends Controller
|
||||
case 'percent_remaining':
|
||||
$licenses = $licenses->OrderPercentRemaining($order);
|
||||
break;
|
||||
case 'order_number':
|
||||
// Handled through the HasOrders scope rather than a raw
|
||||
// licenses.order_number sort — that column moved off
|
||||
// License when Orders became the acquisition data model.
|
||||
$licenses = $licenses->orderByOrderNumber($order);
|
||||
break;
|
||||
default:
|
||||
$allowed_columns =
|
||||
[
|
||||
|
||||
@ -9,6 +9,7 @@ use App\Models\Order;
|
||||
use App\Models\OrderItem;
|
||||
use DomainException;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Shared body of the adjust-quantity controller action. Web and API
|
||||
@ -73,9 +74,15 @@ trait HandlesAdjustQuantity
|
||||
* single Order row. Never dedupes the OrderItem side — each
|
||||
* adjustment is its own line, matching the "one line per
|
||||
* acquisition event" semantic.
|
||||
*
|
||||
* Accepts the base Request rather than AdjustQuantityRequest
|
||||
* specifically so the legacy Api\{Accessory,Consumable,Component}
|
||||
* Controller::update paths — which run through ImageUploadRequest
|
||||
* for their qty-inside-PATCH shape — can call it with the same
|
||||
* shape as the dedicated adjust-quantity endpoint.
|
||||
*/
|
||||
protected function resolveOrderForAdjustment(
|
||||
AdjustQuantityRequest $request,
|
||||
Request $request,
|
||||
Model $model,
|
||||
int $delta,
|
||||
): ?int {
|
||||
|
||||
Reference in New Issue
Block a user