• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

calc element with where condition

Martooti

Member
Hi,
I want to make an currency exchange calc inside the list. Exchange rates change monthly.
I have one table with `Date` (yyyy-mm-dd) , `Dolar`, and `Dolar_Equivalent` (in Egyptian Pound)
and another table with `Month` (dropdown: 1,2,3.., 12) , `Year`(dropdown : 2015, 2016,...), `Exchange_rate`
Dolar_Equivalent I wish to be a calc that multiply `Dolar` by `Exchange_rate` when `Date` from first table equals `Month` and `Year` from the second table.
I tried to use:
"return (int)'{ rt_cars_accounting___dolar}' * '{rt_exchange___dolar_equivalent}' WHERE 'substr( {rt_cars_accounting___date}, 5, 2 )'= '{rt_exchange___month}' AND 'substr( {rt_cars_accounting___date}, 0, 4 )' = '{rt_exchange___year}' ;" but I am getting an error.
Any advice?
Thanks in advance
 
Yeah, I'm not surprised you get an error, that ... erm ... nothing like valid PHP code.

Try something like this ...

Code:
$db = FabrikWorker::getDbo();
$query = $db->getQuery(true);
if (!empty('{rt_cars_accounting___date}') && !empty('{rt_cars_accounting___Dolar}')) {
   $query->select('Exchange_rate')->from('rt_exchange')->where('MONTH("{rt_cars_accounting___date}") = rt_exchange___month AND YEAR("{rt_cars_accounting___date}") = rt_exchange___year');
   $db->setQuery($query);
   $rx = (float) $db->loadResult();
   if (!empty($rx)) {
      return (float)'{rt_cars_accounting___Dolar}' * $rx;
   }
}
return '0';

-- hugh
 
Thank you for your answer Hugh.
Unfortuantely I am getting error:
Error

An error has occurred with a eval'd field - please inform the web-site owner.
Debug: Eval exception : eqiuvalent::_getV() : $db = FabrikWorker::getDbo(); $query = $db->getQuery(true); if (!empty('') && !empty('')) { $query->select('dolar_rate')->from('rt_exchange')->where('MONTH("") = rt_exchange___month AND YEAR("") = rt_exchange___year'); $db->setQuery($query); $rx = (float) $db->loadResult(); if (!empty($rx)) { return (float)'' * $rx; } } return '0'; : syntax error, unexpected '''' (T_CONSTANT_ENCAPSED_STRING)

link
 
Ah, OK, try:

Code:
$db = FabrikWorker::getDbo();
$query = $db->getQuery(true);
$date = '{rt_cars_accounting___date}';
$dollar = '{rt_cars_accounting___Dolar}';
if (!empty($date) && !empty($dollar)) {
  $query->select('Exchange_rate')->from('rt_exchange')->where('MONTH("{rt_cars_accounting___date}") = rt_exchange___month AND YEAR("{rt_cars_accounting___date}") = rt_exchange___year');
  $db->setQuery($query);
  $rx = (float) $db->loadResult();
  if (!empty($rx)) {
      return (float)$dollar * $rx;
  }
}
return '0';

-- hugh
 
With this code when I open the form there is no errors, but when submin I got :

" Unknown column 'rt_exchange___month' in 'where clause' SQL=SELECT dolar_rate FROM rt_exchange WHERE MONTH("2015-03-04 13:02:12") = rt_exchange___month AND YEAR("2015-03-04 13:02:12") = rt_exchange___year "

So I changed it to

"$db = FabrikWorker::getDbo();
$query = $db->getQuery(true);
$date = '{rt_cars_accounting___date}';
$dollar = '{rt_cars_accounting___dolar}';
if (!empty($date) && !empty($dollar)) {
$query->select('dolar_rate')->from('rt_exchange')->where('MONTH("{rt_cars_accounting___date}") = "{rt_exchange___month}" AND YEAR("{rt_cars_accounting___date}") = "{rt_exchange___year}"');
$db->setQuery($query);
$rx = (float) $db->loadResult();
if (!empty($rx)) {
return (float)$dollar * $rx;
}
}
return '0';"

and like that I can submit without errors but every time calc element shows " 0 " :(
 
and another table with `Month` (dropdown: 1,2,3.., 12) , `Year`(dropdown : 2015, 2016,...), `Exchange_rate`
If rt_exchange is your DB table and month/year are the columns I think it must be

$query->select('Exchange_rate')->from('rt_exchange')->where('MONTH("{rt_cars_accounting___date}") = month AND YEAR("{rt_cars_accounting___date}") = year');

{} is for element placeholders, not for DB columns.
 
I changed

$query->select('dolar_rate')->from('rt_exchange')->where('MONTH("{rt_cars_accounting___date}") = "{rt_exchange___month}" AND YEAR("{rt_cars_accounting___date}") = "{rt_exchange___year}"');

TO
$query->select('dolar_rate')->from('rt_exchange')->where('MONTH("{rt_cars_accounting___date}") = rt_exchange.month AND YEAR("{rt_cars_accounting___date}") = rt_exchange.year');

and it is working this way :D

Thank you all for your amazing support!
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top