import bank record

This commit is contained in:
Omair Saleh
2023-03-18 05:02:45 +08:00
parent a84610b5ee
commit fd991d19e0
@@ -20,117 +20,97 @@ class ImportBankRecordController
/**
* @param Request $request
* @return array
* @throws \App\Classes\Exceptions\MalformedRequestException
*/
public function import(Request $request) {
// $object = new DocumentObject('', $request->input('files'), '', ApprovalStatus::APPROVED, 'imports');
echo '<table>
<tr>
<th>Date</th>
<th>Bank</th>
<th>Description</th>
<th>Credit</th>
<th>debit</th>
<th>Pay For</th>
<th>System Reference</th>
<th>Human Reference</th>
<th>Multiple</th>
<th>Match?</th>
</tr>';
$collection = Excel::toCollection(new ImportsBankRecord(), 'daily_transaction_nov.xlsx');
$headers = [
'Date',
'Bank',
'Description',
'Credit',
'Debit',
'Pay For',
'System Reference',
'Human Reference',
'Multiple',
'Match?',
];
$branches = [
0 => 'MBB Cyber',
1 => 'MBB SS2'
1 => 'MBB SS2',
];
$yes = 'Yes';
$no = 'No';
$table = '<table><tr><th>'.implode('</th><th>', $headers).'</th></tr>';
$collection = Excel::toCollection(new ImportsBankRecord(), 'daily_transaction_nov.xlsx');
foreach ($collection as $key => $sheet){
$branch = $branches[$key];
$branch = $branches[$key];
foreach ($sheet as $row) {
$date = Date::excelToDateTimeObject($row['date']);
$credit = (float) $row['credit'];
$debit = (float) $row['debit'];
$creditTransactions = $this->getTransactions($date, $credit, TransactionType::PAYMENT, Booking::class, PaymentMethodType::WALLET, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
$debitTransactions = $this->getTransactions($date, $debit, null, null, null, null, Group::class);
$systemReference = null;
$multiple = 'No';
if($credit){
$payments = Transaction::whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
->where(function ($query){
return $query->where(function ($query){
return $query->where('type', TransactionType::PAYMENT)->where('owner_type', Booking::class)->where('payment_method', '!=', PaymentMethodType::WALLET);
})->orWhere(function ($query){
$query->where('type', TransactionType::TOP_UP);
});
})
->WhereDate('created_at', $date->format('Y-m-d'))
->where('amount', '>', ($credit - 0.01))->where('amount', '<', ($credit + 0.01))
->get();
if(count($payments)) {
foreach ($payments as $transaction){
if($transaction->owner instanceof Booking){
$systemReference[] = $transaction->owner->marking;
}
if($transaction->owner instanceof Wallet){
$systemReference[] = $transaction->bill_no;
}
}
if(count($systemReference) > 1) {
$multiple = 'Yes';
}
$systemReference = implode(',', $systemReference);
}
}
if($debit){
$bills = Group::WhereDate('created_at', $date->format('Y-m-d'))
->where('amount', '>', ($debit - 0.01))->where('amount', '<', ($debit + 0.01))
->get();
if(count($bills)) {
foreach ($bills as $transaction){
if($transaction->owner instanceof Booking){
$systemReference[] = $transaction->owner->marking;
}
if($transaction->owner instanceof Wallet){
$systemReference[] = $transaction->bill_no;
}
}
if(count($systemReference) > 1) {
$multiple = 'Yes';
}
$systemReference = implode(',', $systemReference);
}
$multiple = count($creditTransactions) + count($debitTransactions) > 1 ? $yes : $no;
foreach ($creditTransactions as $transaction) {
$systemReference[] = $transaction->owner instanceof Booking ? $transaction->owner->marking : $transaction->bill_no;
}
foreach ($debitTransactions as $transaction) {
$systemReference[] = $transaction->owner instanceof Booking ? $transaction->owner->marking : $transaction->bill_no;
}
$systemReference = $systemReference ? implode(',', $systemReference) : null;
$matches = $systemReference == $row['remarkreferences'] ? 'Yes' : 'No';
$matches = $systemReference == $row['remarkreferences'] ? $yes : $no;
echo '<tr>
<td>'.$date->format('d-m-Y').'</td>
<td>'.$branch.'</td>
<td>'.$row['description'].'</td>
<td>'.$credit.'</td>
<td>'.$debit.'</td>
<td>'.$row['pay_for'].'</td>
<td>'.$systemReference.'</td>
<td>'.$row['remarkreferences'].'</td>
<td>'.$multiple.'</td>
<td>'.$matches.'</td>
</tr>';
$table .= '<tr>
<td>'.$date->format('d-m-Y').'</td>
<td>'.$branch.'</td>
<td>'.$row['description'].'</td>
<td>'.$credit.'</td>
<td>'.$debit.'</td>
<td>'.$row['pay_for'].'</td>
<td>'.$systemReference.'</td>
<td>'.$row['remarkreferences'].'</td>
<td>'.$multiple.'</td>
<td>'.$matches.'</td>
</tr>';
}
}
// return [];
$table .= '</table>';
echo $table;
}
private function getTransactions($date, $amount, $type, $ownerType, $paymentMethod, $statuses, $model = Transaction::class) {
$query = $model::whereIn('status', $statuses)
->where(function ($query) use ($ownerType, $paymentMethod, $type) {
if ($ownerType) {
$query->where('owner_type', $ownerType);
}
if ($paymentMethod) {
$query->where('payment_method', '!=', $paymentMethod);
}
if ($type) {
$query->where('type', $type);
}
})
->whereDate('created_at', $date->format('Y-m-d'))
->where('amount', '>', ($amount - 0.01))
->where('amount', '<', ($amount + 0.01));
return $query->get();
}
}