fix and update the functionality and the ui for accounting automation

This commit is contained in:
Omair Saleh
2023-04-18 12:07:27 +08:00
parent 6aa2ed53c7
commit 4a6ae598fa
38 changed files with 2088 additions and 566 deletions
@@ -2,34 +2,27 @@
namespace App\Http\Controllers\Accounting;
use App\Classes\Jobs\CreateBankStatementDetails;
use App\Classes\Jobs\CreateBankStatementTransactionOwners;
use App\Classes\Modules\Accounting\ControllersLogic\ImportBankStatementLogic;
use App\Classes\Modules\Accounting\ControllersLogic\ListBankStatementDetailsLogic;
use App\Classes\Modules\Accounting\ControllersLogic\ListBankStatementTransactionsLogic;
use App\Classes\Modules\Accounting\ControllersLogic\UpdateBankStatementDetailLogic;
use App\Classes\Modules\Imports\Services\BankStatementImport;
use App\Classes\Modules\Imports\Services\ImportsBankRecord;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\StatementAccount;
use App\Models\AccountStatement;
use App\Models\StatementTransaction;
use App\Models\Booking;
use App\Models\Company;
use App\Models\Group;
use App\Models\StatementTransactionsDetail;
use App\Models\StatementTransactionOwner;
use App\Models\Transaction;
use App\Models\Wallet;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
use DateTime;
use PhpOffice\PhpSpreadsheet\Shared\Date;
class BankStatementController extends Controller
{
public function index(Request $request)
@@ -55,133 +48,47 @@ class BankStatementController extends Controller
});
}
$statements = $statementsQuery->paginate(10);
$statements = $statementsQuery->orderBy('date_from')->paginate(10);
return view('pages.accounting.bank-statements.index', compact('accounts', 'selectedAccount', 'search', 'statements'));
}
public function import(Request $request)
public function indexv2(Request $request)
{
$selectedAccount = $request->input('account');
$search = $request->input('search');
$request->validate([
'file' => 'required|mimes:csv,txt'
]);
$accounts = StatementAccount::all();
$file = $request->file('file');
$statementsQuery = AccountStatement::query();
$collection = Excel::toCollection(null, $file, null, null, true);
$sheet = $collection->first()->skip(1);
$statementDetails = $sheet->first();
$accountNumber = $statementDetails[0];
$accountType = $statementDetails[1];
$accountName = $statementDetails[2];
$accountCurrency = $statementDetails[3];
$dateFrom = carbon::parse(str_replace(' MY (UTC+08:00)', '', $statementDetails[4]));
$dateTo = carbon::parse(str_replace(' MY (UTC+08:00)', '', $statementDetails[5]));
$totalDebit = $statementDetails[6];
$totalCredit = $statementDetails[7];
$beginBalance = $statementDetails[8];
$endBalance = $statementDetails[9];
$account = StatementAccount::updateOrCreate(
['number' => $accountNumber],
[
'type' => $accountType,
'name' => $accountName,
'currency' => $accountCurrency,
]
);
$statement = AccountStatement::where('date_from', $dateFrom)
->where('date_to', $dateTo)
->where('total_amount', $totalDebit ?: $totalCredit,)
->where('begin_balance', $beginBalance)
->where('end_balance', $endBalance)->first();
if(!$statement){
$statement = new AccountStatement([
'date_from' => $dateFrom,
'date_to' => $dateTo,
'total_amount' => $totalDebit ?: $totalCredit,
'begin_balance' => $beginBalance,
'end_balance' => $endBalance,
]);
if ($selectedAccount) {
$statementsQuery->where('statement_account_id', $selectedAccount);
}
// $existingStatement = AccountStatement::where('date_from', $dateFrom)
// ->where('date_to', $dateTo)
// ->where('statement_account_id', $account->id)
// ->first();
if ($search) {
$statementsQuery->where(function ($query) use ($search) {
$query->where('date_from', 'LIKE', "%$search%")
->orWhere('date_to', 'LIKE', "%$search%")
->orWhere('total_amount', 'LIKE', "%$search%")
->orWhere('begin_balance', 'LIKE', "%$search%")
->orWhere('end_balance', 'LIKE', "%$search%");
});
}
// // if ($existingStatement) {
// // return redirect()->back()->with('error', 'This statement has already been imported.');
// // }
$statements = $statementsQuery->paginate(10);
$account->statements()->save($statement);
CreateBankStatementDetails::dispatch($statement)->delay(30);
// $account = null;
$newTransactions = $sheet->map(function ($row) use ($statement, $account) {
$transactionRef = $row[15];
$amount = $row[17] !== '-' ? ((float) str_replace(',', '', $row[17])) : (-((float) str_replace(',', '', $row[16])));
$transactionDate = $row[10] !== '-' ? carbon::parse(str_replace(' MY (UTC+08:00)', '', $row[10]) . $row[11]) : null;
$postingDate = carbon::createFromFormat('d/M/Y H:i', str_replace(' MY (UTC+08:00)', '', $row[12]) . str_replace(' MY (UTC+08:00)', '', $row[13]));
$transactionDescription = is_numeric($row[14]) ? (int) sprintf('%.2f', $row[14]) : $row[14];
$tellerId = $row[19];
$branchChannel = $row[20];
$transactionCode = $row[21];
$endBalance = $row[22];
$description2 = $row[25];
$description3 = $row[26];
$description4 = $row[27];
$description5 = $row[28];
$transaction = new StatementTransaction([
'transaction_ref' => $transactionRef,
'amount' => $amount,
'transaction_date' => $transactionDate,
'posting_date' => $postingDate,
'transaction_description' => $transactionDescription,
'teller_id' => $tellerId,
'branch_channel' => $branchChannel,
'transaction_code' => $transactionCode,
'end_balance' => $endBalance,
'transaction_description_2' => $description2,
'transaction_description_3' => $description3,
'transaction_description_4' => $description4,
'transaction_description_5' => $description5,
]);
// Check if the transaction already exists for this statement
$existingTransaction = StatementTransaction::where('transaction_ref', $transactionRef)
->where('posting_date', $postingDate)
->where('amount', $amount)
->where('transaction_description', $transactionDescription)
->where('teller_id', $tellerId)
->where('branch_channel', $branchChannel)
->where('transaction_code', $transactionCode)
->where('end_balance', $endBalance)
->first();
if (!$existingTransaction) {
$statement->transactions()->save($transaction);
}
return $transaction;
});
// dd(json_encode($newTransactions));
return redirect()->back()->with('success', 'Statement imported successfully.')->with('newTransactions', $newTransactions);
return view('pages.accounting.bank-statements.indexv2', compact('accounts', 'selectedAccount', 'search', 'statements'));
}
public function rerun(AccountStatement $statement, Request $request)
public function import(Request $request, ImportBankStatementLogic $logic): JsonResponse
{
CreateBankStatementDetails::dispatch($statement)->delay(30);
return $logic->execute($request);
}
public function rerun()
{
CreateBankStatementTransactionOwners::dispatch();
return redirect()->back()->with('success', 'Rerun triggered successfully');
}
@@ -227,6 +134,11 @@ class BankStatementController extends Controller
return $logic->execute($request);
}
public function transactions(Request $request, ListBankStatementTransactionsLogic $logic): JsonResponse
{
return $logic->execute($request);
}
public function update(Request $request, UpdateBankStatementDetailLogic $logic): JsonResponse
{
return $logic->execute($request);
@@ -264,7 +176,7 @@ class BankStatementController extends Controller
$count = 0;
foreach ($transactions as $row) {
$isExist = StatementTransactionsDetail::where('statement_transaction_id', $row->id)->first();
$isExist = StatementTransactionOwner::where('statement_transaction_id', $row->id)->first();
if ($isExist) {
continue;
@@ -371,7 +283,7 @@ class BankStatementController extends Controller
//AccountStatement
// $row->statement()->first()->id)
$statementTransactionsDetail = new StatementTransactionsDetail([
$statementTransactionsDetail = new StatementTransactionOwner([
'date' => $date,
'statement_transaction_id' => $row->id,
'description' => is_null($description) ? "" : $description,