import bank record

This commit is contained in:
Omair Saleh
2023-03-18 05:21:38 +08:00
parent 422a7c9d65
commit 85d38c3645
@@ -10,6 +10,8 @@ use App\Models\Booking;
use App\Models\Group;
use App\Models\Transaction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use PhpOffice\PhpSpreadsheet\Shared\Date;
@@ -19,7 +21,8 @@ class ImportBankRecordController
* @param Request $request
* @return array
*/
public function import(Request $request) {
public function import(Request $request)
{
$headers = [
'Date',
'Bank',
@@ -41,55 +44,59 @@ class ImportBankRecordController
$yes = 'Yes';
$no = 'No';
$table = '<table><tr><th>'.implode('</th><th>', $headers).'</th></tr>';
$table = '<table><tr><th>' . implode('</th><th>', $headers) . '</th></tr>';
$collection = Excel::toCollection(new ImportsBankRecord(), 'daily_transaction_nov.xlsx');
$cacheKey = 'bank_transaction_data';
foreach ($collection as $key => $sheet){
$branch = $branches[$key];
foreach ($sheet as $row) {
$date = Date::excelToDateTimeObject($row['date']);
$credit = (float) $row['credit'];
$debit = (float) $row['debit'];
$creditTransactions = [];
$debitTransactions = [];
$data = Cache::get($cacheKey);
$systemReference = null;
if (!$data) {
$data = $this->fetchDataFromDatabase();
Cache::put($cacheKey, $data, now()->addMinutes(60));
}
if($credit){
$creditTransactions = $this->getTransactions($date, $credit, TransactionType::PAYMENT, Booking::class, PaymentMethodType::WALLET, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
foreach ($creditTransactions as $transaction) {
$systemReference[] = $transaction->owner instanceof Booking ? $transaction->owner->marking : $transaction->bill_no;
}
foreach ($data as $row) {
$branch = $branches[$row->branch_id];
$credit = (float)$row->credit;
$debit = (float)$row->debit;
$creditTransactions = [];
$debitTransactions = [];
$systemReference = null;
if ($credit) {
$creditTransactions = $this->getTransactions($row->date, $credit, TransactionType::PAYMENT, Booking::class, PaymentMethodType::WALLET, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
foreach ($creditTransactions as $transaction) {
$systemReference[] = $transaction->owner instanceof Booking ? $transaction->owner->marking : $transaction->bill_no;
}
if($debit){
$debitTransactions = $this->getTransactions($date, $debit, null, null, null, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED], Group::class);
foreach ($debitTransactions as $transaction) {
$systemReference[] = $transaction->reference;
}
}
$multiple = count($creditTransactions) + count($debitTransactions) > 1 ? $yes : $no;
$systemReference = $systemReference ? implode(',', $systemReference) : null;
$matches = $systemReference == $row['remarkreferences'] ? $yes : $no;
$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>';
}
if ($debit) {
$debitTransactions = $this->getTransactions($row->date, $debit, null, null, null, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED], Group::class);
foreach ($debitTransactions as $transaction) {
$systemReference[] = $transaction->reference;
}
}
$multiple = count($creditTransactions) + count($debitTransactions) > 1 ? $yes : $no;
$systemReference = $systemReference ? implode(',', $systemReference) : null;
$matches = $systemReference == $row->remark_references ? $yes : $no;
$table .= '<tr>
<td>' . $row->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->remark_references . '</td>
<td>' . $multiple . '</td>
<td>' . $matches . '</td>
</tr>';
}
$table .= '</table>';
@@ -97,7 +104,30 @@ class ImportBankRecordController
echo $table;
}
private function getTransactions($date, $amount, $type, $ownerType, $paymentMethod, $statuses, $model = Transaction::class) {
private function fetchDataFromDatabase()
{
$data = DB::table('bank_transactions')
->select([
'date',
'branch_id',
'description',
'credit',
'debit',
'pay_for',
'remark_references',
])
->get();
$data = $data->map(function ($row) {
$row->date = Date::excelToDateTimeObject($row->date);
return $row;
});
return $data;
}
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) {
@@ -114,8 +144,9 @@ class ImportBankRecordController
})
->whereDate('created_at', $date->format('Y-m-d'))
->where('amount', '>', ($amount - 0.01))
->where('amount', '<', ($amount + 0.01));
->where('amount', '<', ($amount + 0.01))
->cursor();
return $query->get();
return $query;
}
}