Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/gga/shopify v2 #4

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ codeunit 30312 "Shpfy GQL DraftOrderComplete" implements "Shpfy IGraphQL"
/// <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, fulfillmentOrders(first: {{NumberOfOrders}}) {nodes{ id, status}} } id, status} userErrors { field, message }}}"}');
exit('{"query": "mutation {draftOrderComplete(id: \"gid://shopify/DraftOrder/{{DraftOrderId}}\") { draftOrder { order { legacyResourceId, name }} userErrors { field, message }}}"}');
end;

/// <summary>
Expand All @@ -22,6 +22,6 @@ codeunit 30312 "Shpfy GQL DraftOrderComplete" implements "Shpfy IGraphQL"
/// <returns>Return value of type Integer.</returns>
internal procedure GetExpectedCost(): Integer
begin
exit(15);
exit(11);
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// <summary>
/// Codeunit Shpfy GQL Get Fulfillments (ID 30317) implements Interface Shpfy IGraphQL.
/// </summary>
codeunit 30317 "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
Expand Up @@ -415,4 +415,9 @@ enum 30111 "Shpfy GraphQL Type" implements "Shpfy IGraphQL"
Caption = 'Get Payment Terms';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL Payment Terms";
}
value(83; GetFulfillments)
{
Caption = 'Get Fulfillments';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL Get Fulfillments";
}
}
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,write_payment_terms,write_draft_orders', 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,write_payment_terms,write_draft_orders', 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ namespace Microsoft.Integration.Shopify;
using Microsoft.Sales.Comment;
using Microsoft.Sales.History;
using Microsoft.Finance.Currency;
using Microsoft.Inventory.Item.Attribute;
using Microsoft.Inventory.Item;

/// <summary>
/// Codeunit Draft Orders API (ID 30159).
Expand All @@ -29,8 +31,8 @@ codeunit 30159 "Shpfy Draft Orders API"
var ShpfyOrderTaxLines: Dictionary of [Text, Decimal]
): BigInteger
var
GraphQuery: TextBuilder;
DraftOrderId: BigInteger;
GraphQuery: TextBuilder;
begin
GraphQuery := CreateDraftOrderGQLRequest(TempShpfyOrderHeader, TempShpfyOrderLine, ShpfyOrderTaxLines);
DraftOrderId := SendDraftOrderGraphQLRequest(GraphQuery);
Expand All @@ -41,23 +43,21 @@ codeunit 30159 "Shpfy Draft Orders API"
/// Completes a draft order in shopify by converting it to an order.
/// </summary>
/// <param name="DraftOrderId">Draft order id that needs to be completed.</param>
/// <param name="NumberOfLines">Maximum amount of possible fulfillment orders.</param>
/// <returns>Json response of a created order in shopify.</returns>
internal procedure CompleteDraftOrder(DraftOrderId: BigInteger; NumberOfLines: Integer): JsonToken
internal procedure CompleteDraftOrder(DraftOrderId: BigInteger): JsonToken
var
GraphQLType: Enum "Shpfy GraphQL Type";
Parameters: Dictionary of [Text, Text];
JResponse: JsonToken;
begin
GraphQLType := "Shpfy GraphQL Type"::DraftOrderComplete;
Parameters.Add('DraftOrderId', Format(DraftOrderId));
Parameters.Add('NumberOfOrders', Format(NumberOfLines));
JResponse := ShpfyCommunicationMgt.ExecuteGraphQL(GraphQLType, Parameters);
exit(JResponse);
end;

/// <summary>
/// Sets a global shopify shop variable to be used.
/// Sets a global shopify shop to be used for draft orders api functionality.
/// </summary>
/// <param name="ShopCode">Shopify shop code to be set.</param>
internal procedure SetShop(ShopCode: Code[20])
Expand Down Expand Up @@ -110,18 +110,15 @@ codeunit 30159 "Shpfy Draft Orders API"

local procedure SendDraftOrderGraphQLRequest(GraphQuery: TextBuilder): BigInteger
var
ShpfyPaymentTermAPI: Codeunit "Shpfy Payment Terms API";
JResponse: JsonToken;
DraftOrderId: BigInteger;
JResponse: JsonToken;
begin
JResponse := ShpfyCommunicationMgt.ExecuteGraphQL(GraphQuery.ToText());
DraftOrderId := ShpfyJsonHelper.GetValueAsBigInteger(JResponse, 'data.draftOrderCreate.draftOrder.legacyResourceId');
exit(DraftOrderId);
end;

