Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[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 = 'SA_SALESMANREP';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Salesman Report
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 trans.*,
39                         ov_amount+ov_discount AS InvoiceTotal,
40                         cust.name AS DebtorName,
41                         cust.curr_code,
42                         branch.br_name,
43                         sorder.customer_ref,
44                         salesman.*
45                 FROM ".TB_PREF."debtor_trans trans,
46                          ".TB_PREF."debtors_master cust,
47                          ".TB_PREF."sales_orders sorder,
48                          ".TB_PREF."cust_branch branch,
49                         ".TB_PREF."salesman salesman
50                 WHERE sorder.order_no=trans.order_
51                     AND sorder.branch_code=branch.branch_code
52                     AND sorder.trans_type = ".ST_SALESORDER."
53                     AND branch.salesman=salesman.salesman_code
54                     AND trans.debtor_no=cust.debtor_no
55                     AND (trans.type=".ST_SALESINVOICE." OR trans.type=".ST_CUSTCREDIT.")
56                     AND trans.tran_date>='$fromdate'
57                     AND trans.tran_date<='$todate'
58                 ORDER BY salesman.salesman_code, trans.tran_date";
59
60         return db_query($sql, "Error getting order details");
61 }
62
63 //----------------------------------------------------------------------------------------------------
64
65 function print_salesman_list()
66 {
67         global $path_to_root;
68
69         $from = $_POST['PARAM_0'];
70         $to = $_POST['PARAM_1'];
71         $summary = $_POST['PARAM_2'];
72         $comments = $_POST['PARAM_3'];
73         $orientation = $_POST['PARAM_4'];
74         $destination = $_POST['PARAM_5'];
75         if ($destination)
76                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
77         else
78                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
79         $orientation = ($orientation ? 'L' : 'P');
80
81         if ($summary == 0)
82                 $sum = _("No");
83         else
84                 $sum = _("Yes");
85
86         $dec = user_price_dec();
87
88         $cols = array(0, 60, 150, 220, 325,     385, 450, 515);
89
90         $headers = array(_('Invoice'), _('Customer'), _('Branch'), _('Customer Ref'),
91                 _('Inv Date'),  _('Total'),     _('Provision'));
92
93         $aligns = array('left', 'left', 'left', 'left', 'left', 'right',        'right');
94
95         $headers2 = array(_('Salesman'), " ",   _('Phone'), _('Email'), _('Provision'),
96                 _('Break Pt.'), _('Provision')." 2");
97
98     $params =   array(  0 => $comments,
99                                         1 => array(  'text' => _('Period'), 'from' => $from, 'to' => $to),
100                                         2 => array(  'text' => _('Summary Only'),'from' => $sum,'to' => ''));
101
102         $aligns2 = $aligns;
103
104         $rep = new FrontReport(_('Salesman Listing'), "SalesmanListing", user_pagesize(), 9, $orientation);
105     if ($orientation == 'L')
106         recalculate_cols($cols);
107         $cols2 = $cols;
108         $rep->Font();
109         $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
110
111         $rep->NewPage();
112         $salesman = 0;
113         $subtotal = $total = $subprov = $provtotal = 0;
114
115         $result = GetSalesmanTrans($from, $to);
116
117         while ($myrow=db_fetch($result))
118         {
119                 $rep->NewLine(0, 2, false, $salesman);
120                 if ($salesman != $myrow['salesman_code'])
121                 {
122                         if ($salesman != 0)
123                         {
124                                 $rep->Line($rep->row - 8);
125                                 $rep->NewLine(2);
126                                 $rep->TextCol(0, 3, _('Total'));
127                                 $rep->AmountCol(5, 6, $subtotal, $dec);
128                                 $rep->AmountCol(6, 7, $subprov, $dec);
129                         $rep->Line($rep->row  - 4);
130                         $rep->NewLine(2);
131                         }
132                         $rep->TextCol(0, 2,     $myrow['salesman_code']." ".$myrow['salesman_name']);
133                         $rep->TextCol(2, 3,     $myrow['salesman_phone']);
134                         $rep->TextCol(3, 4,     $myrow['salesman_email']);
135                         $rep->TextCol(4, 5,     number_format2($myrow['provision'], user_percent_dec()) ." %");
136                         $rep->AmountCol(5, 6, $myrow['break_pt'], $dec);
137                         $rep->TextCol(6, 7,     number_format2($myrow['provision2'], user_percent_dec()) ." %");
138                         $rep->NewLine(2);
139                         $salesman = $myrow['salesman_code'];
140                         $total += $subtotal;
141                         $provtotal += $subprov;
142                         $subtotal = 0;
143                         $subprov = 0;
144                 }
145                 $rate = $myrow['rate'];
146                 $amt = $myrow['InvoiceTotal'] * $rate;
147                 if ($myrow['provision2'] == 0)
148                         $prov = $myrow['provision'] * $amt / 100;
149                 else {
150                         $amt1 = min($amt, max(0, $myrow['break_pt']-$subtotal));
151                         $amt2 = $amt - $amt1;
152
153                         $prov = $amt1*$myrow['provision']/100 + $amt2*$myrow['provision2']/100;
154                 }
155                 if (!$summary)
156                 {
157                         $rep->TextCol(0, 1,     $myrow['trans_no']);
158                         $rep->TextCol(1, 2,     $myrow['DebtorName']);
159                         $rep->TextCol(2, 3,     $myrow['br_name']);
160                         $rep->TextCol(3, 4,     $myrow['customer_ref']);
161                         $rep->DateCol(4, 5,     $myrow['tran_date'], true);
162                         $rep->AmountCol(5, 6, $amt, $dec);
163                         $rep->AmountCol(6, 7, $prov, $dec);
164                         $rep->NewLine();
165                 }
166                 $subtotal += $amt;
167                 $subprov += $prov;
168         }
169         if ($salesman != 0)
170         {
171                 $rep->Line($rep->row - 4);
172                 $rep->NewLine(2);
173                 $rep->TextCol(0, 3, _('Total'));
174                 $rep->AmountCol(5, 6, $subtotal, $dec);
175                 $rep->AmountCol(6, 7, $subprov, $dec);
176                 $rep->Line($rep->row  - 4);
177                 $rep->NewLine(2);
178                 $total += $subtotal;
179                 $provtotal += $subprov;
180         }
181         $rep->fontSize += 2;
182         $rep->TextCol(0, 3, _('Grand Total'));
183         $rep->fontSize -= 2;
184         $rep->AmountCol(5, 6, $total, $dec);
185         $rep->AmountCol(6, 7, $provtotal, $dec);
186         $rep->Line($rep->row  - 4);
187         $rep->NewLine();
188         $rep->End();
189 }
190