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