Created
June 30, 2023 02:03
-
-
Save HakamRaza/60fcf9444bc8983e2ae7c07490b6121f to your computer and use it in GitHub Desktop.
Export table to .csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Payroll\PayrollBankFile; | |
use App\Models\Bank; | |
use App\Models\CompanyPayrollDetail; | |
use App\Models\Staff; | |
use Illuminate\Support\Facades\Crypt; | |
trait PayrollCSVTrait | |
{ | |
public function downloadCsv($staffWagesArr) | |
{ | |
$bank = 'Payroll'; | |
$fileName = $bank . '_' . date("Ymd_Hi"); // add date now plus date range to naming | |
$headers = array( | |
"Content-type" => "text/csv", | |
"Content-Disposition" => "attachment; filename=$fileName.csv", | |
"Pragma" => "no-cache", | |
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0", | |
"Expires" => "0", | |
); | |
$columnTitles = array( // define top column titles | |
'Payment Mode', | |
'Value Date', | |
'Customer Reference No.', | |
'Favourite Beneficiary Code', | |
'Transaction Amount', | |
'Credit Account No.', | |
'Beneficiary Name 1', | |
'Beneficiary Name 2', | |
'Beneficiary Name 3', | |
'New NRIC', | |
'Old NRIC', | |
'Business Registration No.', | |
'Police/ Army/ Passport No.', | |
'Beneficiary Bank Code', | |
'Email', | |
'Advice Detail', | |
'Debit Desc.', | |
'Credit Desc.', | |
'Joint Name', | |
'Joint Old ID', | |
'Joint New ID', | |
'Joint Business Reg. No.', | |
'Joint Police/ Army/ Passport No.', | |
'Purpose of Transfer', | |
'Other Purpose of Transfer', | |
'Rentas Instruction To Bank', | |
); | |
$date = date('dmY'); | |
//when date starts with zero | |
if (substr($date, 0, 1) == 0) { | |
$date = "'$date"; | |
} | |
$callback = function () use ($staffWagesArr, $columnTitles, $date) { | |
$file = fopen('php://output', 'w'); | |
fputcsv($file, $columnTitles); | |
//keep track of number | |
$i = 1; | |
//data records | |
foreach ($staffWagesArr as $staffId => $wage) { | |
$staff = Staff::firstWhere('id', $staffId); | |
$bankId = $staff->payrollStaffDetails->bank_id; | |
$bankCode = Bank::where('id', $bankId)->first()->bank_code; | |
if ($staff->payrollStaffDetails->bank_id === 1) { | |
$paymentMethod = 'IT'; | |
} else { | |
$paymentMethod = 'IG'; | |
} | |
$row['Payment Mode'] = $paymentMethod; | |
$row['Value Date'] = $date; | |
$row['Customer Reference No.'] = $i; | |
$row['Favourite Beneficiary Code'] = $staff->payrollStaffDetails->favourite_beneficiary_code; | |
$row['Transaction Amount'] = number_format($wage, 2, '.', ''); | |
$row['Credit Account No.'] = $staff->payrollStaffDetails->bank_acc_number; | |
$row['Beneficiary Name 1'] = $staff->name; | |
$row['Beneficiary Name 2'] = null; | |
$row['Beneficiary Name 3'] = null; | |
$row['New NRIC'] = Crypt::decryptString($staff->id_number); | |
$row['Old NRIC'] = null; | |
$row['Business Registration No.'] = null; | |
$row['Police/ Army/ Passport No.'] = null; | |
$row['Beneficiary Bank Code'] = $bankCode; | |
$row['Email'] = null; | |
$row['Advice Detail'] = null; | |
$row['Debit Desc.'] = null; | |
$row['Credit Desc.'] = null; | |
$row['Joint Name'] = null; | |
$row['Joint Old ID'] = null; | |
$row['Joint New ID'] = null; | |
$row['Joint Business Reg. No.'] = null; | |
$row['Joint Police/ Army/ Passport No.'] = null; | |
$row['Purpose of Transfer'] = null; | |
$row['Other Purpose of Transfer'] = null; | |
$row['Rentas Instruction To Bank'] = null; | |
fputcsv( | |
$file, | |
array( | |
$row['Payment Mode'], | |
$row['Value Date'], | |
$row['Customer Reference No.'], | |
$row['Favourite Beneficiary Code'], | |
$row['Transaction Amount'], | |
$row['Credit Account No.'], | |
$row['Beneficiary Name 1'], | |
$row['Beneficiary Name 2'], | |
$row['Beneficiary Name 3'], | |
$row['New NRIC'], | |
$row['Old NRIC'], | |
$row['Business Registration No.'], | |
$row['Police/ Army/ Passport No.'], | |
$row['Beneficiary Bank Code'], | |
$row['Email'], | |
$row['Advice Detail'], | |
$row['Debit Desc.'], | |
$row['Credit Desc.'], | |
$row['Joint Name'], | |
$row['Joint Old ID'], | |
$row['Joint New ID'], | |
$row['Joint Business Reg. No.'], | |
$row['Joint Police/ Army/ Passport No.'], | |
$row['Purpose of Transfer'], | |
$row['Other Purpose of Transfer'], | |
$row['Rentas Instruction To Bank'] | |
) | |
); | |
$i++; | |
}; | |
fclose($file); | |
}; | |
return response()->stream($callback, 200, $headers); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment