<?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\TaxDetails;
use App\Models\Tax;
use DB;
use Carbon\Carbon;
class AllTaxController 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_tax',compact('company_listing'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    // public function all_tax_statement($datefrom,$dateto,$customer_id,Request $request)
    // {
        
    //     //   $transaction1=Tax::selectRaw('users.company_name,users.trn_no,tax.invoice_no,SUM(sell_lines.quantity) AS quantity,users.amount,sell_lines.tax')
    //     //                 ->leftjoin('users','users.id','=','tax.customer_id')
    //     //                 ->join('transaction','transaction.customer_id','=','tax.customer_id')
    //     //                 ->leftjoin('sell_lines','sell_lines.transaction_id','=','transaction.id');

    //     // $transaction1->where('sell_lines.tax','=','')->where('transaction.type','=','sale') ->orderBy('tax.invoice_no', 'asc');
    //     // if(!empty($request->datefrom)){
    //     //  $datefrom=date('Y-m-d',strtotime($request->datefrom));

    //     //  $transaction1->whereDate('tax.datefrom', '>=', $datefrom);

    //     // }
    //     // if (!empty($request->dateto)) {
    //     //     $dateto=date('Y-m-d',strtotime($request->dateto));
    //     //     $transaction1->whereDate('tax.dateto', '<=', $dateto);
    //     // }
    //     // $transaction=$transaction1->groupBy(['users.company_name','users.trn_no','tax.invoice_no','tax.id','users.amount','sell_lines.tax'])->get();
    //   $transaction=Tax::with([''])->get();
    //   print_r($transaction);die;
    //      $month=date('F', strtotime($request->datefrom));
    //      $year=date('Y', strtotime($request->datefrom));
         
    //      return view('all_tax_statement_result',compact('month','transaction','year'));
      
    // }
    
    // public function all_tax_statement($datefrom, $dateto, Request $request)
    // {
    //     // $transaction1 = Tax::selectRaw('COALESCE((SELECT SUM(a.quantity) FROM sell_lines a LEFT JOIN transaction b on b.id=a.transaction_id WHERE b.transaction_date >="' . $datefrom . '" AND b.transaction_date <="' . $dateto . '" and b.type="sale"),0) as quantity,users.company_name,users.trn_no,tax.invoice_no,users.amount')
    //     // ->leftjoin('users', 'users.id', '=', 'tax.customer_id')->orderBy('tax.invoice_no', 'asc');
    //     $transaction1 = Tax::selectRaw('users.company_name,users.trn_no,tax.invoice_no,SUM(sell_lines.quantity) AS quantity,users.amount,sell_lines.tax,sell_lines.unit_price')
    //         ->leftjoin('users', 'users.id', '=', 'tax.customer_id')
    //         ->join('transaction', 'transaction.customer_id', '=', 'tax.customer_id')
    //         ->leftjoin('sell_lines', 'sell_lines.transaction_id', '=', 'transaction.id');

    //     $transaction1->where('sell_lines.tax', '!=', '')->where('transaction.type', '=', 'sale')->orderBy('tax.invoice_no', 'asc');
    //     if (!empty($request->datefrom)) {
    //         $datefrom = date('Y-m-d', strtotime($request->datefrom));

    //         $transaction1->whereDate('tax.datefrom', '>=', $datefrom);
    //         $transaction1->whereDate('transaction.transaction_date', '>=', $datefrom);
    //     }
    //     if (!empty($request->dateto)) {
    //         $dateto = date('Y-m-d', strtotime($request->dateto));
    //         $transaction1->whereDate('tax.dateto', '<=', $dateto);
    //         $transaction1->whereDate('transaction.transaction_date', '<=', $dateto);
    //     }
    //     $transaction = $transaction1->groupBy(['users.company_name', 'users.trn_no', 'tax.invoice_no', 'tax.id', 'users.amount', 'sell_lines.tax', 'sell_lines.unit_price'])->get();
    //     // print_r($transaction);
    //     // die;
    //     $month = date('F', strtotime($request->datefrom));
    //     $year = date('Y', strtotime($request->datefrom));
    //     return view('all_tax_statement_result', compact('month', 'transaction', 'year'));
    // }


 public function all_tax_statement($datefrom, $dateto, Request $request)
    {

        $transaction = TaxDetails::where('datefrom','>=',$datefrom)->where('dateto','<=',$dateto)->orderBy('invoice_no', 'asc')->get()->toArray();
        // print_r($transaction);die;
        if(date('F', strtotime($request->datefrom)) == date('F', strtotime($dateto))){
                 $month = date('F', strtotime($request->datefrom));
        }else{
                 $month = date('F', strtotime($datefrom)) .'-'. date('F', strtotime($dateto));
        }
   
        $year = date('Y', strtotime($request->datefrom));
        return view('all_tax_statement_result', compact('month', 'transaction', 'year'));
    }











    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * 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)
    {
        //
    }
}
