Working on UBL

This commit is contained in:
Hillel Coren 2018-01-15 08:05:07 +02:00
parent 2f80e1cc77
commit 2723fb8014
5 changed files with 54 additions and 19 deletions

View File

@ -12,6 +12,7 @@ use CleverIt\UBL\Invoice\Contact;
use CleverIt\UBL\Invoice\TaxTotal; use CleverIt\UBL\Invoice\TaxTotal;
use CleverIt\UBL\Invoice\TaxSubTotal; use CleverIt\UBL\Invoice\TaxSubTotal;
use CleverIt\UBL\Invoice\TaxCategory; use CleverIt\UBL\Invoice\TaxCategory;
use CleverIt\UBL\Invoice\TaxScheme;
use CleverIt\UBL\Invoice\InvoiceLine; use CleverIt\UBL\Invoice\InvoiceLine;
use CleverIt\UBL\Invoice\Item; use CleverIt\UBL\Invoice\Item;
use CleverIt\UBL\Invoice\LegalMonetaryTotal; use CleverIt\UBL\Invoice\LegalMonetaryTotal;
@ -57,26 +58,32 @@ class ConvertInvoiceToUbl extends Job
$taxtotal = new TaxTotal(); $taxtotal = new TaxTotal();
$taxAmount1 = $taxAmount2 = 0; $taxAmount1 = $taxAmount2 = 0;
if ($item->tax_name1 || floatval($item->tax_rate1)) { if ($invoice->tax_name1 || floatval($invoice->tax_rate1)) {
$taxAmount1 = $invoice->taxAmount($taxable, $invoice->tax_rate1); $taxAmount1 = $invoice->taxAmount($taxable, $invoice->tax_rate1);
$taxScheme = ((new TaxScheme()))
->setId($invoice->tax_name1);
$taxtotal->addTaxSubTotal((new TaxSubTotal()) $taxtotal->addTaxSubTotal((new TaxSubTotal())
->setTaxAmount($taxAmount1) ->setTaxAmount($taxAmount1)
->setTaxableAmount($taxable) ->setTaxableAmount($taxable)
->setTaxCategory((new TaxCategory()) ->setTaxCategory((new TaxCategory())
->setId($item->tax_name1) ->setId($invoice->tax_name1)
->setName($item->tax_name1) ->setName($invoice->tax_name1)
->setPercent($item->tax_rate1))); ->setTaxScheme($taxScheme)
->setPercent($invoice->tax_rate1)));
} }
if ($item->tax_name2 || floatval($item->tax_rate2)) { if ($invoice->tax_name2 || floatval($invoice->tax_rate2)) {
$itemTaxAmount2 = $invoice->taxAmount($taxable, $invoice->tax_rate2); $itemTaxAmount2 = $invoice->taxAmount($taxable, $invoice->tax_rate2);
$taxScheme = ((new TaxScheme()))
->setId($invoice->tax_name2);
$taxtotal->addTaxSubTotal((new TaxSubTotal()) $taxtotal->addTaxSubTotal((new TaxSubTotal())
->setTaxAmount($taxAmount2) ->setTaxAmount($taxAmount2)
->setTaxableAmount($taxable) ->setTaxableAmount($taxable)
->setTaxCategory((new TaxCategory()) ->setTaxCategory((new TaxCategory())
->setId($item->tax_name2) ->setId($invoice->tax_name2)
->setName($item->tax_name2) ->setName($invoice->tax_name2)
->setPercent($item->tax_rate2))); ->setTaxScheme($taxScheme)
->setPercent($invoice->tax_rate2)));
} }
$taxtotal->setTaxAmount($taxAmount1 + $taxAmount2); $taxtotal->setTaxAmount($taxAmount1 + $taxAmount2);
@ -134,23 +141,29 @@ class ConvertInvoiceToUbl extends Job
if ($item->tax_name1 || floatval($item->tax_rate1)) { if ($item->tax_name1 || floatval($item->tax_rate1)) {
$itemTaxAmount1 = $invoice->taxAmount($taxable, $item->tax_rate1); $itemTaxAmount1 = $invoice->taxAmount($taxable, $item->tax_rate1);
$taxScheme = ((new TaxScheme()))
->setId($item->tax_name1);
$taxtotal->addTaxSubTotal((new TaxSubTotal()) $taxtotal->addTaxSubTotal((new TaxSubTotal())
->setTaxAmount($itemTaxAmount1) ->setTaxAmount($itemTaxAmount1)
->setTaxableAmount($taxable) ->setTaxableAmount($taxable)
->setTaxCategory((new TaxCategory()) ->setTaxCategory((new TaxCategory())
->setId($item->tax_name1) ->setId($item->tax_name1)
->setName($item->tax_name1) ->setName($item->tax_name1)
->setTaxScheme($taxScheme)
->setPercent($item->tax_rate1))); ->setPercent($item->tax_rate1)));
} }
if ($item->tax_name2 || floatval($item->tax_rate2)) { if ($item->tax_name2 || floatval($item->tax_rate2)) {
$itemTaxAmount2 = $invoice->taxAmount($taxable, $item->tax_rate2); $itemTaxAmount2 = $invoice->taxAmount($taxable, $item->tax_rate2);
$taxScheme = ((new TaxScheme()))
->setId($item->tax_name2);
$taxtotal->addTaxSubTotal((new TaxSubTotal()) $taxtotal->addTaxSubTotal((new TaxSubTotal())
->setTaxAmount($itemTaxAmount2) ->setTaxAmount($itemTaxAmount2)
->setTaxableAmount($taxable) ->setTaxableAmount($taxable)
->setTaxCategory((new TaxCategory()) ->setTaxCategory((new TaxCategory())
->setId($item->tax_name2) ->setId($item->tax_name2)
->setName($item->tax_name2) ->setName($item->tax_name2)
->setTaxScheme($taxScheme)
->setPercent($item->tax_rate2))); ->setPercent($item->tax_rate2)));
} }

