fixed underline in db pager for sortable columns.
[fa-stable.git] / sales / create_recurrent_invoices.php
1 <?php
2
3
4 $page_security = 3;
5 $path_to_root="..";
6 include_once($path_to_root . "/sales/includes/cart_class.inc");
7 include_once($path_to_root . "/includes/session.inc");
8 include_once($path_to_root . "/sales/includes/ui/sales_order_ui.inc");
9 include_once($path_to_root . "/includes/ui.inc");
10 include_once($path_to_root . "/reporting/includes/reporting.inc");
11
12 $js = "";
13 if ($use_popup_windows)
14         $js .= get_js_open_window(900, 600);
15
16 page(_("Create and Print Recurrent Invoices"), false, false, "", $js);
17
18 function set_last_sent($id, $date)
19 {
20         $date = date2sql($date);
21         $sql = "UPDATE ".TB_PREF."recurrent_invoices SET last_sent='$date' WHERE id=$id";
22         db_query($sql,"The recurrent invoice could not be updated or added");
23 }       
24         
25 function create_recurrent_invoices($customer_id, $branch_id, $order_no, $tmpl_no)
26 {
27         $doc = new Cart(30, array($order_no));
28
29         get_customer_details_to_order($doc, $customer_id, $branch_id);
30
31         $doc->trans_type = 30;
32         $doc->trans_no = 0;
33         $doc->document_date = Today(); // 2006-06-15. Added so Invoices and Deliveries get current day
34
35         $doc->due_date = get_invoice_duedate($doc->customer_id, $doc->document_date);
36         $doc->reference = references::get_next($doc->trans_type);
37         $doc->Comments='';
38
39         foreach ($doc->line_items as $line_no=>$item) {
40                 $line = &$doc->line_items[$line_no];
41                 $line->price = get_price($line->stock_id, $doc->customer_currency,
42                         $doc->sales_type, $doc->price_factor, $doc->document_date);
43         }       
44         $cart = $doc;
45         $cart->trans_type = 10;
46         $cart->reference = references::get_next($cart->trans_type);
47         $invno = $cart->write(1);
48         set_last_sent($tmpl_no, $cart->document_date);
49         return $invno;
50 }
51
52 if (isset($_GET['recurrent']))
53 {
54         $invs = array();
55         $sql = "SELECT * FROM ".TB_PREF."recurrent_invoices WHERE id=".$_GET['recurrent'];
56
57         $result = db_query($sql,"could not get recurrent invoice");
58         $myrow = db_fetch($result);
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' => "", 'PARAM_3' => get_first_bank_account(), 
82                         'PARAM_4' => 0, 'PARAM_5' => 0, 'PARAM_6' => "", 'PARAM_7' => 10);
83                 display_note(print_link(_("&Print Recurrent Invoices # $min - # $max"), 107, $ar), 0, 1);
84         }
85 }       
86
87 //-------------------------------------------------------------------------------------------------
88 function get_sales_group_name($group_no)
89 {
90         $sql = "SELECT description FROM ".TB_PREF."groups WHERE id = $group_no";
91         $result = db_query($sql, "could not get group");
92         $row = db_fetch($result);
93         return $row[0];
94 }
95
96 $sql = "SELECT * FROM ".TB_PREF."recurrent_invoices ORDER BY description, group_no, debtor_no";
97 $result = db_query($sql,"could not get recurrent invoices");
98
99 start_table("$table_style width=70%");
100 $th = array(_("Description"), _("Template No"),_("Customer"),_("Branch")."/"._("Group"),_("Days"),_("Monthly"),_("Begin"),_("End"),_("Last Created"),"");
101 table_header($th);
102 $k = 0;
103 $today = add_days(Today(), 1);
104 $due = false;
105 while ($myrow = db_fetch($result)) 
106 {
107         $begin = sql2date($myrow["begin"]);
108         $end = sql2date($myrow["end"]);
109         $last_sent = sql2date($myrow["last_sent"]);
110         if ($myrow['monthly'] > 0)
111                 $due_date = begin_month($last_sent);
112         else
113                 $due_date = $last_sent;
114         $due_date = add_months($due_date, $myrow['monthly']);
115         $due_date = add_days($due_date, $myrow['days']);
116         $overdue = date1_greater_date2($today, $due_date) && date1_greater_date2($today, $begin)
117                 && date1_greater_date2($end, $today);
118         if ($overdue)
119         {
120                 start_row("class='overduebg'");
121                 $due = true;
122         }       
123         else    
124                 alt_table_row_color($k);
125                 
126         label_cell($myrow["description"]);
127         label_cell(get_customer_trans_view_str(30, $myrow["order_no"]));
128         if ($myrow["debtor_no"] == 0)
129         {
130                 label_cell("");
131                 label_cell(get_sales_group_name($myrow["group_no"]));
132         }       
133         else
134         {
135                 label_cell(get_customer_name($myrow["debtor_no"]));
136                 label_cell(get_branch_name($myrow['group_no']));
137         }       
138         label_cell($myrow["days"]);
139         label_cell($myrow['monthly']);
140         label_cell($begin);
141         label_cell($end);
142         label_cell($last_sent);
143         if ($overdue)
144                 label_cell("<a href='$path_to_root/sales/create_recurrent_invoices.php?recurrent=" . $myrow["id"] . "'>" . _("Create Invoices") . "</a>");
145         else
146                 label_cell("");
147         end_row();
148 }
149 end_table();
150 if ($due)
151         display_note(_("Marked items are due."), 1, 0, "class='overduefg'");
152 else
153         display_note(("No recurrent invoices are due."), 1, 0);
154
155 echo '<br>';
156
157 end_page();
158 ?>