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."));
61 if ($_POST['DayNumber'] == '')
62 $_POST['DayNumber'] = 0;
64 if ($input_error != 1)
66 $type = get_post('type');
67 $days = input_num('DayNumber');
68 $from_now = ($type != PTT_FOLLOWING);
69 if ($type == PTT_CASH)
74 if ($selected_id != -1)
76 update_payment_terms($selected_id, $from_now, $_POST['terms'], $days);
77 $note = _('Selected payment terms have been updated');
81 add_payment_terms($from_now, $_POST['terms'], $days);
82 $note = _('New payment terms have been added');
84 //run the sql from either of the above possibilites
85 display_notification($note);
90 if ($Mode == 'Delete')
92 // PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
93 if (key_in_foreign_table($selected_id, 'debtors_master', 'payment_terms'))
95 display_error(_("Cannot delete this payment term, because customer accounts have been created referring to this term."));
99 if (key_in_foreign_table($selected_id, 'suppliers', 'payment_terms'))
101 display_error(_("Cannot delete this payment term, because supplier accounts have been created referring to this term"));
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'));
110 //end if payment terms used in customer or supplier accounts
114 if ($Mode == 'RESET')
117 $sav = get_post('show_inactive');
119 $_POST['show_inactive'] = $sav;
121 //-------------------------------------------------------------------------------------------------
123 $result = get_payment_terms_all(check_value('show_inactive'));
126 start_table(TABLESTYLE);
127 $th = array(_("Description"), _("Type"), _("Due After/Days"), "", "");
128 inactive_control_column($th);
131 $k = 0; //row colour counter
132 while ($myrow = db_fetch($result))
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"));
148 inactive_control_row($th);
151 //-------------------------------------------------------------------------------------------------
152 if (list_updated('type')) {
153 $Ajax->activate('edits');
158 start_table(TABLESTYLE2);
160 $day_in_following_month = $days_before_due = 0;
161 if ($selected_id != -1)
163 if ($Mode == 'Edit') {
164 //editing an existing payment terms
165 $myrow = get_payment_terms($selected_id);
167 $_POST['terms'] = $myrow["terms"];
168 $_POST['DayNumber'] = term_days($myrow);
169 $_POST['type'] = term_type($myrow);
171 hidden('selected_id', $selected_id);
174 text_row(_("Terms Description:"), 'terms', null, 40, 40);
176 payment_type_list_row(_("Payment type:"), 'type', null, true);
178 if ( in_array(get_post('type'), array(PTT_FOLLOWING, PTT_DAYS)))
179 text_row_ex(_("Days (Or Day In Following Month):"), 'DayNumber', 3);
181 hidden('DayNumber', 0);
186 submit_add_or_update_center($selected_id == -1, '', 'both');