<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use App\Models\Transaction;
use App\Models\SellLines;
use App\Models\Tax;
use DB;
class AllTransaction extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $company_listing=User::where('role','client');
        if(auth()->user()->role=='client'){
            $company_listing->where('id',auth()->user()->id);
        }
        $company_listing->get();
        return view('all_statement',compact('company_listing'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function all_statement($datefrom,$dateto,$customer_id,$tax_id)
    {
        // echo $tax_id;die;
        
        $transaction1=SellLines::selectRaw('SUM(sell_lines.quantity) AS quantity,SUM(sell_lines.kilometer) AS kilometer,SUM(sell_lines.quantity_ltr) AS quantity_ltr,SUM(sell_lines .return_qty) AS return_qty,transaction.ledger_value,users.company_name,users.trn_no,sell_lines.unit_price,transaction.transaction_date,sell_lines.tax')
        ->leftjoin('transaction','transaction.id','=','sell_lines.transaction_id')
        ->leftjoin('users','users.id','=','transaction.customer_id')
        ->whereDate('transaction.transaction_date', '>=', $datefrom)
        ->whereDate('transaction.transaction_date', '<=', $dateto)
        ->where('transaction.type','=','sale')
        ->orderBy('transaction.transaction_date', 'asc');
        if ($customer_id !='all') {
            $transaction1->where('transaction.customer_id','=',$customer_id);
        }
        if ($tax_id =='tax') {
            
            $transaction1->where('sell_lines.tax','!=','');
        }
        if ($tax_id =='notax') {
          
            $transaction1->where('sell_lines.tax','=','');
        }
       
        if ($customer_id !='all') {
            $customer_data=User::find($customer_id);
        }else{
            $customer_data=0;
        }
         
        $transaction=$transaction1->groupBy(['transaction.ledger_value','users.company_name','users.trn_no','sell_lines.unit_price','transaction.transaction_date','sell_lines.tax'])->orderBy('transaction.ledger_value', 'asc')->get()->toArray();
        
         if ($customer_id !='all') {
         $invoice_no=Tax::where('tax.customer_id','=',$customer_id)->whereDate('tax.datefrom', '>=', $datefrom)->whereDate('tax.dateto', '<=', $dateto)->orderBy('tax.id', 'DESC')->first();
         }else{
            $invoice_no=0;
        }
    //   if(empty($transaction) && empty($customer_data) && empty($invoice_no)){
    //         return redirect('all')->with('success',' No data found');
    //     }
      
        // print_r($transaction);die;
        if(empty($transaction)){
            echo 'Data not Found';die;
        }
         return view('all_statement_result',compact('datefrom','dateto','transaction','customer_data','invoice_no'));
      
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // ->where('transaction.customer_id','=',$customer_id)->whereDate('transaction.transaction_date', '>=', $datefrom)->whereDate('transaction.transaction_date', '<=', $dateto)
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
