Customizing E-Invoices in Sales Pack

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yuri
    EspoCRM product developer
    • Mar 2014
    • 10024

    #1

    Customizing E-Invoices in Sales Pack

    1. Create a file custom/Espo/Custom/Binding.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);
        }
    }
    2. Create a file custom/Espo/Custom/EInvoice/MyPreparator.php:​

    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);
    
            // Below, we set more fields to the prepared E-Invoice.
            // Everything below is just an example.
    
            /**
             * We assume that your Invoice 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;
        }
    }​
    Last edited by yuri; Yesterday, 05:27 PM.
Working...