[0004212] Work Order Entry: fixed error when voided WO refence is reused.
[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                 sorder.total,
46                 line.stk_code,
47                 item.description,
48                 item.units,
49                 line.quantity,
50                 line.qty_sent
51             FROM ".TB_PREF."sales_orders sorder
52                         INNER JOIN ".TB_PREF."sales_order_details line
53                     ON sorder.order_no = line.order_no
54                     AND sorder.trans_type = line.trans_type
55                     AND sorder.trans_type = ".ST_SALESORDER."
56                 INNER JOIN ".TB_PREF."stock_master item
57                     ON line.stk_code = item.stock_id
58             WHERE sorder.ord_date >='$fromdate'
59                 AND sorder.ord_date <='$todate'";
60         if ($category > 0)
61                 $sql .= " AND item.category_id=".db_escape($category);
62         if ($location != null)
63                 $sql .= " AND sorder.from_stk_loc=".db_escape($location);
64         if ($backorder)
65                 $sql .= " AND line.quantity - line.qty_sent > 0";
66         $sql .= " ORDER BY sorder.order_no";
67
68         return db_query($sql, "Error getting order details");
69 }
70
71 //----------------------------------------------------------------------------------------------------
72
73 function print_order_status_list()
74 {
75         global $path_to_root;
76
77         $from = $_POST['PARAM_0'];
78         $to = $_POST['PARAM_1'];
79         $category = $_POST['PARAM_2'];
80         $location = $_POST['PARAM_3'];
81         $backorder = $_POST['PARAM_4'];
82         $comments = $_POST['PARAM_5'];
83         $orientation = $_POST['PARAM_6'];
84         $destination = $_POST['PARAM_7'];
85         if ($destination)
86                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
87         else
88                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
89         $orientation = ($orientation ? 'L' : 'P');
90
91         if ($category == ALL_NUMERIC)
92                 $category = 0;
93         if ($location == ALL_TEXT)
94                 $location = null;
95         if ($category == 0)
96                 $cat = _('All');
97         else
98                 $cat = get_category_name($category);
99         if ($location == null)
100                 $loc = _('All');
101         else
102                 $loc = get_location_name($location);
103         if ($backorder == 0)
104                 $back = _('All Orders');
105         else
106                 $back = _('Back Orders Only');
107
108         $cols = array(0, 60, 150, 260, 325,     385, 450, 515);
109
110         $headers2 = array(_('Order'), _('Customer'), _('Branch'), _('Customer Ref'),
111                 _('Ord Date'),  _('Del Date'),  _('Loc'));
112
113         $aligns = array('left', 'left', 'right', 'right', 'right', 'right',     'right');
114
115         $headers = array(_('Code'),     _('Description'), _('Ordered'), _('Delivered'),
116                 _('Outstanding'), '', _('Total Amount'));
117
118     $params =   array(  0 => $comments,
119                                         1 => array(  'text' => _('Period'), 'from' => $from, 'to' => $to),
120                                         2 => array(  'text' => _('Category'), 'from' => $cat,'to' => ''),
121                                         3 => array(  'text' => _('Location'), 'from' => $loc, 'to' => ''),
122                                         4 => array(  'text' => _('Selection'),'from' => $back,'to' => ''));
123
124         $aligns2 = $aligns;
125
126         $rep = new FrontReport(_('Order Status Listing'), "OrderStatusListing", user_pagesize(), 9, $orientation);
127     if ($orientation == 'L')
128         recalculate_cols($cols);
129         $cols2 = $cols;
130         $rep->Font();
131         $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
132
133         $rep->NewPage();
134         $orderno = 0;
135         $grand_total = 0;
136
137         $result = GetSalesOrders($from, $to, $category, $location, $backorder);
138         while ($myrow=db_fetch($result))
139         {
140                 $rep->NewLine(0, 2, false, $orderno);
141                 if ($orderno != $myrow['order_no'])
142                 {
143                         if ($orderno != 0)
144                         {
145                                 $rep->Line($rep->row);
146                                 $rep->NewLine();
147                         }
148                         $rep->TextCol(0, 1,     $myrow['order_no']);
149                         $rep->TextCol(1, 2,     get_customer_name($myrow['debtor_no']));
150                         $rep->TextCol(2, 3,     get_branch_name($myrow['branch_code']));
151                         $rep->TextCol(3, 4,     $myrow['customer_ref']);
152                         $rep->DateCol(4, 5,     $myrow['ord_date'], true);
153                         $rep->DateCol(5, 6,     $myrow['delivery_date'], true);
154                         $rep->TextCol(6, 7,     $myrow['from_stk_loc']);
155                         $rep->NewLine(1);
156                         $rep->AmountCol(6, 7, $myrow['total']);
157                         $rep->NewLine(1);
158                         $grand_total += $myrow['total'];                
159                         $orderno = $myrow['order_no'];
160                 }
161                 $rep->TextCol(0, 1,     $myrow['stk_code']);
162                 $rep->TextCol(1, 2,     $myrow['description']);
163                 $dec = get_qty_dec($myrow['stk_code']);
164                 $rep->AmountCol(2, 3, $myrow['quantity'], $dec);
165                 $rep->AmountCol(3, 4, $myrow['qty_sent'], $dec);
166                 $rep->AmountCol(4, 5, $myrow['quantity'] - $myrow['qty_sent'], $dec);
167                 if ($myrow['quantity'] - $myrow['qty_sent'] > 0)
168                 {
169                         $rep->Font('italic');
170                         $rep->TextCol(5, 6,     _('Outstanding'));
171                         $rep->Font();
172                 }
173                 $rep->NewLine();
174         }
175         $rep->Line($rep->row);
176         $rep->NewLine();
177         $rep->TextCol(1, 6, _("Grand Total")); 
178         $rep->AmountCol(6, 7, $grand_total);
179         $rep->Line($rep->row - 5);
180         $rep->End();
181 }
182