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