1e106d0edebbf31be72210045d1c2a62c1c5e486
[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 if (!@$_GET['popup'])
21 {
22         $js = "";
23         if ($use_popup_windows)
24                 $js .= get_js_open_window(900, 500);
25         if ($use_date_picker)
26                 $js .= get_js_date_picker();
27         page(_($help_context = "Supplier Inquiry"), isset($_GET['supplier_id']), false, "", $js);
28 }
29 if (isset($_GET['supplier_id'])){
30         $_POST['supplier_id'] = $_GET['supplier_id'];
31 }
32 if (isset($_GET['FromDate'])){
33         $_POST['TransAfterDate'] = $_GET['FromDate'];
34 }
35 if (isset($_GET['ToDate'])){
36         $_POST['TransToDate'] = $_GET['ToDate'];
37 }
38
39 //------------------------------------------------------------------------------------------------
40
41 if (!@$_GET['popup'])
42         start_form();
43
44 if (!isset($_POST['supplier_id']))
45         $_POST['supplier_id'] = get_global_supplier();
46
47 start_table(TABLESTYLE_NOBORDER);
48 start_row();
49
50 if (!@$_GET['popup'])
51         supplier_list_cells(_("Select a supplier:"), 'supplier_id', null, true, false, false, !@$_GET['popup']);
52
53 date_cells(_("From:"), 'TransAfterDate', '', null, -30);
54 date_cells(_("To:"), 'TransToDate');
55
56 supp_transactions_list_cell("filterType", null, true);
57
58 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), 'default');
59
60 end_row();
61 end_table();
62 set_global_supplier($_POST['supplier_id']);
63
64 //------------------------------------------------------------------------------------------------
65
66 function display_supplier_summary($supplier_record)
67 {
68         $past1 = get_company_pref('past_due_days');
69         $past2 = 2 * $past1;
70         $nowdue = "1-" . $past1 . " " . _('Days');
71         $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
72         $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
73         
74
75     start_table(TABLESTYLE, "width=80%");
76     $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
77         $pastdue1, $pastdue2, _("Total Balance"));
78
79         table_header($th);
80     start_row();
81         label_cell($supplier_record["curr_code"]);
82     label_cell($supplier_record["terms"]);
83     amount_cell($supplier_record["Balance"] - $supplier_record["Due"]);
84     amount_cell($supplier_record["Due"] - $supplier_record["Overdue1"]);
85     amount_cell($supplier_record["Overdue1"] - $supplier_record["Overdue2"]);
86     amount_cell($supplier_record["Overdue2"]);
87     amount_cell($supplier_record["Balance"]);
88     end_row();
89     end_table(1);
90 }
91 //------------------------------------------------------------------------------------------------
92
93 div_start('totals_tbl');
94 if (($_POST['supplier_id'] != "") && ($_POST['supplier_id'] != ALL_TEXT))
95 {
96         $supplier_record = get_supplier_details($_POST['supplier_id'], $_POST['TransToDate']);
97     display_supplier_summary($supplier_record);
98 }
99 div_end();
100
101 if(get_post('RefreshInquiry'))
102 {
103         $Ajax->activate('totals_tbl');
104 }
105
106 //------------------------------------------------------------------------------------------------
107 function systype_name($dummy, $type)
108 {
109         global $systypes_array;
110         return $systypes_array[$type];
111 }
112
113 function trans_view($trans)
114 {
115         return get_trans_view_str($trans["type"], $trans["trans_no"]);
116 }
117
118 function due_date($row)
119 {
120         return ($row["type"]== ST_SUPPINVOICE) || ($row["type"]== ST_SUPPCREDIT) ? $row["due_date"] : '';
121 }
122
123 function gl_view($row)
124 {
125         return get_gl_view_str($row["type"], $row["trans_no"]);
126 }
127
128 function credit_link($row)
129 {
130         if (@$_GET['popup'])
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
165 $sql = get_sql_for_supplier_inquiry($_POST['filterType'], $_POST['TransAfterDate'], $_POST['TransToDate'], $_POST['supplier_id']);
166
167 $cols = array(
168                         _("Type") => array('fun'=>'systype_name', 'ord'=>''), 
169                         _("#") => array('fun'=>'trans_view', 'ord'=>''), 
170                         _("Reference"), 
171                         _("Supplier"),
172                         _("Supplier's Reference"), 
173                         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'desc'), 
174                         _("Due Date") => array('type'=>'date', 'fun'=>'due_date'), 
175                         _("Currency") => array('align'=>'center'),
176                         _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'), 
177                         _("Credit") => array('align'=>'right', 'insert'=>true,'fun'=>'fmt_credit'), 
178                         array('insert'=>true, 'fun'=>'gl_view'),
179                         array('insert'=>true, 'fun'=>'credit_link'),
180                         array('insert'=>true, 'fun'=>'prt_link')
181                         );
182
183 if ($_POST['supplier_id'] != ALL_TEXT)
184 {
185         $cols[_("Supplier")] = 'skip';
186         $cols[_("Currency")] = 'skip';
187 }
188 //------------------------------------------------------------------------------------------------
189
190
191 /*show a table of the transactions returned by the sql */
192 $table =& new_db_pager('trans_tbl', $sql, $cols);
193 $table->set_marker('check_overdue', _("Marked items are overdue."));
194
195 $table->width = "85%";
196
197 display_db_pager($table);
198
199 if (!@$_GET['popup'])
200 {
201         end_form();
202         end_page(@$_GET['popup'], false, false);
203 }
204 ?>