<?php

namespace App\Exports;

use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Support\Facades\Session;
use App\Models\{
	User
};
use App\Models\accounting\AccountQuote;

class QuotationExport implements FromCollection, WithHeadings
{
	/**
	 * @return \Illuminate\Support\Collection
	 */
    private $admin_id;

    public function __construct($admin_id,)
    {
        $this->admin_id = $admin_id;
        
    }
    
	public function collection()
	{
        
        $start_date = isset($_GET["start_date"]) ? $_GET["start_date"] : null;
        $end_date = isset($_GET["end_date"]) ? $_GET["end_date"] : null;

		$query = AccountQuote::where("account_quotes.admin_id","=",$this->admin_id)
        ->leftJoin("account_customers","account_customers.id","=","account_quotes.customer_id");

        if($start_date && $end_date)
        {
            $query->whereDate("account_quotes.created_at",">=",$start_date);
            $query->whereDate("account_quotes.created_at","<=",$end_date);
        }
        
		
		$customer =  $query->select(  
                "account_customers.name as customer_name",
                "account_quotes.quotation_number",
                "account_quotes.quotation_date",
                "account_quotes.valid_date",
                "account_quotes.po_number",
                "account_quotes.description",
                "account_quotes.shipping_charge",
                "account_quotes.subtotal",
                "account_quotes.cgst_total",
                "account_quotes.sgst_total",
                "account_quotes.igst_total",
                "account_quotes.cess_total",
                "account_quotes.grand_total",
                "account_quotes.status"

            )->orderBy('account_quotes.created_at', 'DESC')->get();

           
            return $customer;
        
	}

	public function headings(): array
	{
		return [
            "Customer Name",
            "Quotation Number",
            "Quotation Date",
            "Valid Date",
            "PO Number",
            "Description",
            "Shipping Charge",
            "Subtotal",
            "CGST Total",
            "SGST Total",
            "IGST Total",
            "Cess Total",
            "Grand Total",
            "Status"
        ];
	}
}
