From 1d505e03c1be8cf790f85431c339cf88ab31e146 Mon Sep 17 00:00:00 2001 From: ole1986 Date: Tue, 11 Jun 2024 21:05:40 +0200 Subject: [PATCH] replaced "working hours" product with "service" type allowing to customize the unit --- lang/wc-invoice-pdf-de_DE.mo | Bin 7233 -> 7618 bytes lang/wc-invoice-pdf-de_DE.po | 19 +++++++- model/invoice-pdf.php | 14 +++--- wc-invoice-pdf.php | 70 +++++--------------------- wc/wc_product_service.php | 92 ++++++++++++++++++++++------------- 5 files changed, 92 insertions(+), 103 deletions(-) diff --git a/lang/wc-invoice-pdf-de_DE.mo b/lang/wc-invoice-pdf-de_DE.mo index b00b46474659a20d5a860c75a8afa4971988cd25..7909df123279eb92700d5ed89b48009285dc7bed 100644 GIT binary patch delta 2614 zcmYk;S!`5Q9LMofq(D`mRKY@pUT8}fO9iAXg@V|Y77J1+TaoRybKAKv+$l46S|Q3n zBqWe%lv-+IA{Y_~ibAYW;_@K);PQeY8a3`dpe9P<@?gN|_cwD#Ihp_c-0ht6Kg(_U z?)-}jlV9fz?>CfA;%;KaEyf(iM@Mj=oET-yID7%;;t*Eh&$tmM<`{E3#*nHeiHq@Z zT!$A?P37NeOaT@mRn0=Yjn_>*Ck@Q5~MfX?Pwrk}Ft@U!WTP2Q`%= zsiZ`Tumnpn4_Bf(+KyVfDBgt!kg8@7mFOu<>c&f)Xl*Z`Msh89;W{cIwWbDhIH48mSQh1z~f_>|1F$+&W&=M#|EfE8yoQ{Jc^%T z1@2@XXX7!Ph##Pq;0M%*enN6KzoPcW4diD=lC=`dM-8YF>C#k>WBxUgdTuE5W>kZ% z!F3zzxs4i07&Vdzs-YfKM@iIsNATq=W1dDOxP)GpVjIrE=g`H=sF|)w-kHvP88&mH z8Jlnb)!?@{6|bTy!Vc7?NuWmj1ghZy)J#2(nt@Yj;W^}IE^|=*t2h&XL%J|UG^QC`fT~}M zOhVE$1UK4H6>U_*5!470sEiMxcK0)=890qJVBSC__z_OQPf+#$K<%Z!gX_wCVHGh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ff|1Z&aWCN$O@ zLajc9V;(v_LYb!9r*tX^{gLP~QtKo>Ar=x7iDp7?#RY^Gg9wMhgyhG`ES`#W)x-Fqo>u%H_)17=5sB&r@gjTp(6VXnz5L7!k zodNuSm_e*fXRQ^gruqMzSVk-*{@+%!p(jCYWs2m>N3YTq#K-YvS!2TQxxXwzEn_Xo zHN^XbnkIIxI<#`)Gh!;Cf1FvwY(j4y9uJ#GXsNRBk}vT2sOC@nm>teY znZ8govOXLyE|}n5xh2{i>DlPpwB_q)Z(@4En(V}((mS5`gYi|_ec{btM#GWbc(8nu Uw=udd9EtVz@fGfgcb2dJ4+@#QF#rGn diff --git a/lang/wc-invoice-pdf-de_DE.po b/lang/wc-invoice-pdf-de_DE.po index d29637d..154aa77 100644 --- a/lang/wc-invoice-pdf-de_DE.po +++ b/lang/wc-invoice-pdf-de_DE.po @@ -270,8 +270,23 @@ msgstr "Einstellungen gespeichert" msgid "To the minute calculation" msgstr "Minuten-genaue Berechnung" -msgid "Working hours" -msgstr "Arbeitszeiten" +msgid "Services" +msgstr "Dienstleistungen" + +msgid "Unit information" +msgstr "Angaben zur Einheit" + +msgid "Unit" +msgstr "Einheit" + +msgid "Unit (plural)" +msgstr "Einheit (plural)" + +msgid "The unit next to the quantity (default: h)" +msgstr "Die Einhait nach Angabe der Menge (Standardwert: h)" + +msgid "The unit when quantity is more than one (optional)" +msgstr "Die Einheit wenn die Menge größer als eins (optional)" msgid "Webspace" msgstr "Webspeicher" diff --git a/model/invoice-pdf.php b/model/invoice-pdf.php index c906929..87c854c 100644 --- a/model/invoice-pdf.php +++ b/model/invoice-pdf.php @@ -25,9 +25,10 @@ public function BuildInvoice($invoice, $isOffer = false, $stream = false) $isB2C = true; } - $formatStyle = \NumberFormatter::DECIMAL; + $formatStyle = \NumberFormatter::CURRENCY; $formatter = new \NumberFormatter(get_locale(), $formatStyle); - $formatter->setPattern("#0.00 " . $order->get_currency()); + $formatter->setSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL, $order->get_currency()); + $formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $order->get_currency()); $items = $order->get_items(); @@ -71,7 +72,6 @@ public function BuildInvoice($invoice, $isOffer = false, $stream = false) $pdf->addObject($all, 'all'); - $pdf->ezSetDy(-60); $y = $pdf->y; @@ -104,8 +104,8 @@ public function BuildInvoice($invoice, $isOffer = false, $stream = false) $colOptions = [ 'num' => ['width' => 30], 'desc' => [], - 'qty' => ['justification' => 'right', 'width' => 62], - 'price' => ['justification' => 'right', 'width' => 70], + 'qty' => ['justification' => 'right', 'width' => 55], + 'price' => ['justification' => 'right', 'width' => 80], 'total' => ['justification' => 'right', 'width' => 80], ]; @@ -144,10 +144,10 @@ public function BuildInvoice($invoice, $isOffer = false, $stream = false) } $qtyStr = number_format($v['qty'], 0, ',', ' ') . ' ' . $product->get_price_suffix('', $v['qty']); $product_name .= "\n" . __('Period', 'wc-invoice-pdf') . ": " . \strftime('%x', $current->getTimestamp()) ." - ". \strftime('%x', $next->getTimestamp()) . ''; - } elseif ($product instanceof \WC_Product_Hour) { + } elseif ($product instanceof \WC_Product_Service) { // check if product type is "hour" to output hours instead of Qty $qtyStr = number_format($v['qty'], 1, ',', ' '); - $qtyStr.= '' . $product->get_price_suffix('', $v['qty'], true); + $qtyStr.= '' . $product->get_price_suffix('', $v['qty']); } else { $qtyStr = number_format($v['qty'], 2, ',', ' '); } diff --git a/wc-invoice-pdf.php b/wc-invoice-pdf.php index 7a8c3d8..2e5c45b 100644 --- a/wc-invoice-pdf.php +++ b/wc-invoice-pdf.php @@ -273,68 +273,20 @@ public static function migrate() if (!get_transient('wc-invoice-pdf-migrate')) { return; } + + // increase when something to migration + $migrationIncrement = 4; + $plugin = get_plugin_data(__FILE__); // migration - $version = get_option('_ispconfig_invoice_version', 0); - - if ($version > 0 && $version <= 2) { - // MIGRATION FROM VERSIONS LOWER 1.5.0 - - // fetch all orders containing a "Domain" meta data - $post_ids = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'Domain'"); - // get all products with product type webspace - $webspace_ids = wc_get_products(['limit' => -1, 'type' => 'webspace', 'return' => 'ids']); - - // go through all matching orders with Domain meta data - foreach ($post_ids as $order_id) { - $order = wc_get_order($order_id); - $items = $order->get_items('line_item'); - - // fetch the first webspace product (asuming ithas only one) - $webspace_product = array_pop(array_filter($items, function ($item) { - return get_class($item) === 'WC_Order_Item_Product'; - })); - - if (!$webspace_product) { - // skip when no webspace product found - continue; - } - - $domain = $order->get_meta('Domain', true); - $metadata = $webspace_product->get_meta_data(); - - if (!empty($metadata)) { - usort($metadata, function ($a, $b) { - return $a->id <= $b->id ? -1 : 1; - }); - - $newmetadata = []; - // remove all meta data - foreach ($metadata as $meta) { - $newmetadata[] = ["id" => 0, "key" => $meta->key, "value" => $meta->value]; - $webspace_product->delete_meta_data($meta->key); - } - - // put the domain meta at first - array_unshift($newmetadata, ["id" => 0, "key" => "Domain", "value" => $domain]); - - $webspace_product->set_meta_data($newmetadata); - } else { - $webspace_product->add_meta_data('Domain', $domain); - } - - $webspace_product->save_meta_data(); - - $order->delete_meta_data('Domain'); - $order->save_meta_data(); - } - - $plugin = get_plugin_data(__FILE__); + $version = intval(get_option('wc-invoice-pdf-version', $migrationIncrement)); + + if ($version < $migrationIncrement) { + $wpdb->query("UPDATE $wpdb->terms SET name = 'service', slug = 'service' WHERE term_id IN (SELECT t.term_id FROM $wpdb->terms t LEFT JOIN $wpdb->term_taxonomy tt ON (t.term_id = tt.term_id) WHERE name = 'hour')"); + $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_qty_suffix', meta_value = 'min' WHERE meta_id IN (SELECT meta_id FROM $wpdb->postmeta pm LEFT JOIN $wpdb->posts p ON p.ID = pm.post_id WHERE p.post_type = 'product' AND pm.meta_key = '_hour_useminute' AND pm.meta_value = 'yes')"); - delete_option('_ispconfig_invoice_version'); - update_option('wc-invoice-pdf-version', 3); - - echo '

' . $plugin['Name'] .': Successfully migrated products from ' . count($post_ids) .' orders

'; + update_option('wc-invoice-pdf-version', $migrationIncrement); + echo '

' . $plugin['Name'] .': Successfully migrated service product_type

'; } delete_transient('wc-invoice-pdf-migrate'); diff --git a/wc/wc_product_service.php b/wc/wc_product_service.php index ae71d0d..464e795 100644 --- a/wc/wc_product_service.php +++ b/wc/wc_product_service.php @@ -4,28 +4,33 @@ return; } -add_filter('woocommerce_product_data_tabs', ['WC_Product_Hour','hour_product_data_tab']); +add_filter('woocommerce_product_data_tabs', ['WC_Product_Service','hour_product_data_tab']); -add_action('woocommerce_product_data_panels', ['WC_Product_Hour','product_data_fields']); -add_action('woocommerce_process_product_meta_hour', ['WC_Product_Hour', 'metadata_save']); +add_action('woocommerce_product_data_panels', ['WC_Product_Service','product_data_fields']); +add_action('woocommerce_process_product_meta_service', ['WC_Product_Service', 'metadata_save']); -add_action('admin_footer', ['WC_Product_Hour', 'jsRegister']); -add_filter('product_type_selector', ['WC_Product_Hour','register']); +add_action('admin_footer', ['WC_Product_Service', 'jsRegister']); +add_filter('product_type_selector', ['WC_Product_Service','register']); -class WC_Product_Hour extends WC_Product +class WC_Product_Service extends WC_Product { public static $current; public function __construct($product) { - $this->product_type = 'hour'; + $this->product_type = 'service'; parent::__construct($product); } + public function get_type() + { + return 'service'; + } + public static function register($types) { // Key should be exactly the same as in the class product_type parameter - $types[ 'hour' ] = __('Working hours', 'wc-invoice-pdf'); + $types[ 'service' ] = __('Services', 'wc-invoice-pdf'); return $types; } @@ -35,7 +40,7 @@ public static function jsRegister() ?>