Fix 'table not found' error in migration

This commit is contained in:
Burak Çakırel 2021-01-26 17:01:05 +03:00
parent 8ecf4289e8
commit 2e814cde59
No known key found for this signature in database
GPG Key ID: 48FFBB7771B99C7C

View File

@ -97,7 +97,7 @@ class Version210 extends Listener
$this->addForeignKeys(); $this->addForeignKeys();
DB::transaction(function () { DB::transaction(function () {
$this->totals = collect($this->getTotals(['invoice', 'bill', 'estimate', 'credit_note', 'debit_note'])); $this->totals = $this->getTotals(['invoice', 'bill', 'estimate', 'credit_note', 'debit_note']);
// Sort table's count by ascending to improve performance. // Sort table's count by ascending to improve performance.
foreach ($this->totals->sortBy('count') as $total) { foreach ($this->totals->sortBy('count') as $total) {
@ -105,7 +105,6 @@ class Version210 extends Listener
$this->$method(); $this->$method();
} }
$this->updateCreditNoteTransactionsTable();
$this->updateDocumentIds(); $this->updateDocumentIds();
}); });
@ -125,7 +124,7 @@ class Version210 extends Listener
private function updateInvoiceIds(): void private function updateInvoiceIds(): void
{ {
// Invoice ids do not changed // Invoice ids did not changed
if ('invoice' === $this->totals->sortByDesc('count')->pluck('type')->first()) { if ('invoice' === $this->totals->sortByDesc('count')->pluck('type')->first()) {
return; return;
} }
@ -151,7 +150,7 @@ class Version210 extends Listener
private function updateBillIds(): void private function updateBillIds(): void
{ {
// Bill ids do not changed // Bill ids did not changed
if ('bill' === $this->totals->sortByDesc('count')->pluck('type')->first()) { if ('bill' === $this->totals->sortByDesc('count')->pluck('type')->first()) {
return; return;
} }
@ -180,6 +179,7 @@ class Version210 extends Listener
$this->totals = $this->getTotals(); $this->totals = $this->getTotals();
$this->updateInvoiceIds(); $this->updateInvoiceIds();
$this->updateBillIds(); $this->updateBillIds();
$this->updateCreditNoteTransactionsTable();
$tables = [ $tables = [
'recurring' => 'recurable', 'recurring' => 'recurable',
@ -438,57 +438,29 @@ class Version210 extends Listener
return; return;
} }
$invoices = DB::table('credits_transactions') // Invoice ids did not changed
->join('invoices', 'credits_transactions.document_id', '=', 'invoices.id') if ('invoice' !== $this->totals->sortByDesc('count')->pluck('type')->first()) {
->where('credits_transactions.type', 'expense') $incrementAmount = $this->getIncrementAmount('invoice', 's');
->get(
[
'credits_transactions.id as credits_transactions_id',
'invoices.company_id',
'invoice_number',
'invoices.deleted_at',
]
);
foreach ($invoices as $invoice) { if ($incrementAmount > 0) {
$document = DB::table('documents')
->where('document_number', $invoice->invoice_number)
->where('deleted_at', $invoice->deleted_at)
->where('company_id', $invoice->company_id)
->where('type', Document::INVOICE_TYPE)
->first('id');
if ($document) {
DB::table('credits_transactions') DB::table('credits_transactions')
->where('id', $invoice->credits_transactions_id) ->where('type', 'expense')
->update(['document_id' => $document->id]); ->whereNotNull('document_id')
->where('document_id', '<>', 0)
->increment('document_id', $incrementAmount);
} }
} }
$credit_notes = DB::table('credits_transactions') // Credit Note ids did not changed
->join('credit_notes', 'credits_transactions.document_id', '=', 'credit_notes.id') if ('credit_note' !== $this->totals->sortByDesc('count')->pluck('type')->first()) {
->where('credits_transactions.type', 'income') $incrementAmount = $this->getIncrementAmount('credit_note', 's');
->get(
[
'credits_transactions.id as credits_transactions_id',
'credit_notes.company_id',
'credit_note_number',
'credit_notes.deleted_at',
]
);
foreach ($credit_notes as $credit_note) { if ($incrementAmount > 0) {
$document = DB::table('documents')
->where('document_number', $credit_note->credit_note_number)
->where('deleted_at', $credit_note->deleted_at)
->where('company_id', $credit_note->company_id)
->where('type', self::CREDIT_NOTE_TYPE)
->first('id');
if ($document) {
DB::table('credits_transactions') DB::table('credits_transactions')
->where('id', $credit_note->credits_transactions_id) ->where('type', 'income')
->update(['document_id' => $document->id]); ->whereNotNull('document_id')
->where('document_id', '<>', 0)
->increment('document_id', $incrementAmount);
} }
} }
@ -575,7 +547,13 @@ class Version210 extends Listener
->select('type', DB::raw('COUNT(id) count')) ->select('type', DB::raw('COUNT(id) count'))
->groupBy('type') ->groupBy('type')
->orderBy('id') ->orderBy('id')
->get(); ->get()
->transform(
function ($item, $key) {
$item->type = Str::replaceFirst('-', '_', $item->type);
return $item;
}
);
return $counts; return $counts;
} }