handle delivery schedule dates

This commit is contained in:
Bilal Catic
2018-10-31 12:48:45 +01:00
parent e6b6f6c136
commit d6c5a36e94
8 changed files with 612 additions and 7 deletions

View File

@@ -3,7 +3,8 @@
class Wiaas_Admin_Order_Process_Flow {
public static function init() {
add_action('add_meta_boxes', array(__CLASS__, 'create_custom_meta_box'), 100);
add_action('add_meta_boxes', array(__CLASS__, 'create_custom_meta_box'), 100);
add_action('woocommerce_admin_order_data_after_order_details', array(__CLASS__, 'add_custom_fields_after_order_details'), 10, 1 );
}
public static function create_custom_meta_box() {
@@ -25,6 +26,14 @@ class Wiaas_Admin_Order_Process_Flow {
'default'
);
}
public static function add_custom_fields_after_order_details($order){
$global_estimated_delivery_date = Wiaas_Order::get_global_estimated_date($order->id);
$formated_global_estimated_delivery_date = $global_estimated_delivery_date ? date("Y-m-d", $global_estimated_delivery_date) : "";
$order_id = $order->id;
require 'views/html-order-global-delivery-date.php';
}
public static function add_process_flow_meta_box(){
global $post;
@@ -42,17 +51,38 @@ class Wiaas_Admin_Order_Process_Flow {
require 'views/html-order-process-flow.php';
}
}
public static function add_delivery_dates_meta_box(){
// global $post;
global $post;
// $order_id = $post->ID;
$order_id = $post->ID;
$suppliers = Wiaas_Order::get_suppliers($order_id);
$final_estimated_date = Wiaas_Order::get_final_estimated_date($order_id);
$final_confirmed_date = Wiaas_Order::get_final_confirmed_date($order_id);
$earliest_installation_date = Wiaas_Order::get_earliest_installation_date($order_id);
// $suppliers = Wiaas_Order::get_suppliers($order_id);
// require 'views/html-order-delivery-dates.php';
if ($final_estimated_date === 0){
$final_estimated_date = '-';
}else{
$final_estimated_date = date('Y-m-d', $final_estimated_date);
}
if ($final_confirmed_date === 0){
$final_confirmed_date = '-';
}else{
$final_confirmed_date = date('Y-m-d', $final_confirmed_date);
}
if ($earliest_installation_date === 0){
$earliest_installation_date = '-';
}else{
$earliest_installation_date = date('Y-m-d', $earliest_installation_date);
}
require 'views/html-order-delivery-dates.php';
}
}

View File

@@ -0,0 +1,198 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<div>
<table style="width:100%">
<tr>
<th>Suppliers</th>
<th>Estimated date</th>
<th>Confirmed date</th>
</tr>
<?php
foreach($suppliers as $supplier){
echo '<tr><td>' . $supplier['name'] . '</td>';
$estimated_date = $supplier['estimated_date'] ? date("Y-m-d", $supplier['estimated_date']) : "";
$confirmed_date = $supplier['confirmed_date'] ? date("Y-m-d", $supplier['confirmed_date']) : "";
?>
<td>
<input id=<?php echo 'estimate-date-' . $supplier['id'] ?> type="date" onChange="onEstimatedDeliveryDateChange(<?php echo $supplier['id'] ?>, this.value)" value="<?php echo $estimated_date ?>" />
</td>
<td>
<input id=<?php echo 'confirmed-date-' . $supplier['id'] ?> type="date" onChange="onConfirmedDeliveryDateChange(<?php echo $supplier['id'] ?>, this.value)" value="<?php echo $confirmed_date ?>" />
</td>
</tr>
<tr>
<td colspan="5"><h4>Tracking</h4></td>
</tr>
<tr>
<td><button id=<?php echo $supplier['id'] ?> onClick="addAdditionalTrackingInfo(event)">Add more tracking info</button></td>
</tr>
<?php
foreach($supplier['tracking_info'] as $index => $tracking_info){
?>
<tr>
<td></td>
<td><input id=<?php echo 'supplier_' . $supplier['id'] . '_tracking_num_' . $index ?>
placeholder="Tracking number" value="<?php echo $tracking_info['number'] ?>" /></td>
<td><input id=<?php echo 'supplier_' . $supplier['id'] . '_tracking_url_' . $index ?>
placeholder="Tracking URL" value="<?php echo $tracking_info['url'] ?>" /></td>
<td><button class="dashicons-before dashicons-yes" onClick="saveTrackingInfo(event, <?php echo $supplier['id'] . ',' . $index ?>)"></button></td>
<td><button class="dashicons-before dashicons-dismiss" onClick="deleteTrackingInfo(event, <?php echo $supplier['id'] . ',' . $index ?>)"></button></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5"><hr></td>
</tr>
<?php
}
?>
<tr>
<td><h3>Final dates : </h3></td>
<td><h4><?php echo $final_estimated_date ?></h4></td>
<td><h4><?php echo $final_confirmed_date ?></h4></td>
</tr>
<tr>
<td colspan="5"><hr></td>
</tr>
<tr>
<td><h3>Earliest installation date : </h3></td>
<td><h4><?php echo $earliest_installation_date ?></h4></td>
</tr>
</table>
</div>
<script type="text/javascript">
function onEstimatedDeliveryDateChange(supplierID, date) {
var timestamp = parseInt((new Date(date).getTime() / 1000).toFixed(0));
if (isNaN(timestamp)){
timestamp = '';
}
var data = {
action: 'wiaas_save_estimated_date_for_supplier',
_ajax_nonce: '<?php echo wp_create_nonce( "wiaas_save_estimated_date_for_supplier" ) ?>',
order: '<?php echo $order_id ?>',
supplier: supplierID,
date: timestamp
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
if (response.success){
location.reload();
}else{
alert(response.data[0].message);
}
});
}
function onConfirmedDeliveryDateChange(supplierID, date) {
var timestamp = parseInt((new Date(date).getTime() / 1000).toFixed(0));
if (isNaN(timestamp)){
timestamp = '';
}
var data = {
action: 'wiaas_save_confirmed_date_for_supplier',
_ajax_nonce: '<?php echo wp_create_nonce( "wiaas_save_confirmed_date_for_supplier" ) ?>',
order: '<?php echo $order_id ?>',
supplier: supplierID,
date: timestamp
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
if (response.success){
location.reload();
}else{
alert(response.data[0].message);
}
});
}
function addAdditionalTrackingInfo(e){
e.preventDefault();
var data = {
action: 'wiaas_add_additional_tracking_info_for_supplier_in_order',
_ajax_nonce: '<?php echo wp_create_nonce( "wiaas_add_additional_tracking_info_for_supplier_in_order" ) ?>',
order: '<?php echo $order_id ?>',
supplier: e.target.id
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
if (response.success){
location.reload();
}else{
alert(response.data[0].message);
}
});
}
function saveTrackingInfo(e, supplierID, index){
e.preventDefault();
var tracking_num = document.getElementById('supplier_' + supplierID + '_tracking_num_' + index).value;
var tracking_url = document.getElementById('supplier_' + supplierID + '_tracking_url_' + index).value;
var data = {
action: 'wiaas_save_tracking_info',
_ajax_nonce: '<?php echo wp_create_nonce( "wiaas_save_tracking_info" ) ?>',
order: '<?php echo $order_id ?>',
supplier: supplierID,
index: index,
tracking_num: tracking_num,
tracking_url: tracking_url
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
if (response.success){
location.reload();
}else{
alert(response.data[0].message);
}
});
}
function deleteTrackingInfo(e, supplierID, index){
e.preventDefault();
var data = {
action: 'wiaas_delete_tracking_info',
_ajax_nonce: '<?php echo wp_create_nonce( "wiaas_delete_tracking_info" ) ?>',
order: '<?php echo $order_id ?>',
supplier: supplierID,
index: index
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
if (response.success){
location.reload();
}else{
alert(response.data[0].message);
}
});
}
</script>

View File

@@ -0,0 +1,38 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<p class="form-field form-field-wide">
<label for="estimated-delivery-date">Estimated delivery date:</label>
<input id="estimated-delivery-date" name="estimated-delivery-date"
type="date" value="<?php echo $formated_global_estimated_delivery_date ?>"
onChange="onGlobalEstimatedDeliveryDateChange(this.value)"/>
</p>
<script type="text/javascript">
function onGlobalEstimatedDeliveryDateChange(date) {
var timestamp = parseInt((new Date(date).getTime() / 1000).toFixed(0));
if (isNaN(timestamp)){
timestamp = '';
}
var data = {
action: 'wiaas_save_global_estimated_date_for_order',
_ajax_nonce: '<?php echo wp_create_nonce( "wiaas_save_global_estimated_date_for_order" ) ?>',
order: '<?php echo $order_id ?>',
date: timestamp
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
if (response.success){
location.reload();
}else{
alert(response.data[0].message);
}
});
}
</script>

View File

