1. Create a file custom/Espo/Custom/Binding.php:
2. Create a file custom/Espo/Custom/EInvoice/MyPreparator.php:
Code:
<?php namespace Espo\Custom; use Espo\Core\Binding\BindingProcessor; use Espo\Core\Binding\Binder; use Espo\Custom\EInvoice\MyPreparator; use Espo\Modules\Sales\Tools\Invoice\EInvoice\Preparator; class Binding implements BindingProcessor { public function process(Binder $binder): void { $binder->bindImplementation(Preparator::class, MyPreparator::class); } }
Code:
<?php namespace Espo\Custom\EInvoice; use Einvoicing\Invoice as EInvoice; use Einvoicing\Payments\Payment; use Einvoicing\Payments\Card; use Espo\Modules\Sales\Entities\Invoice; use Espo\Modules\Sales\Tools\Invoice\EInvoice\Preparator; use Espo\Modules\Sales\Tools\Invoice\EInvoice\DefaultPreparator; class MyPreparator implements Preparator { public function __construct( private DefaultPreparator $defaultPreparator, ) {} public function prepare(Invoice $invoice, string $format): EInvoice { $eInvoice = $this->defaultPreparator->prepare($invoice, $format); /** * We assume that your Invoce entity type has the custom fields: * Payment Id, Payment MeansCode, etc. * * We use https://github.com/josemmo/einvoicing/ library. */ $payment = new Payment(); $payment->setId($invoice->get('cPaymentId')); $payment->setMeansCode($invoice->get('cPaymentMeansCode')); $payment->setMeansText($invoice->get('cPaymentMeansText')); $card = new Card(); $card->setHolder($invoice->get('cPaymentCardHolder')); $card->setNetwork($invoice->get('cPaymentCardNetwork')); if ($invoice->get('cPaymentCardPan')) { $card->setPan($invoice->get('cPaymentCardPan')); } $payment->setCard($card); $eInvoice->setPayment($payment); return $eInvoice; } }
Comment