Creating recurrent invoices with date outside fiscal year
[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 set_last_sent($id, $date)
27 {
28         $date = date2sql($date);
29         $sql = "UPDATE ".TB_PREF."recurrent_invoices SET last_sent='$date' WHERE id=".db_escape($id);
30         db_query($sql,"The recurrent invoice could not be updated or added");
31 }       
32
33 function create_recurrent_invoices($customer_id, $branch_id, $order_no, $tmpl_no)
34 {
35         global $Refs;
36
37         $doc = new Cart(ST_SALESORDER, array($order_no));
38
39         get_customer_details_to_order($doc, $customer_id, $branch_id);
40
41         $doc->trans_type = ST_SALESORDER;
42         $doc->trans_no = 0;
43         $doc->document_date = Today(); // 2006-06-15. Added so Invoices and Deliveries get current day
44
45         $doc->due_date = get_invoice_duedate($doc->customer_id, $doc->document_date);
46         $doc->reference = $Refs->get_next($doc->trans_type);
47         //$doc->Comments='';
48
49         foreach ($doc->line_items as $line_no=>$item) {
50                 $line = &$doc->line_items[$line_no];
51                 $line->price = get_price($line->stock_id, $doc->customer_currency,
52                         $doc->sales_type, $doc->price_factor, $doc->document_date);
53         }       
54         $cart = $doc;
55         $cart->trans_type = ST_SALESINVOICE;
56         $cart->reference = $Refs->get_next($cart->trans_type);
57         $invno = $cart->write(1);
58         set_last_sent($tmpl_no, $cart->document_date);
59         return $invno;
60 }
61
62 if (isset($_GET['recurrent']))
63 {
64         $date = Today();
65         if (is_date_in_fiscalyear($date))
66         {
67                 $invs = array();
68                 $sql = "SELECT * FROM ".TB_PREF."recurrent_invoices WHERE id=".db_escape($_GET['recurrent']);
69
70                 $result = db_query($sql,"could not get recurrent invoice");
71                 $myrow = db_fetch($result);
72                 if ($myrow['debtor_no'] == 0)
73                 {
74                         $cust = get_cust_branches_from_group($myrow['group_no']);
75                         while ($row = db_fetch($cust))
76                         {
77                                 $invs[] = create_recurrent_invoices($row['debtor_no'], $row['branch_code'], $myrow['order_no'], $myrow['id']);
78                         }       
79                 }
80                 else
81                 {
82                         $invs[] = create_recurrent_invoices($myrow['debtor_no'], $myrow['group_no'], $myrow['order_no'], $myrow['id']);
83                 }
84                 if (count($invs) > 0)
85                 {
86                         $min = min($invs);
87                         $max = max($invs);
88                 }
89                 else 
90                         $min = $max = 0;
91                 display_notification(sprintf(_("%s recurrent invoice(s) created, # $min - # $max."), count($invs)));
92                 if (count($invs) > 0)
93                 {
94                         $ar = array('PARAM_0' => $min,  'PARAM_1' => $max, 'PARAM_2' => "",
95                                 'PARAM_3' => 0, 'PARAM_4' => 0, 'PARAM_5' => "", 'PARAM_6' => ST_SALESINVOICE);
96                         display_note(print_link(_("&Print Recurrent Invoices # $min - # $max"), 107, $ar), 0, 1);
97                         $ar['PARAM_3'] = 1; 
98                         display_note(print_link(_("&Email Recurrent Invoices # $min - # $max"), 107, $ar), 0, 1);
99                 }
100         }
101         else
102                 display_error(_("The entered date is not in fiscal year."));
103 }       
104
105 //-------------------------------------------------------------------------------------------------
106 function get_sales_group_name($group_no)
107 {
108         $sql = "SELECT description FROM ".TB_PREF."groups WHERE id = ".db_escape($group_no);
109         $result = db_query($sql, "could not get group");
110         $row = db_fetch($result);
111         return $row[0];
112 }
113
114 $sql = "SELECT * FROM ".TB_PREF."recurrent_invoices ORDER BY description, group_no, debtor_no";
115 $result = db_query($sql,"could not get recurrent invoices");
116
117 start_table("$table_style width=70%");
118 $th = array(_("Description"), _("Template No"),_("Customer"),_("Branch")."/"._("Group"),_("Days"),_("Monthly"),_("Begin"),_("End"),_("Last Created"),"");
119 table_header($th);
120 $k = 0;
121 $today = add_days(Today(), 1);
122 $due = false;
123 while ($myrow = db_fetch($result)) 
124 {
125         $begin = sql2date($myrow["begin"]);
126         $end = sql2date($myrow["end"]);
127         $last_sent = sql2date($myrow["last_sent"]);
128         if ($myrow['monthly'] > 0)
129                 $due_date = begin_month($last_sent);
130         else
131                 $due_date = $last_sent;
132         $due_date = add_months($due_date, $myrow['monthly']);
133         $due_date = add_days($due_date, $myrow['days']);
134         $overdue = date1_greater_date2($today, $due_date) && date1_greater_date2($today, $begin)
135                 && date1_greater_date2($end, $today);
136         if ($overdue)
137         {
138                 start_row("class='overduebg'");
139                 $due = true;
140         }       
141         else    
142                 alt_table_row_color($k);
143                 
144         label_cell($myrow["description"]);
145         label_cell(get_customer_trans_view_str(30, $myrow["order_no"]));
146         if ($myrow["debtor_no"] == 0)
147         {
148                 label_cell("");
149                 label_cell(get_sales_group_name($myrow["group_no"]));
150         }       
151         else
152         {
153                 label_cell(get_customer_name($myrow["debtor_no"]));
154                 label_cell(get_branch_name($myrow['group_no']));
155         }       
156         label_cell($myrow["days"]);
157         label_cell($myrow['monthly']);
158         label_cell($begin);
159         label_cell($end);
160         label_cell($last_sent);
161         if ($overdue)
162                 label_cell("<a href='$path_to_root/sales/create_recurrent_invoices.php?recurrent=" . $myrow["id"] . "'>" . _("Create Invoices") . "</a>");
163         else
164                 label_cell("");
165         end_row();
166 }
167 end_table();
168 if ($due)
169         display_note(_("Marked items are due."), 1, 0, "class='overduefg'");
170 else
171         display_note(_("No recurrent invoices are due."), 1, 0);
172
173 echo '<br>';
174
175 end_page();
176 ?>