diff --git a/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMInvoice.php b/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMInvoice.php index b595a86f..ba0c2755 100644 --- a/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMInvoice.php +++ b/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMInvoice.php @@ -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, diff --git a/app/Classes/Modules/PerfexCRM/Services/UpdatesPerfexCRMInvoice.php b/app/Classes/Modules/PerfexCRM/Services/UpdatesPerfexCRMInvoice.php index 21a79a96..1ade5453 100644 --- a/app/Classes/Modules/PerfexCRM/Services/UpdatesPerfexCRMInvoice.php +++ b/app/Classes/Modules/PerfexCRM/Services/UpdatesPerfexCRMInvoice.php @@ -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([ diff --git a/app/Http/Controllers/Imports/ImportBankRecordController.php b/app/Http/Controllers/Imports/ImportBankRecordController.php index effac446..bd0272d2 100644 --- a/app/Http/Controllers/Imports/ImportBankRecordController.php +++ b/app/Http/Controllers/Imports/ImportBankRecordController.php @@ -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 ' - - - - - - - - - - - '; - - $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 = '
DateBankDescriptionCreditPay ForSystem ReferenceHuman ReferenceMultipleMatch?
'; + + $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 ' - - - - - - - - - - '; + 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 .= ' + + + + + + + + + + + + '; } } -// return []; + + $table .= '
'.implode('', $headers).'
'.$date->format('d-m-Y').''.$branch.''.$row['description'].''.$credit.''.$row['pay_for'].''.$systemReference.''.$row['remarkreferences'].''.$multiple.''.$matches.'
'.$date->format('d-m-Y').''.$branch.''.$description.''.$credit.''.$debit.''.$row['pay_for'].''.$systemReference.''.$row['remarkreferences'].''.$multiple.''.$matches.''.$systemAmount.'
'; + + 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(); } }