Added company db upgrade boundary markers in error log.
[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 //      Helper to translate record content to more intuitive form
24 //
25 function term_days($myrow)
26 {
27         return $myrow["day_in_following_month"] != 0 ? $myrow["day_in_following_month"] :
28                 $myrow["days_before_due"];
29 }
30
31 function term_type($myrow)
32 {
33         if ($myrow["day_in_following_month"] != 0)
34                 return PTT_FOLLOWING;
35
36         $days = $myrow["days_before_due"];
37
38         return $days < 0 ? PTT_PRE : ($days ? PTT_DAYS : PTT_CASH);
39 }
40
41 //-------------------------------------------------------------------------------------------
42
43 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
44 {
45
46         $input_error = 0;
47
48         if (!is_numeric($_POST['DayNumber']))
49         {
50                 $input_error = 1;
51                 display_error( _("The number of days or the day in the following month must be numeric."));
52                 set_focus('DayNumber');
53         } 
54         elseif (strlen($_POST['terms']) == 0) 
55         {
56                 $input_error = 1;
57                 display_error( _("The Terms description must be entered."));
58                 set_focus('terms');
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')) 
61         //{
62         //      $input_error = 1;
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');
65         //} 
66         elseif ($_POST['DayNumber'] > 500 && get_post('type')==PTT_DAYS) 
67         {
68                 $input_error = 1;
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');
71         }
72
73         if ($_POST['DayNumber'] == '')
74                 $_POST['DayNumber'] = 0;
75
76         if ($input_error != 1)
77         {
78                 $type = get_post('type');
79                 $days = input_num('DayNumber');
80                 $from_now = ($type != PTT_FOLLOWING);
81                 if ($type == PTT_CASH)
82                         $days = 0;
83                 if ($type == PTT_PRE)
84                         $days = -1;
85
86         if ($selected_id != -1) 
87         {
88                 update_payment_terms($selected_id, $from_now, $_POST['terms'], $days); 
89                         $note = _('Selected payment terms have been updated');
90         } 
91         else 
92         {
93                         add_payment_terms($from_now, $_POST['terms'], $days);
94                         $note = _('New payment terms have been added');
95         }
96         //run the sql from either of the above possibilites
97                 display_notification($note);
98                 $Mode = 'RESET';
99         }
100 }
101
102 if ($Mode == 'Delete')
103 {
104         // PREVENT DELETES IF DEPENDENT RECORDS IN debtors_master
105         if (key_in_foreign_table($selected_id, 'debtors_master', 'payment_terms'))
106         {
107                 display_error(_("Cannot delete this payment term, because customer accounts have been created referring to this term."));
108         } 
109         else 
110         {
111                 if (key_in_foreign_table($selected_id, 'suppliers', 'payment_terms'))
112                 {
113                         display_error(_("Cannot delete this payment term, because supplier accounts have been created referring to this term"));
114                 } 
115                 else 
116                 {
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'));
120                 }
121         }
122         //end if payment terms used in customer or supplier accounts
123         $Mode = 'RESET';
124 }
125
126 if ($Mode == 'RESET')
127 {
128         $selected_id = -1;
129         $sav = get_post('show_inactive');
130         unset($_POST);
131         $_POST['show_inactive'] = $sav;
132 }
133 //-------------------------------------------------------------------------------------------------
134
135 $result = get_payment_terms_all(check_value('show_inactive'));
136
137 start_form();
138 start_table(TABLESTYLE);
139 $th = array(_("Description"), _("Type"), _("Due After/Days"), "", "");
140 inactive_control_column($th);
141 table_header($th);
142
143 $k = 0; //row colour counter
144 while ($myrow = db_fetch($result)) 
145 {
146
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"));
156     end_row();
157
158 }
159
160 inactive_control_row($th);
161 end_table(1);
162
163 //-------------------------------------------------------------------------------------------------
164 if (list_updated('type')) {
165         $Ajax->activate('edits');
166 }
167
168 div_start('edits');
169
170 start_table(TABLESTYLE2);
171
172 $day_in_following_month = $days_before_due = 0;
173 if ($selected_id != -1) 
174 {
175         if ($Mode == 'Edit') {
176                 //editing an existing payment terms
177                 $myrow = get_payment_terms($selected_id);
178
179                 $_POST['terms']  = $myrow["terms"];
180                 $_POST['DayNumber'] = term_days($myrow);
181                 $_POST['type'] = term_type($myrow);
182         }
183         hidden('selected_id', $selected_id);
184 }
185
186 text_row(_("Terms Description:"), 'terms', null, 40, 40);
187
188 payment_type_list_row(_("Payment type:"), 'type', null, true);
189
190 if ( in_array(get_post('type'), array(PTT_FOLLOWING, PTT_DAYS))) 
191         text_row_ex(_("Days (Or Day In Following Month):"), 'DayNumber', 3);
192 else
193         hidden('DayNumber', 0);
194
195 end_table(1);
196 div_end();
197
198 submit_add_or_update_center($selected_id == -1, '', 'both');
199
200 end_form();
201
202 end_page();
203
204 ?>