Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -64,6 +64,16 @@ codeunit 30262 "Shpfy Document Link Mgt."

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnAfterPostSalesDoc', '', true, false)]
local procedure OnAfterSalesPosting(var SalesHeader: Record "Sales Header"; PreviewMode: Boolean; SalesShptHdrNo: Code[20]; SalesInvHdrNo: Code[20]; RetRcpHdrNo: Code[20]; SalesCrMemoHdrNo: Code[20])
var
ShpfyAutoPostTransactions: Codeunit "Shpfy Auto Post Transactions";
begin
CreateDocLinksToBCDocs(SalesHeader, PreviewMode, SalesShptHdrNo, SalesInvHdrNo, RetRcpHdrNo, SalesCrMemoHdrNo);

Commit(); // Ensure Doc. Link To Doc. creation is committed before running the automatic transaction posting.
ShpfyAutoPostTransactions.AutoPostTransactions(SalesInvHdrNo, SalesCrMemoHdrNo);
end;

local procedure CreateDocLinksToBCDocs(var SalesHeader: Record "Sales Header"; PreviewMode: Boolean; SalesShptHdrNo: Code[20]; SalesInvHdrNo: Code[20]; RetRcpHdrNo: Code[20]; SalesCrMemoHdrNo: Code[20])
var
SalesShipmentHeader: Record "Sales Shipment Header";
SalesInvoiceLine: Record "Sales Invoice Line";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ permissionset 30104 "Shpfy - Objects"
report "Shpfy Translator" = X,
codeunit "Company Details Checklist Item" = X,
codeunit "Shpfy Authentication Mgt." = X,
codeunit "Shpfy Auto Gen. Jnl.-Post" = X,
codeunit "Shpfy Auto Post Transactions" = X,
codeunit "Shpfy Background Syncs" = X,
codeunit "Shpfy Balance Today" = X,
codeunit "Shpfy Base64" = X,
Expand Down Expand Up @@ -441,6 +443,7 @@ permissionset 30104 "Shpfy - Objects"
page "Shpfy Customers" = X,
page "Shpfy Data Capture List" = X,
page "Shpfy Disputes" = X,
page "Shpfy Filter Transactions" = X,
page "Shpfy Fulfillment Order Card" = X,
page "Shpfy Fulfillment Order Lines" = X,
page "Shpfy Fulfillment Orders" = X,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------

namespace Microsoft.Integration.Shopify;

using Microsoft.Finance.GeneralLedger.Journal;
using Microsoft.Finance.GeneralLedger.Posting;

