Missing measure of units when printing sales orders
[fa-stable.git] / reporting / rep109.php
1 <?php
2
3 $page_security = 2;
4 // ----------------------------------------------------------------
5 // $ Revision:  2.0 $
6 // Creator:     Joe Hunt
7 // date_:       2005-05-19
8 // Title:       Print Invoices
9 // ----------------------------------------------------------------
10 $path_to_root="../";
11
12 include_once($path_to_root . "includes/session.inc");
13 include_once($path_to_root . "includes/date_functions.inc");
14 include_once($path_to_root . "includes/data_checks.inc");
15 include_once($path_to_root . "sales/includes/sales_db.inc");
16
17 //----------------------------------------------------------------------------------------------------
18
19 // trial_inquiry_controls();
20 print_sales_orders();
21
22 //----------------------------------------------------------------------------------------------------
23
24 function get_sales_order_details($order_no)
25 {
26         $sql = "SELECT stk_code, unit_price, ".TB_PREF."sales_order_details.description,
27                 ".TB_PREF."sales_order_details.quantity, discount_percent, 
28                 qty_invoiced, units,
29                 ".TB_PREF."stock_master.material_cost + ".TB_PREF."stock_master.labour_cost + ".TB_PREF."stock_master.overhead_cost AS standard_cost
30                 FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master 
31                         WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id 
32                                 AND order_no =" . $order_no;
33         return db_query($sql, "Retreive order Line Items");
34 }
35
36 function print_sales_orders()
37 {
38         global $path_to_root;
39         
40         include_once($path_to_root . "reporting/includes/pdf_report.inc");
41         
42         $from = $_POST['PARAM_0'];
43         $to = $_POST['PARAM_1'];
44         $currency = $_POST['PARAM_2'];
45         $bankaccount = $_POST['PARAM_3'];
46         $email = $_POST['PARAM_4'];     
47         $comments = $_POST['PARAM_5'];
48
49         if ($from == null)
50                 $from = 0;
51         if ($to == null)
52                 $to = 0;
53         $dec =user_price_dec();
54         
55         $cols = array(4, 60, 225, 300, 325, 385, 450, 515);
56
57         // $headers in doctext.inc      
58         $aligns = array('left', 'left', 'right', 'left', 'right', 'right', 'right');
59         
60         $params = array('comments' => $comments,
61                                         'bankaccount' => $bankaccount);
62         
63         $baccount = get_bank_account($params['bankaccount']);
64         $cur = get_company_Pref('curr_default');
65         
66         if ($email == 0)
67         {
68                 $rep = new FrontReport(_('SALES ORDER'), "SalesOrderBulk.pdf", user_pagesize());
69                 $rep->currency = $cur;
70                 $rep->Font();
71                 $rep->Info($params, $cols, null, $aligns);
72         }
73
74         for ($i = $from; $i <= $to; $i++)
75         {
76                 $myrow = get_sales_order($i);
77                 $branch = get_branch($myrow["branch_code"]);
78                 if ($email == 1)
79                 {
80                         $rep = new FrontReport("", "", user_pagesize());
81                         $rep->currency = $cur;
82                         $rep->Font();
83                         $rep->title = _('SALES_ORDER');
84                         $rep->filename = "SalesOrder" . $i . ".pdf";
85                         $rep->Info($params, $cols, null, $aligns);
86                 }
87                 else
88                         $rep->title = _('SALES ORDER');
89                 $rep->Header2($myrow, $branch, $myrow, $baccount, 9);
90
91                 $result = get_sales_order_details($i);
92                 $SubTotal = 0;
93                 while ($myrow2=db_fetch($result))
94                 {
95                         $Net = ((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]);
96                         $SubTotal += $Net;
97                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
98                         $DisplayQty = number_format2($myrow2["quantity"],user_qty_dec());
99                         $DisplayNet = number_format2($Net,$dec);
100                         if ($myrow2["discount_percent"]==0)
101                                 $DisplayDiscount ="";
102                         else 
103                                 $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
104                         $rep->TextCol(0, 1,     $myrow2['stk_code'], -2);
105                         $rep->TextCol(1, 2,     $myrow2['description'], -2);
106                         $rep->TextCol(2, 3,     $DisplayQty, -2);
107                         $rep->TextCol(3, 4,     $myrow2['units'], -2);
108                         $rep->TextCol(4, 5,     $DisplayPrice, -2);
109                         $rep->TextCol(5, 6,     $DisplayDiscount, -2);
110                         $rep->TextCol(6, 7,     $DisplayNet, -2);
111                         $rep->NewLine(1);
112                         if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight)) 
113                                 $rep->Header2($myrow, $branch, $sales_order, $baccount);
114                 }
115                 if ($myrow['comments'] != "")
116                 {
117                         $rep->NewLine();
118                         $rep->TextColLines(1, 5, $myrow['comments'], -2);
119                 }       
120                 $DisplaySubTot = number_format2($SubTotal,$dec);
121                 $DisplayFreight = number_format2($myrow["freight_cost"],$dec);
122
123                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
124                 $linetype = true;
125                 $doctype = 9;
126                 if ($rep->currency != $myrow['curr_code'])
127                 {
128                         include($path_to_root . "reporting/includes/doctext2.inc");                     
129                 }       
130                 else
131                 {
132                         include($path_to_root . "reporting/includes/doctext.inc");                      
133                 }       
134
135                 $rep->TextCol(3, 6, $doc_Sub_total, -2);
136                 $rep->TextCol(6, 7,     $DisplaySubTot, -2);
137                 $rep->NewLine();
138                 $rep->TextCol(3, 6, $doc_Shipping, -2);
139                 $rep->TextCol(6, 7,     $DisplayFreight, -2);
140                 $rep->NewLine();
141                 $DisplayTotal = number_format2($myrow["freight_cost"] + $SubTotal, $dec);
142                 $rep->Font('bold');     
143                 $rep->TextCol(3, 6, $doc_TOTAL_ORDER, - 2);
144                 $rep->TextCol(6, 7,     $DisplayTotal, -2);
145                 $rep->Font();   
146                 if ($email == 1)
147                 {
148                         if ($myrow['contact_email'] == '')
149                         {
150                                 $myrow['contact_email'] = $branch['email'];
151                                 $myrow['DebtorName'] = $branch['br_name'];
152                         }
153                         $rep->End($email, $doc_Invoice_no . " " . $myrow['reference'], $myrow);
154                 }       
155         }
156         if ($email == 0)
157                 $rep->End();
158 }
159
160 ?>