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