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