View File

@ -1485,6 +1485,14 @@ class Account extends Eloquent
return $this->hasFeature(FEATURE_PDF_ATTACHMENT) && $this->pdf_email_attachment; return $this->hasFeature(FEATURE_PDF_ATTACHMENT) && $this->pdf_email_attachment;
} }
/**
* @return bool
*/
public function attachUBL()
{
return $this->hasFeature(FEATURE_PDF_ATTACHMENT) && $this->ubl_email_attachment;
}
/** /**
* @return mixed * @return mixed
*/ */

View File

@ -701,11 +701,11 @@ class Invoice extends EntityModel implements BalanceAffecting
/** /**
* @return string * @return string
*/ */
public function getFileName() public function getFileName($extension = 'pdf')
{ {
$entityType = $this->getEntityType(); $entityType = $this->getEntityType();
return trans("texts.$entityType") . '_' . $this->invoice_number . '.pdf'; return trans("texts.$entityType") . '_' . $this->invoice_number . '.' . $extension;
} }
/** /**

View File

@ -8,6 +8,7 @@ use App\Models\Invitation;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Services\TemplateService; use App\Services\TemplateService;
use App\Jobs\ConvertInvoiceToUbl;
use Event; use Event;
use Utils; use Utils;
@ -61,10 +62,14 @@ class ContactMailer extends Mailer
$sent = false; $sent = false;
$pdfString = false; $pdfString = false;
$ublString = false;
if ($account->attachPDF()) { if ($account->attachPDF()) {
$pdfString = $invoice->getPDFString(); $pdfString = $invoice->getPDFString();
} }
if ($account->attachUBL()) {
$ublString = dispatch(new ConvertInvoiceToUbl($invoice));
}
$documentStrings = []; $documentStrings = [];
if ($account->document_email_attachment && $invoice->hasDocuments()) { if ($account->document_email_attachment && $invoice->hasDocuments()) {
@ -88,7 +93,12 @@ class ContactMailer extends Mailer
$isFirst = true; $isFirst = true;
foreach ($invoice->invitations as $invitation) { foreach ($invoice->invitations as $invitation) {
$response = $this->sendInvitation($invitation, $invoice, $emailTemplate, $emailSubject, $pdfString, $documentStrings, $reminder, $isFirst); $data = [
'pdfString' => $pdfString,
'documentStrings' => $documentStrings,
'ublString' => $ublString,
];
$response = $this->sendInvitation($invitation, $invoice, $emailTemplate, $emailSubject, $reminder, $isFirst, $data);
$isFirst = false; $isFirst = false;
if ($response === true) { if ($response === true) {
$sent = true; $sent = true;
@ -126,10 +136,9 @@ class ContactMailer extends Mailer
Invoice $invoice, Invoice $invoice,
$body, $body,
$subject, $subject,
$pdfString,
$documentStrings,
$reminder, $reminder,
$isFirst $isFirst,
$attachments
) { ) {
$client = $invoice->client; $client = $invoice->client;
$account = $invoice->account; $account = $invoice->account;
@ -177,16 +186,20 @@ class ContactMailer extends Mailer
'account' => $account, 'account' => $account,
'client' => $client, 'client' => $client,
'invoice' => $invoice, 'invoice' => $invoice,
'documents' => $documentStrings, 'documents' => $attachments['documentStrings'],
'notes' => $reminder, 'notes' => $reminder,
'bccEmail' => $isFirst ? $account->getBccEmail() : false, 'bccEmail' => $isFirst ? $account->getBccEmail() : false,
'fromEmail' => $account->getFromEmail(), 'fromEmail' => $account->getFromEmail(),
]; ];
if ($account->attachPDF()) { if ($account->attachPDF()) {
$data['pdfString'] = $pdfString; $data['pdfString'] = $attachments['pdfString'];
$data['pdfFileName'] = $invoice->getFileName(); $data['pdfFileName'] = $invoice->getFileName();
} }
if ($account->attachUBL()) {
$data['ublString'] = $attachments['ublString'];
$data['ublFileName'] = $invoice->getFileName('xml');
}
$subject = $this->templateService->processVariables($subject, $variables); $subject = $this->templateService->processVariables($subject, $variables);
$fromEmail = $account->getReplyToEmail() ?: $user->email; $fromEmail = $account->getReplyToEmail() ?: $user->email;

View File

@ -67,12 +67,13 @@ class Mailer
$message->bcc($data['bccEmail']); $message->bcc($data['bccEmail']);
} }
// Attach the PDF to the email // Handle invoice attachments
if (! empty($data['pdfString']) && ! empty($data['pdfFileName'])) { if (! empty($data['pdfString']) && ! empty($data['pdfFileName'])) {
$message->attachData($data['pdfString'], $data['pdfFileName']); $message->attachData($data['pdfString'], $data['pdfFileName']);
} }
if (! empty($data['ublString']) && ! empty($data['ublFileName'])) {
// Attach documents to the email $message->attachData($data['ublString'], $data['ublFileName']);
}
if (! empty($data['documents'])) { if (! empty($data['documents'])) {
foreach ($data['documents'] as $document) { foreach ($data['documents'] as $document) {
$message->attachData($document['data'], $document['name']); $message->attachData($document['data'], $document['name']);