Plugin Directory

Changeset 3497705


Ignore:
Timestamp:
04/02/2026 05:31:16 PM (2 days ago)
Author:
flowdino
Message:

Change Order line et sum

Location:
flowdino/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • flowdino/trunk/changelog.txt

    r3496827 r3497705  
    11*** FlowDino Changelog ***
     2
     32026-04-02 - version 1.1.12
     4* Sales - Shipping lines ("Frais de port") are now imported as WooCommerce shipping items instead of product lines
     5* Sales - Discount lines ("Remise offre acceptée") are now absorbed into the product line subtotal/total, displaying the discount on the product row
     6* Product - Add variations name
    27
    382026-04-01 - version 1.1.11
  • flowdino/trunk/flowdino.php

    r3496827 r3497705  
    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.1.11
     6 * Version: 1.1.12
    77 * Author: Jeremy DUMONT
    88 * License: GPL v2 or later
     
    2828}
    2929if (!defined('FLOWDINO_VERSION')) {
    30     define('FLOWDINO_VERSION', '1.1.11');
     30    define('FLOWDINO_VERSION', '1.1.12');
    3131}
    3232
  • flowdino/trunk/includes/class-flowdino-wc-sync.php

    r3493443 r3497705  
    319319            'active'             => $active,
    320320            'url'                => $variation->get_permalink(),
     321            'variation'          => $variation_label,
    321322        );
    322323
  • flowdino/trunk/includes/tabs/class-flowdino-wc-products-tab.php

    r3493443 r3497705  
    14421442                                    ) ? 1 : 0,
    14431443            'url'                => $variation->get_permalink(),
     1444            'variation'          => $variation_label,
    14441445        );
    14451446
  • flowdino/trunk/includes/tabs/class-flowdino-wc-sales-tab.php

    r3496827 r3497705  
    385385
    386386        if (!empty($order_lines)) {
     387            // --- Classify lines: product, shipping, discount ---
     388            $product_lines  = array();
     389            $shipping_lines = array();
     390            $discount_lines = array();
     391
    387392            foreach ($order_lines as $line) {
     393                $amount   = (float) ($line['amountLines'] ?? 0);
     394                $title_lc = strtolower($line['title'] ?? '');
     395                $has_product = !empty($line['productId']) || !empty($line['product']['id']);
     396
     397                if ($has_product) {
     398                    $product_lines[] = $line;
     399                } elseif (
     400                    strpos($title_lc, 'frais de port') !== false ||
     401                    strpos($title_lc, 'frais de livraison') !== false ||
     402                    strpos($title_lc, 'livraison') !== false ||
     403                    strpos($title_lc, 'expédition') !== false ||
     404                    strpos($title_lc, 'expedition') !== false ||
     405                    strpos($title_lc, 'shipping') !== false
     406                ) {
     407                    $shipping_lines[] = $line;
     408                } elseif ($amount < 0) {
     409                    $discount_lines[] = $line;
     410                } else {
     411                    $product_lines[] = $line;
     412                }
     413            }
     414
     415            // --- Total discount TTC (sum of negative discount lines) ---
     416            $total_discount_ttc = 0.0;
     417            foreach ($discount_lines as $dl) {
     418                $total_discount_ttc += (float) ($dl['amountLines'] ?? 0);
     419            }
     420
     421            // --- Sum of product amounts for proportional discount distribution ---
     422            $products_total_ttc = 0.0;
     423            foreach ($product_lines as $pl) {
     424                $products_total_ttc += max(0.0, (float) ($pl['amountLines'] ?? 0));
     425            }
     426
     427            // --- Product lines (discount absorbed as subtotal/total difference) ---
     428            foreach ($product_lines as $line) {
    388429                $product_id = $this->get_wc_product_id_from_line($line);
    389430                $line_ttc   = (float) ($line['amountLines'] ?? 0);
    390431
     432                // Proportional share of the global discount for this line
     433                $discount_share_ttc = 0.0;
     434                if ($products_total_ttc > 0 && $total_discount_ttc < 0) {
     435                    $discount_share_ttc = $total_discount_ttc * ($line_ttc / $products_total_ttc);
     436                }
     437
    391438                $product   = $product_id ? wc_get_product($product_id) : null;
    392439                $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'];
     440
     441                // subtotal = original gross price (before discount)
     442                $sub_amounts = $this->split_ttc_amount($line_ttc, $billing_country, $tax_class);
     443                // total = net price after discount applied
     444                $net_amounts = $this->split_ttc_amount(max(0.0, $line_ttc + $discount_share_ttc), $billing_country, $tax_class);
     445
     446                $total_lines_ht += $net_amounts['ht'];
    395447
    396448                $item = new WC_Order_Item_Product();
    397449                $item->set_name(sanitize_text_field($line['title'] ?? __('Produit FlowDino', 'flowdino')));
    398450                $item->set_quantity(1);
    399                 $item->set_subtotal($amounts['ht']);
    400                 $item->set_total($amounts['ht']);
     451                $item->set_subtotal($sub_amounts['ht']); // original price → shows discount in WC
     452                $item->set_total($net_amounts['ht']);    // price after discount
    401453
    402454                if ($product_id) {
     
    404456                }
    405457
     458                if (!empty($sub_amounts['rates'])) {
     459                    $subtotal_taxes = WC_Tax::calc_tax($sub_amounts['ht'], $sub_amounts['rates'], false);
     460                    $total_taxes    = WC_Tax::calc_tax($net_amounts['ht'], $sub_amounts['rates'], false);
     461                    $item->set_taxes(array('total' => $total_taxes, 'subtotal' => $subtotal_taxes));
     462                }
     463
     464                $order->add_item($item);
     465            }
     466
     467            // --- Shipping lines ---
     468            $shipping_total_ht  = 0.0;
     469            $shipping_total_tax = 0.0;
     470            foreach ($shipping_lines as $line) {
     471                $shipping_ttc = (float) ($line['amountLines'] ?? 0);
     472                $amounts      = $this->split_ttc_amount($shipping_ttc, $billing_country, '');
     473
     474                $shipping_total_ht  += $amounts['ht'];
     475                $shipping_total_tax += $amounts['tax'];
     476                $total_lines_ht     += $amounts['ht'];
     477
     478                $shipping_item = new WC_Order_Item_Shipping();
     479                $shipping_item->set_name(sanitize_text_field(
     480                    $order_data['shippingMethod'] ?? $line['title'] ?? __('Expédition', 'flowdino')
     481                ));
     482                $shipping_item->set_total($amounts['ht']);
     483
    406484                if (!empty($amounts['rates']) && $amounts['tax'] > 0.0) {
    407485                    $taxes = WC_Tax::calc_tax($amounts['ht'], $amounts['rates'], false);
    408                     $item->set_taxes(array('total' => $taxes, 'subtotal' => $taxes));
     486                    $shipping_item->set_taxes(array('total' => $taxes));
    409487                }
    410488
    411                 $order->add_item($item);
    412             }
     489                $order->add_item($shipping_item);
     490            }
     491
     492            // Set shipping total so WooCommerce order summary reflects the correct amount
     493            if ($shipping_total_ht > 0.0) {
     494                $order->set_shipping_total($shipping_total_ht);
     495                if (wc_tax_enabled() && $shipping_total_tax > 0.0) {
     496                    $order->set_shipping_tax($shipping_total_tax);
     497                }
     498            }
     499
    413500        } else {
    414501            // No lines: compute from amounts
  • flowdino/trunk/readme.txt

    r3496827 r3497705  
    55Tested up to: 6.9
    66Requires PHP: 7.4
    7 Stable tag: 1.1.11
     7Stable tag: 1.1.12
    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.