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