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