Calculation question

marioms

Member
Hello guys,

I want to know if this is possible. I have 3 elements that are the total of different calculations (word count total, hours total and fixed price). Now I want to create an element that will unify them and will show up on the list.

Right now I have it this way:

return "{j32db_education___Total_hours_calculation}";
return "{j32db_education___Total}";
return "{j32db_education___Fixed_price}";

but of course it only shows on the list the first one, the calculation of hours (if that option has been chosen on the creation of the form). I want to create a condition formula that will check up which one has a number to show up so that will be number being shown on the list.

I want something like this:

if "{j32db_education___Total_hours_calculation}" has a number
return "{j32db_education___Total_hours_calculation}";

else

if "{j32db_education___Total}" has a number
return "{j32db_education___Total}";

else

if "{j32db_education___Fixed_price}" has a number
return "{j32db_education___Fixed_price}";


But i am not sure how to make it to work in fabrik, any suggestions? thanks!
 
You can't have a calc element based on the results of other calc elements. There is no way to make sure the input calcs are complete to determine the end results.

The workaround is to put all the required code in your summary calculation.
 
And how could that be done? I mean what should I put in the summary calculation? what about another kind of element like.. display? that could work?
 
Copy/paste the code from your other 3 elements into your summary calculation without their return statements of course. Then put your if/else statement at the end.

If you tell me what you have for code already, I can try to help you that way instead, that might be easier.
 
Awesome, thank you Rackem, this is the code for the 3 calc elements I have:

To calculate the total of the first type is:

$num1 = (int) '{j32db_education___Word_Count_Result_1}';
$num2 = (float) '{j32db_education___Type_raw}';
$num3 = (int) '{j32db_education___Word_Count_Result_2}';
$num4 = (float) '{j32db_education___Type_2_raw}';
$num5 = (int) '{j32db_education___Word_Count_Result_3}';
$num6 = (float) '{j32db_education___Type_3_raw}';
$num7 = (int) '{j32db_education___Word_Count_Result_4}';
$num8 = (float) '{j32db_education___Type_4_raw}';
$total = ($num1 / 1000) * ($num2 * 847.00) + ($num3 / 1000) * ($num4 * 847.00) + ($num5 / 1000) * ($num6 * 847.00) + ($num7 / 1000) * ($num8 * 847.00);
return number_format($total, 2);

The second type is:

$num1 = (int) '{j32db_education___Number_Hours}';
$num2 = (float) '{j32db_education___Contractor_raw}';
$total = $num1 * $num2;
return number_format($total, 2);
And the third is a field element, just to introduce a fixed price.
When I create a form, there are 3 choices, I work with editors and translators, freelancers, so I can pay them in different ways, by word count, by fixed price or by hours worked
 
I think the following is pretty close to what you need. I guessed at what you named your fixed price field element and took a stab at the logic to determine which number to display.

PHP:
    // When I create a form, there are 3 choices, I work with editors and translators, freelancers,
    // so I can pay them in different ways, by word count, by fixed price or by hours worked
    // And the third is a field element, just to introduce a fixed price.
 
   
    // Word count calculation
    $num1 = (int) '{j32db_education___Word_Count_Result_1}';
    $num2 = (float) '{j32db_education___Type_raw}';
    $num3 = (int) '{j32db_education___Word_Count_Result_2}';
    $num4 = (float) '{j32db_education___Type_2_raw}';
    $num5 = (int) '{j32db_education___Word_Count_Result_3}';
    $num6 = (float) '{j32db_education___Type_3_raw}';
    $num7 = (int) '{j32db_education___Word_Count_Result_4}';
    $num8 = (float) '{j32db_education___Type_4_raw}';
    $wordCount = ($num1 / 1000) * ($num2 * 847.00) + ($num3 / 1000) * ($num4 * 847.00) + ($num5 / 1000) * ($num6 * 847.00) + ($num7 / 1000) * ($num8 * 847.00);
   
    // Total hours calculation
    $hours = (int) '{j32db_education___Number_Hours}';
    $contractor = (float) '{j32db_education___Contractor_raw}';
    $totalHours = $hours * $contractor;
   
    // Fixed price
    $fixedPrice = (float) '{j32db_education___fixed_price}';  // Change the element name to match your field element
   
    // Find the way that has a number
    if($wordCount > 0)
        $finalPrice = $wordCount;
    elseif($totalHours > 0)   
        $finalPrice = $totalHours;
    else
        $finalPrice = $fixedPrice;
 
    // Return the formatted price
    return number_format($finalPrice, 2);
 
Now that I have this, I have to think how to make a sum of all the results given on the final price element and rest them to a definit amount (budget), I am not really sure how I will do that, every year the company gives us a budget and from there we can work and create call-off orders (work orders). I am not sure if I have to make a new field called Budget with some new calculations.. how would you think it is the best way of doing it?
 
