c00b0613d05d90052d4d63d3014c47b69d8c3672
[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 end_form();
49 set_global_supplier($_POST['supplier_id']);
50
51 //------------------------------------------------------------------------------------------------
52
53 function display_supplier_summary($supplier_record)
54 {
55         global $table_style;
56
57         $past1 = get_company_pref('past_due_days');
58         $past2 = 2 * $past1;
59         $nowdue = "1-" . $past1 . " " . _('Days');
60         $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
61         $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
62         
63
64     start_table("width=80% $table_style");
65     $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
66         $pastdue1, $pastdue2, _("Total Balance"));
67
68         table_header($th);
69     start_row();
70         label_cell($supplier_record["curr_code"]);
71     label_cell($supplier_record["terms"]);
72     amount_cell($supplier_record["Balance"] - $supplier_record["Due"]);
73     amount_cell($supplier_record["Due"] - $supplier_record["Overdue1"]);
74     amount_cell($supplier_record["Overdue1"] - $supplier_record["Overdue2"]);
75     amount_cell($supplier_record["Overdue2"]);
76     amount_cell($supplier_record["Balance"]);
77     end_row();
78     end_table(1);
79 }
80 //------------------------------------------------------------------------------------------------
81
82 div_start('totals_tbl');
83 if (($_POST['supplier_id'] != "") && ($_POST['supplier_id'] != reserved_words::get_all()))
84 {
85         $supplier_record = get_supplier_details($_POST['supplier_id']);
86     display_supplier_summary($supplier_record);
87 }
88 div_end();
89
90 if(get_post('RefreshInquiry'))
91 {
92         $Ajax->activate('totals_tbl');
93 }
94
95 //------------------------------------------------------------------------------------------------
96 function systype_name($dummy, $type)
97 {
98         return systypes::name($type);
99 }
100
101 function trans_view($trans)
102 {
103         return get_trans_view_str($trans["type"], $trans["trans_no"]);
104 }
105
106 function due_date($row)
107 {
108         return ($row["type"]== 20) || ($row["type"]== 21)
109                  ? $row["due_date"] : '';
110 }
111
112 function gl_view($row)
113 {
114         return get_gl_view_str($row["type"], $row["trans_no"]);
115 }
116
117 function credit_link($row)
118 {
119         return $row['type'] == 20 && $row["TotalAmount"] - $row["Allocated"] > 0 ?
120                 pager_link(_("Credit This"),
121                         "/purchasing/supplier_credit.php?New=1&invoice_no=".
122                         $row['trans_no'], ICON_CREDIT)
123                         : '';
124 }
125
126 function fmt_debit($row)
127 {
128         $value = $row["TotalAmount"];
129         return $value>=0 ? price_format($value) : '';
130
131 }
132
133 function fmt_credit($row)
134 {
135         $value = -$row["TotalAmount"];
136         return $value>0 ? price_format($value) : '';
137 }
138
139 function prt_link($row)
140 {
141                 return print_document_link($row['trans_no'], _("Print"), true, $row['type']);
142 }
143
144 function check_overdue($row)
145 {
146         return $row['OverDue'] == 1
147                 && (abs($row["TotalAmount"]) - $row["Allocated"] != 0);
148 }
149 //------------------------------------------------------------------------------------------------
150
151     $date_after = date2sql($_POST['TransAfterDate']);
152     $date_to = date2sql($_POST['TransToDate']);
153
154     // Sherifoz 22.06.03 Also get the description
155     $sql = "SELECT trans.type, 
156                 trans.trans_no,
157                 trans.reference, 
158                 supplier.supp_name, 
159                 trans.supp_reference,
160         trans.tran_date, 
161                 trans.due_date,
162                 supplier.curr_code, 
163         (trans.ov_amount + trans.ov_gst  + trans.ov_discount) AS TotalAmount, 
164                 trans.alloc AS Allocated,
165                 ((trans.type = 20 OR trans.type = 21) AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue,
166         (ABS(trans.ov_amount + trans.ov_gst  + trans.ov_discount - trans.alloc) <= 0.005) AS Settled
167         FROM ".TB_PREF."supp_trans as trans, ".TB_PREF."suppliers as supplier
168         WHERE supplier.supplier_id = trans.supplier_id
169         AND trans.tran_date >= '$date_after'
170         AND trans.tran_date <= '$date_to'";
171         if ($_POST['supplier_id'] != reserved_words::get_all())
172                 $sql .= " AND trans.supplier_id = '" . $_POST['supplier_id'] . "'";
173         if (isset($_POST['filterType']) && $_POST['filterType'] != reserved_words::get_all())
174         {
175                 if (($_POST['filterType'] == '1')) 
176                 {
177                         $sql .= " AND (trans.type = 20 OR trans.type = 2)";
178                 } 
179                 elseif (($_POST['filterType'] == '2')) 
180                 {
181                         $sql .= " AND trans.type = 20 ";
182                 } 
183                 elseif ($_POST['filterType'] == '3') 
184                 {
185                         $sql .= " AND (trans.type = 22 OR trans.type = 1) ";
186                 } 
187                 elseif (($_POST['filterType'] == '4') || ($_POST['filterType'] == '5')) 
188                 {
189                         $sql .= " AND trans.type = 21  ";
190                 }
191
192                 if (($_POST['filterType'] == '2') || ($_POST['filterType'] == '5')) 
193                 {
194                         $today =  date2sql(Today());
195                         $sql .= " AND trans.due_date < '$today' ";
196                 }
197         }
198
199 $cols = array(
200                         _("Type") => array('fun'=>'systype_name', 'ord'=>''), 
201                         _("#") => array('fun'=>'trans_view', 'ord'=>''), 
202                         _("Reference"), 
203                         _("Supplier"),
204                         _("Supplier's Reference"), 
205                         _("Date") => array('type'=>'date', 'ord'=>'desc'), 
206                         _("Due Date") => array('type'=>'date', 'fun'=>'due_date'), 
207                         _("Currency") => array('align'=>'center'),
208                         _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'), 
209                         _("Credit") => array('align'=>'right', 'insert'=>true,'fun'=>'fmt_credit'), 
210                         array('insert'=>true, 'fun'=>'gl_view'),
211                         array('insert'=>true, 'fun'=>'credit_link')
212                         );
213
214 if ($_POST['supplier_id'] != reserved_words::get_all())
215 {
216         $cols[_("Supplier")] = 'skip';
217         $cols[_("Currency")] = 'skip';
218 }
219 //------------------------------------------------------------------------------------------------
220
221
222 /*show a table of the transactions returned by the sql */
223 $table =& new_db_pager('trans_tbl', $sql, $cols);
224 $table->set_marker('check_overdue', _("Marked items are overdue."));
225
226 if (get_post('RefreshInquiry')) {
227         $table->set_sql($sql);
228         $table->set_columns($cols);
229 }
230 start_form();
231
232 display_db_pager($table);
233
234 end_form();
235 end_page();
236
237 ?>