businessdad icon

EU VAT Assistant - Update VAT on past orders

businessdad | PRO | 10/05/18 04:42:51 PM UTC | 0 ⭐ | 1072 👁️ | Never ⏰ | []
PHP |

2.86 KB

|

None

|

0 👍

/

0 👎

/**
 * Aelia EU VAT Assistant for WooCommerce - Update VAT data for existing orders.
 * This code loops through existing orders and calls the EU VAT Assistant functions
 * that update the VAT data stored against each one.
 *
 * HOW TO USE THIS CODE
 * Add the code to the bottom of your theme's functions.php file (see https://www.skyverge.com/blog/add-custom-code-to-wordpress/).
 *
 * The script accepts the following URL arguments:
 * - update_order_vat_data=1. This argument triggers the processing of orders.
 * - start_date=YYMMDD. The script will take orders created on or after this date.
 * - end_date=YYMMDD. The script will take orders created BEFORE this date.
 * - debug=1. If specified, the script will print out debug information.
 *
 * GPL DISCLAIMER
 * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
 * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
 * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
 * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
 *
 * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
 *
 * @author Aelia
 */
add_action('admin_init', function() {
    if(!is_admin() || empty($_GET['update_order_vat_data']) || !current_user_can('manage_woocommerce')) {
        return;
    }
 
    global $wpdb;
 
    // Start date
    if(isset($_GET['start_date'])) {
        $start_date = $_GET['start_date'];
    }
    else {
        // Take the 1st of January of current year as the default start date
        $start_date = date('Y0101');
    }
 
    // End date
    if(isset($_GET['end_date'])) {
        $end_date = $_GET['end_date'];
    }
    else {
        // Take tomorrow as the default end date
        $default_end_date = new DateTime('tomorrow');
        $end_date = $default_end_date->format('Ymd');
    }
 
    $px = $wpdb->prefix;
    $SQL = "
        SELECT
            ORDERS.ID AS order_id
        FROM
            {$px}posts AS ORDERS
        WHERE
            (ORDERS.post_type IN ('shop_order')) AND
            (ORDERS.post_status IN ('wc-processing', 'wc-completed')) AND
            (ORDERS.post_date >= '$start_date') AND
            (ORDERS.post_date < '$end_date')
    ";
 
    // Debug
    if(isset($_GET['debug'])) {
        var_dump($SQL);
    }
    // Fetch the orders to process
    $dataset = $wpdb->get_results($SQL);
 
    if(isset($_GET['debug'])) {
        var_dump($SQL, $dataset);
    }
 
    foreach($dataset as $entry) {
        $order = new \Aelia\WC\EU_VAT_Assistant\Order($entry->order_id);
        // Generate and store details about order VAT
        $order->update_vat_data();
        // Save EU VAT compliance evidence
        //$order->store_vat_evidence();
    }
});

Comments