3e233b1477bcbbb5dc9e12b512c5d88eb7af95f2
[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_cells("filterType", null);
44
45 check_cells(_("show settled:"), 'showSettled', null);
46
47 submit_cells('Refresh Inquiry', _("Search"));
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 (db_num_rows($result) == 0)
114 {
115         display_note(_("There are no transactions to display for the given dates."), 1, 1);
116         end_page();
117         exit;
118 }
119
120 //------------------------------------------------------------------------------------------------
121
122 /*show a table of the transactions returned by the sql */
123
124 start_table("$table_style width=90%");
125 if ($_POST['supplier_id'] == reserved_words::get_all())
126         $th = array(_("Type"), _("Number"), _("Reference"), _("Supplier"),
127                 _("Supp Reference"), _("Date"), _("Due Date"), _("Currency"),
128                 _("Debit"), _("Credit"), _("Allocated"), _("Balance"), "");
129 else
130         $th = array(_("Type"), _("Number"), _("Reference"),     _("Supp Reference"), _("Date"), _("Due Date"),
131                 _("Debit"), _("Credit"), _("Allocated"), _("Balance"), "");
132 table_header($th);
133
134 $j = 1;
135 $k = 0; //row colour counter
136 $over_due = false;
137 while ($myrow = db_fetch($result))
138 {
139
140         if ($myrow['OverDue'] == 1)
141         {
142                 start_row("class='overduebg'");
143                 $over_due = true;
144         }
145         else
146         {
147                 alt_table_row_color($k);
148         }
149
150         $date = sql2date($myrow["tran_date"]);
151
152         $duedate = ((($myrow["type"] == 20) || ($myrow["type"]== 21))?sql2date($myrow["due_date"]):"");
153
154
155         label_cell(systypes::name($myrow["type"]));
156         label_cell(get_trans_view_str($myrow["type"],$myrow["trans_no"]));
157         label_cell($myrow["reference"]);
158         if ($_POST['supplier_id'] == reserved_words::get_all())
159                 label_cell($myrow["supp_name"]);
160         label_cell($myrow["supp_reference"]);
161         label_cell($date);
162         label_cell($duedate);
163     if ($_POST['supplier_id'] == reserved_words::get_all())
164         label_cell($myrow["curr_code"]);
165     if ($myrow["TotalAmount"] >= 0)
166         label_cell("");
167         amount_cell(abs($myrow["TotalAmount"]));
168         if ($myrow["TotalAmount"] < 0)
169                 label_cell("");
170         amount_cell($myrow["Allocated"]);
171         if ($myrow["type"] == 1 || $myrow["type"] == 21 || $myrow["type"] == 22)
172                 $balance = -$myrow["TotalAmount"] - $myrow["Allocated"];
173         else
174                 $balance = $myrow["TotalAmount"] - $myrow["Allocated"];
175         amount_cell($balance);
176
177         //if (($myrow["type"] == 1 || $myrow["type"] == 21 || $myrow["type"] == 22) &&
178         //      $myrow["Void"] == 0)
179         if (($myrow["type"] == 1 || $myrow["type"] == 21 || $myrow["type"] == 22) &&
180                 $balance > 0)
181         {
182                 label_cell("<a href='$path_to_root/purchasing/allocations/supplier_allocate.php?trans_no=" .
183                         $myrow["trans_no"]. "&trans_type=" . $myrow["type"] . "'>" . _("Allocations") . "</a>");
184         }
185         else
186                 label_cell("");
187
188         end_row();
189
190         $j++;
191         If ($j == 12)
192         {
193                 $j = 1;
194                 table_header($th);
195         }
196 //end of page full new headings if
197 }
198 //end of while loop
199
200 end_table(1);
201 if ($over_due)
202         display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");
203
204 end_page();
205 ?>