0a5c65d8f0f5e88ac12985571d7cf543374f728a
[fa-stable.git] / purchasing / inquiry / supplier_allocation_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_SUPPLIERALLOC';
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 $js = "";
19 if ($use_popup_windows)
20         $js .= get_js_open_window(900, 500);
21 if ($use_date_picker)
22         $js .= get_js_date_picker();
23 page(_("Supplier Allocation Inquiry"), false, false, "", $js);
24
25 if (isset($_GET['supplier_id']))
26 {
27         $_POST['supplier_id'] = $_GET['supplier_id'];
28 }
29 if (isset($_GET['FromDate']))
30 {
31         $_POST['TransAfterDate'] = $_GET['FromDate'];
32 }
33 if (isset($_GET['ToDate']))
34 {
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("class='tablestyle_noborder'");
46 start_row();
47
48 supplier_list_cells(_("Select a supplier: "), 'supplier_id', $_POST['supplier_id'], true);
49
50 date_cells(_("From:"), 'TransAfterDate', '', null, -30);
51 date_cells(_("To:"), 'TransToDate', '', null, 1);
52
53 supp_allocations_list_cell("filterType", null);
54
55 check_cells(_("show settled:"), 'showSettled', null);
56
57 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), 'default');
58
59 set_global_supplier($_POST['supplier_id']);
60
61 end_row();
62 end_table();
63 end_form();
64 //------------------------------------------------------------------------------------------------
65 function check_overdue($row)
66 {
67         return ($row['TotalAmount']>$row['Allocated']) && 
68                 $row['OverDue'] == 1;
69 }
70
71 function systype_name($dummy, $type)
72 {
73         global $systypes_array;
74         
75         return $systypes_array[$type];
76 }
77
78 function view_link($trans)
79 {
80         return get_trans_view_str($trans["type"], $trans["trans_no"]);
81 }
82
83 function due_date($row)
84 {
85         return (($row["type"] == ST_SUPPINVOICE) || ($row["type"]== ST_SUPPCREDIT))
86                 ? $row["due_date"] : "";
87 }
88
89 function fmt_balance($row)
90 {
91         $value = ($row["type"] == ST_BANKPAYMENT || $row["type"] == ST_SUPPCREDIT || $row["type"] == ST_SUPPAYMENT)
92                 ? -$row["TotalAmount"] - $row["Allocated"]
93                 : $row["TotalAmount"] - $row["Allocated"];
94         return $value;
95 }
96
97 function alloc_link($row)
98 {
99         $link = 
100         pager_link(_("Allocations"),
101                 "/purchasing/allocations/supplier_allocate.php?trans_no=" .
102                         $row["trans_no"]. "&trans_type=" . $row["type"], ICON_MONEY );
103
104         return (($row["type"] == ST_BANKPAYMENT || $row["type"] == ST_SUPPCREDIT || $row["type"] == ST_SUPPAYMENT) 
105                 && (-$row["TotalAmount"] - $row["Allocated"]) > 0)
106                 ? $link : '';
107 }
108
109 function fmt_debit($row)
110 {
111         $value = -$row["TotalAmount"];
112         return $value>=0 ? price_format($value) : '';
113
114 }
115
116 function fmt_credit($row)
117 {
118         $value = $row["TotalAmount"];
119         return $value>0 ? price_format($value) : '';
120 }
121 //------------------------------------------------------------------------------------------------
122
123  $date_after = date2sql($_POST['TransAfterDate']);
124  $date_to = date2sql($_POST['TransToDate']);
125
126     // Sherifoz 22.06.03 Also get the description
127     $sql = "SELECT 
128                 trans.type, 
129                 trans.trans_no,
130                 trans.reference, 
131                 supplier.supp_name, 
132                 trans.supp_reference,
133         trans.tran_date, 
134                 trans.due_date,
135                 supplier.curr_code, 
136         (trans.ov_amount + trans.ov_gst  + trans.ov_discount) AS TotalAmount, 
137                 trans.alloc AS Allocated,
138                 ((trans.type = ".ST_SUPPINVOICE." OR trans.type = ".ST_SUPPCREDIT.") AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue
139         FROM "
140                         .TB_PREF."supp_trans as trans, "
141                         .TB_PREF."suppliers as supplier
142         WHERE supplier.supplier_id = trans.supplier_id
143         AND trans.tran_date >= '$date_after'
144         AND trans.tran_date <= '$date_to'";
145
146         if ($_POST['supplier_id'] != ALL_TEXT)
147                 $sql .= " AND trans.supplier_id = ".db_escape($_POST['supplier_id']);
148         if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
149         {
150                 if (($_POST['filterType'] == '1') || ($_POST['filterType'] == '2'))
151                 {
152                         $sql .= " AND trans.type = ".ST_SUPPINVOICE." ";
153                 }
154                 elseif ($_POST['filterType'] == '3')
155                 {
156                         $sql .= " AND trans.type = ".ST_SUPPAYMENT." ";
157                 }
158                 elseif (($_POST['filterType'] == '4') || ($_POST['filterType'] == '5'))
159                 {
160                         $sql .= " AND trans.type = ".ST_SUPPCREDIT." ";
161                 }
162
163                 if (($_POST['filterType'] == '2') || ($_POST['filterType'] == '5'))
164                 {
165                         $today =  date2sql(Today());
166                         $sql .= " AND trans.due_date < '$today' ";
167                 }
168         }
169
170         if (!check_value('showSettled'))
171         {
172                 $sql .= " AND (round(abs(ov_amount + ov_gst + ov_discount) - alloc,6) != 0) ";
173         }
174
175 $cols = array(
176         _("Type") => array('fun'=>'systype_name'),
177         _("#") => array('fun'=>'view_link', 'ord'=>''),
178         _("Reference"), 
179         _("Supplier") => array('ord'=>''), 
180         _("Supp Reference"),
181         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'asc'),
182         _("Due Date") => array('fun'=>'due_date'),
183         _("Currency") => array('align'=>'center'),
184         _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'), 
185         _("Credit") => array('align'=>'right', 'insert'=>true, 'fun'=>'fmt_credit'), 
186         _("Allocated") => 'amount', 
187         _("Balance") => array('type'=>'amount', 'insert'=>true, 'fun'=>'fmt_balance'),
188         array('insert'=>true, 'fun'=>'alloc_link')
189         );
190
191 if ($_POST['supplier_id'] != ALL_TEXT) {
192         $cols[_("Supplier")] = 'skip';
193         $cols[_("Currency")] = 'skip';
194 }
195 //------------------------------------------------------------------------------------------------
196
197 $table =& new_db_pager('doc_tbl', $sql, $cols);
198 $table->set_marker('check_overdue', _("Marked items are overdue."));
199
200 $table->width = "90%";
201 start_form();
202
203 display_db_pager($table);
204
205 end_form();
206 end_page();
207 ?>