Skip to content

Commit

Permalink
[Shopify] Connector - Export Posted Sales Invoices to Shopify as Orde…
Browse files Browse the repository at this point in the history
…rs (#26748)

This pull request does not have a related issue as it's part of the
delivery for development agreed directly
with [@AndreiPanko](https://github.com/AndreiPanko)

Here's a quick summary:

### Created a report to export Posted Sales Invoices to Shopify as
Orders.

Shopify Shop has a **new action "Payment Terms Mapping"** that takes the
user to the **new page "Shopify Payment Terms Mapping"**. Shopify
Payment Terms cannot be manually inserted or deleted, they can only be
refreshed with an action that pulls them from Shopify. By default
**FIXED** Payment Term is selected as primary during the synchronization
of the Payment Terms.
Before Posted Sales Invoices can be synced, a payment term code from
Shopify needs to be mapped to the Business Central one or a primary
Shopify Payment Term needs to be selected with a Boolean "Is Primary" -
this payment term will be used if the Posted Sales Invoice payment terms
code doesn't have a mapping to Shopify. This payment term code will also
be filled during the sync of Shopify orders.
  
A new Scope is required. To update payment terms on a draft order –
write_payment_terms (read_payment_terms scope was removed because it
causes strange behavior - every time a Shopify shop card is opened it
states that the scope has changed and requires authorization.
write_payment_terms overwrites read_payment_terms). To create a draft
order - write_draft_orders.

### To export Posted Sales Invoices, here are some conditions that they
need to meet:

- The **new Boolean field "Posted Invoice Sync"** needs to be enabled.
- The "Shopify Order Id" field on the Posted Sales Invoice needs to be
0.
- Posted Sales Invoice "Bill-To Customer" has been mapped to the Shopify
Customer or Shopify Companies table.
- Posted Sales Invoice "Bill-To Customer" is not a customer used as a
Default Customer in the Shopify Shop Card or Shopify Customer Template.
- There are no lines of type non-comment (or type is populated but No.
is empty).
- There are no fraction quantities.
- There must be a mapped Payment Terms Code or a selected Primary
Payment Terms Code on the new "Shopify Payment Terms Mapping" page.
- If there is at least one line of type "Item" where the selected
item/variant doesn’t have a link to Shopify Product/Shopify variant and
the **new Boolean field "Items must be mapped to Products"** on the
Shopify Shop card is selected - the Posted Sales Invoice will not be
exported.
- Posted Sales Invoice currency code matches Shopify shop currency code
(explanation at the bottom).

If one of the previous conditions is not met, the Posted Sales Invoice
"Shopify Order Id" field will be marked with -2. If the GraphQL request
fails, then the field will be marked with -1. If everything goes
correctly, the field will be filled with the created Shopify Order ID.
Users can modify the "Shopify Order Id" field in the posted document
(allowed changes from -1 or -2 to 0. The functionality works the same as
in the Update Posted Shipment document.)
 
This export can be executed with a new action that is added to Shopify
Shop - "Sync Posted Sales Invoices" or just by running the report "Sync
Invoices to Shopify". The draft order in Shopify is created with the
values from the Posted Sales Invoice order and lines. If the Remaining
Amount of the Posted Sales Invoice is 0, then the order is marked as
paid, otherwise, the status will be "Payment pending" and a mapped
Payment Terms code will be sent out. After that, the created draft order
will be completed and the Shopify order created. All orders are
automatically fulfilled. To achieve “paid” status on the Shopify order –
use the Manual payment function in the Shopify Order card. Once the
report is executed, the Inventory will be synced.

Order sync will import created orders, then check if the order is
present in the **new Shopify Invoice Header table** (this table is
filled with the Shopify order ID of the exported Posted Sales Invoice)
and if so:
- Shopify Order is marked as processed.
- A link is added to the Posted Sales Invoice (related documents).

### A few details about the mapping of the values and some additional
functionality:
- All non-comment type lines are exported as custom products (including
Items if the new Boolean field "Items must be mapped to Products"** on
the Shopify Shop card is not selected).
- Taxes from the lines are sent out as a custom product with the title
consisting of "VAT Calculation Type" + VAT %.
- Invoice discounts from the lines and the header are added up and sent
out as a discount to the whole order with the title "Invoice Discount
Amount"
- Posted Sales Invoice comments are exported as notes.
- Once the draft order is completed, an order confirmation email will be
sent out to the customer. This functionality is from Shopify itself.
- If the product/variant exists in Shopify, the price, title and other
information will be taken from Shopify and not the one in the Posted
Sales Invoice line. This is how the draftOrderCreate mutation works.

### Out of scope functionalities:
- Open orders and open invoices.
- Fulfillment/Tracking Details.
- Refund / partial refund status.
- Weight export.

**Known issues with different currencies (that's why Posted Sales
Invoices with different currencies than Shopify Shop cannot be exported
at the moment):**
- Currently sending out different currencies than the Shopify Shop
currency cannot be done because if the currency of the custom product
differs from the Shopify shop's currency, the amount sent will still be
accepted in the local Shopify shop's currency before being converted to
the order's currency and the amount will be incorrect. For example, if
the VAT amount is 100 GBP, but the Shopify shop's currency is EUR, the
custom product will be exported with an amount of 100 GBP. However, this
amount will be treated as 100 EUR and then converted to GBP according to
the currency exchange rate in Shopify.

Fixes #26819
Fixes
[AB#446399](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/446399)

---------

Co-authored-by: Jesper Schulz-Wedde <[email protected]>
Co-authored-by: Tine Staric <[email protected]>
  • Loading branch information
3 people authored Aug 23, 2024
1 parent 2a768e4 commit 7c3fe3e
Show file tree
Hide file tree
Showing 26 changed files with 1,581 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Apps/W1/Shopify/app/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"idRanges": [
{
"from": 30100,
"to": 30360
"to": 30370
}
],
"internalsVisibleTo": [
Expand Down
43 changes: 43 additions & 0 deletions Apps/W1/Shopify/app/src/Base/Pages/ShpfyShopCard.Page.al
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ page 30101 "Shpfy Shop Card"
ApplicationArea = All;
ToolTip = 'Specifies the status of a product in Shopify via the sync when an item is removed in Shopify or an item is blocked in Business Central.';
}
field("Items Mapped to Products"; Rec."Items Mapped to Products")
{
ApplicationArea = All;
ToolTip = 'Specifies if only the items that are mapped to Shopify products/Shopify variants are synchronized from Posted Sales Invoices to Shopify.';
}
}
group(PriceSynchronization)
{
Expand Down Expand Up @@ -517,6 +522,11 @@ page 30101 "Shpfy Shop Card"
end;
}
#endif
field("Posted Invoice Sync"; Rec."Posted Invoice Sync")
{
ApplicationArea = All;
ToolTip = 'Specifies whether the posted sales invoices can be synchronized to Shopify.';
}
}
group(ReturnsAndRefunds)
{
Expand Down Expand Up @@ -650,6 +660,19 @@ page 30101 "Shpfy Shop Card"
RunPageLink = "Shop Code" = field(Code);
ToolTip = 'Maps the Shopify payment methods to the related payment methods and prioritize them.';
}
action(PaymentTerms)
{
ApplicationArea = All;
Caption = 'Payment Terms Mapping';
Image = SuggestPayment;
Promoted = true;
PromotedCategory = Category4;
PromotedIsBig = true;
PromotedOnly = true;
RunObject = page "Shpfy Payment Terms Mapping";
RunPageLink = "Shop Code" = field(Code);
ToolTip = 'Maps the Shopify payment terms to the related payment terms and prioritize them.';
}
action(Orders)
{
ApplicationArea = All;
Expand Down Expand Up @@ -986,6 +1009,26 @@ page 30101 "Shpfy Shop Card"
Report.Run(Report::"Shpfy Sync Shipm. to Shopify");
end;
}
action(SyncPostedSalesInvoices)
{
ApplicationArea = All;
Ellipsis = true;
Caption = 'Sync Posted Sales Invoices';
Image = Export;
Promoted = true;
PromotedCategory = Category5;
PromotedIsBig = true;
PromotedOnly = true;
ToolTip = 'Synchronize posted sales invoices to Shopify. Synchronization will be performed only if the Posted Invoice Sync field is enabled in the Shopify shop.';

trigger OnAction();
var
ExportInvoicetoShpfy: Report "Shpfy Sync Invoices to Shpfy";
begin
ExportInvoicetoShpfy.SetShop(Rec.Code);
ExportInvoicetoShpfy.Run();
end;
}
action(SyncDisputes)
{
ApplicationArea = All;
Expand Down
8 changes: 8 additions & 0 deletions Apps/W1/Shopify/app/src/Base/Tables/ShpfyShop.Table.al
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,14 @@ table 30102 "Shpfy Shop"
{
DataClassification = SystemMetadata;
}
field(201; "Items Mapped to Products"; Boolean)
{
Caption = 'Items Must be Mapped to Products';
}
field(202; "Posted Invoice Sync"; Boolean)
{
Caption = 'Posted Invoice Sync';
}
}

keys
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Microsoft.Integration.Shopify;

/// <summary>
/// Codeunit Shpfy GQL DraftOrderComplete (ID 30341) implements Interface Shpfy IGraphQL.
/// </summary>
codeunit 30341 "Shpfy GQL DraftOrderComplete" implements "Shpfy IGraphQL"
{
Access = Internal;

/// <summary>
/// GetGraphQL.
/// </summary>
/// <returns>Return value of type Text.</returns>
internal procedure GetGraphQL(): Text
begin
exit('{"query": "mutation {draftOrderComplete(id: \"gid://shopify/DraftOrder/{{DraftOrderId}}\") { draftOrder { order { legacyResourceId, name }} userErrors { field, message }}}"}');
end;

/// <summary>
/// GetExpectedCost.
/// </summary>
/// <returns>Return value of type Integer.</returns>
internal procedure GetExpectedCost(): Integer
begin
exit(11);
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Microsoft.Integration.Shopify;

/// <summary>
/// Codeunit Shpfy GQL Fulfill Order (ID 30355) implements Interface Shpfy IGraphQL.
/// </summary>
codeunit 30355 "Shpfy GQL Fulfill Order" implements "Shpfy IGraphQL"
{
Access = Internal;

/// <summary>
/// GetGraphQL.
/// </summary>
/// <returns>Return value of type Text.</returns>
internal procedure GetGraphQL(): Text
begin
exit('{"query": "mutation { fulfillmentCreateV2 ( fulfillment: { lineItemsByFulfillmentOrder: [{ fulfillmentOrderId: \"gid://shopify/FulfillmentOrder/{{FulfillmentOrderId}}\" }] }) { fulfillment {id, status} userErrors {field, message}}}"}');
end;

/// <summary>
/// GetExpectedCost.
/// </summary>
/// <returns>Return value of type Integer.</returns>
internal procedure GetExpectedCost(): Integer
begin
exit(10);
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// <summary>
/// Codeunit Shpfy GQL Get Fulfillments (ID 30356) implements Interface Shpfy IGraphQL.
/// </summary>
codeunit 30356 "Shpfy GQL Get Fulfillments" implements "Shpfy IGraphQL"
{
Access = Internal;

/// <summary>
/// GetGraphQL.
/// </summary>
/// <returns>Return value of type Text.</returns>
internal procedure GetGraphQL(): Text
begin
exit('{"query": "{order (id: \"gid://shopify/Order/{{OrderId}}\") { fulfillmentOrders ( first: {{NumberOfOrders}}) { nodes { id }}}}"}');
end;

/// <summary>
/// GetExpectedCost.
/// </summary>
/// <returns>Return value of type Integer.</returns>
internal procedure GetExpectedCost(): Integer
begin
exit(6);
end;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

namespace Microsoft.Integration.Shopify;

/// <summary>
/// Codeunit Shpfy GQL Payment Terms (ID 30357) implements Interface Shpfy IGraphQL.
/// </summary>
codeunit 30357 "Shpfy GQL Payment Terms" implements "Shpfy IGraphQL"
{
Access = Internal;

/// <summary>
/// GetGraphQL.
/// </summary>
/// <returns>Return value of type Text.</returns>
internal procedure GetGraphQL(): Text
begin
exit('{"query": "{paymentTermsTemplates{id name paymentTermsType dueInDays description translatedName}}"}');
end;

/// <summary>
/// GetExpectedCost.
/// </summary>
/// <returns>Return value of type Integer.</returns>
internal procedure GetExpectedCost(): Integer
begin
exit(1);
end;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Microsoft.Integration.Shopify;

codeunit 30168 "Shpfy GQL ShopLocales" implements "Shpfy IGraphQL"
codeunit 30358 "Shpfy GQL ShopLocales" implements "Shpfy IGraphQL"
{

internal procedure GetGraphQL(): Text
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Microsoft.Integration.Shopify;

codeunit 30159 "Shpfy GQL TranslationsRegister" implements "Shpfy IGraphQL"
codeunit 30359 "Shpfy GQL TranslationsRegister" implements "Shpfy IGraphQL"
{

internal procedure GetGraphQL(): Text
Expand Down
20 changes: 20 additions & 0 deletions Apps/W1/Shopify/app/src/GraphQL/Enums/ShpfyGraphQLType.Enum.al
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,26 @@ enum 30111 "Shpfy GraphQL Type" implements "Shpfy IGraphQL"
Caption = 'Get Order Transactions';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL OrderTransactions";
}
value(80; DraftOrderComplete)
{
Caption = 'Draft Order Complete';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL DraftOrderComplete";
}
value(81; FulfillOrder)
{
Caption = 'Fulfill Order';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL Fulfill Order";
}
value(82; GetPaymentTerms)
{
Caption = 'Get Payment Terms';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL Payment Terms";
}
value(83; GetFulfillmentOrderIds)
{
Caption = 'Get Fulfillments';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL Get Fulfillments";
}
value(85; ProductVariantDelete)
{
Caption = 'Product Variant Delete';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ codeunit 30199 "Shpfy Authentication Mgt."

var
// https://shopify.dev/api/usage/access-scopes
ScopeTxt: Label 'write_orders,read_all_orders,write_assigned_fulfillment_orders,read_checkouts,write_customers,read_discounts,write_files,write_merchant_managed_fulfillment_orders,write_fulfillments,write_inventory,read_locations,read_payment_terms,write_products,write_shipping,read_shopify_payments_disputes,read_shopify_payments_payouts,write_returns,write_translations,write_third_party_fulfillment_orders,write_order_edits,write_companies,write_publications,read_locales', Locked = true;
ScopeTxt: Label 'write_orders,read_all_orders,write_assigned_fulfillment_orders,read_checkouts,write_customers,read_discounts,write_files,write_merchant_managed_fulfillment_orders,write_fulfillments,write_inventory,read_locations,write_products,write_shipping,read_shopify_payments_disputes,read_shopify_payments_payouts,write_returns,write_translations,write_third_party_fulfillment_orders,write_order_edits,write_companies,write_publications,read_payment_terms,write_payment_terms,write_draft_orders,read_locales', Locked = true;
ShopifyAPIKeyAKVSecretNameLbl: Label 'ShopifyApiKey', Locked = true;
ShopifyAPISecretAKVSecretNameLbl: Label 'ShopifyApiSecret', Locked = true;
MissingAPIKeyTelemetryTxt: Label 'The api key has not been initialized.', Locked = true;
Expand Down
Loading

0 comments on commit 7c3fe3e

Please sign in to comment.