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