Hello Community,
When clicking on the 'New' button in the Invoice lookup a new Invoice with all the details from Installment plan detail is being created and auto filled . The logic behind this implementation is helpful for me to implement the same requirement for a custom object.
https://academy.creatio.com/documents/sales-enterprise/7-16/add-invoice-based-installment-plan-step#XREF_95168
Thanks in Advance.
Regards,
Sivaranjani
Like
Hi,
You need to study the following chain in the debugger:
1) SupplyPaymentPageV2 schema "Invoice" lookup confguration:
"Invoice": {
                    lookupListConfig: {
                        columns: ["PaymentStatus"],
                        filter: function() {
                            return this.getInvoiceLookupFilter();
                        },
                        updateViewModel: function() {
                            if (!this.Terrasoft.Features.getIsEnabled("UseLookupInvoiceForSupplyPaymentDetail")) {
                                return;
                            }
                            this.defaultModeActionButtonClickOld = this.defaultModeActionButtonClick.bind(this);
                            this.defaultModeActionButtonClick = function(tag) {
                                if (tag === "add") {
                                    Terrasoft.LookupUtilities.HideModalBox();
                                    var info = this.getLookupInfo();
                                    info._publishGenerateInvoice();
                                } else {
                                    this.defaultModeActionButtonClickOld(tag);
                                }
                            };
                            this.initCanEdit = function() {
                                this.set("canEdit", false);
                            };
                        }
                    }
                }
in this code we can see that the logic of the "New" button was overridden that calls the _publishGenerateInvoice method once the "New" button is clicked.
2) _publishGenerateInvoice - this is a method from the SupplyPaymentPageV2 schema that performs the "CreateSupplyPaymentInvoice"  sanbox message sending (BIDIRECTIONAL message type):
...
messages:
...
"CreateSupplyPaymentInvoice": {
                    mode: this.Terrasoft.MessageMode.PTP,
                    direction: this.Terrasoft.MessageDirectionType.BIDIRECTIONAL
                }
...
methods:
...
_publishGenerateInvoice: function() {
                    this.sandbox.publish("CreateSupplyPaymentInvoice", null, [this.sandbox.id]);
                },
...
3) The subscription to the "CreateSupplyPaymentInvoice" message is performed in the SupplyPaymentDetailV2 schema in the subscribeSandboxEvents method:
this.sandbox.subscribe("CreateSupplyPaymentInvoice", function() {
                                var row = this.getActiveRow();
                                this.asyncGenerateInvoices([row]);
                            }, this, [this.sandbox.id]);
4) Once the message is received the logic in the asyncGenerateInvoices method is executed for all active rows selected in the detail.
5) asyncGenerateInvoices - method from the SupplyPaymentDetailV2 schema:
asyncGenerateInvoices: function(collection) {
                        collection = collection || [];
                        this.index = 0;
                        this.existInvoiceCount = 0;
                        collection.forEach(function(item) {
                            var methods = [];
                            methods.push(function(next) {
                                this.generateInvoice(next, item);
                            });
                            methods.push(this.hideBodyMask);
                            methods.push(this);
                            this.showBodyMask();
                            Terrasoft.chain.apply(this, methods);
                        }, this);
                    },
executes the generateInvoice method for all received active rows that don't have an invoice.
6) generateInvoice is also the method from the SupplyPaymentDetailV2 schema:
generateInvoice: function(next, item) {
                        var invoice = item.get("Invoice");
                        if ((invoice === null && this.getIsFeatureEnabled("UseLookupInvoiceForSupplyPaymentDetail")) ||
                            (invoice && this.Terrasoft.isGUID(invoice.value))) {
                            this.existInvoiceCount++;
                            this.validateResult({
                                error: result.ExistInvoice,
                                existInvoiceCount: this.existInvoiceCount
                            });
                            next();
                        } else {
                            this.setAdditionAttributes(next, item);
                        }
                    },
performs data validation and executes the setAdditionAttributes method.
7) setAdditionAttributes method from the SupplyPaymentDetailV2 schema:
setAdditionAttributes: function(next, item) {
                        this.getIncrementCode(function(result) {
                            item.set("Number", result);
                            item.set("StartDate", new Date());
                            var esq = this.getSupplyPaymentProductEntitySchemaQuery(item.get("Id"));
                            esq.getEntityCollection(function(response) {
                                if (response.success) {
                                    const products = response.collection;
                                    this.processProducts(products);
                                    item.set("ProductCollection", products);
                                    this.insertInvoice(next, item);
                                }
                            }, this);
                        }, this);
                    },
calls the insertInvoice method for each product in the supply payment (see the getSupplyPaymentProductEntitySchemaQuery method logic).
8) insertInvoice method performs the actual invoices insert:
insertInvoice: function(next, item) {
                        var insert = this.getInvoiceInsertQuery(item);
                        insert.execute(function(response) {
                            if (response.success) {
                                var config = {
                                    invoiceId: response.id,
                                    id: item.get("Id"),
                                    amount: item.get("AmountPlan"),
                                    number: item.get("Number"),
                                    startDate: item.get("StartDate"),
                                    products: item.get("ProductCollection")
                                };
                                var invoice = {
                                    displayValue: item.get("Number"),
                                    value: response.id
                                };
                                item.set("Invoice", invoice);
                                if (config.products.getCount() === 0) {
                                    this.updateSupplyPaymentElement(next, config);
                                } else {
                                    this.insertInvoiceProducts(next, config);
                                }
                            } else {
                                var message = this.generateInvoiceErrorMessage(response);
                                this.showErrorMessage(message);
                            }
                        }, this);
                    },
with products into invoices (according to the logic from the insertInvoiceProducts method here).
So the complete chain is: SupplyPaymentPageV2 schema "Invoice" lookup modification -> _publishGenerateInvoice -> asyncGenerateInvoices -> generateInvoice -> setAdditionAttributes -> insertInvoice. There is no article to share unfortunately, the code itself should be studied carefully to implement the same functionality in the custom detail.
