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