Skip to content

Commit

Permalink
Initiate customer defaults on creation (#157)
Browse files Browse the repository at this point in the history
* Adding ledger code sIupport for customers
* Adding functions for creating customers in DK from order billing information
* Creating the customer for an order in DK using separate HTTP calls )This is because DK can't take in payment terms, payment methods and ledger codes in the same call as the order.)
  • Loading branch information
aldavigdis authored Jul 4, 2024
1 parent 8fc9932 commit 37d2ff6
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 30 deletions.
2 changes: 2 additions & 0 deletions js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class NineteenEightyWoo {
make_invoice_if_kennitala_is_missing: Boolean( formData.get( 'make_invoice_if_kennitala_is_missing' ) ),
email_invoice: Boolean( formData.get( 'email_invoice' ) ),
make_credit_invoice: Boolean( formData.get( 'make_credit_invoice' ) ),
domestic_customer_ledger_code: formData.get( 'domestic_customer_ledger_code' ),
international_customer_ledger_code: formData.get( 'international_customer_ledger_code' ),
fetch_products: true
}

Expand Down
69 changes: 69 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Config {
const DEFAULT_LEDGER_CODE_REDUCED_SALE = 's003';
const DEFAULT_LEDGER_CODE_REDUCED_PURCHASE = '';

const DEFAULT_LEDGER_CODE_DOMESTIC_CUSTOMERS = '0001';
const DEFAULT_LEDGER_CODE_INTERNATIONAL_CUSTOMERS = '0002';

/**
* Get the DK API key
*
Expand Down Expand Up @@ -652,4 +655,70 @@ public static function get_make_credit_invoice(): bool {
public static function set_make_credit_invoice( bool $value ): bool {
return update_option( 'make_credit_invoice', (int) $value );
}

/**
* Get the ledger code for domestic customers
*
* This is the ledger code that is used of the customer's country is the
* same as the shop's country.
*
* @return string The ledger code.
*/
public static function get_domestic_customer_ledger_code(): string {
return (string) (
get_option(
'1984_woo_dk_domestic_customer_ledger_code',
self::DEFAULT_LEDGER_CODE_DOMESTIC_CUSTOMERS
)
);
}

/**
* Set the ledger code for domestic customers
*
* @param string $value The ledger code to set.
*
* @return bool True on success, false on failure.
*/
public static function set_domestic_customer_ledger_code(
string $value
): bool {
return update_option(
'1984_woo_dk_domestic_customer_ledger_code',
(string) $value
);
}

/**
* Get the ledger code for international customers
*
* This is the ledger code that is used of the customer's country is not the
* same as the shop's country.
*
* @return string The ledger code.
*/
public static function get_international_customer_ledger_code(): string {
return (string) (
get_option(
'1984_woo_dk_international_customer_ledger_code',
self::DEFAULT_LEDGER_CODE_INTERNATIONAL_CUSTOMERS
)
);
}

/**
* Set the ledger code for international customers
*
* @param string $value The ledger code to set.
*
* @return bool True on success, false on failure.
*/
public static function set_international_customer_ledger_code(
string $value
): bool {
return update_option(
'1984_woo_dk_international_customer_ledger_code',
(string) $value
);
}
}
78 changes: 77 additions & 1 deletion src/Export/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use NineteenEightyFour\NineteenEightyWoo\Service\DKApiRequest;
use NineteenEightyFour\NineteenEightyWoo\Config;

use NineteenEightyFour\NineteenEightyWoo\Helpers\Order as OrderHelper;
use stdClass;
use WC_Customer;
use WC_Order;
use WP_Error;

/**
Expand Down Expand Up @@ -49,6 +51,42 @@ public static function create_in_dk( WC_Customer $customer ): bool|WP_Error {
return true;
}

/**
* Create a customer record in DK representing a WooCommerce order record
*
* This facilitates "guests", as instead of using a WC_Customer object
* directly, this takes the customer information for the order itself.
*
* @param WC_Order $wc_order The WooCommerce order.
*
* @return bool|WP_Error True on success, false if connection was
* established but the request was rejected, WC_Error
* if there was a connection error.
*/
public static function create_in_dk_from_order(
WC_Order $wc_order
): bool|WP_Error {
$api_request = new DKApiRequest();
$request_body = wp_json_encode(
self::to_dk_customer_body_from_order( $wc_order )
);

$result = $api_request->request_result(
self::API_PATH,
$request_body,
);

if ( $result instanceof WP_Error ) {
return $result;
}

if ( 200 !== $result->response_code ) {
return false;
}

return true;
}

