Small bugs in Customer Detail Report and Supplier Detail Report.
[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 = 'SA_CUSTBULKREP';
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."debtors_master.curr_code,
36                         ".TB_PREF."debtors_master.dimension_id,
37                         ".TB_PREF."debtors_master.dimension2_id,
38                         ".TB_PREF."sales_types.sales_type,
39                         ".TB_PREF."cust_branch.branch_code,
40                         ".TB_PREF."cust_branch.br_name,
41                         ".TB_PREF."cust_branch.br_address,
42                         ".TB_PREF."cust_branch.br_post_address,
43                         ".TB_PREF."cust_branch.contact_name,
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=".db_escape($salesid)."
61                                 AND ".TB_PREF."areas.area_code=".db_escape($area);
62                 else
63                         $sql .= " WHERE ".TB_PREF."areas.area_code=".db_escape($area);
64         }
65         elseif ($salesid != 0)
66                 $sql .= " WHERE ".TB_PREF."salesman.salesman_code=".db_escape($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 function get_contacts_for_branch($branch)
76 {
77         $sql = "SELECT p.*, r.action, r.type, CONCAT(r.type,'.',r.action) as ext_type 
78                 FROM ".TB_PREF."crm_persons p,".TB_PREF."crm_contacts r WHERE r.person_id=p.id AND r.type='cust_branch' 
79                         AND r.entity_id=".db_escape($branch);
80         $res = db_query($sql, "can't retrieve branch contacts");
81         $results = array();
82         while($contact = db_fetch($res))
83                 $results[] = $contact;
84         return $results;
85 }
86
87 function getTransactions($debtorno, $branchcode, $date)
88 {
89         $date = date2sql($date);
90
91         $sql = "SELECT SUM((ov_amount+ov_freight+ov_discount)*rate) AS Turnover
92                 FROM ".TB_PREF."debtor_trans
93                 WHERE debtor_no=".db_escape($debtorno)."
94                 AND branch_code=".db_escape($branchcode)."
95                 AND (type=".ST_SALESINVOICE." OR type=".ST_CUSTCREDIT.")
96                 AND tran_date >='$date'";
97
98     $result = db_query($sql,"No transactions were returned");
99
100         $row = db_fetch_row($result);
101         return $row[0];
102 }
103
104 //----------------------------------------------------------------------------------------------------
105
106 function print_customer_details_listing()
107 {
108     global $path_to_root;
109
110     $from = $_POST['PARAM_0'];
111     $area = $_POST['PARAM_1'];
112     $folk = $_POST['PARAM_2'];
113     $more = $_POST['PARAM_3'];
114     $less = $_POST['PARAM_4'];
115     $comments = $_POST['PARAM_5'];
116         $orientation = $_POST['PARAM_6'];
117         $destination = $_POST['PARAM_7'];
118         if ($destination)
119                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
120         else
121                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
122
123         $orientation = ($orientation ? 'L' : 'P');
124     $dec = 0;
125
126         if ($area == ALL_NUMERIC)
127                 $area = 0;
128         if ($folk == ALL_NUMERIC)
129                 $folk = 0;
130
131         if ($area == 0)
132                 $sarea = _('All Areas');
133         else
134                 $sarea = get_area_name($area);
135         if ($folk == 0)
136                 $salesfolk = _('All Sales Folk');
137         else
138                 $salesfolk = get_salesman_name($folk);
139         if ($more != '')
140                 $morestr = _('Greater than ') . number_format2($more, $dec);
141         else
142                 $morestr = '';
143         if ($less != '')
144                 $lessstr = _('Less than ') . number_format2($less, $dec);
145         else
146                 $lessstr = '';
147
148         $more = (double)$more;
149         $less = (double)$less;
150
151         $cols = array(0, 150, 300, 425, 550);
152
153         $headers = array(_('Customer Postal Address'), _('Price/Turnover'),     _('Branch Contact Information'),
154                 _('Branch Delivery Address'));
155
156         $aligns = array('left', 'left', 'left', 'left');
157
158     $params =   array(  0 => $comments,
159                                     1 => array('text' => _('Activity Since'),   'from' => $from,                'to' => ''),
160                                     2 => array('text' => _('Sales Areas'),              'from' => $sarea,               'to' => ''),
161                                     3 => array('text' => _('Sales Folk'),               'from' => $salesfolk,   'to' => ''),
162                                     4 => array('text' => _('Activity'),                 'from' => $morestr,     'to' => $lessstr . " " . get_company_pref("curr_default")));
163
164     $rep = new FrontReport(_('Customer Details Listing'), "CustomerDetailsListing", user_pagesize(), 9, $orientation);
165     if ($orientation == 'L')
166         recalculate_cols($cols);
167
168     $rep->Font();
169     $rep->Info($params, $cols, $headers, $aligns);
170     $rep->NewPage();
171
172         $result = get_customer_details_for_report($area, $folk);
173
174         $carea = '';
175         $sman = '';
176         while ($myrow=db_fetch($result))
177         {
178                 $printcustomer = true;
179                 if ($more != '' || $less != '')
180                 {
181                         $turnover = getTransactions($myrow['debtor_no'], $myrow['branch_code'], $from);
182                         if ($more != 0.0 && $turnover <= (double)$more)
183                                 $printcustomer = false;
184                         if ($less != 0.0 && $turnover >= (double)$less)
185                                 $printcustomer = false;
186                 }
187                 if ($printcustomer)
188                 {
189                         if ($carea != $myrow['description'])
190                         {
191                                 $rep->fontSize += 2;
192                                 $rep->NewLine(2, 7);
193                                 $rep->Font('bold');
194                                 $rep->TextCol(0, 3,     _('Customers in') . " " . $myrow['description']);
195                                 $carea = $myrow['description'];
196                                 $rep->fontSize -= 2;
197                                 $rep->Font();
198                                 $rep->NewLine();
199                         }
200                         if ($sman != $myrow['salesman_name'])
201                         {
202                                 $rep->fontSize += 2;
203                                 $rep->NewLine(1, 7);
204                                 $rep->Font('bold');
205                                 $rep->TextCol(0, 3,     $myrow['salesman_name']);
206                                 $sman = $myrow['salesman_name'];
207                                 $rep->fontSize -= 2;
208                                 $rep->Font();
209                                 $rep->NewLine();
210                         }
211                         $rep->NewLine();
212                         // Here starts the new report lines 2010-11-02 Joe Hunt
213                         $contacts = get_contacts_for_branch($myrow['branch_code']);
214                         $rep->TextCol(0, 1,     $myrow['name']);
215                         $rep->TextCol(1, 2,     _('Price List') . ": " . $myrow['sales_type']);
216                         $rep->TextCol(2, 3,     $myrow['br_name']);
217                         $rep->NewLine();
218                         $adr = Explode("\n", $myrow['address']);
219                         if ($myrow['br_post_address'] == '')
220                                 $adr2 = Explode("\n", $myrow['br_address']);
221                         else
222                                 $adr2 = Explode("\n", $myrow['br_post_address']);
223                         $count1 = count($adr);
224                         $count2 = count($adr2);
225                         $count1 = max($count1, $count2);
226                         $count1 = max($count1, 4); 
227                         if (isset($adr[0]))
228                                 $rep->TextCol(0, 1, $adr[0]);
229                         $rep->TextCol(1, 2,     _('Currency') . ": " . $myrow['curr_code']);
230                         if (isset($contacts[0]))
231                                 $rep->TextCol(2, 3, $contacts[0]['name']. " " .$contacts[0]['name2']);
232                         if (isset($adr2[0]))    
233                                 $rep->TextCol(3, 4, $adr2[0]);
234                         $rep->NewLine();
235                         if (isset($adr[1]))
236                                 $rep->TextCol(0, 1, $adr[1]);
237                         if ($myrow['dimension_id'] != 0)
238                         {
239                                 $dim = get_dimension($myrow['dimension_id']);
240                                 $rep->TextCol(1, 2,     _('Dimension') . ": " . $dim['name']);
241                         }               
242                         if (isset($contacts[0]))
243                                 $rep->TextCol(2, 3, _('Ph') . ": " . $contacts[0]['phone']);
244                         if (isset($adr2[1]))
245                                 $rep->TextCol(3, 4, $adr2[1]);
246                         $rep->NewLine();
247                         if (isset($adr[2]))
248                                 $rep->TextCol(0, 1, $adr[2]);
249                         if ($myrow['dimension2_id'] != 0)
250                         {
251                                 $dim = get_dimension($myrow['dimension2_id']);
252                                 $rep->TextCol(1, 2,     _('Dimension') . " 2: " . $dim['name']);
253                         }       
254                         if (isset($contacts[0]))
255                                 $rep->TextCol(2, 3, _('Fax') . ": " . $contacts[0]['fax']);
256                         if (isset($adr2[2]))
257                                 $rep->TextCol(3, 4, $adr2[2]);
258                         if ($more != 0.0 || $less != 0.0)
259                                 $rep->TextCol(1, 2,     _('Turnover') . ": " . number_format2($turnover, $dec));
260                         for ($i = 3; $i < $count1; $i++)
261                         {
262                                 $rep->NewLine();
263                                 if (isset($adr[$i]))
264                                         $rep->TextCol(0, 1, $adr[$i]);
265                                 if ($i == 3 && isset($contacts[0]) && isset($contacts[0]['email']))     
266                                         $rep->TextCol(2, 3, _('Email') . ": " . $contacts[0]['email']);
267                                 if (isset($adr2[$i]))
268                                         $rep->TextCol(3, 4, $adr2[$i]);
269                         }       
270                         $rep->NewLine();
271                         /*
272                         $rep->TextCol(0, 1,     $myrow['name']);
273                         $adr = Explode("\n", $myrow['address']);
274                         $count1 = count($adr);
275                         for ($i = 0; $i < $count1; $i++)
276                                 $rep->TextCol(0, 1, $adr[$i], 0, ($i + 1) * $rep->lineHeight);
277                         $count1++;
278                         $rep->TextCol(1, 2,     _('Price List') . ": " . $myrow['sales_type']);
279                         if ($more != 0.0 || $less != 0.0)
280                                 $rep->TextCol(1, 2,     _('Turnover') . ": " . number_format2($turnover, $dec), 0, $rep->lineHeight);
281                         $rep->TextCol(2, 3,     $myrow['br_name']);
282
283                         $contacts = get_branch_contacts($myrow['branch_code']);
284                         if (isset($contacts[0]))
285                         {
286                                 $rep->TextCol(2, 3, $contacts[0]['name'], 0, $rep->lineHeight);
287                                 $rep->TextCol(2, 3, _('Ph') . ": " . $contacts[0]['phone'], 0, 2 * $rep->lineHeight);
288                                 $rep->TextCol(2, 3, _('Fax') . ": " . $contacts[0]['fax'], 0, 3 * $rep->lineHeight);
289                         }
290                         $adr = Explode("\n", $myrow['br_address']);
291                         $count2 = count($adr);
292                         for ($i = 0; $i < $count2; $i++)
293                                 $rep->TextCol(3, 4, $adr[$i], 0, ($i + 1) * $rep->lineHeight);
294                         //$rep->TextCol(3, 4, $myrow['email'], 0, ($count2 + 1) * $rep->lineHeight);
295                         $count2++;
296                         $count1 = Max($count1, $count2);
297                         $count1 = Max($count1, 4);
298                         $rep->NewLine($count3);
299                         */
300                         $rep->Line($rep->row + 8);
301                         $rep->NewLine(0, 3);
302                 }
303         }
304     $rep->End();
305 }
306
307 ?>