A simple hook can achieve this. Here's some inspiration: In this case, I had to derive the Opportunity number from the year and add a auto increasing number to it. And, it has to be a string, because of the "-" character in the number.
PHP Code:
$hook_array['before_save'][] = array(0, "_insert_quotenr", 'custom/modules/Opportunities/_insert_quotenr.php', '_insert_quotenr', 'generateQuoteNr');
PHP Code:
class _insert_quotenr {
function generateQuoteNr(& $bean, $event, $arguments) {
print_r($_REQUEST);
if (empty ($bean->name)) {
if ($_REQUEST['relate_to'] == "Project" AND !empty ($_REQUEST['relate_id'])) {
require_once 'modules/Project/Project.php';
$Project = new Project();
$Project->retrieve($_REQUEST['relate_id']);
// print_r($bean);exit;
// Fill in the Project
$bean->account_id = $Project->account_id_c;
// Fill in the Account
$bean->project_id_c = $_REQUEST['relate_id'];
// We need a new quote nr
$query = "SELECT SUBSTRING(name, 9, 4) AS name FROM opportunities WHERE name LIKE ('" .$Project->name. "%') ORDER BY name DESC";
//echo $query; exit;
$result = $bean->db->query($query);
$row = $bean->db->fetchByAssoc($result);
if (empty ($row['name'])) {
//We have a new year
$projnr = $Project->name . "-01";
} else {
///We have an existing year
$nr = sprintf("%02d", $row['name'] + 1);
$projnr = $Project->name . "-" . $nr;
}
echo $projnr;
$bean->name = $projnr;
} else {
die("foobar");
}
}
}
}
Bookmarks