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