local procedure AddShippingAddressToGraphQuery(var GraphQuery: TextBuilder; var TempShpfyOrderHeader: Record "Shpfy Order Header" temporary)
var
myInt: Integer;
begin
GraphQuery.Append(', shippingAddress: {');
if TempShpfyOrderHeader."Ship-to Address" <> '' then begin
Expand Down Expand Up @@ -223,6 +220,8 @@ codeunit 30159 "Shpfy Draft Orders API"
GraphQuery.Append(', variantId: \"gid://shopify/ProductVariant/');
GraphQuery.Append(Format(TempShpfyOrderLine."Shopify Variant Id"));
GraphQuery.Append('\"');

AddItemAttributes(GraphQuery, TempShpfyOrderLine."Item No.");
end;

GraphQuery.Append(', quantity: ');
Expand Down Expand Up @@ -254,8 +253,6 @@ codeunit 30159 "Shpfy Draft Orders API"
end;

local procedure AddDiscountAmountToGraphQuery(var GraphQuery: TextBuilder; DiscountAmount: Decimal; DiscountTitle: Text)
var
myInt: Integer;
begin
GraphQuery.Append(', appliedDiscount: {');
GraphQuery.Append('description: \"');
Expand Down Expand Up @@ -327,20 +324,20 @@ codeunit 30159 "Shpfy Draft Orders API"
end;

local procedure ShopifyPaymentTermsExists(
var ShpfyPaymentTerm: Record "Shpfy Payment Terms";
var ShpfyPaymentTerms: Record "Shpfy Payment Terms";
var TempShpfyOrderHeader: Record "Shpfy Order Header" temporary;
var SalesInvoiceHeader: Record "Sales Invoice Header"
): Boolean
begin
SalesInvoiceHeader.Get(TempShpfyOrderHeader."Sales Invoice No.");
ShpfyPaymentTerm.SetRange("Payment Terms Code", SalesInvoiceHeader."Payment Terms Code");
ShpfyPaymentTerm.SetRange("Shop Code", ShpfyShop.Code);
ShpfyPaymentTerms.SetRange("Payment Terms Code", SalesInvoiceHeader."Payment Terms Code");
ShpfyPaymentTerms.SetRange("Shop Code", ShpfyShop.Code);

if not ShpfyPaymentTerm.FindFirst() then begin
ShpfyPaymentTerm.SetRange("Payment Terms Code");
ShpfyPaymentTerm.SetRange("Is Primary", true);
if not ShpfyPaymentTerms.FindFirst() then begin
ShpfyPaymentTerms.SetRange("Payment Terms Code");
ShpfyPaymentTerms.SetRange("Is Primary", true);

if not ShpfyPaymentTerm.FindFirst() then
if not ShpfyPaymentTerms.FindFirst() then
exit(false);
end;

Expand All @@ -354,4 +351,42 @@ codeunit 30159 "Shpfy Draft Orders API"
Currency.Get(CurrencyCode);
exit(Currency."ISO Code");
end;

local procedure IsItem(ItemNo: Code[20]): Boolean
var
Item: Record Item;
begin
exit(Item.Get(ItemNo));
end;

local procedure AddItemAttributes(var GraphQuery: TextBuilder; ItemNo: Code[20])
var
Item: Record Item;
ItemAttribute: Record "Item Attribute";
ItemAttributeValue: Record "Item Attribute Value";
ItemAttributeValueMapping: Record "Item Attribute Value Mapping";
begin
Item.Get(ItemNo);
ItemAttributeValueMapping.SetRange("Table ID", Database::Item);
ItemAttributeValueMapping.SetRange("No.", ItemNo);
if ItemAttributeValueMapping.FindSet() then begin
GraphQuery.Append(', customAttributes: [');
repeat
ItemAttribute.Get(ItemAttributeValueMapping."Item Attribute ID");
ItemAttributeValue.Get(ItemAttribute.ID, ItemAttributeValueMapping."Item Attribute Value ID");

GraphQuery.Append('{');
GraphQuery.Append('key: \"');
GraphQuery.Append(ShpfyCommunicationMgt.EscapeGrapQLData(Format(ItemAttribute.Name)));
GraphQuery.Append('\"');

