Merge branch 'master' of github.com:akaunting/akaunting

This commit is contained in:
Cüneyt Şentürk 2021-01-26 15:33:57 +03:00
commit f7e3e7fc60
3 changed files with 41 additions and 17 deletions

View File

@ -512,7 +512,13 @@ abstract class DocumentShow extends Base
$this->hideNote = $hideNote; $this->hideNote = $hideNote;
$this->hideAttachment = $hideAttachment; $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->textItems = $textItems;
$this->textQuantity = $textQuantity; $this->textQuantity = $textQuantity;

View File

@ -100,8 +100,8 @@ class Version210 extends Listener
$this->totals = collect($this->getTotals(['invoice', 'bill', 'estimate', 'credit_note', 'debit_note'])); $this->totals = collect($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->sort() as $table => $count) { foreach ($this->totals->sortBy('count') as $total) {
$method = 'copy' . Str::plural(Str::studly($table)); $method = 'copy' . Str::plural(Str::studly($total->type));
$this->$method(); $this->$method();
} }
@ -125,10 +125,8 @@ class Version210 extends Listener
private function updateInvoiceIds(): void private function updateInvoiceIds(): void
{ {
$sorted = $this->totals->sortDesc()->keys();
// Invoice ids do not changed // Invoice ids do not changed
if ('invoice' === $sorted->first()) { if ('invoice' === $this->totals->sortByDesc('count')->pluck('type')->first()) {
return; return;
} }
@ -153,10 +151,8 @@ class Version210 extends Listener
private function updateBillIds(): void private function updateBillIds(): void
{ {
$sorted = $this->totals->sortDesc()->keys();
// Bill ids do not changed // Bill ids do not changed
if ('bill' === $sorted->first()) { if ('bill' === $this->totals->sortByDesc('count')->pluck('type')->first()) {
return; return;
} }
@ -181,6 +177,7 @@ class Version210 extends Listener
private function updateDocumentIds() private function updateDocumentIds()
{ {
$this->totals = $this->getTotals();
$this->updateInvoiceIds(); $this->updateInvoiceIds();
$this->updateBillIds(); $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; $incrementAmount = 0;
foreach ($this->totals->sortDesc()->keys()->takeUntil($key) as $table) { foreach ($this->totals->sortByDesc('count')->pluck('type')->takeUntil($type) as $table) {
$incrementAmount += optional( $incrementAmount += optional(
DB::table($table . $suffix)->orderByDesc('id')->first('id'), DB::table($table . $suffix)->orderByDesc('id')->first('id'),
function ($document) { 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 = []; $counts = [];
foreach ($tables as $table) { foreach ($types as $type) {
if (!Schema::hasTable(Str::plural($table))) { if (!Schema::hasTable(Str::plural($type))) {
continue; 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 private function batchCopyRelations(string $table, string $type): void

View File

@ -3,6 +3,7 @@
namespace App\Traits; namespace App\Traits;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Str;
trait Relationships trait Relationships
{ {
@ -15,7 +16,8 @@ trait Relationships
continue; 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; return $counter;