1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Calculating Cost and Totals in forms

Discussion in 'Community' started by nbradshaw, Jun 12, 2019.

  1. nbradshaw

    nbradshaw Member

    Level: Community
    So - I have read dozens and dozens of forum posts on this subject - but it is just not clear to me how to properly calculate row totals then the Grand Total (in separate Group)

    I can successfully use the calc element to get the individual row totals (ie, unit price * quantity) using the code below:

    PHP:
    $myCalc = (int)'{bid_system_5_repeat___quantity_raw}' * (int)'{bid_system_5_repeat___rate_raw}';
    return $myCalc;
    However, I am unclear if I am supposed to use a calc element (since it is not an input field and can cause issues with order firing, etc...when trying to get the grand total (in a different group)

    Can someone please point me to a solid article on how to set all of this up properly? Also - Can I create a wiki that makes this super duper easily to understand after I get this setup properly?
     
  2. nbradshaw

    nbradshaw Member

    Level: Community
    Friendly Bump.
     
  3. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    There's a lot of ways to skin that cat.

    First question, do you need to see the total update in real time on the page as you enter data in the repeat group?

    -- hugh
     
  4. nbradshaw

    nbradshaw Member

    Level: Community
    Hi Hugh!

    Yes, I do need it in real time....that's the issue I am having right now.

    I did just figure out how to do it with this code with a calc element (with Ajax = Yes)...however, it is just not realtime (but it does work on save)

    PHP:
    $sum = 0;
    $sum += array_sum($data['bid_system_5_repeat___total']);
    return $sum;
    And how about formatting to $0.00?
     
    Last edited: Jun 13, 2019
  5. nbradshaw

    nbradshaw Member

    Level: Community
    Friendly bump.
     
  6. startpoint

    startpoint Active Member

    Level: Community
  7. nbradshaw

    nbradshaw Member

    Level: Community
    Thanks StarPoint. I am still needing guidance on how to do the realtime row calculations? Can someone please assist with this one?
     
  8. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Did you add the element name you are using to the "additional AJAX elements" (or whatever that field is called in the calc settings)?

    If you use a placeholder in your calc, like {mytable___element}, then we will automatically set up the Javascript to watch that element, and fire off the AJAX calc when it changes. But if you need us to watch an element which isn't in the calc code as a placeholder (such as accessing it through $data[]), then you need to tell us, in that "additional elements" setting, which element name(s) to watch.

    -- hugh
     
  9. nbradshaw

    nbradshaw Member

    Level: Community
    A big HUGE thanks to @startpoint !!! He was gracious enough work with me on this and wrote a custom jquery form_x.js file to make this work to fit my needs. Thanks again @startpoint !!! Not to take away from you also Hugh, you have helped me countless times also!!!
     
  10. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    @nbradshaw @startpoint well, looks like I was a day or two late.

    I just committed the first cut of a new 'total' plugin, that allows two types of total calculations:

    "Repeat Totalling" - totalling one or more elements from a repeat group into a total on the main group (like, summing 'qty' on a repeat line item to a single 'total qty' in the main group).

    "Multiple Totalling" - totalling multiple elements from the same group into a total, for example "cost * tax" to get a "cost with tax". This works in either repeat or non repeat groups.

    The totalling allows you to specify the operand to use for each element.

    To get the new plugin, do a full github update, then a J! "Discover" install for the new "Fabrik - total element" plugin, and then enable it in the J! plugin manager.

    Tooltips probably need some work, but should give you enough to go on.

    -- hugh
     
    Bren, bea, dimoss and 2 others like this.
  11. nbradshaw

    nbradshaw Member

    Level: Community
    That is freaken awesome Hugh!!! That is a HUGE new feature, in my opinion!!! That is BLOG worthy!

    I will check it out and give you some feedback.
     
  12. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Yes please, feedback needed.

    The next thing I need to work out is subtotals and totals. So you can use a total element to total up other total elements. At the moment, there is no guarantee what order they get run in, so using totalling other total elements isn't really possible. It's not going to be trivial.

    -- hugh
     
  13. nbradshaw

    nbradshaw Member

    Level: Community
    I haven't gotten to this quite yet to test...but just a thought of how to possibly get around those obstacles you are speaking of (from a non-hardcore programmers mind). Could you simply just do the math all over again on all of the row totals - then just add a user input field within the plugin saying "tax %". Then you would have one subtotal where the user leaves "tax %" empty - then on the Grand Total - they input say 8.25% into that user input field and the plugin simply gets the raw row totals and multiplies by 8.25%...

    Not sure if all that made sense...but just a thought.
     
  14. bea

    bea Active Member

    Level: Community
    Hi Hugh,
    thank you for this great plugin.

    I've a calc to count the result of a repeated group element, which are 'not okay' (dropdown, value=2).
    Does it make sense, to extend this total plugin with a condition?

    Cheers,
    Bianka
     
  15. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    @bea - I can have a look at doing that. I think it might be possible, although complicated by the fact that we do the calculations in both Javascript on the browser side, and in PHP on the server side ... on the basis that we don't "trust" submitted totals from the form ... so (say) someone can't spoof a purchase order by manually setting the 'total' element to a lower value on the posted form data, we re-calculate totals on submission, and when re-displaying the record. So I'd have to come up with a way of allowing a condition to be specified which would work in both JS and PHP.

    -- hugh
     

Share This Page