That is a pretty open-ended question. The budget numbers probably belong in a separate list and you will need some more custom code to do the appropriate processing. Fabrik has lots of ways to do things but I'm not really sure what the best approach would be for you. Here are some links that might help you out or give you some ideas of things to try. I have found browsing the wiki and going through the tutorials to be a great way to get new ideas about how to do things.

http://fabrikar.com/forums/index.php?wiki/design-strategy/
http://fabrikar.com/forums/index.php?wiki/common-php-tasks/
http://fabrikar.com/forums/index.php?wiki/php-form-plugin/
http://fabrikar.com/forums/index.php?wiki/php-scheduled-task/
 
Hi Rackem,

I tried to, at least, get a sum of the column tweaking a bit the options of the element, in the list view settings I can choose to get the sum of the element. But there is a problem, when I go to see on the list the result, it appears this to me:

15x0er.jpg

What could be the problem? it is making the sum of something else instead of the real total..
 
I think the problem is that the number format function changes your value to a string. The sum only works on numbers. You can check this by changing the last line from the code I gave before to

PHP:
return round($finalPrice, 2);

That will leave the value as a number but rounded off to two decimal places. Then your list sum should work as expected.
 
Now it works, but I need the numbers to show up with 2 decimals at the end:

2zteq6r.jpg



PS. Is it possible to introduce the symbol ? to those quantities on the list?
Thanks!!!
 
Ah, right. Using round() won't add decimal places where they won't exist like number_format() will do.

Try this instead for your last line.
PHP:
return round((100 * $finalPrice), 0) / 100;

Adding that symbol would make the value a string. Would it be acceptable to just put that symbol in your header and sum total label? For example "Final Total (?)" and "Total ?"
 
Hmm, I am surprised that didn't work. I did a quick Goggle search and PHP is able to translate certain string values into numbers. It can't translate currency symbols or comma separator but it will recognize decimals. So you could try

PHP:
return number_format($finalPrice, 2, ".", "");
 
I changed it, now it shows me the quantities up alright, but the result of the sum still shows 1 decimal :S

15x1lkz.jpg

This is how I have the code:

PHP:
// Word count calculation
    $num1 = (int) '{j32db_education___Word_Count_Result_1}';
    $num2 = (float) '{j32db_education___Type_raw}';
    $num3 = (int) '{j32db_education___Word_Count_Result_2}';
    $num4 = (float) '{j32db_education___Type_2_raw}';
    $num5 = (int) '{j32db_education___Word_Count_Result_3}';
    $num6 = (float) '{j32db_education___Type_3_raw}';
    $num7 = (int) '{j32db_education___Word_Count_Result_4}';
    $num8 = (float) '{j32db_education___Type_4_raw}';
    $wordCount = ($num1 / 1000) * ($num2 * 847.00) + ($num3 / 1000) * ($num4 * 847.00) + ($num5 / 1000) * ($num6 * 847.00) + ($num7 / 1000) * ($num8 * 847.00);
 
    // Total hours calculation
    $hours = (int) '{j32db_education___Number_Hours}';
    $contractor = (float) '{j32db_education___Contractor_raw}';
    $totalHours = $hours * $contractor;
 
    // Fixed price
    $fixedPrice = (float) '{j32db_education___Fixed_price}';
 
    // Find the way that has a number
    if($wordCount > 0)
        $finalPrice = $wordCount;
    elseif($totalHours > 0) 
        $finalPrice = $totalHours;
  else
        $finalPrice = $fixedPrice;
 
    // Return the formatted price
    return number_format($finalPrice, 2,".", "");
 
Hmm ... have you tried using element formatting strings to add the euro symbol and set 2 decimal places?

If so, does the total field use the same formatting string (because if it doesn't it probably should and this might be classed as a minor bug)?
 
Hi Sophist, I did not try to use the element "Format String" field... what should I exactly input on that field to get my number correctly? (Euro symbol and 2 decimal places)
 
I got it working somehow, on the code I put at the end:

PHP:
return round((100 * $finalPrice), 0) / 100;

and on the "Format String" field inside the element I wrote:

PHP:
%01.2f €

That made the magic so far.
 
In the Element settings for a field element type, Formatting tab, if you hover over the Format String label you will see it refers to sprintf. A google search for "php sprintf" comes up with the PHP manual page which gives details of %f for formatting floating point numbers.

So to format as ?123.45 you would put "?%.2f". If you want a fixed length (say 5.2 e.g. "? 123.45") then"?%5.2f", or with leading zeros (e.g. "?00123.45") then "?%05.2f".

Note: I have not tested these - they are just from the documentation.
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top