34 lines
2.0 KiB
PHP
34 lines
2.0 KiB
PHP
<?php
|
|
namespace App\Modules\Purchasing;
|
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Tempo\Finance\Payables\Payables;
|
|
use Tempo\Purchasing\Embedded\Models\{StockItem,Requisition};
|
|
use Tempo\Purchasing\Embedded\Services\{SupplyPlanning,EquipmentWorkflow};
|
|
|
|
/** Bounded catalogue discovery; the existing receipt workflow remains the physical authority. */
|
|
final class ReceiptCatalogue
|
|
{
|
|
public function query(object $order,object $line) {
|
|
$q=StockItem::query()->whereRaw('lower(btrim(unit))=lower(btrim(?))',[$line->unit]);
|
|
if($line->purchase_type==='service')return $q->whereRaw('1=0');
|
|
$fixed=$order->snapshot['catalogue_sources'][$line->requisition_item_id]['stock_item_id']??null;
|
|
if($fixed!==null)$q->whereKey($fixed);
|
|
if(Schema::hasTable('supply_records')){
|
|
$mapping=app(SupplyPlanning::class)->latest('delivery')->where('purchase_order_id',$order->id)->where('data->purchase_order_item_id',$line->id)->first();
|
|
if($mapping)$q->whereKey($mapping->stock_item_id);
|
|
}
|
|
if($line->purchase_type==='spare_part'){
|
|
abort_unless($line->spare_part_id&&$line->equipment_id,422,'A ordem não contém associação autorizada da peça.');
|
|
$part=app(EquipmentWorkflow::class)->linkedPart((int)$line->spare_part_id,(int)$line->equipment_id,Requisition::findOrFail($order->requisition_id)->department_id);$q->whereKey($part->stock_item_id);
|
|
}
|
|
return $q;
|
|
}
|
|
public function revision(StockItem $item):string { return hash('sha256',Payables::canonical($item->toArray())); }
|
|
public function selected(array $data):array {
|
|
$ids=array_values(array_unique(array_filter(array_column($data['items'],'stock_item_id'))));sort($ids,SORT_NUMERIC);$revisions=[];
|
|
foreach($ids as$id){$item=StockItem::whereKey($id)->lockForUpdate()->first();abort_unless($item,422,'Artigo fora do catálogo autorizado.');$revisions[(string)$id]=$this->revision($item);}
|
|
ksort($revisions);return $revisions;
|
|
}
|
|
}
|