Fixed get_post() support for numeric fields.
[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 ($SysPrefs->use_popup_windows)
22         $js .= get_js_open_window(900, 600);
23 if (user_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(_($help_context = "Search Not Invoiced Deliveries"), false, false, "", $js);
30 }
31 else
32 {
33         $_POST['OutstandingOnly'] = false;
34         page(_($help_context = "Search All Deliveries"), false, false, "", $js);
35 }
36
37 if (isset($_GET['selected_customer']))
38 {
39         $_POST['customer_id'] = $_GET['selected_customer'];
40 }
41 elseif (isset($_POST['selected_customer']))
42 {
43         $_POST['customer_id'] = $_POST['selected_customer'];
44 }
45
46 if (isset($_POST['BatchInvoice']))
47 {
48         // checking batch integrity
49     $del_count = 0;
50     if (isset($_POST['Sel_'])) {
51                 foreach($_POST['Sel_'] as $delivery => $branch) {
52                         $checkbox = 'Sel_'.$delivery;
53                         if (check_value($checkbox))     {
54                                 if (!$del_count) {
55                                         $del_branch = $branch;
56                                 }
57                                 else {
58                                         if ($del_branch != $branch)     {
59                                                 $del_count=0;
60                                                 break;
61                                         }
62                                 }
63                                 $selected[] = $delivery;
64                                 $del_count++;
65                         }
66                 }
67         }
68     if (!$del_count) {
69                 display_error(_('For batch invoicing you should
70                     select at least one delivery. All items must be dispatched to
71                     the same customer branch.'));
72     } else {
73                 $_SESSION['DeliveryBatch'] = $selected;
74                 meta_forward($path_to_root . '/sales/customer_invoice.php','BatchInvoice=Yes');
75     }
76 }
77
78 //-----------------------------------------------------------------------------------
79 if (get_post('_DeliveryNumber_changed')) 
80 {
81         $disable = get_post('DeliveryNumber') !== '';
82
83         $Ajax->addDisable(true, 'DeliveryAfterDate', $disable);
84         $Ajax->addDisable(true, 'DeliveryToDate', $disable);
85         $Ajax->addDisable(true, 'StockLocation', $disable);
86         $Ajax->addDisable(true, '_SelectStockFromList_edit', $disable);
87         $Ajax->addDisable(true, 'SelectStockFromList', $disable);
88         // if search is not empty rewrite table
89         if ($disable) {
90                 $Ajax->addFocus(true, 'DeliveryNumber');
91         } else
92                 $Ajax->addFocus(true, 'DeliveryAfterDate');
93         $Ajax->activate('deliveries_tbl');
94 }
95
96 //-----------------------------------------------------------------------------------
97
98 start_form(false, false, $_SERVER['PHP_SELF'] ."?OutstandingOnly=".$_POST['OutstandingOnly']);
99
100 start_table(TABLESTYLE_NOBORDER);
101 start_row();
102 ref_cells(_("#:"), 'DeliveryNumber', '',null, '', true);
103 date_cells(_("from:"), 'DeliveryAfterDate', '', null, -user_transaction_days());
104 date_cells(_("to:"), 'DeliveryToDate', '', null, 1);
105
106 locations_list_cells(_("Location:"), 'StockLocation', null, true);
107 end_row();
108
109 end_table();
110 start_table(TABLESTYLE_NOBORDER);
111 start_row();
112
113 stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true);
114
115 customer_list_cells(_("Select a customer: "), 'customer_id', null, true, true);
116
117 submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
118
119 hidden('OutstandingOnly', $_POST['OutstandingOnly']);
120
121 end_row();
122
123 end_table(1);
124 //---------------------------------------------------------------------------------------------
125
126 function trans_view($trans, $trans_no)
127 {
128         return get_customer_trans_view_str(ST_CUSTDELIVERY, $trans['trans_no']);
129 }
130
131 function batch_checkbox($row)
132 {
133         $name = "Sel_" .$row['trans_no'];
134         return $row['Done'] ? '' :
135                 "<input type='checkbox' name='$name' value='1' >"
136 // add also trans_no => branch code for checking after 'Batch' submit
137          ."<input name='Sel_[".$row['trans_no']."]' type='hidden' value='"
138          .$row['branch_code']."'>\n";
139 }
140
141 function edit_link($row)
142 {
143         return $row["Outstanding"]==0 ? '' :
144                 trans_editor_link(ST_CUSTDELIVERY, $row['trans_no']);
145 }
146
147 function prt_link($row)
148 {
149         return print_document_link($row['trans_no'], _("Print"), true, ST_CUSTDELIVERY, ICON_PRINT);
150 }
151
152 function invoice_link($row)
153 {
154         return $row["Outstanding"]==0 ? '' :
155                 pager_link(_('Invoice'), "/sales/customer_invoice.php?DeliveryNumber=" 
156                         .$row['trans_no'], ICON_DOC);
157 }
158
159 function check_overdue($row)
160 {
161         return date1_greater_date2(Today(), sql2date($row["due_date"])) && 
162                         $row["Outstanding"]!=0;
163 }
164 //------------------------------------------------------------------------------------------------
165 $sql = get_sql_for_sales_deliveries_view(get_post('DeliveryAfterDate'), get_post('DeliveryToDate'), get_post('customer_id'),    
166         get_post('SelectStockFromList'), get_post('StockLocation'), get_post('DeliveryNumber'), get_post('OutstandingOnly'));
167
168 $cols = array(
169                 _("Delivery #") => array('fun'=>'trans_view', 'align'=>'right'), 
170                 _("Customer"), 
171                 'branch_code' => 'skip',
172                 _("Branch") => array('ord'=>''), 
173                 _("Contact"),
174                 _("Reference"), 
175                 _("Cust Ref"), 
176                 _("Delivery Date") => array('type'=>'date', 'ord'=>''),
177                 _("Due By") => 'date', 
178                 _("Delivery Total") => array('type'=>'amount', 'ord'=>''),
179                 _("Currency") => array('align'=>'center'),
180                 submit('BatchInvoice',_("Batch"), false, _("Batch Invoicing")) 
181                         => array('insert'=>true, 'fun'=>'batch_checkbox', 'align'=>'center'),
182                 array('insert'=>true, 'fun'=>'edit_link'),
183                 array('insert'=>true, 'fun'=>'invoice_link'),
184                 array('insert'=>true, 'fun'=>'prt_link')
185 );
186
187 //-----------------------------------------------------------------------------------
188 if (isset($_SESSION['Batch']))
189 {
190     foreach($_SESSION['Batch'] as $trans=>$del)
191         unset($_SESSION['Batch'][$trans]);
192     unset($_SESSION['Batch']);
193 }
194
195 $table =& new_db_pager('deliveries_tbl', $sql, $cols);
196 $table->set_marker('check_overdue', _("Marked items are overdue."));
197
198 //$table->width = "92%";
199
200 display_db_pager($table);
201
202 end_form();
203 end_page();
204