3ad2fd261e0e17662d47b04d7c858bca472a31fa
[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_cells("filterType", null);
42
43 submit_cells('Refresh Inquiry', _("Search"));
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 if (($_POST['supplier_id'] != "") && ($_POST['supplier_id'] != reserved_words::get_all()))
138 {
139         $supplier_record = get_supplier_details($_POST['supplier_id']);
140     display_supplier_summary($supplier_record);
141 }
142
143 //------------------------------------------------------------------------------------------------
144
145 $result = get_transactions();
146
147 if (db_num_rows($result) == 0)
148 {
149         display_note(_("There are no transactions to display for the given dates."), 1, 1);
150         end_page();
151         exit;
152 }
153
154 //------------------------------------------------------------------------------------------------
155
156 /*show a table of the transactions returned by the sql */
157
158 start_table("$table_style width=80%");
159 if ($_POST['supplier_id'] == reserved_words::get_all())
160         $th = array(_("Type"), _("#"), _("Reference"), _("Supplier"),
161                 _("Supplier's Reference"), _("Date"), _("Due Date"), _("Currency"),
162                 _("Debit"), _("Credit"), "");
163 else            
164         $th = array(_("Type"), _("#"), _("Reference"),
165                 _("Supplier's Reference"), _("Date"), _("Due Date"),
166                 _("Debit"), _("Credit"), "");
167 table_header($th);
168
169 $j = 1;
170 $k = 0; //row colour counter
171 $over_due = false;
172 while ($myrow = db_fetch($result)) 
173 {
174
175         if ($myrow['OverDue'] == 1)
176         {
177                 start_row("class='overduebg'");
178                 $over_due = true;
179         } 
180         else 
181         {
182                 alt_table_row_color($k);
183         }
184
185         $date = sql2date($myrow["tran_date"]);
186
187         $duedate = ((($myrow["type"]== 20) || ($myrow["type"]== 21))?sql2date($myrow["due_date"]):"");
188
189
190         label_cell(systypes::name($myrow["type"]));
191         label_cell(get_trans_view_str($myrow["type"],$myrow["trans_no"]));      
192         label_cell(get_trans_view_str($myrow["type"],$myrow["trans_no"], $myrow["reference"]));
193         if ($_POST['supplier_id'] == reserved_words::get_all())
194                 label_cell($myrow["supp_name"]);
195         label_cell($myrow["supp_reference"]);
196         label_cell($date);
197         label_cell($duedate);
198     if ($_POST['supplier_id'] == reserved_words::get_all())
199         label_cell($myrow["curr_code"]);
200     if ($myrow["TotalAmount"] >= 0)
201         label_cell("");
202         amount_cell(abs($myrow["TotalAmount"]));
203         if ($myrow["TotalAmount"] < 0)
204                 label_cell("");
205
206         label_cell(get_gl_view_str($myrow["type"], $myrow["trans_no"]));
207
208         end_row();
209
210         $j++;
211         If ($j == 12)
212         {
213                 $j=1;
214                 table_header($th);
215         }
216 //end of page full new headings if
217 }
218 //end of while loop
219
220 end_table(1);
221 if ($over_due)
222         display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");
223
224
225 end_page();
226 ?>