@if(!empty($purchase_order_line))
{!! Form::hidden('purchases[' . $row_count . '][purchase_order_line_id]', $purchase_order_line->id ); !!}
@endif
@if(!empty($purchase_requisition_line))
{!! Form::hidden('purchases[' . $row_count . '][purchase_requisition_line_id]', $purchase_requisition_line->id ); !!}
@endif
{!! Form::hidden('purchases[' . $row_count . '][product_id]', $product->id ); !!}
{!! Form::hidden('purchases[' . $row_count . '][variation_id]', $variation->id , ['class' => 'hidden_variation_id']); !!}
{{-- R3: when a store GRN row was seeded from the warehouse's dispatched
batch, carry the source warehouse purchase_line so the store's
received stock is linked back to the exact warehouse batch. --}}
@if(!empty(($dispatch_batch ?? null)['source_purchase_line_id']))
{!! Form::hidden('purchases[' . $row_count . '][source_purchase_line_id]', $dispatch_batch['source_purchase_line_id']); !!}
@endif
@php
$check_decimal = 'false';
if($product->unit->allow_decimal == 0){
$check_decimal = 'true';
}
$currency_precision = session('business.currency_precision', 2);
$quantity_precision = session('business.quantity_precision', 2);
$quantity_value = !empty($purchase_order_line) ? $purchase_order_line->quantity : 1;
$quantity_value = !empty($purchase_requisition_line) ? $purchase_requisition_line->quantity - $purchase_requisition_line->po_quantity_purchased : $quantity_value;
$max_quantity = !empty($purchase_order_line) ? $purchase_order_line->quantity - $purchase_order_line->po_quantity_purchased - $purchase_order_line->po_quantity_damaged - $purchase_order_line->po_quantity_lost : 0;
$max_quantity = !empty($purchase_requisition_line) ? $purchase_requisition_line->quantity - $purchase_requisition_line->po_quantity_purchased : $max_quantity;
$quantity_value = !empty($imported_data) ? $imported_data['quantity'] : $quantity_value;
// MULTI-BATCH GRN: when one PO line was dispatched from several
// batches, purchase_order_lines.blade.php renders one row PER BATCH
// and passes that batch's own quantity here. Each row then carries
// its own lot / expiry / manufacturer, so the store records the real
// split instead of the whole quantity under the first lot.
$quantity_value = isset($batch_quantity_override) ? $batch_quantity_override : $quantity_value;
// Figure out the pre-selected unit here (ahead of the
@php
$pp_without_discount = !empty($purchase_order_line) ? $purchase_order_line->pp_without_discount/$purchase_order->exchange_rate : $variation->default_purchase_price;
$discount_percent = !empty($purchase_order_line) ? $purchase_order_line->discount_percent : 0;
$purchase_price = !empty($purchase_order_line) ? $purchase_order_line->purchase_price/$purchase_order->exchange_rate : $variation->default_purchase_price;
$tax_id = !empty($purchase_order_line) ? $purchase_order_line->tax_id : $product->tax;
$tax_id = !empty($imported_data['tax_id']) ? $imported_data['tax_id'] : $tax_id;
$pp_without_discount = !empty($imported_data['unit_cost_before_discount']) ? $imported_data['unit_cost_before_discount'] : $pp_without_discount;
$discount_percent = !empty($imported_data['discount_percent']) ? $imported_data['discount_percent'] : $discount_percent;
@endphp
{!! Form::text('purchases[' . $row_count . '][pp_without_discount]',
number_format($pp_without_discount, $currency_precision, $currency_details->decimal_separator, $currency_details->thousand_separator), ['class' => 'form-control input-sm purchase_unit_cost_without_discount input_number', 'required']); !!}
@if(!empty($last_purchase_line))
@php
// The stored last-purchase price is per BASE unit;
// scale it to the pre-selected sub-unit so the hint
// is comparable with the cost input beside it.
$_prev_price_multiplier = 1;
if (!empty($sub_units) && isset($_preselect_purchase) && isset($sub_units[$_preselect_purchase])) {
$_prev_price_multiplier = $sub_units[$_preselect_purchase]['multiplier'] ?? 1;
}
@endphp
@lang('lang_v1.prev_unit_price'): @format_currency($last_purchase_line->pp_without_discount * $_prev_price_multiplier)
@endif
@php
// Batch manufacturer, defaulted then left editable — same idiom as
// lot/expiry above. Preference order:
// 1. imported CSV value
// 2. the warehouse's dispatched batch (store GRN, R3 prefill)
// 3. the PO line this GRN row came from
// 4. the product's primary manufacturer
$batch_manufacturer_id = !empty($imported_data['manufacturer_id'])
? $imported_data['manufacturer_id']
: (!empty(($dispatch_batch ?? null)['manufacturer_id'])
? $dispatch_batch['manufacturer_id']
: (!empty($purchase_order_line->manufacturer_id)
? $purchase_order_line->manufacturer_id
: $product->manufacturer_id));
$row_manufacturers = $product->selectableManufacturers();
@endphp
@if($row_manufacturers->isNotEmpty())
{{-- Mandatory on a supplier WAREHOUSE, exactly like lot number and
expiry above: the batch's maker must travel to the store and
onto the patient's invoice, so it cannot be left blank at the
point it is captured. Stores inherit it from the dispatch
prefill, so it stays optional there. --}}
{!! Form::select('purchases[' . $row_count . '][manufacturer_id]', $row_manufacturers, $batch_manufacturer_id,
array_merge(['class' => 'form-control input-sm', 'placeholder' => __('messages.please_select')],
session('business.is_supplier_warehouse') ? ['required' => 'required'] : [])); !!}
@else
@endif
@endif
{{-- PURCHASE ORDER rows: choose the manufacturer to order THIS product
from. Independent of the requisition — the same medicine can be made
by several manufacturers, so the maker is picked fresh on every PO,
limited to those mapped to the product (product_manufacturers).
On save the PO is split into one PO per manufacturer. --}}
@if(!empty($is_purchase_order) && session('business.is_supplier_warehouse'))
@php
$row_manufacturers = $product->selectableManufacturers();
@endphp