Additional fixes in write_customer() definition.
[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
24 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
25 {
26
27         $input_error = 0;
28
29         if (!is_numeric($_POST['DayNumber']))
30         {
31                 $input_error = 1;
32                 display_error( _("The number of days or the day in the following month must be numeric."));
33                 set_focus('DayNumber');
34         } 
35         elseif (strlen($_POST['terms']) == 0) 
36         {
37                 $input_error = 1;
38                 display_error( _("The Terms description must be entered."));
39                 set_focus('terms');
40         }
41         $early_days = input_num('early_days', 0);
42         if ($early_days) {
43                 if ($early_days >= $_POST['DayNumber']) {
44                         $input_error = 1;
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)
48                 {
49                         $input_error = 1;
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');
52                 } 
53         } else {
54                 if (input_num('early_discount', 0)) {
55                         $input_error = 1;
56                         display_error(_("Early payment days option should be positive and less than payment term days."));
57                         set_focus('early_days');
58                 }
59         }
60
61         if ($input_error != 1)
62         {
63         if ($selected_id != -1) 
64         {
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');
67         } 
68         else 
69         {
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');
72         }
73         //run the sql from either of the above possibilites
74                 display_notification($note);
75                 $Mode = 'RESET';
76         }
77 }
78
79 if ($Mode == 'Delete')
80 {
81         // PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
82         if (key_in_foreign_table($selected_id, 'debtors_master', 'payment_terms'))
83         {
84                 display_error(_("Cannot delete this payment term, because customer accounts have been created referring to this term."));
85         } 
86         else 
87         {
88                 if (key_in_foreign_table($selected_id, 'suppliers', 'payment_terms'))
89                 {
90                         display_error(_("Cannot delete this payment term, because supplier accounts have been created referring to this term"));
91                 } 
92                 else 
93                 {
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'));
97                 }
98         }
99         //end if payment terms used in customer or supplier accounts
100         $Mode = 'RESET';
101 }
102
103 if ($Mode == 'RESET')
104 {
105         $selected_id = -1;
106         $sav = get_post('show_inactive');
107         unset($_POST);
108         $_POST['show_inactive'] = $sav;
109 }
110 //-------------------------------------------------------------------------------------------------
111
112 $result = get_payment_terms_all(check_value('show_inactive'));
113
114 start_form();
115 start_table(TABLESTYLE);
116 $th = array(_("Description"), _("Type"), _("Due After/Days"), _("Early payment discount"),"", "");
117 inactive_control_column($th);
118 table_header($th);
119
120 $k = 0; //row colour counter
121 while ($myrow = db_fetch($result)) 
122 {
123         $days = $myrow['days'];
124         $percent = $myrow['early_discount'];
125
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"));
134     end_row();
135
136 }
137
138 inactive_control_row($th);
139 end_table(1);
140
141 //-------------------------------------------------------------------------------------------------
142 if (list_updated('type')) {
143         $Ajax->activate('edits');
144 }
145
146 div_start('edits');
147
148 start_table(TABLESTYLE2);
149
150 if ($selected_id != -1) 
151 {
152         if ($Mode == 'Edit') {
153                 //editing an existing payment terms
154                 $myrow = get_payment_terms($selected_id);
155
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'];
161         }
162         hidden('selected_id', $selected_id);
163 }
164
165 text_row(_("Terms Description:"), 'terms', null, 40, 40);
166
167 payment_type_list_row(_("Payment type:"), 'type', null, true);
168
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);
173 } else
174         hidden('DayNumber', 0);
175
176 end_table(1);
177 div_end();
178
179 submit_add_or_update_center($selected_id == -1, '', 'both');
180
181 end_form();
182
183 end_page();