*** empty log message ***
[fa-stable.git] / gl / manage / currencies.php
1 <?php
2
3 $page_security = 9;
4 $path_to_root="../..";
5 include_once($path_to_root . "/includes/session.inc");
6
7 page(_("Currencies"));
8
9 include_once($path_to_root . "/includes/ui.inc");
10 include_once($path_to_root . "/includes/banking.inc");
11
12 //---------------------------------------------------------------------------------------------
13
14 if (isset($_GET['selected_id']))
15 {
16         $selected_id = $_GET['selected_id'];
17
18 elseif (isset($_POST['selected_id']))
19 {
20         $selected_id = $_POST['selected_id'];
21 }
22 else
23         $selected_id = "";
24 //---------------------------------------------------------------------------------------------
25
26 function check_data()
27 {
28         if (strlen($_POST['Abbreviation']) == 0) 
29         {
30                 display_error( _("The currency abbreviation must be entered."));
31                 return false;
32         } 
33         elseif (strlen($_POST['CurrencyName']) == 0) 
34         {
35                 display_error( _("The currency name must be entered."));
36                 return false;           
37         } 
38         elseif (strlen($_POST['Symbol']) == 0) 
39         {
40                 display_error( _("The currency symbol must be entered."));
41                 return false;           
42         } 
43         elseif (strlen($_POST['hundreds_name']) == 0) 
44         {
45                 display_error( _("The hundredths name must be entered."));
46                 return false;           
47         }       
48         
49         return true;
50 }
51
52 //---------------------------------------------------------------------------------------------
53
54 function handle_submit()
55 {
56         global $selected_id;
57         
58         if (!check_data())
59                 return false;
60                 
61         if ($selected_id != "") 
62         {
63
64                 update_currency($_POST['Abbreviation'], $_POST['Symbol'], $_POST['CurrencyName'], 
65                         $_POST['country'], $_POST['hundreds_name']);
66         } 
67         else 
68         {
69
70                 add_currency($_POST['Abbreviation'], $_POST['Symbol'], $_POST['CurrencyName'], 
71                         $_POST['country'], $_POST['hundreds_name']);
72         }
73         
74         return true;
75 }
76
77 //---------------------------------------------------------------------------------------------
78
79 function check_can_delete()
80 {
81         global $selected_id;
82                 
83         if ($selected_id == "")
84                 return false;
85         // PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
86         $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtors_master WHERE curr_code = '$selected_id'";
87         $result = db_query($sql);
88         $myrow = db_fetch_row($result);
89         if ($myrow[0] > 0) 
90         {
91                 display_error(_("Cannot delete this currency, because customer accounts have been created referring to this currency."));
92                 return false;
93         }
94
95         $sql= "SELECT COUNT(*) FROM ".TB_PREF."suppliers WHERE curr_code = '$selected_id'";
96         $result = db_query($sql);
97         $myrow = db_fetch_row($result);
98         if ($myrow[0] > 0) 
99         {
100                 display_error(_("Cannot delete this currency, because supplier accounts have been created referring to this currency."));
101                 return false;
102         }
103                 
104         $sql= "SELECT COUNT(*) FROM ".TB_PREF."company WHERE curr_default = '$selected_id'";
105         $result = db_query($sql);
106         $myrow = db_fetch_row($result);
107         if ($myrow[0] > 0) 
108         {
109                 display_error(_("Cannot delete this currency, because the company preferences uses this currency."));
110                 return false;
111         }
112         
113         // see if there are any bank accounts that use this currency
114         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bank_accounts WHERE bank_curr_code = '$selected_id'";
115         $result = db_query($sql);
116         $myrow = db_fetch_row($result);
117         if ($myrow[0] > 0) 
118         {
119                 display_error(_("Cannot delete this currency, because thre are bank accounts that use this currency."));
120                 return false;
121         }
122         
123         return true;
124 }
125
126 //---------------------------------------------------------------------------------------------
127
128 function handle_delete()
129 {
130         global $selected_id;
131         if (!check_can_delete())
132                 return;
133         //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
134         
135         delete_currency($selected_id);
136
137         meta_forward($_SERVER['PHP_SELF']);
138 }
139
140 //---------------------------------------------------------------------------------------------
141
142 function display_currencies()
143 {
144         global $table_style;
145
146         $company_currency = get_company_currency();     
147         
148     $result = get_currencies();
149     
150     start_table($table_style);
151     $th = array(_("Abbreviation"), _("Symbol"), _("Currency Name"),
152         _("Hundredths name"), _("Country"), "", "");
153     table_header($th);  
154     
155     $k = 0; //row colour counter
156     
157     while ($myrow = db_fetch($result)) 
158     {
159         
160         if ($myrow[1] == $company_currency) 
161         {
162                 start_row("class='currencybg'");
163         } 
164         else
165                 alt_table_row_color($k);
166                 
167         label_cell($myrow["curr_abrev"]);
168                 label_cell($myrow["curr_symbol"]);
169                 label_cell($myrow["currency"]);
170                 label_cell($myrow["hundreds_name"]);
171                 label_cell($myrow["country"]);
172                 edit_link_cell("selected_id=" . $myrow["curr_abrev"]);
173                 if ($myrow["curr_abrev"] != $company_currency)
174                         delete_link_cell("selected_id=" . $myrow["curr_abrev"]. "&delete=1");
175                 
176                 end_row();
177                 
178     } //END WHILE LIST LOOP
179     
180     end_table();
181     
182     display_note(_("The marked currency is the home currency which cannot be deleted."), 0, 0, "class='currentfg'");
183 }
184
185 //---------------------------------------------------------------------------------------------
186
187 function display_currency_edit($selected_id)
188 {
189         global $table_style2;
190         
191         start_form();
192         start_table($table_style2);
193
194         if ($selected_id != "") 
195         {
196                 //editing an existing currency
197                 $myrow = get_currency($selected_id);
198
199                 $_POST['Abbreviation'] = $myrow["curr_abrev"];
200                 $_POST['Symbol'] = $myrow["curr_symbol"];
201                 $_POST['CurrencyName']  = $myrow["currency"];
202                 $_POST['country']  = $myrow["country"];
203                 $_POST['hundreds_name']  = $myrow["hundreds_name"];
204
205                 hidden('selected_id', $selected_id);
206                 hidden('Abbreviation', $_POST['Abbreviation']);
207                 label_row(_("Currency Abbreviation:"), $_POST['Abbreviation']);         
208         } 
209         else 
210         { 
211                 text_row_ex(_("Currency Abbreviation:"), 'Abbreviation', 4, 3);         
212         }
213
214         text_row_ex(_("Currency Symbol:"), 'Symbol', 10);
215         text_row_ex(_("Currency Name:"), 'CurrencyName', 20);
216         text_row_ex(_("Hundredths Name:"), 'hundreds_name', 15);        
217         text_row_ex(_("Country:"), 'country', 40);      
218
219         end_table(1);
220
221         submit_add_or_update_center($selected_id == "");
222
223         end_form();
224 }
225
226 //---------------------------------------------------------------------------------------------
227
228 if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM'])) 
229 {
230
231         if (handle_submit()) 
232         {
233                 meta_forward($_SERVER['PHP_SELF']);             
234         }       
235 }
236
237 //--------------------------------------------------------------------------------------------- 
238
239 if (isset($_GET['delete'])) 
240 {
241
242         handle_delete();
243 }
244
245 //---------------------------------------------------------------------------------------------
246
247 display_currencies();
248
249 hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter a New Currency"));
250
251 display_currency_edit($selected_id);
252
253 //---------------------------------------------------------------------------------------------
254
255 end_page();
256
257 ?>