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';
14 include($path_to_root . "/includes/session.inc");
16 page(_($help_context = "Payment Terms"));
18 include($path_to_root . "/includes/ui.inc");
20 simple_page_mode(true);
22 //------------------------------
23 // Helper to translate record content to more intuitive form
25 function term_days($myrow)
27 return $myrow["day_in_following_month"] != 0 ? $myrow["day_in_following_month"] :
28 $myrow["days_before_due"];
31 function term_type($myrow)
33 if ($myrow["day_in_following_month"] != 0)
36 $days = $myrow["days_before_due"];
38 return $days < 0 ? PTT_PRE : ($days ? PTT_DAYS : PTT_CASH);
41 //-------------------------------------------------------------------------------------------
43 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
48 if (!is_numeric($_POST['DayNumber']))
51 display_error( _("The number of days or the day in the following month must be numeric."));
52 set_focus('DayNumber');
54 elseif (strlen($_POST['terms']) == 0)
57 display_error( _("The Terms description must be entered."));
59 } // there should be no limits by 30 here if they want longer payment terms. Joe Hunt 2010-05-31
60 //elseif ($_POST['DayNumber'] > 30 && !check_value('DaysOrFoll'))
63 // 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."));
64 // set_focus('DayNumber');
66 elseif ($_POST['DayNumber'] > 500 && get_post('type')==PTT_DAYS)
69 display_error( _("When the Day of the Following Month is selected the number entered should be less than 500 days."));
70 set_focus('DayNumber');
73 if ($_POST['DayNumber'] == '')
74 $_POST['DayNumber'] = 0;
76 if ($input_error != 1)
78 $type = get_post('type');
79 $days = input_num('DayNumber');
80 $from_now = ($type != PTT_FOLLOWING);
81 if ($type == PTT_CASH)
86 if ($selected_id != -1)
88 update_payment_terms($selected_id, $from_now, $_POST['terms'], $days);
89 $note = _('Selected payment terms have been updated');
93 add_payment_terms($from_now, $_POST['terms'], $days);
94 $note = _('New payment terms have been added');
96 //run the sql from either of the above possibilites
97 display_notification($note);
102 if ($Mode == 'Delete')
104 // PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
105 if (key_in_foreign_table($selected_id, 'debtors_master', 'payment_terms'))
107 display_error(_("Cannot delete this payment term, because customer accounts have been created referring to this term."));
111 if (key_in_foreign_table($selected_id, 'suppliers', 'payment_terms'))
113 display_error(_("Cannot delete this payment term, because supplier accounts have been created referring to this term"));
117 //only delete if used in neither customer or supplier accounts
118 delete_payment_terms($selected_id);
119 display_notification(_('Selected payment terms have been deleted'));
122 //end if payment terms used in customer or supplier accounts
126 if ($Mode == 'RESET')
129 $sav = get_post('show_inactive');
131 $_POST['show_inactive'] = $sav;
133 //-------------------------------------------------------------------------------------------------
135 $result = get_payment_terms_all(check_value('show_inactive'));
138 start_table(TABLESTYLE);
139 $th = array(_("Description"), _("Type"), _("Due After/Days"), "", "");
140 inactive_control_column($th);
143 $k = 0; //row colour counter
144 while ($myrow = db_fetch($result))
147 alt_table_row_color($k);
148 $type = term_type($myrow);
149 $days = term_days($myrow);
150 label_cell($myrow["terms"]);
151 label_cell($pterm_types[$type]);
152 label_cell($type == PTT_DAYS ? "$days "._("days") : ($type == PTT_FOLLOWING ? $days : _("N/A")));
153 inactive_control_cell($myrow["terms_indicator"], $myrow["inactive"], 'payment_terms', "terms_indicator");
154 edit_button_cell("Edit".$myrow["terms_indicator"], _("Edit"));
155 delete_button_cell("Delete".$myrow["terms_indicator"], _("Delete"));
160 inactive_control_row($th);
163 //-------------------------------------------------------------------------------------------------
164 if (list_updated('type')) {
165 $Ajax->activate('edits');
170 start_table(TABLESTYLE2);
172 $day_in_following_month = $days_before_due = 0;
173 if ($selected_id != -1)
175 if ($Mode == 'Edit') {
176 //editing an existing payment terms
177 $myrow = get_payment_terms($selected_id);
179 $_POST['terms'] = $myrow["terms"];
180 $_POST['DayNumber'] = term_days($myrow);
181 $_POST['type'] = term_type($myrow);
183 hidden('selected_id', $selected_id);
186 text_row(_("Terms Description:"), 'terms', null, 40, 40);
188 payment_type_list_row(_("Payment type:"), 'type', null, true);
190 if ( in_array(get_post('type'), array(PTT_FOLLOWING, PTT_DAYS)))
191 text_row_ex(_("Days (Or Day In Following Month):"), 'DayNumber', 3);
193 hidden('DayNumber', 0);
198 submit_add_or_update_center($selected_id == -1, '', 'both');