0216392f6d2624a3c2ebff68c73330d778456b1f
[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 Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 $page_security=2;
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(false, true);
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'), true);
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         return systypes::name($type);
74 }
75
76 function view_link($trans)
77 {
78         return get_trans_view_str($trans["type"], $trans["trans_no"]);
79 }
80
81 function due_date($row)
82 {
83         return (($row["type"] == 20) || ($row["type"]== 21))
84                 ? $row["due_date"] : "";
85 }
86
87 function fmt_balance($row)
88 {
89         $value = ($row["type"] == 1 || $row["type"] == 21 || $row["type"] == 22)
90                 ? -$row["TotalAmount"] - $row["Allocated"]
91                 : $row["TotalAmount"] - $row["Allocated"];
92         return $value;
93 }
94
95 function alloc_link($row)
96 {
97         $link = 
98         pager_link(_("Allocations"),
99                 "/purchasing/allocations/supplier_allocate.php?trans_no=" .
100                         $row["trans_no"]. "&trans_type=" . $row["type"], ICON_MONEY );
101
102         return (($row["type"] == 1 || $row["type"] == 21 || $row["type"] == 22) 
103                 && (-$row["TotalAmount"] - $row["Allocated"]) > 0)
104                 ? $link : '';
105 }
106
107 function fmt_debit($row)
108 {
109         $value = -$row["TotalAmount"];
110         return $value>=0 ? price_format($value) : '';
111
112 }
113
114 function fmt_credit($row)
115 {
116         $value = $row["TotalAmount"];
117         return $value>0 ? price_format($value) : '';
118 }
119 //------------------------------------------------------------------------------------------------
120
121  $date_after = date2sql($_POST['TransAfterDate']);
122  $date_to = date2sql($_POST['TransToDate']);
123
124     // Sherifoz 22.06.03 Also get the description
125     $sql = "SELECT 
126                 trans.type, 
127                 trans.trans_no,
128                 trans.reference, 
129                 supplier.supp_name, 
130                 trans.supp_reference,
131         trans.tran_date, 
132                 trans.due_date,
133                 supplier.curr_code, 
134         (trans.ov_amount + trans.ov_gst  + trans.ov_discount) AS TotalAmount, 
135                 trans.alloc AS Allocated,
136                 ((trans.type = 20 OR trans.type = 21) AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue
137         FROM "
138                         .TB_PREF."supp_trans as trans, "
139                         .TB_PREF."suppliers as supplier
140         WHERE supplier.supplier_id = trans.supplier_id
141         AND trans.tran_date >= '$date_after'
142         AND trans.tran_date <= '$date_to'";
143         if ($_POST['supplier_id'] != reserved_words::get_all())
144                 $sql .= " AND trans.supplier_id = '" . $_POST['supplier_id'] . "'";
145         if (isset($_POST['filterType']) && $_POST['filterType'] != reserved_words::get_all())
146         {
147                 if (($_POST['filterType'] == '1') || ($_POST['filterType'] == '2'))
148                 {
149                         $sql .= " AND trans.type = 20 ";
150                 }
151                 elseif ($_POST['filterType'] == '3')
152                 {
153                         $sql .= " AND trans.type = 22 ";
154                 }
155                 elseif (($_POST['filterType'] == '4') || ($_POST['filterType'] == '5'))
156                 {
157                         $sql .= " AND trans.type = 21 ";
158                 }
159
160                 if (($_POST['filterType'] == '2') || ($_POST['filterType'] == '5'))
161                 {
162                         $today =  date2sql(Today());
163                         $sql .= " AND trans.due_date < '$today' ";
164                 }
165         }
166
167         if (!check_value('showSettled'))
168         {
169                 $sql .= " AND (round(abs(ov_amount + ov_gst + ov_discount) - alloc,6) != 0) ";
170         }
171
172 $cols = array(
173         _("Type") => array('fun'=>'systype_name'),
174         _("#") => array('fun'=>'view_link', 'ord'=>''),
175         _("Reference"), 
176         _("Supplier") => array('ord'=>''), 
177         _("Supp Reference"),
178         _("Date") => array('type'=>'date', 'ord'=>'asc'),
179         _("Due Date") => array('fun'=>'due_date'),
180         _("Currency") => array('align'=>'center'),
181         _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'), 
182         _("Credit") => array('align'=>'right', 'insert'=>true, 'fun'=>'fmt_credit'), 
183         _("Allocated") => 'amount', 
184         _("Balance") => array('type'=>'amount', 'insert'=>true, 'fun'=>'fmt_balance'),
185         array('insert'=>true, 'fun'=>'alloc_link')
186         );
187
188 if ($_POST['supplier_id'] != reserved_words::get_all()) {
189         $cols[_("Supplier")] = 'skip';
190         $cols[_("Currency")] = 'skip';
191 }
192 //------------------------------------------------------------------------------------------------
193
194 $table =& new_db_pager('doc_tbl', $sql, $cols);
195 $table->set_marker('check_overdue', _("Marked items are overdue."));
196
197 if (get_post('RefreshInquiry')) {
198         $table->set_sql($sql);
199         $table->set_columns($cols);
200 }
201 $table->width = "90%";
202 start_form();
203
204 display_db_pager($table);
205
206 end_form();
207 end_page();
208 ?>