{{-- ======================================================== PRODUCT ROW EDIT PARTIAL ======================================================== This partial is used for editing product rows in POS. jQuery functionality is initialized on page load but only executes when new rows are added. --}} @php // ========================================== // 1. TAX STATUS SETUP // ========================================== $tax_status = session()->get('tax_status', 'active'); $hide_tax = session()->get('business.enable_inline_tax') == 1 ? '' : 'hide'; // If tax is disabled, override hide_tax to always hide tax-related fields if($tax_status == 'disabled') { $hide_tax = 'hide'; $tax_id = null; $item_tax = 0; $unit_price_inc_tax = $product->default_sell_price; } else { $tax_id = $product->tax_id; $item_tax = !empty($product->item_tax) ? $product->item_tax : 0; $unit_price_inc_tax = $product->sell_price_inc_tax; } @endphp @php // ========================================== // 2. COMMON SETTINGS // ========================================== $common_settings = session()->get('business.common_settings'); $multiplier = 1; $action = !empty($action) ? $action : ''; @endphp @php // ========================================== // 3. SUB-UNIT MULTIPLIER // ========================================== foreach($sub_units as $key => $value) { if(!empty($product->sub_unit_id) && $product->sub_unit_id == $key) { $multiplier = $value['multiplier']; } } @endphp @php // ========================================== // 4. TAX AMOUNT CALCULATION // ========================================== $tax_amount = 0; if(session()->get('business.enable_inline_tax') == 1) { $tax_amount = ($product->sell_price_inc_tax - $product->default_sell_price) * $product->quantity_ordered; } @endphp @php // ========================================== // 5. PRODUCT DISCOUNTS SETUP // ========================================== $total_tax_amount = 0; // Force reload the product with discounts to get the correct data $product_with_discounts = \App\Product::with(['discounts'])->find($product->product_id); // Get discount_allowed as boolean $discount_allowed = false; if($product_with_discounts) { if(isset($product_with_discounts->discount_allowed)) { if(is_bool($product_with_discounts->discount_allowed)) { $discount_allowed = $product_with_discounts->discount_allowed; } else { $discount_allowed = (int)$product_with_discounts->discount_allowed === 1 || $product_with_discounts->discount_allowed === '1'; } } } // If there's a discount applied, discount is allowed if(!empty($discount)) { $discount_allowed = true; } // Get discounts $product_discounts = collect(); $discounts_data = []; $discounts_json = '[]'; if($discount_allowed && $product_with_discounts) { $product_discounts = $product_with_discounts->discounts ?? collect(); $discounts_data = $product_discounts->toArray(); // Multiply discount quantities by the multiplier $adjusted_discounts = []; foreach($discounts_data as $discount) { $adjusted_discount = $discount; if(isset($discount['from_quantity'])) { $adjusted_discount['from_quantity'] = $discount['from_quantity'] * $multiplier; } if(isset($discount['to_quantity']) && !is_null($discount['to_quantity'])) { $adjusted_discount['to_quantity'] = $discount['to_quantity'] * $multiplier; } if(isset($discount['discount_type']) && $discount['discount_type'] == 'fixed' && isset($discount['discount_amount'])) { $adjusted_discount['discount_amount'] = $discount['discount_amount'] * $multiplier; } $adjusted_discounts[] = $adjusted_discount; } $discounts_data = $adjusted_discounts; $discounts_json = json_encode($adjusted_discounts); // If no discounts in relationship, try direct query if($product_discounts->count() == 0) { try { $product_discounts = \App\ProductDiscount::where('product_id', $product->product_id) ->orderBy('from_quantity', 'asc') ->get(); $discounts_data = $product_discounts->toArray(); $adjusted_discounts = []; foreach($discounts_data as $discount) { $adjusted_discount = $discount; if(isset($discount['from_quantity'])) { $adjusted_discount['from_quantity'] = $discount['from_quantity'] * $multiplier; } if(isset($discount['to_quantity']) && !is_null($discount['to_quantity'])) { $adjusted_discount['to_quantity'] = $discount['to_quantity'] * $multiplier; } if(isset($discount['discount_type']) && $discount['discount_type'] == 'fixed' && isset($discount['discount_amount'])) { $adjusted_discount['discount_amount'] = $discount['discount_amount'] * $multiplier; } $adjusted_discounts[] = $adjusted_discount; } $discounts_data = $adjusted_discounts; $discounts_json = json_encode($adjusted_discounts); } catch(\Exception $e) { Log::error('Failed to fetch discounts: ' . $e->getMessage()); } } } // Get global discount info $global_discount_type = !empty($discount) ? $discount->discount_type : 'fixed'; $global_discount_amount = !empty($discount) ? $discount->discount_amount : 0; $has_global_discount = !empty($discount); $base_price = $product->sell_price_inc_tax; @endphp @php // ========================================== // 6. UNIT PRICES // ========================================== $unit_prices = []; $unit_prices_data = []; $default_unit_price = null; $default_unit_multiplier = 1; foreach($sub_units as $key => $value) { // Try to get unit price from the value array first $unit_price = isset($value['unit_price']) && !empty($value['unit_price']) ? (float)$value['unit_price'] : null; // If no unit price in the array, try to fetch from database using the unit ID if(is_null($unit_price) || $unit_price == 0) { try { $unit = \App\Unit::find($key); if($unit && !empty($unit->unit_price)) { $unit_price = (float)$unit->unit_price; } } catch(\Exception $e) { Log::error('Failed to fetch unit price for unit ID ' . $key . ': ' . $e->getMessage()); } } $unit_prices[$key] = $unit_price; $unit_prices_data[$key] = [ 'unit_id' => $key, 'unit_name' => $value['name'] ?? 'Unknown', 'unit_price' => $unit_price, 'multiplier' => $value['multiplier'] ?? 1, 'has_price' => !is_null($unit_price) && $unit_price > 0 ]; // Track default unit if(!empty($product->sub_unit_id) && $product->sub_unit_id == $key) { $default_unit_price = $unit_price; $default_unit_multiplier = $value['multiplier'] ?? 1; } } // If no default unit found, use the first one if(is_null($default_unit_price) && count($sub_units) > 0) { $first_key = array_key_first($sub_units); $default_unit_price = $unit_prices[$first_key] ?? null; $default_unit_multiplier = $sub_units[$first_key]['multiplier'] ?? 1; } @endphp @php // ========================================== // 7. PRICE GROUP DETECTION // ========================================== $has_price_group = false; $price_group_id = null; if(!empty($price_group) && $price_group != '0' && $price_group != 0) { $has_price_group = true; $price_group_id = $price_group; } // Also check if there's a hidden price group from the form if(!$has_price_group && !empty(request()->input('price_group')) && request()->input('price_group') != '0') { $has_price_group = true; $price_group_id = request()->input('price_group'); } // Check session for price group if(!$has_price_group && session()->has('price_group_id') && session()->get('price_group_id') != '0') { $has_price_group = true; $price_group_id = session()->get('price_group_id'); } // Determine the initial price to display $initial_price = $unit_price_inc_tax; if($has_price_group) { $initial_price = $product->sell_price_inc_tax * $multiplier; } elseif(!is_null($default_unit_price) && $default_unit_price > 0) { $initial_price = $default_unit_price; } @endphp {{-- ======================================================== HTML ROW MARKUP ======================================================== --}} {{-- PRODUCT NAME & INFO --}} @if(!empty($so_line)) @endif @php $product_name = $product->product_name; if(!empty($product->brand)){ $product_name .= ' ' . $product->brand; } @endphp @php $hide_tax = session()->get('business.enable_inline_tax') == 1 ? '' : 'hide'; $tax_id = $product->tax_id; $item_tax = !empty($product->item_tax) ? $product->item_tax : 0; $unit_price_inc_tax = $product->sell_price_inc_tax; if($hide_tax == 'hide'){ $tax_id = null; $unit_price_inc_tax = $product->default_sell_price; } if(!empty($so_line) && $action !== 'edit') { $tax_id = $so_line->tax_id; $item_tax = $so_line->item_tax; $unit_price_inc_tax = $so_line->unit_price_inc_tax; } $discount_type = !empty($product->line_discount_type) ? $product->line_discount_type : 'fixed'; $discount_amount = !empty($product->line_discount_amount) ? $product->line_discount_amount : 0; if(!empty($discount)) { $discount_type = $discount->discount_type; $discount_amount = $discount->discount_amount; } if(!empty($so_line) && $action !== 'edit') { $discount_type = $so_line->line_discount_type; $discount_amount = $so_line->line_discount_amount; } $sell_line_note = !empty($product->sell_line_note) ? $product->sell_line_note : ''; if(!empty($so_line)){ $sell_line_note = $so_line->sell_line_note; } $warranty_id = !empty($action) && $action == 'edit' && !empty($product->warranties->first()) ? $product->warranties->first()->id : $product->warranty_id; if($discount_type == 'fixed') { $discount_amount = $discount_amount * $multiplier; } $max_quantity = $product->qty_available; $formatted_max_quantity = $product->formatted_qty_available; if(!empty($action) && $action == 'edit' && !empty($so_line)) { $qty_available = $so_line->quantity - $so_line->so_quantity_invoiced + $product->quantity_ordered; $max_quantity = $qty_available; $formatted_max_quantity = number_format($qty_available, session('business.quantity_precision', 2), session('currency')['decimal_separator'], session('currency')['thousand_separator']); } elseif(!empty($so_line) && $so_line->qty_available <= $max_quantity) { $max_quantity = $so_line->qty_available; $formatted_max_quantity = $so_line->formatted_qty_available; } $max_qty_rule = $max_quantity; $max_qty_msg = __('validation.custom-messages.quantity_not_available', ['qty'=> $formatted_max_quantity, 'unit' => $product->unit]); $lot_enabled = session()->get('business.enable_lot_number'); $exp_enabled = session()->get('business.enable_product_expiry'); $lot_no_line_id = !empty($product->lot_no_line_id) ? $product->lot_no_line_id : ''; @endphp @if(!empty($discount)) {!! Form::hidden("products[$row_count][discount_id]", $discount->id); !!} @endif {{-- Product Name Display --}} @if( ($edit_price || $edit_discount) && empty($is_direct_sell) )
{!! $product_name !!}
@else {!! $product_name !!} @endif
{{-- Lot Number & Expiry --}} @if(!empty($product->lot_numbers) && empty($is_sales_order)) @endif {{-- Serial Number Display --}} @if(!empty($product->matched_serial_number)) SN: {{ $product->matched_serial_number }} @if(!empty($product->serial_number_id)) @endif @if(isset($product->serial_number_available)) (Available: {{ $product->serial_number_available }}) @endif @endif {{-- Product Price Modal --}} @if(empty($is_direct_sell)) @endif {{-- Modifiers --}} @if(in_array('modifiers', $enabled_modules))
@if(!empty($product->product_ms)) @include('restaurant.product_modifier_set.modifier_for_product', ['edit_modifiers' => true, 'row_count' => $loop->index, 'product_ms' => $product->product_ms]) @endif
@endif {{-- Direct sell note --}} @if(!empty($is_direct_sell))

@lang('lang_v1.sell_line_description_help')

@endif {{-- QUANTITY --}} @if(!empty($product->transaction_sell_lines_id)) @endif @if(empty($product->quantity_ordered)) @php $product->quantity_ordered = 1; @endphp @endif @php $allow_decimal = true; if($product->unit_allow_decimal != 1) { $allow_decimal = false; } @endphp @foreach($sub_units as $key => $value) @if(!empty($product->sub_unit_id) && $product->sub_unit_id == $key) @php $max_qty_rule = $max_qty_rule / $multiplier; $unit_name = $value['name']; $max_qty_msg = __('validation.custom-messages.quantity_not_available', ['qty'=> $max_qty_rule, 'unit' => $unit_name]); if(!empty($product->lot_no_line_id)){ $max_qty_msg = __('lang_v1.quantity_error_msg_in_lot', ['qty'=> $max_qty_rule, 'unit' => $unit_name]); } if($value['allow_decimal']) { $allow_decimal = true; } @endphp @endif @endforeach {{-- Quantity Input --}}
matched_serial_number) && isset($product->serial_number_available) && $product->serial_number_available >= 0) data-serial-number="true" data-serial-max-qty="{{ $product->serial_number_available }}" data-rule-max-value="{{ $product->serial_number_available }}" data-msg-max-value="@lang('Quantity Exceeded', ['qty' => $product->serial_number_available])" data-msg_max_default="@lang('Quantity Exceeded', ['qty' => $product->serial_number_available])" style="border-color: #17a2b8;" @elseif($product->enable_stock && empty($pos_settings['allow_overselling']) && empty($is_sales_order)) data-rule-max-value="{{$max_qty_rule}}" data-qty_available="{{$product->qty_available}}" data-msg-max-value="{{$max_qty_msg}}" data-msg_max_default="@lang('validation.custom-messages.quantity_not_available', ['qty'=> $product->formatted_qty_available, 'unit' => $product->unit])" @endif >
{{-- Sub-Unit Selector (Mobile) --}}
@if(count($sub_units) > 0) @else {{$product->unit}} @endif
{{-- SUB-UNIT SELECTOR (Desktop) --}} @if(count($sub_units) > 0) @else {{$product->unit}} @endif {{-- SECONDARY UNIT --}} @if(!empty($product->second_unit))
@lang('lang_v1.quantity_in_second_unit', ['unit' => $product->second_unit])*:
@endif {{-- HIDDEN FIELDS --}} {{-- COMBO PRODUCTS --}} @if($product->product_type == 'combo' && !empty($product->combo_products)) @foreach($product->combo_products as $k => $combo_product) @if(isset($action) && $action == 'edit') @php $combo_product['qty_required'] = $combo_product['quantity'] / $product->quantity_ordered; $qty_total = $combo_product['quantity']; @endphp @else @php $qty_total = $combo_product['qty_required']; @endphp @endif @if(isset($action) && $action == 'edit') @endif @endforeach @endif {{-- DIRECT SELL FIELDS --}} @if(!empty($is_direct_sell)) @if(!empty($pos_settings['inline_service_staff']))
{!! Form::select("products[" . $row_count . "][res_service_staff_id]", $waiters, !empty($product->res_service_staff_id) ? $product->res_service_staff_id : null, ['class' => 'form-control select2 order_line_service_staff', 'placeholder' => __('restaurant.select_service_staff'), 'required' => (!empty($pos_settings['is_service_staff_required']) && $pos_settings['is_service_staff_required'] == 1) ? true : false ]); !!}
@endif @php $pos_unit_price = !empty($product->unit_price_before_discount) ? $product->unit_price_before_discount : $product->default_sell_price; if(!empty($so_line) && $action !== 'edit') { $pos_unit_price = $so_line->unit_price_before_discount; } @endphp @if(!empty($last_sell_line))
@lang('lang_v1.prev_unit_price'): @format_currency($last_sell_line->unit_price_before_discount) @endif {!! Form::text("products[$row_count][line_discount_amount]", @num_format($discount_amount), ['class' => 'form-control input_number row_discount_amount']); !!}
{!! Form::select("products[$row_count][line_discount_type]", ['fixed' => __('lang_v1.fixed'), 'percentage' => __('lang_v1.percentage')], $discount_type , ['class' => 'form-control row_discount_type']); !!} @if(!empty($discount))

