Skip to content

Commit

Permalink
fixed invoice creation and added custom action
Browse files Browse the repository at this point in the history
  • Loading branch information
BeycanDeveloper committed Oct 23, 2024
1 parent 21ec6a3 commit c2880da
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 54 deletions.
56 changes: 32 additions & 24 deletions app/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> $actions
* @param object $order
* @return array<mixed>
*/
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);
}

/**
Expand All @@ -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 '<div class="order_data_column">';
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
},
"require": {
"beycanpress/freshbooks": "^0.1.6",
"beycanpress/freshbooks": "^0.1.7",
"beycanpress/csf": "2.4.0"
}
}
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion vendor/beycanpress/freshbooks/composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
12 changes: 6 additions & 6 deletions vendor/beycanpress/freshbooks/src/Model/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 7 additions & 7 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
Expand Down
10 changes: 5 additions & 5 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => '__root__',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'edebdfe3748ddc56310991c56db7e479ddfc64cc',
'reference' => '21ec6a38d582db80615c679200142f490e0de920',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -13,7 +13,7 @@
'__root__' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'edebdfe3748ddc56310991c56db7e479ddfc64cc',
'reference' => '21ec6a38d582db80615c679200142f490e0de920',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion woocommerce-freshbooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c2880da

Please sign in to comment.