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