Preparation for Excel Writer continued
[fa-stable.git] / reporting / rep106.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 = 2;
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 // trial_inquiry_controls();
30 print_salesman_list();
31
32 //----------------------------------------------------------------------------------------------------
33
34 function GetSalesmanTrans($from, $to)
35 {
36         $fromdate = date2sql($from);
37         $todate = date2sql($to);
38
39         $sql = "SELECT DISTINCT ".TB_PREF."debtor_trans.*,
40                 ov_amount+ov_discount AS InvoiceTotal,
41                 ".TB_PREF."debtors_master.name AS DebtorName, ".TB_PREF."debtors_master.curr_code, ".TB_PREF."cust_branch.br_name,
42                 ".TB_PREF."cust_branch.contact_name, ".TB_PREF."salesman.*
43                 FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master, ".TB_PREF."sales_orders, ".TB_PREF."cust_branch,
44                         ".TB_PREF."salesman
45                 WHERE ".TB_PREF."sales_orders.order_no=".TB_PREF."debtor_trans.order_
46                     AND ".TB_PREF."sales_orders.branch_code=".TB_PREF."cust_branch.branch_code
47                     AND ".TB_PREF."cust_branch.salesman=".TB_PREF."salesman.salesman_code
48                     AND ".TB_PREF."debtor_trans.debtor_no=".TB_PREF."debtors_master.debtor_no
49                     AND (".TB_PREF."debtor_trans.type=10 OR ".TB_PREF."debtor_trans.type=11)
50                     AND ".TB_PREF."debtor_trans.tran_date>='$fromdate'
51                     AND ".TB_PREF."debtor_trans.tran_date<='$todate'
52                 ORDER BY ".TB_PREF."salesman.salesman_code, ".TB_PREF."debtor_trans.tran_date";
53
54         return db_query($sql, "Error getting order details");
55 }
56
57 //----------------------------------------------------------------------------------------------------
58
59 function print_salesman_list()
60 {
61         global $path_to_root;
62
63         $from = $_POST['PARAM_0'];
64         $to = $_POST['PARAM_1'];
65         $summary = $_POST['PARAM_2'];
66         $comments = $_POST['PARAM_3'];
67         $destination = $_POST['PARAM_4'];
68         if ($destination)
69         {
70                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
71                 $filename = "SalesmanListing.xml";
72         }       
73         else
74         {
75                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
76                 $filename = "SalesmanListing.pdf";
77         }
78
79         if ($summary == 0)
80                 $sum = _("No");
81         else
82                 $sum = _("Yes");
83
84         $dec = user_qty_dec();
85
86         $cols = array(0, 60, 150, 220, 325,     385, 450, 515);
87
88         $headers = array(_('Invoice'), _('Customer'), _('Branch'), _('Customer Ref'),
89                 _('Inv Date'),  _('Total'),     _('Provision'));
90
91         $aligns = array('left', 'left', 'left', 'left', 'left', 'right',        'right');
92
93         $headers2 = array(_('Salesman'), " ",   _('Phone'), _('Email'), _('Provision'),
94                 _('Break Pt.'), _('Provision')." 2");
95
96     $params =   array(  0 => $comments,
97                                         1 => array(  'text' => _('Period'), 'from' => $from, 'to' => $to),
98                                         2 => array(  'text' => _('Summary Only'),'from' => $sum,'to' => ''));
99
100         $cols2 = $cols;
101         $aligns2 = $aligns;
102
103         $rep = new FrontReport(_('Salesman Listing'), $filename, user_pagesize());
104         $rep->Font();
105         $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
106
107         $rep->Header();
108         $salesman = 0;
109         $subtotal = $total = $subprov = $provtotal = 0;
110
111         $result = GetSalesmanTrans($from, $to);
112
113         while ($myrow=db_fetch($result))
114         {
115                 if ($rep->row < $rep->bottomMargin + (2 * $rep->lineHeight))
116                 {
117                         $salesman = 0;
118                         $rep->Header();
119                 }
120                 $rep->NewLine(0, 2, false, $salesman);
121                 if ($salesman != $myrow['salesman_code'])
122                 {
123                         if ($salesman != 0)
124                         {
125                                 $rep->Line($rep->row - 8);
126                                 $rep->NewLine(2);
127                                 $rep->TextCol(0, 3, _('Total'));
128                                 $rep->AmountCol(5, 6, $subtotal, $dec);
129                                 $rep->AmountCol(6, 7, $subprov, $dec);
130                         $rep->Line($rep->row  - 4);
131                         $rep->NewLine(2);
132                         }
133                         $rep->TextCol(0, 2,     $myrow['salesman_code']." ".$myrow['salesman_name']);
134                         $rep->TextCol(2, 3,     $myrow['salesman_phone']);
135                         $rep->TextCol(3, 4,     $myrow['salesman_email']);
136                         $rep->TextCol(4, 5,     number_format2($myrow['provision'], user_percent_dec()) ." %");
137                         $rep->AmountCol(5, 6, $myrow['break_pt'], $dec);
138                         $rep->TextCol(6, 7,     number_format2($myrow['provision2'], user_percent_dec()) ." %");
139                         $rep->NewLine(2);
140                         $salesman = $myrow['salesman_code'];
141                         $total += $subtotal;
142                         $provtotal += $subprov;
143                         $subtotal = 0;
144                         $subprov = 0;
145                 }
146                 $rate = $myrow['rate'];
147                 $amt = $myrow['InvoiceTotal'] * $rate;
148                 if ($subprov > $myrow['break_pt'] && $myrow['provision2'] != 0)
149                         $prov = $myrow['provision2'] * $amt / 100;
150                 else
151                         $prov = $myrow['provision'] * $amt / 100;
152                 if (!$summary)
153                 {
154                         $rep->TextCol(0, 1,     $myrow['trans_no']);
155                         $rep->TextCol(1, 2,     $myrow['DebtorName']);
156                         $rep->TextCol(2, 3,     $myrow['br_name']);
157                         $rep->TextCol(3, 4,     $myrow['contact_name']);
158                         $rep->DateCol(4, 5,     $myrow['tran_date'], true);
159                         $rep->AmountCol(5, 6, $amt, $dec);
160                         $rep->AmountCol(6, 7, $prov, $dec);
161                         $rep->NewLine();
162                         if ($rep->row < $rep->bottomMargin + (2 * $rep->lineHeight))
163                         {
164                                 $salesman = 0;
165                                 $rep->Header();
166                         }
167                 }
168                 $subtotal += $amt;
169                 $subprov += $prov;
170         }
171         if ($salesman != 0)
172         {
173                 $rep->Line($rep->row - 4);
174                 $rep->NewLine(2);
175                 $rep->TextCol(0, 3, _('Total'));
176                 $rep->AmountCol(5, 6, $subtotal, $dec);
177                 $rep->AmountCol(6, 7, $subprov, $dec);
178                 $rep->Line($rep->row  - 4);
179                 $rep->NewLine(2);
180                 $total += $subtotal;
181                 $provtotal += $subprov;
182         }
183         $rep->fontSize += 2;
184         $rep->TextCol(0, 3, _('Grand Total'));
185         $rep->fontSize -= 2;
186         $rep->AmountCol(5, 6, $total, $dec);
187         $rep->AmountCol(6, 7, $provtotal, $dec);
188         $rep->Line($rep->row  - 4);
189         $rep->NewLine();
190         $rep->End();
191 }
192
193 ?>