55c99864c892462085fc76e129c450527973377f
[fa-stable.git] / sales / manage / recurrent_invoices.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_SRECURRENT';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15 include($path_to_root . "/includes/ui.inc");
16 include_once($path_to_root . "/sales/includes/sales_db.inc");
17
18 $js = "";
19 if ($SysPrefs->use_popup_windows)
20         $js .= get_js_open_window(900, 600);
21 if (user_use_date_picker())
22         $js .= get_js_date_picker();
23
24 page(_($help_context = "Recurrent Invoices"), false, false, "", $js);
25
26 check_db_has_template_orders(_("There is no template order in database.
27         You have to create at least one sales order marked as template to be able to define recurrent invoices."));
28
29 simple_page_mode(true);
30
31 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
32 {
33
34         $input_error = 0;
35
36         if (!get_post('group_no'))
37         {
38                 $input_error = 1;
39                 if (get_post('debtor_no'))
40                         display_error(_("This customer has no branches. Please define at least one branch for this customer first."));
41                 else
42                         display_error(_("There are no tax groups defined in the system. At least one tax group is required before proceeding."));
43                 set_focus('debtor_no');
44         }
45         if (strlen($_POST['description']) == 0) 
46         {
47                 $input_error = 1;
48                 display_error(_("The invoice description cannot be empty."));
49                 set_focus('description');
50         }
51         if (!check_recurrent_invoice_description($_POST['description'], $selected_id))
52         {
53                 $input_error = 1;
54                 display_error(_("This recurrent invoice description is already in use."));
55                 set_focus('description');
56         }
57         if (!is_date($_POST['begin']))
58         {
59                 $input_error = 1;
60                 display_error(_("The entered date is invalid."));
61                 set_focus('begin');
62         }
63         if (!is_date($_POST['end']))
64         {
65                 $input_error = 1;
66                 display_error(_("The entered date is invalid."));
67                 set_focus('end');
68         }
69         if (isset($_POST['last_sent']) && !is_date($_POST['last_sent'])) {
70                 $input_error = 1;
71                 display_error(_("The entered date is invalid."));
72                 set_focus('last_sent');
73         }
74         if (!$_POST['days'] && !$_POST['monthly'])
75         {
76                 $input_error = 1;
77                 display_error(_("No recurence interval has been entered."));
78                 set_focus('days');
79         }
80
81         if ($input_error != 1)
82         {
83         if ($selected_id != -1) 
84         {
85                 update_recurrent_invoice($selected_id, $_POST['description'], $_POST['order_no'], input_num('debtor_no'), 
86                         input_num('group_no'), input_num('days', 0), input_num('monthly', 0), $_POST['begin'], $_POST['end']);
87                 if (isset($_POST['last_sent'])) 
88                                 update_last_sent_recurrent_invoice($selected_id, $_POST['last_sent']);
89                         $note = _('Selected recurrent invoice has been updated');
90         } 
91         else 
92         {
93                 add_recurrent_invoice($_POST['description'], $_POST['order_no'], input_num('debtor_no'), input_num('group_no'),
94                         input_num('days', 0), input_num('monthly', 0), $_POST['begin'], $_POST['end']);
95                         $note = _('New recurrent invoice has been added');
96         }
97     
98                 display_notification($note);
99                 $Mode = 'RESET';
100         }
101
102
103 if ($Mode == 'Delete')
104 {
105
106         $cancel_delete = 0;
107
108         if ($cancel_delete == 0) 
109         {
110                 delete_recurrent_invoice($selected_id);
111
112                 display_notification(_('Selected recurrent invoice has been deleted'));
113         } //end if Delete area
114         $Mode = 'RESET';
115
116
117 if ($Mode == 'RESET')
118 {
119         $selected_id = -1;
120         unset($_POST);
121 }
122 //-------------------------------------------------------------------------------------------------
123
124 $result = get_recurrent_invoices();
125
126 start_form();
127 start_table(TABLESTYLE, "width=70%");
128 $th = array(_("Description"), _("Template No"),_("Customer"),_("Branch")."/"._("Group"),_("Days"),_("Monthly"),_("Begin"),_("End"),_("Last Created"),"", "");
129 table_header($th);
130 $k = 0;
131 while ($myrow = db_fetch($result)) 
132 {
133         $begin = sql2date($myrow["begin"]);
134         $end = sql2date($myrow["end"]);
135         $last_sent = $myrow["last_sent"] == '0000-00-00' ? '' : sql2date($myrow["last_sent"]);
136         
137         alt_table_row_color($k);
138                 
139         label_cell($myrow["description"]);
140         label_cell(get_customer_trans_view_str(ST_SALESORDER, $myrow["order_no"]));
141         if ($myrow["debtor_no"] == 0)
142         {
143                 label_cell("");
144                 label_cell(get_sales_group_name($myrow["group_no"]));
145         }       
146         else
147         {
148                 label_cell(get_customer_name($myrow["debtor_no"]));
149                 label_cell(get_branch_name($myrow['group_no']));
150         }       
151         label_cell($myrow["days"]);
152         label_cell($myrow['monthly']);
153         label_cell($begin);
154         label_cell($end);
155         label_cell($last_sent);
156         edit_button_cell("Edit".$myrow["id"], _("Edit"));
157         delete_button_cell("Delete".$myrow["id"], _("Delete"));
158         end_row();
159 }
160 end_table();
161
162 end_form();
163 echo '<br>';
164
165 //-------------------------------------------------------------------------------------------------
166
167 start_form();
168
169 start_table(TABLESTYLE2);
170
171 if ($selected_id != -1) 
172 {
173         if ($Mode == 'Edit') {
174                 //editing an existing area
175                 $myrow = get_recurrent_invoice($selected_id);
176
177                 $_POST['description']  = $myrow["description"];
178                 $_POST['order_no']  = $myrow["order_no"];
179                 $_POST['debtor_no']  = $myrow["debtor_no"];
180                 $_POST['group_no']  = $myrow["group_no"];
181                 $_POST['days']  = $myrow["days"];
182                 $_POST['monthly']  = $myrow["monthly"];
183                 $_POST['begin']  = sql2date($myrow["begin"]);
184                 $_POST['end']  = sql2date($myrow["end"]);
185                 $_POST['last_sent']  = ($myrow['last_sent']=="0000-00-00"?"":sql2date($myrow["last_sent"]));
186         } 
187         hidden("selected_id", $selected_id);
188 }
189
190
191 text_row_ex(_("Description:"), 'description', 50); 
192
193 templates_list_row(_("Template:"), 'order_no');
194
195 customer_list_row(_("Customer:"), 'debtor_no', null, " ", true);
196
197 if ($_POST['debtor_no'] > 0)
198         customer_branches_list_row(_("Branch:"), $_POST['debtor_no'], 'group_no', null, false);
199 else    
200         sales_groups_list_row(_("Sales Group:"), 'group_no', null);
201
202 small_amount_row(_("Days:"), 'days', 0, null, null, 0);
203
204 small_amount_row(_("Monthly:"), 'monthly', 0, null, null, 0);
205
206 date_row(_("Begin:"), 'begin');
207
208 date_row(_("End:"), 'end', null, null, 0, 0, 5);
209
210 if ($selected_id != -1 && @$_POST['last_sent'] != "")
211         date_row(_("Last Created"), 'last_sent');
212
213 end_table(1);
214
215 submit_add_or_update_center($selected_id == -1, '', 'both');
216
217 end_form();
218
219 end_page();
220 ?>