db251ae50ca4e46a7eff0c4abdd785ff09c194a6
[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 Sales Orders
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_sent, 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 $print_as_quote = 0;
37
38 function print_sales_orders()
39 {
40         global $path_to_root, $print_as_quote;
41         
42         include_once($path_to_root . "reporting/includes/pdf_report.inc");
43         
44         $from = $_POST['PARAM_0'];
45         $to = $_POST['PARAM_1'];
46         $currency = $_POST['PARAM_2'];
47         $bankaccount = $_POST['PARAM_3'];
48         $email = $_POST['PARAM_4'];     
49         $print_as_quote = $_POST['PARAM_5'];    
50         $comments = $_POST['PARAM_6'];
51
52         if ($from == null)
53                 $from = 0;
54         if ($to == null)
55                 $to = 0;
56         $dec =user_price_dec();
57         
58         $cols = array(4, 60, 225, 300, 325, 385, 450, 515);
59
60         // $headers in doctext.inc      
61         $aligns = array('left', 'left', 'right', 'left', 'right', 'right', 'right');
62         
63         $params = array('comments' => $comments,
64                                         'bankaccount' => $bankaccount);
65         
66         $baccount = get_bank_account($params['bankaccount']);
67         $cur = get_company_Pref('curr_default');
68         
69         if ($email == 0)
70         {
71                 if ($print_as_quote == 1)
72                         $rep = new FrontReport(_("QUOTE"), "QuoteBulk.pdf", user_pagesize());
73                 else    
74                         $rep = new FrontReport(_("SALES ORDER"), "SalesOrderBulk.pdf", user_pagesize());
75                 $rep->currency = $cur;
76                 $rep->Font();
77                 $rep->Info($params, $cols, null, $aligns);
78         }
79
80         for ($i = $from; $i <= $to; $i++)
81         {
82                 $myrow = get_sales_order($i);
83                 $branch = get_branch($myrow["branch_code"]);
84                 if ($email == 1)
85                 {
86                         $rep = new FrontReport("", "", user_pagesize());
87                         $rep->currency = $cur;
88                         $rep->Font();
89                         if ($print_as_quote == 1)
90                         {
91                                 $rep->title = _('QUOTE');
92                                 $rep->filename = "Quote" . $i . ".pdf";
93                         }
94                         else
95                         {
96                                 $rep->title = _("SALES ORDER");
97                                 $rep->filename = "SalesOrder" . $i . ".pdf";
98                         }       
99                         $rep->Info($params, $cols, null, $aligns);
100                 }
101                 else
102                         $rep->title = ($print_as_quote==1 ? _("QUOTE") : _("SALES ORDER"));
103                 $rep->Header2($myrow, $branch, $myrow, $baccount, 9);
104
105                 $result = get_sales_order_details($i);
106                 $SubTotal = 0;
107                 while ($myrow2=db_fetch($result))
108                 {
109                         $Net = ((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]);
110                         $SubTotal += $Net;
111                         $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
112                         $DisplayQty = number_format2($myrow2["quantity"],user_qty_dec());
113                         $DisplayNet = number_format2($Net,$dec);
114                         if ($myrow2["discount_percent"]==0)
115                                 $DisplayDiscount ="";
116                         else 
117                                 $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";
118                         $rep->TextCol(0, 1,     $myrow2['stk_code'], -2);
119                         $rep->TextCol(1, 2,     $myrow2['description'], -2);
120                         $rep->TextCol(2, 3,     $DisplayQty, -2);
121                         $rep->TextCol(3, 4,     $myrow2['units'], -2);
122                         $rep->TextCol(4, 5,     $DisplayPrice, -2);
123                         $rep->TextCol(5, 6,     $DisplayDiscount, -2);
124                         $rep->TextCol(6, 7,     $DisplayNet, -2);
125                         $rep->NewLine(1);
126                         if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight)) 
127                                 $rep->Header2($myrow, $branch, $sales_order, $baccount);
128                 }
129                 if ($myrow['comments'] != "")
130                 {
131                         $rep->NewLine();
132                         $rep->TextColLines(1, 5, $myrow['comments'], -2);
133                 }       
134                 $DisplaySubTot = number_format2($SubTotal,$dec);
135                 $DisplayFreight = number_format2($myrow["freight_cost"],$dec);
136
137                 $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
138                 $linetype = true;
139                 $doctype = 9;
140                 if ($rep->currency != $myrow['curr_code'])
141                 {
142                         include($path_to_root . "reporting/includes/doctext2.inc");                     
143                 }       
144                 else
145                 {
146                         include($path_to_root . "reporting/includes/doctext.inc");                      
147                 }       
148
149                 $rep->TextCol(3, 6, $doc_Sub_total, -2);
150                 $rep->TextCol(6, 7,     $DisplaySubTot, -2);
151                 $rep->NewLine();
152                 $rep->TextCol(3, 6, $doc_Shipping, -2);
153                 $rep->TextCol(6, 7,     $DisplayFreight, -2);
154                 $rep->NewLine();
155                 $DisplayTotal = number_format2($myrow["freight_cost"] + $SubTotal, $dec);
156                 $rep->Font('bold');     
157                 $rep->TextCol(3, 6, $doc_TOTAL_ORDER, - 2);
158                 $rep->TextCol(6, 7,     $DisplayTotal, -2);
159                 $rep->Font();   
160                 if ($email == 1)
161                 {
162                         if ($myrow['contact_email'] == '')
163                         {
164                                 $myrow['contact_email'] = $branch['email'];
165                                 $myrow['DebtorName'] = $branch['br_name'];
166                         }
167                         $rep->End($email, $doc_Invoice_no . " " . $myrow['reference'], $myrow);
168                 }       
169         }
170         if ($email == 0)
171                 $rep->End();
172 }
173
174 ?>