mirror of
https://github.com/crater-invoice/crater.git
synced 2025-12-15 09:52:55 -05:00
Send Invoices/Estimates/Payments as email attachments
This commit is contained in:
@@ -13,15 +13,17 @@ class SendEstimateMail extends Mailable
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public $data = [];
|
||||
public $pdfData;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($data)
|
||||
public function __construct($data, $pdfData)
|
||||
{
|
||||
$this->data = $data;
|
||||
$this->pdfData = $pdfData;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,9 +42,17 @@ class SendEstimateMail extends Mailable
|
||||
'mailable_id' => $this->data['estimate']['id']
|
||||
]);
|
||||
|
||||
return $this->from($this->data['from'])
|
||||
->subject($this->data['subject'])
|
||||
->markdown('emails.send.estimate', ['data', $this->data]);
|
||||
$mailContent = $this->from($this->data['from'])
|
||||
->subject($this->data['subject'])
|
||||
->markdown('emails.send.estimate', ['data', $this->data]);
|
||||
|
||||
if ($this->pdfData) {
|
||||
$mailContent->attachData(
|
||||
$this->pdfData->output(),
|
||||
$this->data['estimate']['estimate_number'] . '.pdf'
|
||||
);
|
||||
}
|
||||
|
||||
return $mailContent;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,15 +13,17 @@ class SendInvoiceMail extends Mailable
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public $data = [];
|
||||
public $pdfData;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($data)
|
||||
public function __construct($data, $pdfData)
|
||||
{
|
||||
$this->data = $data;
|
||||
$this->pdfData = $pdfData;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,9 +41,18 @@ class SendInvoiceMail extends Mailable
|
||||
'mailable_type' => Invoice::class,
|
||||
'mailable_id' => $this->data['invoice']['id']
|
||||
]);
|
||||
|
||||
$mailContent = $this->from($this->data['from'])
|
||||
->subject($this->data['subject'])
|
||||
->markdown('emails.send.invoice', ['data', $this->data]);
|
||||
|
||||
return $this->from($this->data['from'])
|
||||
->subject($this->data['subject'])
|
||||
->markdown('emails.send.invoice', ['data', $this->data]);
|
||||
if ($this->pdfData) {
|
||||
$mailContent->attachData(
|
||||
$this->pdfData->output(),
|
||||
$this->data['invoice']['invoice_number'] . '.pdf'
|
||||
);
|
||||
}
|
||||
|
||||
return $mailContent;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,15 +14,17 @@ class SendPaymentMail extends Mailable
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public $data = [];
|
||||
public $pdfData;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($data)
|
||||
public function __construct($data, $pdfData)
|
||||
{
|
||||
$this->data = $data;
|
||||
$this->pdfData = $pdfData;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,9 +43,17 @@ class SendPaymentMail extends Mailable
|
||||
'mailable_id' => $this->data['payment']['id']
|
||||
]);
|
||||
|
||||
return $this->from($this->data['from'])
|
||||
->subject($this->data['subject'])
|
||||
->markdown('emails.send.payment', ['data', $this->data]);
|
||||
|
||||
$mailContent = $this->from($this->data['from'])
|
||||
->subject($this->data['subject'])
|
||||
->markdown('emails.send.payment', ['data', $this->data]);
|
||||
|
||||
if ($this->pdfData) {
|
||||
$mailContent->attachData(
|
||||
$this->pdfData->output(),
|
||||
$this->data['payment']['payment_number'] . '.pdf'
|
||||
);
|
||||
}
|
||||
|
||||
return $mailContent;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,8 +379,9 @@ class Estimate extends Model implements HasMedia
|
||||
$data['user'] = $this->user->toArray();
|
||||
$data['company'] = $this->company->toArray();
|
||||
$data['body'] = $this->getEmailBody($data['body']);
|
||||
$pdfData = ($this->getEmailAttachmentSetting()) ? $this->getPDFData() : null;
|
||||
|
||||
\Mail::to($data['to'])->send(new SendEstimateMail($data));
|
||||
\Mail::to($data['to'])->send(new SendEstimateMail($data, $pdfData));
|
||||
|
||||
if ($this->status == Estimate::STATUS_DRAFT) {
|
||||
$this->status = Estimate::STATUS_SENT;
|
||||
@@ -468,6 +469,17 @@ class Estimate extends Model implements HasMedia
|
||||
return $this->getFormattedString($this->notes);
|
||||
}
|
||||
|
||||
public function getEmailAttachmentSetting()
|
||||
{
|
||||
$estimateAsAttachment = CompanySetting::getSetting('estimate_email_attachment', $this->company_id);
|
||||
|
||||
if($estimateAsAttachment == 'NO') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getEmailBody($body)
|
||||
{
|
||||
$values = array_merge($this->getFieldsArray(), $this->getExtraFields());
|
||||
|
||||
@@ -429,6 +429,7 @@ class Invoice extends Model implements HasMedia
|
||||
$data['user'] = $this->user->toArray();
|
||||
$data['company'] = Company::find($this->company_id);
|
||||
$data['body'] = $this->getEmailBody($data['body']);
|
||||
$pdfData = ($this->getEmailAttachmentSetting()) ? $this->getPDFData() : null;
|
||||
|
||||
if ($this->status == Invoice::STATUS_DRAFT) {
|
||||
$this->status = Invoice::STATUS_SENT;
|
||||
@@ -436,7 +437,7 @@ class Invoice extends Model implements HasMedia
|
||||
$this->save();
|
||||
}
|
||||
|
||||
\Mail::to($data['to'])->send(new SendInvoiceMail($data));
|
||||
\Mail::to($data['to'])->send(new SendInvoiceMail($data, $pdfData));
|
||||
|
||||
return [
|
||||
'success' => true
|
||||
@@ -526,6 +527,17 @@ class Invoice extends Model implements HasMedia
|
||||
return PDF::loadView('app.pdf.invoice.' . $invoiceTemplate->view);
|
||||
}
|
||||
|
||||
public function getEmailAttachmentSetting()
|
||||
{
|
||||
$invoiceAsAttachment = CompanySetting::getSetting('invoice_email_attachment', $this->company_id);
|
||||
|
||||
if($invoiceAsAttachment == 'NO') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getCompanyAddress()
|
||||
{
|
||||
$format = CompanySetting::getSetting('invoice_company_address_format', $this->company_id);
|
||||
|
||||
@@ -124,8 +124,9 @@ class Payment extends Model implements HasMedia
|
||||
$data['user'] = $this->user->toArray();
|
||||
$data['company'] = Company::find($this->company_id);
|
||||
$data['body'] = $this->getEmailBody($data['body']);
|
||||
$pdfData = ($this->getEmailAttachmentSetting()) ? $this->getPDFData() : null;
|
||||
|
||||
\Mail::to($data['to'])->send(new SendPaymentMail($data));
|
||||
\Mail::to($data['to'])->send(new SendPaymentMail($data, $pdfData));
|
||||
|
||||
return [
|
||||
'success' => true
|
||||
@@ -400,6 +401,17 @@ class Payment extends Model implements HasMedia
|
||||
return $this->getFormattedString($format);
|
||||
}
|
||||
|
||||
public function getEmailAttachmentSetting()
|
||||
{
|
||||
$paymentAsAttachment = CompanySetting::getSetting('payment_email_attachment', $this->company_id);
|
||||
|
||||
if($paymentAsAttachment == 'NO') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getNotes()
|
||||
{
|
||||
return $this->getFormattedString($this->notes);
|
||||
|
||||
Reference in New Issue
Block a user