Changed API for input/lists functions, added empty hints when needed
[fa-stable.git] / sales / inquiry / sales_deliveries_view.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 . "/sales/includes/sales_ui.inc");
8 include_once($path_to_root . "/reporting/includes/reporting.inc");
9
10 $js = "";
11 if ($use_popup_windows)
12         $js .= get_js_open_window(900, 600);
13 if ($use_date_picker)
14         $js .= get_js_date_picker();
15
16 if (isset($_GET['OutstandingOnly']) && ($_GET['OutstandingOnly'] == true))
17 {
18         $_POST['OutstandingOnly'] = true;
19         page(_("Search Not Invoiced Deliveries"), false, false, "", $js);
20 }
21 else
22 {
23         $_POST['OutstandingOnly'] = false;
24         page(_("Search All Deliveries"), false, false, "", $js);
25 }
26
27 if (isset($_GET['selected_customer']))
28 {
29         $selected_customer = $_GET['selected_customer'];
30 }
31 elseif (isset($_POST['selected_customer']))
32 {
33         $selected_customer = $_POST['selected_customer'];
34 }
35 else
36         $selected_customer = -1;
37
38 if (isset($_POST['BatchInvoice']))
39 {
40
41         // checking batch integrity
42     $del_count = 0;
43     foreach($_SESSION['Batch'] as $delivery)
44     {
45                 $checkbox = 'Sel_'.$delivery['trans'];
46                 if (check_value($checkbox))
47                 {
48                 if (!$del_count)
49                 {
50                                 $del_customer = $delivery['cust'];
51                                 $del_branch = $delivery['branch'];
52                 }
53                 else
54                 {
55                                 if ($del_customer!=$delivery['cust'] || $del_branch != $delivery['branch'])
56                                 {
57                                 $del_count=0;
58                                 break;
59                                 }
60                 }
61                 $selected[] = $delivery['trans'];
62                 $del_count++;
63                 }
64     }
65
66     if (!$del_count)
67     {
68                 display_error(_('For batch invoicing you should
69                     select at least one delivery. All items must be dispatched to
70                     the same customer branch.'));
71     }
72     else
73     {
74                 $_SESSION['DeliveryBatch'] = $selected;
75                 meta_forward($path_to_root . '/sales/customer_invoice.php','BatchInvoice=Yes');
76     }
77 }
78 //-----------------------------------------------------------------------------------
79 print_hidden_script(13);
80
81 start_form(false, false, $_SERVER['PHP_SELF'] ."?OutstandingOnly=" . $_POST['OutstandingOnly'] .SID);
82
83 start_table("class='tablestyle_noborder'");
84 start_row();
85 ref_cells(_("#:"), 'DeliveryNumber');
86 date_cells(_("from:"), 'DeliveryAfterDate', '', null, -30);
87 date_cells(_("to:"), 'DeliveryToDate', '', null, 1);
88
89 locations_list_cells(_("Location:"), 'StockLocation', null, true);
90
91 stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true);
92
93 submit_cells('SearchOrders', _("Search"));
94
95 hidden('OutstandingOnly', $_POST['OutstandingOnly']);
96
97 end_row();
98
99 end_table();
100
101 //---------------------------------------------------------------------------------------------
102
103 if (isset($_POST['SelectStockFromList']) && ($_POST['SelectStockFromList'] != "") &&
104         ($_POST['SelectStockFromList'] != reserved_words::get_all()))
105 {
106         $selected_stock_item = $_POST['SelectStockFromList'];
107 }
108 else
109 {
110         unset($selected_stock_item);
111 }
112
113 //---------------------------------------------------------------------------------------------
114 $sql = "SELECT ".TB_PREF."debtor_trans.trans_no, "
115         .TB_PREF."debtors_master.curr_code, "
116         .TB_PREF."debtors_master.name, "
117         .TB_PREF."cust_branch.br_name, "
118         .TB_PREF."debtor_trans.reference, "
119         .TB_PREF."debtor_trans.tran_date, "
120         .TB_PREF."debtor_trans.due_date, "
121         .TB_PREF."sales_orders.customer_ref, "
122         .TB_PREF."sales_orders.deliver_to, ";
123
124 $sql .= " Sum(".TB_PREF."debtor_trans_details.quantity-"
125                  .TB_PREF."debtor_trans_details.qty_done) AS Outstanding, ";
126
127 $sql .= " Sum(".TB_PREF."debtor_trans_details.qty_done) AS Done, ";
128
129 //$sql .= " Sum(".TB_PREF."debtor_trans_details.unit_price*"
130 // .TB_PREF."debtor_trans_details.quantity*(1-"
131 // .TB_PREF."debtor_trans_details.discount_percent)) AS DeliveryValue";
132 $sql .= "(ov_amount+ov_gst+ov_freight+ov_freight_tax) AS DeliveryValue";
133 $sql .=" FROM "
134          .TB_PREF."sales_orders, "
135          .TB_PREF."debtor_trans, "
136          .TB_PREF."debtor_trans_details, "
137          .TB_PREF."debtors_master, "
138          .TB_PREF."cust_branch
139                 WHERE "
140                 .TB_PREF."sales_orders.order_no = ".TB_PREF."debtor_trans.order_ AND "
141                 .TB_PREF."debtor_trans.debtor_no = ".TB_PREF."debtors_master.debtor_no
142                         AND ".TB_PREF."debtor_trans.type = 13
143                         AND ".TB_PREF."debtor_trans_details.debtor_trans_no = ".TB_PREF."debtor_trans.trans_no
144                         AND ".TB_PREF."debtor_trans_details.debtor_trans_type = ".TB_PREF."debtor_trans.type
145                         AND ".TB_PREF."debtor_trans.branch_code = ".TB_PREF."cust_branch.branch_code
146                         AND ".TB_PREF."debtor_trans.debtor_no = ".TB_PREF."cust_branch.debtor_no ";
147
148 //figure out the sql required from the inputs available
149 if (isset($_POST['DeliveryNumber']) && $_POST['DeliveryNumber'] != "")
150 {
151         $sql .= " AND ".TB_PREF."debtor_trans.trans_no LIKE '%". $_POST['DeliveryNumber'] ."' GROUP BY ".TB_PREF."debtor_trans.trans_no";
152 }
153 else
154 {
155
156         $date_after = date2sql($_POST['DeliveryAfterDate']);
157         $date_before = date2sql($_POST['DeliveryToDate']);
158
159         $sql .= " AND ".TB_PREF."debtor_trans.tran_date >= '$date_after'";
160         $sql .= " AND ".TB_PREF."debtor_trans.tran_date <= '$date_before'";
161
162         if ($selected_customer != -1)
163                 $sql .= " AND ".TB_PREF."debtor_trans.debtor_no='" . $selected_customer . "' ";
164
165         if (isset($selected_stock_item))
166                 $sql .= " AND ".TB_PREF."debtor_trans_details.stock_id='". $selected_stock_item ."' ";
167
168         if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != reserved_words::get_all())
169                 $sql .= " AND ".TB_PREF."sales_orders.from_stk_loc = '". $_POST['StockLocation'] . "' ";
170
171         if ($_POST['OutstandingOnly'] == true) {
172          $sql .= " AND ".TB_PREF."debtor_trans_details.qty_done < ".TB_PREF."debtor_trans_details.quantity ";
173         }
174
175         $sql .= " GROUP BY ".TB_PREF."debtor_trans.trans_no ";
176 //      .TB_PREF."debtor_trans.debtor_no, "
177 //      .TB_PREF."debtor_trans.branch_code, "
178 //              ".TB_PREF."sales_orders.customer_ref, "
179 //              .TB_PREF."debtor_trans.tran_date";
180
181 } //end no delivery number selected
182
183 $result = db_query($sql,"No deliveries were returned");
184
185 //-----------------------------------------------------------------------------------
186 if (isset($_SESSION['Batch']))
187 {
188     foreach($_SESSION['Batch'] as $trans=>$del)
189         unset($_SESSION['Batch'][$trans]);
190     unset($_SESSION['Batch']);
191 }
192 if ($result)
193 {
194         /*show a table of the deliveries returned by the sql */
195
196         start_table("$table_style colspan=7 width=95%");
197         $th = array(_("Delivery #"), _("Customer"), _("Branch"), _("Reference"), _("Delivery Date"),
198                 _("Due By"), _("Delivery Total"), _("Currency"), submit('BatchInvoice','Batch Inv', false),
199                  "", "", "");
200         table_header($th);
201
202         $j = 1;
203         $k = 0; //row colour counter
204         $overdue_items = false;
205         while ($myrow = db_fetch($result))
206         {
207             $_SESSION['Batch'][] = array('trans'=>$myrow["trans_no"],
208             'cust'=>$myrow["name"],'branch'=>$myrow["br_name"] );
209
210             $view_page = get_customer_trans_view_str(13, $myrow["trans_no"]);
211             $formated_del_date = sql2date($myrow["tran_date"]);
212             $formated_due_date = sql2date($myrow["due_date"]);
213             $not_closed =  $myrow["Outstanding"]!=0;
214
215         // if overdue orders, then highlight as so
216
217         if (date1_greater_date2(Today(), $formated_due_date) && $not_closed )
218         {
219                  start_row("class='overduebg'");
220                  $overdue_items = true;
221         }
222         else
223         {
224                         alt_table_row_color($k);
225         }
226
227                 label_cell($view_page);
228                 label_cell($myrow["name"]);
229                 label_cell($myrow["br_name"]);
230                 label_cell($myrow["reference"]);
231                 label_cell($formated_del_date);
232                 label_cell($formated_due_date);
233                 amount_cell($myrow["DeliveryValue"]);
234                 label_cell($myrow["curr_code"]);
235                 if (!$myrow['Done'])
236                     check_cells(null,'Sel_'. $myrow['trans_no'],0,false);
237                 else
238                     label_cell("");
239                 if ($_POST['OutstandingOnly'] == true || $not_closed)
240                 {
241                 $modify_page = $path_to_root . "/sales/customer_delivery.php?" . SID . "ModifyDelivery=" . $myrow["trans_no"];
242                 $invoice_page = $path_to_root . "/sales/customer_invoice.php?" . SID . "DeliveryNumber=" .$myrow["trans_no"];
243                 label_cell("<a href='$modify_page'>" . _("Edit") . "</a>");
244                         label_cell(print_document_link($myrow['trans_no'], _("Print")));
245
246                 label_cell($not_closed ? "<a href='$invoice_page'>" . _("Invoice") . "</a>" : '');
247
248                 }
249                 else
250                 {
251                 label_cell("");
252                 label_cell("");
253                 label_cell("");
254                 }
255                 end_row();;
256
257                 $j++;
258                 If ($j == 12)
259                 {
260                         $j = 1;
261                         table_header($th);
262                 }
263                 //end of page full new headings if
264         }
265         //end of while loop
266
267         end_table();
268
269    if ($overdue_items)
270                 display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");
271 }
272
273 echo "<br>";
274 end_form();
275
276 end_page();
277 ?>
278