<?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 TransactionStatementController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
         if(auth()->user()->role == 'client'){
               $company_listing=User::where('role','client')->where('users.id',auth()->user()->id)->get(); 
          }else{
               $company_listing=User::where('role','client')->get();
          } 
        
        return view('transaction-statement',compact('company_listing'));
    }


    function fetchTransactionData(Request $request){
      
        $result = array('data' => array());
         if(auth()->user()->role == 'client'){
               $report=Transaction::select(DB::raw('SUM(transaction.amount) as total'),'transaction.customer_id')
                            ->leftjoin('users','users.id','=','transaction.customer_id')
                            ->where('users.id','=',auth()->user()->id)
                            ->where('transaction.type','=','sale')
                            ->groupBy('transaction.customer_id');
               
          }else{
               $report=Transaction::select(DB::raw('SUM(transaction.amount) as total'),'transaction.customer_id')
                            ->leftjoin('users','users.id','=','transaction.customer_id') 
                            ->where('transaction.type','=','sale')
                            ->groupBy('transaction.customer_id');$company_listing=User::where('role','client')->get();
          } 
      

// print_r($report);die;
        if(!empty($request->datefrom)){
         $datefrom=date('Y-m-d',strtotime($request->datefrom));

         $report->whereDate('transaction.transaction_date', '>=', $datefrom);

        }
        if (!empty($request->dateto)) {
            $dateto=date('Y-m-d',strtotime($request->dateto));
            $report->whereDate('transaction.transaction_date', '<=', $dateto);
        }
      
        if (!empty($request->company_id)) {
            if (auth()->user()->role == 'admin' || auth()->user()->role == 'users') {
                $report->where('transaction.customer_id','=',$request->company_id);
            }
           
        }
// && auth()->user()->role == 'users
        $data=$report->get();
        // echo $request->company_id;die;

         $i=1;
         foreach ($data as $key => $value) {
          $username=User::where('id','=',$value['customer_id'])->first();
          // print_r($username);die;
            $button='';
            
                  $button.=' <td> <a  class="btn bg-gradient-green btn-sm" href="'.url('statement/'.$username['id'].'/'.$datefrom.'/'.$dateto).'" target="_blank">Print</a>
                </td>';


                $result['data'][$key] = array(
                $i,
                $username['company_name'],$value['total'],
                $button,
            );
       $i++;  }
     echo json_encode($result);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

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

    function statement($customer_id,$datefrom,$dateto){
date_default_timezone_set('Asia/Dubai');
        $transaction=Transaction::select('sell_lines.*','transaction.*','transaction.created_at as new_time','users.*','vehicles.*')->leftjoin('sell_lines','sell_lines.transaction_id','=','transaction.id')->leftjoin('users','users.id','=','transaction.customer_id')->leftjoin('vehicles','vehicles.id','=','transaction.vehicle_number')->where('transaction.customer_id','=',$customer_id)->whereDate('transaction.transaction_date', '>=', $datefrom)->whereDate('transaction.transaction_date', '<=', $dateto)->orderBy('transaction.id', 'asc')->get();
        // print_r($transaction);die;
        $company_data=User::where('id','=',$customer_id)->first();
        $invoice_no=Tax::where('tax.customer_id','=',$customer_id)->whereDate('tax.datefrom', '>=', $datefrom)->whereDate('tax.dateto', '<=', $dateto)->orderBy('tax.id', 'DESC')->first();
        // print_r($transaction);die;
        return view('statement',compact('datefrom','dateto','transaction','company_data','invoice_no'));
    }

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