Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Calculated Sum of 2 fields

  1. #1
    kinshibuya's Avatar
    kinshibuya is offline A Sugar Hero
    Join Date
    Jul 2008
    Location
    brasil
    Posts
    521

    Default Calculated Sum of 2 fields before save

    Hi!
    i''m using sugar crm CE 5,0,f and i would like to know how can i make a sum of fields
    for example. i have the fields amountfb1_c and amountfb2_c on Opportunities module
    how to make the field amount automaticaly calculate the sum of amountfb1_c + amoutfb2_c on the detail view?

    any ideas???

    I looked to the thead:

    http://www.sugarcrm.com/forums/showt...&highlight=sum

    but if for 4.5 version, there are some files i couldn't find on 5.0.0f
    Last edited by kinshibuya; 2008-09-08 at 09:32 PM.

  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: Calculated Sum of 2 fields

    Hi kinshibuya

    What do you need to do?

    1. Create the file custom/modules/Opportunities/logic_hooks.php
    PHP Code:
    $hook_version 1;
    $hook_array = Array();

    $hook_array['before_save'] = Array();
    $hook_array['before_save'][] = Array(1'calculateField''modules/Opportunities/OpportunityLogicHook.php','OpportunityLogicHook''calculateField'); 
    2. Create the file modules/Opportunities/OpportunityLogicHook.php
    PHP Code:
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    class 
    OpportunityLogicHook {
        function 
    calculateField(&$focus$event$arguments) {
            
    $focus->amount $focus->amountfb1_c $focus->amoutfb2_c;
        }

    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
    kinshibuya's Avatar
    kinshibuya is offline A Sugar Hero
    Join Date
    Jul 2008
    Location
    brasil
    Posts
    521

    Default Re: Calculated Sum of 2 fields

    Oi orbigado por responder

    teria uma maneira de fazer com que a soma apareça no campo Amount antes de salvar?
    colocando o valor 1 e depois o valor 2 ele some os 2 automaticamente a apareça no campo amount antes de salvar.

  4. #4
    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: Calculated Sum of 2 fields

    Oi kinshibuya

    Basta criar o logic_hook after_retrieve no mesmo módulo apontando para a mesma função:

    PHP Code:
    $hook_array['after_retrieve'] = Array();
    $hook_array['after_retrieve'][] = Array(1'calculateField''modules/Opportunities/OpportunityLogicHook.php','OpportunityLogicHook''calculateField'); 
    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.

  5. #5
    kinshibuya's Avatar
    kinshibuya is offline A Sugar Hero
    Join Date
    Jul 2008
    Location
    brasil
    Posts
    521

    Default Re: Calculated Sum of 2 fields

    Hi.
    having a problem on edit view the field apears ok, burt on list view the amount field doen't apear at all.

  6. #6
    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: Calculated Sum of 2 fields

    Hi kinshibuya

    For ListView you need to define the logic_hook process_record:

    It may call the same LogicHook function in before_save and after_retrieve.
    PHP Code:
    $hook_array['process_record'] = Array();
    $hook_array['process_record'][] = Array(1'calculateField''modules/Opportunities/OpportunityLogicHook.php','OpportunityLogicHook''calculateField'); 
    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.

  7. #7
    kinshibuya's Avatar
    kinshibuya is offline A Sugar Hero
    Join Date
    Jul 2008
    Location
    brasil
    Posts
    521

    Default Re: Calculated Sum of 2 fields

    Where do i put this code?
    Last edited by kinshibuya; 2008-09-12 at 01:57 PM.

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

    Default Re: Calculated Sum of 2 fields

    Quote Originally Posted by andopes
    Oi kinshibuya

    Basta criar o logic_hook after_retrieve no mesmo módulo apontando para a mesma função:

    PHP Code:
    $hook_array['after_retrieve'] = Array();
    $hook_array['after_retrieve'][] = Array(1'calculateField''modules/Opportunities/OpportunityLogicHook.php','OpportunityLogicHook''calculateField'); 

    Caro andopes,
    Estou tentando aplicar essa sugestão, mas não estou tendo sucesso.
    Onde esse código deve ser colocado? dentro do arquivo já existente logic_hooks.php?

    Como por exemplo:

    $hook_version =1;
    $hook_array = Array();

    $hook_array['after_retrieve'] = Array();
    $hook_array['after_retrieve'][] = Array(1, 'calcular', 'custom/modules/Cinco_Vendas/calculo.php', 'Calculo', 'calcular');

    $hook_array['before_save'] = Array();
    $hook_array['before_save'][] = Array(2, 'calcular', 'custom/modules/Cinco_Vendas/calculo.php', 'Calculo', 'calcular');

    $hook_array['process_record'] = Array();
    $hook_array['process_record'][] = Array(3, 'calcular', 'custom/modules/Cinco_Vendas/calculo.php', 'Calculo', 'calcular');

    ?>

  9. #9
    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: Calculated Sum of 2 fields

    Olá Jorge

    Se você quer aplicar esse logic_hook em um módulo novo, que você teria criado pelo Module Builder, então é desnecessário definir no logic_hook, embore funcione.
    O conceito de logic_hook é para realizar customizações upgrade safe. Aparentemente você está querendo definir um logic_hook em um custom module.
    Minha sugestão é criar os métodos retrieve e save no na classe do seu módulo.

    Neste caso o código seria parecido com esse:

    PHP Code:
        function retrieve($id) {
            
    parent::retrieve($id);
            
            
    /*
            adicione seu código aqui
            */
            
            
    return $this;
        }
        
        function 
    save($check_notify false) {
            
    /*
            adicione seu código aqui
            */
            
            
    parent::save($check_notify);
            
            return 
    $this->id;
        } 
    Mas se você quer aplicar o logic_hook em um módulo nativo do Sugar ai vai a explicação:
    O arquivo contendo as definições do logic_hook deve estar em: custom/modules/<Modulo>/logic_hooks.php

    $hook_array['after_retrieve'][] = Array(1, 'calcular', 'custom/modules/Cinco_Vendas/calculo.php', 'Calculo', 'calcular');

    'after_retrieve' : o logic_hook a ser invocado, depois de carregar o registro do banco;
    Primeiro elemento do array (1): um contador;
    Segundo elemento do array ('calcular'): um identificador, você precisa garantir que para o mesmo logic_hook (after_retrieve) não existirão 2 elementos com esse mesmo valor;
    Terceiro elemento ('custom/modules/Cinco_Vendas/calculo.php'): nome do arquivo a ser incluído;
    Quarto elemento ('Calculo'): nome da classe a ser instanciada (presente neste arquivo);
    Quinto elemento ('calcular'): nome do método desta classe contendo a lógica a ser implementada.

    Uma informação importante.
    No arquivo contendo a classe a ser incluído você deve colocar a linha depois da tag de abertura do php:

    PHP Code:
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); 
    Espero que isso tenha ajudado, depois você paga a breja.

    Abraços
    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.

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

    Default Re: Calculated Sum of 2 fields

    andopes,
    Obrigado pela resposta rápida.
    Como aprendiz do Sugar vou estudar com muita atenção a tua sugestão.
    Abraços

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Calculated fields in 5.0
    By salimmohd82 in forum Developer Help
    Replies: 9
    Last Post: 2012-01-24, 08:35 PM
  2. Sum of 2 fields
    By aschaeffer in forum Help
    Replies: 10
    Last Post: 2009-02-23, 02:48 PM
  3. ireport - calculated fields
    By mikesolomon in forum Help
    Replies: 0
    Last Post: 2008-04-24, 12:58 PM
  4. Creating Fields - calculated fields and 'related' field
    By gipps in forum General Discussion
    Replies: 0
    Last Post: 2007-07-21, 10:17 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
  •