7a012905589c617165d86a2bdcc657149840e8ce
[fa-stable.git] / purchasing / inquiry / supplier_inquiry.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_SUPPTRANSVIEW';
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(_($help_context = "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();
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'), 'default');
54
55 end_row();
56 end_table();
57 set_global_supplier($_POST['supplier_id']);
58
59 //------------------------------------------------------------------------------------------------
60
61 function display_supplier_summary($supplier_record)
62 {
63         global $table_style;
64
65         $past1 = get_company_pref('past_due_days');
66         $past2 = 2 * $past1;
67         $nowdue = "1-" . $past1 . " " . _('Days');
68         $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
69         $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
70         
71
72     start_table("width=80% $table_style");
73     $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
74         $pastdue1, $pastdue2, _("Total Balance"));
75
76         table_header($th);
77     start_row();
78         label_cell($supplier_record["curr_code"]);
79     label_cell($supplier_record["terms"]);
80     amount_cell($supplier_record["Balance"] - $supplier_record["Due"]);
81     amount_cell($supplier_record["Due"] - $supplier_record["Overdue1"]);
82     amount_cell($supplier_record["Overdue1"] - $supplier_record["Overdue2"]);
83     amount_cell($supplier_record["Overdue2"]);
84     amount_cell($supplier_record["Balance"]);
85     end_row();
86     end_table(1);
87 }
88 //------------------------------------------------------------------------------------------------
89
90 div_start('totals_tbl');
91 if (($_POST['supplier_id'] != "") && ($_POST['supplier_id'] != ALL_TEXT))
92 {
93         $supplier_record = get_supplier_details($_POST['supplier_id']);
94     display_supplier_summary($supplier_record);
95 }
96 div_end();
97
98 if(get_post('RefreshInquiry'))
99 {
100         $Ajax->activate('totals_tbl');
101 }
102
103 //------------------------------------------------------------------------------------------------
104 function systype_name($dummy, $type)
105 {
106         global $systypes_array;
107         return $systypes_array[$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"]== ST_SUPPINVOICE) || ($row["type"]== ST_SUPPCREDIT) ? $row["due_date"] : '';
118 }
119
120 function gl_view($row)
121 {
122         return get_gl_view_str($row["type"], $row["trans_no"]);
123 }
124
125 function credit_link($row)
126 {
127         return $row['type'] == ST_SUPPINVOICE && $row["TotalAmount"] - $row["Allocated"] > 0 ?
128                 pager_link(_("Credit This"),
129                         "/purchasing/supplier_credit.php?New=1&invoice_no=".
130                         $row['trans_no'], ICON_CREDIT)
131                         : '';
132 }
133
134 function fmt_debit($row)
135 {
136         $value = $row["TotalAmount"];
137         return $value>=0 ? price_format($value) : '';
138
139 }
140
141 function fmt_credit($row)
142 {
143         $value = -$row["TotalAmount"];
144         return $value>0 ? price_format($value) : '';
145 }
146
147 function prt_link($row)
148 {
149                 return print_document_link($row['trans_no'], _("Print"), true, $row['type']);
150 }
151
152 function check_overdue($row)
153 {
154         return $row['OverDue'] == 1
155                 && (abs($row["TotalAmount"]) - $row["Allocated"] != 0);
156 }
157 //------------------------------------------------------------------------------------------------
158
159 $sql = get_sql_for_supplier_inquiry();
160
161 $cols = array(
162                         _("Type") => array('fun'=>'systype_name', 'ord'=>''), 
163                         _("#") => array('fun'=>'trans_view', 'ord'=>''), 
164                         _("Reference"), 
165                         _("Supplier"),
166                         _("Supplier's Reference"), 
167                         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'desc'), 
168                         _("Due Date") => array('type'=>'date', 'fun'=>'due_date'), 
169                         _("Currency") => array('align'=>'center'),
170                         _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'), 
171                         _("Credit") => array('align'=>'right', 'insert'=>true,'fun'=>'fmt_credit'), 
172                         array('insert'=>true, 'fun'=>'gl_view'),
173                         array('insert'=>true, 'fun'=>'credit_link')
174                         );
175
176 if ($_POST['supplier_id'] != ALL_TEXT)
177 {
178         $cols[_("Supplier")] = 'skip';
179         $cols[_("Currency")] = 'skip';
180 }
181 //------------------------------------------------------------------------------------------------
182
183
184 /*show a table of the transactions returned by the sql */
185 $table =& new_db_pager('trans_tbl', $sql, $cols);
186 $table->set_marker('check_overdue', _("Marked items are overdue."));
187
188 $table->width = "85%";
189
190 display_db_pager($table);
191
192 end_form();
193 end_page();
194
195 ?>