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
2 changes: 1 addition & 1 deletion Apps/W1/Shopify/app/src/Base/Pages/ShpfyShopCard.Page.al
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ page 30101 "Shpfy Shop Card"

trigger OnAction();
var
ExportInvoicetoShpfy: Report "Shpfy Export Invoice to Shpfy";
ExportInvoicetoShpfy: Report "Shpfy Sync Invoices to Shpfy";
begin
ExportInvoicetoShpfy.SetShop(Rec.Code);
ExportInvoicetoShpfy.Run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ enum 30111 "Shpfy GraphQL Type" implements "Shpfy IGraphQL"
Caption = 'Get Payment Terms';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL Payment Terms";
}
value(83; GetFulfillments)
value(83; GetFulfillmentOrderIds)
{
Caption = 'Get Fulfillments';
Implementation = "Shpfy IGraphQL" = "Shpfy GQL Get Fulfillments";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,6 @@ codeunit 30159 "Shpfy Draft Orders API"
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,45 @@ namespace Microsoft.Integration.Shopify;
/// </summary>
codeunit 30315 "Shpfy Fulfillment API"
{
Access = Internal;

var
ShpfyCommunicationMgt: Codeunit "Shpfy Communication Mgt.";

/// <summary>
/// Creates a fulfillment for a provided fulfillment order id.
/// </summary>
/// <param name="FulfillmentOrderId">Fulfillment order id.</param>
internal procedure CreateFulfillment(FulfillmentOrderId: Text)
internal procedure CreateFulfillment(FulfillmentOrderId: BigInteger)
var
JResponse: JsonToken;
GraphQLType: Enum "Shpfy GraphQL Type";
Parameters: Dictionary of [Text, Text];
begin
GraphQLType := "Shpfy GraphQL Type"::FulfillOrder;
Parameters.Add('FulfillmentOrderId', FulfillmentOrderId);
Parameters.Add('FulfillmentOrderId', Format(FulfillmentOrderId));
JResponse := ShpfyCommunicationMgt.ExecuteGraphQL(GraphQLType, Parameters);
end;

/// <summary>
/// Gets fulfillment orders for a provided shopify order id.
/// Gets fulfillment order ids 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
/// <returns>List of fulfillment order ids.</returns>
internal procedure GetFulfillmentOrderIds(OrderId: Text; NumberOfLines: Integer) FulfillmentOrderList: List of [BigInteger]
var
GraphQLType: Enum "Shpfy GraphQL Type";
Parameters: Dictionary of [Text, Text];
JFulfillments: JsonToken;
begin
GraphQLType := "Shpfy GraphQL Type"::GetFulfillments;
GraphQLType := "Shpfy GraphQL Type"::GetFulfillmentOrderIds;
Parameters.Add('OrderId', OrderId);
Parameters.Add('NumberOfOrders', Format(NumberOfLines));
JFulfillments := ShpfyCommunicationMgt.ExecuteGraphQL(GraphQLType, Parameters);
exit(JFulfillments);
FulfillmentOrderList := ParseFulfillmentOrders(JFulfillments);

exit(FulfillmentOrderList);
end;

/// <summary>
Expand All @@ -49,4 +54,16 @@ codeunit 30315 "Shpfy Fulfillment API"
begin
ShpfyCommunicationMgt.SetShop(ShopCode);
end;

local procedure ParseFulfillmentOrders(JFulfillments: JsonToken) FulfillmentOrderList: List of [BigInteger]
var
ShpfyJsonHelper: Codeunit "Shpfy Json Helper";
JArray: JsonArray;
JToken: JsonToken;
begin
JArray := ShpfyJsonHelper.GetJsonArray(JFulfillments, 'data.order.fulfillmentOrders.nodes');

foreach JToken in JArray do
FulfillmentOrderList.Add(ShpfyCommunicationMgt.GetIdOfGId(ShpfyJsonHelper.GetValueAsText(JToken, 'id')));
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace Microsoft.Integration.Shopify;

using Microsoft.Sales.History;
using Microsoft.Finance.GeneralLedger.Setup;
using Microsoft.Utilities;
using Microsoft.Sales.Receivables;

/// <summary>
Expand Down Expand Up @@ -47,11 +46,12 @@ codeunit 30316 "Shpfy Posted Invoice Export"
var
TempShpfyOrderHeader: Record "Shpfy Order Header" temporary;
TempShpfyOrderLine: Record "Shpfy Order Line" temporary;
ShpfyFulfillmentAPI: Codeunit "Shpfy Fulfillment API";
DraftOrderId: BigInteger;
ShpfyOrderTaxLines: Dictionary of [Text, Decimal];
JFulfillments: JsonToken;
FulfillmentOrderIds: List of [BigInteger];
JResponse: JsonToken;
OrderId: BigInteger;
OrderNo: Text;
begin
if not IsInvoiceExportable(SalesInvoiceHeader) then begin
SetSalesInvoiceShopifyOrderInformation(SalesInvoiceHeader, -2, '');
Expand All @@ -62,46 +62,26 @@ codeunit 30316 "Shpfy Posted Invoice Export"

DraftOrderId := ShpfyDraftOrdersAPI.CreateDraftOrder(TempShpfyOrderHeader, TempShpfyOrderLine, ShpfyOrderTaxLines);
JResponse := ShpfyDraftOrdersAPI.CompleteDraftOrder(DraftOrderId);
JFulfillments := ShpfyFulfillmentAPI.GetFulfillmentOrders(
Format(ShpfyJsonHelper.GetValueAsBigInteger(JResponse, 'data.draftOrderComplete.draftOrder.order.legacyResourceId')),
GetNumberOfLines(TempShpfyOrderLine, ShpfyOrderTaxLines)
);
CreateFulfillmentsForShopifyOrder(JFulfillments);

if IsSuccess(JResponse) then begin
CreateShpfyInvoiceHeader(JResponse, SalesInvoiceHeader."No.");
SetSalesInvoiceShopifyOrderInformation(
SalesInvoiceHeader,
ShpfyJsonHelper.GetValueAsBigInteger(JResponse, 'data.draftOrderComplete.draftOrder.order.legacyResourceId'),
Format(ShpfyJsonHelper.GetValueAsText(JResponse, 'data.draftOrderComplete.draftOrder.order.name'))
);
OrderId := ShpfyJsonHelper.GetValueAsBigInteger(JResponse, 'data.draftOrderComplete.draftOrder.order.legacyResourceId');
OrderNo := ShpfyJsonHelper.GetValueAsText(JResponse, 'data.draftOrderComplete.draftOrder.order.name');

FulfillmentOrderIds := ShpfyFulfillmentAPI.GetFulfillmentOrderIds(Format(OrderId), GetNumberOfLines(TempShpfyOrderLine, ShpfyOrderTaxLines));
CreateFulfillmentsForShopifyOrder(FulfillmentOrderIds);
CreateShpfyInvoiceHeader(OrderId);
SetSalesInvoiceShopifyOrderInformation(SalesInvoiceHeader, OrderId, Format(OrderNo));
AddDocumentLinkToBCDocument(SalesInvoiceHeader);
end else
SetSalesInvoiceShopifyOrderInformation(SalesInvoiceHeader, -1, '');
end;

local procedure CreateFulfillmentsForShopifyOrder(JFulfillments: JsonToken)
local procedure CreateFulfillmentsForShopifyOrder(FulfillmentOrderIds: List of [BigInteger])
var
FulfillmentOrderList: List of [Text];
FulfillmentOrderId: Text;
FulfillmentOrderId: BigInteger;
begin
FulfillmentOrderList := ParseFulfillmentOrders(JFulfillments);

foreach FulfillmentOrderId in FulfillmentOrderList do begin
foreach FulfillmentOrderId in FulfillmentOrderIds do
ShpfyFulfillmentAPI.CreateFulfillment(FulfillmentOrderId);
end;
end;

local procedure ParseFulfillmentOrders(JFulfillments: JsonToken) FulfillmentOrderList: List of [Text]
var
ShpfyCommunicationMgt: Codeunit "Shpfy Communication Mgt.";
JArray: JsonArray;
JToken: JsonToken;
begin
JArray := ShpfyJsonHelper.GetJsonArray(JFulfillments, 'data.order.fulfillmentOrders.nodes');

foreach JToken in JArray do
FulfillmentOrderList.Add(Format(ShpfyCommunicationMgt.GetIdOfGId(ShpfyJsonHelper.GetValueAsText(JToken, 'id'))));
end;

local procedure IsInvoiceExportable(SalesInvoiceHeader: Record "Sales Invoice Header"): Boolean
Expand Down Expand Up @@ -246,7 +226,7 @@ codeunit 30316 "Shpfy Posted Invoice Export"
SalesInvoiceHeader.CalcFields("Invoice Discount Amount");
TempShpfyOrderHeader."Discount Amount" := SalesInvoiceHeader."Invoice Discount Amount";
TempShpfyOrderHeader."Shop Code" := ShpfyShop.Code;
TempShpfyOrderHeader.Insert();
TempShpfyOrderHeader.Insert(false);
end;

local procedure MapCurrencyCode(SalesInvoiceHeader: Record "Sales Invoice Header"): Code[10]
Expand All @@ -267,7 +247,7 @@ codeunit 30316 "Shpfy Posted Invoice Export"
var
ShpfyCustomer: Record "Shpfy Customer";
begin
TempShpfyOrderHeader."Bill-to Name" := SalesInvoiceHeader."Bill-to Name";
TempShpfyOrderHeader."Bill-to Name" := Format(SalesInvoiceHeader."Bill-to Name");
TempShpfyOrderHeader."Bill-to Name 2" := SalesInvoiceHeader."Bill-to Name 2";
TempShpfyOrderHeader."Bill-to Address" := SalesInvoiceHeader."Bill-to Address";
TempShpfyOrderHeader."Bill-to Address 2" := SalesInvoiceHeader."Bill-to Address 2";
Expand All @@ -279,7 +259,7 @@ codeunit 30316 "Shpfy Posted Invoice Export"

ShpfyCustomer.SetRange("Customer No.", SalesInvoiceHeader."Bill-to Customer No.");
if ShpfyCustomer.FindFirst() then begin
TempShpfyOrderHeader.Email := ShpfyCustomer.Email;
TempShpfyOrderHeader.Email := Format(ShpfyCustomer.Email);
TempShpfyOrderHeader."Phone No." := ShpfyCustomer."Phone No.";
end;
end;
Expand All @@ -289,7 +269,7 @@ codeunit 30316 "Shpfy Posted Invoice Export"
SalesInvoiceHeader: Record "Sales Invoice Header"
)
begin
TempShpfyOrderHeader."Ship-to Name" := SalesInvoiceHeader."Ship-to Name";
TempShpfyOrderHeader."Ship-to Name" := Format(SalesInvoiceHeader."Ship-to Name");
TempShpfyOrderHeader."Ship-to Name 2" := SalesInvoiceHeader."Ship-to Name 2";
TempShpfyOrderHeader."Ship-to Address" := SalesInvoiceHeader."Ship-to Address";
TempShpfyOrderHeader."Ship-to Address 2" := SalesInvoiceHeader."Ship-to Address 2";
Expand Down Expand Up @@ -333,7 +313,7 @@ codeunit 30316 "Shpfy Posted Invoice Export"
TempShpfyOrderLine.Taxable := false;
TempShpfyOrderLine."Unit Price" := SalesInvoiceLine."Unit Price";
TempShpfyOrderHeader."Discount Amount" += SalesInvoiceLine."Line Discount Amount";
TempShpfyOrderHeader.Modify();
TempShpfyOrderHeader.Modify(false);

if ShpfyShop."UoM as Variant" then
MapUOMProductVariants(SalesInvoiceLine, TempShpfyOrderLine)
Expand All @@ -348,7 +328,7 @@ codeunit 30316 "Shpfy Posted Invoice Export"

MapTaxLine(SalesInvoiceLine, ShpfyOrderTaxLines);

TempShpfyOrderLine.Insert();
TempShpfyOrderLine.Insert(false);
end;

local procedure MapUOMProductVariants(SalesInvoiceLine: Record "Sales Invoice Line"; var TempShpfyOrderLine: Record "Shpfy Order Line" temporary)
Expand Down Expand Up @@ -402,12 +382,12 @@ codeunit 30316 "Shpfy Posted Invoice Export"
exit(ShpfyJsonHelper.GetJsonArray(JsonTokenResponse, 'data.draftOrderComplete.userErrors').Count() = 0);
end;

local procedure CreateShpfyInvoiceHeader(JResponse: JsonToken; InvoiceNo: Code[20])
local procedure CreateShpfyInvoiceHeader(OrderId: BigInteger)
var
ShpfyInvoiceHeader: Record "Shpfy Invoice Header";
begin
ShpfyInvoiceHeader.Init();
ShpfyInvoiceHeader.Validate("Shopify Order Id", ShpfyJsonHelper.GetValueAsBigInteger(JResponse, 'data.draftOrderComplete.draftOrder.order.legacyResourceId'));
ShpfyInvoiceHeader.Validate("Shopify Order Id", OrderId);
ShpfyInvoiceHeader.Insert(true);
end;

Expand All @@ -421,7 +401,7 @@ codeunit 30316 "Shpfy Posted Invoice Export"
DocLinkToBCDoc."Shopify Document Id" := SalesInvoiceHeader."Shpfy Order Id";
DocLinkToBCDoc."Document Type" := BCDocumentTypeConvert.Convert(SalesInvoiceHeader);
DocLinkToBCDoc."Document No." := SalesInvoiceHeader."No.";
DocLinkToBCDoc.Insert();
DocLinkToBCDoc.Insert(true);
end;

local procedure GetNumberOfLines(var TempShpfyOrderLine: Record "Shpfy Order Line" temporary; var ShpfyOrderTaxLines: Dictionary of [Text, Decimal]): Integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ using Microsoft.Sales.History;

codeunit 30314 "Shpfy Update Sales Invoice"
{
Access = Internal;

[EventSubscriber(ObjectType::Page, Page::"Posted Sales Inv. - Update", 'OnAfterRecordChanged', '', false, false)]
local procedure OnAfterRecordChanged(var SalesInvoiceHeader: Record "Sales Invoice Header"; xSalesInvoiceHeader: Record "Sales Invoice Header"; var IsChanged: Boolean)
begin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pageextension 30125 "Shpfy Sales Invoice Update" extends "Posted Sales Inv. - Up
{
group(Shopify)
{
Caption = 'Shopify';
Visible = ShopifyTabVisible;

field("Shpfy Order Id"; Rec."Shpfy Order Id")
{
ApplicationArea = Basic, Suite;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ namespace Microsoft.Integration.Shopify;
using Microsoft.Sales.History;

/// <summary>
/// Report Shpfy Export Invoice to Shpfy (ID 30117).
/// Report Shpfy Sync Invoices to Shpfy (ID 30117).
/// </summary>
report 30117 "Shpfy Export Invoice to Shpfy"
report 30117 "Shpfy Sync Invoices to Shpfy"
{
ApplicationArea = All;
Caption = 'Export Invoice to Shopify';
Caption = 'Sync Invoices to Shopify';
ProcessingOnly = true;
UsageCategory = Administration;
UsageCategory = Tasks;

dataset
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ table 30156 "Shpfy Invoice Header"
{
Caption = 'Shopify Invoice Header';
DataClassification = CustomerContent;
Access = Internal;

fields
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Microsoft.Integration.Shopify;
/// </summary>
codeunit 30168 "Shpfy Payment Terms API"
{
Access = Internal;

var
CommunicationMgt: Codeunit "Shpfy Communication Mgt.";
JsonHelper: Codeunit "Shpfy Json Helper";
Expand Down Expand Up @@ -63,8 +65,8 @@ codeunit 30168 "Shpfy Payment Terms API"
PaymentTermRecordRef.SetTable(ShpfyPaymentTerms);

if IsNew then
ShpfyPaymentTerms.Insert()
ShpfyPaymentTerms.Insert(true)
else
ShpfyPaymentTerms.Modify();
ShpfyPaymentTerms.Modify(true);
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ table 30157 "Shpfy Payment Terms"
{
Caption = 'Payment Terms';
DataClassification = CustomerContent;
Access = Internal;

fields
{
Expand Down