Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[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 debtor.debtor_no,
33                         debtor.name,
34                         debtor.address,
35                         debtor.curr_code,
36                         debtor.dimension_id,
37                         debtor.dimension2_id,
38                         debtor.notes,
39                         pricelist.sales_type,
40                         branch.branch_code,
41                         branch.br_name,
42                         branch.br_address,
43                         branch.br_post_address,
44                         branch.area,
45                         branch.salesman,
46                         area.description,
47                         salesman.salesman_name
48                 FROM ".TB_PREF."debtors_master debtor
49                 INNER JOIN ".TB_PREF."cust_branch branch ON debtor.debtor_no=branch.debtor_no
50                 INNER JOIN ".TB_PREF."sales_types pricelist     ON debtor.sales_type=pricelist.id
51                 INNER JOIN ".TB_PREF."areas area ON branch.area = area.area_code
52                 INNER JOIN ".TB_PREF."salesman salesman ON branch.salesman=salesman.salesman_code
53                 WHERE debtor.inactive = 0";
54         if ($area != 0)
55         {
56                 if ($salesid != 0)
57                         $sql .= " AND salesman.salesman_code=".db_escape($salesid)."
58                                 AND area.area_code=".db_escape($area);
59                 else
60                         $sql .= " AND area.area_code=".db_escape($area);
61         }
62         elseif ($salesid != 0)
63                 $sql .= " AND salesman.salesman_code=".db_escape($salesid);
64         $sql .= " ORDER BY description,
65                         salesman.salesman_name,
66                         debtor.debtor_no,
67                         branch.branch_code";
68
69     return db_query($sql,"No transactions were returned");
70 }
71
72 function get_contacts_for_branch($branch)
73 {
74         $sql = "SELECT p.*, r.action, r.type, CONCAT(r.type,'.',r.action) as ext_type 
75                 FROM ".TB_PREF."crm_persons p,"
76                         .TB_PREF."crm_contacts r
77                 WHERE r.person_id=p.id AND r.type='cust_branch' 
78                         AND r.entity_id=".db_escape($branch);
79         $res = db_query($sql, "can't retrieve branch contacts");
80         $results = array();
81         while($contact = db_fetch($res))
82                 $results[] = $contact;
83         return $results;
84 }
85
86 function getTransactions($debtorno, $branchcode, $date)
87 {
88         $date = date2sql($date);
89
90         $sql = "SELECT SUM((ov_amount+ov_freight+ov_discount)*rate) AS Turnover
91                 FROM ".TB_PREF."debtor_trans
92                 WHERE debtor_no=".db_escape($debtorno)."
93                 AND branch_code=".db_escape($branchcode)."
94                 AND (type=".ST_SALESINVOICE." OR type=".ST_CUSTCREDIT.")
95                 AND tran_date >='$date'";
96
97     $result = db_query($sql,"No transactions were returned");
98
99         $row = db_fetch_row($result);
100         return $row[0];
101 }
102
103 //----------------------------------------------------------------------------------------------------
104
105 function print_customer_details_listing()
106 {
107     global $path_to_root;
108
109     $from = $_POST['PARAM_0'];
110     $area = $_POST['PARAM_1'];
111     $folk = $_POST['PARAM_2'];
112     $more = $_POST['PARAM_3'];
113     $less = $_POST['PARAM_4'];
114     $comments = $_POST['PARAM_5'];
115         $orientation = $_POST['PARAM_6'];
116         $destination = $_POST['PARAM_7'];
117         if ($destination)
118                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
119         else
120                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
121
122         $orientation = ($orientation ? 'L' : 'P');
123     $dec = 0;
124
125         if ($area == ALL_NUMERIC)
126                 $area = 0;
127         if ($folk == ALL_NUMERIC)
128                 $folk = 0;
129
130         if ($area == 0)
131                 $sarea = _('All Areas');
132         else
133                 $sarea = get_area_name($area);
134         if ($folk == 0)
135                 $salesfolk = _('All Sales Folk');
136         else
137                 $salesfolk = get_salesman_name($folk);
138         if ($more != '')
139                 $morestr = _('Greater than ') . number_format2($more, $dec);
140         else
141                 $morestr = '';
142         if ($less != '')
143                 $lessstr = _('Less than ') . number_format2($less, $dec);
144         else
145                 $lessstr = '';
146
147         $more = (double)$more;
148         $less = (double)$less;
149
150         $cols = array(0, 150, 300, 425, 550);
151
152         $headers = array(_('Customer Postal Address'), _('Price/Turnover'),     _('Branch Contact Information'),
153                 _('Branch Delivery Address'));
154
155         $aligns = array('left', 'left', 'left', 'left');
156
157     $params =   array(  0 => $comments,
158                                     1 => array('text' => _('Activity Since'),   'from' => $from,                'to' => ''),
159                                     2 => array('text' => _('Sales Areas'),              'from' => $sarea,               'to' => ''),
160                                     3 => array('text' => _('Sales Folk'),               'from' => $salesfolk,   'to' => ''),
161                                     4 => array('text' => _('Activity'),                 'from' => $morestr,     'to' => $lessstr . " " . get_company_pref("curr_default")));
162
163     $rep = new FrontReport(_('Customer Details Listing'), "CustomerDetailsListing", user_pagesize(), 9, $orientation);
164     if ($orientation == 'L')
165         recalculate_cols($cols);
166
167     $rep->Font();
168     $rep->Info($params, $cols, $headers, $aligns);
169     $rep->NewPage();
170
171         $result = get_customer_details_for_report($area, $folk);
172
173         $carea = '';
174         $sman = '';
175         while ($myrow=db_fetch($result))
176         {
177                 $printcustomer = true;
178                 if ($more != '' || $less != '')
179                 {
180                         $turnover = getTransactions($myrow['debtor_no'], $myrow['branch_code'], $from);
181                         if ($more != 0.0 && $turnover <= (double)$more)
182                                 $printcustomer = false;
183                         if ($less != 0.0 && $turnover >= (double)$less)
184                                 $printcustomer = false;
185                 }
186                 if ($printcustomer)
187                 {
188                         $newrow = 0;
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
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 ($myrow['notes'] != '')
255                         {
256                                 $oldrow = $rep->row;
257                                 $rep->NewLine();
258                                 $rep->TextColLines(1, 2, _("General Notes:")." ".$myrow['notes'], -2);
259                                 $newrow = $rep->row;
260                                 $rep->row = $oldrow;
261                         }       
262                         if (isset($contacts[0]))
263                                 $rep->TextCol(2, 3, _('Fax') . ": " . $contacts[0]['fax']);
264                         if (isset($adr2[2]))
265                                 $rep->TextCol(3, 4, $adr2[2]);
266                         if ($more != 0.0 || $less != 0.0)
267                                 $rep->TextCol(1, 2,     _('Turnover') . ": " . number_format2($turnover, $dec));
268                         for ($i = 3; $i < $count1; $i++)
269                         {
270                                 $rep->NewLine();
271                                 if (isset($adr[$i]))
272                                         $rep->TextCol(0, 1, $adr[$i]);
273                                 if ($i == 3 && isset($contacts[0]) && isset($contacts[0]['email']))     
274                                         $rep->TextCol(2, 3, _('Email') . ": " . $contacts[0]['email']);
275                                 if (isset($adr2[$i]))
276                                         $rep->TextCol(3, 4, $adr2[$i]);
277                         }       
278                         if ($newrow != 0 && $newrow < $rep->row)
279                                 $rep->row = $newrow;
280                         $rep->NewLine();
281                         $rep->Line($rep->row + 8);
282                         $rep->NewLine(0, 3);
283                 }
284         }
285     $rep->End();
286 }
287