/// <summary>
/// Codeunit Shpfy Auto Gen. Jnl.-Post (ID 30422).
/// </summary>
codeunit 30422 "Shpfy Auto Gen. Jnl.-Post"
{
EventSubscriberInstance = Manual;

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Gen. Jnl.-Post", 'OnBeforeCode', '', false, false)]
local procedure OnBeforeCode(var GenJournalLine: Record "Gen. Journal Line"; var HideDialog: Boolean)
begin
HideDialog := true;
end;

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Gen. Jnl.-Post", 'OnBeforeShowPostResultMessage', '', false, false)]
local procedure OnBeforeShowPostResultMessage(var GenJnlLine: Record "Gen. Journal Line"; TempJnlBatchName: Code[10]; var IsHandled: Boolean)
begin
IsHandled := true;
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------

namespace Microsoft.Integration.Shopify;

using Microsoft.Finance.GeneralLedger.Journal;
using Microsoft.Finance.GeneralLedger.Posting;
using Microsoft.Sales.History;

/// <summary>
/// Codeunit Shpfy Auto Post Transactions (ID 30236).
/// </summary>
codeunit 30236 "Shpfy Auto Post Transactions"
{
Access = Internal;

internal procedure AutoPostTransactions(SalesInvoiceHeaderNo: Code[20]; SalesCrMemoHeaderNo: Code[20])
begin
if SalesInvoiceHeaderNo <> '' then
PostOrderTransaction(SalesInvoiceHeaderNo);
if SalesCrMemoHeaderNo <> '' then
PostRefundTransaction(SalesCrMemoHeaderNo);
end;

local procedure PostOrderTransaction(SalesInvoiceHeaderNo: Code[20])
var
SalesInvoiceHeader: Record "Sales Invoice Header";
OrderTransaction: Record "Shpfy Order Transaction";
begin
if not SalesInvoiceHeader.Get(SalesInvoiceHeaderNo) then
exit;

if SalesInvoiceHeader."Shpfy Order Id" = 0 then
exit;

OrderTransaction.SetRange("Shopify Order Id", SalesInvoiceHeader."Shpfy Order Id");
OrderTransaction.SetFilter(Type, '%1|%2', OrderTransaction.Type::Capture, OrderTransaction.Type::Sale);
PostTransaction(OrderTransaction, SalesInvoiceHeader."Posting Date");
end;

local procedure PostRefundTransaction(SalesCrMemoHeaderNo: Code[20])
var
SalesCrMemoHeader: Record "Sales Cr.Memo Header";
OrderTransaction: Record "Shpfy Order Transaction";
begin
if not SalesCrMemoHeader.Get(SalesCrMemoHeaderNo) then
exit;

if SalesCrMemoHeader."Shpfy Refund Id" = 0 then
exit;

OrderTransaction.SetRange("Refund Id", SalesCrMemoHeader."Shpfy Refund Id");
OrderTransaction.SetRange(Type, OrderTransaction.Type::Refund);
PostTransaction(OrderTransaction, SalesCrMemoHeader."Posting Date");
end;

local procedure PostTransaction(var OrderTransaction: Record "Shpfy Order Transaction"; PostingDate: Date)
begin
OrderTransaction.SetRange(Status, OrderTransaction.Status::Success);
OrderTransaction.SetRange(Used, false);
if OrderTransaction.FindSet() then
repeat
if ShouldAutoPost(OrderTransaction) then
CreateAndPostJournalLine(OrderTransaction, PostingDate);
until OrderTransaction.Next() = 0;
end;

local procedure ShouldAutoPost(OrderTransaction: Record "Shpfy Order Transaction"): Boolean
var
PaymentMethodMapping: Record "Shpfy Payment Method Mapping";
begin
if not PaymentMethodMapping.Get(OrderTransaction.Shop, OrderTransaction.Gateway, OrderTransaction."Credit Card Company") then
exit(false);

if not PaymentMethodMapping."Post Automatically" then
exit(false);

if (PaymentMethodMapping."Auto-Post Jnl. Template" = '') or
(PaymentMethodMapping."Auto-Post Jnl. Batch" = '') then
exit(false);

exit(true);
end;

local procedure CreateAndPostJournalLine(var OrderTransaction: Record "Shpfy Order Transaction"; PostingDate: Date)
var
GenJournalLine: Record "Gen. Journal Line";
PaymentMethodMapping: Record "Shpfy Payment Method Mapping";
SuggestPayments: Report "Shpfy Suggest Payments";
AutoGenJnlPost: Codeunit "Shpfy Auto Gen. Jnl.-Post";
begin
PaymentMethodMapping.Get(OrderTransaction.Shop, OrderTransaction.Gateway, OrderTransaction."Credit Card Company");
SuggestPayments.SetJournalParameters(PaymentMethodMapping."Auto-Post Jnl. Template", PaymentMethodMapping."Auto-Post Jnl. Batch", PostingDate);
SuggestPayments.GetOrderTransactions(OrderTransaction);
SuggestPayments.CreateGeneralJournalLines();
GenJournalLine.SetRange("Shpfy Transaction Id", OrderTransaction."Shopify Transaction Id");
if GenJournalLine.FindFirst() then begin
BindSubscription(AutoGenJnlPost);
GenJournalLine.SendToPosting(Codeunit::"Gen. Jnl.-Post");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Post with Job Queue is enabled on the GLSetup then the lines won't be posted directly and the lines might be deleted before the actual posting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, changed that not used lines (appears if error occurs during posting) would be deleted before generating new ones in "Suggest Shopify Payments" report.
image
image
What this changes, previously each time "Suggest Shopify Payments" report would be executed but lines not posted new gen journal line would be created
Executed first time:
image
Executed second time:
image
After this change old ones will always be removed, hope that is acceptable
image

UnbindSubscription(AutoGenJnlPost);
end;
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------

namespace Microsoft.Integration.Shopify;

/// <summary>
/// Page Shpfy Filter Transactions (ID 30176).
/// </summary>
page 30176 "Shpfy Filter Transactions"
{
Caption = 'Filter Postable Transactions';
PageType = StandardDialog;
ApplicationArea = All;

layout
{
area(Content)
{
field(Gateway; Gateway)
{
Caption = 'Gateway';
ToolTip = 'Specifies the transaction gateway to filter transactions by. Leave blank to include all gateways.';
Editable = false;

trigger OnAssistEdit()
var
PaymentMethodMapping: Record "Shpfy Payment Method Mapping";
begin
PaymentMethodMapping.SetRange("Post Automatically", true);
if Page.RunModal(0, PaymentMethodMapping) = Action::LookupOK then begin
ShopCode := PaymentMethodMapping."Shop Code";
Gateway := PaymentMethodMapping.Gateway;
CreditCardCompany := PaymentMethodMapping."Credit Card Company";
end;
end;
}
field(StartDate; StartDate)
{
Caption = 'Start Date';
ToolTip = 'Specifies the earliest transaction creation date to include in the filter. Leave blank to include all transactions from the beginning.';
}
field(EndDate; EndDate)
{
Caption = 'End Date';
ToolTip = 'Specifies the latest transaction creation date to include in the filter. Leave blank to include all transactions.';
}
}
}

var
ShopCode: Code[20];
Gateway: Text[30];
CreditCardCompany: Text[50];
StartDate: Date;
EndDate: Date;

internal procedure GetParameters(var NewShopCode: Code[20]; var NewGateway: Text[30]; var NewCreditCardCompany: Text[50]; var NewStartDate: DateTime; var NewEndDate: DateTime)
begin
NewShopCode := ShopCode;
NewGateway := Gateway;
NewCreditCardCompany := CreditCardCompany;
NewStartDate := CreateDateTime(StartDate, 0T);
NewEndDate := CreateDateTime(EndDate, 0T);
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ page 30132 "Shpfy Payment Methods Mapping"
ApplicationArea = All;
ToolTip = 'Specifies the corresponding payment method in D365BC.';
}
field(PostAutomatically; Rec."Post Automatically")
{
ApplicationArea = All;
ToolTip = 'Specifies whether payment transactions using this gateway should be automatically posted when the related invoice or credit memo is posted.';
}
field(AutoPostJnlTemplate; Rec."Auto-Post Jnl. Template")
{
ApplicationArea = All;
ToolTip = 'Specifies the general journal template to use for automatically posting payment transactions.';
Enabled = Rec."Post Automatically";
ShowMandatory = Rec."Post Automatically";
}
field(AutoPostJnlBatch; Rec."Auto-Post Jnl. Batch")
{
ApplicationArea = All;
ToolTip = 'Specifies the general journal batch to use for automatically posting payment transactions.';
Enabled = Rec."Post Automatically";
ShowMandatory = Rec."Post Automatically";
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ page 30134 "Shpfy Transactions"
ApplicationArea = All;
ToolTip = 'Specifies the Posted Invoice number to which the transaction relates.';
}
field("Auto-Post Enabled"; Rec."Auto-Post Enabled")
{
ApplicationArea = All;
ToolTip = 'Specifies whether auto-posting is enabled for the transaction.';
}
}
}
}
Expand Down Expand Up @@ -194,6 +199,31 @@ page 30134 "Shpfy Transactions"
SuggestPayments.Run();
end;
}
action(ShowPostableTransactions)
{
ApplicationArea = All;
Caption = 'Filter Postable Transactions';
Image = FilterLines;
ToolTip = 'Show transactions that need to be posted. Filters by auto-post enabled, not yet posted, and linked to a posted invoice. Optionally filter by gateway and date range.';

trigger OnAction()
begin
FilterPostableTransactions()
end;
}
action(ClearFilter)
{
ApplicationArea = All;
Caption = 'Clear Filter';
Image = ClearFilter;
ToolTip = 'Remove the postable transactions filter and show all transactions.';

trigger OnAction()
begin
Rec.ClearMarks();
Rec.MarkedOnly(false);
end;
}
}
area(Promoted)
{
Expand All @@ -210,6 +240,8 @@ page 30134 "Shpfy Transactions"
{
Caption = 'Related';
actionref(CustLedgerEntries_Promoted; CustLedgerEntries) { }
actionref(ShowPostableTransactions_Promoted; ShowPostableTransactions) { }
actionref(ClearFilter_Promoted; ClearFilter) { }
}
}
}
Expand All @@ -232,4 +264,58 @@ page 30134 "Shpfy Transactions"

