1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Jobs/Invoice/CreateXInvoice.php

273 lines
11 KiB
PHP
Raw Normal View History

2023-03-12 12:13:59 +01:00
<?php
namespace App\Jobs\Invoice;
use App\Models\Invoice;
2023-04-03 21:00:47 +02:00
use App\Models\Country;
2023-03-12 12:13:59 +01:00
use horstoeko\zugferd\ZugferdDocumentBuilder;
use horstoeko\zugferd\ZugferdDocumentPdfBuilder;
use horstoeko\zugferd\ZugferdProfiles;
2023-03-12 12:58:48 +01:00
use Illuminate\Bus\Queueable;
2023-03-12 12:46:10 +01:00
use Illuminate\Contracts\Queue\ShouldQueue;
2023-03-12 12:58:48 +01:00
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
2023-03-12 12:13:59 +01:00
class CreateXInvoice implements ShouldQueue
2023-03-12 12:13:59 +01:00
{
2023-03-12 12:58:48 +01:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2023-03-12 12:46:10 +01:00
public Invoice $invoice;
2023-03-12 12:13:59 +01:00
2023-04-04 09:18:22 +02:00
public function __construct(Invoice $invoice, bool $alterPDF, string $custompdfpath = "")
2023-03-12 12:13:59 +01:00
{
$this->invoice = $invoice;
2023-04-04 08:58:01 +02:00
$this->alterpdf = $alterPDF;
2023-04-04 09:18:22 +02:00
$this->custompdfpath = $custompdfpath;
2023-03-12 12:13:59 +01:00
}
/**
* Execute the job.
*
*
* @return string
2023-03-12 12:13:59 +01:00
*/
public function handle(): string
2023-03-12 12:13:59 +01:00
{
$invoice = $this->invoice;
$company = $invoice->company;
$client = $invoice->client;
$profile = "";
switch ($company->xinvoice_type){
case "EN16931":
$profile = ZugferdProfiles::PROFILE_EN16931;
break;
case "XInvoice_2_2":
$profile = ZugferdProfiles::PROFILE_XRECHNUNG_2_2;
break;
case "XInvoice_2_1":
$profile = ZugferdProfiles::PROFILE_XRECHNUNG_2_1;
break;
case "XInvoice_2_0":
$profile = ZugferdProfiles::PROFILE_XRECHNUNG_2;
break;
case "XInvoice_1_0":
$profile = ZugferdProfiles::PROFILE_XRECHNUNG;
break;
case "XInvoice-Extended":
$profile = ZugferdProfiles::PROFILE_EXTENDED;
break;
case "XInvoice-BasicWL":
$profile = ZugferdProfiles::PROFILE_BASICWL;
break;
case "XInvoice-Basic":
$profile = ZugferdProfiles::PROFILE_BASIC;
break;
}
$xrechnung = ZugferdDocumentBuilder::CreateNew($profile);
2023-03-12 12:13:59 +01:00
$xrechnung
->setDocumentInformation($invoice->number, "380", date_create($invoice->date), $invoice->client->getCurrencyCode())
->setDocumentSupplyChainEvent(date_create($invoice->date))
2023-04-03 21:00:47 +02:00
->setDocumentSeller($company->getSetting('name'))
2023-04-04 11:46:51 +02:00
->setDocumentSellerAddress($company->getSetting("address1"), "", "", $company->getSetting("postal_code"), $company->getSetting("city"), $company->country()->iso_3166_2)
2023-03-12 12:13:59 +01:00
->setDocumentBuyer($client->name, $client->number)
2023-04-04 11:46:51 +02:00
->setDocumentBuyerAddress($client->address1, "", "", $client->postal_code, $client->city, $client->country->iso_3166_2)
->setDocumentBuyerReference($client->leitweg_id)
2023-04-04 11:46:51 +02:00
->setDocumentBuyerContact($client->primary_contact()->first()->first_name." ".$client->primary_contact()->first()->last_name, "", $client->primary_contact()->first()->phone, "", $client->primary_contact()->first()->email)
->addDocumentPaymentTerm(ctrans("texts.xinvoice_payable", ['payeddue' => date_create($invoice->date)->diff(date_create($invoice->due_date))->format("%d"), 'paydate' => $invoice->due_date]));
if (!empty($invoice->public_notes)){
$xrechnung->addDocumentNote($invoice->public_notes);
}
if(!empty($invoice->po_number)){
$xrechnung->setDocumentBuyerOrderReferencedDocument($invoice->po_number);
}
2023-03-12 12:13:59 +01:00
2023-04-03 21:00:47 +02:00
if (str_contains($company->getSetting('vat_number'), "/")){
$xrechnung->addDocumentSellerTaxRegistration("FC", $company->getSetting('vat_number'));
2023-03-12 12:58:48 +01:00
}
else {
2023-04-03 21:00:47 +02:00
$xrechnung->addDocumentSellerTaxRegistration("VA", $company->getSetting('vat_number'));
2023-03-12 12:58:48 +01:00
}
$invoicingdata = $invoice->calc();
//Create line items and calculate taxes
$taxtype1 = "";
switch ($company->tax_type1){
case "ZeroRate":
$taxtype1 = "Z";
break;
case "Tax Exempt":
$taxtype1 = "E";
break;
case "Reversal of tax liabilty":
$taxtype1 = "AE";
break;
case "intra-community delivery":
$taxtype1 = "K";
break;
case "Out of EU":
$taxtype1 = "G";
break;
case "Outside the tax scope":
$taxtype1 = "O";
break;
case "Canary Islands":
$taxtype1 = "L";
break;
case "Ceuta / Melila":
$taxtype1 = "M";
break;
2023-03-14 21:41:43 +01:00
default:
$taxtype1 = "S";
break;
}
$taxtype2 = "";
switch ($company->tax_type2){
case "ZeroRate":
$taxtype2 = "Z";
break;
case "Tax Exempt":
$taxtype2 = "E";
break;
case "Reversal of tax liabilty":
$taxtype2 = "AE";
break;
case "intra-community delivery":
$taxtype2 = "K";
break;
case "Out of EU":
$taxtype2 = "G";
break;
case "Outside the tax scope":
$taxtype2 = "O";
break;
case "Canary Islands":
$taxtype2 = "L";
break;
case "Ceuta / Melila":
$taxtype2 = "M";
break;
2023-03-14 21:41:43 +01:00
default:
$taxtype2 = "S";
break;
}
$taxtype3 = "";
switch ($company->tax_type3){
case "ZeroRate":
$taxtype3 = "Z";
break;
case "Tax Exempt":
$taxtype3 = "E";
break;
case "Reversal of tax liabilty":
$taxtype3 = "AE";
break;
case "intra-community delivery":
$taxtype3 = "K";
break;
case "Out of EU":
$taxtype3 = "G";
break;
case "Outside the tax scope":
$taxtype3 = "O";
break;
case "Canary Islands":
$taxtype3 = "L";
break;
case "Ceuta / Melila":
$taxtype3 = "M";
break;
2023-03-14 21:41:43 +01:00
default:
$taxtype3 = "S";
break;
}
2023-03-12 12:45:04 +01:00
foreach ($invoice->line_items as $index => $item){
$xrechnung->addNewPosition($index)
2023-03-12 12:58:48 +01:00
->setDocumentPositionProductDetails($item->notes)
2023-03-12 12:45:04 +01:00
->setDocumentPositionGrossPrice($item->gross_line_total)
2023-03-12 12:58:48 +01:00
->setDocumentPositionNetPrice($item->line_total);
if (isset($item->task_id)){
$xrechnung->setDocumentPositionQuantity($item->quantity, "HUR");
}
else{
$xrechnung->setDocumentPositionQuantity($item->quantity, "H87");
}
2023-03-14 21:26:08 +01:00
// According to european law, each artical can only have one tax percentage
if (empty($item->tax_name1) && empty($item->tax_name2) && empty($item->tax_name3)){
if (!empty($invoice->tax_name1)){
$xrechnung->addDocumentPositionTax($taxtype1, 'VAT', $invoice->tax_rate1);
2023-03-14 21:26:08 +01:00
}
elseif (!empty($invoice->tax_name2)){
$xrechnung->addDocumentPositionTax($taxtype2, 'VAT', $invoice->tax_rate2);
2023-03-14 21:26:08 +01:00
}
elseif (!empty($invoice->tax_name3)){
$xrechnung->addDocumentPositionTax($taxtype3, 'VAT', $invoice->tax_rate3);
2023-03-14 21:26:08 +01:00
}
else{
nlog("Can't add correct tax position");
}
}
else {
if ($item->tax_name1 != "" && $item->tax_name2 == "" && $item->tax_name3 == ""){
$xrechnung->addDocumentPositionTax($taxtype1, 'VAT', $item->tax_rate1);
2023-03-14 21:26:08 +01:00
}
elseif ($item->tax_name1 == "" && $item->tax_name2 != "" && $item->tax_name3 == ""){
$xrechnung->addDocumentPositionTax($taxtype2, 'VAT', $item->tax_rate2);
2023-03-14 21:26:08 +01:00
}
elseif ($item->tax_name1 == "" && $item->tax_name2 == "" && $item->tax_name3 != ""){
$xrechnung->addDocumentPositionTax($taxtype3, 'VAT', $item->tax_rate3);
2023-03-14 21:26:08 +01:00
}
}
2023-03-12 12:45:04 +01:00
}
if ($invoice->isPartial()){
$xrechnung->setDocumentSummation($invoice->amount, $invoice->amount-$invoice->balance, $invoicingdata->getSubTotal(), $invoicingdata->getTotalSurcharges(), $invoicingdata->getTotalDiscount(), $invoicingdata->getSubTotal(), $invoicingdata->getItemTotalTaxes(), null, $invoice->partial);
} else {
$xrechnung->setDocumentSummation($invoice->amount, $invoice->amount-$invoice->balance, $invoicingdata->getSubTotal(), $invoicingdata->getTotalSurcharges(), $invoicingdata->getTotalDiscount(), $invoicingdata->getSubTotal(), $invoicingdata->getItemTotalTaxes(), null, 0.0);
2023-03-12 12:45:04 +01:00
}
if (count($invoicingdata->getTaxMap()) > 0){
$tax = explode(" ", $invoicingdata->getTaxMap()[0]["name"]);
$xrechnung->addDocumentTax($taxtype1, "VAT", $invoicingdata->getTaxMap()[0]["total"]/(explode("%", end($tax))[0]/100), $invoicingdata->getTaxMap()[0]["total"], explode("%", end($tax))[0]);
2023-03-12 12:45:04 +01:00
}
if (count($invoicingdata->getTaxMap()) > 1) {
$tax = explode(" ", $invoicingdata->getTaxMap()[1]["name"]);
$xrechnung->addDocumentTax($taxtype2, "VAT", $invoicingdata->getTaxMap()[1]["total"]/(explode("%", end($tax))[0]/100), $invoicingdata->getTaxMap()[1]["total"], explode("%", end($tax))[0]);
2023-03-12 12:45:04 +01:00
}
if (count($invoicingdata->getTaxMap()) > 2) {
$tax = explode(" ", $invoicingdata->getTaxMap()[2]["name"]);
$xrechnung->addDocumentTax($taxtype3, "VAT", $invoicingdata->getTaxMap()[2]["total"]/(explode("%", end($tax))[0]/100), $invoicingdata->getTaxMap()[2]["total"], explode("%", end($tax))[0]);
2023-03-12 12:45:04 +01:00
}
$disk = config('filesystems.default');
2023-04-03 21:00:47 +02:00
if(!Storage::exists($client->xinvoice_filepath($invoice->invitations->first()))){
Storage::makeDirectory($client->xinvoice_filepath($invoice->invitations->first()));
}
$xrechnung->writeFile(Storage::disk($disk)->path($client->xinvoice_filepath($invoice->invitations->first()) . $invoice->getFileName("xml")));
2023-04-04 08:58:01 +02:00
if ($this->alterpdf){
2023-04-04 09:18:22 +02:00
if ($this->custompdfpath != ""){
$pdfBuilder = new ZugferdDocumentPdfBuilder($xrechnung, $this->custompdfpath);
2023-04-04 08:58:01 +02:00
$pdfBuilder->generateDocument();
2023-04-04 09:18:22 +02:00
$pdfBuilder->saveDocument($this->custompdfpath);
}
else {
$filepath_pdf = $client->invoice_filepath($invoice->invitations->first()).$invoice->getFileName();
$file = Storage::disk($disk)->exists($filepath_pdf);
if ($file) {
$pdfBuilder = new ZugferdDocumentPdfBuilder($xrechnung, Storage::disk($disk)->path($filepath_pdf));
$pdfBuilder->generateDocument();
$pdfBuilder->saveDocument(Storage::disk($disk)->path($filepath_pdf));
}
2023-04-04 08:58:01 +02:00
}
}
2023-04-03 21:00:47 +02:00
return $client->invoice_filepath($invoice->invitations->first()).$invoice->getFileName("xml");
2023-03-12 12:13:59 +01:00
}
}