Troubleshooting VAT calculation issue

40 views Asked by At

The first item scanned is coming correct, but the items scanned after are not calculating VAT. I attempted to implement the necessary adjustments, but unfortunately, my efforts failed. I'm still determining the specific areas that need changes to achieve the desired outcome. Additionally, I encountered difficulties while installing the debug bar, which further complicated the troubleshooting process. Despite my best efforts, I remain unable to resolve the issue. Any guidance or assistance would be greatly appreciated.

public function getProductsByBarcode()
{
    if (request()->ajax()) {
        $output = [];
        try {
            $location_id = request()->input('location_id', null);
            $check_qty = request()->input('check_qty', false);
            $business_id = request()->session()->get('user.business_id');
            $product_types = request()->get('product_types', []);
            $price_group_id = request()->input('price_group', null);
            $sku = request()->get('sku');
            if(empty($sku)){
                $sku = request()->input('sku');
            }

            $query = Product::join('variations', 'products.id', '=', 'variations.product_id')
                        ->active()
                        ->whereNull('variations.deleted_at')
                        ->leftjoin('units as U', 'products.unit_id', '=', 'U.id')
                        ->leftjoin(
                            'variation_location_details AS VLD',
                            function ($join) use ($location_id) {
                                $join->on('variations.id', '=', 'VLD.variation_id');
                                if (! empty($location_id)) {
                                    $join->where(function ($query) use ($location_id) {
                                        $query->where('VLD.location_id', '=', $location_id);
                                        $query->orWhereNull('VLD.location_id');
                                    });
                                }
                            }
                        );

            if (! empty($price_group_id)) {
                $query->leftjoin(
                    'variation_group_prices AS VGP',
                    function ($join) use ($price_group_id) {
                        $join->on('variations.id', '=', 'VGP.variation_id')
                            ->where('VGP.price_group_id', '=', $price_group_id);
                    }
                );
            }

            $query->where('products.business_id', $business_id)
                  ->where('products.type', '!=', 'modifier');

            if (! empty($product_types)) {
                $query->whereIn('products.type', $product_types);
            }

            $query->where('products.business_id', $business_id)
                  ->where('products.type', '!=', 'modifier')
                  ->where('products.sku', '=', $sku);

            if ($check_qty) {
                $query->where('VLD.qty_available', '>', 0);
            }

            if (!empty($location_id)) {
                $query->ForLocation($location_id);
            }

            $query->select(
                    'products.id as product_id',
                    'products.name',
                    'products.type',
                    'products.enable_stock',
                    'variations.id as variation_id',
                    'variations.name as variation',
                    'VLD.qty_available',
                    'variations.sell_price_inc_tax as selling_price',
                    'variations.sub_sku',
                    'U.short_name as unit'
                );

            $query->groupBy('variations.id');

            $product = $query->orderBy('VLD.qty_available', 'desc')->first();

            if(empty($product)){
                $output['success'] = false;
                $output['msg'] = __('lang_v1.no_products_found');
                return $output;
            } else {
                $is_overselling_allowed = request()->get('is_overselling_allowed');
                $for_so = request()->get('for_so');
                $is_draft = request()->get('is_draft');
                $enable_stock = $product->enable_stock;
                $qty_available = $product->qty_available;
                $variation_id = $product->variation_id;

                if ($enable_stock != 1 || $qty_available > 0 || $is_overselling_allowed || $for_so || $is_draft) {
                    $row_count = request()->get('product_row');
                    $row_count = $row_count + 1;
                    $quantity = request()->get('quantity', 1);
                    $is_direct_sell = false;
                    $output = $this->getSellLineRowForBarcode($variation_id, $location_id, $quantity, $row_count, $is_direct_sell);
                } else {
                    $output['success'] = false;
                    $output['msg'] = __('lang_v1.item_out_of_stock');
                    return $output;
                }
            }
        } catch (\Exception $e) {
            Log::emergency('File:'.$e->getFile().'Line:'.$e->getLine().'Message:'.$e->getMessage());
            $output['success'] = false;
            $output['msg'] = __('lang_v1.item_out_of_stock');
        }
        return $output;
    }
}

private function getSellLineRowForBarcode($variation_id, $location_id, $quantity, $row_count, $is_direct_sell, $so_line = null)
{
    // omitted for brevity
}
1

