Facture triggers

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 :frowning:

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?