jmaxb1
January 30, 2024, 4:13pm
1
Hello. I’m adding some functions triggers to Dolibarr with a module to optimize some actions.
Per example:
I made this trigger to works when a credit note is validated.
case 'BILL_VALIDATE':
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
global $langs, $conf, $hookmanager,$user,$db,$mysoc;
$thirdparty = new Societe($db);
$thirdparty->fetch($object->socid);
$paiement = new Paiement($db);
$paiement->datepaye = dol_now();
if($object->type == 2){
$facid = $object->id;
$monto = $object->total_ttc;
$idpaiment = 4;
$paiement->amounts = array($facid=>$monto);
$paiement->multicurrency_amounts = array($facid=>$monto);
$paiement->paiementid = $idpaiment;
$paiement->num_payment = $object->ref;
$paiement_id =$paiement->create($user, 1 , $thirdparty);
$sql = "UPDATE ".MAIN_DB_PREFIX."paiement_facture SET";
$sql.= " amount='".price2num($monto, 'MU')."'";
$sql.= " WHERE fk_paiement=".$paiement_id;
$db->query($sql);
$label = '(CustomerInvoicePayment)';
$accountid = $object->fk_account;
$chqemetteur = 'CASHDESKPANEL';
$chqbank = 'CASHDESKPANEL';
$result = $paiement->addPaymentToBank($user, 'payment', $label, $accountid ,$chqemetteur,$chqbank);
$object->setPaid($user);
}
break;
It works fine. But, there is a thing I can’t solve. When I use function create from instance Paiement.
I want to add the Ref of the Credit note to the record. So, if I go to the bank records it’s will be easy to filter and find that Credit note.
When I use this :
$paiement->num_payment = $object->ref;
I got inserted the value of the PROV not the value after validated the Credit note.
Any ideas?
Hmm, this reminds me of a bug that is hitting me
opened 03:21PM - 30 Aug 23 UTC
Bug
### Bug
The "processing hook" of the module seems to be too early:
* When an… invoice is validated, the checkbox "append file" in the email template does nothing. The notification seems to be generated before the final invoice is completely generated.
* The Reference still gives back the "PROV" number, not the final invoice number
* When a notification is triggered by a completed payment, the attached document is still the original, old invoice - not the new version with the payment remark.
* When a subscription triggers an automatic validated invoice, there is also no document attached. In this case, even the public download link placeholder is empty - so in this case no invoice at all can be sent to the customer automatically.
As a conclusion: The notification should be processed after all the things which had triggered the notification are completely processed. Or the notification should be in general delayed by a few seconds, so it can be assured that all the needed processes are completed before the notification is generated.
### Environment Version
18.0.0
### Environment OS
Ubuntu 22.04
### Environment Web server
NGINX
### Environment PHP
8.1
### Environment Database
MariaDB 11
### Environment URL(s)
_No response_
### Expected and actual behavior
See above. The notification should be generated after all underlying processes which are triggering the notification are completed, so that it can be assured that all variables, links and attached files are already up-to-date.
Maybe a solution is to add a delay of a few seconds before the notification task is triggered, so that it can be assured that all underlying variables and documents are processed and up-to-date before generating the notification.
### Steps to reproduce the behavior
Follow the procedure in the description and i.e. validate an invoice which sends a notification to the customer.
The reference is still the previois "PROV" number, the file is not attached (as the invoice is not yet finally generated when the notification is triggered).
### Attached files
_No response_
jmaxb1
January 31, 2024, 9:40am
3
There is another trigger BILL_PAYED. I’ve tried to do something like.
set credit note as paid. Then in BILL_PAYED trigger I added :
$paiement = new Paiement($db);
$paiement->datepaye = dol_now();
if($object->type == 2){
$facid = $object->id;
$monto = $object->total_ttc;
$idpaiment = 4;
$paiement->amounts = array($facid=>$monto);
$paiement->multicurrency_amounts = array($facid=>$monto);
$paiement->paiementid = $idpaiment;
$paiement->num_payment = $object->ref;
$paiement_id =$paiement->create($user, 1 , $thirdparty);
$sql = "UPDATE ".MAIN_DB_PREFIX."paiement_facture SET";
$sql.= " amount='".price2num($monto, 'MU')."'";
$sql.= " WHERE fk_paiement=".$paiement_id;
$db->query($sql);
$label = '(CustomerInvoicePayment)';
$accountid = $object->fk_account;
$chqemetteur = 'CASHDESKPANEL';
$chqbank = 'CASHDESKPANEL';
$result = $paiement->addPaymentToBank($user, 'payment', $label, $accountid ,$chqemetteur,$chqbank);
}
but without success. Have you tried using that trigger?