1412c372c90c8bc5bb3b6b8b3542c2dc8de87cb6
[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
42         if ($_POST['DayNumber'] == '')
43                 $_POST['DayNumber'] = 0;
44
45         if ($input_error != 1)
46         {
47
48         if ($selected_id != -1) 
49         {
50                 update_payment_terms($selected_id, get_post('terms'), get_post('type'), input_num('DayNumber', 0)); 
51                         $note = _('Selected payment terms have been updated');
52         } 
53         else 
54         {
55                         add_payment_terms(get_post('terms'), get_post('type'), input_num('DayNumber', 0));
56                         $note = _('New payment terms have been added');
57         }
58         //run the sql from either of the above possibilites
59                 display_notification($note);
60                 $Mode = 'RESET';
61         }
62 }
63
64 if ($Mode == 'Delete')
65 {
66         // PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
67         if (key_in_foreign_table($selected_id, 'debtors_master', 'payment_terms'))
68         {
69                 display_error(_("Cannot delete this payment term, because customer accounts have been created referring to this term."));
70         } 
71         else 
72         {
73                 if (key_in_foreign_table($selected_id, 'suppliers', 'payment_terms'))
74                 {
75                         display_error(_("Cannot delete this payment term, because supplier accounts have been created referring to this term"));
76                 } 
77                 else 
78                 {
79                         //only delete if used in neither customer or supplier accounts
80                         delete_payment_terms($selected_id);
81                         display_notification(_('Selected payment terms have been deleted'));
82                 }
83         }
84         //end if payment terms used in customer or supplier accounts
85         $Mode = 'RESET';
86 }
87
88 if ($Mode == 'RESET')
89 {
90         $selected_id = -1;
91         $sav = get_post('show_inactive');
92         unset($_POST);
93         $_POST['show_inactive'] = $sav;
94 }
95 //-------------------------------------------------------------------------------------------------
96
97 $result = get_payment_terms_all(check_value('show_inactive'));
98
99 start_form();
100 start_table(TABLESTYLE);
101 $th = array(_("Description"), _("Type"), _("Due After/Days"), "", "");
102 inactive_control_column($th);
103 table_header($th);
104
105 $k = 0; //row colour counter
106 while ($myrow = db_fetch($result)) 
107 {
108         $days = $myrow['days'];
109         alt_table_row_color($k);
110     label_cell($myrow["terms"]);
111     label_cell($pterm_types[$myrow['type']]);
112     label_cell($myrow['type'] == PTT_DAYS ? "$days "._("days") : ($myrow['type'] == PTT_FOLLOWING ? $days : _("N/A")));
113         inactive_control_cell($myrow["id"], $myrow["inactive"], 'payment_terms', "id");
114         edit_button_cell("Edit".$myrow["id"], _("Edit"));
115         delete_button_cell("Delete".$myrow["id"], _("Delete"));
116     end_row();
117
118 }
119
120 inactive_control_row($th);
121 end_table(1);
122
123 //-------------------------------------------------------------------------------------------------
124 if (list_updated('type')) {
125         $Ajax->activate('edits');
126 }
127
128 div_start('edits');
129
130 start_table(TABLESTYLE2);
131
132 if ($selected_id != -1) 
133 {
134         if ($Mode == 'Edit') {
135                 //editing an existing payment terms
136                 $myrow = get_payment_terms($selected_id);
137
138                 $_POST['terms']  = $myrow["terms"];
139                 $_POST['type'] = $myrow['type'];
140                 $_POST['DayNumber'] = $myrow['days'];
141         }
142         hidden('selected_id', $selected_id);
143 }
144
145 text_row(_("Terms Description:"), 'terms', null, 40, 40);
146
147 payment_type_list_row(_("Payment type:"), 'type', null, true);
148
149 if ( in_array(get_post('type'), array(PTT_FOLLOWING, PTT_DAYS))) 
150         text_row_ex(_("Days (Or Day In Following Month):"), 'DayNumber', 3);
151 else
152         hidden('DayNumber', 0);
153
154 end_table(1);
155 div_end();
156
157 submit_add_or_update_center($selected_id == -1, '', 'both');
158
159 end_form();
160
161 end_page();
162