Monday, May 22, 2017

Displaying In stock and out stock in my video library

I have stored number of stocks in my Video model and also quantity of the customer's rented video in my Rental Model. The thing is I have to deduct the quantity from the stocks. I'm having difficulty doing the adding and subtracting.

RentalController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Http\Requests;
use App\Rental;
use App\Customer;
use App\Video;
use App\Category;
use Auth;
use App\RentalDetail;
use DB;

class RentalController extends Controller {

    public function index() {
        $rentals = Rental::all();
        if(Auth::guest()){
            return redirect('home');
        }

        return view('rental.index',compact('rentals'));
    } 

    public function create(Request $request) {

        $this->validate($request ,[
            'customer.*.name' => 'required',
            'videos.*.id' => 'required'
            ]);

        $rental = new Rental;
        $customers = Customer::pluck('name' , 'id');
        // $videos = Video::pluck('title' , 'id');
        $videos = Video::with('category')->get();

        return view('rental.create',compact('rental' ,'customers','videos'));

    }

    public function store(Request $request) {
        $this->validate($request,[
             'customer.*.name' => 'required',
            'videos.*.id' => 'required'
            ]);

        $data = $request->all();

        DB::rental_details(function() use ($data){

            $rental = new Rental;
            $rental->customer_id = $data['customer_id'];
            $rental->date_rented = Carbon::now(); 
            $rental->interest_amount = 0;
            $rental->status = 'Rented';
            $total_amount = 0;
            $rental->save();    


            foreach ($data['videos'] as $video) {
                $detail = new RentalDetail;
                $detail->video_id = $video['id'];
                $detail->rental_id = $rental->id;
                $detail->quantity = 1;
                // $detail->stock+=Category::find($category['id'])->category
                $detail->status = 'Rented';
                $detail->price += Video::find($video['id'])->category->price;
                $detail->save();

                $total_amount += $detail->price;  
            }

            $rental->total_amount = $total_amount;
            $rental->save();


        });

        flash('Record Successfully Saved','success');
        return redirect()->action('RentalController@index');
    }

    public function edit(Rental $rental) {

        $customers = Customer::pluck('name' , 'id');
         $videos = Video::with('category')->get();

        return view('rental.create',compact('rental' ,'customers','videos'));
    }

    public function update(Request $request, Rental $rental) {
         $this->validate($request ,[
            'videos.*.id' => 'required'
            ]);
        $data = $request->all();

           $rental->customer_id = $data['customer_id'];
            $rental->video_title=$data['video_title'];
            $rental->date_rented = Carbon::now(); 
            $rental->total_amount=$data['total_amount'];
            $rental->save();

        flash('Record Successfully Updated.','success');
        return redirect()->action('RentalController@index');
    }

    public function destroy(Rental $rental) {
        $rental->delete();
        flash('Record Successfully Deleted.','success');
        return redirect()->action('RentalController@index');
    }

    public function show(Rental $rental) {

        return view('rental.show',compact('rental'));
    }
}

VideoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Video;
use App\Category;
use App\DataTables\VideoDataTable;

class VideoController extends Controller {
    public function __construct(){
        $this->middleware('auth');
    }

    public function index(VideoDataTable $dataTable)
    {
        return $dataTable->render('video.index');

    }

    public function create(){
        $video= new Video;

        $categories = Category::pluck('name','id');

        return view('video.create', compact('video','categories'));
    }

    public function store(Request $request) {
        $data = $request->all();
        $video = new Video;
        $video->title = $data['title'];
        $video->description = $data['description'];
        $video->category_id = $data['category_id'];
        $video->stock = $data['stock'];
        $video->save();

        flash('Request Successfully Saved', 'success');
        return redirect()->action('VideoController@index');
    }

    public function edit(Video $video) {

        $categories = Category::pluck('name','id');
        return view('video.edit',compact('video','categories'));
    }

    public function update(Request $request, Video $video) {
        $data = $request->all();

      $video->title = $data['title'];
        $video->description = $data['description'];
        $video->category_id = $data['category_id'];
        $video->stock = $data['stock'];

        $video->save();

    flash('Request Successfully Updated', 'success');
        return redirect()->action('VideoController@index');
    }

    public function destroy(Video $video) {
        $video->delete();

        flash('Request Successfully Deleted', 'success');
        return redirect()->action('VideoController@index');
    }

    public function show(Video $video) {

        return view('video.show', compact('video'));
    }
}

Database of Rental

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRentalsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('rentals', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamp('date_rented');
            $table->integer('amount');
            $table->integer('total_amount');
            $table->integer('interest_amount');
            $table->string('status')->default('Rented'); // rented | returned

            $table->timestamps();
            $table->integer('customer_id')->unsigned();
            $table->foreign('customer_id')
            ->on('customers')
            ->references('id')
            ->onDelete('restrict');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('rentals');
    }
}

Database of Videos

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVideosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('videos', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('description');

            $table->integer('stock');
            $table->timestamps();

            $table->integer('category_id')->unsigned();
            $table->foreign('category_id')
                ->on('categories')
                ->references('id')
                ->onUpdate('cascade')
                ->onDelete('restrict');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('videos');
    }
}

Do I need to add another data table like in stock and out stock in the rental? Can you help me out by giving some example codes? Thank you



via Gold May Davao

Advertisement