Created
April 25, 2017 09:02
-
-
Save jacques/57c672e5e7efe1b26be65fe3adb48c91 to your computer and use it in GitHub Desktop.
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
/** | |
* Exports the Standard Two-Day Debit Order Service File | |
*/ | |
$app->get('/admin/debitorders/:batch_id/exportgalaxy', $authenticate($app), $is_admin($app), function ($batch_id) use ($app, $dsns) { | |
$batch = $dsns['bank']->prepare("SELECT * FROM company_debitorder_batches WHERE id=?")->execute([$batch_id])->fetchRow(); | |
/** | |
* Only batches which have been finalised can be exported. | |
*/ | |
#if ($batch['processed_at'] == '0000-00-00 00:00:00') { | |
# die ("You cannot export a batch that is still open."); | |
#} | |
if ($batch['type'] == 'twoday' || $batch['type'] == 'oneday') { | |
$fp = fopen('/var/tmp/galaxy_debitorder_batch_' . $batch_id . '.txt', 'w'); | |
$iyandi_user_code = '7955'; | |
$header = vsprintf ("Header %04d%-30s%08d%04d\r\n", [ $iyandi_user_code, 'IMOGO', date("Ymd"), $batch_id]); | |
fwrite($fp, $header); | |
$efts = $dsns['bank']->prepare("SELECT * FROM company_debitorder_queue WHERE company_debitorder_batch_id=?")->execute([$batch_id])->fetchAll(); | |
$date_minus_2days = Carbon::now()->subDays(2); | |
if ($batch['type'] == 'oneday') { | |
$date_plus_2days = Carbon::createFromFormat('Y-m-d', $batch['action_date']); | |
} else { | |
$date_plus_2days = Carbon::createFromFormat('Y-m-d', $batch['action_date']); | |
} | |
$date_plus_7days = Carbon::createFromFormat('Y-m-d', $batch['action_date'])->addDays(7); | |
$date_minus_2days = $date_minus_2days->format('Ymd'); | |
$date_plus_2days = $date_plus_2days->format('Ymd'); | |
$date_plus_7days = $date_plus_7days->format('Ymd'); | |
foreach ($efts as $eft) { | |
# Deduct %04d%04d%06d%13s%-30s%-6s%-06s%10s%-30s%06d%011d%1d%8d%06d%011d%1d%8d%8d%8d%1s%-15s%-50s%013d%-10s%50s\r\n | |
fwrite($fp, trim(vsprintf("Deduct %04d%04d%06d%13s%-30s%-6s%-06s%10s%-30s%06d%011d%1d%8d%06d%011d%1d%8d%8d%8d%1s%-14s%1s%-50s%013d%-10s%50s\r\n", | |
[ | |
$iyandi_user_code, # Company Number | |
null, # Company Branch Number | |
null, # Company Branch Postal Code | |
'9999999999999', # IMB use txn ref | |
substr($eft['bank_account_holder'], 0, 30), # Last name | |
substr($eft['bank_account_holder'], 0, 1), # Initials | |
'000000', # Postal code - 6 characters - for the individual | |
'0000000000', # Mobile Number - 10 chars max | |
null, # Email Address - 30 chars max | |
$eft['bank_account_branch_code'], | |
$eft['bank_account_number'], | |
$eft['bank_account_type'], | |
$date_plus_2days, | |
'000000', | |
'0000000000', | |
0, | |
$date_minus_2days, | |
$date_plus_7days, | |
date("Ymd"), | |
'M', | |
substr('CB ' . $eft['client_customer_reference'] . ' ' . $eft['company_debitorder_batch_id'], 0, 14), | |
'Z', | |
substr('CASHB ' . $eft['company_debitorder_batch_id'] . ' ' . $eft['client_transaction_reference'] . ' ' . $date_plus_2days, 0, 50), | |
$eft['debit_onceoff_amount'], | |
($batch['type'] == 'oneday') ? 'AbsaDirectSSVS' : 'AbsaDirectTWO DAY', | |
null | |
])) . "\r\n"); | |
} | |
$trailer = vsprintf("Trailer%09d%17s%011d\r\n", [$batch['debitorders'], null, $batch['amount']]); | |
fwrite($fp, $trailer); | |
fclose($fp); | |
$data = file_get_contents('/var/tmp/galaxy_debitorder_batch_' . $batch_id . '.txt'); | |
$app->contentType('application/octet-stream'); | |
$app->response()->header('Pragma', 'no-cache'); | |
$app->response()->header('Expires', 0); | |
$app->response()->header('Content-Disposition', 'attachment; filename="imogo-galaxy-debitorer-batch-' . $batch_id . '.txt"'); | |
$app->response()->header('Content-Transfer-Encoding', 'ascii'); | |
$app->response()->header('Content-Length', strlen($data)); | |
$app->response()->body($data); | |
} else { | |
die ("You can only export galaxy files for two day batches."); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment