{{-- ======================================================== 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 ======================================================== --}}
@lang('lang_v1.sell_line_description_help')
@endif{!! __('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))