Old ineffective sql_trail superseded by new improved db_trail logging only calls...
[fa-stable.git] / sales / create_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_SALESINVOICE';
13 $path_to_root = "..";
14 include_once($path_to_root . "/sales/includes/cart_class.inc");
15 include_once($path_to_root . "/includes/session.inc");
16 include_once($path_to_root . "/sales/includes/ui/sales_order_ui.inc");
17 include_once($path_to_root . "/includes/ui.inc");
18 include_once($path_to_root . "/reporting/includes/reporting.inc");
19 include_once($path_to_root . "/sales/includes/sales_db.inc");
20
21 $js = "";
22 if ($SysPrefs->use_popup_windows)
23         $js .= get_js_open_window(900, 600);
24 if (user_use_date_picker())
25         $js .= get_js_date_picker();
26
27 page(_($help_context = "Create and Print Recurrent Invoices"), false, false, "", $js);
28
29 function calculate_from($myrow)
30 {
31         if ($myrow["last_sent"] == '0000-00-00')
32                 $from = sql2date($myrow["begin"]);
33         else
34                 $from = sql2date($myrow["last_sent"]);
35         return $from;
36 }
37
38 function calculate_next($myrow)
39 {
40         if ($myrow["last_sent"] == '0000-00-00')
41                 $next = sql2date($myrow["begin"]);
42         else
43                 $next = sql2date($myrow["last_sent"]);
44         $next = add_months($next, $myrow['monthly']);
45         $next = add_days($next, $myrow['days']);
46         return add_days($next,-1);
47 }
48
49 $id = find_submit("confirmed");
50 if ($id != -1 && is_date_closed($_POST['trans_date']))
51 {
52         display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
53         set_focus('trans_date');
54         $_POST['create'.$id] = 1;       //re-display current page
55         $id = -1;
56 }
57
58 if ($id != -1)
59 {
60         $Ajax->activate('_page_body');
61         $invs = create_recurrent_invoices($id, get_post('from'), get_post('to'), get_post('trans_date'), get_post('memo'));
62
63         if (count($invs) > 0)
64         {
65                 $min = min($invs);
66                 $max = max($invs);
67         }
68         else 
69                 $min = $max = 0;
70         display_notification(sprintf(_("%s recurrent invoice(s) created, # %s - # %s."), count($invs), $min, $max));
71         if (count($invs) > 0)
72         {
73                 $ar = array('PARAM_0' => $min."-".ST_SALESINVOICE,      'PARAM_1' => $max."-".ST_SALESINVOICE, 'PARAM_2' => "",
74                         'PARAM_3' => 0, 'PARAM_4' => 0, 'PARAM_5' => "", 'PARAM_6' => "", 'PARAM_7' => user_def_print_orientation());
75                 display_note(print_link(sprintf(_("&Print Recurrent Invoices # %s - # %s"), $min, $max), 107, $ar), 0, 1);
76                 $ar['PARAM_3'] = 1; // email
77                 display_note(print_link(sprintf(_("&Email Recurrent Invoices # %s - # %s"), $min, $max), 107, $ar), 0, 1);
78         }
79 }
80
81
82
83 $id = find_submit('create');
84 if ($id != -1)
85 {
86         $Ajax->activate('_page_body');
87         $date = Today();
88         $myrow = get_recurrent_invoice($id);
89         $from = calculate_from($myrow);
90         $to = add_months($from, $myrow['monthly']);
91         $to = add_days($to, $myrow['days']);
92
93         if (!is_date_in_fiscalyear($date))
94                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
95         elseif (!date1_greater_date2(add_days(Today(), 1), $to))
96                 display_error(_("Recurrent invoice cannot be generated before last day of covered period."));
97         elseif (check_recurrent_invoice_prices($id))
98                 display_error(_("Recurrent invoices cannot be generated because some items have no price defined in customer currency."));
99         elseif (!check_sales_order_type($myrow['order_no']))
100                 display_error(_("Recurrent invoices cannot be generated because selected sales order template uses prepayment sales terms. Change payment terms and try again."));
101         else {
102                 $count = recurrent_invoice_count($id);
103
104                 $_POST['trans_date'] = $to;
105                 start_form();
106                 start_table(TABLESTYLE, "width=50%");
107                 label_row(_('Description:'), $myrow["description"]);
108                 label_row(_('Template:'), get_customer_trans_view_str(ST_SALESORDER, $myrow["order_no"]));
109                 label_row(_('Number of invoices:'), $count);
110                 date_row(_('Invoice date:'), 'trans_date');
111                 text_row(_('Invoice notice:'), 'memo', sprintf(_("Recurrent Invoice covers period %s - %s."), $from, add_days($to, -1)),
112                         100, 100);
113                 end_table();
114                 hidden('from', $from, true);
115                 hidden('to', $to, true);
116                 br();
117                 submit_center_first('confirmed'.$id, _('Create'), _('Create recurrent invoices'), false, ICON_OK);
118                 submit_center_last('cancel', _('Cancel'), _('Return to recurrent invoices'), false, ICON_ESCAPE);
119                 submit_js_confirm("do_create".$id, sprintf(_("You are about to issue %s invoices.\n Do you want to continue?"), $count));
120                 end_form();
121
122                 display_footer_exit();
123         }
124 }
125 else
126 {
127 $result = get_recurrent_invoices(Today());
128
129 start_form();
130 start_table(TABLESTYLE, "width=70%");
131 $th = array(_("Description"), _("Template No"),_("Customer"),_("Branch")."/"._("Group"),_("Days"),_("Monthly"),_("Begin"),_("End"),_("Next invoice"),"");
132 table_header($th);
133 $k = 0;
134 $due = false;
135 while ($myrow = db_fetch($result)) 
136 {
137         if ($myrow['overdue'])
138         {
139                 start_row("class='overduebg'");
140                 $due = true;
141         }
142         else
143                 alt_table_row_color($k);
144
145         label_cell($myrow["description"]);
146         label_cell(get_customer_trans_view_str(ST_SALESORDER, $myrow["order_no"]), "nowrap align='right'");
147         if ($myrow["debtor_no"] == 0)
148         {
149                 label_cell("");
150
151                 label_cell(get_sales_group_name($myrow["group_no"]));
152         }
153         else
154         {
155                 label_cell(get_customer_name($myrow["debtor_no"]));
156                 label_cell(get_branch_name($myrow['group_no']));
157         }
158         label_cell($myrow["days"]);
159         label_cell($myrow['monthly']);
160         label_cell(sql2date($myrow['begin']),  "align='center'");
161         label_cell(sql2date($myrow['end']),  "align='center'");
162         label_cell(calculate_next($myrow),  "align='center'");
163         if ($myrow['overdue'])
164         {
165                 $count = recurrent_invoice_count($myrow['id']);
166                 if ($count)
167                 {
168                         button_cell("create".$myrow["id"], sprintf(_("Create %s Invoice(s)"), $count), "", ICON_DOC, 'process');
169                 } else {
170                         label_cell('');
171                 }
172         }
173         else
174                 label_cell("");
175         end_row();
176 }
177 end_table();
178 end_form();
179 if ($due)
180         display_note(_("Marked items are due."), 1, 0, "class='overduefg'");
181 else
182         display_note(_("No recurrent invoices are due."), 1, 0);
183
184 br();
185 }
186 end_page();