Hi,
I have the following problem;
I've created a custom module called "Acc_Accounts"
Now I want my field "acc_number" to be automatically increased when a new account is entered.
I've read several posts on Sugar about a logic hook and now I found a solution which was shared on Sugar, but it still doesn't seems to work.
I've saved the files "logic_hook.php" and "add_code_hook.php" in custom\modules\Acc_Accounts
I hope someone can tell me what is wrong with my code?
Thanksss
logic_hook.php
add_code_hook.phpPHP Code:<?php
class Acc_Accounts_class
{
function Acc_Accounts_method(&$bean, $event, $arguments=null)
{
if ($event != 'after_save') return;
$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(1, 'add_code', 'custom/modules/Acc_Accounts/add_code_hook.php','add_code', 'add_code');
}
}
?>
PHP Code:<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class add_code {
const CODE_PREFIX = "";
const CODE_SUFFIX = "";
const CODE_SEPARATOR = "-";
const CODE_FIELD = "acc_number";
const CUSTOM_TABLE = "acc_accounts";
const ADD_DATE = "1";
const DATE_FORMAT = "y";
const ZERO_PADDING = 4; //minimum amount of characters desired for the number. ie 4 = 0001, 3 = 001
const FIRST_NUM = "0001"; //default to start with.
function add_code(&$bean, $event, $arguments)
{
require_once('log4php/LoggerManager.php');
//pattern to detect valid code, designed to detect non padded numbering too.
$pattern = "/^" . self::CODE_PREFIX . (self::ADD_DATE?"[0-9]{2}":"") . self::CODE_SEPARATOR . "([0-9]+)" . self::CODE_SUFFIX . "$/";
$GLOBALS['log']->debug("add_code function fired after save.");
if(!preg_match($pattern, $bean->fetched_row[self::CODE_FIELD])) {
$db = DBManagerFactory::getInstance();
$query = "select " . self::CODE_FIELD . " from " . self::CUSTOM_TABLE .
" where (" . self::CODE_FIELD . " <> '' or " . self::CODE_FIELD . " is not null) order by right(" . self::CODE_FIELD . ", 4) desc limit 1";
$result = $db->query($query, true, 'Error selecting most recent ' . self::CODE_FIELD . ' CODE');
if ($row=$db->fetchByAssoc($result)) {
$last_code = $row[self::CODE_FIELD];
} else {
//no codes exist, generate default - PREFIX CURRENT_YEAR SEPARATOR FIRST_NUM
$last_code = self::CODE_PREFIX . (self::ADD_DATE?date(self::DATE_FORMAT):"") . self::CODE_SEPARATOR . self::FIRST_NUM . self::SUFFIX;
}
$GLOBALS['log']->debug("Last Code: " . $last_code);
preg_match($pattern, $last_code, $matches);
$num=$matches[1];
$num++;
$pads = self::ZERO_PADDING - strlen($num);
$new_code = self::CODE_PREFIX . (self::ADD_DATE?date(self::DATE_FORMAT):"") . self::CODE_SEPARATOR;
//preform the lead padding
for($i=0; $i < $pads; $i++) {
$new_code .= "0";
}
$new_code .= $num . self::CODE_SUFFIX;
$GLOBALS['log']->debug("New Code: " . $last_code);
//write to database
$query = "INSERT INTO " . self::CUSTOM_TABLE . " (id_c, " . self::CODE_FIELD . ") VALUES ('$bean->id', '$new_code') ON DUPLICATE KEY UPDATE " . self::CODE_FIELD . "='$new_code'";
$result = $db->query($query, true, 'Error adding ' . self::CODE_PREFIX . ' code');
} else {
$GLOBALS['log']->debug("Code Exists...Exiting " . $bean->fetched_row[" . self::CODE_FIELD . "]);
}
}
}
?>


LinkBack URL
About LinkBacks



Reply With Quote



Bookmarks