Replaced the global variables for table styles to defined CSS classes.
[fa-stable.git] / admin / payment_terms.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_PAYTERMS';
13 $path_to_root="..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "Payment Terms"));
17
18 include($path_to_root . "/includes/ui.inc");
19
20 simple_page_mode(true);
21 //-------------------------------------------------------------------------------------------
22
23 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
24 {
25
26         $inpug_error = 0;
27
28         if (!is_numeric($_POST['DayNumber']))
29         {
30                 $inpug_error = 1;
31                 display_error( _("The number of days or the day in the following month must be numeric."));
32                 set_focus('DayNumber');
33         } 
34         elseif (strlen($_POST['terms']) == 0) 
35         {
36                 $inpug_error = 1;
37                 display_error( _("The Terms description must be entered."));
38                 set_focus('terms');
39         } 
40         elseif ($_POST['DayNumber'] > 30 && !check_value('DaysOrFoll')) 
41         {
42                 $inpug_error = 1;
43                 display_error( _("When the check box to indicate a day in the following month is the due date, the due date cannot be a day after the 30th. A number between 1 and 30 is expected."));
44                 set_focus('DayNumber');
45         } 
46         elseif ($_POST['DayNumber'] > 500 && check_value('DaysOrFoll')) 
47         {
48                 $inpug_error = 1;
49                 display_error( _("When the check box is not checked to indicate that the term expects a number of days after which accounts are due, the number entered should be less than 500 days."));
50                 set_focus('DayNumber');
51         }
52
53         if ($_POST['DayNumber'] == '')
54                 $_POST['DayNumber'] = 0;
55
56         if ($inpug_error != 1)
57         {
58         if ($selected_id != -1) 
59         {
60                 update_payment_terms($selected_id, check_value('DaysOrFoll'), $_POST['terms'], $_POST['DayNumber']); 
61                         $note = _('Selected payment terms have been updated');
62         } 
63         else 
64         {
65                         add_payment_terms(check_value('DaysOrFoll'), $_POST['terms'], $_POST['DayNumber']);
66                         $note = _('New payment terms have been added');
67         }
68         //run the sql from either of the above possibilites
69                 display_notification($note);
70                 $Mode = 'RESET';
71         }
72 }
73
74 if ($Mode == 'Delete')
75 {
76         // PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
77         if (key_in_foreign_table($selected_id, 'debtors_master', 'payment_terms'))
78         {
79                 display_error(_("Cannot delete this payment term, because customer accounts have been created referring to this term."));
80         } 
81         else 
82         {
83                 if (key_in_foreign_table($selected_id, 'suppliers', 'payment_terms'))
84                 {
85                         display_error(_("Cannot delete this payment term, because supplier accounts have been created referring to this term"));
86                 } 
87                 else 
88                 {
89                         //only delete if used in neither customer or supplier accounts
90                         delete_payment_terms($selected_id);
91                         display_notification(_('Selected payment terms have been deleted'));
92                 }
93         }
94         //end if payment terms used in customer or supplier accounts
95         $Mode = 'RESET';
96 }
97
98 if ($Mode == 'RESET')
99 {
100         $selected_id = -1;
101         $sav = get_post('show_inactive');
102         unset($_POST);
103         $_POST['show_inactive'] = $sav;
104 }
105 //-------------------------------------------------------------------------------------------------
106
107 $result = get_payment_terms_all(check_value('show_inactive'));
108
109 start_form();
110 start_table(TABLESTYLE);
111 $th = array(_("Description"), _("Following Month On"), _("Due After (Days)"), "", "");
112 inactive_control_column($th);
113 table_header($th);
114
115 $k = 0; //row colour counter
116 while ($myrow = db_fetch($result)) 
117 {
118         if ($myrow["day_in_following_month"] == 0) 
119         {
120                 $full_text = _("N/A");
121         } 
122         else 
123         {
124                 $full_text = $myrow["day_in_following_month"];
125         }
126
127         if ($myrow["days_before_due"] == 0) 
128         {
129                 $after_text = _("N/A");
130         } 
131         else 
132         {
133                 $after_text = $myrow["days_before_due"] . " " . _("days");
134         }
135
136         alt_table_row_color($k);
137
138     label_cell($myrow["terms"]);
139     label_cell($full_text);
140     label_cell($after_text);
141         inactive_control_cell($myrow["terms_indicator"], $myrow["inactive"], 'payment_terms', "terms_indicator");
142         edit_button_cell("Edit".$myrow["terms_indicator"], _("Edit"));
143         delete_button_cell("Delete".$myrow["terms_indicator"], _("Delete"));
144     end_row();
145
146
147 } //END WHILE LIST LOOP
148
149 inactive_control_row($th);
150 end_table(1);
151
152 //-------------------------------------------------------------------------------------------------
153
154 start_table(TABLESTYLE2);
155
156 $day_in_following_month = $days_before_due = 0;
157 if ($selected_id != -1) 
158 {
159         if ($Mode == 'Edit') {
160                 //editing an existing payment terms
161                 $myrow = get_payment_terms($selected_id);
162
163                 $_POST['terms']  = $myrow["terms"];
164                 $days_before_due  = $myrow["days_before_due"];
165                 $day_in_following_month  = $myrow["day_in_following_month"];
166                 unset($_POST['DayNumber']);
167         }
168         hidden('selected_id', $selected_id);
169 }
170 text_row(_("Terms Description:"), 'terms', null, 40, 40);
171
172 check_row(_("Due After A Given No. Of Days:"), 'DaysOrFoll', $day_in_following_month == 0);
173
174 if (!isset($_POST['DayNumber'])) 
175 {
176     if ($days_before_due != 0)
177         $_POST['DayNumber'] = $days_before_due;
178     else
179         $_POST['DayNumber'] = $day_in_following_month;
180 }
181
182 text_row_ex(_("Days (Or Day In Following Month):"), 'DayNumber', 3);
183
184 end_table(1);
185
186 submit_add_or_update_center($selected_id == -1, '', 'both');
187
188 end_form();
189
190 end_page();
191
192 ?>