Temporary fixes for php encoding library bugs ending with segfault.
[fa-stable.git] / reporting / rep105.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_SALESBULKREP';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Order Status List
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/includes/data_checks.inc");
24 include_once($path_to_root . "/sales/includes/sales_db.inc");
25 include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");
26
27 //----------------------------------------------------------------------------------------------------
28
29 print_order_status_list();
30
31 //----------------------------------------------------------------------------------------------------
32
33 function GetSalesOrders($from, $to, $category=0, $location=null, $backorder=0)
34 {
35         $fromdate = date2sql($from);
36         $todate = date2sql($to);
37
38         $sql= "SELECT sorder.order_no,
39                                 sorder.debtor_no,
40                 sorder.branch_code,
41                 sorder.customer_ref,
42                 sorder.ord_date,
43                 sorder.from_stk_loc,
44                 sorder.delivery_date,
45                 line.stk_code,
46                 item.description,
47                 item.units,
48                 line.quantity,
49                 line.qty_sent
50             FROM ".TB_PREF."sales_orders sorder
51                         INNER JOIN ".TB_PREF."sales_order_details line
52                     ON sorder.order_no = line.order_no
53                     AND sorder.trans_type = line.trans_type
54                     AND sorder.trans_type = ".ST_SALESORDER."
55                 INNER JOIN ".TB_PREF."stock_master item
56                     ON line.stk_code = item.stock_id
57             WHERE sorder.ord_date >='$fromdate'
58                 AND sorder.ord_date <='$todate'";
59         if ($category > 0)
60                 $sql .= " AND item.category_id=".db_escape($category);
61         if ($location != null)
62                 $sql .= " AND sorder.from_stk_loc=".db_escape($location);
63         if ($backorder)
64                 $sql .= " AND line.quantity - line.qty_sent > 0";
65         $sql .= " ORDER BY sorder.order_no";
66
67         return db_query($sql, "Error getting order details");
68 }
69
70 //----------------------------------------------------------------------------------------------------
71
72 function print_order_status_list()
73 {
74         global $path_to_root;
75
76         $from = $_POST['PARAM_0'];
77         $to = $_POST['PARAM_1'];
78         $category = $_POST['PARAM_2'];
79         $location = $_POST['PARAM_3'];
80         $backorder = $_POST['PARAM_4'];
81         $comments = $_POST['PARAM_5'];
82         $orientation = $_POST['PARAM_6'];
83         $destination = $_POST['PARAM_7'];
84         if ($destination)
85                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
86         else
87                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
88         $orientation = ($orientation ? 'L' : 'P');
89
90         if ($category == ALL_NUMERIC)
91                 $category = 0;
92         if ($location == ALL_TEXT)
93                 $location = null;
94         if ($category == 0)
95                 $cat = _('All');
96         else
97                 $cat = get_category_name($category);
98         if ($location == null)
99                 $loc = _('All');
100         else
101                 $loc = get_location_name($location);
102         if ($backorder == 0)
103                 $back = _('All Orders');
104         else
105                 $back = _('Back Orders Only');
106
107         $cols = array(0, 60, 150, 260, 325,     385, 450, 515);
108
109         $headers2 = array(_('Order'), _('Customer'), _('Branch'), _('Customer Ref'),
110                 _('Ord Date'),  _('Del Date'),  _('Loc'));
111
112         $aligns = array('left', 'left', 'right', 'right', 'right', 'right',     'right');
113
114         $headers = array(_('Code'),     _('Description'), _('Ordered'), _('Delivered'),
115                 _('Outstanding'), '');
116
117     $params =   array(  0 => $comments,
118                                         1 => array(  'text' => _('Period'), 'from' => $from, 'to' => $to),
119                                         2 => array(  'text' => _('Category'), 'from' => $cat,'to' => ''),
120                                         3 => array(  'text' => _('Location'), 'from' => $loc, 'to' => ''),
121                                         4 => array(  'text' => _('Selection'),'from' => $back,'to' => ''));
122
123         $aligns2 = $aligns;
124
125         $rep = new FrontReport(_('Order Status Listing'), "OrderStatusListing", user_pagesize(), 9, $orientation);
126     if ($orientation == 'L')
127         recalculate_cols($cols);
128         $cols2 = $cols;
129         $rep->Font();
130         $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
131
132         $rep->NewPage();
133         $orderno = 0;
134
135         $result = GetSalesOrders($from, $to, $category, $location, $backorder);
136
137         while ($myrow=db_fetch($result))
138         {
139                 $rep->NewLine(0, 2, false, $orderno);
140                 if ($orderno != $myrow['order_no'])
141                 {
142                         if ($orderno != 0)
143                         {
144                                 $rep->Line($rep->row);
145                                 $rep->NewLine();
146                         }
147                         $rep->TextCol(0, 1,     $myrow['order_no']);
148                         $rep->TextCol(1, 2,     get_customer_name($myrow['debtor_no']));
149                         $rep->TextCol(2, 3,     get_branch_name($myrow['branch_code']));
150                         $rep->TextCol(3, 4,     $myrow['customer_ref']);
151                         $rep->DateCol(4, 5,     $myrow['ord_date'], true);
152                         $rep->DateCol(5, 6,     $myrow['delivery_date'], true);
153                         $rep->TextCol(6, 7,     $myrow['from_stk_loc']);
154                         $rep->NewLine(2);
155                         $orderno = $myrow['order_no'];
156                 }
157                 $rep->TextCol(0, 1,     $myrow['stk_code']);
158                 $rep->TextCol(1, 2,     $myrow['description']);
159                 $dec = get_qty_dec($myrow['stk_code']);
160                 $rep->AmountCol(2, 3, $myrow['quantity'], $dec);
161                 $rep->AmountCol(3, 4, $myrow['qty_sent'], $dec);
162                 $rep->AmountCol(4, 5, $myrow['quantity'] - $myrow['qty_sent'], $dec);
163                 if ($myrow['quantity'] - $myrow['qty_sent'] > 0)
164                 {
165                         $rep->Font('italic');
166                         $rep->TextCol(5, 6,     _('Outstanding'));
167                         $rep->Font();
168                 }
169                 $rep->NewLine();
170         }
171         $rep->Line($rep->row);
172         $rep->End();
173 }
174