diff --git a/app/WooCommerce.php b/app/WooCommerce.php index 58544d2..a9efd19 100644 --- a/app/WooCommerce.php +++ b/app/WooCommerce.php @@ -17,33 +17,44 @@ class WooCommerce */ private ?Invoice $invoice = null; - /** - * @var string - */ - private string $metaKey = 'freshbooks_invoice_id'; - /** * constructor */ public function __construct() { - $this->dailyInvoiceCreatedOrderIdsReset(); + add_filter('woocommerce_order_actions', [$this, 'addOrderActions'], 10, 2); add_action('woocommerce_order_status_completed', [$this, 'invoiceProcess']); add_action('woocommerce_admin_order_data_after_order_details', [$this, 'backend'], 10); + add_action('woocommerce_process_shop_order_meta', [$this, 'executeInvoiceAction'], 50); } /** - * @return void + * @param array $actions + * @param object $order + * @return array */ - public function dailyInvoiceCreatedOrderIdsReset(): void + public function addOrderActions(array $actions, object $order): array { - $today = gmdate('Y-m-d'); - $lastReset = get_option('wcfb_invoice_created_order_ids_reset', ''); + $invoiceId = get_post_meta($order->get_id(), 'wcfb_invoice_id', true); + + if (!$invoiceId) { + $actions['create-invoice'] = esc_html__('Create FreshBooks Invoice', 'woocommerce-freshbooks'); + } + + return $actions; + } - if ($today != $lastReset) { - update_option('wcfb_invoice_created_order_ids', []); - update_option('wcfb_invoice_created_order_ids_reset', $today); + /** + * @param int $orderId + * @return void + */ + public function executeInvoiceAction(int $orderId): void + { + if ('create-invoice' !== filter_input(INPUT_POST, 'wc_order_action')) { + return; } + + $this->invoiceProcess($orderId); } /** @@ -52,9 +63,9 @@ public function dailyInvoiceCreatedOrderIdsReset(): void */ public function backend(object $order): void { - $invoiceId = get_post_meta($order->get_id(), $this->metaKey, true); - $invoiceNumber = get_post_meta($order->get_id(), 'freshbooks_invoice_number', true); - $accountId = get_post_meta($order->get_id(), 'freshbooks_invoice_account_id', true); + $invoiceId = get_post_meta($order->get_id(), 'wcfb_invoice_id', true); + $invoiceNumber = get_post_meta($order->get_id(), 'wcfb_invoice_number', true); + $accountId = get_post_meta($order->get_id(), 'wcfb_invoice_account_id', true); if ($invoiceId) { echo '
'; @@ -89,8 +100,7 @@ public function invoiceProcess(int $orderId): void return; } - $invoiceCreatedOrderIds = get_option('wcfb_invoice_created_order_ids', []); - if (in_array($orderId, $invoiceCreatedOrderIds)) { + if (get_post_meta($orderId, 'wcfb_invoice_id', true)) { return; } @@ -166,11 +176,9 @@ public function invoiceProcess(int $orderId): void } $this->invoice->create(); - $invoiceCreatedOrderIds[] = $orderId; - update_option('wcfb_invoice_created_order_ids', $invoiceCreatedOrderIds); - update_post_meta($orderId, $this->metaKey, $this->invoice->getId()); - update_post_meta($orderId, 'freshbooks_invoice_number', $this->invoice->getInvoiceNumber()); - update_post_meta($orderId, 'freshbooks_invoice_account_id', $this->invoice->getAccountId()); + update_post_meta($orderId, 'wcfb_invoice_id', $this->invoice->getId()); + update_post_meta($orderId, 'wcfb_invoice_number', $this->invoice->getInvoiceNumber()); + update_post_meta($orderId, 'wcfb_invoice_account_id', $this->invoice->getAccountId()); if ($this->setting('sendToEmail')) { $this->invoice->sendToEMail($email); @@ -202,7 +210,7 @@ public function paymentCompleted(int $orderId): void $order = wc_get_order($orderId); if (!$this->invoice) { - $invoiceId = (int) $order->get_meta('_wcfb_invoice_id'); + $invoiceId = get_post_meta($orderId, 'wcfb_invoice_id', true); $this->invoice = $conn->invoice()->getById($invoiceId); } diff --git a/composer.json b/composer.json index c844dbf..fcb9728 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } }, "require": { - "beycanpress/freshbooks": "^0.1.6", + "beycanpress/freshbooks": "^0.1.7", "beycanpress/csf": "2.4.0" } } diff --git a/composer.lock b/composer.lock index 0fe63fc..dc683f5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "38939e457648ab95dc0ba44272eb0b47", + "content-hash": "9ec7af8e693c2fd6d6b1a813f5af6a44", "packages": [ { "name": "beycanpress/csf", @@ -35,16 +35,16 @@ }, { "name": "beycanpress/freshbooks", - "version": "0.1.6", + "version": "0.1.7", "source": { "type": "git", "url": "https://github.com/BeycanPress/freshbooks.git", - "reference": "9d3d4effffe9c3c7d1bbabb71bddf3c34b07e926" + "reference": "8923f62618a3575f8356df0f7ffaec7538c40df5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/BeycanPress/freshbooks/zipball/9d3d4effffe9c3c7d1bbabb71bddf3c34b07e926", - "reference": "9d3d4effffe9c3c7d1bbabb71bddf3c34b07e926", + "url": "https://api.github.com/repos/BeycanPress/freshbooks/zipball/8923f62618a3575f8356df0f7ffaec7538c40df5", + "reference": "8923f62618a3575f8356df0f7ffaec7538c40df5", "shasum": "" }, "require": { @@ -69,9 +69,9 @@ ], "support": { "issues": "https://github.com/BeycanPress/freshbooks/issues", - "source": "https://github.com/BeycanPress/freshbooks/tree/0.1.6" + "source": "https://github.com/BeycanPress/freshbooks/tree/0.1.7" }, - "time": "2024-10-12T07:27:04+00:00" + "time": "2024-10-23T09:01:00+00:00" } ], "packages-dev": [], diff --git a/readme.txt b/readme.txt index e7d0627..258ad85 100644 --- a/readme.txt +++ b/readme.txt @@ -4,8 +4,8 @@ Tags: woocommerce, automation, freshbooks, invoicing, accounting Requires at least: 5.0 Tested up to: 6.6.2 Requires PHP: 8.1 -Stable Tag: 1.1.5 -Version: 1.1.5 +Stable Tag: 1.1.6 +Version: 1.1.6 License: GPLv3 License URI: https://www.gnu.org/licenses/gpl-3.0.html diff --git a/vendor/beycanpress/freshbooks/composer.json b/vendor/beycanpress/freshbooks/composer.json index 3a547db..5738be4 100644 --- a/vendor/beycanpress/freshbooks/composer.json +++ b/vendor/beycanpress/freshbooks/composer.json @@ -1,6 +1,6 @@ { "name": "beycanpress/freshbooks", - "version": "0.1.6", + "version": "0.1.7", "description": "FreshBooks API SDK.", "keywords": ["freshbooks", "api", "sdk"], "type": "library", diff --git a/vendor/beycanpress/freshbooks/src/Model/Invoice.php b/vendor/beycanpress/freshbooks/src/Model/Invoice.php index e08316f..51f0e73 100644 --- a/vendor/beycanpress/freshbooks/src/Model/Invoice.php +++ b/vendor/beycanpress/freshbooks/src/Model/Invoice.php @@ -257,9 +257,9 @@ class Invoice private ?object $discountTotal = null; /** - * @var float + * @var float|int */ - private float $discountValue = 0; + private float|int $discountValue = 0; /** * @var string @@ -804,10 +804,10 @@ public function setDiscountTotal(object $discountTotal): Invoice } /** - * @param float $discountValue + * @param float|int $discountValue * @return Invoice */ - public function setDiscountValue(float $discountValue): Invoice + public function setDiscountValue(float|int $discountValue): Invoice { $this->discountValue = $discountValue; return $this; @@ -1332,9 +1332,9 @@ public function getDiscountTotal(): ?object } /** - * @return float + * @return float|int */ - public function getDiscountValue(): float + public function getDiscountValue(): float|int { return $this->discountValue; } diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 68afee4..812489d 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -32,23 +32,23 @@ }, { "name": "beycanpress/freshbooks", - "version": "0.1.6", - "version_normalized": "0.1.6.0", + "version": "0.1.7", + "version_normalized": "0.1.7.0", "source": { "type": "git", "url": "https://github.com/BeycanPress/freshbooks.git", - "reference": "9d3d4effffe9c3c7d1bbabb71bddf3c34b07e926" + "reference": "8923f62618a3575f8356df0f7ffaec7538c40df5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/BeycanPress/freshbooks/zipball/9d3d4effffe9c3c7d1bbabb71bddf3c34b07e926", - "reference": "9d3d4effffe9c3c7d1bbabb71bddf3c34b07e926", + "url": "https://api.github.com/repos/BeycanPress/freshbooks/zipball/8923f62618a3575f8356df0f7ffaec7538c40df5", + "reference": "8923f62618a3575f8356df0f7ffaec7538c40df5", "shasum": "" }, "require": { "php": ">=8.1" }, - "time": "2024-10-12T07:27:04+00:00", + "time": "2024-10-23T09:01:00+00:00", "type": "library", "installation-source": "dist", "autoload": { @@ -69,7 +69,7 @@ ], "support": { "issues": "https://github.com/BeycanPress/freshbooks/issues", - "source": "https://github.com/BeycanPress/freshbooks/tree/0.1.6" + "source": "https://github.com/BeycanPress/freshbooks/tree/0.1.7" }, "install-path": "../beycanpress/freshbooks" } diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index 0281d33..9d81cd2 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -3,7 +3,7 @@ 'name' => '__root__', 'pretty_version' => 'dev-master', 'version' => 'dev-master', - 'reference' => 'edebdfe3748ddc56310991c56db7e479ddfc64cc', + 'reference' => '21ec6a38d582db80615c679200142f490e0de920', 'type' => 'library', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), @@ -13,7 +13,7 @@ '__root__' => array( 'pretty_version' => 'dev-master', 'version' => 'dev-master', - 'reference' => 'edebdfe3748ddc56310991c56db7e479ddfc64cc', + 'reference' => '21ec6a38d582db80615c679200142f490e0de920', 'type' => 'library', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), @@ -29,9 +29,9 @@ 'dev_requirement' => false, ), 'beycanpress/freshbooks' => array( - 'pretty_version' => '0.1.6', - 'version' => '0.1.6.0', - 'reference' => '9d3d4effffe9c3c7d1bbabb71bddf3c34b07e926', + 'pretty_version' => '0.1.7', + 'version' => '0.1.7.0', + 'reference' => '8923f62618a3575f8356df0f7ffaec7538c40df5', 'type' => 'library', 'install_path' => __DIR__ . '/../beycanpress/freshbooks', 'aliases' => array(), diff --git a/woocommerce-freshbooks.php b/woocommerce-freshbooks.php index e2f2f78..49294bb 100644 --- a/woocommerce-freshbooks.php +++ b/woocommerce-freshbooks.php @@ -6,7 +6,7 @@ /** * Plugin Name: FreshBooks Integration for WooCommerce - * Version: 1.1.5 + * Version: 1.1.6 * Author URI: https://beycanpress.com/ * Description: FreshBooks Integration for WooCommerce * Author: BeycanPress LLC