Support bulk downloading PDFs

This commit is contained in:
Hillel Coren 2017-07-19 12:59:56 +03:00
parent 2bef0bdb66
commit 047a0fb2be
7 changed files with 149 additions and 3 deletions

View File

@ -0,0 +1,75 @@
<?php
namespace App\Jobs;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\User;
use App\Ninja\Mailers\UserMailer;
use Barracuda\ArchiveStream\Archive;
/**
* Class SendInvoiceEmail.
*/
class DownloadInvoices extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/**
* @var User
*/
protected $user;
/**
* @var array
*/
protected $invoices;
/**
* Create a new job instance.
*
* @param mixed $files
* @param mixed $settings
*/
public function __construct(User $user, $invoices)
{
$this->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);
}
}
}

View File

@ -185,10 +185,15 @@ class InvoiceDatatable extends EntityDatatable
if ($this->entityType == ENTITY_INVOICE || $this->entityType == ENTITY_QUOTE) { if ($this->entityType == ENTITY_INVOICE || $this->entityType == ENTITY_QUOTE) {
$actions[] = \DropdownButton::DIVIDER; $actions[] = \DropdownButton::DIVIDER;
$actions[] = [
'label' => mtrans($this->entityType, 'download_' . $this->entityType),
'url' => 'javascript:submitForm_'.$this->entityType.'("download")',
];
$actions[] = [ $actions[] = [
'label' => mtrans($this->entityType, 'email_' . $this->entityType), 'label' => mtrans($this->entityType, 'email_' . $this->entityType),
'url' => 'javascript:submitForm_'.$this->entityType.'("emailInvoice")', 'url' => 'javascript:submitForm_'.$this->entityType.'("emailInvoice")',
]; ];
$actions[] = \DropdownButton::DIVIDER;
$actions[] = [ $actions[] = [
'label' => mtrans($this->entityType, 'mark_sent'), 'label' => mtrans($this->entityType, 'mark_sent'),
'url' => 'javascript:submitForm_'.$this->entityType.'("markSent")', 'url' => 'javascript:submitForm_'.$this->entityType.'("markSent")',

View File

@ -119,14 +119,17 @@ class UserMailer extends Mailer
/** /**
* @param Invitation $invitation * @param Invitation $invitation
*/ */
public function sendMessage($user, $subject, $message, $invoice = false) public function sendMessage($user, $subject, $message, $data = false)
{ {
if (! $user->email) { if (! $user->email) {
return; return;
} }
$invoice = $data && isset($data['invoice']) ? $data['invoice'] : false;
$view = 'user_message'; $view = 'user_message';
$data = [
$data = $data ?: [];
$data += [
'userName' => $user->getDisplayName(), 'userName' => $user->getDisplayName(),
'primaryMessage' => $subject, 'primaryMessage' => $subject,
'secondaryMessage' => $message, 'secondaryMessage' => $message,

View File

@ -9,6 +9,7 @@ use App\Models\Invoice;
use App\Ninja\Datatables\InvoiceDatatable; use App\Ninja\Datatables\InvoiceDatatable;
use App\Ninja\Repositories\ClientRepository; use App\Ninja\Repositories\ClientRepository;
use App\Ninja\Repositories\InvoiceRepository; use App\Ninja\Repositories\InvoiceRepository;
use App\Jobs\DownloadInvoices;
use Auth; use Auth;
use Utils; use Utils;
@ -54,6 +55,22 @@ class InvoiceService extends BaseService
return $this->invoiceRepo; 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 array $data
* @param Invoice|null $invoice * @param Invoice|null $invoice

View File

@ -140,7 +140,9 @@ class PaymentService extends BaseService
$subject = trans('texts.auto_bill_failed', ['invoice_number' => $invoice->invoice_number]); $subject = trans('texts.auto_bill_failed', ['invoice_number' => $invoice->invoice_number]);
$message = sprintf('%s: %s', ucwords($paymentDriver->providerName()), $exception->getMessage()); $message = sprintf('%s: %s', ucwords($paymentDriver->providerName()), $exception->getMessage());
$mailer = app('App\Ninja\Mailers\UserMailer'); $mailer = app('App\Ninja\Mailers\UserMailer');
$mailer->sendMessage($invoice->user, $subject, $message, $invoice); $mailer->sendMessage($invoice->user, $subject, $message, [
'invoice' => $invoice
]);
} }
return false; return false;

View File

@ -0,0 +1,41 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddLateFees extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('account_email_settings', function ($table) {
$table->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');
});
}
}

View File

@ -2308,6 +2308,9 @@ $LANG = array(
'late_fee_amount' => 'Late Fee Amount', 'late_fee_amount' => 'Late Fee Amount',
'late_fee_percent' => 'Late Fee Percent', 'late_fee_percent' => 'Late Fee Percent',
'late_fee_added' => 'Late fee added on :date', 'late_fee_added' => 'Late fee added on :date',
'download_invoice' => 'Download Invoice',
'download_quote' => 'Download Quote',
'invoices_are_attached' => 'Your invoice PDFs are attached.',
); );