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

class PurchaseOrderExport 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 = AccountPurchaseOrder::where("account_purchase_orders.admin_id","=",$this->admin_id)
        ->leftJoin("account_customers","account_customers.id","=","account_purchase_orders.vendor_id");

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

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

           
            return $customer;
        
	}

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