b76c949fb5f5f3fe4b5c29b554c69e9e10d97c7c
[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_once($path_to_root . "/includes/db_pager.inc");
15 include_once($path_to_root . "/includes/session.inc");
16
17 include_once($path_to_root . "/purchasing/includes/purchasing_ui.inc");
18 include_once($path_to_root . "/reporting/includes/reporting.inc");
19
20 $js = "";
21 if ($SysPrefs->use_popup_windows)
22         $js .= get_js_open_window(900, 500);
23 if (user_use_date_picker())
24         $js .= get_js_date_picker();
25 page(_($help_context = "Supplier Inquiry"), isset($_GET['supplier_id']), false, "", $js);
26
27 if (isset($_GET['supplier_id'])){
28         $_POST['supplier_id'] = $_GET['supplier_id'];
29 }
30
31 if (isset($_GET['FromDate'])){
32         $_POST['TransAfterDate'] = $_GET['FromDate'];
33 }
34 if (isset($_GET['ToDate'])){
35         $_POST['TransToDate'] = $_GET['ToDate'];
36 }
37
38 //------------------------------------------------------------------------------------------------
39
40 start_form();
41
42 if (!isset($_POST['supplier_id']))
43         $_POST['supplier_id'] = get_global_supplier();
44
45 start_table(TABLESTYLE_NOBORDER);
46 start_row();
47
48 if (!$page_nested)
49         supplier_list_cells(_("Select a supplier:"), 'supplier_id', null, true, false, false, true);
50
51 date_cells(_("From:"), 'TransAfterDate', '', null, -user_transaction_days());
52 date_cells(_("To:"), 'TransToDate');
53
54 supp_transactions_list_cell("filterType", null, true);
55
56 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), 'default');
57
58 end_row();
59 end_table();
60 set_global_supplier($_POST['supplier_id']);
61
62 //------------------------------------------------------------------------------------------------
63
64 function display_supplier_summary($supplier_record)
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(TABLESTYLE, "width='80%'");
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'] != ALL_TEXT))
93 {
94         $supplier_record = get_supplier_details($_POST['supplier_id'], $_POST['TransToDate']);
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         global $systypes_array;
108         return $systypes_array[$type];
109 }
110
111 function trans_view($trans)
112 {
113         return get_trans_view_str($trans["type"], $trans["trans_no"]);
114 }
115
116 function due_date($row)
117 {
118         return ($row["type"]== ST_SUPPINVOICE) || ($row["type"]== ST_SUPPCREDIT) ? $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         global $page_nested;
129
130         if ($page_nested)
131                 return '';
132         return $row['type'] == ST_SUPPINVOICE && $row["TotalAmount"] - $row["Allocated"] > 0 ?
133                 pager_link(_("Credit This"),
134                         "/purchasing/supplier_credit.php?New=1&invoice_no=".
135                         $row['trans_no'], ICON_CREDIT)
136                         : '';
137 }
138
139 function fmt_debit($row)
140 {
141         $value = $row["TotalAmount"];
142         return $value>0 ? price_format($value) : '';
143
144 }
145
146 function fmt_credit($row)
147 {
148         $value = -$row["TotalAmount"];
149         return $value>0 ? price_format($value) : '';
150 }
151
152 function prt_link($row)
153 {
154         if ($row['type'] == ST_SUPPAYMENT || $row['type'] == ST_BANKPAYMENT || $row['type'] == ST_SUPPCREDIT) 
155                 return print_document_link($row['trans_no']."-".$row['type'], _("Print Remittance"), true, ST_SUPPAYMENT, ICON_PRINT);
156 }
157
158 function check_overdue($row)
159 {
160         return $row['OverDue'] == 1
161                 && (abs($row["TotalAmount"]) - $row["Allocated"] != 0);
162 }
163
164 function edit_link($row)
165 {
166         global $page_nested;
167
168         if ($page_nested)
169                 return '';
170         return trans_editor_link($row['type'], $row['trans_no']);
171 }
172 //------------------------------------------------------------------------------------------------
173
174 $sql = get_sql_for_supplier_inquiry(get_post('filterType'), get_post('TransAfterDate'), get_post('TransToDate'), get_post('supplier_id'));
175
176 $cols = array(
177                         _("Type") => array('fun'=>'systype_name', 'ord'=>''), 
178                         _("#") => array('fun'=>'trans_view', 'ord'=>''), 
179                         _("Reference"), 
180                         _("Supplier"),
181                         _("Supplier's Reference"), 
182                         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'desc'), 
183                         _("Due Date") => array('type'=>'date', 'fun'=>'due_date'), 
184                         _("Currency") => array('align'=>'center'),
185                         _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'), 
186                         _("Credit") => array('align'=>'right', 'insert'=>true,'fun'=>'fmt_credit'), 
187                         array('insert'=>true, 'fun'=>'gl_view'),
188                         array('insert'=>true, 'fun'=>'credit_link'),
189                         array('insert'=>true, 'fun'=>'prt_link'),
190                         array('insert'=>true, 'fun'=>'edit_link')
191                         );
192
193 if ($_POST['supplier_id'] != ALL_TEXT)
194 {
195         $cols[_("Supplier")] = 'skip';
196         $cols[_("Currency")] = 'skip';
197 }
198
199 /*show a table of the transactions returned by the sql */
200 $table =& new_db_pager('trans_tbl', $sql, $cols);
201 $table->set_marker('check_overdue', _("Marked items are overdue."));
202
203 $table->width = "85%";
204
205 display_db_pager($table);
206
207 end_form();
208 end_page();
209