# модулі Auth Category Products виконані без міграцій та відношень


[
                
            'reference' => 'required',
            'name' => 'required',
            'alias' => 'required',
            'image' => 'required|base64image',
            'status' => 'required',
            'description' => 'required',
            'price' => 'required',
            'currency' => 'required',
            'size' => 'required',
            'color' => 'required',
            'state' => 'required',
            'availability' => 'required',
            'category_id' => 'required',

        ]

        $validator = Validator::make( $request->all(),[
                
                'reference' => 'required',
                'name' => 'required',
                'alias' => 'required',
                'image' => 'required',
                'status' => 'required',
                'description' => 'required',
                'price' => 'required',
                'currency' => 'required',
                'size' => 'required',
                'color' => 'required',
                'state' => 'required',
                'availability' => 'required',
                'category_id' => 'required',
    
            ]);
            
            public function inactive($id)
    {

        DB::table('ranges')
            ->where('id', $id)
            ->update(['status' => '0']);

        return redirect()->route('admin.services.category',$id);
    }

    public function active($id)
    {

        DB::table('ranges')
            ->where('id',$id)
            ->update(['status'=> '1']);

        return redirect()->route('admin.services.category',$id);
    }

    public function addProduct(Request $request) 
    {
        $id = $request->basket_id;

        $product_id = $request->product_id;

        $quantity = $request->quantity;

        $price = $request->price;

        $currency = $request->currency;
        
        $count = 1;

        $product_id = Product

        if (empty($id)) {
        /***
         * потрібно додати знижку
         *  в мшграції знижок додане поле
         */
            $basket = Basket::create(['total_price' => $price,'count' => $count,'currency' => $currency,'discont'=> $discont]);//??????

            $basket->products()->attach($product_id,['quantity' => $quantity,'price' => $price,'price_with_discont' => $discont]);///??????

            if($basket){
                
                $basket->quantity = $quantity;

                $basket->update();
            }
            return sendResponse(new BasketResource($basket),'Basket created successfully.');
            
        } else {
            
            $basket = Basket::findOrFail($id);

            $discont = Discont::find(1);///????????

            $price_with_discont = $price / 100 * $discont->value + $price;//??????

            $basket->products()->attach($product_id,['quantity' => $quantity,'price' => $price,'price_with_discont' => $price_with_discont]);

            $total_price = $basket->products->sum('pivot.price');
            
            $total_products = $basket->products->count();

            if($total_price >= $discont->premium){//??якщо сума покупки більша або рівна премшгь знижці то 

                if($basket){   

                    $basket->total_price = $total_price;
                    $basket->quantity = $quantity;
                    $basket->count = $total_products;
                    $basket->currency = $currency;
                    $basket->discont = $discont->premium + ;//???????? додаємо знижку на продукт
                    $basket->update();
                }
                
            }//??

            return sendResponse(new BasketResource($basket),'Basket created successfully.');
        }
    }
    'livrasons' => [
                [
                'id'=> $this->whenPivotLoaded('basket_livrason', function(){
                        return $this->pivot->livrason_id;
                        }),
                'company'=> $this->whenPivotLoaded('basket_livrason', function(){
                    return $this->pivot->company;
                    }),
                'pivot_price'=> $this->whenPivotLoaded('basket_livrason', function(){
                        return $this->pivot->price;
                    }),
                'pivot_package_type'=> $this->whenPivotLoaded('basket_livrason', function(){
                        return $this->pivot->package_type;
                    }),
                ],
                [
                'id'=> $this->whenPivotLoaded('basket_livrason', function(){
                        return $this->pivot->livrason_id;
                        }),
                'company'=> $this->whenPivotLoaded('basket_livrason', function(){
                        return $this->pivot->company;
                        }),
                'price'=> $this->whenPivotLoaded('basket_livrason', function(){
                        return $this->pivot->price;
                        }),
                'package_type'=> $this->whenPivotLoaded('basket_livrason', function(){
                        return $this->pivot->package_type;
                        }),
                ],
                [
                'id'=> $this->whenPivotLoaded('basket_livrason', function(){
                        return $this->pivot->livrason_id;
                    }),  
                'company'=> $this->whenPivotLoaded('basket_livrason', function(){
                        return $this->pivot->company;
                    }),  
                'price'=> $this->whenPivotLoaded('basket_livrason', function(){
                        return $this->pivot->price;
                    }),  
                'package)type'=> $this->whenPivotLoaded('basket_livrason', function(){
                        return $this->pivot->package_type;
                    }),  
                ],
            ],

            $table->float('value')->default(0);
            $table->string('badge')->default('%');
            $table->foreignId('product_id')->nullable()->constrained()->cascadeOnDelete();
            $table->enum('status', DiscontStatus::TYPES)->default(DiscontStatus::DEACTIVATED);