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