<?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\AccountCreditNote;

class CreditNoteExport 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 = AccountCreditNote::where("account_credit_notes.admin_id","=",$this->admin_id)
        ->leftJoin("account_customers","account_customers.id","=","account_credit_notes.customer_id");

        if($start_date && $end_date)
        {
            $query->whereDate("account_credit_notes.created_at",">=",$start_date);
            $query->whereDate("account_credit_notes.created_at","<=",$end_date);
        }
        
		
		$customer =  $query->select(  
                "account_customers.name as customer_name",
                "account_credit_notes.credit_note_number",
                "account_credit_notes.invoice_id",
                "account_credit_notes.invoice_date",
                "account_credit_notes.date",
                "account_credit_notes.reason",
                "account_credit_notes.description",
                "account_credit_notes.shipping_charge",
                "account_credit_notes.subtotal",
                "account_credit_notes.cgst_total",
                "account_credit_notes.sgst_total",
                "account_credit_notes.igst_total",
                "account_credit_notes.cess_total",
                "account_credit_notes.grand_total",
                "account_credit_notes.status"

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

           
            return $customer;
        
	}

	public function headings(): array
	{
		return [
            "Customer Name",
            "Credit Note Number",
            "Invoice Id",
            "Invoice Date",
            "Date",
            "Reason",
            "Description",
            "Shipping Charge",
            "Subtotal",
            "CGST Total",
            "SGST Total",
            "IGST Total",
            "Cess Total",
            "Grand Total",
            "Status"
        ];
	}
}
