Calculated Fields [SOLVED]

A place for users and developers of the Xataface to discuss and receive support.

Calculated Fields [SOLVED]

Postby cantlep » Tue Mar 16, 2010 11:08 am

Hi All,

I know calculated fields have been mentioned in the past on the forum but a lot of them relate to old versions and I'm hoping some more functionality exists now-a-days. I'm running the latest BETA version.

Here's what I'm trying to do.

I have a table called "BillData". Within it are various fields. The ones of concern are BillTotalExVAT and BillTotalIncVAT. (I think you can see where I'm going with this). I want to manually populate BillTotalExVAT but I want BillTotalIncVAT to update accordingly.

I've tried it like this (Perhaps I'm being a bit too simple) but it doesn't work via Dataface (with mysql Cmnd line, it works fine).

in tables/BillData/fields.ini I have this

Code: Select all
[BillTotalIncVAT]
widget:label = "Bill Total (Inc VAT)"
visibility:browse = hidden
visibility:find = hidden
vocabulary = BillTotalIncVATCalc


in tables/BillData/valuelist.ini I have this

Code: Select all
[BillTotalIncVATCalc]
__sql__="SELECT BillTotalExVAT*0.175+BillTotalExVAT FROM BillData"


Sadly, the column titled BillTotalIncVAT is just left blank :-(

I'm probably going about this the wrong way. Can anyone assist at all?

Thanks

Paul
Last edited by cantlep on Thu Mar 18, 2010 3:34 am, edited 1 time in total.
cantlep
 
Posts: 172
Joined: Fri Mar 05, 2010 2:14 am

Re: Calculated Fields

Postby shannah » Tue Mar 16, 2010 12:31 pm

Here's how I would do this:

Suppose I have a field (subtotal) and I want to add a calculated field (totalAfterTax).

Step 1: Add a dummy calculated grafted field using the __sql__ directive in the fields.ini file (not to be confused with the __sql__ directive in the valuelists.ini files).

e.g.
Code: Select all
__sql__ = "select c.*, c.subtotal as totalAfterTax from items c"


Step 2: Use the xxx__display() delegate method to override the display of the totalAfterTax field with your calculation:
Code: Select all
function totalAfterTax__display(&$record){
    return $record->val('subtotal')*1.07;
}


The reason I don't do the calculation in the __sql__ part is because of deficiencies in the SQL parser that causes it not to handle inline arithmetic very happily... it's on the list.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Calculated Fields

Postby cantlep » Tue Mar 16, 2010 4:32 pm

Hi Steve, Fantastic as usual. All works like a charm.

You've been pretty busy this eve haven't you! Well appreciated, I'm sure.

Is there anyway that I can restrict the output to 2 decimal places for this dummy field? (equiv of say decimal(10,2)) ?

Thanks again

Paul
cantlep
 
Posts: 172
Joined: Fri Mar 05, 2010 2:14 am

Re: Calculated Fields

Postby shannah » Tue Mar 16, 2010 4:42 pm

Check out the money_format function.
http://ca2.php.net/money_format
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Calculated Fields

Postby cantlep » Tue Mar 16, 2010 5:19 pm

Fantastic :-) All sorted using money_format()

Cheers

Paul
cantlep
 
Posts: 172
Joined: Fri Mar 05, 2010 2:14 am


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 38 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved