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