calc = $this->invoice->calc(); $this->fac = new Facturae($this->profile); $this->fac->setNumber('', $this->invoice->number); $this->fac->setIssueDate($this->invoice->date); $this->fac->setPrecision(Facturae::PRECISION_LINE); $this->buildBuyer() ->buildSeller() ->buildItems() ->setDiscount() ->setPoNumber() ->signDocument(); $disk = config('filesystems.default'); if (!Storage::disk($disk)->exists($this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()))) { Storage::makeDirectory($this->invoice->client->e_invoice_filepath($this->invoice->invitations->first())); } $this->fac->export(Storage::disk($disk)->path($this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()) . $this->invoice->getFileName("xsig"))); return $this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()) . $this->invoice->getFileName("xsig"); } private function setPoNumber(): self { if(strlen($this->invoice->po_number) > 1) { $this->fac->setReferences($this->invoice->po_number); } return $this; } private function setDiscount(): self { if($this->invoice->discount > 0) { $this->fac->addDiscount(ctrans('texts.discount'), $this->calc->getTotalDiscount()); } return $this; } private function buildItems(): self { foreach($this->invoice->line_items as $item) { $this->fac->addItem(new FacturaeItem([ 'name' => $item->product_key, 'description' => $item->notes, 'quantity' => $item->quantity, 'unitPrice' => $item->cost, 'discountsAndRebates' => $item->discount, 'charges' => [], 'discounts' => [], 'taxes' => $this->buildRatesForItem($item), // 'specialTaxableEvent' => FacturaeItem::SPECIAL_TAXABLE_EVENT_NON_SUBJECT, // 'specialTaxableEventReason' => '', // 'specialTaxableEventReasonDescription' => '', ])); } return $this; } private function buildRatesForItem(\stdClass $item): array { $data = []; if (strlen($item->tax_name1) > 1) { $data[$this->resolveTaxCode($item->tax_name1)] = $item->tax_rate1; } if (strlen($item->tax_name2) > 1) { $data[$this->resolveTaxCode($item->tax_name2)] = $item->tax_rate2; } if (strlen($item->tax_name3) > 1) { $data[$this->resolveTaxCode($item->tax_name3)] = $item->tax_rate3; } return $data; } private function resolveTaxCode(string $tax_name) { return match (strtoupper($tax_name)) { 'IVA' => Facturae::TAX_IVA, 'IPSI' => Facturae::TAX_IPSI, 'IGIC' => Facturae::TAX_IGIC, 'IRPF' => Facturae::TAX_IRPF, 'IRNR' => Facturae::TAX_IRNR, 'ISS' => Facturae::TAX_ISS, 'REIVA' => Facturae::TAX_REIVA, 'REIGIC' => Facturae::TAX_REIGIC, 'REIPSI' => Facturae::TAX_REIPSI, 'IPS' => Facturae::TAX_IPS, 'RLEA' => Facturae::TAX_RLEA, 'IVPEE' => Facturae::TAX_IVPEE, 'IPCNG' => Facturae::TAX_IPCNG, 'IACNG' => Facturae::TAX_IACNG, 'IDEC' => Facturae::TAX_IDEC, 'ILTCAC' => Facturae::TAX_ILTCAC, 'IGFEI' => Facturae::TAX_IGFEI, 'ISS' => Facturae::TAX_ISS, 'IMGSN' => Facturae::TAX_IMGSN, 'IMSN' => Facturae::TAX_IMSN, 'IMPN' => Facturae::TAX_IMPN, 'IIIMAB' => Facturae::TAX_IIIMAB, 'ICIO' => Facturae::TAX_ICIO, 'IECDPCAC' => Facturae::TAX_IECDPCAC, 'IGTECM' => Facturae::TAX_IGTECM, 'IE' => Facturae::TAX_IE, 'RA' => Facturae::TAX_RA, 'ITPAJD' => Facturae::TAX_ITPAJD, 'OTHER' => Facturae::TAX_OTHER, 'IMVDN' => Facturae::TAX_IMVDN, default => Facturae::TAX_IVA, }; } private function buildSeller(): self { $company = $this->invoice->company; $seller = new FacturaeParty([ "isLegalEntity" => true, // Se asume true si se omite "taxNumber" => $company->settings->vat_number, "name" => $company->present()->name(), "address" => $company->settings->address1, "postCode" => $company->settings->postal_code, "town" => $company->settings->city, "province" => $company->settings->state, "countryCode" => $company->country()->iso_3166_3, // Se asume España si se omite "book" => "0", // Libro "merchantRegister" => "RG", // Registro Mercantil "sheet" => "1", // Hoja "folio" => "2", // Folio "section" => "3", // Sección "volume" => "4", // Tomo "email" => $company->settings->email, "phone" => $company->settings->phone, "fax" => "", "website" => $company->settings->website, "contactPeople" => $company->owner()->present()->name(), // "cnoCnae" => "04647", // Clasif. Nacional de Act. Económicas // "ineTownCode" => "280796" // Cód. de municipio del INE ]); $this->fac->setSeller($seller); return $this; } private function buildBuyer(): self { $buyer = new FacturaeParty([ "isLegalEntity" => $this->invoice->client->has_valid_vat_number, "taxNumber" => $this->invoice->client->vat_number, "name" => $this->invoice->client->present()->name(), "firstSurname" => $this->invoice->client->present()->first_name(), "lastSurname" => $this->invoice->client->present()->last_name(), "address" => $this->invoice->client->address1, "postCode" => $this->invoice->client->postal_code, "town" => $this->invoice->client->city, "province" => $this->invoice->client->state, "countryCode" => $this->invoice->client->country->iso_3166_3, // Se asume España si se omite "email" => $this->invoice->client->present()->email(), "phone" => $this->invoice->client->present()->phone(), "fax" => "", "website" => substr($this->invoice->client->present()->website(), 0 ,60), "contactPeople" => $this->invoice->client->present()->first_name()." ".$this->invoice->client->present()->last_name(), // "cnoCnae" => "04791", // Clasif. Nacional de Act. Económicas // "ineTownCode" => "280796" // Cód. de municipio del INE ]); $this->fac->setBuyer($buyer); return $this; } private function signDocument(): self { // $ssl_cert = file_get_contents(base_path('e_invoice_cert.p12')); // $this->fac->sign($ssl_cert, null, "SuperSecretPassword"); return $this; } }