Fixed start order in initialy sorted pagers.
[fa-stable.git] / purchasing / inquiry / supplier_inquiry.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 $page_security=2;
13 $path_to_root="../..";
14 include($path_to_root . "/includes/db_pager.inc");
15 include($path_to_root . "/includes/session.inc");
16
17 include($path_to_root . "/purchasing/includes/purchasing_ui.inc");
18
19 $js = "";
20 if ($use_popup_windows)
21         $js .= get_js_open_window(900, 500);
22 if ($use_date_picker)
23         $js .= get_js_date_picker();
24 page(_("Supplier Inquiry"), false, false, "", $js);
25
26 if (isset($_GET['supplier_id'])){
27         $_POST['supplier_id'] = $_GET['supplier_id'];
28 }
29 if (isset($_GET['FromDate'])){
30         $_POST['TransAfterDate'] = $_GET['FromDate'];
31 }
32 if (isset($_GET['ToDate'])){
33         $_POST['TransToDate'] = $_GET['ToDate'];
34 }
35
36 //------------------------------------------------------------------------------------------------
37
38 start_form(false, true);
39
40 if (!isset($_POST['supplier_id']))
41         $_POST['supplier_id'] = get_global_supplier();
42
43 start_table("class='tablestyle_noborder'");
44 start_row();
45
46 supplier_list_cells(_("Select a supplier:"), 'supplier_id', null, true);
47
48 date_cells(_("From:"), 'TransAfterDate', '', null, -30);
49 date_cells(_("To:"), 'TransToDate');
50
51 supp_allocations_list_cell("filterType", null);
52
53 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), true);
54
55 end_row();
56 end_table();
57 end_form();
58 set_global_supplier($_POST['supplier_id']);
59
60 //------------------------------------------------------------------------------------------------
61
62 function display_supplier_summary($supplier_record)
63 {
64         global $table_style;
65
66         $past1 = get_company_pref('past_due_days');
67         $past2 = 2 * $past1;
68         $nowdue = "1-" . $past1 . " " . _('Days');
69         $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
70         $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
71         
72
73     start_table("width=80% $table_style");
74     $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
75         $pastdue1, $pastdue2, _("Total Balance"));
76
77         table_header($th);
78     start_row();
79         label_cell($supplier_record["curr_code"]);
80     label_cell($supplier_record["terms"]);
81     amount_cell($supplier_record["Balance"] - $supplier_record["Due"]);
82     amount_cell($supplier_record["Due"] - $supplier_record["Overdue1"]);
83     amount_cell($supplier_record["Overdue1"] - $supplier_record["Overdue2"]);
84     amount_cell($supplier_record["Overdue2"]);
85     amount_cell($supplier_record["Balance"]);
86     end_row();
87     end_table(1);
88 }
89 //------------------------------------------------------------------------------------------------
90
91 div_start('totals_tbl');
92 if (($_POST['supplier_id'] != "") && ($_POST['supplier_id'] != reserved_words::get_all()))
93 {
94         $supplier_record = get_supplier_details($_POST['supplier_id']);
95     display_supplier_summary($supplier_record);
96 }
97 div_end();
98
99 if(get_post('RefreshInquiry'))
100 {
101         $Ajax->activate('totals_tbl');
102 }
103
104 //------------------------------------------------------------------------------------------------
105 function systype_name($dummy, $type)
106 {
107         return systypes::name($type);
108 }
109
110 function trans_view($trans)
111 {
112         return get_trans_view_str($trans["type"], $trans["trans_no"]);
113 }
114
115 function due_date($row)
116 {
117         return ($row["type"]== 20) || ($row["type"]== 21)
118                  ? $row["due_date"] : '';
119 }
120
121 function gl_view($row)
122 {
123         return get_gl_view_str($row["type"], $row["trans_no"]);
124 }
125
126 function credit_link($row)
127 {
128         return $row['type'] == 20 && $row["TotalAmount"] - $row["Allocated"] > 0 ?
129                 pager_link(_("Credit This"),
130                         "/purchasing/supplier_credit.php?New=1&invoice_no=".
131                         $row['trans_no'], ICON_CREDIT)
132                         : '';
133 }
134
135 function fmt_debit($row)
136 {
137         $value = $row["TotalAmount"];
138         return $value>=0 ? price_format($value) : '';
139
140 }
141
142 function fmt_credit($row)
143 {
144         $value = -$row["TotalAmount"];
145         return $value>0 ? price_format($value) : '';
146 }
147
148 function prt_link($row)
149 {
150                 return print_document_link($row['trans_no'], _("Print"), true, $row['type']);
151 }
152
153 function check_overdue($row)
154 {
155         return $row['OverDue'] == 1
156                 && (abs($row["TotalAmount"]) - $row["Allocated"] != 0);
157 }
158 //------------------------------------------------------------------------------------------------
159
160     $date_after = date2sql($_POST['TransAfterDate']);
161     $date_to = date2sql($_POST['TransToDate']);
162
163     // Sherifoz 22.06.03 Also get the description
164     $sql = "SELECT trans.type, 
165                 trans.trans_no,
166                 trans.reference, 
167                 supplier.supp_name, 
168                 trans.supp_reference,
169         trans.tran_date, 
170                 trans.due_date,
171                 supplier.curr_code, 
172         (trans.ov_amount + trans.ov_gst  + trans.ov_discount) AS TotalAmount, 
173                 trans.alloc AS Allocated,
174                 ((trans.type = 20 OR trans.type = 21) AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue,
175         (ABS(trans.ov_amount + trans.ov_gst  + trans.ov_discount - trans.alloc) <= 0.005) AS Settled
176         FROM ".TB_PREF."supp_trans as trans, ".TB_PREF."suppliers as supplier
177         WHERE supplier.supplier_id = trans.supplier_id
178         AND trans.tran_date >= '$date_after'
179         AND trans.tran_date <= '$date_to'";
180         if ($_POST['supplier_id'] != reserved_words::get_all())
181                 $sql .= " AND trans.supplier_id = '" . $_POST['supplier_id'] . "'";
182         if (isset($_POST['filterType']) && $_POST['filterType'] != reserved_words::get_all())
183         {
184                 if (($_POST['filterType'] == '1')) 
185                 {
186                         $sql .= " AND (trans.type = 20 OR trans.type = 2)";
187                 } 
188                 elseif (($_POST['filterType'] == '2')) 
189                 {
190                         $sql .= " AND trans.type = 20 ";
191                 } 
192                 elseif ($_POST['filterType'] == '3') 
193                 {
194                         $sql .= " AND (trans.type = 22 OR trans.type = 1) ";
195                 } 
196                 elseif (($_POST['filterType'] == '4') || ($_POST['filterType'] == '5')) 
197                 {
198                         $sql .= " AND trans.type = 21  ";
199                 }
200
201                 if (($_POST['filterType'] == '2') || ($_POST['filterType'] == '5')) 
202                 {
203                         $today =  date2sql(Today());
204                         $sql .= " AND trans.due_date < '$today' ";
205                 }
206         }
207
208 $cols = array(
209                         _("Type") => array('fun'=>'systype_name', 'ord'=>''), 
210                         _("#") => array('fun'=>'trans_view', 'ord'=>''), 
211                         _("Reference"), 
212                         _("Supplier"),
213                         _("Supplier's Reference"), 
214                         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'desc'), 
215                         _("Due Date") => array('type'=>'date', 'fun'=>'due_date'), 
216                         _("Currency") => array('align'=>'center'),
217                         _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'), 
218                         _("Credit") => array('align'=>'right', 'insert'=>true,'fun'=>'fmt_credit'), 
219                         array('insert'=>true, 'fun'=>'gl_view'),
220                         array('insert'=>true, 'fun'=>'credit_link')
221                         );
222
223 if ($_POST['supplier_id'] != reserved_words::get_all())
224 {
225         $cols[_("Supplier")] = 'skip';
226         $cols[_("Currency")] = 'skip';
227 }
228 //------------------------------------------------------------------------------------------------
229
230
231 /*show a table of the transactions returned by the sql */
232 $table =& new_db_pager('trans_tbl', $sql, $cols);
233 $table->set_marker('check_overdue', _("Marked items are overdue."));
234
235 if (get_post('RefreshInquiry')) {
236         $table->set_sql($sql);
237         $table->set_columns($cols);
238 }
239 $table->width = "85%";
240 start_form();
241
242 display_db_pager($table);
243
244 end_form();
245 end_page();
246
247 ?>