noalyss Version-9
acc_currency.js
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018 Dany De Bontridder <dany@alchimerys.be>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18/**
19 * @file
20 * @brief All the currency related ajax calls
21 */
22
23/**
24 * Delete a old currency rate via ajax, but all currency must have at least one rate, delete the rate and hide the
25 * row in the table "currency_rate_table"
26 * @param {int} p_dossier
27 * @param {int} p_id
28 * @see currency_mtable_input.php
29 * DOMID
30 * - row = currency_rate_{p_id}
31 * - table = currency_rate_table
32 */
33function CurrencyRateDelete(p_dossier, p_id)
34{
35 smoke.confirm("Confirm ?", function (e) {
36 if (e) {
37 waiting_box();
38 var a = new Ajax.Request("ajax_misc.php", {
39 method: 'get',
40 parameters:{gDossier:p_dossier,op:"CurrencyRateDelete",currency_rate_id:p_id},
41 onSuccess: function (req)
42 {
43 remove_waiting_box();
44 var answer=req.responseText.evalJSON();
45 if ( answer['status'] == 'NOK') {
46 smoke.alert(answer['content']);
47 } else {
48 $('currency_rate_'+p_id).hide();
49 alternate_row_color("currency_rate_table");
50 }
51 }
52 });
53 }
54 });
55
56}
57/**
58 * Update the field p_update with the rate from the currency
59 * @param DOMID p_code where the cr_code_iso will be selected
60 * @param DOMID p_update element to update
61 */
62function CurrencyUpdateValue(p_dossier,p_code,p_update)
63{
64 new Ajax.Request("ajax_misc.php",{
65 method:"get",
66 asynchronous:false,
67 parameters:{p_code:$(p_code).value,gDossier:p_dossier,op:'CurrencyRate'},
68 onSuccess:function (req) {
69 var answer=req.responseText.evalJSON();
70 if ( answer.status == "OK") {
71 $(p_update).value=answer.content;
72 } else {
73 smoke.alert(answer.content);
74 }
75 }
76 });
77}
78/**
79 * Update the field p_update with the code ISO from the currency
80 * @param DOMID p_code where the cr_code_iso will be selected
81 * @param DOMID p_update element to update
82 */
83function CurrencyUpdateCode(p_dossier,p_code,p_update)
84{
85 new Ajax.Request("ajax_misc.php",{
86 method:"get",
87 parameters:{p_code:$(p_code).value,gDossier:p_dossier,op:'CurrencyCode'},
88 onSuccess:function (req) {
89 var answer=req.responseText.evalJSON();
90 if ( answer.status == "OK") {
91 $(p_update).innerHTML=answer.content;
92 } else {
93 smoke.alert(answer.content);
94 }
95 }
96 });
97}
98/**
99 * Update the field Update with the amount in EUR (= default currency), ledger sale or purchase
100 * @param DOMID p_rate where the rate is stored
101 * @param DOMID p_update element to update with the rate
102 */
103function CurrencyCompute(p_rate,p_update)
104{
105 var tvac=1;
106
107 if ($('tvac')) {
108 tvac=$('tvac').innerHTML;
109 } else if ($("htva")) {
110 tvac=$('htva').innerHTML;
111 }
112
113 if ( isNaN(tvac)) {
114 tvac=1;
115 }
116 var rate=$(p_rate).value;
117 if ( isNaN(rate)) {
118 rate=1;
119 }
120 var tot=tvac/rate;
121 tot=Math.round(tot*100)/100;
122 $(p_update).innerHTML=tot;
123
124}
125/**
126 * Update the field Update with the amount in EUR (= default currency) for Miscealleneous Operation
127 * amount for DEB (domid : default_currency_deb , totalDeb)
128 * amount for CRED ( domid = default_currency_cred , totalCred),
129 * @param DOMID p_rate where the rate is stored
130 * @param DOMID p_update element to update with the rate
131 */
132function CurrencyComputeMisc(p_rate,p_update)
133{
134 var debAmount=$('totalDeb').innerHTML;
135 var credAmount=$('totalCred').innerHTML;
136
137 if ( isNaN(debAmount)) {
138 debAmount=0;
139 }
140 if ( isNaN(credAmount)) {
141 credAmount=0;
142 }
143 var rate=$(p_rate).value;
144 if ( isNaN(rate) || parseFloat(rate) == 0) {
145 rate=1;
146 }
147 var totDeb=debAmount/rate;
148 totDeb=Math.round(totDeb*100)/100;
149 $('default_currency_deb').innerHTML=totDeb;
150
151 var totCred=credAmount/rate;
152 totCred=Math.round(totCred*100)/100;
153 $('default_currency_cred').innerHTML=totCred;
154
155}
156
157/**
158 * Update the screen of input for Purchase and Sale
159 * @param {type} p_dossier
160 * @param {type} p_code name of the SELECT containing the currency code
161 * @param {type} p_update Domid of the element to update
162 * @param {type} p_rate domid of the element containing the currency rate
163 * @param {type} p_eur_amount domid of the amount in default currency to update
164 * @returns {undefined}
165 */
166function LedgerCurrencyUpdate(p_dossier,p_code,p_update,p_rate,p_eur_amount)
167{
168 // Hide or show the row of the table with the amount in EUR
169 if ($(p_code).value != 0) {
170 $('row_currency').show();
171 }else {
172 $('row_currency').hide();
173 }
174 CurrencyUpdateValue(p_dossier,p_code,p_rate);
175 CurrencyUpdateCode(p_dossier,p_code,p_update);
176 // Compute all the fields
177 compute_all_ledger ();
178
179}
180/**
181 * Update the screen of input for Misc. Operation
182 * @param {type} p_dossier
183 * @param {type} p_code name of the SELECT containing the currency code
184 * @param {type} p_update Domid of the element to update
185 * @param {type} p_rate domid of the element containing the currency rate
186 * @param {type} p_eur_amount domid of the amount in default currency to update
187 * @returns {undefined}
188 */
189function LedgerCurrencyUpdateMisc(p_dossier,p_code,p_update,p_rate,p_eur_amount)
190{
191 // Hide or show the row of the table with the amount in EUR (= default currency)
192 if ($(p_code).value != -1) {
193 $('row_currency').show();
194 }else {
195 $('row_currency').hide();
196 }
197 CurrencyUpdateValue(p_dossier,p_code,p_rate);
198 CurrencyUpdateCode(p_dossier,p_code,p_update);
199 // Compute all the fields
200 checkTotalDirect();
201
202}