Customizing E-Invoices in Sales Pack

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yuri
    Member
    • Mar 2014
    • 8415

    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);
    
            /**
             * 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;
        }
    }​
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.
  • hom
    Junior Member
    • Nov 2017
    • 28

    #2
    Hello Yuri,

    Thank you very much for the instructions!

    I tried it out straight away and it worked well.
    I noticed two things:

    1) When adding a Transfer section, you have to make sure to use the add function for transfers.

    2) The Mandate section is not fully defined in the parent library. You have included the link to the UBL generator in your instructions. There you can see that only two values are queried for mandates (Mandate reference id and Debited account id). In order to fulfill at least the German xRechnung standard, the field (Bank assigned creditor id) must still be included. As far as I can see, this should be added to the josemmo library. Do you see it the same way or am I making a mistake? If it needs to be added there, I can write to josemmo directly.

    May others find it helpful. Here is my MyPreparator.php with working transfer fields. With these fields and the fields you have already implemented, an XML invoice can be generated that also satisfies an external check in the current version.

    Code:
    <?php
    namespace Espo\Custom\EInvoice;
    
    use Einvoicing\Invoice as EInvoice;
    use Einvoicing\Payments\Payment;
    use Einvoicing\Payments\Transfer;
    
    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'));
            
            $transfer = new Transfer();        
            $transfer->setAccountName($invoice->get('cPaymentAccountName'));
            $transfer->setAccountID($invoice->get('cPaymentAccountID'));
            $transfer->setProvider($invoice->get('cPaymentServiceProviderID'));
    
           $payment->addTransfer($transfer);
           $eInvoice->setPayment($payment);
    
           return $eInvoice;
        }
    }
    Thank you very much!

    Comment

    • yuri
      Member
      • Mar 2014
      • 8415

      #3
      Isn't it already available in the library?


      If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

      Comment

      Working...