diff --git a/app/Abstracts/DocumentModel.php b/app/Abstracts/DocumentModel.php index 98df080f5..3fe0a2d78 100644 --- a/app/Abstracts/DocumentModel.php +++ b/app/Abstracts/DocumentModel.php @@ -15,6 +15,11 @@ abstract class DocumentModel extends Model { use Cloneable, Currencies, DateTime, Media, Recurring; + public function totals_sorted() + { + return $this->totals()->orderBy('sort_order'); + } + public function scopeDue($query, $date) { return $query->whereDate('due_at', '=', $date); diff --git a/app/Http/Controllers/Purchases/Bills.php b/app/Http/Controllers/Purchases/Bills.php index 2f5c3744b..4660f5753 100644 --- a/app/Http/Controllers/Purchases/Bills.php +++ b/app/Http/Controllers/Purchases/Bills.php @@ -74,7 +74,7 @@ class Bills extends Controller $date_format = $this->getCompanyDateFormat(); // Get Bill Totals - foreach ($bill->totals as $bill_total) { + foreach ($bill->totals_sorted as $bill_total) { $bill->{$bill_total->code} = $bill_total->amount; } @@ -343,7 +343,7 @@ class Bills extends Controller $pdf = app('dompdf.wrapper'); $pdf->loadHTML($html); - $file_name = 'bill_' . time() . '.pdf'; + $file_name = $this->getBillFileName($bill); return $pdf->download($file_name); } diff --git a/app/Http/Controllers/Sales/Invoices.php b/app/Http/Controllers/Sales/Invoices.php index b9d4559d5..aaf7834a0 100644 --- a/app/Http/Controllers/Sales/Invoices.php +++ b/app/Http/Controllers/Sales/Invoices.php @@ -77,7 +77,7 @@ class Invoices extends Controller $date_format = $this->getCompanyDateFormat(); // Get Invoice Totals - foreach ($invoice->totals as $invoice_total) { + foreach ($invoice->totals_sorted as $invoice_total) { $invoice->{$invoice_total->code} = $invoice_total->amount; } @@ -332,7 +332,9 @@ class Invoices extends Controller $pdf = app('dompdf.wrapper'); $pdf->loadHTML($html); - $file = storage_path('app/temp/invoice_'.time().'.pdf'); + $file_name = $this->getInvoiceFileName($invoice); + + $file = storage_path('app/temp/' . $file_name); $invoice->pdf_path = $file; @@ -394,7 +396,7 @@ class Invoices extends Controller //$pdf->setPaper('A4', 'portrait'); - $file_name = 'invoice_'.time().'.pdf'; + $file_name = $this->getInvoiceFileName($invoice); return $pdf->download($file_name); } diff --git a/app/Traits/Purchases.php b/app/Traits/Purchases.php index e5189d469..7532c698a 100644 --- a/app/Traits/Purchases.php +++ b/app/Traits/Purchases.php @@ -2,6 +2,8 @@ namespace App\Traits; +use Illuminate\Support\Str; + trait Purchases { /** @@ -58,4 +60,14 @@ trait Purchases return $statuses; } + + public function getBillFileName($bill, $separator = '-', $extension = 'pdf') + { + return $this->getSafeBillNumber($bill, $separator) . $separator . time() . '.' . $extension; + } + + public function getSafeBillNumber($bill, $separator = '-') + { + return Str::slug($bill->bill_number, $separator, language()->getShortCode()); + } } diff --git a/app/Traits/Sales.php b/app/Traits/Sales.php index abc9e12a3..d20020f30 100644 --- a/app/Traits/Sales.php +++ b/app/Traits/Sales.php @@ -2,6 +2,8 @@ namespace App\Traits; +use Illuminate\Support\Str; + trait Sales { /** @@ -60,4 +62,14 @@ trait Sales return $statuses; } + + public function getInvoiceFileName($invoice, $separator = '-', $extension = 'pdf') + { + return $this->getSafeInvoiceNumber($invoice, $separator) . $separator . time() . '.' . $extension; + } + + public function getSafeInvoiceNumber($invoice, $separator = '-') + { + return Str::slug($invoice->invoice_number, $separator, language()->getShortCode()); + } } diff --git a/resources/views/portal/invoices/show.blade.php b/resources/views/portal/invoices/show.blade.php index c3138d577..cdc985bbc 100644 --- a/resources/views/portal/invoices/show.blade.php +++ b/resources/views/portal/invoices/show.blade.php @@ -236,7 +236,7 @@
- @foreach ($invoice->totals as $total) + @foreach ($invoice->totals_sorted as $total) @if ($total->code != 'total') @stack($total->code . '_td_start') diff --git a/resources/views/portal/invoices/signed.blade.php b/resources/views/portal/invoices/signed.blade.php index 34e95ef09..e1d172c4e 100644 --- a/resources/views/portal/invoices/signed.blade.php +++ b/resources/views/portal/invoices/signed.blade.php @@ -184,7 +184,7 @@
- @foreach($invoice->totals as $total) + @foreach($invoice->totals_sorted as $total) @if($total->code != 'total') diff --git a/resources/views/purchases/bills/print.blade.php b/resources/views/purchases/bills/print.blade.php index bc16f7865..330e0f86b 100644 --- a/resources/views/purchases/bills/print.blade.php +++ b/resources/views/purchases/bills/print.blade.php @@ -173,7 +173,7 @@
- @foreach ($bill->totals as $total) + @foreach ($bill->totals_sorted as $total) @if ($total->code != 'total') @stack($total->code . '_td_start')
diff --git a/resources/views/purchases/bills/show.blade.php b/resources/views/purchases/bills/show.blade.php index 3611cc7a5..322e17203 100644 --- a/resources/views/purchases/bills/show.blade.php +++ b/resources/views/purchases/bills/show.blade.php @@ -410,7 +410,7 @@
{{ trans($total['name']) }}:
- @foreach ($bill->totals as $total) + @foreach ($bill->totals_sorted as $total) @if ($total->code != 'total') @stack($total->code . '_td_start') diff --git a/resources/views/sales/invoices/print_classic.blade.php b/resources/views/sales/invoices/print_classic.blade.php index ee2f0018f..83cca35a7 100644 --- a/resources/views/sales/invoices/print_classic.blade.php +++ b/resources/views/sales/invoices/print_classic.blade.php @@ -111,7 +111,7 @@ @date($invoice->due_at)

@stack('due_at_input_end') - @foreach ($invoice->totals as $total) + @foreach ($invoice->totals_sorted as $total) @if ($total->code == 'total') {{ trans($total->name) }}:@money($total->amount - $invoice->paid, $invoice->currency_code, true)

@@ -189,7 +189,7 @@
- @foreach ($invoice->totals as $total) + @foreach ($invoice->totals_sorted as $total) @if ($total->code != 'total') @stack($total->code . '_td_start')
diff --git a/resources/views/sales/invoices/print_default.blade.php b/resources/views/sales/invoices/print_default.blade.php index 0797f1218..0fcf537eb 100644 --- a/resources/views/sales/invoices/print_default.blade.php +++ b/resources/views/sales/invoices/print_default.blade.php @@ -177,7 +177,7 @@
- @foreach ($invoice->totals as $total) + @foreach ($invoice->totals_sorted as $total) @if ($total->code != 'total') @stack($total->code . '_td_start')
diff --git a/resources/views/sales/invoices/print_modern.blade.php b/resources/views/sales/invoices/print_modern.blade.php index 4c58bae8e..dade8bec3 100644 --- a/resources/views/sales/invoices/print_modern.blade.php +++ b/resources/views/sales/invoices/print_modern.blade.php @@ -158,7 +158,7 @@
- @foreach ($invoice->totals as $total) + @foreach ($invoice->totals_sorted as $total) @if ($total->code != 'total') @stack($total->code . '_td_start') {{ trans($total->title) }}: diff --git a/resources/views/sales/invoices/show.blade.php b/resources/views/sales/invoices/show.blade.php index b5dafbf9f..f11cbae79 100644 --- a/resources/views/sales/invoices/show.blade.php +++ b/resources/views/sales/invoices/show.blade.php @@ -427,7 +427,7 @@
- @foreach ($invoice->totals as $total) + @foreach ($invoice->totals_sorted as $total) @if ($total->code != 'total') @stack($total->code . '_td_start')