-
Notifications
You must be signed in to change notification settings - Fork 445
[Shopify] Automatic Transaction Posting #9525
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
Open
Onat Buyukakkus (onbuyuka)
wants to merge
18
commits into
main
Choose a base branch
from
bugs/620951-shopify-automatic-transaction-posting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6ade014
[Shopify] Automatic Transaction Posting
onbuyuka 43ccc94
Merge branch 'main' of https://github.com/microsoft/BCApps into bugs/…
onbuyuka 9fc5f4a
Merge branch 'main' of https://github.com/microsoft/BCApps into bugs/…
onbuyuka a1dc4d7
Merge branch 'main' of https://github.com/microsoft/BCApps into bugs/…
onbuyuka 34eab3e
Address PR review feedback on Shopify automatic transaction posting
onbuyuka bd28a61
Fix clashing test object ID: use 139415 for Shpfy Auto Post Trans. Test
onbuyuka f4b58b5
Address review feedback: perf, style and test coverage
onbuyuka 2f9d707
Make auto-post test fixture self-contained to fix CI posting setup fa…
onbuyuka b77c22f
Seed number series in auto-post test so first-run shop init succeeds
onbuyuka addd2a6
Revert "Seed number series in auto-post test so first-run shop init s…
onbuyuka 97bba5e
Revert "Make auto-post test fixture self-contained to fix CI posting …
onbuyuka b61a70b
Mark auto-post test as an integration test
onbuyuka 1c5672b
Fix propagation test flake and add skipped-reason diagnostic
onbuyuka 7d9d4bb
Set source code on auto-post test journal template for country locali…
onbuyuka 3e7a814
[Shopify] Address automatic posting review feedback
onbuyuka 73ba397
Merge remote-tracking branch 'origin/main' into bugs/620951-shopify-a…
onbuyuka 7e2fbbc
[Shopify] Address auto-post review feedback and fix build
onbuyuka ecef587
[Shopify] Fix automatic refund posting review findings
onbuyuka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/Apps/W1/Shopify/App/src/Transactions/Codeunits/ShpfyAutoGenJnlPost.Codeunit.al
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // ------------------------------------------------------------------------------------------------ | ||
| // 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). | ||
| /// Creates a dedicated, single-use journal batch (cloned from the configured one) and builds the general | ||
| /// journal line(s) for a single Shopify order/refund payment transaction into it. It is invoked through | ||
| /// Codeunit.Run so that any failure while creating the batch or building lines is trapped and rolled back | ||
| /// without leaving a batch or line behind. While bound, it also pre-confirms the "posting after working | ||
| /// date" prompt so the automatic posting stays non-interactive. | ||
| /// </summary> | ||
| codeunit 30422 "Shpfy Auto Gen. Jnl.-Post" | ||
| { | ||
| Access = Internal; | ||
| EventSubscriberInstance = Manual; | ||
| TableNo = "Shpfy Order Transaction"; | ||
| Permissions = tabledata "Gen. Journal Batch" = rim; | ||
|
|
||
| trigger OnRun() | ||
| begin | ||
| CreateBatchAndBuildLines(Rec); | ||
| end; | ||
|
|
||
| var | ||
| PaymentMethodMapping: Record "Shpfy Payment Method Mapping"; | ||
| PostingDate: Date; | ||
| IsolatedTemplateName: Code[10]; | ||
| IsolatedBatchName: Code[10]; | ||
|
|
||
| internal procedure SetParameters(NewPaymentMethodMapping: Record "Shpfy Payment Method Mapping"; NewPostingDate: Date) | ||
| begin | ||
| PaymentMethodMapping := NewPaymentMethodMapping; | ||
| PostingDate := NewPostingDate; | ||
| Clear(IsolatedTemplateName); | ||
| Clear(IsolatedBatchName); | ||
| end; | ||
|
|
||
| internal procedure GetIsolatedBatch(var NewTemplateName: Code[10]; var NewBatchName: Code[10]) | ||
| begin | ||
| NewTemplateName := IsolatedTemplateName; | ||
| NewBatchName := IsolatedBatchName; | ||
| end; | ||
|
|
||
| local procedure CreateBatchAndBuildLines(var OrderTransaction: Record "Shpfy Order Transaction") | ||
| var | ||
| SuggestPayments: Report "Shpfy Suggest Payments"; | ||
| begin | ||
| CreateIsolatedBatch(); | ||
| SuggestPayments.SetJournalParameters(IsolatedTemplateName, IsolatedBatchName, PostingDate); | ||
| SuggestPayments.GetOrderTransactions(OrderTransaction); | ||
| SuggestPayments.CreateGeneralJournalLines(); | ||
| end; | ||
|
|
||
| local procedure CreateIsolatedBatch() | ||
| var | ||
| ConfiguredBatch: Record "Gen. Journal Batch"; | ||
| IsolatedBatch: Record "Gen. Journal Batch"; | ||
| begin | ||
| ConfiguredBatch.Get(PaymentMethodMapping."Auto-Post Jnl. Template", PaymentMethodMapping."Auto-Post Jnl. Batch"); | ||
| IsolatedBatch := ConfiguredBatch; | ||
| IsolatedBatch.Name := GetUniqueBatchName(ConfiguredBatch."Journal Template Name"); | ||
| IsolatedBatch.Insert(true); | ||
| IsolatedTemplateName := IsolatedBatch."Journal Template Name"; | ||
| IsolatedBatchName := IsolatedBatch.Name; | ||
| end; | ||
|
|
||
| local procedure GetUniqueBatchName(TemplateName: Code[10]): Code[10] | ||
| var | ||
| ExistingBatch: Record "Gen. Journal Batch"; | ||
| CandidateName: Code[10]; | ||
| begin | ||
| repeat | ||
| CandidateName := CopyStr('SHPFY' + CopyStr(DelChr(Format(CreateGuid()), '=', '{}-'), 1, 5), 1, 10); | ||
| until not ExistingBatch.Get(TemplateName, CandidateName); | ||
| exit(CandidateName); | ||
| end; | ||
|
|
||
| [EventSubscriber(ObjectType::Codeunit, Codeunit::"Gen. Jnl.-Post Batch", 'OnBeforeCheckLine', '', false, false)] | ||
| local procedure PreconfirmWorkingDateOnBeforeCheckLine(var PostingAfterWorkingDateConfirmed: Boolean) | ||
| begin | ||
| PostingAfterWorkingDateConfirmed := true; | ||
| end; | ||
| } |
177 changes: 177 additions & 0 deletions
177
src/Apps/W1/Shopify/App/src/Transactions/Codeunits/ShpfyAutoPostTransactions.Codeunit.al
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| // ------------------------------------------------------------------------------------------------ | ||
| // 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.Document; | ||
| using Microsoft.Sales.History; | ||
|
|
||
| /// <summary> | ||
| /// Codeunit Shpfy Auto Post Transactions (ID 30236). | ||
| /// Automatically posts Shopify order and refund payment transactions as general journal lines when | ||
| /// the related sales invoice or credit memo is posted, provided the transaction's payment method | ||
| /// mapping is configured for automatic posting. Posting is synchronous and best-effort: a failure to | ||
| /// post a payment is logged as a skipped record and never blocks or reverses the document posting. | ||
| /// Each transaction is posted through a dedicated, single-use journal batch so that only the generated | ||
| /// lines are posted and pre-existing lines in the configured batch are never touched. | ||
| /// </summary> | ||
| codeunit 30236 "Shpfy Auto Post Transactions" | ||
| { | ||
| Access = Internal; | ||
| Permissions = tabledata "Gen. Journal Batch" = rimd, | ||
| tabledata "Gen. Journal Line" = rimd; | ||
|
|
||
| internal procedure AutoPostTransactions(SalesInvoiceHeaderNo: Code[20]; SalesCrMemoHeaderNo: Code[20]) | ||
|
onbuyuka marked this conversation as resolved.
onbuyuka marked this conversation as resolved.
|
||
| begin | ||
| if SalesInvoiceHeaderNo <> '' then | ||
| PostOrderTransactions(SalesInvoiceHeaderNo); | ||
| if SalesCrMemoHeaderNo <> '' then | ||
| PostRefundTransactions(SalesCrMemoHeaderNo); | ||
| end; | ||
|
|
||
| local procedure PostOrderTransactions(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; | ||
|
|
||
| // Defer until all sales documents for the order are posted, so a partial invoice can't consume the whole transaction. | ||
| if OpenSalesDocumentExistsForOrder(SalesInvoiceHeader."Shpfy Order Id") then | ||
| exit; | ||
|
|
||
| OrderTransaction.SetRange("Shopify Order Id", SalesInvoiceHeader."Shpfy Order Id"); | ||
| OrderTransaction.SetFilter(Type, '%1|%2', OrderTransaction.Type::Capture, OrderTransaction.Type::Sale); | ||
|
onbuyuka marked this conversation as resolved.
|
||
| PostTransactions(OrderTransaction, SalesInvoiceHeader."Posting Date"); | ||
| end; | ||
|
|
||
| local procedure PostRefundTransactions(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; | ||
|
|
||
| // Defer until all credit memos for the refund are posted. | ||
|
onbuyuka marked this conversation as resolved.
Outdated
|
||
| if OpenSalesDocumentExistsForRefund(SalesCrMemoHeader."Shpfy Refund Id") then | ||
| exit; | ||
|
|
||
| OrderTransaction.SetRange("Refund Id", SalesCrMemoHeader."Shpfy Refund Id"); | ||
| OrderTransaction.SetRange(Type, OrderTransaction.Type::Refund); | ||
| PostTransactions(OrderTransaction, SalesCrMemoHeader."Posting Date"); | ||
| end; | ||
|
|
||
| local procedure OpenSalesDocumentExistsForOrder(ShopifyOrderId: BigInteger): Boolean | ||
| var | ||
| SalesHeader: Record "Sales Header"; | ||
| begin | ||
| SalesHeader.SetRange("Shpfy Order Id", ShopifyOrderId); | ||
| exit(not SalesHeader.IsEmpty()); | ||
| end; | ||
|
|
||
| local procedure OpenSalesDocumentExistsForRefund(ShopifyRefundId: BigInteger): Boolean | ||
| var | ||
| SalesHeader: Record "Sales Header"; | ||
| begin | ||
| SalesHeader.SetRange("Shpfy Refund Id", ShopifyRefundId); | ||
| exit(not SalesHeader.IsEmpty()); | ||
| end; | ||
|
|
||
| local procedure PostTransactions(var OrderTransaction: Record "Shpfy Order Transaction"; PostingDate: Date) | ||
|
onbuyuka marked this conversation as resolved.
|
||
| var | ||
| PaymentMethodMapping: Record "Shpfy Payment Method Mapping"; | ||
| AutoGenJnlPost: Codeunit "Shpfy Auto Gen. Jnl.-Post"; | ||
| begin | ||
| OrderTransaction.SetRange(Status, OrderTransaction.Status::Success); | ||
|
onbuyuka marked this conversation as resolved.
|
||
| OrderTransaction.SetRange(Used, false); | ||
| if not OrderTransaction.FindSet() then | ||
| exit; | ||
|
|
||
| // Bind once per document; unbind after the loop is safe because PostTransaction never raises. | ||
| BindSubscription(AutoGenJnlPost); | ||
| repeat | ||
|
onbuyuka marked this conversation as resolved.
|
||
| if GetAutoPostMapping(OrderTransaction, PaymentMethodMapping) then | ||
| PostTransaction(AutoGenJnlPost, OrderTransaction, PaymentMethodMapping, PostingDate); | ||
| until OrderTransaction.Next() = 0; | ||
| UnbindSubscription(AutoGenJnlPost); | ||
| end; | ||
|
|
||
| local procedure GetAutoPostMapping(OrderTransaction: Record "Shpfy Order Transaction"; var PaymentMethodMapping: Record "Shpfy Payment Method Mapping"): Boolean | ||
| begin | ||
| if not PaymentMethodMapping.Get(OrderTransaction.Shop, OrderTransaction.Gateway, OrderTransaction."Credit Card Company") then | ||
|
onbuyuka marked this conversation as resolved.
Outdated
|
||
| exit(false); | ||
| if not PaymentMethodMapping."Post Automatically" then | ||
| exit(false); | ||
| exit((PaymentMethodMapping."Auto-Post Jnl. Template" <> '') and (PaymentMethodMapping."Auto-Post Jnl. Batch" <> '')); | ||
| end; | ||
|
|
||
| local procedure PostTransaction(var AutoGenJnlPost: Codeunit "Shpfy Auto Gen. Jnl.-Post"; OrderTransaction: Record "Shpfy Order Transaction"; PaymentMethodMapping: Record "Shpfy Payment Method Mapping"; PostingDate: Date) | ||
|
onbuyuka marked this conversation as resolved.
onbuyuka marked this conversation as resolved.
|
||
| var | ||
| GenJournalLine: Record "Gen. Journal Line"; | ||
| GenJnlPostBatch: Codeunit "Gen. Jnl.-Post Batch"; | ||
| TemplateName: Code[10]; | ||
| BatchName: Code[10]; | ||
| ErrorText: Text; | ||
| begin | ||
| // Build into a single-use batch via Codeunit.Run so any failure is trapped and rolled back. | ||
|
onbuyuka marked this conversation as resolved.
Outdated
|
||
| AutoGenJnlPost.SetParameters(PaymentMethodMapping, PostingDate); | ||
| if not AutoGenJnlPost.Run(OrderTransaction) then begin | ||
|
onbuyuka marked this conversation as resolved.
Outdated
|
||
| LogFailure(OrderTransaction, GetLastErrorText()); | ||
| exit; | ||
| end; | ||
|
|
||
| AutoGenJnlPost.GetIsolatedBatch(TemplateName, BatchName); | ||
| if BatchName = '' then | ||
| exit; | ||
|
|
||
| GenJournalLine.SetRange("Journal Template Name", TemplateName); | ||
| GenJournalLine.SetRange("Journal Batch Name", BatchName); | ||
| if not GenJournalLine.FindSet() then begin | ||
| RemoveIsolatedBatch(TemplateName, BatchName); | ||
| exit; | ||
| end; | ||
|
|
||
| // Commit the lines first: batch posting commits internally. | ||
| Commit(); | ||
|
onbuyuka marked this conversation as resolved.
Outdated
|
||
|
|
||
| if GenJnlPostBatch.Run(GenJournalLine) then begin | ||
| RemoveIsolatedBatch(TemplateName, BatchName); | ||
| exit; | ||
| end; | ||
|
|
||
| // Posting failed after commit: drop the batch and log, without affecting the posted document. | ||
| ErrorText := GetLastErrorText(); | ||
| RemoveIsolatedBatch(TemplateName, BatchName); | ||
| LogFailure(OrderTransaction, ErrorText); | ||
| end; | ||
|
|
||
| local procedure RemoveIsolatedBatch(TemplateName: Code[10]; BatchName: Code[10]) | ||
| var | ||
| IsolatedBatch: Record "Gen. Journal Batch"; | ||
| begin | ||
| // Delete cascades to leftover lines; commit so a later rollback can't resurrect the batch. | ||
| if IsolatedBatch.Get(TemplateName, BatchName) then | ||
| IsolatedBatch.Delete(true); | ||
|
onbuyuka marked this conversation as resolved.
Outdated
|
||
| Commit(); | ||
| end; | ||
|
|
||
| local procedure LogFailure(OrderTransaction: Record "Shpfy Order Transaction"; ErrorText: Text) | ||
| var | ||
| Shop: Record "Shpfy Shop"; | ||
| SkippedRecord: Codeunit "Shpfy Skipped Record"; | ||
| begin | ||
| if Shop.Get(OrderTransaction.Shop) then | ||
| SkippedRecord.LogSkippedRecord(OrderTransaction."Shopify Transaction Id", OrderTransaction.RecordId, CopyStr(ErrorText, 1, 250), Shop); | ||
| Commit(); | ||
| end; | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
src/Apps/W1/Shopify/App/src/Transactions/Pages/ShpfyFilterTransactions.Page.al
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // ------------------------------------------------------------------------------------------------ | ||
| // 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). | ||
| /// Request dialog used to filter the Shopify transactions list down to the transactions that are | ||
| /// ready to be posted, optionally narrowed by payment gateway and creation date range. | ||
| /// </summary> | ||
| page 30176 "Shpfy Filter Transactions" | ||
| { | ||
| Caption = 'Filter Postable Transactions'; | ||
| PageType = StandardDialog; | ||
| ApplicationArea = All; | ||
|
onbuyuka marked this conversation as resolved.
|
||
|
|
||
| layout | ||
| { | ||
| area(Content) | ||
| { | ||
| field(Gateway; Gateway) | ||
| { | ||
| Caption = 'Gateway'; | ||
| ToolTip = 'Specifies the transaction gateway to filter transactions by. Leave blank to include all gateways set up for automatic posting.'; | ||
| Editable = false; | ||
|
|
||
| trigger OnAssistEdit() | ||
| var | ||
| PaymentMethodMapping: Record "Shpfy Payment Method Mapping"; | ||
| begin | ||
| PaymentMethodMapping.SetRange("Post Automatically", true); | ||
| if Page.RunModal(Page::"Shpfy Payment Methods Mapping", 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); | ||
| if EndDate <> 0D then | ||
| NewEndDate := CreateDateTime(EndDate, 235959.999T); | ||
| end; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.