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