mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
118 lines
4.4 KiB
PHP
118 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Carbon\Carbon;
|
|
use App\Classes\Modules\Transactions\Processors\CreateInvoiceTransactionProcessorWithInvoiceNo;
|
|
use App\Classes\Modules\Transactions\Processors\CreateInvoiceTransactionProcessor;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\DocumentType;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Models\Booking;
|
|
use App\Models\Transaction;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class RegenerateInvoice extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'regenerateInvoice';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Regenerate invoice';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
// Allocate sufficient memory as needed
|
|
// ini_set('memory_limit', '256M');
|
|
|
|
$processed_invoice = 1;
|
|
|
|
Booking::where('status', ApprovalStatus::COMPLETED)
|
|
->whereDate('updated_at', '>=', Carbon::parse('2023-01-01'))
|
|
->orderBy('id')
|
|
->chunk(100, function ($bookings) use (&$processed_invoice) {
|
|
foreach ($bookings as $booking) {
|
|
$logMessage = 'Counter ' . $processed_invoice;
|
|
$this->printAndLog($logMessage);
|
|
$processed_invoice++;
|
|
|
|
if (
|
|
!$booking->transactions()->where('transactions.type', TransactionType::INVOICE)->exists()
|
|
|| $booking->transactions()->where('transactions.type', TransactionType::INVOICE)->first()->created_at->greaterThanOrEqualTo(Carbon::parse('2023-09-06'))
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
// Delete transactions and documents in one query
|
|
$booking->transactions()->whereIn('transactions.type', [TransactionType::INVOICE, TransactionType::SUPPLIER_DELIVER])->delete();
|
|
$booking->documents()->whereIn('document_type', [DocumentType::INVOICE, DocumentType::PURCHASE_ORDER, DocumentType::DELIVER_ORDER, DocumentType::SUPPLIER_DELIVER_ORDER])->delete();
|
|
|
|
$booking->status = ApprovalStatus::APPROVED;
|
|
$booking->save();
|
|
|
|
$deletedInvoice = $booking->transactions()
|
|
->whereIn('type', [TransactionType::INVOICE])
|
|
->onlyTrashed()
|
|
->orderBy('created_at', 'asc')
|
|
->first();
|
|
|
|
if ($deletedInvoice) {
|
|
$bill_no = $deletedInvoice->bill_no;
|
|
if (str_ends_with($bill_no, '-deleted')) {
|
|
$bill_no = str_replace('-deleted', '', $bill_no);
|
|
}
|
|
|
|
$deletedInvoice->bill_no = $bill_no . '-deleted';
|
|
$deletedInvoice->save();
|
|
|
|
$existing_invoice_bill_no = Transaction::where('bill_no', $bill_no)->first();
|
|
|
|
if ($existing_invoice_bill_no) {
|
|
$this->printAndLog('Delete existing bill_no');
|
|
$this->printAndLog(json_encode($existing_invoice_bill_no));
|
|
$existing_invoice_bill_no->forceDelete();
|
|
}
|
|
|
|
(App()->make(CreateInvoiceTransactionProcessorWithInvoiceNo::class))->execute($booking, $bill_no);
|
|
$logMessage = 'Regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $bill_no . '. Old bill_no - ' . $deletedInvoice->bill_no;
|
|
$this->printAndLog($logMessage);
|
|
} else {
|
|
(App()->make(CreateInvoiceTransactionProcessor::class))->execute($booking);
|
|
$logMessage = 'Regenerated new invoice. Booking Marking - ' . $booking->marking;
|
|
$this->printAndLog($logMessage);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public function printAndLog($string)
|
|
{
|
|
// $this->info($string);
|
|
Log::channel('regenerateInvoice')->info($string);
|
|
}
|
|
}
|