Results 1 to 4 of 4

Thread: Let auto_increment (logic_hook) start over whenever a new Project_Call is created ??

  1. #1
    dekleinemedia is offline A Prolific Poster
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    241

    Exclamation Let auto_increment (logic_hook) start over whenever a new Project_Call is created ??

    Hi, I have the following issue;

    I have customized my Project Module with some custom fields. Now my Project Module has a subpanel Calls.
    In Calls (custom/modules/Calls) i have created a logic_hook which has the function to auto-increase whenever a new Call is created.
    The problem is;
    When I have created a new Project, and i want to create a new Call through subpanel the auto-increment works fine. First Call gets #1, Second Call gets #2 etc..

    BUT when I create a new Project (Project #2) and I create a new Call through the subpanel, the First Call gets #3. The auto-increment of logic_hook has continued. How can i make this so, that whenever I create a new Project, and i create a new Call by subpanel the logic_hook starts over again with #1 ??

    I have added my add_code_hook file and a screenshot of the situation.

    Please help me and tell me how I can fix this Thanks in advance

    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 "index_number_c";
        const 
    CUSTOM_TABLE "calls_cstm";
        const 
    ADD_DATE "0";
        const 
    DATE_FORMAT "y";
        const 
    ZERO_PADDING 1//minimum amount of characters desired for the number. ie 4 = 0001, 3 = 001
        
    const FIRST_NUM "1"//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 ".self::CODE_FIELD." desc limit 1";  
        
                
    $result $db->query($querytrue'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($querytrue'Error adding ' self::CODE_PREFIX ' code');
            } else {
                
    $GLOBALS['log']->debug("Code Exists...Exiting " $bean->fetched_row[" . self::CODE_FIELD . "]);        
            }
        }
    }

    ?>
    Attached Images Attached Images  
    Kind regards,

    De Kleine Media


    SugarCRM CE v.5.2.0h
    Windows platform
    MySQL v.5.1
    phpMyAdmin - 2.11.2.2
    Apache Server v.2.0

  2. #2
    andopes's Avatar
    andopes is offline A Sugar Hero | Help Forum Moderator
    Join Date
    Jul 2006
    Location
    São Paulo - Brazil
    Posts
    8,335

    Default Re: Let auto_increment (logic_hook) start over whenever a new Project_Call is created

    You need to modify the add_code_hook to get the latest auto increment number based on related project.
    For example, you can count how many calls is related to the related project and then set the auto increment field as the total count plus one.

    Cheers
    André Lopes
    DevToolKit / Project of the Month - June 2009
    Lampada Global Services- Open Source Solutions
    Avenida Ipiranga, 318
    Bloco B - CJ 1602
    São Paulo, SP 01046-010
    Brazil
    Office: +55 11 3237-3110
    Mobile: +55 11 7636-5859
    e-mail: andre@lampadaglobal.com

    Lampada Global delivers offshore software development and support services to customers around the world.
    Lampada is proud to be a SugarCRM Gold Partner, revolutionizing Customer Relationship Management.

    I DO NOT answer questions through PM and Email. If you need some help post your question into SugarForum.

  3. #3
    SugarDev.net is offline Sugar Community Member
    Join Date
    Feb 2008
    Posts
    1,401

    Default Re: Let auto_increment (logic_hook) start over whenever a new Project_Call is created

    try

    PHP Code:
    $query =  " select " self::CODE_FIELD " from " self::CUSTOM_TABLE .  
                       
    " where (" self::CODE_FIELD " <> '' or " self::CODE_FIELD " is not null) AND parent_id = '".$bean->parent_id."' order by ".self::CODE_FIELD." desc limit 1"
    Developers go here
    Businesses go there (Dutch)

    Modules:
    SugarDev.net Developer Tools | Config | Dutch Language Pack
    "Nothing gets fixed unless there is a bug"

  4. #4
    dekleinemedia is offline A Prolific Poster
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    241

    Default Re: Let auto_increment (logic_hook) start over whenever a new Project_Call is created

    Hi, first thanks for the help André and SugarDev!

    When i try to change SugarDev's code in my add_code_hook.php (Calls) after repair i want to create a new Call in a Project, i get this message;

    Error selecting most recent index_number_c CODE Query Failed: select index_number_c from calls_cstm where (index_number_c <> '' or index_number_c is not null) AND parent_id = '' order by index_number_c desc limit 1::MySQL error 1054: Unknown column 'parent_id' in 'where clause'

    There is no parent_id in calls_cstm only in calls column. How to solve this? Thankss
    Last edited by dekleinemedia; 2009-07-22 at 11:58 AM.
    Kind regards,

    De Kleine Media


    SugarCRM CE v.5.2.0h
    Windows platform
    MySQL v.5.1
    phpMyAdmin - 2.11.2.2
    Apache Server v.2.0

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 2009-04-08, 10:13 PM
  2. Replies: 2
    Last Post: 2009-04-06, 11:48 AM
  3. The value of a checkbox for logic_hook
    By JVWay in forum Developer Help
    Replies: 2
    Last Post: 2008-06-25, 04:33 AM
  4. 1st logic_hook ....help please!
    By Iggby in forum Developer Help
    Replies: 5
    Last Post: 2008-02-19, 07:18 PM
  5. Ayuda con logic_hook
    By fedepia in forum Español
    Replies: 5
    Last Post: 2007-09-14, 05:14 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •