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 //-------------------------------------------------------------------------------------------
24 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
29 if (!is_numeric($_POST['DayNumber']))
32 display_error( _("The number of days or the day in the following month must be numeric."));
33 set_focus('DayNumber');
35 elseif (strlen($_POST['terms']) == 0)
38 display_error( _("The Terms description must be entered."));
41 $early_days = input_num('early_days', 0);
43 if ($early_days >= $_POST['DayNumber']) {
45 display_error(_("Early payment days option should be shorter that payment term days."));
46 set_focus('early_days');
47 } else if (!check_num('early_discount', 0, 100) or input_num('early_discount') == 0)
50 display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
51 set_focus('early_discount');
54 if (input_num('early_discount', 0)) {
56 display_error(_("Early payment days option should be positive and less than payment term days."));
57 set_focus('early_days');
61 if ($input_error != 1)
63 if ($selected_id != -1)
65 update_payment_terms($selected_id, get_post('terms'), get_post('type'), input_num('DayNumber', 0), input_num('early_discount')/100, $early_days);
66 $note = _('Selected payment terms have been updated');
70 add_payment_terms(get_post('terms'), get_post('type'), input_num('DayNumber', 0), input_num('early_discount')/100, $early_days);
71 $note = _('New payment terms have been added');
73 //run the sql from either of the above possibilites
74 display_notification($note);
79 if ($Mode == 'Delete')
81 // PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
82 if (key_in_foreign_table($selected_id, 'debtors_master', 'payment_terms'))
84 display_error(_("Cannot delete this payment term, because customer accounts have been created referring to this term."));
88 if (key_in_foreign_table($selected_id, 'suppliers', 'payment_terms'))
90 display_error(_("Cannot delete this payment term, because supplier accounts have been created referring to this term"));
94 //only delete if used in neither customer or supplier accounts
95 delete_payment_terms($selected_id);
96 display_notification(_('Selected payment terms have been deleted'));
99 //end if payment terms used in customer or supplier accounts
103 if ($Mode == 'RESET')
106 $sav = get_post('show_inactive');
108 $_POST['show_inactive'] = $sav;
110 //-------------------------------------------------------------------------------------------------
112 $result = get_payment_terms_all(check_value('show_inactive'));
115 start_table(TABLESTYLE);
116 $th = array(_("Description"), _("Type"), _("Due After/Days"), _("Early payment discount"),"", "");
117 inactive_control_column($th);
120 $k = 0; //row colour counter
121 while ($myrow = db_fetch($result))
123 $days = $myrow['days'];
124 $percent = $myrow['early_discount'];
126 alt_table_row_color($k);
127 label_cell($myrow["terms"]);
128 label_cell($pterm_types[$myrow['type']]);
129 label_cell($myrow['type'] == PTT_DAYS ? "$days "._("days") : ($myrow['type'] == PTT_FOLLOWING ? $days : _("N/A")));
130 label_cell(in_array($myrow['type'], array(PTT_FOLLOWING, PTT_DAYS)) ? ($percent==0 ? _("None") : ($percent*100).'/'.$myrow['early_days']) : _("N/A"));
131 inactive_control_cell($myrow["id"], $myrow["inactive"], 'payment_terms', "id");
132 edit_button_cell("Edit".$myrow["id"], _("Edit"));
133 delete_button_cell("Delete".$myrow["id"], _("Delete"));
138 inactive_control_row($th);
141 //-------------------------------------------------------------------------------------------------
142 if (list_updated('type')) {
143 $Ajax->activate('edits');
148 start_table(TABLESTYLE2);
150 if ($selected_id != -1)
152 if ($Mode == 'Edit') {
153 //editing an existing payment terms
154 $myrow = get_payment_terms($selected_id);
156 $_POST['terms'] = $myrow["terms"];
157 $_POST['type'] = $myrow['type'];
158 $_POST['DayNumber'] = $myrow['days'];
159 $_POST['early_discount'] = $myrow['early_discount']*100;
160 $_POST['early_days'] = $myrow['early_days'];
162 hidden('selected_id', $selected_id);
165 text_row(_("Terms Description:"), 'terms', null, 40, 40);
167 payment_type_list_row(_("Payment type:"), 'type', null, true);
169 if ( in_array(get_post('type'), array(PTT_FOLLOWING, PTT_DAYS))) {
170 text_row_ex(_("Days (Or Day In Following Month):"), 'DayNumber', 3);
171 small_amount_row(_("Days of early payment discount option:"), 'early_days', null, null, _('days'), 0);
172 small_amount_row(_("Early payment discount percent:"), 'early_discount', null, null, _('%'), 1);
174 hidden('DayNumber', 0);
179 submit_add_or_update_center($selected_id == -1, '', 'both');