Moved all SQL statements from PHP files into relevant *_db.inc files.
[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
24 page(_($help_context = "Create and Print Recurrent Invoices"), false, false, "", $js);
25
26 function create_recurrent_invoices($customer_id, $branch_id, $order_no, $tmpl_no)
27 {
28         global $Refs;
29
30         $doc = new Cart(ST_SALESORDER, array($order_no));
31
32         get_customer_details_to_order($doc, $customer_id, $branch_id);
33
34         $doc->trans_type = ST_SALESORDER;
35         $doc->trans_no = 0;
36         $doc->document_date = Today(); // 2006-06-15. Added so Invoices and Deliveries get current day
37
38         $doc->due_date = get_invoice_duedate($doc->customer_id, $doc->document_date);
39         $doc->reference = $Refs->get_next($doc->trans_type);
40         //$doc->Comments='';
41
42         foreach ($doc->line_items as $line_no=>$item) {
43                 $line = &$doc->line_items[$line_no];
44                 $line->price = get_price($line->stock_id, $doc->customer_currency,
45                         $doc->sales_type, $doc->price_factor, $doc->document_date);
46         }       
47         $cart = $doc;
48         $cart->trans_type = ST_SALESINVOICE;
49         $cart->reference = $Refs->get_next($cart->trans_type);
50         $invno = $cart->write(1);
51         update_last_sent_recurrent_invoice($tmpl_no, $cart->document_date);
52         return $invno;
53 }
54
55 if (isset($_GET['recurrent']))
56 {
57         $invs = array();
58         $myrow = get_recurrent_invoice($_GET['recurrent']);
59         if ($myrow['debtor_no'] == 0)
60         {
61                 $cust = get_cust_branches_from_group($myrow['group_no']);
62                 while ($row = db_fetch($cust))
63                 {
64                         $invs[] = create_recurrent_invoices($row['debtor_no'], $row['branch_code'], $myrow['order_no'], $myrow['id']);
65                 }       
66         }
67         else
68         {
69                 $invs[] = create_recurrent_invoices($myrow['debtor_no'], $myrow['group_no'], $myrow['order_no'], $myrow['id']);
70         }
71         if (count($invs) > 0)
72         {
73                 $min = min($invs);
74                 $max = max($invs);
75         }
76         else 
77                 $min = $max = 0;
78         display_notification(sprintf(_("%s recurrent invoice(s) created, # $min - # $max."), count($invs)));
79         if (count($invs) > 0)
80         {
81                 $ar = array('PARAM_0' => $min,  'PARAM_1' => $max, 'PARAM_2' => "",
82                         'PARAM_3' => 0, 'PARAM_4' => 0, 'PARAM_5' => "", 'PARAM_6' => ST_SALESINVOICE);
83                 display_note(print_link(_("&Print Recurrent Invoices # $min - # $max"), 107, $ar), 0, 1);
84                 $ar['PARAM_3'] = 1; 
85                 display_note(print_link(_("&Email Recurrent Invoices # $min - # $max"), 107, $ar), 0, 1);
86         }
87 }       
88
89 $result = get_recurrent_invoices();
90
91 start_table("$table_style width=70%");
92 $th = array(_("Description"), _("Template No"),_("Customer"),_("Branch")."/"._("Group"),_("Days"),_("Monthly"),_("Begin"),_("End"),_("Last Created"),"");
93 table_header($th);
94 $k = 0;
95 $today = add_days(Today(), 1);
96 $due = false;
97 while ($myrow = db_fetch($result)) 
98 {
99         $begin = sql2date($myrow["begin"]);
100         $end = sql2date($myrow["end"]);
101         $last_sent = sql2date($myrow["last_sent"]);
102         if ($myrow['monthly'] > 0)
103                 $due_date = begin_month($last_sent);
104         else
105                 $due_date = $last_sent;
106         $due_date = add_months($due_date, $myrow['monthly']);
107         $due_date = add_days($due_date, $myrow['days']);
108         $overdue = date1_greater_date2($today, $due_date) && date1_greater_date2($today, $begin)
109                 && date1_greater_date2($end, $today);
110         if ($overdue)
111         {
112                 start_row("class='overduebg'");
113                 $due = true;
114         }       
115         else    
116                 alt_table_row_color($k);
117                 
118         label_cell($myrow["description"]);
119         label_cell(get_customer_trans_view_str(30, $myrow["order_no"]));
120         if ($myrow["debtor_no"] == 0)
121         {
122                 label_cell("");
123                 label_cell(get_sales_group_name($myrow["group_no"]));
124         }       
125         else
126         {
127                 label_cell(get_customer_name($myrow["debtor_no"]));
128                 label_cell(get_branch_name($myrow['group_no']));
129         }       
130         label_cell($myrow["days"]);
131         label_cell($myrow['monthly']);
132         label_cell($begin);
133         label_cell($end);
134         label_cell($last_sent);
135         if ($overdue)
136                 label_cell("<a href='$path_to_root/sales/create_recurrent_invoices.php?recurrent=" . $myrow["id"] . "'>" . _("Create Invoices") . "</a>");
137         else
138                 label_cell("");
139         end_row();
140 }
141 end_table();
142 if ($due)
143         display_note(_("Marked items are due."), 1, 0, "class='overduefg'");
144 else
145         display_note(_("No recurrent invoices are due."), 1, 0);
146
147 br();
148
149 end_page();
150 ?>