Do not print zero lines amount of 0.00 in Sales Documents if service item, $no_zero_l...
[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, $date, $from, $to)
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 = $date; 
37
38         $doc->due_date = get_invoice_duedate($doc->payment, $doc->document_date);
39         $doc->reference = $Refs->get_next($doc->trans_type);
40         $doc->Comments = sprintf(_("Recurrent Invoice covers period %s - %s."), $from, add_days($to, -1));
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, $to);
52         return $invno;
53 }
54
55 function calculate_from($myrow)
56 {
57         if ($myrow["last_sent"] == '0000-00-00')
58                 $from = sql2date($myrow["begin"]);
59         else
60                 $from = sql2date($myrow["last_sent"]);
61         return $from;   
62 }
63
64 $id = find_submit("create");
65 if ($id != -1)
66 {
67         $Ajax->activate('_page_body');
68         $date = Today();
69         if (is_date_in_fiscalyear($date))
70         {
71                 $invs = array();
72                 $myrow = get_recurrent_invoice($id);
73                 $from = calculate_from($myrow);
74                 $to = add_months($from, $myrow['monthly']);
75                 $to = add_days($to, $myrow['days']);
76                 if ($myrow['debtor_no'] == 0)
77                 {
78                         $cust = get_cust_branches_from_group($myrow['group_no']);
79                         while ($row = db_fetch($cust))
80                         {
81                                 $invs[] = create_recurrent_invoices($row['debtor_no'], $row['branch_code'], $myrow['order_no'], $myrow['id'],
82                                         $date, $from, $to);
83                         }       
84                 }
85                 else
86                 {
87                         $invs[] = create_recurrent_invoices($myrow['debtor_no'], $myrow['group_no'], $myrow['order_no'], $myrow['id'],
88                                 $date, $from, $to);
89                 }
90                 if (count($invs) > 0)
91                 {
92                         $min = min($invs);
93                         $max = max($invs);
94                 }
95                 else 
96                         $min = $max = 0;
97                 display_notification(sprintf(_("%s recurrent invoice(s) created, # %s - # %s."), count($invs), $min, $max));
98                 if (count($invs) > 0)
99                 {
100                         $ar = array('PARAM_0' => $min."-".ST_SALESINVOICE,      'PARAM_1' => $max."-".ST_SALESINVOICE, 'PARAM_2' => "",
101                                 'PARAM_3' => 0, 'PARAM_4' => 0, 'PARAM_5' => "", 'PARAM_6' => ST_SALESINVOICE);
102                         display_note(print_link(sprintf(_("&Print Recurrent Invoices # %s - # %s"), $min, $max), 107, $ar), 0, 1);
103                         $ar['PARAM_3'] = 1; // email
104                         display_note(print_link(sprintf(_("&Email Recurrent Invoices # %s - # %s"), $min, $max), 107, $ar), 0, 1);
105                 }
106         }
107         else
108                 display_error(_("The entered date is not in fiscal year."));
109 }
110
111 $result = get_recurrent_invoices();
112
113 start_form();
114 start_table(TABLESTYLE, "width=70%");
115 $th = array(_("Description"), _("Template No"),_("Customer"),_("Branch")."/"._("Group"),_("Days"),_("Monthly"),_("Begin"),_("End"),_("Last Created"),"");
116 table_header($th);
117 $k = 0;
118 $today = add_days(Today(), 1);
119 $due = false;
120 while ($myrow = db_fetch($result)) 
121 {
122         $begin = sql2date($myrow["begin"]);
123         $end = sql2date($myrow["end"]);
124         $last_sent = calculate_from($myrow);
125         $due_date = add_months($last_sent, $myrow['monthly']);
126         $due_date = add_days($due_date, $myrow['days']);
127
128         $overdue = date1_greater_date2($today, $due_date) && date1_greater_date2($today, $begin)
129                 && date1_greater_date2($end, $today);
130         if ($overdue)
131         {
132                 start_row("class='overduebg'");
133                 $due = true;
134         }       
135         else    
136                 alt_table_row_color($k);
137                 
138         label_cell($myrow["description"]);
139         label_cell(get_customer_trans_view_str(30, $myrow["order_no"]));
140         if ($myrow["debtor_no"] == 0)
141         {
142                 label_cell("");
143                 label_cell(get_sales_group_name($myrow["group_no"]));
144         }       
145         else
146         {
147                 label_cell(get_customer_name($myrow["debtor_no"]));
148                 label_cell(get_branch_name($myrow['group_no']));
149         }       
150         label_cell($myrow["days"]);
151         label_cell($myrow['monthly']);
152         label_cell($begin);
153         label_cell($end);
154         label_cell(($myrow['last_sent']=="0000-00-00")?"":$last_sent);
155         if ($overdue)
156                 button_cell("create".$myrow["id"], _("Create Invoices"), "", ICON_DOC);
157         else
158                 label_cell("");
159         end_row();
160 }
161 end_table();
162 end_form();
163 if ($due)
164         display_note(_("Marked items are due."), 1, 0, "class='overduefg'");
165 else
166         display_note(_("No recurrent invoices are due."), 1, 0);
167
168 br();
169
170 end_page();
171 ?>