<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\CIQCompany;
use App\Chalan;
use App\ChalanProduct;
use App\product_category;
use DB;
use PDF;
use Validator;

class ChalanController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $chalans = Chalan::get();
        return view('Admin.company_chalan.index', compact('chalans'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $chalan_no = Chalan::count();
        $chalan_no = date('Y').($chalan_no + 1);

        $companies = CIQCompany::where('status', 1)->get();
        $products = DB::table('product_productinfo')->where('status', 1)->get();
        $categories = product_category::orderBy('category_name')->get();
        return view('Admin.company_chalan.create', compact('companies', 'products', 'chalan_no', 'categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
          'company_id' => 'required',
          'chalan_no' => 'required|unique:chalans',
          'date' => 'required',
          'category_id.*' => 'required',
          'product_id.*' => 'required',
          'qty.*' => 'required',
        ]);

        if ($validator->fails()) {
          return redirect()->back()
                   ->withErrors($validator)
                   ->withInput();
        }

        $data = new Chalan();
        $data->company_id = $request->company_id;
        $data->chalan_no = $request->chalan_no;
        $data->date = $request->date;
        $data->total = $request->total;
        $data->status = 1;
        $data->save();

        $category_id= $request->category_id;
        $product_id= $request->product_id;
        $qty= $request->qty;
        $unit_price= $request->unit_price;
        if ($product_id) {
            foreach ($category_id as $key => $value ){
                if(!empty($value)){

                    $product = DB::table('product_productinfo')->where('id', $product_id[$key])->first();

                    $com_inv_pro= new ChalanProduct();
                    $com_inv_pro->chalan_id=$data->id;
                    $com_inv_pro->category_id=$value;
                    $com_inv_pro->product_id=$product_id[$key];
                    $com_inv_pro->qty =$qty[$key];
                    $com_inv_pro->description =$product->product_name;
                    $com_inv_pro->unit_price =$unit_price[$key];
                    $com_inv_pro->save(); 
                }
            }   
        }

        $notification=array(
            'messege'   =>'Chalan Added Successfull',
            'alert-type'=>'success'
        );

        return redirect()->back()->with($notification); 
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = Chalan::find($id);
        $data_items = ChalanProduct::where('chalan_id', $id)->get();
        $company = CIQCompany::where('id', $data->company_id)->first();
        $pdf = PDF::loadView('Admin.company_chalan.chalan_pdf',  $data , compact('data', 'company', 'data_items'));
        return $pdf->stream($company->name.'-Chalan-'.$data->date.'.pdf', compact('data'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $companies = CIQCompany::where('status', 1)->get();
        $products = DB::table('product_productinfo')->orderBy('product_name')->where('status', 1)->get();
        $data = Chalan::find($id);
        $data_items = ChalanProduct::where('chalan_id', $id)->get();
        $company = CIQCompany::where('id', $data->company_id)->first();
        $categories = product_category::orderBy('category_name')->get();
        return view('Admin.company_chalan.edit', compact('companies', 'products', 'data', 'data_items', 'company', 'categories'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = Chalan::find($id);
        $data->company_id = $request->company_id;
        $data->chalan_no = $request->chalan_no;
        $data->date = $request->date;
        $data->total = $request->total;
        $data->status = 1;
        $data->save();

        ChalanProduct::where('chalan_id', $id)->delete();
        $category_id= $request->category_id;
        $product_id= $request->product_id;
        $qty= $request->qty;
        $unit_price= $request->unit_price;
        if ($product_id) {
            foreach ($category_id as $key => $value ){
                if(!empty($value)){

                    $product = DB::table('product_productinfo')->where('id', $product_id[$key])->first();

                    $com_inv_pro= new ChalanProduct();
                    $com_inv_pro->chalan_id=$data->id;
                    $com_inv_pro->category_id=$value;
                    $com_inv_pro->product_id=$product_id[$key];
                    $com_inv_pro->qty =$qty[$key];
                    $com_inv_pro->description =$product->product_name;
                    $com_inv_pro->unit_price =$unit_price[$key];
                    $com_inv_pro->save(); 
                }
            }   
        }

        $notification=array(
            'messege'   =>'Chalan Added Successfull',
            'alert-type'=>'success'
        );

        return redirect()->back()->with($notification); 
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = Chalan::findOrFail($id);
        $data->delete();
        ChalanProduct::where('chalan_id', $id)->delete();
        $notification = array(
            'messege'   => 'Delete Successfull',
            'alert-type' => 'error'
        );
        return redirect()->back()->with($notification);
    }
}
