e7e6255dadb79ae3db3232aa94edda072b82b132
[fa-stable.git] / reporting / rep205.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_SUPPLIERANALYTIC';
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 . "/includes/db/crm_contacts_db.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
26
27 //----------------------------------------------------------------------------------------------------
28
29 print_supplier_details_listing();
30
31 function get_supplier_details_for_report()
32 {
33         $sql = "SELECT ".TB_PREF."suppliers.supplier_id,
34                         ".TB_PREF."suppliers.supp_name,
35                         ".TB_PREF."suppliers.address,
36                         ".TB_PREF."suppliers.supp_address,
37                         ".TB_PREF."suppliers.supp_ref,
38                         ".TB_PREF."suppliers.contact,
39                         ".TB_PREF."suppliers.curr_code,
40                         ".TB_PREF."suppliers.dimension_id,
41                         ".TB_PREF."suppliers.dimension2_id,
42                         ".TB_PREF."suppliers.gst_no
43                 FROM ".TB_PREF."suppliers
44                 ORDER BY supp_name";
45
46     return db_query($sql,"No transactions were returned");
47 }
48
49
50 function getTransactions($supplier_id, $date)
51 {
52         $date = date2sql($date);
53
54         $sql = "SELECT SUM((ov_amount+ov_discount)*rate) AS Turnover
55                 FROM ".TB_PREF."supp_trans
56                 WHERE supplier_id=".db_escape($supplier_id)."
57                 AND (type=".ST_SUPPINVOICE." OR type=".ST_SUPPCREDIT.")
58                 AND tran_date >='$date'";
59
60     $result = db_query($sql,"No transactions were returned");
61
62         $row = db_fetch_row($result);
63         return $row[0];
64 }
65
66 //----------------------------------------------------------------------------------------------------
67
68 function print_supplier_details_listing()
69 {
70     global $path_to_root;
71
72     $from = $_POST['PARAM_0'];
73     $more = $_POST['PARAM_1'];
74     $less = $_POST['PARAM_2'];
75     $comments = $_POST['PARAM_3'];
76         $orientation = $_POST['PARAM_4'];
77         $destination = $_POST['PARAM_5'];
78         if ($destination)
79                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
80         else
81                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
82
83         $orientation = ($orientation ? 'L' : 'P');
84     $dec = 0;
85
86         if ($more != '')
87                 $morestr = _('Greater than ') . number_format2($more, $dec);
88         else
89                 $morestr = '';
90         if ($less != '')
91                 $lessstr = _('Less than ') . number_format2($less, $dec);
92         else
93                 $lessstr = '';
94
95         $more = (double)$more;
96         $less = (double)$less;
97
98         $cols = array(0, 150, 300, 425, 550);
99
100         $headers = array(_('Mailing Address:'), _('Turnover'),  _('Contact Information'),
101                 _('Physical Address'));
102
103         $aligns = array('left', 'left', 'left', 'left');
104
105     $params =   array(  0 => $comments,
106                                     1 => array('text' => _('Activity Since'),   'from' => $from,                'to' => ''),
107                                     2 => array('text' => _('Activity'),                 'from' => $morestr,     'to' => $lessstr . " " . get_company_pref("curr_default")));
108
109     $rep = new FrontReport(_('Supplier Details Listing'), "SupplierDetailsListing", user_pagesize(), 9, $orientation);
110     if ($orientation == 'L')
111         recalculate_cols($cols);
112
113     $rep->Font();
114     $rep->Info($params, $cols, $headers, $aligns);
115     $rep->NewPage();
116
117         $result = get_supplier_details_for_report();
118
119         while ($myrow=db_fetch($result))
120         {
121                 $printsupplier = true;
122                 if ($more != '' || $less != '')
123                 {
124                         $turnover = getTransactions($myrow['supplier_id'], $from);
125                         if ($more != 0.0 && $turnover <= (double)$more)
126                                 $printsupplier = false;
127                         if ($less != 0.0 && $turnover >= (double)$less)
128                                 $printsupplier = false;
129                 }
130                 if ($printsupplier)
131                 {
132                         $rep->NewLine();
133                         // Here starts the new report lines 2013-01-28 Joe Hunt
134                         $contacts = get_supplier_contacts($myrow['supplier_id']);
135                         $rep->TextCol(0, 1,     $myrow['supp_name']);
136                         $rep->TextCol(1, 2,     _('Tax_Id') . ": " . $myrow['gst_no']);
137                         $rep->TextCol(2, 3,     $myrow['contact']);
138                         $rep->NewLine();
139                         $adr = Explode("\n", $myrow['address']);
140                         $adr2 = Explode("\n", $myrow['supp_address']);
141                         $count1 = count($adr);
142                         $count2 = count($adr2);
143                         $count1 = max($count1, $count2);
144                         $count1 = max($count1, 4); 
145                         if (isset($adr[0]))
146                                 $rep->TextCol(0, 1, $adr[0]);
147                         $rep->TextCol(1, 2,     _('Currency') . ": " . $myrow['curr_code']);
148                         if (isset($contacts[0]))
149                                 $rep->TextCol(2, 3, $contacts[0]['name']. " " .$contacts[0]['name2']);
150                         if (isset($adr2[0]))    
151                                 $rep->TextCol(3, 4, $adr2[0]);
152                         $rep->NewLine();
153                         if (isset($adr[1]))
154                                 $rep->TextCol(0, 1, $adr[1]);
155                         if ($myrow['dimension_id'] != 0)
156                         {
157                                 $dim = get_dimension($myrow['dimension_id']);
158                                 $rep->TextCol(1, 2,     _('Dimension') . ": " . $dim['name']);
159                         }               
160                         if (isset($contacts[0]))
161                                 $rep->TextCol(2, 3, _('Ph') . ": " . $contacts[0]['phone']);
162                         if (isset($adr2[1]))
163                                 $rep->TextCol(3, 4, $adr2[1]);
164                         $rep->NewLine();
165                         if (isset($adr[2]))
166                                 $rep->TextCol(0, 1, $adr[2]);
167                         if ($myrow['dimension2_id'] != 0)
168                         {
169                                 $dim = get_dimension($myrow['dimension2_id']);
170                                 $rep->TextCol(1, 2,     _('Dimension') . " 2: " . $dim['name']);
171                         }       
172                         if (isset($contacts[0]))
173                                 $rep->TextCol(2, 3, _('Fax') . ": " . $contacts[0]['fax']);
174                         if (isset($adr2[2]))
175                                 $rep->TextCol(3, 4, $adr2[2]);
176                         if ($more != 0.0 || $less != 0.0)
177                                 $rep->TextCol(1, 2,     _('Turnover') . ": " . number_format2($turnover, $dec));
178                         for ($i = 3; $i < $count1; $i++)
179                         {
180                                 $rep->NewLine();
181                                 if (isset($adr[$i]))
182                                         $rep->TextCol(0, 1, $adr[$i]);
183                                 if ($i == 3 && isset($contacts[0]) && isset($contacts[0]['email']))     
184                                         $rep->TextCol(2, 3, _('Email') . ": " . $contacts[0]['email']);
185                                 if (isset($adr2[$i]))
186                                         $rep->TextCol(3, 4, $adr2[$i]);
187                         }       
188                         $rep->NewLine();
189                         $rep->Line($rep->row + 8);
190                         $rep->NewLine(0, 3);
191                 }
192         }
193     $rep->End();
194 }
195
196 ?>