From 047a0fb2be37049b0a199c3db58913485925d442 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 19 Jul 2017 12:59:56 +0300 Subject: [PATCH] Support bulk downloading PDFs --- app/Jobs/DownloadInvoices.php | 75 +++++++++++++++++++ app/Ninja/Datatables/InvoiceDatatable.php | 5 ++ app/Ninja/Mailers/UserMailer.php | 7 +- app/Services/InvoiceService.php | 17 +++++ app/Services/PaymentService.php | 4 +- .../2017_07_18_124150_add_late_fees.php | 41 ++++++++++ resources/lang/en/texts.php | 3 + 7 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 app/Jobs/DownloadInvoices.php create mode 100644 database/migrations/2017_07_18_124150_add_late_fees.php diff --git a/app/Jobs/DownloadInvoices.php b/app/Jobs/DownloadInvoices.php new file mode 100644 index 000000000000..be731c178fe9 --- /dev/null +++ b/app/Jobs/DownloadInvoices.php @@ -0,0 +1,75 @@ +user = $user; + $this->invoices = $invoices; + } + + /** + * Execute the job. + * + * @param ContactMailer $mailer + */ + public function handle(UserMailer $userMailer) + { + // if queues are disabled download a zip file + if (config('queue.default') === 'sync') { + $zip = Archive::instance_by_useragent(date('Y-m-d') . '-Invoice_PDFs'); + foreach ($this->invoices as $invoice) { + $zip->add_file($invoice->getFileName(), $invoice->getPDFString()); + } + $zip->finish(); + exit; + + // otherwise sends the PDFs in an email + } else { + $data = []; + foreach ($this->invoices as $invoice) { + $data[] = [ + 'name' => $invoice->getFileName(), + 'data' => $invoice->getPDFString(), + ]; + } + + $subject = trans('texts.invoices_are_attached'); + $data = [ + 'documents' => $data + ]; + + $userMailer->sendMessage($this->user, $subject, false, $data); + } + } +} diff --git a/app/Ninja/Datatables/InvoiceDatatable.php b/app/Ninja/Datatables/InvoiceDatatable.php index f76144b3faeb..35da042591f0 100644 --- a/app/Ninja/Datatables/InvoiceDatatable.php +++ b/app/Ninja/Datatables/InvoiceDatatable.php @@ -185,10 +185,15 @@ class InvoiceDatatable extends EntityDatatable if ($this->entityType == ENTITY_INVOICE || $this->entityType == ENTITY_QUOTE) { $actions[] = \DropdownButton::DIVIDER; + $actions[] = [ + 'label' => mtrans($this->entityType, 'download_' . $this->entityType), + 'url' => 'javascript:submitForm_'.$this->entityType.'("download")', + ]; $actions[] = [ 'label' => mtrans($this->entityType, 'email_' . $this->entityType), 'url' => 'javascript:submitForm_'.$this->entityType.'("emailInvoice")', ]; + $actions[] = \DropdownButton::DIVIDER; $actions[] = [ 'label' => mtrans($this->entityType, 'mark_sent'), 'url' => 'javascript:submitForm_'.$this->entityType.'("markSent")', diff --git a/app/Ninja/Mailers/UserMailer.php b/app/Ninja/Mailers/UserMailer.php index e4698897195f..aacfb530fadb 100644 --- a/app/Ninja/Mailers/UserMailer.php +++ b/app/Ninja/Mailers/UserMailer.php @@ -119,14 +119,17 @@ class UserMailer extends Mailer /** * @param Invitation $invitation */ - public function sendMessage($user, $subject, $message, $invoice = false) + public function sendMessage($user, $subject, $message, $data = false) { if (! $user->email) { return; } + $invoice = $data && isset($data['invoice']) ? $data['invoice'] : false; $view = 'user_message'; - $data = [ + + $data = $data ?: []; + $data += [ 'userName' => $user->getDisplayName(), 'primaryMessage' => $subject, 'secondaryMessage' => $message, diff --git a/app/Services/InvoiceService.php b/app/Services/InvoiceService.php index db84008750f0..5eb2eaaf1820 100644 --- a/app/Services/InvoiceService.php +++ b/app/Services/InvoiceService.php @@ -9,6 +9,7 @@ use App\Models\Invoice; use App\Ninja\Datatables\InvoiceDatatable; use App\Ninja\Repositories\ClientRepository; use App\Ninja\Repositories\InvoiceRepository; +use App\Jobs\DownloadInvoices; use Auth; use Utils; @@ -54,6 +55,22 @@ class InvoiceService extends BaseService return $this->invoiceRepo; } + /** + * @param $ids + * @param $action + * + * @return int + */ + public function bulk($ids, $action) + { + if ($action == 'download') { + $invoices = $this->getRepo()->findByPublicIdsWithTrashed($ids); + dispatch(new DownloadInvoices(Auth::user(), $invoices)); + } else { + return parent::bulk($ids, $action); + } + } + /** * @param array $data * @param Invoice|null $invoice diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index e65e57e7f439..1c8f5b97fe5d 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -140,7 +140,9 @@ class PaymentService extends BaseService $subject = trans('texts.auto_bill_failed', ['invoice_number' => $invoice->invoice_number]); $message = sprintf('%s: %s', ucwords($paymentDriver->providerName()), $exception->getMessage()); $mailer = app('App\Ninja\Mailers\UserMailer'); - $mailer->sendMessage($invoice->user, $subject, $message, $invoice); + $mailer->sendMessage($invoice->user, $subject, $message, [ + 'invoice' => $invoice + ]); } return false; diff --git a/database/migrations/2017_07_18_124150_add_late_fees.php b/database/migrations/2017_07_18_124150_add_late_fees.php new file mode 100644 index 000000000000..b5fe0f4be135 --- /dev/null +++ b/database/migrations/2017_07_18_124150_add_late_fees.php @@ -0,0 +1,41 @@ +decimal('late_fee1_amount', 13, 2)->nullable(); + $table->decimal('late_fee1_percent', 13, 3)->nullable(); + $table->decimal('late_fee2_amount', 13, 2)->nullable(); + $table->decimal('late_fee2_percent', 13, 3)->nullable(); + $table->decimal('late_fee3_amount', 13, 2)->nullable(); + $table->decimal('late_fee3_percent', 13, 3)->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('account_email_settings', function ($table) { + $table->dropColumn('late_fee1_amount'); + $table->dropColumn('late_fee1_percent'); + $table->dropColumn('late_fee2_amount'); + $table->dropColumn('late_fee2_percent'); + $table->dropColumn('late_fee3_amount'); + $table->dropColumn('late_fee3_percent'); + }); + } +} diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 11ef9b6a3037..abc8104c63da 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2308,6 +2308,9 @@ $LANG = array( 'late_fee_amount' => 'Late Fee Amount', 'late_fee_percent' => 'Late Fee Percent', 'late_fee_added' => 'Late fee added on :date', + 'download_invoice' => 'Download Invoice', + 'download_quote' => 'Download Quote', + 'invoices_are_attached' => 'Your invoice PDFs are attached.', );