/**
* Update the DK customer record for a WooCommerce costumer
*
Expand Down Expand Up @@ -203,4 +241,42 @@ public static function id_to_dk_customer_body( int $customer_id ): array {
$customer = new WC_Customer( $customer_id );
return self::to_dk_customer_body( $customer );
}

/**
* Generate a customer request body for DK from order information
*
* @param WC_Order $wc_order The WooCommerce order.
*
* @return stdClass An object representing the customer information for the order.
*/
public static function to_dk_customer_body_from_order(
WC_Order $wc_order
): stdClass {
$payment_mapping = Config::get_payment_mapping(
$wc_order->get_payment_method()
);

$store_location = wc_get_base_location();
if ( $wc_order->get_billing_country() === $store_location['country'] ) {
$ledger_code = Config::get_domestic_customer_ledger_code();
} else {
$ledger_code = Config::get_international_customer_ledger_code();
}

return (object) array(
'Number' => OrderHelper::get_kennitala( $wc_order ),
'Name' => $wc_order->get_formatted_billing_full_name(),
'Address1' => $wc_order->get_billing_address_1(),
'Address2' => $wc_order->get_billing_address_2(),
'Country' => $wc_order->get_billing_country(),
'City' => $wc_order->get_billing_city(),
'ZipCode' => $wc_order->get_billing_postcode(),
'Phone' => $wc_order->get_billing_phone(),
'Email' => $wc_order->get_billing_email(),
'SalesPerson' => Config::get_default_sales_person_number(),
'PaymentMode' => $payment_mapping->dk_mode,
'PaymentTerm' => $payment_mapping->dk_term,
'LedgerCode' => $ledger_code,
);
}
}
7 changes: 7 additions & 0 deletions src/Export/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace NineteenEightyFour\NineteenEightyWoo\Export;