PresentmentCurrencyVisible := OrderHeader.IsPresentmentCurrencyOrder();
end;

local procedure FilterPostableTransactions()
var
PaymentMethodMapping: Record "Shpfy Payment Method Mapping";
FilterTransactions: Page "Shpfy Filter Transactions";
FilterShopCode: Code[20];
FilterCreditCardCompany: Text[50];
FilterGateway: Text[30];
FilterStartDate: DateTime;
FilterEndDate: DateTime;
begin
if not (FilterTransactions.RunModal() = Action::OK) then
exit;

FilterTransactions.GetParameters(FilterShopCode, FilterGateway, FilterCreditCardCompany, FilterStartDate, FilterEndDate);
if (FilterStartDate <> 0DT) or (FilterEndDate <> 0DT) then
if FilterEndDate <> 0DT then
Rec.SetRange("Created At", FilterStartDate, FilterEndDate)
else
Rec.SetRange("Created At", FilterStartDate, CreateDateTime(DMY2Date(31, 12, 9999), 0T));
Rec.SetRange(Used, false);
Rec.SetFilter("Posted Invoice No.", '<>%1', '');

Rec.ClearMarks();
Rec.MarkedOnly(false);
if FilterGateway <> '' then
MarkPostableTransactions(FilterShopCode, FilterCreditCardCompany, FilterGateway)
else begin
PaymentMethodMapping.SetRange("Post Automatically", true);
if PaymentMethodMapping.FindSet() then
repeat
MarkPostableTransactions(PaymentMethodMapping."Shop Code", PaymentMethodMapping."Credit Card Company", PaymentMethodMapping.Gateway);
until PaymentMethodMapping.Next() = 0;
end;

Rec.MarkedOnly(true);
Rec.SetRange(Shop);
Rec.SetRange(Gateway);
Rec.SetRange("Credit Card Company");
Rec.SetRange("Created At");
Rec.SetRange(Used);
Rec.SetRange("Posted Invoice No.");
end;

local procedure MarkPostableTransactions(FilterShopCode: Code[20]; FilterCreditCardCompany: Text[50]; FilterGateway: Text[30])
begin
Rec.SetRange(Shop, FilterShopCode);
Rec.SetRange(Gateway, FilterGateway);
Rec.SetRange("Credit Card Company", FilterCreditCardCompany);
if Rec.FindSet() then
repeat
Rec.Mark(true);
until Rec.Next() = 0;
end;
}
Loading