<?php

namespace App\Http\Controllers\Dashboard;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Invoice;
use App\Customer;
use App\InvoiceItems;
use App\Payments;
use PHPUnit\Framework\Constraint\Count;

class ReportsController extends Controller
{
    public function itemsToBeDelivired(Request $request){
        return view('dashboard.pages.reports.items_delivired');
    }

    public function reportItemsToBeDelivired(Request $request){
        $start_date = $request->start_date;
        $end_date = $request->end_date;
       
        if(isset($start_date) && isset($end_date)){

          $invoices = Invoice::leftJoin('invoice_items','invoice_items.invoice_id','invoices.id')
          ->leftJoin('products','products.product_id','invoice_items.product_id')
          ->leftJoin('categories','categories.id','products.category_id')        
          ->leftJoin('glass_lenses','glass_lenses.product_id','invoice_items.product_id')
          ->select('invoices.*','invoice_items.type','invoice_items.product_id','invoice_items.quantity','products.description as product_description','glass_lenses.description as lenses_description','categories.category_name')
          ->where('invoices.updated_at', '>=',$start_date)
          ->orwhere('invoices.updated_at','<=',$end_date)
          ->where('invoices.status','pending')
          ->distinct()
          ->get();
       
        }else{
         $invoices = null;   
        }

        return view('dashboard.pages.reports.print_items_delivired_report')->with(compact('invoices','start_date','end_date'));
    }

    public function getPendingInvoices(Request $request){
        return view('dashboard.pages.reports.pending_invoices');
    }

    public function reportgetPendingInvoices(Request $request){
        $date_from = $request->date_from;
        $total_net = 0;
        $total_paied = 0;
        $total_balance = 0;
        $totals = 0;
        $total_discount = 0;
        
        if(isset($date_from)){

            $invoices = Invoice::leftJoin('invoice_items','invoice_items.invoice_id','invoices.id')
                ->select('invoices.*')
                ->whereDate('invoices.created_at','<=', $date_from)
                ->where('invoices.status','delivered')
                ->distinct()
                ->get();
        }else
            $invoices = null;

        foreach($invoices as $invoice){
            $total_net += $invoice->total;
            $total_paied += $invoice->paied;
            $total_balance += $invoice->remaining;

            
            if($invoice->discount_type == 'fixed')
                $totals += ($invoice->total + $invoice->discount_value);
            elseif($invoice->discount_type == 'percentage')
                $totals += ($invoice->total / (1 - ($invoice->discount_value / 100)));

                
            if($invoice->discount_type == 'fixed')
                $total_discount += $invoice->discount_value;
            elseif($invoice->discount_type == 'percentage')
                $total_discount += (($invoice->total / (1 - ($invoice->discount_value / 100))) - $invoice->total);
            
        }
        
        return view('dashboard.pages.reports.print_pending_invoices_report')->with(compact('invoices','date_from','total_net','total_paied','total_balance','totals','total_discount'));
    }

     public function getReturnInvoices(Request $request){
        return view('dashboard.pages.reports.return_invoices');
    }

    public function reportgetReturnInvoices(Request $request){
        $date_from = $request->return_date_from;
        $date_to = $request->return_date_to;
        $total_net = 0;
        $total_paied = 0;
        $total_balance = 0;
        $totals = 0;
        $total_discount = 0;
        
        if(isset($date_from) && isset($date_to)){

           $invoices = Invoice::leftJoin('invoice_items','invoice_items.invoice_id','invoices.id')
            ->select('invoices.*')
            ->where('invoices.status','canceled')
            ->where(function ($q) use ($date_from ,$date_to ){
                $q->Where('invoices.updated_at','>=', $date_from);
                $q->orWhere('invoices.updated_at','<=', $date_to);
            })
            ->distinct()
            ->get();

        }else
            $invoices = null;
        
        foreach($invoices as $invoice){
            $total_net += $invoice->total;
            $total_paied += $invoice->paied;
            $total_balance += $invoice->remaining;

            if($invoice->discount_type == 'fixed')
                $totals += ($invoice->total + $invoice->discount_value);
            elseif($invoice->discount_type == 'percentage')
                $totals += ($invoice->total / (1 - ($invoice->discount_value / 100)));

                
            if($invoice->discount_type == 'fixed')
                $total_discount += $invoice->discount_value;
            elseif($invoice->discount_type == 'percentage')
                $total_discount += (($invoice->total / (1 - ($invoice->discount_value / 100))) - $invoice->total);
            
        }

        return view('dashboard.pages.reports.print_return_invoices_report')->with(compact('invoices','date_from','date_to','total_net','total_paied','total_balance','totals','total_discount'));
    }