use Automattic\WooCommerce\Admin\API\Customers;
use NineteenEightyFour\NineteenEightyWoo\Config;
use NineteenEightyFour\NineteenEightyWoo\Export\Order as ExportOrder;
use NineteenEightyFour\NineteenEightyWoo\Export\Customer as ExportCustomer;
use NineteenEightyFour\NineteenEightyWoo\Helpers\Order as OrderHelper;
use NineteenEightyFour\NineteenEightyWoo\Service\DKApiRequest;
use WC_Order;
use WP_Error;
Expand Down Expand Up @@ -33,6 +36,10 @@ class Invoice {
public static function create_in_dk(
WC_Order $wc_order
): string|false|WP_Error {
if ( ! ExportCustomer::is_in_dk( OrderHelper::get_kennitala( $wc_order ) ) ) {
Customer::create_in_dk_from_order( $wc_order );
}

$invoice_number = self::get_dk_invoice_number( $wc_order );

if ( false === empty( $invoice_number ) ) {
Expand Down
23 changes: 5 additions & 18 deletions src/Export/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace NineteenEightyFour\NineteenEightyWoo\Export;

use Automattic\WooCommerce\Admin\API\Customers;
use NineteenEightyFour\NineteenEightyWoo\Brick\Math\BigDecimal;
use NineteenEightyFour\NineteenEightyWoo\Service\DKApiRequest;
use NineteenEightyFour\NineteenEightyWoo\Config;
Expand Down Expand Up @@ -125,24 +126,14 @@ public static function get_dk_order_number(
* @param WC_Order $wc_order The WooCommerce order object.
*/
public static function to_dk_order_body( WC_Order $wc_order ): array {
$kennitala = OrderHelper::get_kennitala( $wc_order );

$order_props = array();
$customer_array = array();
$recipient_array = array();
$customer_array = array( 'Number' => $kennitala );

$order_props['Reference'] = 'WC-' . $wc_order->get_id();

$customer_array['Number'] = OrderHelper::get_kennitala( $wc_order );

if ( ! ExportCustomer::is_in_dk( OrderHelper::get_kennitala( $wc_order ) ) ) {
$customer_array['Name'] = $wc_order->get_formatted_billing_full_name();
$customer_array['Address1'] = $wc_order->get_billing_address_1();
$customer_array['Address2'] = $wc_order->get_billing_address_2();
$customer_array['City'] = $wc_order->get_billing_city();
$customer_array['ZipCode'] = $wc_order->get_billing_postcode();
$customer_array['Phone'] = $wc_order->get_billing_phone();
$customer_array['Email'] = $wc_order->get_billing_email();
}

$recipient_array['Name'] = $wc_order->get_formatted_billing_full_name();
$recipient_array['Address1'] = $wc_order->get_shipping_address_1();
$recipient_array['Address2'] = $wc_order->get_shipping_address_2();
Expand All @@ -152,10 +143,6 @@ public static function to_dk_order_body( WC_Order $wc_order ): array {

$store_location = wc_get_base_location();

if ( $wc_order->get_billing_country() !== $store_location['country'] ) {
$customer_array['Country'] = $wc_order->get_billing_country();
}

if ( $wc_order->get_shipping_country() !== $store_location['country'] ) {
$recipient_array['Country'] = $wc_order->get_shipping_country();
}
Expand Down Expand Up @@ -242,7 +229,7 @@ public static function to_dk_order_body( WC_Order $wc_order ): array {
$total_amount = $total->minus( $total_tax );

$order_props['TotalAmount'] = $total_amount->toFloat();
$order_props['TotalAmountWithTax'] = $total_tax->toFloat();
$order_props['TotalAmountWithTax'] = $total->toFloat();

return $order_props;
}
Expand Down
19 changes: 13 additions & 6 deletions src/Rest/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,19 @@ public static function rest_api_callback(
);
}

if (
true === property_exists(
$rest_json,
'enable_kennitala_in_block'
)
) {
if ( property_exists( $rest_json, 'domestic_customer_ledger_code' ) ) {
Config::set_domestic_customer_ledger_code(
$rest_json->domestic_customer_ledger_code
);
}

if ( property_exists( $rest_json, 'international_customer_ledger_code' ) ) {
Config::set_international_customer_ledger_code(
$rest_json->international_customer_ledger_code
);
}

if ( property_exists( $rest_json, 'enable_kennitala_in_block' ) ) {
Config::set_kennitala_block_field_enabled(
$rest_json->enable_kennitala_in_block
);
Expand Down
8 changes: 4 additions & 4 deletions style/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ body.woocommerce_page_1984-dk-woo .wrap {
font-size: 1.5rem;
}

#dk-ledger-codes-table {
.dk-ledger-codes-table {
width: auto;
}

#dk-ledger-codes-table td, #dk-ledger-codes-table th[scope=col] {
.dk-ledger-codes-table td, .dk-ledger-codes-table th[scope=col] {
width: 6em;
}

#dk-ledger-codes-table input {
.dk-ledger-codes-table input {
width: 4em;
}

Expand Down Expand Up @@ -200,7 +200,7 @@ body.woocommerce_page_1984-dk-woo .wrap {
display: block;
}

#dk-ledger-codes-table td, #dk-ledger-codes-table th[scope=col] {
.dk-ledger-codes-table td, .dk-ledger-codes-table th[scope=col] {
width: auto;
}
}
39 changes: 38 additions & 1 deletion views/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class="regular-text api-key-input"
);
?>
</p>
<table id="dk-ledger-codes-table" class="form-table">
<table id="dk-ledger-codes-table" class="form-table dk-ledger-codes-table">
<thead>
<tr>
<th></th>
Expand Down Expand Up @@ -513,7 +513,44 @@ class="regular-text api-key-input"
</p>
</td>
</tr>
</tbody>
</table>
</section>

<section class="section">
<h2><?php esc_html_e( 'Customers', '1984-dk-woo' ); ?></h2>
<table id="customers-table" class="form-table dk-ledger-codes-table">
<tbody>
<tr>
<th scope="row" class="column-title column-primary">
<label for="domestic_customer_ledger_code_field">
<?php esc_html_e( 'Ledger Code for Domestic Customers', '1984-dk-woo' ); ?>
</label>
</th>
<td>
<input
id="domestic_customer_ledger_code_field"
name="domestic_customer_ledger_code"
type="text"
value="<?php echo esc_attr( Config::get_domestic_customer_ledger_code() ); ?>"
/>
</td>
</tr>
<tr>
<th scope="row" class="column-title column-primary">
<label for="international_customer_ledger_code_field">
<?php esc_html_e( 'Ledger Code for International Customers', '1984-dk-woo' ); ?>
</label>
</th>
<td>
<input
id="international_customer_ledger_code_field"
name="international_customer_ledger_code"
type="text"
value="<?php echo esc_attr( Config::get_international_customer_ledger_code() ); ?>"
/>
</td>
</tr>
</tbody>
</table>
</section>
Expand Down

0 comments on commit 37d2ff6

Please sign in to comment.