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