Plugin Directory

Changeset 3496827


Ignore:
Timestamp:
04/01/2026 04:43:32 PM (4 days ago)
Author:
flowdino
Message:

Fix orders

Location:
flowdino/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • flowdino/trunk/changelog.txt

    r3493443 r3496827  
    11*** FlowDino Changelog ***
     2
     32026-04-01 - version 1.1.11
     4* Sales - Orders are now imported with status "Processing" instead of "Pending"
     5* Sales - Order lines are matched against WooCommerce products via FlowDino sync table, externalKey, SKU and title
     6* Sales - Stock levels are automatically decremented when a new order is imported
     7* Sales - Shipping label URL (urlEtiquette) is saved on the order and displayed as a download button in the WC order admin page
     8* Sales - Tax handling: TTC amounts are split into HT + VAT when WooCommerce taxes are enabled
     9* Sales - Added resync feature: reset sync date from a chosen date and re-import all orders
    210
    3112026-03-28 - version 1.0.9
  • flowdino/trunk/flowdino.php

    r3496763 r3496827  
    44 * Plugin URI: https://www.flowdino.com
    55 * Description: Automatically synchronize your WooCommerce catalog with the FlowDino platform for centralized product management.
    6  * Version: 1.0.9
     6 * Version: 1.1.11
    77 * Author: Jeremy DUMONT
    88 * License: GPL v2 or later
     
    2828}
    2929if (!defined('FLOWDINO_VERSION')) {
    30     define('FLOWDINO_VERSION', '1.0.9');
     30    define('FLOWDINO_VERSION', '1.1.11');
    3131}
    3232
  • flowdino/trunk/includes/tabs/class-flowdino-wc-sales-tab.php

    r3496763 r3496827  
    380380
    381381        // --- Order lines ---
    382         $order_lines  = $order_data['orderLines'] ?? array();
    383         $total_lines  = 0.0;
     382        $order_lines     = $order_data['orderLines'] ?? array();
     383        $total_lines_ht  = 0.0;
     384        $billing_country = $order->get_billing_country();
    384385
    385386        if (!empty($order_lines)) {
    386387            foreach ($order_lines as $line) {
    387388                $product_id = $this->get_wc_product_id_from_line($line);
    388                 $line_total = (float) ($line['amountLines'] ?? 0);
    389                 $total_lines += $line_total;
     389                $line_ttc   = (float) ($line['amountLines'] ?? 0);
     390
     391                $product   = $product_id ? wc_get_product($product_id) : null;
     392                $tax_class = $product ? $product->get_tax_class() : '';
     393                $amounts   = $this->split_ttc_amount($line_ttc, $billing_country, $tax_class);
     394                $total_lines_ht += $amounts['ht'];
    390395
    391396                $item = new WC_Order_Item_Product();
    392397                $item->set_name(sanitize_text_field($line['title'] ?? __('Produit FlowDino', 'flowdino')));
    393398                $item->set_quantity(1);
    394                 $item->set_total($line_total);
    395                 $item->set_subtotal($line_total);
     399                $item->set_subtotal($amounts['ht']);
     400                $item->set_total($amounts['ht']);
    396401
    397402                if ($product_id) {
    398403                    $item->set_product_id($product_id);
    399                     $product = wc_get_product($product_id);
    400                     if ($product && $product->get_type() === 'variable') {
    401                         // Leave variation_id at 0 – enough for line creation
    402                     }
     404                }
     405
     406                if (!empty($amounts['rates']) && $amounts['tax'] > 0.0) {
     407                    $taxes = WC_Tax::calc_tax($amounts['ht'], $amounts['rates'], false);
     408                    $item->set_taxes(array('total' => $taxes, 'subtotal' => $taxes));
    403409                }
    404410
     
    409415            $amount_order = (float) ($order_data['amountOrder'] ?? 0);
    410416            $fees_order   = (float) ($order_data['feesOrder']   ?? 0);
    411             $total_lines  = $amount_order - $fees_order;
     417            $line_ttc     = $amount_order - $fees_order;
     418
     419            $amounts        = $this->split_ttc_amount($line_ttc, $billing_country, '');
     420            $total_lines_ht = $amounts['ht'];
    412421
    413422            $item = new WC_Order_Item_Product();
     
    418427            ));
    419428            $item->set_quantity(1);
    420             $item->set_total($total_lines);
    421             $item->set_subtotal($total_lines);
     429            $item->set_subtotal($amounts['ht']);
     430            $item->set_total($amounts['ht']);
     431
     432            if (!empty($amounts['rates']) && $amounts['tax'] > 0.0) {
     433                $taxes = WC_Tax::calc_tax($amounts['ht'], $amounts['rates'], false);
     434                $item->set_taxes(array('total' => $taxes, 'subtotal' => $taxes));
     435            }
     436
    422437            $order->add_item($item);
    423438        }
    424439
    425         // Totals
    426         $order->set_total((float) ($order_data['amountOrder'] ?? $total_lines));
     440        // Aggregate tax line items from per-item taxes
     441        if (wc_tax_enabled()) {
     442            $order->calculate_taxes();
     443        }
     444
     445        // Grand total = TTC amount from FlowDino
     446        $grand_total_ttc = (float) ($order_data['amountOrder'] ?? 0);
     447        $order->set_total($grand_total_ttc > 0 ? $grand_total_ttc : $total_lines_ht);
    427448
    428449        // FlowDino order reference as meta
     
    584605    private function clean_field($value) {
    585606        return sanitize_text_field(wp_strip_all_tags($value));
     607    }
     608
     609    /**
     610     * Split a TTC (tax-included) amount into HT (ex-tax) and tax portions.
     611     *
     612     * If WooCommerce taxes are disabled, returns the full amount as HT with no tax.
     613     * Uses WC_Tax::find_rates() with the billing country and product tax class.
     614     *
     615     * @param float  $ttc           Amount including tax.
     616     * @param string $country       Billing country ISO code (e.g. 'FR').
     617     * @param string $tax_class     WooCommerce tax class ('' = standard).
     618     * @return array { ht: float, tax: float, rates: array }
     619     */
     620    private function split_ttc_amount(float $ttc, string $country, string $tax_class = ''): array {
     621        $empty = array('ht' => $ttc, 'tax' => 0.0, 'rates' => array());
     622
     623        if (!wc_tax_enabled() || $ttc <= 0) {
     624            return $empty;
     625        }
     626
     627        $rates = WC_Tax::find_rates(array(
     628            'country'   => $country,
     629            'tax_class' => $tax_class,
     630        ));
     631
     632        if (empty($rates)) {
     633            $rates = WC_Tax::get_rates($tax_class);
     634        }
     635
     636        if (empty($rates)) {
     637            return $empty;
     638        }
     639
     640        $total_rate = array_sum(array_column($rates, 'rate'));
     641        if ($total_rate <= 0) {
     642            return array('ht' => $ttc, 'tax' => 0.0, 'rates' => $rates);
     643        }
     644
     645        $ht  = (float) wc_format_decimal($ttc / (1.0 + $total_rate / 100.0), wc_get_price_decimals());
     646        $tax = (float) wc_format_decimal($ttc - $ht, wc_get_price_decimals());
     647
     648        return array('ht' => $ht, 'tax' => $tax, 'rates' => $rates);
    586649    }
    587650
  • flowdino/trunk/readme.txt

    r3493443 r3496827  
    55Tested up to: 6.9
    66Requires PHP: 7.4
    7 Stable tag: 1.0.9
     7Stable tag: 1.1.11
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
Note: See TracChangeset for help on using the changeset viewer.