💬 General

ADD invoice no after status changed

👤 Laz61
Q
Question
Hi guys, the invoice number is auto added after the invoice is SAVED. i want to change this to status, when status is "pending" then i want to add the invoice no, and not directly after clicking the save button so the invoice stays like pre invoice after the status is changed from "created" to "pending" the invoice number needs to be added.

PLEASE HELP!

Thanks!
A
Community Solution
✓ Answered
I fixed it. allot sad that the official invoice number field (invoice_no) could not be updated or edited. and it's a uitype 4 increment field etc... but i made it work with the main invoice_no field!!!

increment invoice_no on status changed, so this works like a proforma invoice or pre invoice. YOU can make as much invoices you want, the invoice no will not be added. after trigger the invoice gets his number and the increment gets a +1 for the next trigger for invoice no.

I worked days for this code ( i am NOT a developer maybe thats why? :) ) so respect it and share your codes with the community!!!

if somebody needs it here is WHAT YOU NEED TO DO! follow the steps!!! :smile:

1 - CREATE one new file (registerworkflow.php) to register workflow in root of Vtiger CRM and add all these line in that file.

<?php
require_once 'include/utils/utils.php';
require 'modules/com_vtiger_workflow/VTEntityMethodManager.inc';

$emm = new VTEntityMethodManager($adb);

//$emm->addEntityMethod("Module Name","Label", "Path to file" , "Method Name" );
$emm->addEntityMethod("Invoice", "Genereer Factuurnr", "custom/wf/InvoiceHandler.php", "GenereerFactuurnr");
?>

2 - CREATE one new file {YOUR_VTIGER_ROOT FOLDER}/custom/wf/InvoiceHandler.php and add these lines

<?php
/////////////////LAZ61 vTiger Discussions/////////////////////////
function GenereerFactuurnr($entity){
$adb = PearDatabase::getInstance();
$wsId = $entity->getId();

$parts = explode('x', $wsId);
$entityId = $parts[1];

$check = $adb->pquery("select cur_id,prefix from vtiger_modentity_num where semodule=? and active = 1", array($module));
$prefix = $adb->query_result($check, 0, 'prefix');
$curid = $adb->query_result($check, 0, 'cur_id');

$prev_inv_no = $prefix . $curid;

$strip = strlen($curid) - strlen($curid + 1);
if ($strip < 0)
$strip = 0;
$temp = str_repeat("0", $strip);

$req_no.= $temp . ($curid + 1);
$check2 = $adb->pquery("SELECT * FROM vtiger_modentity_num WHERE num_id = '29' ORDER BY cur_id"); <<==CHANGE num_id= 'TO your ID, check database for the correct id no'
$curid2 = $adb->query_result($check2, 0, 'cur_id');
$adb->pquery("UPDATE vtiger_invoice SET invoice_no = '000' '$curid2' WHERE invoiceid = ?", Array($entityId));

$adb->pquery("UPDATE vtiger_modentity_num SET cur_id = cur_id +1 WHERE semodule = 'Invoice' and active = 1");
}
/////////////////LAZ61 vTiger Discussions/////////////////////////
?>

3 - RUN https://YOUR-VTIGER/registerworkflow.php from your browser JUST ONCE!

4 - MAKE a select option field in invoice module for the workflow trigger to get invoice_no and increment with +1

5 - ADD workflow to trigger the custom function on select option is selected

6 - OPEN FILE {YOUR_VTIGER_ROOT FOLDER}/data/CRMEntity.php

7 - GO TO LINE 1510 and find this CODE: $req_no.= $temp . ($curid + 1);

8 - AFTER THIS CODE $req_no.= $temp . ($curid + 1);

9 - ADD CODE

if ($module == 'Invoice') {
/////////////////LAZ61 vTiger Discussions/////////////////////////
} else {
$adb->pquery("UPDATE vtiger_modentity_num SET cur_id=? where cur_id=? and active=1 AND semodule=?", array($req_no, $curid, $module));
return decode_html($prev_inv_no);
}

10 - DELETE OR COMMENT OUT LINE 1512 -> //return decode_html($prev_inv_no);

11 - HAVE FUN!
← Back to General Join to answer →