Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[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 //      Helper to translate record content to more intuitive form
24 //
25 function term_days($myrow)
26 {
27         return $myrow["day_in_following_month"] != 0 ? $myrow["day_in_following_month"] :
28                 $myrow["days_before_due"];
29 }
30
31 function term_type($myrow)
32 {
33         if ($myrow["day_in_following_month"] != 0)
34                 return PTT_FOLLOWING;
35
36         $days = $myrow["days_before_due"];
37
38         return $days < 0 ? PTT_PRE : ($days ? PTT_DAYS : PTT_CASH);
39 }
40
41 //-------------------------------------------------------------------------------------------
42
43 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
44 {
45
46         $input_error = 0;
47
48         if (!is_numeric($_POST['DayNumber']))
49         {
50                 $input_error = 1;
51                 display_error( _("The number of days or the day in the following month must be numeric."));
52                 set_focus('DayNumber');
53         } 
54         elseif (strlen($_POST['terms']) == 0) 
55         {
56                 $input_error = 1;
57                 display_error( _("The Terms description must be entered."));
58                 set_focus('terms');
59         }
60
61         if ($_POST['DayNumber'] == '')
62                 $_POST['DayNumber'] = 0;
63
64         if ($input_error != 1)
65         {
66                 $type = get_post('type');
67                 $days = input_num('DayNumber');
68                 $from_now = ($type != PTT_FOLLOWING);
69                 if ($type == PTT_CASH)
70                         $days = 0;
71                 if ($type == PTT_PRE)
72                         $days = -1;
73
74         if ($selected_id != -1) 
75         {
76                 update_payment_terms($selected_id, $from_now, $_POST['terms'], $days); 
77                         $note = _('Selected payment terms have been updated');
78         } 
79         else 
80         {
81                         add_payment_terms($from_now, $_POST['terms'], $days);
82                         $note = _('New payment terms have been added');
83         }
84         //run the sql from either of the above possibilites
85                 display_notification($note);
86                 $Mode = 'RESET';
87         }
88 }
89
90 if ($Mode == 'Delete')
91 {
92         // PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
93         if (key_in_foreign_table($selected_id, 'debtors_master', 'payment_terms'))
94         {
95                 display_error(_("Cannot delete this payment term, because customer accounts have been created referring to this term."));
96         } 
97         else 
98         {
99                 if (key_in_foreign_table($selected_id, 'suppliers', 'payment_terms'))
100                 {
101                         display_error(_("Cannot delete this payment term, because supplier accounts have been created referring to this term"));
102                 } 
103                 else 
104                 {
105                         //only delete if used in neither customer or supplier accounts
106                         delete_payment_terms($selected_id);
107                         display_notification(_('Selected payment terms have been deleted'));
108                 }
109         }
110         //end if payment terms used in customer or supplier accounts
111         $Mode = 'RESET';
112 }
113
114 if ($Mode == 'RESET')
115 {
116         $selected_id = -1;
117         $sav = get_post('show_inactive');
118         unset($_POST);
119         $_POST['show_inactive'] = $sav;
120 }
121 //-------------------------------------------------------------------------------------------------
122
123 $result = get_payment_terms_all(check_value('show_inactive'));
124
125 start_form();
126 start_table(TABLESTYLE);
127 $th = array(_("Description"), _("Type"), _("Due After/Days"), "", "");
128 inactive_control_column($th);
129 table_header($th);
130
131 $k = 0; //row colour counter
132 while ($myrow = db_fetch($result)) 
133 {
134
135         alt_table_row_color($k);
136         $type = term_type($myrow);
137         $days = term_days($myrow);
138     label_cell($myrow["terms"]);
139     label_cell($pterm_types[$type]);
140     label_cell($type == PTT_DAYS ? "$days "._("days") : ($type == PTT_FOLLOWING ? $days : _("N/A")));
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
148 inactive_control_row($th);
149 end_table(1);
150
151 //-------------------------------------------------------------------------------------------------
152 if (list_updated('type')) {
153         $Ajax->activate('edits');
154 }
155
156 div_start('edits');
157
158 start_table(TABLESTYLE2);
159
160 $day_in_following_month = $days_before_due = 0;
161 if ($selected_id != -1) 
162 {
163         if ($Mode == 'Edit') {
164                 //editing an existing payment terms
165                 $myrow = get_payment_terms($selected_id);
166
167                 $_POST['terms']  = $myrow["terms"];
168                 $_POST['DayNumber'] = term_days($myrow);
169                 $_POST['type'] = term_type($myrow);
170         }
171         hidden('selected_id', $selected_id);
172 }
173
174 text_row(_("Terms Description:"), 'terms', null, 40, 40);
175
176 payment_type_list_row(_("Payment type:"), 'type', null, true);
177
178 if ( in_array(get_post('type'), array(PTT_FOLLOWING, PTT_DAYS))) 
179         text_row_ex(_("Days (Or Day In Following Month):"), 'DayNumber', 3);
180 else
181         hidden('DayNumber', 0);
182
183 end_table(1);
184 div_end();
185
186 submit_add_or_update_center($selected_id == -1, '', 'both');
187
188 end_form();
189
190 end_page();
191