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