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