    public function getCustomers(Request $request){
        return view('dashboard.pages.reports.customers');
    }

    public function reportgetCustomers(Request $request){
        $date_from = $request->date_from;
        $date_to = $request->date_to;
       if(isset($date_from) && isset($date_to)){

          $customers = Customer::where('customers.created_at', '>=',$date_from)
            ->orwhere('customers.created_at','<=',$date_to)
            ->get();
       }else{
           $customers = null;
       }
        return view('dashboard.pages.reports.print_customers_report')->with(compact('customers','date_from','date_to'));
    }

    
    public function getSalesTransactions(Request $request){
        return view('dashboard.pages.reports.sales_transactions');
    }

    public function reportSalesTransactions(Request $request){
        $date_from = $request->date_from;
        $date_to = $request->date_to;
        $total_net = 0;
        $total_paied = 0;
        $total_balance = 0;
        $totals = 0;
        $total_discount = 0;
         
        if(isset($date_from) && isset($date_to)){

          $invoices = Invoice::orderBy('created_at')
              ->whereDate('invoices.created_at', '>=',$date_from)
              ->whereDate('invoices.created_at','<=',$date_to)
              ->get()
              ->groupBy(function($item) {
              return $item->created_at->format('y-m-d');
          });

            foreach($invoices as $invoiceItems){
                foreach($invoiceItems as $invoice){
                
                    $total_net += $invoice->total;
                    $total_paied += $invoice->paied;
                    $total_balance += $invoice->remaining;

                    if($invoice->discount_type == 'fixed')
                        $totals += ($invoice->total + $invoice->discount_value);
                    elseif($invoice->discount_type == 'percentage')
                        $totals += ($invoice->total / (1 - ($invoice->discount_value / 100)));

                        
                    if($invoice->discount_type == 'fixed')
                        $total_discount += $invoice->discount_value;
                    elseif($invoice->discount_type == 'percentage')
                        $total_discount += (($invoice->total / (1 - ($invoice->discount_value / 100))) - $invoice->total);
                }        
            }

       }else
          $invoices = null;
       
        return view('dashboard.pages.reports.print_sales_transactions_report')->with(compact('invoices','date_from','date_to','total_net','total_paied','total_balance','totals','total_discount'));
    }

    public function getCashierTransactions(Request $request){
        return view('dashboard.pages.reports.cashier_transactions');
    }


    public function reportCashierTransactions(Request $request){
        $date_from = $request->date_from;
        $date_to = $request->date_to;
         
        if(isset($date_from) && isset($date_to)){

          $invoices = Invoice::leftJoin('payments','payments.invoice_id','invoices.id')
              ->leftJoin('customers','customers.customer_id','invoices.customer_id')
              ->leftJoin('users','users.id','payments.beneficiary')
              ->whereDate('invoices.created_at', '>=',$date_from)
              ->whereDate('invoices.created_at','<=',$date_to)
              ->select(['invoices.*','payments.type','payments.bank','payments.card_number','payments.expiration_date','payments.currency','payments.payed_amount','payments.exchange_rate','payments.local_payment','payments.created_at as payment_created_at'])
              ->groupBy('payments.id')
              ->get();

       }else
          $invoices = null;
       
        return view('dashboard.pages.reports.print_cashier_transactions_report')->with(compact('invoices','date_from','date_to'));
    }


    
    public function getSalesSummary(Request $request){
        return view('dashboard.pages.reports.sales_summary');
    }

