Merged latest changes from stable branch.
[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
20 $js = "";
21 if ($use_popup_windows)
22         $js .= get_js_open_window(900, 600);
23 if ($use_date_picker)
24         $js .= get_js_date_picker();
25
26 page(_($help_context = "Create and Print Recurrent Invoices"), false, false, "", $js);
27
28 function create_recurrent_invoices($customer_id, $branch_id, $order_no, $tmpl_no, $date, $from, $to)
29 {
30         global $Refs;
31
32         $doc = new Cart(ST_SALESORDER, array($order_no));
33
34         get_customer_details_to_order($doc, $customer_id, $branch_id);
35
36         $doc->trans_type = ST_SALESORDER;
37         $doc->trans_no = 0;
38         $doc->document_date = $date; 
39
40         $doc->due_date = get_invoice_duedate($doc->payment, $doc->document_date);
41         $doc->reference = $Refs->get_next($doc->trans_type);
42         $doc->Comments = sprintf(_("Recurrent Invoice covers period %s - %s."), $from, add_days($to, -1));
43
44         foreach ($doc->line_items as $line_no=>$item) {
45                 $line = &$doc->line_items[$line_no];
46                 $line->price = get_price($line->stock_id, $doc->customer_currency,
47                         $doc->sales_type, $doc->price_factor, $doc->document_date);
48         }       
49         $cart = $doc;
50         $cart->trans_type = ST_SALESINVOICE;
51         $cart->reference = $Refs->get_next($cart->trans_type);
52         $invno = $cart->write(1);
53         if ($invno == -1)
54         {
55                 display_error(_("The entered reference is already in use."));
56                 display_footer_exit();
57         }               
58         update_last_sent_recurrent_invoice($tmpl_no, $to);
59         return $invno;
60 }
61
62 function calculate_from($myrow)
63 {
64         if ($myrow["last_sent"] == '0000-00-00')
65                 $from = sql2date($myrow["begin"]);
66         else
67                 $from = sql2date($myrow["last_sent"]);
68         return $from;   
69 }
70
71 if (!isset($_POST['date'])) {
72   $_POST['date'] = Today();
73 }
74
75 $id = find_submit("create");
76 if ($id != -1)
77 {
78         $Ajax->activate('_page_body');
79         $date = $_POST['date'];
80         if (is_date_in_fiscalyear($date))
81         {
82                 $invs = array();
83                 $myrow = get_recurrent_invoice($id);
84                 $from = calculate_from($myrow);
85                 $to = add_months($from, $myrow['monthly']);
86                 $to = add_days($to, $myrow['days']);
87                 if ($myrow['debtor_no'] == 0)
88                 {
89                         $cust = get_cust_branches_from_group($myrow['group_no']);
90                         while ($row = db_fetch($cust))
91                         {
92                                 $invs[] = create_recurrent_invoices($row['debtor_no'], $row['branch_code'], $myrow['order_no'], $myrow['id'],
93                                         $date, $from, $to);
94                         }       
95                 }
96                 else
97                 {
98                         $invs[] = create_recurrent_invoices($myrow['debtor_no'], $myrow['group_no'], $myrow['order_no'], $myrow['id'],
99                                 $date, $from, $to);
100                 }
101                 if (count($invs) > 0)
102                 {
103                         $min = min($invs);
104                         $max = max($invs);
105                 }
106                 else 
107                         $min = $max = 0;
108                 display_notification(sprintf(_("%s recurrent invoice(s) created, # %s - # %s."), count($invs), $min, $max));
109                 if (count($invs) > 0)
110                 {
111                         $ar = array('PARAM_0' => $min."-".ST_SALESINVOICE,      'PARAM_1' => $max."-".ST_SALESINVOICE, 'PARAM_2' => "",
112                                 'PARAM_3' => 0, 'PARAM_4' => 0, 'PARAM_5' => "", 'PARAM_6' => ST_SALESINVOICE);
113                         display_note(print_link(sprintf(_("&Print Recurrent Invoices # %s - # %s"), $min, $max), 107, $ar), 0, 1);
114                         $ar['PARAM_3'] = 1; // email
115                         display_note(print_link(sprintf(_("&Email Recurrent Invoices # %s - # %s"), $min, $max), 107, $ar), 0, 1);
116                 }
117         }
118         else
119                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
120 }
121
122 $result = get_recurrent_invoices();
123
124 start_form();
125 start_table(TABLESTYLE_NOBORDER);
126 start_row();
127 date_cells(_("Invoice date:"), 'date', '');
128 end_row();
129 end_table();
130
131 start_table(TABLESTYLE, "width=70%");
132 $th = array(_("Description"), _("Template No"),_("Customer"),_("Branch")."/"._("Group"),_("Days"),_("Monthly"),_("Begin"),_("End"),_("Last Created"),"");
133 table_header($th);
134 $k = 0;
135 $today = add_days($_POST['date'], 1);
136 $due = false;
137 while ($myrow = db_fetch($result)) 
138 {
139         $begin = sql2date($myrow["begin"]);
140         $end = sql2date($myrow["end"]);
141         $last_sent = calculate_from($myrow);
142         $due_date = add_months($last_sent, $myrow['monthly']);
143         $due_date = add_days($due_date, $myrow['days']);
144
145         $overdue = date1_greater_date2($today, $due_date) && date1_greater_date2($today, $begin)
146                 && date1_greater_date2($end, $today);
147         if ($overdue)
148         {
149                 start_row("class='overduebg'");
150                 $due = true;
151         }       
152         else    
153                 alt_table_row_color($k);
154                 
155         label_cell($myrow["description"]);
156         label_cell(get_customer_trans_view_str(30, $myrow["order_no"]));
157         if ($myrow["debtor_no"] == 0)
158         {
159                 label_cell("");
160                 label_cell(get_sales_group_name($myrow["group_no"]));
161         }       
162         else
163         {
164                 label_cell(get_customer_name($myrow["debtor_no"]));
165                 label_cell(get_branch_name($myrow['group_no']));
166         }       
167         label_cell($myrow["days"]);
168         label_cell($myrow['monthly']);
169         label_cell($begin);
170         label_cell($end);
171         label_cell(($myrow['last_sent']=="0000-00-00")?"":$last_sent);
172         if ($overdue)
173                 button_cell("create".$myrow["id"], _("Create Invoices"), "", ICON_DOC);
174         else
175                 label_cell("");
176         end_row();
177 }
178 end_table();
179 end_form();
180 if ($due)
181         display_note(_("Marked items are due."), 1, 0, "class='overduefg'");
182 else
183         display_note(_("No recurrent invoices are due."), 1, 0);
184
185 br();
186
187 end_page();
188 ?>