Merge branch 'fix-regenerate-bulk-invoice' of gitlab.com:CIEFWorldwideSdnBhd/exchange-2.0 into development

This commit is contained in:
edmondlang
2023-09-06 15:46:16 +08:00
2 changed files with 41 additions and 28 deletions
+5
View File
@@ -99,6 +99,11 @@ return [
'emergency' => [
'path' => storage_path('logs/laravel.log'),
],
'regenerateInvoice' => [
'driver' => 'single',
'path' => storage_path('logs/regenerateInvoice.log'),
'level' => 'info',
],
],
];
+36 -28
View File
@@ -28,6 +28,7 @@ use App\Classes\Modules\Bookings\Processors\CreatePurchaseOrderFor1688OrderProce
use App\Classes\Modules\Documents\Services\DeletesDocument;
use App\Classes\Modules\Transactions\Processors\CreateInvoiceTransactionProcessorWithInvoiceNo;
use App\Classes\Modules\Transactions\Services\DeletesTransaction;
use Illuminate\Support\Facades\Log;
/*
|--------------------------------------------------------------------------
@@ -492,41 +493,48 @@ Route::get('/payments/manual', function(){
Route::get('/invoice/fix', function(){
set_time_limit(14400);
$bookings = Booking::where('status', ApprovalStatus::COMPLETED)
$processed_invoice = 1;
Booking::where('status', ApprovalStatus::COMPLETED)
->whereDate('updated_at', '>=', Carbon::parse('01-01-2023'))
->take(10) // to test on development
->get();
->orderBy('id')
->chunk(100, function ($bookings) use ($processed_invoice){
foreach ($bookings as $booking) {
foreach($bookings as $booking){
$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();
Log::channel('regenerateInvoice')->info('Counter ' . $processed_invoice);
$processed_invoice += 1;
$deletedInvoice = $booking->transactions()
->whereIn('type', [TransactionType::INVOICE])
->onlyTrashed()
->orderBy('created_at', 'asc')
->first();
$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();
if ($deletedInvoice) {
$bill_no = $deletedInvoice->bill_no;
$deletedInvoice = $booking->transactions()
->whereIn('type', [TransactionType::INVOICE])
->onlyTrashed()
->orderBy('created_at', 'asc')
->first();
// check if bill_no ends with '-deleted'
if (str_ends_with($bill_no, '-deleted')) {
$bill_no = str_replace('-deleted', '', $bill_no);
if ($deletedInvoice) {
$bill_no = $deletedInvoice->bill_no;
// check if bill_no ends with '-deleted'
if (str_ends_with($bill_no, '-deleted')) {
$bill_no = str_replace('-deleted', '', $bill_no);
}
$deletedInvoice->bill_no = $bill_no . '-deleted';
$deletedInvoice->save();
(App()->make(CreateInvoiceTransactionProcessorWithInvoiceNo::class))->execute($booking, $bill_no);
dump('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $bill_no . '. Old bill_no - ' . $deletedInvoice->bill_no);
} else {
(App()->make(CreateInvoiceTransactionProcessor::class))->execute($booking);
dump('regenerated new invoice. Booking Marking - ' . $booking->marking);
}
}
$deletedInvoice->bill_no = $bill_no . '-deleted';
(App()->make(CreateInvoiceTransactionProcessorWithInvoiceNo::class))->execute($booking, $bill_no);
$deletedInvoice->save();
dump('regenerated invoice. Booking Marking - ' . $booking->marking . '. Bill_no - ' . $bill_no . '. Old bill_no - ' . $deletedInvoice->bill_no);
} else {
(App()->make(CreateInvoiceTransactionProcessor::class))->execute($booking);
dump('regenerated new invoice. Booking Marking - ' . $booking->marking);
}
}
);
})->name('invoice.fix');
Route::get('/1688/fix/{reference}', function($reference){