1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
<?php
class view_deliveries {
public function __construct() {
add_action( 'admin_init', array( &$this, 'prdd_data_export' ) );
}
public static function prdd_woocommerce_prdd_history_page() {
if ( isset( $_GET[ 'action' ] ) ) {
$action = $_GET[ 'action' ];
} else {
$action = '';
}
if ( $action == 'history' || $action == '' ) {
$active_settings = "nav-tab-active";
}
if ( $action == 'history' || $action == '' ) {
global $wpdb;
include_once( 'prdd-class-view-deliveries-table.php' );
$prdd_table = new PRDD_View_Deliveries_Table();
$prdd_table->prdd_prepare_items();
?>
<div class="wrap">
<h2><?php _e( 'All Deliveries', 'woocommerce-prdd' ); ?></h2>
<?php do_action( 'prdd_page_top' ); ?>
<form id="prdd-view-deliveries" method="get" action="<?php echo admin_url( 'admin.php?page=woocommerce_prdd_history_page' ); ?>">
<div id="prdd_update_event_message"></div>
<p id="prdd_add_order">
<a href="
<?php echo isset ( $_GET[ 'prdd_view' ] ) && $_GET[ 'prdd_view' ] == "prdd_calender" ? admin_url( 'admin.php?page=woocommerce_prdd_history_page' ) : esc_url( add_query_arg( 'prdd_view', 'prdd_calender' ) ); ?>"
class="button-secondary">
<?php isset ( $_GET[ 'prdd_view' ] ) && $_GET[ 'prdd_view' ] == "prdd_calender" ? _e( 'View Delivery Listing', 'woocommerce-prdd' ) : _e ( 'Calendar View', 'woocommerce-prdd' ); ?>
</a>
<?php
if ( !isset( $_GET[ 'prdd_view' ] ) ) {
$gcal = new TSGcal();
$total_deliveries_to_export = prdd_common::prdd_get_total_deliveries_to_export();
$display_button_setting = get_option( 'prdd_admin_add_to_calendar_delivery_calendar' );
if( $gcal->get_api_mode() == "directly" && 'on' == $display_button_setting ) {
?>
<input type="button" class="button-secondary" id="prdd_admin_add_to_calendar_delivery" style="float:right;" value="<?php _e( 'Add to Google Calendar', 'woocommerce-prdd' ); ?>">
<script type="text/javascript">
jQuery( document ).ready( function(){
jQuery( "#prdd_admin_add_to_calendar_delivery" ).on( 'click', function() {
<?php if ( count( $total_deliveries_to_export ) > 0 ) {?>
var orders_to_export = "<?php echo count( $total_deliveries_to_export ); ?>";
jQuery( "#prdd_update_event_message" ).html( "Total orders to export " + orders_to_export + " ... " );
var data = {
action: "prdd_admin_delivery_calendar_events"
};
jQuery.post( "<?php echo get_admin_url(); ?>/admin-ajax.php", data, function( response ) {
jQuery( "#prdd_update_event_message" ).html( orders_to_export + " deliveries have been exported to your Google Calendar. Please refresh your Google Calendar." );
});
<?php } else {?>
jQuery( "#prdd_update_event_message" ).html( "No pending orders left to be exported." );
<?php }?>
});
});
</script>
<?php
}
?>
<a href="<?php echo esc_url( add_query_arg( 'download', 'data.print' ) ); ?>" style="float:right;" class="button-secondary"><?php _e( 'Print', 'woocommerce-prdd' ); ?></a>
<a href="<?php echo esc_url( add_query_arg( 'download', 'data.csv' ) ); ?>" style="float:right;" class="button-secondary"><?php _e( 'CSV', 'woocommerce-prdd' ); ?></a>
<?php }?>
</p>
<input type="hidden" name="page" value="woocommerce_prdd_history_page" />
<?php if ( isset( $_GET[ 'prdd_view' ] ) && ( $_GET[ 'prdd_view' ] == 'prdd_calender' ) ) { ?>
<h2><?php _e( 'Calendar View', 'woocommerce-prdd' ); ?></h2>
<div id='calendar'></div>
<?php } else {
$prdd_table->views();
$prdd_table->display();
} ?>
</form>
<?php do_action( 'prdd_page_bottom' ); ?>
</div>
<?php
}
}
public function prdd_data_export() {
global $wpdb;
$tab_status = '';
if ( isset( $_GET[ 'status' ] ) ) {
$tab_status = $_GET[ 'status' ];
}
if ( isset( $_GET[ 'download' ] ) && ( $_GET[ 'download' ] == 'data.csv' ) ) {
$report = view_deliveries::generate_data( $tab_status );
$csv = view_deliveries::generate_csv( $report );
header( "Content-type: application/x-msdownload" );
header( "Content-Disposition: attachment; filename=data.csv" );
header( "Pragma: no-cache" );
header( "Expires: 0" );
echo $csv;
exit;
} else if ( isset( $_GET[ 'download' ] ) && ( $_GET[ 'download' ] == 'data.print' ) ) {
$report = view_deliveries::generate_data( $tab_status );
$currency_symbol = get_woocommerce_currency_symbol();
$print_data_columns = "
<tr>
<th style='border:1px solid black;padding:5px;'>" . __( 'Order ID', 'woocommerce-prdd' ) . "</th>
<th style='border:1px solid black;padding:5px;'>" . __( 'Customer Name', 'woocommerce-prdd' ) . "</th>
<th style='border:1px solid black;padding:5px;'>" . __( 'Product Name', 'woocommerce-prdd' ) . "</th>
<th style='border:1px solid black;padding:5px;'>" . __( 'Delivery Date', 'woocommerce-prdd' ) . "</th>
<th style='border:1px solid black;padding:5px;'>" . __( 'Delivery Time', 'woocommerce-prdd' ) . "</th>
<th style='border:1px solid black;padding:5px;'>" . __( 'Quantity', 'woocommerce-prdd' ) . "</th>
<th style='border:1px solid black;padding:5px;'>" . __( 'Amount', 'woocommerce-prdd' ) . "</th>
<th style='border:1px solid black;padding:5px;'>" . __( 'Order Date', 'woocommerce-prdd' ) . "</th>
</tr>";
$print_data_row_data = '';
foreach ( $report as $key => $value ) {
$print_data_row_data .= "<tr>
<td style='border:1px solid black;padding:5px;'>" . $value->order_id . "</td>
<td style='border:1px solid black;padding:5px;'>" . $value->customer_name . "</td>
<td style='border:1px solid black;padding:5px;'>" . $value->product_name . "</td>
<td style='border:1px solid black;padding:5px;'>" . $value->delivery_date . "</td>
<td style='border:1px solid black;padding:5px;'>" . $value->time . "</td>
<td style='border:1px solid black;padding:5px;'>" . $value->quantity . "</td>
<td style='border:1px solid black;padding:5px;'>" . $currency_symbol . $value->amount . "</td>
<td style='border:1px solid black;padding:5px;'>" . $value->order_date . "</td>
</tr>";
}
$print_data_columns = apply_filters( 'prdd_view_deliveries_print_columns', $print_data_columns );
$print_data_row_data = apply_filters( 'prdd_view_deliveries_print_rows', $print_data_row_data, $report );
$print_data = "<table style='border:1px solid black;border-collapse:collapse;'>" . $print_data_columns . $print_data_row_data . "</table>";
echo $print_data;
exit;
}
}
function generate_data( $tab_status ) {
global $wpdb;
$results = array();
$current_time = current_time( 'timestamp' );
$current_date = date( "Y-m-d", $current_time );
if ( $tab_status == 'future' ) {
$query_get_id = "SELECT * FROM `".$wpdb->prefix."woocommerce_order_itemmeta`
WHERE meta_key = %s
AND meta_value >= %s ORDER BY order_item_id DESC";
$get_order_item_id = $wpdb->get_results( $wpdb->prepare( $query_get_id, '_prdd_date', $current_date ) );
} else if ( $tab_status == 'today_delivery' ) {
$query_get_id = "SELECT * FROM `".$wpdb->prefix."woocommerce_order_itemmeta`
WHERE meta_key = %s
AND meta_value = %s ORDER BY order_item_id DESC";
$get_order_item_id = $wpdb->get_results( $wpdb->prepare( $query_get_id, '_prdd_date', $current_date ) );
} else {
$query_get_id = "SELECT * FROM `".$wpdb->prefix."woocommerce_order_itemmeta`
WHERE meta_key = %s ORDER BY order_item_id DESC";
$get_order_item_id = $wpdb->get_results( $wpdb->prepare( $query_get_id, '_prdd_date' ) );
}
$date_formats = prdd_get_delivery_arrays( 'prdd_date_formats' );
$global_settings = json_decode( get_option( 'woocommerce_prdd_global_settings' ) );
$date_format_set = $date_formats[ $global_settings->prdd_date_format ];
$time_format_set = $global_settings->prdd_time_format;
$report = array();
$i = 0;
$time_slot = $date = $from_time = $to_time = $time = '';
foreach ( $get_order_item_id as $key => $value ) {
$time_slot = $date = $time = '';
$query_order_id = "SELECT order_id FROM `".$wpdb->prefix."woocommerce_order_items`
WHERE order_item_id = %d";
$get_order_id = $wpdb->get_results( $wpdb->prepare( $query_order_id, $value->order_item_id ) );
if ( false !== get_post_status( $get_order_id[0]->order_id ) ) {
$order = new WC_Order( $get_order_id[0]->order_id );
} else {
continue;
}
if( version_compare( get_option( 'woocommerce_version ' ), '3.0.0', ">=" ) ) {
$order_post_status = $order->get_status();
if( $order_post_status != 'trash' ) {
$order_post_status = "wc-" . $order_post_status;
}
} else {
$order_post_status = $order->post_status;
}
if( isset( $order_post_status ) && ( $order_post_status != '' ) && ( $order_post_status != 'wc-cancelled' ) && ( $order_post_status != 'wc-refunded' ) && ( $order_post_status != 'trash' ) && ( $order_post_status != 'wc-failed' ) ) {
$query_get_dates = "SELECT meta_value, meta_key FROM `".$wpdb->prefix."woocommerce_order_itemmeta`
WHERE meta_key IN (%s,%s,%s)
AND meta_value
AND order_item_id = %d";
$get_dates = $wpdb->get_results( $wpdb->prepare( $query_get_dates, '_prdd_date', '_prdd_time_slot','_product_id', $value->order_item_id ) );
foreach ( $get_dates as $k => $v ) {
if( $v->meta_key == '_prdd_date' ) {
$date = $v->meta_value;
}
if ( $v->meta_key == '_prdd_time_slot' ) {
$time_slot = $v->meta_value;
}
if ( $v->meta_key == '_product_id' ) {
$product_id = $v->meta_value;
}
}
$report[$i] = new stdClass();
$report[$i]->order_id = $get_order_id[0]->order_id;
if( version_compare( get_option( 'woocommerce_version ' ), '3.0.0', ">=" ) ) {
$report[$i]->customer_name = $order->get_billing_first_name() . " " . $order->get_billing_last_name();
} else {
$report[$i]->customer_name = $order->billing_first_name . " " . $order->billing_last_name;
}
$report[$i]->product_id = $product_id;
$report[$i]->product_name = get_the_title( $product_id );
$report[$i]->delivery_date = date( $date_format_set, strtotime( $date ) );
if ( $time_slot != '' ) {
$time_slot_arr = explode( " - ", $time_slot );
if( isset( $time_slot_arr[ 0 ] ) && $time_slot_arr[ 0 ] != '' ) {
$from_time = $time_slot_arr[ 0 ];
}
if( isset( $time_slot_arr[ 1 ] ) && $time_slot_arr[ 1 ] != '' ) {
$to_time = $time_slot_arr[ 1 ];
}
if ( $time_format_set == 12 ) {
$from_time_str = date( 'h:i A', strtotime( $from_time ) );
$to_time_str = date( 'h:i A', strtotime( $to_time ) );
$time = $from_time_str . " - " . $to_time_str;
} else {
$time = $from_time . " - " . $to_time;
}
}
$report[ $i ]->time = $time;
$get_quantity = $order->get_items();
$get_quantity = array_reverse( $get_quantity, true );
foreach( $get_quantity as $k => $v ) {
$product_exists = 'NO';
if ( $v[ 'product_id' ] == $product_id ) {
foreach ( $report as $delivery_key => $delivery_value ) {
if ( $delivery_value->order_id == $get_order_id[0]->order_id && $v[ 'product_id' ] == $delivery_value->product_id ) {
if ( isset( $book_value->item_id ) && $k == $book_value->item_id ) {
$product_exists = 'YES';
}
}
}
if ( $product_exists == 'NO' ) {
$selected_quantity = $v[ 'qty' ];
$amount = $v[ 'line_total' ] + $v[ 'line_tax' ];
$report[$i]->item_id = $k;
break;
}
}
}
$report[$i]->quantity = $selected_quantity;
$report[$i]->amount = $amount;
if( version_compare( get_option( 'woocommerce_version ' ), '3.0.0', ">=" ) ) {
$order_date_obj = $order->get_date_created();
$order_date = $order_date_obj->format( 'Y-m-d' );
} else {
$order_date = $order->completed_date;
}
$report[$i]->order_date = $order_date;
$i++;
}
}
return apply_filters( 'prdd_export_data', $report );
}
function generate_csv( $report ) {
$currency_symbol = get_woocommerce_currency_symbol();
$csv = 'Order ID,Customer Name,Product Name,Delivery Date,Delivery Time,Quantity,Amount, Order Date';
$csv .= "\n";
foreach ( $report as $key => $value ) {
$order_id = $value->order_id;
$customer_name = $value->customer_name;
$product_name = $value->product_name;
$delivery_date = $value->delivery_date;
$time = $value->time;
$selected_quantity = $value->quantity;
$amount = $value->amount;
if ( absint( $value->order_id ) > 0 && false !== get_post_status( $value->order_id ) ) {
$the_order = wc_get_order( $value->order_id );
$currency = ( version_compare( WOOCOMMERCE_VERSION, "3.0.0" ) < 0 ) ? $the_order->get_order_currency() : $the_order->get_currency();
} else {
$currency = get_woocommerce_currency();
}
$currency_symbol = get_woocommerce_currency_symbol( $currency );
$amount = html_entity_decode( $currency_symbol ) . " " . $amount;
$order_date = $value->order_date;
$csv .= $order_id . ',' . $customer_name . ',' . $product_name . ',"' . $delivery_date . '","' . $time . '",' . $selected_quantity . ',' . $amount . ',' . $order_date;
$csv .= "\n";
}
$csv = apply_filters( 'prdd_csv_data', $csv, $report );
return $csv;
}
}
$view_deliveries = new view_deliveries();