Changed $path_to_root and stamped copyright
[fa-stable.git] / reporting / rep103.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 2;
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Customer Details Listing
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 . "/gl/includes/gl_db.inc");
25
26 //----------------------------------------------------------------------------------------------------
27
28 // trial_inquiry_controls();
29 print_customer_details_listing();
30
31 function get_customer_details_for_report($area=0, $salesid=0) 
32 {
33         $sql = "SELECT ".TB_PREF."debtors_master.debtor_no,
34                         ".TB_PREF."debtors_master.name,
35                         ".TB_PREF."debtors_master.address,
36                         ".TB_PREF."sales_types.sales_type,
37                         ".TB_PREF."cust_branch.branch_code,
38                         ".TB_PREF."cust_branch.br_name,
39                         ".TB_PREF."cust_branch.br_address,
40                         ".TB_PREF."cust_branch.contact_name,
41                         ".TB_PREF."cust_branch.phone,
42                         ".TB_PREF."cust_branch.fax,
43                         ".TB_PREF."cust_branch.email,
44                         ".TB_PREF."cust_branch.area,
45                         ".TB_PREF."cust_branch.salesman,
46                         ".TB_PREF."areas.description,
47                         ".TB_PREF."salesman.salesman_name
48                 FROM ".TB_PREF."debtors_master 
49                 INNER JOIN ".TB_PREF."cust_branch
50                         ON ".TB_PREF."debtors_master.debtor_no=".TB_PREF."cust_branch.debtor_no
51                 INNER JOIN ".TB_PREF."sales_types
52                         ON ".TB_PREF."debtors_master.sales_type=".TB_PREF."sales_types.id
53                 INNER JOIN ".TB_PREF."areas
54                         ON ".TB_PREF."cust_branch.area = ".TB_PREF."areas.area_code
55                 INNER JOIN ".TB_PREF."salesman
56                         ON ".TB_PREF."cust_branch.salesman=".TB_PREF."salesman.salesman_code";
57         if ($area != 0)
58         {
59                 if ($salesid != 0)
60                         $sql .= " WHERE ".TB_PREF."salesman.salesman_code='$salesid' 
61                                 AND ".TB_PREF."areas.area_code='$area'";
62                 else            
63                         $sql .= " WHERE ".TB_PREF."areas.area_code='$area'";
64         }
65         elseif ($salesid != 0)
66                 $sql .= " WHERE ".TB_PREF."salesman.salesman_code='$salesid'";
67         $sql .= " ORDER BY description,
68                         ".TB_PREF."salesman.salesman_name,
69                         ".TB_PREF."debtors_master.debtor_no,
70                         ".TB_PREF."cust_branch.branch_code";
71                                         
72     return db_query($sql,"No transactions were returned");
73 }
74
75                                         
76 function getTransactions($debtorno, $branchcode, $date)
77 {
78         $date = date2sql($date);
79
80         $sql = "SELECT SUM((ov_amount+ov_freight+ov_discount)*rate) AS Turnover
81                 FROM ".TB_PREF."debtor_trans
82                 WHERE debtor_no='$debtorno'
83                 AND branch_code='$branchcode'
84                 AND (type=10 or type=11)
85                 AND trandate >='$date'";
86                 
87     $result = db_query($sql,"No transactions were returned");
88
89         $row = db_fetch_row($result);
90         return $row[0];
91 }
92
93 //----------------------------------------------------------------------------------------------------
94
95 function print_customer_details_listing()
96 {
97     global $path_to_root;
98
99     include_once($path_to_root . "/reporting/includes/pdf_report.inc");
100
101     $from = $_POST['PARAM_0'];
102     $area = $_POST['PARAM_1'];
103     $folk = $_POST['PARAM_2'];
104     $more = $_POST['PARAM_3'];
105     $less = $_POST['PARAM_4'];
106     $comments = $_POST['PARAM_5'];
107     
108     $dec = 0;
109
110         if ($area == reserved_words::get_all_numeric())
111                 $area = 0;
112         if ($folk == reserved_words::get_all_numeric())
113                 $folk = 0;
114
115         if ($area == 0)
116                 $sarea = _('All Areas');
117         else
118                 $sarea = get_area_name($area);
119         if ($folk == 0)
120                 $salesfolk = _('All Sales Folk');
121         else
122                 $salesfolk = get_salesman_name($folk);
123         if ($more != '')
124                 $morestr = _('Greater than ') . number_format2($more, $dec);
125         else
126                 $morestr = '';
127         if ($less != '')
128                 $lessstr = _('Less than ') . number_format2($less, $dec);
129         else
130                 $lessstr = '';
131         
132         $more = (double)$more;  
133         $less = (double)$less;
134
135         $cols = array(0, 150, 300, 400, 550);
136
137         $headers = array(_('Customer Postal Address'), _('Price/Turnover'),     _('Branch Contact Information'),
138                 _('Branch Delivery Address'));
139
140         $aligns = array('left', 'left', 'left', 'left');
141
142     $params =   array(  0 => $comments,
143                                     1 => array('text' => _('Activity Since'),   'from' => $from,                'to' => ''),
144                                     2 => array('text' => _('Sales Areas'),              'from' => $sarea,               'to' => ''),
145                                     3 => array('text' => _('Sales Folk'),               'from' => $salesfolk,   'to' => ''),
146                                     4 => array('text' => _('Activity'),                 'from' => $morestr,     'to' => $lessstr));
147
148     $rep = new FrontReport(_('Customer Details Listing'), "CustomerDetailsListing.pdf", user_pagesize());
149
150     $rep->Font();
151     $rep->Info($params, $cols, $headers, $aligns);
152     $rep->Header();
153
154         $result = get_customer_details_for_report($area, $folk);
155         
156         $carea = '';
157         $sman = '';
158         while ($myrow=db_fetch($result)) 
159         {
160                 $printcustomer = true;
161                 if ($more != '' || $less != '')
162                 {
163                         $turnover = getTransactions($myrow['debtor_no'], $myrow['branch_code'], $from);
164                         if ($more != 0.0 && $turnover <= (double)$more)
165                                 $printcustomer = false;
166                         if ($less != 0.0 && $turnover >= (double)$less)
167                                 $printcustomer = false;
168                 }       
169                 if ($printcustomer)
170                 {
171                         if ($carea != $myrow['description'])
172                         {
173                                 $rep->fontSize += 2;
174                                 $rep->NewLine(2, 7);
175                                 $rep->Font('bold');     
176                                 $rep->TextCol(0, 3,     _('Customers in') . " " . $myrow['description']);
177                                 $carea = $myrow['description'];
178                                 $rep->fontSize -= 2;
179                                 $rep->Font();
180                                 $rep->NewLine();
181                         }       
182                         if ($sman != $myrow['salesman_name'])
183                         {
184                                 $rep->fontSize += 2;
185                                 $rep->NewLine(1, 7);
186                                 $rep->Font('bold');     
187                                 $rep->TextCol(0, 3,     $myrow['salesman_name']);
188                                 $sman = $myrow['salesman_name'];
189                                 $rep->fontSize -= 2;
190                                 $rep->Font();
191                                 $rep->NewLine();
192                         }
193                         $rep->NewLine();
194                         $rep->TextCol(0, 1,     $myrow['name']);
195                         $adr = Explode("\n", $myrow['address']);
196                         $count1 = count($adr);
197                         for ($i = 0; $i < $count1; $i++)
198                                 $rep->TextCol(0, 1, $adr[$i], 0, ($i + 1) * $rep->lineHeight);
199                         $count1++;              
200                         $rep->TextCol(1, 2,     _('Price List') . ": " . $myrow['sales_type']);
201                         if ($more != 0.0 || $less != 0.0)
202                                 $rep->TextCol(1, 2,     _('Turnover') . ": " . number_format2($turnover, $dec), 0, $rep->lineHeight);
203                         $rep->TextCol(2, 3,     $myrow['br_name']);
204                         $rep->TextCol(2, 3, $myrow['contact_name'], 0, $rep->lineHeight);
205                         $rep->TextCol(2, 3, _('Ph') . ": " . $myrow['phone'], 0, 2 * $rep->lineHeight);
206                         $rep->TextCol(2, 3, _('Fax') . ": " . $myrow['fax'], 0, 3 * $rep->lineHeight);
207                         $adr = Explode("\n", $myrow['br_address']);
208                         $count2 = count($adr);
209                         for ($i = 0; $i < $count2; $i++)
210                                 $rep->TextCol(3, 4, $adr[$i], 0, ($i + 1) * $rep->lineHeight);
211                         $rep->TextCol(3, 4, $myrow['email'], 0, ($count2 + 1) * $rep->lineHeight);
212                         $count2++;
213                         $count1 = Max($count1, $count2);
214                         $count1 = Max($count1, 4);
215                         $rep->NewLine($count1); 
216                         $rep->Line($rep->row + 8);
217                         $rep->NewLine(0, 3);
218                 }
219         }
220     $rep->End();
221 }
222
223 ?>