CasperSecurity
<?php
namespace App\Exports;
use App\Models\FinancialTransactions;
use App\Models\FinancialTransactionsLedgerHead;
use App\Models\Bank;
use App\Models\CashType; // Make sure this is imported
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
class BankBookExport implements FromCollection, WithHeadings, WithMapping
{
protected $start_date;
protected $end_date;
protected $bank_name;
protected $cash_type; // Add cash_type as a property
public function __construct($start_date, $end_date, $bank_name = null, $cash_type = null)
{
$this->start_date = $start_date;
$this->end_date = $end_date;
$this->bank_name = $bank_name; // Set the bank_name property
$this->cash_type = $cash_type; // Set the cash_type property
}
public function collection()
{
// If bank_name is set, get Bank Book data
if ($this->bank_name) {
list($bank_nm, $account_no) = explode('_', $this->bank_name); // Split bank name and account number
$bank = Bank::where('bank_name', $bank_nm)->where('bank_account_no', $account_no)->latest()->first();
if ($bank) {
// Fetch the financial transactions for the given bank within the date range, and only those with voucher_status = 2
return FinancialTransactions::where('bank_id', $bank->id)
->whereDate('voucher_date', '>=', $this->start_date)
->whereDate('voucher_date', '<=', $this->end_date)
->where('voucher_status', 2) // Only include transactions with voucher_status = 2
->get();
}
}
// If cash_type is set, get Cash Book data
if ($this->cash_type) {
$cash = CashType::where('cash_type_name', $this->cash_type)->latest()->first();
if ($cash) {
// Fetch the financial transactions for the given cash type within the date range, and only those with voucher_status = 2
return FinancialTransactions::where('cash_type_id', $cash->id)
->whereDate('voucher_date', '>=', $this->start_date)
->whereDate('voucher_date', '<=', $this->end_date)
->where('voucher_status', 2) // Only include transactions with voucher_status = 2
->get();
}
}
// Return an empty collection if neither bank_name nor cash_type is provided
return collect([]);
}
// Map the financial transaction details to Excel columns
public function map($transaction): array
{
// Get all financial transaction ledger head details for this transaction
$details = FinancialTransactionsLedgerHead::select('financial_transactions_ledger_heads.*', 'ledger_heads.ledger_head_name')
->join('ledger_heads', 'financial_transactions_ledger_heads.ledger_head_id', '=', 'ledger_heads.id')
->where('finance_transaction_id', $transaction->id)
->get();
$mapped = [
'Voucher Date' => $transaction->voucher_date,
'Voucher Number' => $transaction->voucher_no,
'Debit' => $transaction->total_debit_amount,
'Credit' => $transaction->total_credit_amount,
];
// Loop through all the ledger details for this transaction and map them
$ledgerDetails = [];
foreach ($details as $detail) {
$ledgerDetails[] = [
'Ledger Head Name' => $detail->ledger_head_name,
'Balance' => $detail->amount,
];
}
// Combine the mapped data with the ledger details
return array_merge($mapped, ...$ledgerDetails);
}
// Set the headings for the Excel file
public function headings(): array
{
return [
'Voucher Date',
'Voucher Number',
'Debit ',
'Credit',
'Ledger Head Name',
'Balance',
];
}
}