Skip to content

Commit

Permalink
replaced "working hours" product with "service" type allowing to cust…
Browse files Browse the repository at this point in the history
…omize the unit
  • Loading branch information
ole1986 committed Jun 11, 2024
1 parent 0211f56 commit 1d505e0
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 103 deletions.
Binary file modified lang/wc-invoice-pdf-de_DE.mo
Binary file not shown.
19 changes: 17 additions & 2 deletions lang/wc-invoice-pdf-de_DE.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 7 additions & 7 deletions model/invoice-pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -71,7 +72,6 @@ public function BuildInvoice($invoice, $isOffer = false, $stream = false)

$pdf->addObject($all, 'all');


$pdf->ezSetDy(-60);

$y = $pdf->y;
Expand Down Expand Up @@ -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],
];

Expand Down Expand Up @@ -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<strong>" . __('Period', 'wc-invoice-pdf') . ": " . \strftime('%x', $current->getTimestamp()) ." - ". \strftime('%x', $next->getTimestamp()) . '</strong>';
} 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, ',', ' ');
}
Expand Down
70 changes: 11 additions & 59 deletions wc-invoice-pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<div class="notice notice-success"><p><strong>' . $plugin['Name'] .':</strong> Successfully migrated products from ' . count($post_ids) .' orders</p></div>';
update_option('wc-invoice-pdf-version', $migrationIncrement);
echo '<div class="notice notice-success"><p><strong>' . $plugin['Name'] .':</strong> Successfully migrated service product_type</p></div>';
}

delete_transient('wc-invoice-pdf-migrate');
Expand Down
92 changes: 57 additions & 35 deletions wc/wc_product_service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -35,7 +40,7 @@ public static function jsRegister()
?>
<script type='text/javascript'>
jQuery( document ).ready( function() {
jQuery( '.options_group.pricing' ).addClass( 'show_if_hour' ).show();
jQuery( '.options_group.pricing' ).addClass( 'show_if_service' ).show();
<?php if ($product_object instanceof self) : ?>
jQuery('.general_options').show();
jQuery('.general_options > a').trigger('click');
Expand Down Expand Up @@ -70,15 +75,15 @@ public function add_to_cart_text()

public static function hour_product_data_tab($product_data_tabs)
{
$product_data_tabs['linked_product']['class'][] = 'hide_if_hour';
$product_data_tabs['attribute']['class'][] = 'hide_if_hour';
$product_data_tabs['advanced']['class'][] = 'hide_if_hour';
$product_data_tabs['shipping']['class'][] = 'hide_if_hour';
$product_data_tabs['linked_product']['class'][] = 'hide_if_service';
$product_data_tabs['attribute']['class'][] = 'hide_if_service';
$product_data_tabs['advanced']['class'][] = 'hide_if_service';
$product_data_tabs['shipping']['class'][] = 'hide_if_service';

$product_data_tabs['hour_tab'] = array(
'label' => __('Working hours', 'wc-invoice-pdf'),
'target' => 'hour_data_tab',
'class' => 'show_if_hour'
'label' => __('Unit information', 'wc-invoice-pdf'),
'target' => 'service_data_tab',
'class' => 'show_if_service'
);

return $product_data_tabs;
Expand All @@ -87,8 +92,19 @@ public static function hour_product_data_tab($product_data_tabs)

public static function product_data_fields()
{
echo '<div id="hour_data_tab" class="panel woocommerce_options_panel">';
woocommerce_wp_checkbox(['id' => '_hour_useminute', 'label' => __('minutes', 'wc-invoice-pdf'), 'description' => __("To the minute calculation", 'wc-invoice-pdf')]);
echo '<div id="service_data_tab" class="panel woocommerce_options_panel">';
woocommerce_wp_text_input([
'id' => '_qty_suffix',
'label' => __('Unit', 'wc-invoice-pdf'),
'description' => __("The unit next to the quantity (default: h)", 'wc-invoice-pdf'),
'style' => 'width: 100px'
]);
woocommerce_wp_text_input([
'id' => '_qty_suffix_plural',
'label' => __('Unit (plural)', 'wc-invoice-pdf'),
'description' => __("The unit when quantity is more than one (optional)", 'wc-invoice-pdf'),
'style' => 'width: 100px'
]);
echo '</div>';
}

Expand All @@ -97,33 +113,39 @@ public static function product_data_fields()
*/
public static function metadata_save($post_id)
{
update_post_meta($post_id, '_hour_useminute', $_POST['_hour_useminute']);
}
$suffix = sanitize_title($_POST['_qty_suffix']);
$suffix_plural = sanitize_title($_POST['_qty_suffix_plural']);

public function get_price_suffix($price = '', $qty = 1, $shorten = false)
{
$plural = $qty > 1 ? 's' : '';
if (!empty($suffix)) {
update_post_meta($post_id, '_qty_suffix', $suffix);
} else {
delete_post_meta($post_id, '_qty_suffix');
}

$suffix = __('Hour' . $plural, 'wc-invoice-pdf');
if (!empty($suffix_plural)) {
update_post_meta($post_id, '_qty_suffix_plural', $suffix_plural);
} else {
delete_post_meta($post_id, '_qty_suffix_plural');
}
}

if ($shorten) {
public function get_price_suffix($price = '', $qty = 1)
{
$suffix = $this->get_meta('_qty_suffix', true);
if (empty($suffix)) {
$suffix = 'h';
}


if ($this->get_meta('_hour_useminute', true)) {
$suffix = __('minute' . $plural, 'wc-invoice-pdf');
if ($shorten) {
$suffix = 'min';
}
$suffix_plural = $this->get_meta('_qty_suffix_plural', true);
if (empty($suffix_plural)) {
$suffix_plural = $suffix;
}

return ' ' . $suffix;
return ' ' . ($qty > 1 ? $suffix_plural : $suffix);
}

public function get_price_html($price = '')
{
$price = wc_price(wc_get_price_to_display($this, array( 'price' => $this->get_regular_price() ))) . ' ' . __('per', 'wc-invoice-pdf') . $this->get_price_suffix('', 1);
return apply_filters('woocommerce_get_price_html', $price, $this);
}
}
}

0 comments on commit 1d505e0

Please sign in to comment.