1

I am trying to get a total from the rows returned for the selected opportunity.

When a opportunity is selected each product they have purchased and its price is listed. I am trying to use the price for each purchased product to get a subtotal for all sales made with that opportunity.

Here is the code I have:

function total(&$focus, $event, $arguments)
{
    $total = 0;
    foreach ($this->bean->Product_Sales['sales_price_c'] as $entry) {
        $total += unformat_number($entry['sales_price_c']);
    }
    $this->bean->ss->assign('total_sales_c', format_number($total));
}

Example of how rows are returned:

[Product_Name_Field] [Product_Price_Field] [Sales_Person_Field] [Etc_Field]

Only qty(1) Product sold per returned row.

What am I doing wrong?
Thanks in advance.

Damund
  • 75
  • 11
  • What result do you get with that code? Is there a "quantity" you need to take account of? – Bill Woodger Jan 17 '16 at 08:35
  • I am trying to calculate the total value of all products purchased but the loaded opportunity. So far I am not successful. – Damund Jan 17 '16 at 08:37
  • You've said that already. Want to take a shot at answering my questions? – Bill Woodger Jan 17 '16 at 08:39
  • The quantity is the dollar amount of each product. This code is not working. There is only one product sale per returned row. – Damund Jan 17 '16 at 08:40
  • Please inspect what you have in that variable $this->bean via xdebug or print it's content to the sugarcrm.log for example like this: $GLOBALS['log']->fatal("ProductSales" . json_encode($this->bean)); and post this here so we can help you – pauel Jan 18 '16 at 09:49
  • @pauel I figured this out what do you think? Can you see any short comings that could be an issue? – Damund Feb 09 '16 at 07:56

1 Answers1

1

Okay I figured it out!!!!

This is File view.detail.php in Custom/Module/Opportunities/Views/

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

require_once('include/MVC/View/views/view.detail.php');

class OpportunitiesViewDetail extends ViewDetail {

  function OpportunitiesViewDetail(){
  parent::ViewDetail();
  }

  function display() {
$account = new Opportunity();//var = new ModuleName() in singular form
$account->retrieve($_REQUEST['record']);//This grabs the record
$contacts = $account->get_linked_beans('opportunities_op_ps_product_sales_1','Contact');
//this uses the get_linked_beans(Param 1 is the linked var name found in the vardefs ,Param 2 is the name of the object you are creating. The name can be anything you like.)

// loop through the created associations to get fields.
foreach ( $contacts as $contact ) {
    $total += $contact->sales_price_c;//add the value of each sale to the variable
}
//populate the field you want with the value in the $total var
echo "
       <script>
    var total = '$total';
       $(document).ready(function(){
    $('#total_sales_c').after(total); });
       </script>";

  parent::display();
  }
}
?>

Hopefully this will help others.

Damund
  • 75
  • 11