@@ -1,6 +1,12 @@
<?php
add_action( 'wp_ajax_wiaas_create_order_delivery_process', 'wiaas_ajax_create_order_delivery_process' );
add_action( 'wp_ajax_wiaas_add_additional_tracking_info_for_supplier_in_order', 'wiaas_ajax_add_additional_tracking_info_for_supplier_in_order' );
add_action( 'wp_ajax_wiaas_save_tracking_info', 'wiaas_ajax_save_tracking_info');
add_action( 'wp_ajax_wiaas_delete_tracking_info', 'wiaas_ajax_delete_tracking_info');
add_action( 'wp_ajax_wiaas_save_estimated_date_for_supplier', 'wiaas_ajax_save_estimated_date_for_supplier');
add_action( 'wp_ajax_wiaas_save_confirmed_date_for_supplier', 'wiaas_ajax_save_confirmed_date_for_supplier');
add_action( 'wp_ajax_wiaas_save_global_estimated_date_for_order', 'wiaas_ajax_save_global_estimated_date_for_order');
/**
* Creates delivery process for order
@@ -20,3 +26,115 @@ function wiaas_ajax_create_order_delivery_process() {
wp_send_json_error($error);
}
/**
* Adds additional tracking info for supplier in order
*/
function wiaas_ajax_add_additional_tracking_info_for_supplier_in_order(){
check_ajax_referer('wiaas_add_additional_tracking_info_for_supplier_in_order');
$error = new WP_Error('-1', 'Failed to add additional tracking info');
if (!isset($_POST['order']) || !isset($_POST['supplier'])){
wp_send_json_error($error);
}
$order_id = intval( $_POST['order'] );
$supplier_id = intval( $_POST['supplier'] );
if (Wiaas_Order::add_additional_tracking_info($order_id, $supplier_id)){
wp_send_json_success();
}
wp_send_json_error($error);
}
function wiaas_ajax_save_tracking_info(){
check_ajax_referer('wiaas_save_tracking_info');
$error = new WP_Error('-1', 'Failed to save tracking info');
if (!isset($_POST['order']) || !isset($_POST['supplier']) || !isset($_POST['index'])
|| !isset($_POST['tracking_num']) || !isset($_POST['tracking_url'])){
wp_send_json_error($error);
}
$order_id = intval( $_POST['order'] );
$supplier_id = intval( $_POST['supplier'] );
$index = intval($_POST['index']);
$tracking_num = $_POST['tracking_num'];
$tracking_url = $_POST['tracking_url'];
if (Wiaas_Order::save_tracking_info($order_id, $supplier_id, $index, $tracking_num, $tracking_url)){
wp_send_json_success();
}
wp_send_json_error($error);
}
function wiaas_ajax_delete_tracking_info(){
check_ajax_referer('wiaas_delete_tracking_info');
$error = new WP_Error('-1', 'Failed to save tracking info');
if (!isset($_POST['order']) || !isset($_POST['supplier']) || !isset($_POST['index'])){
wp_send_json_error($error);
}
$order_id = intval( $_POST['order'] );
$supplier_id = intval( $_POST['supplier'] );
$index = intval($_POST['index']);
if (Wiaas_Order::delete_tracking_info($order_id, $supplier_id, $index)){
wp_send_json_success();
}
wp_send_json_error($error);
}
function wiaas_ajax_save_estimated_date_for_supplier(){
check_ajax_referer('wiaas_save_estimated_date_for_supplier');
$error = new WP_Error('-1', 'Failed to save estimated date');
if (!isset($_POST['order']) || !isset($_POST['supplier']) || !isset($_POST['date'])){
wp_send_json_error($error);
}
$order_id = intval( $_POST['order'] );
$supplier_id = intval( $_POST['supplier'] );
$date = intval($_POST['date']);
if (Wiaas_Order::save_estimated_date($order_id, $supplier_id, $date)){
wp_send_json_success();
}
wp_send_json_error($error);
}
function wiaas_ajax_save_confirmed_date_for_supplier(){
check_ajax_referer('wiaas_save_confirmed_date_for_supplier');
$error = new WP_Error('-1', 'Failed to save confirmed date');
if (!isset($_POST['order']) || !isset($_POST['supplier']) || !isset($_POST['date'])){
wp_send_json_error($error);
}
$order_id = intval( $_POST['order'] );
$supplier_id = intval( $_POST['supplier'] );
$date = intval($_POST['date']);
if (Wiaas_Order::save_confirmed_date($order_id, $supplier_id, $date)){
wp_send_json_success();
}
wp_send_json_error($error);
}
function wiaas_ajax_save_global_estimated_date_for_order(){
check_ajax_referer('wiaas_save_global_estimated_date_for_order');
$error = new WP_Error('-1', 'Failed to save global estimated date for order');
if (!isset($_POST['order']) || !isset($_POST['date'])){
wp_send_json_error($error);
}
$order_id = intval( $_POST['order'] );
$date = intval($_POST['date']);
if (Wiaas_Order::save_global_estimated_date($order_id, $date)){
wp_send_json_success();
}
wp_send_json_error($error);
}