diff --git a/app/Abstracts/View/Components/DocumentShow.php b/app/Abstracts/View/Components/DocumentShow.php index b9f73d80d..cf9f87688 100644 --- a/app/Abstracts/View/Components/DocumentShow.php +++ b/app/Abstracts/View/Components/DocumentShow.php @@ -512,7 +512,13 @@ abstract class DocumentShow extends Base $this->hideNote = $hideNote; $this->hideAttachment = $hideAttachment; - $this->attachment = !empty($attachment) ? $attachment : !empty($document) ? $document->attachment : ''; + $this->attachment = ''; + + if (!empty($attachment)) { + $this->attachment = $attachment; + } else if (!empty($document)) { + $this->attachment = $document->attachment; + } $this->textItems = $textItems; $this->textQuantity = $textQuantity; diff --git a/app/Listeners/Update/V21/Version210.php b/app/Listeners/Update/V21/Version210.php index f58a83ad5..ef4f74602 100644 --- a/app/Listeners/Update/V21/Version210.php +++ b/app/Listeners/Update/V21/Version210.php @@ -100,8 +100,8 @@ class Version210 extends Listener $this->totals = collect($this->getTotals(['invoice', 'bill', 'estimate', 'credit_note', 'debit_note'])); // Sort table's count by ascending to improve performance. - foreach ($this->totals->sort() as $table => $count) { - $method = 'copy' . Str::plural(Str::studly($table)); + foreach ($this->totals->sortBy('count') as $total) { + $method = 'copy' . Str::plural(Str::studly($total->type)); $this->$method(); } @@ -125,10 +125,8 @@ class Version210 extends Listener private function updateInvoiceIds(): void { - $sorted = $this->totals->sortDesc()->keys(); - // Invoice ids do not changed - if ('invoice' === $sorted->first()) { + if ('invoice' === $this->totals->sortByDesc('count')->pluck('type')->first()) { return; } @@ -153,10 +151,8 @@ class Version210 extends Listener private function updateBillIds(): void { - $sorted = $this->totals->sortDesc()->keys(); - // Bill ids do not changed - if ('bill' === $sorted->first()) { + if ('bill' === $this->totals->sortByDesc('count')->pluck('type')->first()) { return; } @@ -181,6 +177,7 @@ class Version210 extends Listener private function updateDocumentIds() { + $this->totals = $this->getTotals(); $this->updateInvoiceIds(); $this->updateBillIds(); @@ -420,11 +417,11 @@ class Version210 extends Listener } } - private function getIncrementAmount(string $key, string $suffix): int + private function getIncrementAmount(string $type, string $suffix): int { $incrementAmount = 0; - foreach ($this->totals->sortDesc()->keys()->takeUntil($key) as $table) { + foreach ($this->totals->sortByDesc('count')->pluck('type')->takeUntil($type) as $table) { $incrementAmount += optional( DB::table($table . $suffix)->orderByDesc('id')->first('id'), function ($document) { @@ -571,18 +568,37 @@ class Version210 extends Listener } } - private function getTotals(array $tables): array + private function getTotals(array $types = []): Collection { + if (DB::table('documents')->count() > 0) { + $counts = DB::table('documents') + ->select('type', DB::raw('COUNT(id) count')) + ->groupBy('type') + ->orderBy('id') + ->get(); + + return $counts; + } + $counts = []; - foreach ($tables as $table) { - if (!Schema::hasTable(Str::plural($table))) { + foreach ($types as $type) { + if (!Schema::hasTable(Str::plural($type))) { continue; } - $counts[$table] = DB::table(Str::plural($table))->count(); + $count = DB::table(Str::plural($type))->count(); + + if ($count === 0) { + continue; + } + + $values = new \stdClass(); + $values->type = $type; + $values->count = $count; + $counts[] = $values; } - return $counts; + return collect($counts); } private function batchCopyRelations(string $table, string $type): void diff --git a/app/Traits/Relationships.php b/app/Traits/Relationships.php index b220037ba..b73e7cc11 100644 --- a/app/Traits/Relationships.php +++ b/app/Traits/Relationships.php @@ -3,6 +3,7 @@ namespace App\Traits; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Support\Str; trait Relationships { @@ -15,7 +16,8 @@ trait Relationships continue; } - $counter[] = $c . ' ' . strtolower(trans_choice('general.' . $text, ($c > 1) ? 2 : 1)); + $text = Str::contains($text, '::') ? $text : 'general.' . $text; + $counter[] = $c . ' ' . strtolower(trans_choice($text, ($c > 1) ? 2 : 1)); } return $counter;