Results 1 to 10 of 10

Thread: Calculate Fields

  1. #1
    xf4driver is offline Member
    Join Date
    Jan 2009
    Posts
    5

    Default Calculate Fields

    First entry here so bear with me. I created 2 custom modules for placing orders called requisitions & line items. I have fields in line items named quantity, price & total. I have a field in the requisitions module called total cost. What I need sugar to do is calculate quantity * price = total for each of the line items, display the total, & then place the sum of total in the total cost field of the requisitions module.
    I have read the thread in wiki on logic hooks & most of the ones on calculating fields in the forum. I have copied the format shown in this thread: http://www.sugarcrm.com/forums/showt...culated+fields but substituted my field names & nothing happens. I may be putting the files in the wrong place.
    Can someone help with this. Would appreciate it if you could be as specific as you can as I am a programming novice. I use 5.1 CE on a Windows based system.
    Also, if someone knows of a module that does something similar I can upload it & use that coding as a guide.

  2. #2
    Iggby's Avatar
    Iggby is offline Sugar Community Member
    Join Date
    Feb 2008
    Location
    Huntingtonton Beach, Ca
    Posts
    101

    Default Re: Calculate Fields

    I have a logic hook that calculates the sum of fields with a module and it works perfectly. I would suggest posting the code that you are trying that doesn't work so we can give more specific suggestions on your problem.

    As far as pulling values between 2 modules, I cannot be of help as that is something I have been trying to get accomplished for months with no success.

    Also this post probably would be better off in the developers help section.
    Development Setup
    Sugar 5.0.0c
    Fedora Core 7
    Plesk 8.3
    PHP 5.2.4-1.fc7
    MySQL 5.0.45.fc7

  3. #3
    xf4driver is offline Member
    Join Date
    Jan 2009
    Posts
    5

    Default Re: Calculate Fields

    Thanks for the prompt reply. Here is my code:
    <?php
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    $hook_version=1;
    $hook_array=Array();
    $hook_array['before_save']=Array();
    $hook_array['before_save']
    []=Array(1,'calculatefield','modules/Line6_LineItems/total.php','TotalClass','calculatefield_before_sav e');
    ?>
    <?php

    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    class Total {
    function calculateField(&$focus, $event, $arguments) {
    $focus->total = $focus->quantity * $focus->price;
    }
    }

    ?>
    I pretty much copied the example from the referenced thread & plugged in my module & field names.

  4. #4
    Iggby's Avatar
    Iggby is offline Sugar Community Member
    Join Date
    Feb 2008
    Location
    Huntingtonton Beach, Ca
    Posts
    101

    Default Re: Calculate Fields

    Quote Originally Posted by xf4driver View Post
    Thanks for the prompt reply. Here is my code:
    <?php
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    $hook_version=1;
    $hook_array=Array();
    $hook_array['before_save']=Array();
    $hook_array['before_save']
    []=Array(1,'calculateField','modules/Line6_LineItems/total.php','Total','calculateField');
    ?>
    <?php

    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    class Total {
    function calculateField(&$focus, $event, $arguments) {
    $focus->total = $focus->quantity * $focus->price;
    }
    }

    ?>
    I pretty much copied the example from the referenced thread & plugged in my module & field names.
    Try the changes in bold. You are referencing the class TotalClass in your logic hook but then the class is called Total in your script. Also your function within the class needs to be the same. At least this is what looks incorrect to me. I also do the formula differently but this may be a more correct way, so try the changes mentioned first, if it still doesn't work I can show the way I accessed the fields in my script.
    Development Setup
    Sugar 5.0.0c
    Fedora Core 7
    Plesk 8.3
    PHP 5.2.4-1.fc7
    MySQL 5.0.45.fc7

  5. #5
    xf4driver is offline Member
    Join Date
    Jan 2009
    Posts
    5

    Default Re: Calculate Fields

    I know this is kind of basic but where do I put the code files. I must be putting them in the wrong place.

  6. #6
    Iggby's Avatar
    Iggby is offline Sugar Community Member
    Join Date
    Feb 2008
    Location
    Huntingtonton Beach, Ca
    Posts
    101

    Default Re: Calculate Fields

    I put the script and logic hook file in custom/modules/MODULE/ where MODULE is the name of the module the logic hook goes to. This way the files will not be overwritten in an upgrade. You also would need to change the path in the logic hook file to this location ex:

    $hook_array['before_save'] []=Array(1,'calculateField','custom/modules/total.php','Total','calculateField');

    make sure the permissions are correct as well, sometimes when transferring files I forget to make the file accessible by the we server and things dont work.

    Good luck.
    Development Setup
    Sugar 5.0.0c
    Fedora Core 7
    Plesk 8.3
    PHP 5.2.4-1.fc7
    MySQL 5.0.45.fc7

  7. #7
    Iggby's Avatar
    Iggby is offline Sugar Community Member
    Join Date
    Feb 2008
    Location
    Huntingtonton Beach, Ca
    Posts
    101

    Default Re: Calculate Fields

    by the way that wouldn't happen to be f4 as in phantom would it?
    Development Setup
    Sugar 5.0.0c
    Fedora Core 7
    Plesk 8.3
    PHP 5.2.4-1.fc7
    MySQL 5.0.45.fc7

  8. #8
    xf4driver is offline Member
    Join Date
    Jan 2009
    Posts
    5

    Default Re: Calculate Fields

    Yes it would. Flew them in the USMC many years ago. The J & S model. Can you show me your math script. Mine is still not working.

  9. #9
    xf4driver is offline Member
    Join Date
    Jan 2009
    Posts
    5

    Default Re: Calculate Fields

    I got it to work! Thanks for all your help.

  10. #10
    Iggby's Avatar
    Iggby is offline Sugar Community Member
    Join Date
    Feb 2008
    Location
    Huntingtonton Beach, Ca
    Posts
    101

    Default Re: Calculate Fields

    Hopefully something I said led you in the right direction.

    My friend flew f18's for the core. I just fly a bonanza. I appreciate your service to our country.
    Development Setup
    Sugar 5.0.0c
    Fedora Core 7
    Plesk 8.3
    PHP 5.2.4-1.fc7
    MySQL 5.0.45.fc7

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How to calculate?
    By Hill in forum Developer Help
    Replies: 9
    Last Post: 2009-01-22, 03:49 PM
  2. calculate number of days between two dates
    By AnjaliSharma in forum Help
    Replies: 0
    Last Post: 2009-01-15, 08:33 AM
  3. Calculate Sum Of 2 Fields
    By DanBrown in forum Help
    Replies: 11
    Last Post: 2008-11-20, 08:51 AM
  4. Replies: 4
    Last Post: 2007-04-03, 02:27 AM
  5. Auto Calculate & Populate Fields
    By coowboy in forum Developer Help
    Replies: 2
    Last Post: 2006-06-26, 11:58 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
  •