app/Http/Controllers/ProductStockController.php
/**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param ProductStock $product_stock
     * @return JsonResponse
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, ProductStock $product_stock)
    {
        $request->validate(
            [
                'selling_price' => 'nullable|numeric',
                'vat' => 'nullable|numeric',
                'discount' => 'nullable|numeric',
                'final_price' => 'nullable|numeric',
                'buying_price' => 'nullable|numeric',
                'damaged' => 'nullable|numeric',
                'type' => 'nullable|string',
                'available_quantity' => 'nullable|string',
                'branch_id' => 'nullable|exists:branches,id',
                'product_id' => 'nullable|exists:products,id',
                'unit_id' => 'nullable|exists:units,id',
                'supplier_id' => 'nullable|exists:suppliers,id',
                'per_unit_price' => 'nullable|numeric',
                'quantity' => 'nullable|numeric',
                'cost' => 'nullable|numeric',
                'sku' => 'required|exists:product_stock_logs,sku',
            ]
        );
        $data['branch_id'] = $request->branch_id ? $request->branch_id : $product_stock->branch_id;
        $data['product_id'] = $request->product_id ? $request->product_id : $product_stock->product_id;
        $data['selling_price'] = $request->selling_price ? $request->selling_price : $product_stock->selling_price;
        $data['vat'] = $request->vat ? $request->vat : $product_stock->vat;
        $data['discount'] = $request->discount ? $request->discount : $product_stock->discount;
        $data['buying_price'] = $request->per_unit_price ? $request->per_unit_price : $product_stock->buying_price;
        $data['damaged'] = $request->damaged ? $request->damaged : $product_stock->damaged;
        $data['type'] = $request->type ? $request->type : $product_stock->type;
        $data['available_quantity'] = $request->quantity ? $request->quantity : $product_stock->available_quantity;
        $total = $request->selling_price ? $request->selling_price : $product_stock->selling_price;
        $vat = 0;
        $discount = 0;
        if (!empty($request->vat) && $request->vat > 0) {
            $vat = ($request->vat * $total) / 100;
        }
        if (!empty($request->discount) && $request->discount > 0) {
            $discount = ($request->discount * $total) / 100;
        }
        $final_selling_price = $total + $vat + $discount;
        $data['final_price'] = $final_selling_price;
        if ($product_stock->update($data)) {
            $product_stock_log = ProductStockLog::query()
                ->where('sku','=', $request->sku)
                ->first();
            $log_data['branch_id'] = $request->branch_id ? $request->branch_id : $product_stock_log->branch_id;
            $log_data['product_id'] = $request->product_id ? $request->product_id : $product_stock_log->product_id;
            $log_data['unit_id'] = $request->unit_id ? $request->unit_id : $product_stock_log->unit_id;
            $log_data['supplier_id'] = $request->supplier_id ? $request->supplier_id : $product_stock_log->supplier_id;
            $log_data['per_unit_price'] = $request->per_unit_price ? $request->per_unit_price : $product_stock_log->per_unit_price;
            $log_data['quantity'] = $request->quantity ? $request->quantity : $product_stock_log->quantity;
            $log_data['cost'] = $request->cost ? $request->cost : $product_stock_log->cost;
            $product_stock_log->product_stock_id = $product_stock->id;
            if ($product_stock_log->update($log_data))
            {
                return Helper::response_with_data(ProductStock::with('product_stock_log', 'product')->find($product_stock->id), false);
            }
        }
        return Helper::response_with_data(null, true);
    }
public function update ( Request $ request , ProductStock $ product_stock ) { $ request -> validate ( [ 'selling_price' => 'nullable|numeric' , 'vat' => 'nullable|numeric' , 'discount' => 'nullable|numeric' , 'final_price' => 'nullable|numeric' , 'buying_price' => 'nullable|numeric' , 'damaged' => 'nullable|numeric' , 'type' => 'nullable|string' , 'available_quantity' => 'nullable|string' , 'branch_id' => 'nullable|exists:branches,id' , 'product_id' => 'nullable|exists:products,id' , 'unit_id' => 'nullable|exists:units,id' , 'supplier_id' => 'nullable|exists:suppliers,id' , 'per_unit_price' => 'nullable|numeric' , 'quantity' => 'nullable|numeric' , 'cost' => 'nullable|numeric' , 'sku' => 'required|exists:product_stock_logs,sku' , ] ) ; $ data [ 'branch_id' ] = $ request -> branch_id ? $ request -> branch_id : $ product_stock -> branch_id ; $ data [ 'product_id' ] = $ request -> product_id ? $ request -> product_id : $ product_stock -> product_id ; $ data [ 'selling_price' ] = $ request -> selling_price ? $ request -> selling_price : $ product_stock -> selling_price ; $ data [ 'vat' ] = $ request -> vat ? $ request -> vat : $ product_stock -> vat ; $ data [ 'discount' ] = $ request -> discount ? $ request -> discount : $ product_stock -> discount ; $ data [ 'buying_price' ] = $ request -> per_unit_price ? $ request -> per_unit_price : $ product_stock -> buying_price ; $ data [ 'damaged' ] = $ request -> damaged ? $ request -> damaged : $ product_stock -> damaged ; $ data [ 'type' ] = $ request -> type ? $ request -> type : $ product_stock -> type ; $ data [ 'available_quantity' ] = $ request -> quantity ? $ request -> quantity : $ product_stock -> available_quantity ; $ total = $ request -> selling_price ? $ request -> selling_price : $ product_stock -> selling_price ; $ vat = 0 ; $ discount = 0 ; if ( ! empty ( $ request -> vat ) && $ request -> vat > 0 ) { $ vat = ( $ request -> vat * $ total ) / 100 ; } if ( ! empty ( $ request -> discount ) && $ request -> discount > 0 ) { $ discount = ( $ request -> discount * $ total ) / 100 ; } $ final_selling_price = $ total + $ vat + $ discount ; $ data [ 'final_price' ] = $ final_selling_price ; if ( $ product_stock -> update ( $ data ) ) { $ product_stock_log = ProductStockLog :: query ( ) -> where ( 'sku' , '=' , $ request -> sku ) -> first ( ) ; $ log_data [ 'branch_id' ] = $ request -> branch_id ? $ request -> branch_id : $ product_stock_log -> branch_id ; $ log_data [ 'product_id' ] = $ request -> product_id ? $ request -> product_id : $ product_stock_log -> product_id ; $ log_data [ 'unit_id' ] = $ request -> unit_id ? $ request -> unit_id : $ product_stock_log -> unit_id ; $ log_data [ 'supplier_id' ] = $ request -> supplier_id ? $ request -> supplier_id : $ product_stock_log -> supplier_id ; $ log_data [ 'per_unit_price' ] = $ request -> per_unit_price ? $ request -> per_unit_price : $ product_stock_log -> per_unit_price ; $ log_data [ 'quantity' ] = $ request -> quantity ? $ request -> quantity : $ product_stock_log -> quantity ; $ log_data [ 'cost' ] = $ request -> cost ? $ request -> cost : $ product_stock_log -> cost ; $ product_stock_log -> product_stock_id = $ product_stock -> id ; if ( $ product_stock_log -> update ( $ log_data ) ) { return Helper :: response_with_data ( ProductStock :: with ( 'product_stock_log' , 'product' ) -> find ( $ product_stock -> id ) , false ) ; } } return Helper :: response_with_data ( null , true ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
//Stock //common // Stock Log //return $data;