GraphQuery.Append(', value: \"');
GraphQuery.Append(ShpfyCommunicationMgt.EscapeGrapQLData(Format(ItemAttributeValue.Value)));
GraphQuery.Append('\"');
GraphQuery.Append('},')
until ItemAttributeValueMapping.Next() = 0;
GraphQuery.Append(']');
end;

end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,47 @@ namespace Microsoft.Integration.Shopify;
codeunit 30315 "Shpfy Fulfillment API"
{
var
ShpfyJsonHelper: Codeunit "Shpfy Json Helper";
ShpfyCommunicationMgt: Codeunit "Shpfy Communication Mgt.";

/// <summary>
/// Fulfills shopify orders for each fulfillment id parsed from a completed draft order.
/// Creates a fulfillment for a provided fulfillment order id.
/// </summary>
/// <param name="JResponse">Json response from a completed draft order</param>
/// <param name="ShopCode">Shopify shop code to be used.</param>
internal procedure FulfillShopifyOrder(JResponse: JsonToken; ShopCode: Code[20])
/// <param name="FulfillmentOrderId">Fulfillment order id.</param>
internal procedure CreateFulfillment(FulfillmentOrderId: Text)
var
FulfillmentOrderList: List of [Text];
FulfillmentOrderId: Text;
ResponseJsonToken: JsonToken;
JResponse: JsonToken;
GraphQLType: Enum "Shpfy GraphQL Type";
Parameters: Dictionary of [Text, Text];
begin
ShpfyCommunicationMgt.SetShop(ShopCode);
FulfillmentOrderList := ParseFulfillmentOrders(JResponse);
GraphQLType := "Shpfy GraphQL Type"::FulfillOrder;

foreach FulfillmentOrderId in FulfillmentOrderList do begin
Parameters.Add('FulfillmentOrderId', FulfillmentOrderId);
ResponseJsonToken := ShpfyCommunicationMgt.ExecuteGraphQL(GraphQLType, Parameters);
Clear(Parameters);
end;
Parameters.Add('FulfillmentOrderId', FulfillmentOrderId);
JResponse := ShpfyCommunicationMgt.ExecuteGraphQL(GraphQLType, Parameters);
end;

local procedure ParseFulfillmentOrders(ResponseJsonToken: JsonToken) FulfillmentOrderList: List of [Text]
/// <summary>
/// Gets fulfillment orders for a provided shopify order id.
/// </summary>
/// <param name="OrderId">Shopify order id to get fulfillments from.</param>
/// <param name="NumberOfLines">Number of fulfillment orders to get.</param>
/// <returns>Fulfillment orders.</returns>
internal procedure GetFulfillmentOrders(OrderId: Text; NumberOfLines: Integer) JFulfillments: JsonToken
var
Counter: Integer;
FulfillmentOrderArray: JsonArray;
FulfillmentObject: JsonObject;
FulfillmentOrderToken: JsonToken;
JToken: JsonToken;
GraphQLType: Enum "Shpfy GraphQL Type";
Parameters: Dictionary of [Text, Text];
begin
FulfillmentObject := ResponseJsonToken.AsObject();
FulfillmentObject.SelectToken('data.draftOrderComplete.draftOrder.order.fulfillmentOrders.nodes', JToken);
FulfillmentOrderArray := ShpfyJsonHelper.GetJsonArray(JToken, '');
GraphQLType := "Shpfy GraphQL Type"::GetFulfillments;
Parameters.Add('OrderId', OrderId);
Parameters.Add('NumberOfOrders', Format(NumberOfLines));
JFulfillments := ShpfyCommunicationMgt.ExecuteGraphQL(GraphQLType, Parameters);
exit(JFulfillments);
end;

for Counter := 0 to FulfillmentOrderArray.Count() - 1 do begin
FulfillmentOrderArray.Get(Counter, FulfillmentOrderToken);
FulfillmentOrderList.Add(Format(ShpfyCommunicationMgt.GetIdOfGId(ShpfyJsonHelper.GetValueAsText(FulfillmentOrderToken, 'id'))));
end;
/// <summary>
/// Sets a global shopify shop to be used for fulfillment api functionality.
/// </summary>
/// <param name="ShopCode">Shopify shop code to be set.</param>
internal procedure SetShop(ShopCode: Code[20])
begin
ShpfyCommunicationMgt.SetShop(ShopCode);
end;
}
Loading