@foreach($purchase_order->purchase_lines as $purchase_line) @if($purchase_line->quantity - $purchase_line->po_quantity_purchased - $purchase_line->po_quantity_damaged - $purchase_line->po_quantity_lost > 0) @php // A warehouse-fulfilled line may have shipped from SEVERAL batches (FEFO // splits an order line when a lot runs short). Each batch carries its own // lot number, expiry and manufacturer, all of which must reach the // customer's invoice — so render ONE ROW PER BATCH rather than folding the // whole quantity into the earliest-expiry lot. // // $dispatch_batches_all is keyed by store variation and holds every // dispatched batch, earliest expiry first. Absent for external-supplier // POs, where this falls back to the original single-row behaviour. $_line_batches = ($dispatch_batches_all ?? [])[$purchase_line->variation_id] ?? []; // Batch quantities are BASE units while the row renders in the pre-selected // purchase sub-unit (the controller already converted the PO line). Divide // by the same multiplier so split rows stay in the row's own unit and // re-multiply correctly on save. $_mult = !empty($purchase_line->sub_unit_multiplier) ? (float) $purchase_line->sub_unit_multiplier : 1; @endphp @if(count($_line_batches) > 1) @foreach($_line_batches as $_batch) @include('purchase.partials.purchase_entry_row', [ 'variations' => [$purchase_line->variations], 'product' => $purchase_line->product, 'row_count' => $row_count, 'variation_id' => $purchase_line->variation_id, 'taxes' => $taxes, 'currency_details' => $currency_details, 'hide_tax' => $hide_tax, 'sub_units' => $sub_units_array[$purchase_line->id], 'purchase_order_line' => $purchase_line, 'purchase_order' => $purchase_order, 'dispatch_batch' => $_batch, 'batch_quantity_override' => $_mult > 0 ? ($_batch['quantity'] / $_mult) : $_batch['quantity'], ]) @php $row_count++; @endphp @endforeach @else @include('purchase.partials.purchase_entry_row', [ 'variations' => [$purchase_line->variations], 'product' => $purchase_line->product, 'row_count' => $row_count, 'variation_id' => $purchase_line->variation_id, 'taxes' => $taxes, 'currency_details' => $currency_details, 'hide_tax' => $hide_tax, 'sub_units' => $sub_units_array[$purchase_line->id], 'purchase_order_line' => $purchase_line, 'purchase_order' => $purchase_order, 'dispatch_batch' => ($dispatch_batches ?? [])[$purchase_line->variation_id] ?? null ]) @php $row_count++; @endphp @endif @endif @endforeach