Results 1 to 6 of 6

Thread: Logic_hooks - Wrong calculate with cents.

  1. #1
    jorgeluiz is offline Sugar Community Member
    Join Date
    Apr 2008
    Location
    Brasil - Porto Alegre - RS
    Posts
    74

    Default Logic_hooks - Wrong calculate with cents.

    Hello,
    I use Logic_hooks to make a calculation:
    [Quantity] * [cost price] = [cost]
    [Quantity] * [sell price] = [sale]
    [cost] - [sale] = gross margin

    [5] * [200.00] = [1000.00]
    [5] * [300.00] = [1500.00]
    [1000.00] - [1500.00] = [500.00]

    My problem:

    When the price is full, with only 0.00, fine works.
    An error occurs when getting absurd with prices in cents.

    [5] * [200.01] = [1000.05]
    [5] * [300.00] = [1500.00]
    [1000.05] - [1500.00] = [-98505.00]

    Reviewed all the fields in mysql, formats semicolon, but not identified anything to size error.
    I found some posts here on the forum and decimal precision of rounding, but did not find what you need.
    Someone has encountered a problem like this?
    If someone has a suggestion, thanks a lot

    Sugar CE version 5.1a
    PHP Version 5.2.5
    MySQL Version: 5.0.45
    Windows Server 2003

  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: Logic_hooks - Wrong calculate with cents.

    Hi Jorge

    Use the format_number / unformat_number function into modules/Currencies/Currency.php
    This may fix these issues.

    Regards
    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
    jorgeluiz is offline Sugar Community Member
    Join Date
    Apr 2008
    Location
    Brasil - Porto Alegre - RS
    Posts
    74

    Default Re: Logic_hooks - Wrong calculate with cents.

    Olá André,
    Mais alguns detalhes:
    Pelo Admin, configurei o Sugar para usar o separador de milhar o ',' (ponto) e os decimais a ',' (vírgula) e estou usando duas casas decimais.
    Os campo de 'valor' são Currency e os de quantidade são Integer.
    No Mysql os campos 'valor' estão como decimal (26,6) e quantidade INT(11)

    Mas não sei exatamente o que deve ser alterado no arquivo Currency.php?

    PHP Code:
       function unformat_number($string) {
        static 
    $currency null;
        if(!isset(
    $currency)) {
            global 
    $current_user;
            
    $currency = new Currency();
            if(
    $current_user->getPreference('currency')) $currency->retrieve($current_user->getPreference('currency'));
            else 
    $currency->retrieve('-99'); // use default if none set
        
    }
        
        
    $seps get_number_seperators();
        
    // remove num_grp_sep and replace decimal seperater with decimal
        
    $string trim(str_replace(array($seps[0], $seps[1], $currency->symbol), array('''.'''), $string));
         
    preg_match('/[0-9\.]*/'$string$string);
        return 
    trim($string[0]); 
    Grande abraço e obrigado pela ajuda.

    Jorge
    Sugar CE version 5.1a
    PHP Version 5.2.5
    MySQL Version: 5.0.45
    Windows Server 2003

  4. #4
    jorgeluiz is offline Sugar Community Member
    Join Date
    Apr 2008
    Location
    Brasil - Porto Alegre - RS
    Posts
    74

    Default Re: Logic_hooks - Wrong calculate with cents.

    Hi André,
    sorry, but posted in Portuguese.

    A few more details:
    By Admin, set up the Sugar to use the tab of thousands o ',' (point) and the decimal ',' (comma) and I am using two decimal places.
    The field of 'value' are of the Currency and quantity are Integer.
    Mysql in the 'value' are as decimal (26,6) and quantity INT (11)

    But I do not know exactly what needs to be changed in Currency.php file?

    Jorge
    Sugar CE version 5.1a
    PHP Version 5.2.5
    MySQL Version: 5.0.45
    Windows Server 2003

  5. #5
    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: Logic_hooks - Wrong calculate with cents.

    Hi Jorge

    Don't you worry about that.
    Your Power Translator Tabajara seems pretty good!
    Actually you don't need to modify anything into Currency.php
    It contains the functions I mentioned previously. Both gets parameters which guide it to format/unformat numbers.
    You should create a simple php script to run outside Sugar which gets both values you are going to calculate and generate the result you are looking for.
    Make changes on this script and when you get the correct result you can add the formula into your logic hook.
    You may use the Currency.php functions to facility the work to you removing the formatting characters.

    Regards
    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.

  6. #6
    jorgeluiz is offline Sugar Community Member
    Join Date
    Apr 2008
    Location
    Brasil - Porto Alegre - RS
    Posts
    74

    Default Re: Logic_hooks - Wrong calculate with cents.

    André,
    Vai ter gente procurando no Google o Power Translation Tabajara pra baixar!!!
    Abraços

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 2011-12-25, 01:17 PM
  2. Statsgraph 1.0 for SugarCRM 4.5
    By tfrebault in forum Downloads
    Replies: 63
    Last Post: 2009-10-06, 03:15 AM
  3. Module with no SubPanels
    By dop3 in forum Developer Help
    Replies: 3
    Last Post: 2008-06-29, 06:24 PM
  4. PHP: calculate Fiscal Year; suggestions?
    By wivaku in forum Developer Help
    Replies: 5
    Last Post: 2007-06-20, 11:39 PM
  5. Time is wrong in "home" tab
    By emk in forum Help
    Replies: 4
    Last Post: 2006-01-20, 05:15 AM

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
  •