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