There are 1 answers

3
Shaurya Dogra On

Update the below function and let me know the results:

private function getSellLineRowForBarcode($variation_id, $location_id, $quantity, $row_count, $is_direct_sell, $so_line = null)
{

 $business_id = request()->session()->get('user.business_id');

$business_details = $this->businessUtil->getDetails($business_id);
$pos_settings = empty($business_details->pos_settings) ? $this->businessUtil->defaultPosSettings() : json_decode($business_details->pos_settings, true);

$check_qty = !empty($pos_settings['allow_overselling']) ? false : true;


$is_sales_order = request()->has('is_sales_order') && request()->input('is_sales_order') == 'true' ? true : false;
$is_draft = request()->has('is_draft') && request()->input('is_draft') == 'true' ? true : false;

if ($is_sales_order || !empty($so_line) || $is_draft) {
    $check_qty = false;
}


if (request()->input('disable_qty_alert') === 'true') {
    $pos_settings['allow_overselling'] = true;
}


$product = $this->productUtil->getDetailsFromVariation($variation_id, $business_id, $location_id, $check_qty);


if (!isset($product->quantity_ordered)) {
    $product->quantity_ordered = $quantity;
}


$product->secondary_unit_quantity = !isset($product->secondary_unit_quantity) ? 0 : $product->secondary_unit_quantity;


$product->formatted_qty_available = $this->productUtil->num_f($product->qty_available, false, null, true);


$sub_units = $this->productUtil->getSubUnits($business_id, $product->unit_id, false, $product->product_id);


$customer_id = request()->get('customer_id', null);
$cg = $this->contactUtil->getCustomerGroup($business_id, $customer_id);
$percent = (empty($cg) || empty($cg->amount) || $cg->price_calculation_type != 'percentage') ? 0 : $cg->amount;
$product->default_sell_price = $product->default_sell_price + ($percent * $product->default_sell_price / 100);
$product->sell_price_inc_tax = $product->sell_price_inc_tax + ($percent * $product->sell_price_inc_tax / 100);


$tax_dropdown = TaxRate::forBusinessDropdown($business_id, true, true);


$lot_numbers = [];
if (request()->session()->get('business.enable_lot_number') == 1 || request()->session()->get('business.enable_product_expiry') == 1) {
    $lot_number_obj = $this->transactionUtil->getLotNumbersFromVariation($variation_id, $business_id, $location_id, true);
    foreach ($lot_number_obj as $lot_number) {
        $lot_number->qty_formated = $this->productUtil->num_f($lot_number->qty_available);
        $lot_numbers[] = $lot_number;
    }
}
$product->lot_numbers = $lot_numbers;

$price_group = request()->input('price_group');
if (!empty($price_group) && $price_group != "NaN") {
    $variation_group_prices = $this->productUtil->getVariationGroupPrice($variation_id, $price_group, $product->tax_id);
    if (!empty($variation_group_prices['price_inc_tax'])) {
        $product->sell_price_inc_tax = $variation_group_prices['price_inc_tax'];
        $product->default_sell_price = $variation_group_prices['price_exc_tax'];
    }
}


$warranties = $this->__getwarranties();


$output = [
    'variation_id' => $variation_id,
    'success' => true,
    'enable_sr_no' => $product->enable_sr_no
];


if (request()->get('type') == 'sell-return') {
    $output['html_content'] = view('sell_return.partials.product_row')->with(compact('product', 'row_count', 'tax_dropdown', 'enabled_modules', 'sub_units'))->render();
} else {
    $is_cg = !empty($cg->id) ? true : false;
    $discount = $this->productUtil->getProductDiscount($product, $business_id, $location_id, $is_cg, $price_group, $variation_id);
    $edit_discount = auth()->user()->can('edit_product_discount_from_pos_screen');
    $edit_price = auth()->user()->can('edit_product_price_from_pos_screen');

    $output['html_content'] = view('sale_pos.product_row')
        ->with(compact('product', 'row_count', 'tax_dropdown', 'enabled_modules', 'pos_settings', 'sub_units', 'discount', 'waiters', 'edit_discount', 'edit_price', 'purchase_line_id', 'warranties', 'quantity', 'is_direct_sell', 'so_line', 'is_sales_order', 'last_sell_line'))
        ->render();
}

return $output;
}