    public function reportSalesSummary(Request $request){
        $date_from = $request->date_from;
        $date_to = $request->date_to;
         
        if(isset($date_from) && isset($date_to)){

          $products = InvoiceItems::leftJoin('products','products.product_id','invoice_items.product_id')
              ->leftJoin('glass_lenses','glass_lenses.product_id','invoice_items.product_id')
              ->leftJoin('categories','categories.id','products.category_id')
              ->whereDate('invoice_items.created_at', '>=',$date_from)
              ->whereDate('invoice_items.created_at','<=',$date_to)
              ->select(['invoice_items.*','categories.category_name','glass_lenses.description as lens_description','glass_lenses.price as lens_price','glass_lenses.retail_price as lens_retail_price','products.description','products.total','products.price','products.retail_price',\DB::raw('count(*) as item_count')])
              ->groupBy('invoice_items.product_id')
              ->get()
              ->groupBy(function($item) {
                return $item->category_name;
            });

        }else
          $products = null;

          return view('dashboard.pages.reports.print_sales_summary_report')->with(compact('products','date_from','date_to'));
    }

    public function getSalesSummarySalesman(Request $request){
        return view('dashboard.pages.reports.sales_summary_salesman');
    }

    public function reportSalesSummarySalesman(Request $request){
        $date_from = $request->date_from;
        $date_to = $request->date_to;
         
        if(isset($date_from) && isset($date_to)){

          $products = Invoice::leftJoin('invoice_items','invoice_items.invoice_id','invoices.id')
              ->leftJoin('products','products.product_id','invoice_items.product_id')
              ->leftJoin('users','users.id','invoices.user_id')
              ->leftJoin('glass_lenses','glass_lenses.product_id','invoice_items.product_id')
              ->leftJoin('categories','categories.id','products.category_id')
              ->whereDate('invoice_items.created_at', '>=',$date_from)
              ->whereDate('invoice_items.created_at','<=',$date_to)
              ->select(['invoice_items.*','invoices.invoice_code','categories.category_name','users.first_name as user_name','glass_lenses.description as lens_description','glass_lenses.price as lens_price','glass_lenses.retail_price as lens_retail_price','products.description','products.total','products.price','products.retail_price',\DB::raw('count(*) as item_count')])
              ->groupBy('invoice_items.product_id')
              ->get()
              ->groupBy(function($e) {
                return $e->user_name;
                });

        }else
          $products = null;

          return view('dashboard.pages.reports.print_sales_summary_salesman_report')->with(compact('products','date_from','date_to'));
    }


    public function reportReturningInvoice(Request $request){
        
        $invoices = Invoice::leftJoin('users', 'users.id', 'invoices.user_id')
            ->leftJoin('customers', 'customers.customer_id', 'invoices.customer_id')
            ->select(['invoices.*', 'users.first_name as user_name', 'customers.english_name as customer_name'])
            ->where('status', 'canceled')
            ->get();
        
        return view('dashboard.pages.reports.returning_invoice')->with(compact('invoices'));
    }

        public function printReturnInvoiceEn(Request $request, $id)
    {
        $invoice = Invoice::where('invoice_code', $id)->first();

        $invoiceItems = Invoice::where('invoice_code', $id)
            ->leftJoin('invoice_items', 'invoice_items.invoice_id', 'invoices.id')
            ->leftJoin('products', 'products.product_id', 'invoice_items.product_id')
            ->select('invoice_items.*', 'products.description', 'products.discount_type', 'products.discount_value')->get();

        $payments = Payments::where('invoice_id',$invoice->id)->first();

        return view('dashboard.pages.reports.print_return_invoice_en')->with(compact('invoice', 'invoiceItems', 'id','payments'));
    }

    public function printReturnInvoiceAr(Request $request, $id)
    {
        $invoice = Invoice::where('invoice_code', $id)->first();

        $invoiceItems = Invoice::where('invoice_code', $id)
            ->leftJoin('invoice_items', 'invoice_items.invoice_id', 'invoices.id')
            ->leftJoin('products', 'products.product_id', 'invoice_items.product_id')
            ->select('invoice_items.*', 'products.description', 'products.discount_type', 'products.discount_value')->get();

        $payments = Payments::where('invoice_id',$invoice->id)->first();

        return view('dashboard.pages.reports.print_return_invoice_ar')->with(compact('invoice', 'invoiceItems', 'id','payments'));
    }
}