Catching event before PDF invoice generation and email sent

Discussion in 'Customization & add-ons' started by valentinuzzi, Oct 17, 2012.

  1. valentinuzzi

    valentinuzzi aMember Pro Customer

    Joined:
    Oct 22, 2010
    Messages:
    13
    Hi all :)

    I've a personal plugin to implement the Italian numbering of invoices that works perfectly, after payment the invoice has the correct number.

    The problem is that the number is set by this code:

    PHP:
    class Am_Plugin_Ivaitaliana extends Am_Plugin
    {
     
        public function 
    isConfigured()
        {
            return (bool) 
    $this->getDi()->config->get('iva');
        }
       
        public function 
    onPaymentAfterInsert (Am_Event $event){
           
            
    $config Am_Di::getInstance()->config;
            
    $anno=$config->get('anno_fiscale');
            
    $anno_corrente=date(Y);
            
    $numero=0;
            if (
    $anno_corrente != $anno) {
            
    $numero=1;
            
    $config->saveValue(anno_fiscale,$anno_corrente);   
            }
            
    $config2 Am_Di::getInstance()->config;
            if (
    $numero !=1){
            
    $numero=$config2->get('ricevuta_next');
            }
            
    Am_Di::getInstance()->db->query("UPDATE am_invoice SET num_ricevuta=$numero WHERE num_ricevuta='-1'");
            
    $numero=$numero+1;   
            
    $config2->saveValue(ricevuta_next,$numero);     
        }
       
     
    }
    which catches the event onPaymentAfterInsert. The online version of the invoice is correct, but the emailed one has an incorrect number because this cose is probably running soon after the email was sent with the invoice generated.

    Could someone help me find another event to catch to be sure this code is running also before the invoice is generated to be emailed?

    Thanks a lot :)
  2. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    Do you need to generate invoice numbers successful payments only? In this case, I believe we must add "onPaymentBeforeInsert" hook.

    If that is another case, please explain.
  3. valentinuzzi

    valentinuzzi aMember Pro Customer

    Joined:
    Oct 22, 2010
    Messages:
    13
    Yes, only successful payments, either Paypal or Offline or any other one :) I'm trying changing "onPaymentAfterInsert" with "onPaymentBeforeInsert", I will let you know.
  4. valentinuzzi

    valentinuzzi aMember Pro Customer

    Joined:
    Oct 22, 2010
    Messages:
    13
    Unfortunately the solution of changing "onPaymentAfterInsert" with "onPaymentBeforeInsert" is not working. This is caused by the way I decided to implement the numbering.

    I added the column "num_ricevuta" in the datatbase (table am_invoice) with the default value "-1" and I change it to the correct number on the basis of the stored value "ricevuta_next", then the Invoice.php file generates the correct number reading that column.

    I think that the event "onPaymentBeforeInsert" comes too earlier, the line in the am_invoice table is not already there, and the plugin doesn't work.

    The event to catch must be before sending the email with invoice to the admin but after the payment is registered in am_invoice, otherwise I will have to change strategy.

    Thanks a lot Alex. ;)
  5. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    Of course it does not work as there is no such hook yet. But it is easy to define :)

    Edit file amember/application/default/models/InvoicePayment.php
    find line
    PHP:
    parent::insert($reload);
    and right before this line insert:
    PHP:
            $this->getDi()->hook->call(new Am_Event('paymentBeforeInsert',
                array(
    'payment' => $this,
                      
    'invoice' => $this->getInvoice(),
                      
    'user'    => $this->getInvoice()->getUser())));
    We will include it into next release, so it is still safe to upgrade.
  6. valentinuzzi

    valentinuzzi aMember Pro Customer

    Joined:
    Oct 22, 2010
    Messages:
    13
    :) ah ah, thanks a lot...

    Unfortunately it is not enough, with this change my plugin doesn't work at all, even the online invoice is incorrect.

    Here the complete plugin:
    PHP:
    class Am_Form_Setup_Ivaitaliana extends Am_Form_Setup
    {
        public function 
    __construct()
        {
            
    parent::__construct('ivaitaliana_setup');
            
    $this->setTitle("Iva Italiana");
            
    $this->setComment('Qui aggiungi un tuo commento...');
        }
        public function 
    initElements()
        {
            
    $config Am_Di::getInstance()->config;
            if (!
    $config->get('ricevuta_next')){
            
    $config->saveValue(ricevuta_next,"1");
            }
            if (!
    $config->get('anno_fiscale')){
            
    $config->saveValue(anno_fiscale,date(Y));
            }
                   
            
    $f1 $this->addFieldset()->setLabel('Impostazione percentuale di IVA');
            
    $f1->addText("iva",array('size'=>5))->setLabel(array("Inserisci IVA senza %"));
         
           
            
    $f2 $this->addFieldset()->setLabel('Impostazione del prossimo numero di ricevuta');
            
    $f2->addText("ricevuta_next",array('size'=>5))->setLabel(array("La prossima ricevuta avrà numero:"));       
                   
            
    $this->addScript()->setScript(<<<CUT
    var nuovo=0;
    var vecchio=$('input[name="ricevuta_next"]').val();
    $('input[name="ricevuta_next"]').change(function(){
    nuovo=$('input[name="ricevuta_next"]').val();   
    });
    $('#save-0').click(function(e){
    if (parseInt(nuovo)<parseInt(vecchio) && nuovo!=0){   
    if(confirm("Stai inserendo il valore "+nuovo+"  che è più piccolo di quello attuale. Sei proprio sicuro?")){
    return true;
    } else {
    e.preventDefault();
    return false;
    }
    }
    if (parseInt(nuovo)>parseInt(vecchio)){
    if(confirm("Hai impostato il valore "+nuovo+" come prossimo numero di ricevuta, confermi?")){
    return true;
    }else{
    e.preventDefault();
    return false;
    }
    }
    });
    CUT
            );
                   
           
        }
     
        
    //public function afterSaveConfig(Am_Config $before, Am_Config $after)
          //  {
          //    print_rre($after);
          // }
     
       
    }
     
     
    class 
    Am_Plugin_Ivaitaliana extends Am_Plugin
    {
     
        public function 
    isConfigured()
        {
            return (bool) 
    $this->getDi()->config->get('iva');
        }
       
        public function 
    onPaymentAfterInsert (Am_Event $event){
           
            
    $config Am_Di::getInstance()->config;
            
    $anno=$config->get('anno_fiscale');
            
    $anno_corrente=date(Y);
            
    $numero=0;
            if (
    $anno_corrente != $anno) {
            
    $numero=1;
            
    $config->saveValue(anno_fiscale,$anno_corrente);   
            }
            
    $config2 Am_Di::getInstance()->config;
            if (
    $numero !=1){
            
    $numero=$config2->get('ricevuta_next');
            }
            
    Am_Di::getInstance()->db->query("UPDATE am_invoice SET num_ricevuta=$numero WHERE num_ricevuta='-1'");
            
    $numero=$numero+1;   
            
    $config2->saveValue(ricevuta_next,$numero);     
        }
       
     
    }
    I made the change in the InvoicePayment.php as you said and changed the

    public function onPaymentAfterInsert (Am_Event $event)

    either to

    public function onPaymentBeforeInsert (Am_Event $event)

    and

    public function paymentBeforeInsert (Am_Event $event)

    but none of them is working :(
  7. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    It will be lot easier to resolve if you contact us via helpdesk with FTP info. If that is impossible, pack your aMember folder (without application/configs/config.php file) and send to ticket.

Share This Page