Viewing File: /home/ubuntu/vedadeals-backend-base/app/Services/ProductService.php

<?php 

namespace App\Services;

use Illuminate\Http\Request;

use DB, Exception;

use App\Models\{ User, Product, ProductInventory, OrderProduct, Cart, Country };

use App\Jobs\SendEmailJob;

use Setting;

use Illuminate\Support\Str;

class ProductService {

	/**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(Request $request)
    {

    }

    /**
     * @method update_product_inventory()
     *
     * @uses to update product inventory when quantity of an product changes.
     *
     * @created Karthick
     * 
     * @updated
     *
     * @param product_id, quanity (no of products to update in inventory) ,status (YES or NO)
     *
     * @return 
     */

    public static function update_product_inventory($product_id , $quantity = 0, $status) {
        
        try {

        	$product = Product::find($product_id);

        	$product_inventory = ProductInventory::firstOrCreate(['product_id' => $product_id]);

            DB::beginTransaction();

        	if($status == PRODUCT_NOT_SOLD) {

                $product->update([ 'quantity' => $quantity, 'stock_status' => !$quantity ? OUT_OF_STOCK : IN_STOCK ]);

        		$result = $product_inventory->update([ 
                    'total' => $product_inventory->sold ? $product_inventory->total + $quantity : $quantity, 
                    'remaining' => $product_inventory->sold ? $product_inventory->remaining + $quantity : $quantity, 
                ]);

        	} elseif($status == PRODUCT_SOLD) {

                $product_quantity = $product->quantity - $quantity;

                $product->update([ 'quantity' => $product_quantity, 'stock_status' => !$product_quantity ? OUT_OF_STOCK : IN_STOCK ]);

        		$result = $product_inventory->update([ 'remaining' => $product_inventory->remaining - $quantity, 'sold' => $product_inventory->sold + $quantity ]);

        	} elseif($status == PRODUCT_CANCELLED) {

                $product_quantity = $product->quantity + $quantity;

                $product->update([ 'quantity' => $product_quantity, 'stock_status' => IN_STOCK ]);

                $result = $product_inventory->update([ 'remaining' => $product_inventory->remaining + $quantity, 'sold' => $product_inventory->sold - $quantity ]);

            }

        	if(!$result) {

        		throw new Exception(tr('product_save_failed'));
        	}

            DB::commit();

        	$response_array = ['result' => true];

            return response()->json($response_array, 200);

        } catch(Exception $e) {

            DB::rollback();

            $response = ['result' => false, 'error' => $e->getMessage(), 'error_code' => $e->getCode()];

            return response()->json($response, 200);
        }
    }

    /**
     * @method update_cart_product_inventory()
     *
     * @uses to update product inventory while user complete an order by cart.
     *
     * @created Karthick
     * 
     * @updated
     *
     * @param order_id
     *
     * @return 
     */

    public static function update_cart_product_inventory($order_id) {
        
        try {

            $order_products = OrderProduct::where(['order_id' => $order_id])->get(['product_id', 'quantity']);

            DB::beginTransaction();

            foreach($order_products as $order_product) {

                $product_inventory = ProductInventory::firstOrCreate(['product_id' => $order_product->product_id]);

                $product_quantity = $order_product->product->quantity - $order_product->quantity;

                $order_product->product()->update([ 'quantity' => $product_quantity, 'stock_status' => !$product_quantity ? OUT_OF_STOCK : IN_STOCK ]);

                $result = $product_inventory->update([ 'remaining' => $product_inventory->remaining - $order_product->quantity, 'sold' => $product_inventory->sold + $order_product->quantity ]);

            }

            if(!$result) {

                throw new Exception(tr('order_payment_save_failed'));
            }

            DB::commit();

            $response_array = ['result' => true];

            return response()->json($response_array, 200);

        } catch(Exception $e) {

            DB::rollback();

            $response = ['result' => false, 'error' => $e->getMessage(), 'error_code' => $e->getCode()];

            return response()->json($response, 200);
        }
    }

    /**
     * @method products_base_validations()
     *
     * @uses to validate base validation of products while making order, adding to cart
     *
     * @created Karthick
     * 
     * @updated
     *
     * @param object $product, $quantity
     *
     * @return 
     */

    public static function products_base_validations($product, $quantity) {

        if(!$product) {

            throw new Exception(api_error(126), 126);
        }

        if(!$product->stock_status) {

            throw new Exception(api_error(128), 128);
        }

        if($product->quantity < $quantity) {

            $message = tr('product_quantity_error_message_first_half', $product->quantity).' '.tr('product_quantity_error_message_second_half', $product->name);

            throw new Exception($message, 127);
        }
    }

