local/modules/CreditNote/EventListener/CreditNoteListener.php line 28

  1. <?php
  2. /*************************************************************************************/
  3. /*      This file is part of the module CreditNote                                   */
  4. /*                                                                                   */
  5. /*      For the full copyright and license information, please view the LICENSE.txt  */
  6. /*      file that was distributed with this source code.                             */
  7. /*************************************************************************************/
  8. namespace CreditNote\EventListener;
  9. use CreditNote\CreditNote;
  10. use CreditNote\Event\CreditNoteEvents;
  11. use CreditNote\Event\PropelEvent;
  12. use CreditNote\Model\CreditNote as CreditNoteModel;
  13. use CreditNote\Model\Map\CreditNoteTableMap;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Thelia\Model\ConfigQuery;
  16. /**
  17.  * @author Gilles Bourgeat <gilles.bourgeat@gmail.com>
  18.  */
  19. class CreditNoteListener implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * Add credit note ref before save
  23.      * @param PropelEvent $event
  24.      */
  25.     public function generateCreditNoteInvoiceRef(PropelEvent $event)
  26.     {
  27.         /** @var CreditNoteModel $instance */
  28.         $instance $event->getInstance();
  29.         if ($instance->isColumnModified(CreditNoteTableMap::STATUS_ID)) {
  30.             if ($instance->getInvoiceRef() !== null && !$instance->getCreditNoteStatus()->getInvoiced()) {
  31.                 throw new \Exception('This credit note is already invoiced, you can not cancel it');
  32.             }
  33.             if ($instance->getCreditNoteStatus()->getInvoiced() && $instance->getInvoiceRef() === null) {
  34.                 if ((int) CreditNote::getConfigValue(CreditNote::CONFIG_KEY_INVOICE_REF_WITH_THELIA_ORDER)) {
  35.                     if (!class_exists('\InvoiceRef\EventListeners\OrderListener')) {
  36.                         throw new \Exception('Missing module InvoiceRef');
  37.                     }
  38.                     // dans le cas ou la facturation suit celle des commandes
  39.                     $invoiceRef ConfigQuery::create()
  40.                         ->findOneByName('invoiceRef');
  41.                     $value $invoiceRef->getValue();
  42.                     $instance->setInvoiceRef($value)
  43.                         ->setInvoiceDate(new \DateTime())
  44.                     ;
  45.                     $invoiceRef->setValue(++$value)
  46.                         ->save();
  47.                 } else {
  48.                     // cas ou la facturation suit sa propre règle
  49.                     $ref CreditNote::getConfigValue(CreditNote::CONFIG_KEY_INVOICE_REF_PREFIX) . str_pad(
  50.                             (int) CreditNote::getConfigValue(CreditNote::CONFIG_KEY_INVOICE_REF_INCREMENT1) + 1,
  51.                             CreditNote::getConfigValue(CreditNote::CONFIG_KEY_INVOICE_REF_MIN_LENGTH8),
  52.                             "0",
  53.                             STR_PAD_LEFT
  54.                         );
  55.                     $instance->setInvoiceRef($ref);
  56.                     $instance->setInvoiceDate(new \DateTime());
  57.                     CreditNote::setConfigValue(
  58.                         CreditNote::CONFIG_KEY_INVOICE_REF_INCREMENT,
  59.                         (int) CreditNote::getConfigValue(CreditNote::CONFIG_KEY_INVOICE_REF_INCREMENT1) + 1
  60.                     );
  61.                 }
  62.             }
  63.         }
  64.     }
  65.     /**
  66.      * Add credit note ref before save
  67.      * @param PropelEvent $event
  68.      */
  69.     public function generateCreditNoteRef(PropelEvent $event)
  70.     {
  71.         /** @var CreditNoteModel $instance */
  72.         $instance $event->getInstance();
  73.         if ($instance->getRef() === null) {
  74.             $ref CreditNote::getConfigValue(CreditNote::CONFIG_KEY_REF_PREFIX) . str_pad(
  75.                 (int) CreditNote::getConfigValue(CreditNote::CONFIG_KEY_REF_INCREMENT1) + 1,
  76.                 CreditNote::getConfigValue(CreditNote::CONFIG_KEY_REF_MIN_LENGTH8),
  77.                 "0",
  78.                 STR_PAD_LEFT
  79.             );
  80.             $instance->setRef($ref);
  81.             $instance->setInvoiceDate(new \DateTime());
  82.         }
  83.         $this->generateCreditNoteInvoiceRef($event);
  84.     }
  85.     public function incrementCreditNoteRef(PropelEvent $event)
  86.     {
  87.         CreditNote::setConfigValue(
  88.             CreditNote::CONFIG_KEY_REF_INCREMENT,
  89.             (int) CreditNote::getConfigValue(CreditNote::CONFIG_KEY_REF_INCREMENT1) + 1
  90.         );
  91.     }
  92.     public static function getSubscribedEvents()
  93.     {
  94.         return array(
  95.             CreditNoteEvents::preInsert(CreditNoteTableMap::TABLE_NAME)  => [
  96.                 'generateCreditNoteRef'128
  97.             ],
  98.             CreditNoteEvents::postInsert(CreditNoteTableMap::TABLE_NAME)  => [
  99.                 'incrementCreditNoteRef'128
  100.             ],
  101.             CreditNoteEvents::preUpdate(CreditNoteTableMap::TABLE_NAME)  => [
  102.                 'generateCreditNoteInvoiceRef'128
  103.             ]
  104.         );
  105.     }
  106. }