Merge branch 'dillon/debug-and-fixes-1-22' into development

This commit is contained in:
Dillon
2023-03-19 22:31:45 +08:00
3 changed files with 124 additions and 63 deletions
@@ -16,14 +16,17 @@ class CreatesPerfexCRMInvoice
*/
public function execute(InvoicePerfexCRMObject $invoicePerfexCRMObject) {
try{
$subtotal = floor($invoicePerfexCRMObject->getSubTotal() * 100) / 100;
$total = floor($invoicePerfexCRMObject->getTotal() * 100) / 100;
$data = [
'clientid' => $invoicePerfexCRMObject->getClientId(),
'number' => $invoicePerfexCRMObject->getNumber(),
'date' => $invoicePerfexCRMObject->getDate(),
'duedate' => $invoicePerfexCRMObject->getDueDate(),
'currency' => $invoicePerfexCRMObject->getCurrency(),
'subtotal' => number_format($invoicePerfexCRMObject->getSubTotal(), 2, '.', ''),
'total' => number_format($invoicePerfexCRMObject->getTotal(), 2, '.', ''),
'subtotal' => number_format($subtotal, 2, '.', ''),
'total' => number_format($total, 2, '.', ''),
'billing_street' => $invoicePerfexCRMObject->getBillingStreet(),
'project_id' => $invoicePerfexCRMObject->getProjectId(),
'allowed_payment_modes[0]' => 1,
@@ -35,6 +35,9 @@ class UpdatesPerfexCRMInvoice
array_push($newInvoiceItems,$item);
}
$allowedPaymentModes = [];
array_push($allowedPaymentModes, 1, 2);
$data = [
'number' => $invoice->number,
'date' => $invoice->date,
@@ -46,7 +49,7 @@ class UpdatesPerfexCRMInvoice
'shipping_street' => $invoice->billing_street,
'project_id' => $projectId,
'items' => $newInvoiceItems,
'allowed_payment_modes' => $invoice->allowed_payment_modes,
'allowed_payment_modes' => $allowedPaymentModes,
];
$response = Http::asJson()->withHeaders([
@@ -2,14 +2,16 @@
namespace App\Http\Controllers\Imports;
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
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\Booking;
use App\Models\Company;
use App\Models\Group;
use App\Models\Transaction;
use App\Models\Wallet;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use PhpOffice\PhpSpreadsheet\Shared\Date;
@@ -19,84 +21,137 @@ 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>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?',
'System Amount'
];
$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']);
$date = Carbon::instance(Date::excelToDateTimeObject($row['date']));
$description = $row['description'];
$credit = (float) $row['credit'];
$debit = (float) $row['debit'];
$creditTransactions = [];
$debitTransactions = [];
$systemReference = null;
$multiple = 'No';
$systemAmount = null;
$transactions = 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)->where('amount', '<', ($credit + 0.01))
->get();
if(count($transactions)) {
foreach ($transactions as $transaction){
if($transaction->owner instanceof Booking){
$systemReference[] = $transaction->owner->marking;
}
if($transaction->owner instanceof Wallet){
$systemReference[] = $transaction->bill_no;
}
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;
$systemAmount[] = $transaction->amount;
}
if(count($systemReference) > 1) {
$multiple = 'Yes';
$creditTransactions = $this->getTransactions($date, $credit, TransactionType::TOP_UP, Wallet::class, null, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
foreach ($creditTransactions as $transaction) {
$systemReference[] = $transaction->owner instanceof Booking ? $transaction->owner->marking : $transaction->bill_no;
$systemAmount[] = $transaction->amount;
}
$systemReference = implode(',', $systemReference);
}
$matches = $systemReference == $row['remarkreferences'] ? 'Yes' : 'No';
if($debit){
$debitTransactions = $this->getTransactions($date, $debit, null, null, null, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED], Group::class);
echo '<tr>
<td>'.$date->format('d-m-Y').'</td>
<td>'.$branch.'</td>
<td>'.$row['description'].'</td>
<td>'.$credit.'</td>
<td>'.$row['pay_for'].'</td>
<td>'.$systemReference.'</td>
<td>'.$row['remarkreferences'].'</td>
<td>'.$multiple.'</td>
<td>'.$matches.'</td>
</tr>';
if(!count($debitTransactions)) {
foreach (['YSN', 'HCK', 'ATVANTIC', 'HIGH HILL'] as $reference){
if(str_contains($description, $reference)) {
$paymentDate = $date->addDays(1)->format('Y-m-d');
if($reference = 'ATVANTIC'){
$paymentDate = $date->format('Y-m-d');
}
$issuer = Company::where('name', 'like', '%'.$reference.'%')->get()->pluck('id');
$debitTransactions = Group::whereIn('issuer', $issuer)->whereDate('created_at', $paymentDate)->get();
break;
}
}
}
foreach ($debitTransactions as $transaction) {
$systemReference[] = $transaction->reference;
$systemAmount[] = $transaction->amount;
}
}
$multiple = count($creditTransactions) + count($debitTransactions) > 1 ? $yes : $no;
$systemReference = $systemReference ? implode(',', $systemReference) : null;
$systemAmount = $systemAmount ? implode(',', $systemAmount) : null;
$matches = $systemReference == $row['remarkreferences'] ? $yes : $no;
$table .= '<tr>
<td>'.$date->format('d-m-Y').'</td>
<td>'.$branch.'</td>
<td>'.$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>
<td>'.$systemAmount.'</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();
}
}