eb36170e7a0827fee5d9495d51835340aaf8af38
[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                 return;
128         //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
129         
130         delete_currency($selected_id);
131         display_notification(_('Selected currency has been deleted'));
132         $Mode = 'RESET';
133 }
134
135 //---------------------------------------------------------------------------------------------
136
137 function display_currencies()
138 {
139         global $table_style;
140
141         $company_currency = get_company_currency();     
142         
143     $result = get_currencies();
144         start_form();    
145     start_table($table_style);
146     $th = array(_("Abbreviation"), _("Symbol"), _("Currency Name"),
147         _("Hundredths name"), _("Country"), "", "");
148     table_header($th);  
149     
150     $k = 0; //row colour counter
151     
152     while ($myrow = db_fetch($result)) 
153     {
154         
155         if ($myrow[1] == $company_currency) 
156         {
157                 start_row("class='currencybg'");
158         } 
159         else
160                 alt_table_row_color($k);
161                 
162         label_cell($myrow["curr_abrev"]);
163                 label_cell($myrow["curr_symbol"]);
164                 label_cell($myrow["currency"]);
165                 label_cell($myrow["hundreds_name"]);
166                 label_cell($myrow["country"]);
167                 edit_button_cell("Edit".$myrow["curr_abrev"], _("Edit"));
168                 if ($myrow["curr_abrev"] != $company_currency)
169                         edit_button_cell("Delete".$myrow["curr_abrev"], _("Delete"));
170                 else
171                         label_cell('');
172                 end_row();
173                 
174     } //END WHILE LIST LOOP
175     
176     end_table();
177         end_form();    
178     display_note(_("The marked currency is the home currency which cannot be deleted."), 0, 0, "class='currentfg'");
179 }
180
181 //---------------------------------------------------------------------------------------------
182
183 function display_currency_edit($selected_id)
184 {
185         global $table_style2, $Mode;
186         
187         start_form();
188         start_table($table_style2);
189
190         if ($selected_id != '') 
191         {
192                 if ($Mode == 'Edit') {
193                         //editing an existing currency
194                         $myrow = get_currency($selected_id);
195
196                         $_POST['Abbreviation'] = $myrow["curr_abrev"];
197                         $_POST['Symbol'] = $myrow["curr_symbol"];
198                         $_POST['CurrencyName']  = $myrow["currency"];
199                         $_POST['country']  = $myrow["country"];
200                         $_POST['hundreds_name']  = $myrow["hundreds_name"];
201                 }
202                 hidden('Abbreviation');
203                 hidden('selected_id', $selected_id);
204                 label_row(_("Currency Abbreviation:"), $_POST['Abbreviation']);
205         } 
206         else 
207         { 
208                 text_row_ex(_("Currency Abbreviation:"), 'Abbreviation', 4, 3);         
209         }
210
211         text_row_ex(_("Currency Symbol:"), 'Symbol', 10);
212         text_row_ex(_("Currency Name:"), 'CurrencyName', 20);
213         text_row_ex(_("Hundredths Name:"), 'hundreds_name', 15);        
214         text_row_ex(_("Country:"), 'country', 40);      
215
216         end_table(1);
217
218         submit_add_or_update_center($selected_id == '', '', true);
219
220         end_form();
221 }
222
223 //---------------------------------------------------------------------------------------------
224
225 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
226         handle_submit();
227
228 //--------------------------------------------------------------------------------------------- 
229
230 if ($Mode == 'Delete')
231         handle_delete();
232
233 //---------------------------------------------------------------------------------------------
234 if ($Mode == 'RESET')
235 {
236                 $selected_id = '';
237                 $_POST['Abbreviation'] = $_POST['Symbol'] = '';
238                 $_POST['CurrencyName'] = $_POST['country']  = '';
239                 $_POST['hundreds_name']  = '';
240 }
241
242 display_currencies();
243
244 display_currency_edit($selected_id);
245
246 //---------------------------------------------------------------------------------------------
247
248 end_page();
249
250 ?>