From be37f9b5ac44d40e0cb1fa1115fced8cb14d2404 Mon Sep 17 00:00:00 2001 From: Dany De Bontridder Date: Sun, 22 Apr 2018 12:28:08 +0200 Subject: [PATCH] Currency : add ajax call to delete one rate , add documentation , remove debug info --- html/ajax_misc.php | 4 +- html/js/acc_currency.js | 56 +++++++++++++ include/ajax/ajax_currency.php | 94 ++++++++++++++++++++++ include/class/currency_mtable.class.php | 31 +++++-- include/lib/function_javascript.php | 2 +- include/template/currency_mtable_input.php | 21 ++++- 6 files changed, 198 insertions(+), 10 deletions(-) create mode 100644 html/js/acc_currency.js create mode 100644 include/ajax/ajax_currency.php diff --git a/html/ajax_misc.php b/html/ajax_misc.php index 66e68779a..db1fc7c02 100644 --- a/html/ajax_misc.php +++ b/html/ajax_misc.php @@ -244,7 +244,9 @@ $path = array( // From FollowUp , update a comment on a file 'update_comment_followUp'=>'ajax_follow_up', // TVA param - "tva_parameter"=>"ajax_tva_parameter" + "tva_parameter"=>"ajax_tva_parameter", + // Currency , delete a rate + "CurrencyRateDelete"=>"ajax_currency" ) ; if (array_key_exists($op, $path)) { diff --git a/html/js/acc_currency.js b/html/js/acc_currency.js new file mode 100644 index 000000000..356eef8df --- /dev/null +++ b/html/js/acc_currency.js @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2018 Dany De Bontridder + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ +/** + * @file + * @brief All the currency related ajax calls + */ + +/** + * Delete a old currency rate via ajax, but all currency must have at least one rate, delete the rate and hide the + * row in the table "currency_rate_table" + * @param {int} p_dossier + * @param {int} p_id + * @see currency_mtable_input.php + * DOMID + * - row = currency_rate_{p_id} + * - table = currency_rate_table + */ +function CurrencyRateDelete(p_dossier, p_id) +{ + smoke.confirm("Confirm ?", function (e) { + if (e) { + waiting_box(); + var a = new Ajax.Request("ajax_misc.php", { + method: 'get', + parameters:{gDossier:p_dossier,op:"CurrencyRateDelete",currency_rate_id:p_id}, + onSuccess: function (req) + { + remove_waiting_box(); + var answer=req.responseText.evalJSON(); + if ( answer['status'] == 'NOK') { + smoke.alert(answer['content']); + } else { + $('currency_rate_'+p_id).hide(); + alternate_row_color("currency_rate_table"); + } + } + }); + } + }); + +} \ No newline at end of file diff --git a/include/ajax/ajax_currency.php b/include/ajax/ajax_currency.php new file mode 100644 index 000000000..e1068c5b8 --- /dev/null +++ b/include/ajax/ajax_currency.php @@ -0,0 +1,94 @@ + + +if (!defined('ALLOWED')) + die('Appel direct ne sont pas permis'); + +/** + * @file + * @brief Ajax response for currency related calls + */ +$err=0; +$a_answer=array(); +$a_answer['status']="NOK"; +$http=new HttpInput(); +try +{ + $act=$http->request("op"); +} +catch (Exception $ex) +{ + $a_answer['content']=$ex->getMessage(); + $jsson=json_encode($a_answer, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); + header('Content-Type: application/json;charset=utf-8'); + echo $jsson; + return; +} + +/* security check */ +/** + * @todo add module CFGCURRENCY + */ +if ($g_user->check_module('CFGCURRENCY')==0) +{ + // return; +} + +/* + * Select action + */ +switch ($act) +{ + case 'CurrencyRateDelete': + try { + $currency_rate_id=$http->get("currency_rate_id","number"); + // check that a least one rate is remaining for this currency + + // 1. get the currency + $currency_id=$cn->get_value("select currency_id from currency_history where id=$1",[$currency_rate_id]); + + // 2. get the number of rate + if ( $currency_id == "") { + throw new Exception(_("Taux inexistant")); + } + $cnt=$cn->get_value("select count(*) from currency_history where currency_id=$1",[$currency_id]); + + // 3. if number of rate > 1 , then delete + if ( $cnt > 1) + { + $cn->exec_sql("delete from currency_history where id=$1",[$currency_rate_id]); + $a_answer['status']=_("OK"); + $a_answer['content']=_("Taux effacé"); + } else { + $a_answer['content']=_("Non effacé : Il faut au moins un taux"); + } + } catch (Exception $ex) { + $a_answer['content']=$ex->getMessage(); + } + break; +} + +$jsson=json_encode($a_answer, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); +if (!headers_sent()) +{ + header('Content-Type: application/json;charset=utf-8'); +} +echo $jsson; diff --git a/include/class/currency_mtable.class.php b/include/class/currency_mtable.class.php index ace6ed1d4..b3faf9e21 100644 --- a/include/class/currency_mtable.class.php +++ b/include/class/currency_mtable.class.php @@ -27,6 +27,10 @@ require_once NOALYSS_INCLUDE.'/lib/manage_table_sql.class.php'; require_once NOALYSS_INCLUDE.'/database/currency_sql.class.php'; require_once NOALYSS_INCLUDE.'/database/currency_history_sql.class.php'; +/** + * Manage the configuration of currency , add currency, rate, remove and update + * Concerned tables are v_currency_last_value _SQL , Currency_SQL , Currency_History_SQL + */ class Currency_MTable extends Manage_Table_SQL { @@ -64,12 +68,15 @@ class Currency_MTable extends Manage_Table_SQL * returns TRUE the currency is used otherwise FALSE. We cannot delete a currency which is used in a * operation * @returns boolean true if currency is used + * @todo Currency_MTable.is_currency_used to implement */ function is_currency_used() { return FALSE; } - + /** + * Box to enter either a new currency or update a existing one + */ function input() { $record=$this->get_table(); @@ -77,7 +84,7 @@ class Currency_MTable extends Manage_Table_SQL $cr_code_iso->size=10; $cr_name=new IText("cr_name", $record->cr_name); $cr_name->size=50; - $a_histo=$record->cn->get_array("select to_char(ch_from,'DD.MM.YYYY') as str_from,ch_value + $a_histo=$record->cn->get_array("select id,to_char(ch_from,'DD.MM.YYYY') as str_from,ch_value from currency_history where @@ -96,7 +103,12 @@ class Currency_MTable extends Manage_Table_SQL } /** - * + * Check that the value are correct : + * - Code iso must be unique + * - Name cannot be empty + * - At least one rate + * - Date of the rate + * */ function check() { @@ -183,7 +195,7 @@ class Currency_MTable extends Manage_Table_SQL } /** - * + * Either insert a new currency + one rate or add a rate to an existing currency */ function save() { @@ -235,7 +247,16 @@ class Currency_MTable extends Manage_Table_SQL throw ($ex); } } - + /** + * Fill the object from request + * parameters : + * - cr_code_iso + * - cr_name + * - p_id + * - new_rate_date + * - new_rate_value + * + */ function from_request() { $http=new HttpInput(); diff --git a/include/lib/function_javascript.php b/include/lib/function_javascript.php index 0db0317e2..a03245a0a 100644 --- a/include/lib/function_javascript.php +++ b/include/lib/function_javascript.php @@ -2781,7 +2781,7 @@ function load_all_script() echo js_include('sorttable.js'); echo js_include('nicEdit.js'); echo js_include('managetable.js'); - + echo js_include('acc_currency.js'); } ?> diff --git a/include/template/currency_mtable_input.php b/include/template/currency_mtable_input.php index dae160cb3..e48c6ee5f 100644 --- a/include/template/currency_mtable_input.php +++ b/include/template/currency_mtable_input.php @@ -45,27 +45,42 @@ if (!defined('ALLOWED')) die('Appel direct ne sont pas permis'); - - + +
+ - + +
+ +
+ +