Changed license type to GPLv3 in top of files
[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         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
64
65         $from = $_POST['PARAM_0'];
66         $to = $_POST['PARAM_1'];
67         $summary = $_POST['PARAM_2'];
68         $comments = $_POST['PARAM_3'];
69
70         if ($summary == 0)
71                 $sum = _("No");
72         else
73                 $sum = _("Yes");
74
75         $dec = user_qty_dec();
76
77         $cols = array(0, 60, 150, 220, 325,     385, 450, 515);
78
79         $headers = array(_('Invoice'), _('Customer'), _('Branch'), _('Customer Ref'),
80                 _('Inv Date'),  _('Total'),     _('Provision'));
81
82         $aligns = array('left', 'left', 'left', 'left', 'left', 'right',        'right');
83
84         $headers2 = array(_('Salesman'), " ",   _('Phone'), _('Email'), _('Provision'),
85                 _('Break Pt.'), _('Provision')." 2");
86
87     $params =   array(  0 => $comments,
88                                         1 => array(  'text' => _('Period'), 'from' => $from, 'to' => $to),
89                                         2 => array(  'text' => _('Summary Only'),'from' => $sum,'to' => ''));
90
91         $cols2 = $cols;
92         $aligns2 = $aligns;
93
94         $rep = new FrontReport(_('Salesman Listing'), "SalesmanListing.pdf", user_pagesize());
95         $rep->Font();
96         $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
97
98         $rep->Header();
99         $salesman = 0;
100         $subtotal = $total = $subprov = $provtotal = 0;
101
102         $result = GetSalesmanTrans($from, $to);
103
104         while ($myrow=db_fetch($result))
105         {
106                 if ($rep->row < $rep->bottomMargin + (2 * $rep->lineHeight))
107                 {
108                         $salesman = 0;
109                         $rep->Header();
110                 }
111                 $rep->NewLine(0, 2, false, $salesman);
112                 if ($salesman != $myrow['salesman_code'])
113                 {
114                         if ($salesman != 0)
115                         {
116                                 $rep->Line($rep->row - 8);
117                                 $rep->NewLine(2);
118                                 $rep->TextCol(0, 3, _('Total'));
119                                 $rep->TextCol(5, 6, number_format2($subtotal, $dec));
120                                 $rep->TextCol(6, 7, number_format2($subprov, $dec));
121                         $rep->Line($rep->row  - 4);
122                         $rep->NewLine(2);
123                                 //$rep->Line($rep->row);
124                         }
125                         $rep->TextCol(0, 2,     $myrow['salesman_code']." ".$myrow['salesman_name']);
126                         $rep->TextCol(2, 3,     $myrow['salesman_phone']);
127                         $rep->TextCol(3, 4,     $myrow['salesman_email']);
128                         $rep->TextCol(4, 5,     number_format2($myrow['provision'], user_percent_dec()) ." %");
129                         $rep->TextCol(5, 6,     number_format2($myrow['break_pt'], $dec));
130                         $rep->TextCol(6, 7,     number_format2($myrow['provision2'], user_percent_dec()) ." %");
131                         $rep->NewLine(2);
132                         $salesman = $myrow['salesman_code'];
133                         $total += $subtotal;
134                         $provtotal += $subprov;
135                         $subtotal = 0;
136                         $subprov = 0;
137                 }
138                 $date = sql2date($myrow['tran_date']);
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->TextCol(4, 5,     $date);
152                         $rep->TextCol(5, 6,     number_format2($amt, $dec));
153                         $rep->TextCol(6, 7,     number_format2($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->TextCol(5, 6, number_format2($subtotal, $dec));
170                 $rep->TextCol(6, 7, number_format2($subprov, $dec));
171                 $rep->Line($rep->row  - 4);
172                 $rep->NewLine(2);
173                 //$rep->Line($rep->row);
174                 $total += $subtotal;
175                 $provtotal += $subprov;
176         }
177         $rep->fontSize += 2;
178         $rep->TextCol(0, 3, _('Grand Total'));
179         $rep->fontSize -= 2;
180         $rep->TextCol(5, 6, number_format2($total, $dec));
181         $rep->TextCol(6, 7, number_format2($provtotal, $dec));
182         $rep->Line($rep->row  - 4);
183         $rep->End();
184 }
185
186 ?>