    /**
     * @method get_user_cart_amount()
     *
     * @uses to get user cart total & discount amount
     *
     * @created Karthick
     * 
     * @updated
     *
     * @param user_id
     *
     * @return 
     */

    public static function get_user_cart_amount($user_id) {

        $cart_products = Cart::has('product')->where(['user_id' => $user_id])->get(['product_id', 'quantity']);

        $data['cart_total_amount'] = $data['cart_checkout_amount'] = $data['cart_discount_amount'] = 0;

        foreach($cart_products as $cart_product) {

            $product = Product::find($cart_product->product_id, ['price', 'discount_price', 'stock_status']);

            if($product) {

                if($product->stock_status == IN_STOCK) {

                    $data['cart_checkout_amount'] += $product->price * $cart_product->quantity;

                    $data['cart_discount_amount'] += $product->discount_price * $cart_product->quantity;
                }
            }
        }

        $data['cart_final_amount'] = $data['cart_checkout_amount'];
        
        $data['cart_total_amount'] = formatted_amount($data['cart_checkout_amount'] + $data['cart_discount_amount']);

        $data['cart_checkout_amount'] = formatted_amount($data['cart_checkout_amount']);

        $data['cart_discount_amount'] = formatted_amount($data['cart_discount_amount']);

        return $data;
    }

    /**
     * @method get_guest_cart_amount()
     *
     * @uses to get user cart total & discount amount
     *
     * @created Karthick
     * 
     * @updated
     *
     * @param carts_data
     *
     * @return 
     */

    public static function get_guest_cart_amount($carts_data) {

        $data['cart_total_amount'] = $data['cart_checkout_amount'] = $data['cart_discount_amount'] = 0;

        foreach($carts_data as $cart_data) {

            $product = Product::find($cart_data->product_id, ['price', 'discount_price', 'stock_status']);

            if($product) {

                if($product->stock_status == IN_STOCK) {

                    $data['cart_checkout_amount'] += $product->price * $cart_data->quantity;

                    $data['cart_discount_amount'] += $product->discount_price * $cart_data->quantity;
                }
            }            
        }

        $data['cart_total_amount'] = formatted_amount($data['cart_checkout_amount'] + $data['cart_discount_amount']);

        $data['cart_checkout_amount'] = formatted_amount($data['cart_checkout_amount']);

        $data['cart_discount_amount'] = formatted_amount($data['cart_discount_amount']);
        
        return $data;
    }

    /**
     * @method get_order_amount()
     *
     * @uses to get order total & discount
     *
     * @created Karthick
     * 
     * @updated
     *
     * @param order_products
     *
     * @return 
     */

    public static function get_order_amount($order_products) {

        $data['order_total_amount'] = $data['order_checkout_amount'] = $data['order_discount_total'] = 0;

        foreach($order_products as $order_product) {

            $data['order_checkout_amount'] += $order_product->per_quantity_price * $order_product->quantity;

            $data['order_discount_total'] += $order_product->discount_price * $order_product->quantity;
        }

        $data['order_total_amount'] = formatted_amount($data['order_checkout_amount'] + $data['order_discount_total']);

        $data['order_checkout_amount'] = formatted_amount($data['order_checkout_amount']);

        $data['order_discount_total'] = formatted_amount($data['order_discount_total']);
        
        return $data;
    }

    /**
     * @method check_country_availability()
     *
     * @uses to check the availability of state and country
     *
     * @created Karthick
     * 
     * @updated
     *
     * @param order_products
     *
     * @return 
     */

    public static function check_country_availability($state, $country, $type) {

        $data = [];

        if(!$state || !$country) {

            $data['is_available'] = NO;

            $data['availability_msg'] = tr('availability_msg', '');

        } else {

            $available = Country::when($type == COUNTRY_CODE, function($query) use ($country){
                                $query->where(['country_code' => $country]);
                            })->when($type == COUNTRY, function($query) use ($country){
                                $query->where(['country' => $country]);
                            })->where(['state' => $state])->exists();

            if(in_array($country, ["IN", "CA", "India", "Canada"]) || $available) {

                $data['is_available'] = YES;

                $data['availability_msg'] = '';

            } else {

                $data['is_available'] = NO;

                $data['availability_msg'] = tr('availability_msg', "($state - $country)");

            }
        }

        return $data;
    }

}
Back to Directory File Manager