Added paged query result.
[fa-stable.git] / purchasing / inquiry / supplier_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
10 $js = "";
11 if ($use_popup_windows)
12         $js .= get_js_open_window(900, 500);
13 if ($use_date_picker)
14         $js .= get_js_date_picker();
15 page(_("Supplier Inquiry"), false, false, "", $js);
16
17 if (isset($_GET['supplier_id'])){
18         $_POST['supplier_id'] = $_GET['supplier_id'];
19 }
20 if (isset($_GET['FromDate'])){
21         $_POST['TransAfterDate'] = $_GET['FromDate'];
22 }
23 if (isset($_GET['ToDate'])){
24         $_POST['TransToDate'] = $_GET['ToDate'];
25 }
26
27 //------------------------------------------------------------------------------------------------
28
29 start_form(false, true);
30
31 if (!isset($_POST['supplier_id']))
32         $_POST['supplier_id'] = get_global_supplier();
33
34 start_table("class='tablestyle_noborder'");
35 start_row();
36
37 supplier_list_cells(_("Select a supplier:"), 'supplier_id', null, true);
38
39 date_cells(_("From:"), 'TransAfterDate', '', null, -30);
40 date_cells(_("To:"), 'TransToDate');
41
42 supp_allocations_list_cell("filterType", null);
43
44 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), true);
45
46 end_row();
47 end_table();
48
49 end_form();
50
51 set_global_supplier($_POST['supplier_id']);
52
53 //------------------------------------------------------------------------------------------------
54
55 function display_supplier_summary($supplier_record)
56 {
57         global $table_style;
58
59         $past1 = get_company_pref('past_due_days');
60         $past2 = 2 * $past1;
61         $nowdue = "1-" . $past1 . " " . _('Days');
62         $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
63         $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
64         
65
66     start_table("width=80% $table_style");
67     $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
68         $pastdue1, $pastdue2, _("Total Balance"));
69
70         table_header($th);
71     start_row();
72         label_cell($supplier_record["curr_code"]);
73     label_cell($supplier_record["terms"]);
74     amount_cell($supplier_record["Balance"] - $supplier_record["Due"]);
75     amount_cell($supplier_record["Due"] - $supplier_record["Overdue1"]);
76     amount_cell($supplier_record["Overdue1"] - $supplier_record["Overdue2"]);
77     amount_cell($supplier_record["Overdue2"]);
78     amount_cell($supplier_record["Balance"]);
79     end_row();
80     end_table(1);
81 }
82 //------------------------------------------------------------------------------------------------
83
84 div_start('totals_tbl');
85 if (($_POST['supplier_id'] != "") && ($_POST['supplier_id'] != reserved_words::get_all()))
86 {
87         $supplier_record = get_supplier_details($_POST['supplier_id']);
88     display_supplier_summary($supplier_record);
89 }
90 div_end();
91
92 //------------------------------------------------------------------------------------------------
93 function systype_name($dummy, $type)
94 {
95         return systypes::name($type);
96 }
97
98 function trans_view($trans)
99 {
100         return get_trans_view_str($trans["type"], $trans["trans_no"]);
101 }
102
103 function due_date($row)
104 {
105         return ($row["type"]== 20) || ($row["type"]== 21)
106                  ? sql2date($row["due_date"]) : '';
107 }
108
109 function gl_view($row)
110 {
111         return get_gl_view_str($row["type"], $row["trans_no"]);
112 }
113
114 function fmt_debit($row)
115 {
116         $value = $row["TotalAmount"];
117         return $value>=0 ? price_format($value) : '';
118
119 }
120
121 function fmt_credit($row)
122 {
123         $value = -$row["TotalAmount"];
124         return $value>0 ? price_format($value) : '';
125 }
126
127 function prt_link($row)
128 {
129                 return print_document_link($row['trans_no'], _("Print"), true, $row['type']);
130 }
131
132 function check_overdue($row)
133 {
134         return $row['OverDue'] == 1
135                 && (abs($row["TotalAmount"]) - $row["Allocated"] != 0);
136 }
137 //------------------------------------------------------------------------------------------------
138
139     $date_after = date2sql($_POST['TransAfterDate']);
140     $date_to = date2sql($_POST['TransToDate']);
141
142     // Sherifoz 22.06.03 Also get the description
143     $sql = "SELECT trans.type, 
144                 trans.trans_no,
145                 trans.reference, 
146                 supplier.supp_name, 
147                 trans.supp_reference,
148         trans.tran_date, 
149                 trans.due_date,
150                 supplier.curr_code, 
151         (trans.ov_amount + trans.ov_gst  + trans.ov_discount) AS TotalAmount, 
152                 trans.alloc AS Allocated,
153                 ((trans.type = 20 OR trans.type = 21) AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue,
154         (ABS(trans.ov_amount + trans.ov_gst  + trans.ov_discount - trans.alloc) <= 0.005) AS Settled
155         FROM ".TB_PREF."supp_trans as trans, ".TB_PREF."suppliers as supplier
156         WHERE supplier.supplier_id = trans.supplier_id
157         AND trans.tran_date >= '$date_after'
158         AND trans.tran_date <= '$date_to'";
159         if ($_POST['supplier_id'] != reserved_words::get_all())
160                 $sql .= " AND trans.supplier_id = '" . $_POST['supplier_id'] . "'";
161         if (isset($_POST['filterType']) && $_POST['filterType'] != reserved_words::get_all())
162         {
163                 if (($_POST['filterType'] == '1')) 
164                 {
165                         $sql .= " AND (trans.type = 20 OR trans.type = 2)";
166                 } 
167                 elseif (($_POST['filterType'] == '2')) 
168                 {
169                         $sql .= " AND trans.type = 20 ";
170                 } 
171                 elseif ($_POST['filterType'] == '3') 
172                 {
173                         $sql .= " AND (trans.type = 22 OR trans.type = 1) ";
174                 } 
175                 elseif (($_POST['filterType'] == '4') || ($_POST['filterType'] == '5')) 
176                 {
177                         $sql .= " AND trans.type = 21  ";
178                 }
179
180                 if (($_POST['filterType'] == '2') || ($_POST['filterType'] == '5')) 
181                 {
182                         $today =  date2sql(Today());
183                         $sql .= " AND trans.due_date < '$today' ";
184                 }
185         }
186
187 $cols = array(
188                         _("Type") => array('type'=>'spec', 'fun'=>'systype_name', 'ord'=>''), 
189                         _("#") => array('type'=>'spec', 'fun'=>'trans_view', 'ord'=>''), 
190                         _("Reference"), 
191                         _("Supplier") => 'text',
192                         _("Supplier's Reference"), 
193                         _("Date") => array('type'=>'date', 'ord'=>'desc'), 
194                         _("Due Date") => array('type'=>'spec', 'fun'=>'due_date'), 
195                         _("Currency") => 'text',
196                         _("Debit") => array('type'=>'spec', 'fun'=>'fmt_debit'), 
197                         _("Credit") => array('type'=>'insert', 'fun'=>'fmt_credit'), 
198                         array('type'=>'insert', 'fun'=>'gl_view'),
199                         );
200
201 if ($_POST['supplier_id'] != reserved_words::get_all())
202 {
203         $cols[_("Supplier")] = 'skip';
204         $cols[_("Currency")] = 'skip';
205 }
206 //------------------------------------------------------------------------------------------------
207
208
209 /*show a table of the transactions returned by the sql */
210 $table =& new_db_pager('trans_tbl', $sql, $cols);
211 $table->set_marker('check_overdue', _("Marked items are overdue."));
212
213 if(get_post('RefreshInquiry'))
214 {
215         $table->set_sql($sql);
216         $table->set_columns($cols);
217         $Ajax->activate('trans_tbl');
218         $Ajax->activate('totals_tbl');
219 }
220
221 start_form();
222 display_db_pager($table);
223 end_form();
224
225 end_page();
226
227 ?>