Code cleanup.
[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/db_pager.inc");
6 include($path_to_root . "/includes/session.inc");
7
8 include($path_to_root . "/sales/includes/sales_ui.inc");
9 include_once($path_to_root . "/reporting/includes/reporting.inc");
10
11 $js = "";
12 if ($use_popup_windows)
13         $js .= get_js_open_window(900, 600);
14 if ($use_date_picker)
15         $js .= get_js_date_picker();
16
17 if (isset($_GET['OutstandingOnly']) && ($_GET['OutstandingOnly'] == true))
18 {
19         $_POST['OutstandingOnly'] = true;
20         page(_("Search Not Invoiced Deliveries"), false, false, "", $js);
21 }
22 else
23 {
24         $_POST['OutstandingOnly'] = false;
25         page(_("Search All Deliveries"), false, false, "", $js);
26 }
27
28 if (isset($_GET['selected_customer']))
29 {
30         $selected_customer = $_GET['selected_customer'];
31 }
32 elseif (isset($_POST['selected_customer']))
33 {
34         $selected_customer = $_POST['selected_customer'];
35 }
36 else
37         $selected_customer = -1;
38
39 if (isset($_POST['BatchInvoice']))
40 {
41         // checking batch integrity
42     $del_count = 0;
43     foreach($_POST['Sel_'] as $delivery => $branch) {
44                 $checkbox = 'Sel_'.$delivery;
45                 if (check_value($checkbox))     {
46                 if (!$del_count) {
47                                 $del_branch = $branch;
48                 }
49                 else {
50                                 if ($del_branch != $branch)     {
51                                 $del_count=0;
52                                 break;
53                                 }
54                 }
55                 $selected[] = $delivery;
56                 $del_count++;
57                 }
58     }
59
60     if (!$del_count) {
61                 display_error(_('For batch invoicing you should
62                     select at least one delivery. All items must be dispatched to
63                     the same customer branch.'));
64     } else {
65                 $_SESSION['DeliveryBatch'] = $selected;
66                 meta_forward($path_to_root . '/sales/customer_invoice.php','BatchInvoice=Yes');
67     }
68 }
69
70 //-----------------------------------------------------------------------------------
71 if (get_post('_DeliveryNumber_changed')) 
72 {
73         $disable = get_post('DeliveryNumber') !== '';
74
75         $Ajax->addDisable(true, 'DeliveryAfterDate', $disable);
76         $Ajax->addDisable(true, 'DeliveryToDate', $disable);
77         $Ajax->addDisable(true, 'StockLocation', $disable);
78         $Ajax->addDisable(true, '_SelectStockFromList_edit', $disable);
79         $Ajax->addDisable(true, 'SelectStockFromList', $disable);
80         // if search is not empty rewrite table
81         if ($disable) {
82                 $Ajax->addFocus(true, 'DeliveryNumber');
83         } else
84                 $Ajax->addFocus(true, 'DeliveryAfterDate');
85         $Ajax->activate('deliveries_tbl');
86 }
87
88 //-----------------------------------------------------------------------------------
89
90 start_form(false, false, $_SERVER['PHP_SELF'] ."?OutstandingOnly=" . $_POST['OutstandingOnly'] .SID);
91
92 start_table("class='tablestyle_noborder'");
93 start_row();
94 ref_cells(_("#:"), 'DeliveryNumber', '',null, '', true);
95 date_cells(_("from:"), 'DeliveryAfterDate', '', null, -30);
96 date_cells(_("to:"), 'DeliveryToDate', '', null, 1);
97
98 locations_list_cells(_("Location:"), 'StockLocation', null, true);
99
100 stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true);
101
102 submit_cells('SearchOrders', _("Search"),'',_('Select documents'), true);
103
104 hidden('OutstandingOnly', $_POST['OutstandingOnly']);
105
106 end_row();
107
108 end_table();
109
110 //---------------------------------------------------------------------------------------------
111
112 if (isset($_POST['SelectStockFromList']) && ($_POST['SelectStockFromList'] != "") &&
113         ($_POST['SelectStockFromList'] != reserved_words::get_all()))
114 {
115         $selected_stock_item = $_POST['SelectStockFromList'];
116 }
117 else
118 {
119         unset($selected_stock_item);
120 }
121
122 //---------------------------------------------------------------------------------------------
123 function trans_view($trans, $trans_no)
124 {
125         return get_customer_trans_view_str(13, $trans['trans_no']);
126 }
127
128 function batch_checkbox($row)
129 {
130         $name = "Sel_" .$row['trans_no'];
131         return $row['Done'] ? '' :
132                 "<input type='checkbox' name='$name' value='1' >"
133 // add also trans_no => branch code for checking after 'Batch' submit
134          ."<input name='Sel_[".$row['trans_no']."]' type='hidden' value='"
135          .$row['branch_code']."'>\n";
136 }
137
138 function edit_link($row)
139 {
140         return $row["Outstanding"]==0 ? '' :
141                 pager_link(_('Edit'), "/sales/customer_delivery.php?ModifyDelivery="
142                         .$row['trans_no']);
143 }
144
145 function prt_link($row)
146 {
147         return print_document_link($row['trans_no'], _("Print"), true, 13);
148 }
149
150 function invoice_link($row)
151 {
152         return $row["Outstanding"]==0 ? '' :
153                 pager_link(_('Invoice'), "/sales/customer_invoice.php?DeliveryNumber=" 
154                         .$row['trans_no']);
155 }
156
157 function check_overdue($row)
158 {
159         return date1_greater_date2(Today(), sql2date($row["due_date"])) && 
160                         $row["Outstanding"]!=0;
161 }
162 //------------------------------------------------------------------------------------------------
163 $sql = "SELECT trans.trans_no,
164                 debtor.name,
165                 branch.branch_code,
166                 branch.br_name,
167                 sorder.deliver_to,
168                 trans.reference,
169                 sorder.customer_ref,
170                 trans.tran_date,
171                 trans.due_date,
172                 (ov_amount+ov_gst+ov_freight+ov_freight_tax) AS DeliveryValue,
173                 debtor.curr_code,
174                 Sum(line.quantity-line.qty_done) AS Outstanding,
175                 Sum(line.qty_done) AS Done
176         FROM "
177          .TB_PREF."sales_orders as sorder, "
178          .TB_PREF."debtor_trans as trans, "
179          .TB_PREF."debtor_trans_details as line, "
180          .TB_PREF."debtors_master as debtor, "
181          .TB_PREF."cust_branch as branch
182                 WHERE
183                 sorder.order_no = trans.order_ AND
184                 trans.debtor_no = debtor.debtor_no
185                         AND trans.type = 13
186                         AND line.debtor_trans_no = trans.trans_no
187                         AND line.debtor_trans_type = trans.type
188                         AND trans.branch_code = branch.branch_code
189                         AND trans.debtor_no = branch.debtor_no ";
190
191 if ($_POST['OutstandingOnly'] == true) {
192          $sql .= " AND line.qty_done < line.quantity ";
193 }
194
195 //figure out the sql required from the inputs available
196 if (isset($_POST['DeliveryNumber']) && $_POST['DeliveryNumber'] != "")
197 {
198         $sql .= " AND trans.trans_no LIKE '%". $_POST['DeliveryNumber'] ."'";
199         $sql .= " GROUP BY trans.trans_no";
200 }
201 else
202 {
203         $sql .= " AND trans.tran_date >= '".date2sql($_POST['DeliveryAfterDate'])."'";
204         $sql .= " AND trans.tran_date <= '".date2sql($_POST['DeliveryToDate'])."'";
205
206         if ($selected_customer != -1)
207                 $sql .= " AND trans.debtor_no='" . $selected_customer . "' ";
208
209         if (isset($selected_stock_item))
210                 $sql .= " AND line.stock_id='". $selected_stock_item ."' ";
211
212         if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != reserved_words::get_all())
213                 $sql .= " AND sorder.from_stk_loc = '". $_POST['StockLocation'] . "' ";
214
215         $sql .= " GROUP BY trans.trans_no ";
216
217 } //end no delivery number selected
218
219 $cols = array(
220                 _("Delivery #") => array('fun'=>'trans_view'), 
221                 _("Customer"), 
222                 'branch_code' => 'skip',
223                 _("Branch") => array('ord'=>''), 
224                 _("Contact"),
225                 _("Reference"), 
226                 _("Cust Ref"), 
227                 _("Delivery Date") => array('type'=>'date', 'ord'=>''),
228                 _("Due By") => 'date', 
229                 _("Delivery Total") => array('type'=>'amount', 'ord'=>''),
230                 _("Currency") => array('align'=>'center'),
231                 submit('BatchInvoice','Batch Inv', false) 
232                         => array('insert'=>true, 'fun'=>'batch_checkbox', 'align'=>'center'),
233                 array('insert'=>true, 'fun'=>'edit_link'),
234                 array('insert'=>true, 'fun'=>'invoice_link'),
235                 array('insert'=>true, 'fun'=>'prt_link')
236 );
237
238 //-----------------------------------------------------------------------------------
239 if (isset($_SESSION['Batch']))
240 {
241     foreach($_SESSION['Batch'] as $trans=>$del)
242         unset($_SESSION['Batch'][$trans]);
243     unset($_SESSION['Batch']);
244 }
245
246 $table =& new_db_pager('deliveries_tbl', $sql, $cols);
247 $table->set_marker('check_overdue', _("Marked items are overdue."));
248
249 start_form();
250
251 display_db_pager($table);
252
253 end_form();
254 end_page();
255
256 ?>
257