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