{!! __('lang_v1.applied_discount_text', ['discount_name' => $discount->name, 'starts_at' => $discount->formated_starts_at, 'ends_at' => $discount->formated_ends_at]) !!}

@endif @if(!empty($last_sell_line))
@lang('lang_v1.prev_discount'): @if($last_sell_line->line_discount_type == 'percentage') {{@num_format($last_sell_line->line_discount_amount)}}% @else @format_currency($last_sell_line->line_discount_amount) @endif @endif {!! Form::hidden("products[$row_count][item_tax]", @num_format($item_tax), ['class' => 'item_tax']); !!} {!! Form::select("products[$row_count][tax_id]", $tax_dropdown['tax_rates'], $tax_id, [ 'placeholder' => 'Select', 'class' => 'form-control tax_id', 'disabled' => $tax_status == 'disabled' ? 'disabled' : '' ], $tax_dropdown['attributes']); !!} @else @if(!empty($pos_settings['inline_service_staff']))
{!! Form::select("products[" . $row_count . "][res_service_staff_id]", $waiters, !empty($product->res_service_staff_id) ? $product->res_service_staff_id : null, ['class' => 'form-control select2 order_line_service_staff', 'placeholder' => __('restaurant.select_service_staff'), 'required' => (!empty($pos_settings['is_service_staff_required']) && $pos_settings['is_service_staff_required'] == 1) ? true : false ]); !!}
@endif @endif {{-- UNIT PRICE INCLUDING TAX --}} {{-- MARGIN / PROFIT PERCENTAGE --}} @if (empty($pos_settings['show_margin'])) @php $profit_percent = $product->profit_percent ?? 0; if(empty($profit_percent) && !empty($product->variation_id)) { $variation = \App\Variation::find($product->variation_id); if($variation && isset($variation->profit_percent)) { $profit_percent = $variation->profit_percent; } } if(empty($profit_percent) && !empty($product->variations) && $product->variations->count() > 0) { $profit_percent = $product->variations->first()->profit_percent ?? 0; } @endphp @endif {{-- WARRANTY (if enabled) --}} @if(!empty($common_settings['enable_product_warranty']) && !empty($is_direct_sell)) {!! Form::select("products[$row_count][warranty_id]", $warranties, $warranty_id, ['placeholder' => __('messages.please_select'), 'class' => 'form-control']); !!} @endif {{-- LINE TOTAL --}} @php $subtotal_type = !empty($pos_settings['is_pos_subtotal_editable']) ? 'text' : 'hidden'; @endphp {{$product->quantity_ordered * $unit_price_inc_tax}} {{-- REMOVE BUTTON --}} {{-- TAX AMOUNT (hidden by default) --}} @if(session()->get('business.enable_inline_tax') == 1) @endif