Rerun of Remittance
[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 include($path_to_root . "/reporting/includes/reporting.inc");
19
20 $js = "";
21 if ($use_popup_windows)
22         $js .= get_js_open_window(900, 500);
23 if ($use_date_picker)
24         $js .= get_js_date_picker();
25 page(_($help_context = "Supplier Inquiry"), false, false, "", $js);
26
27 if (isset($_GET['supplier_id'])){
28         $_POST['supplier_id'] = $_GET['supplier_id'];
29 }
30 if (isset($_GET['FromDate'])){
31         $_POST['TransAfterDate'] = $_GET['FromDate'];
32 }
33 if (isset($_GET['ToDate'])){
34         $_POST['TransToDate'] = $_GET['ToDate'];
35 }
36
37 //------------------------------------------------------------------------------------------------
38
39 start_form();
40
41 if (!isset($_POST['supplier_id']))
42         $_POST['supplier_id'] = get_global_supplier();
43
44 start_table("class='tablestyle_noborder'");
45 start_row();
46
47 supplier_list_cells(_("Select a supplier:"), 'supplier_id', null, true);
48
49 date_cells(_("From:"), 'TransAfterDate', '', null, -30);
50 date_cells(_("To:"), 'TransToDate');
51
52 supp_allocations_list_cell("filterType", null);
53
54 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), 'default');
55
56 end_row();
57 end_table();
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'] != ALL_TEXT))
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         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         return $row['type'] == ST_SUPPINVOICE && $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         if ($row['type'] == ST_SUPPAYMENT || $row['type'] == ST_BANKPAYMENT || $row['type'] == ST_SUPPCREDIT) 
151                 return print_document_link($row['trans_no']."-".$row['type'], _("Print Remittance"), true, ST_SUPPAYMENT, ICON_PRINT);
152 }
153
154 function check_overdue($row)
155 {
156         return $row['OverDue'] == 1
157                 && (abs($row["TotalAmount"]) - $row["Allocated"] != 0);
158 }
159 //------------------------------------------------------------------------------------------------
160
161     $date_after = date2sql($_POST['TransAfterDate']);
162     $date_to = date2sql($_POST['TransToDate']);
163
164     // Sherifoz 22.06.03 Also get the description
165     $sql = "SELECT trans.type, 
166                 trans.trans_no,
167                 trans.reference, 
168                 supplier.supp_name, 
169                 trans.supp_reference,
170         trans.tran_date, 
171                 trans.due_date,
172                 supplier.curr_code, 
173         (trans.ov_amount + trans.ov_gst  + trans.ov_discount) AS TotalAmount, 
174                 trans.alloc AS Allocated,
175                 ((trans.type = ".ST_SUPPINVOICE." OR trans.type = ".ST_SUPPCREDIT.") AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue,
176         (ABS(trans.ov_amount + trans.ov_gst  + trans.ov_discount - trans.alloc) <= 0.005) AS Settled
177         FROM ".TB_PREF."supp_trans as trans, ".TB_PREF."suppliers as supplier
178         WHERE supplier.supplier_id = trans.supplier_id
179         AND trans.tran_date >= '$date_after'
180         AND trans.tran_date <= '$date_to'
181                 AND trans.ov_amount != 0";      // exclude voided transactions
182         if ($_POST['supplier_id'] != ALL_TEXT)
183                 $sql .= " AND trans.supplier_id = ".db_escape($_POST['supplier_id']);
184         if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
185         {
186                 if (($_POST['filterType'] == '1')) 
187                 {
188                         $sql .= " AND (trans.type = ".ST_SUPPINVOICE." OR trans.type = ".ST_BANKDEPOSIT.")";
189                 } 
190                 elseif (($_POST['filterType'] == '2')) 
191                 {
192                         $sql .= " AND trans.type = ".ST_SUPPINVOICE." ";
193                 } 
194                 elseif ($_POST['filterType'] == '3') 
195                 {
196                         $sql .= " AND (trans.type = ".ST_SUPPAYMENT." OR trans.type = ".ST_BANKPAYMENT.") ";
197                 } 
198                 elseif (($_POST['filterType'] == '4') || ($_POST['filterType'] == '5')) 
199                 {
200                         $sql .= " AND trans.type = ".ST_SUPPCREDIT."  ";
201                 }
202
203                 if (($_POST['filterType'] == '2') || ($_POST['filterType'] == '5')) 
204                 {
205                         $today =  date2sql(Today());
206                         $sql .= " AND trans.due_date < '$today' ";
207                 }
208         }
209
210 $cols = array(
211                         _("Type") => array('fun'=>'systype_name', 'ord'=>''), 
212                         _("#") => array('fun'=>'trans_view', 'ord'=>''), 
213                         _("Reference"), 
214                         _("Supplier"),
215                         _("Supplier's Reference"), 
216                         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'desc'), 
217                         _("Due Date") => array('type'=>'date', 'fun'=>'due_date'), 
218                         _("Currency") => array('align'=>'center'),
219                         _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'), 
220                         _("Credit") => array('align'=>'right', 'insert'=>true,'fun'=>'fmt_credit'), 
221                         array('insert'=>true, 'fun'=>'gl_view'),
222                         array('insert'=>true, 'fun'=>'credit_link'),
223                         array('insert'=>true, 'fun'=>'prt_link')
224                         );
225
226 if ($_POST['supplier_id'] != ALL_TEXT)
227 {
228         $cols[_("Supplier")] = 'skip';
229         $cols[_("Currency")] = 'skip';
230 }
231 //------------------------------------------------------------------------------------------------
232
233
234 /*show a table of the transactions returned by the sql */
235 $table =& new_db_pager('trans_tbl', $sql, $cols);
236 $table->set_marker('check_overdue', _("Marked items are overdue."));
237
238 $table->width = "85%";
239
240 display_db_pager($table);
241
242 end_form();
243 end_page();
244
245 ?>