Serenity: How to calculate total rows

Created on 24 May 2018  路  12Comments  路  Source: serenity-is/Serenity

I am stacked in how to calculate total rows using TypeScript. Thank you for any help.

community-support question

All 12 comments

Just google it and ask in stackowerflow or similar websites. Not in serenity issues.

Thank you @VictorTomaili
I want to it using the serenity platform.
What I need is to calculate total records and do some process in the Dialog.ts.

But first question is different. You asking for typescript and just calculate total rows. Try add more description on your question for community related help.

Thank you
I have a form that have a field SerialNo. The SerialNo is a string and I want to calculate the serial number automatically using combination of 4 part. Location, code, year and then total records entered for an specific year. Like "BA-012-18-102". I can get the other parts but I am currently stacked with getting total records.

I have an example!

Now, I know what you may think: "WHAT HAVE YOU DONE?!?! MY EYES!!"
In my defense, this is NOT production, this is me trying out typescript for the first time!

A little background, this is a Dialog to calculate a premium for an insurance policy.
Each time you add a new record in the detail or change the combos, I run a function to recalculate the premium.

This is a mini-potpourri of instructions that might help you with something.

        constructor() {
            super();

            this.form.StateId.changeSelect2(() => {
                this.calcPremium();
            });

            this.form.LimitId.changeSelect2(() => {
                this.calcPremium();
            });

            this.form.YearsInBusinessId.changeSelect2(() => {
                this.calcPremium();
            });

            this.form.LossHistoryId.changeSelect2(() => {
                this.calcPremium();
            });

            (this.form.CrossQuoteDetailList.view as any).onDataChanged.subscribe(() => {
                this.calcPremium();
            });

        }

        protected afterLoadEntity() {
            super.afterLoadEntity();

            // these fields are only required in new record mode
            this.form.EffectiveDate.set_maxDate(Q.parseISODateTime("2050-01-01"));

        }
        calcPremium() {
            try {
                var total = 0;
                var VehicleCount = 0;

                var CSLLimit = 0;
                var PIPLimit = 0;
                var UMPDLimit = 0;

                var MPBasePremium = 0;
                var PIPBasePremium = 0;
                var UMBIBasePremium = 0;
                var UMPDBasePremium = 0;

                var ratesForFleet = [0, -5, -10, -15, -25];
                var ratesForMinor = [0, 5, 10, 15];
                var ratesForMajor = [0, 10, 20];

                var rateYears = 0;
                var rateLossHistory = 0;

                rateYears = Q.first(Default.CrossYearsRow.getLookup().items, x => x.CrossYearsId.toString() == this.form.YearsInBusinessId.value).Value;

                rateLossHistory = Q.first(Default.CrossLossHistoryRow.getLookup().items, x => x.CrossLossHistoryId.toString() == this.form.LossHistoryId.value).Value;

                var rateFleetId = 0;
                var rateFleet = 0;

                var rateMinor = 0;
                var rateMajor = 0;

                if (this.form.LimitId.value == "1") {
                    CSLLimit = 750000;
                } else {
                    CSLLimit = 1000000;
                }

                if (this.form.StateId.value == "3") {
                    MPBasePremium = 42;
                    UMBIBasePremium = 23;
                }

                if (this.form.StateId.value == "5") {
                    UMPDLimit = 3500;
                    MPBasePremium = 61;
                    UMBIBasePremium = 12;
                    UMPDBasePremium = 2
                }

                if (this.form.StateId.value == "43") {
                    PIPLimit = 2500;
                    UMPDLimit = 25000;
                    MPBasePremium = 10;
                    PIPBasePremium = 5;
                    UMBIBasePremium = 19;
                    UMPDBasePremium = 20;
                }

                for (var k of this.form.CrossQuoteDetailList.getItems()) {
                    VehicleCount = VehicleCount + k.NoOfVehicles;
                }

                if (VehicleCount <= 4) {
                    rateFleetId = 1;
                } else {
                    if (VehicleCount <= 10) {
                        rateFleetId = 2;
                    } else {
                        if (VehicleCount <= 20) {
                            rateFleetId = 3;
                        } else {
                            if (VehicleCount <= 50) {
                                rateFleetId = 4;
                            } else {
                                rateFleetId = 5;
                            }
                        }
                    }
                }

                for (var k of this.form.CrossQuoteDetailList.getItems()) {

                    var cslPremium = 0;
                    var MPPremium = 0;
                    var PIPPremium = 0;
                    var UMBIPremium = 0
                    var UMPDPremium = 0;
                    cslPremium = Q.first(Default.CrossCslRateRow.getLookup().items, x => x.StateId.toString() == this.form.StateId.value && x.LimitId.toString() == this.form.LimitId.value && x.RadiusId == k.RadiusId && x.SizeId == k.SizeId).BaseRate;

                    MPPremium = MPBasePremium;
                    PIPPremium = PIPBasePremium;
                    UMBIPremium = UMBIBasePremium;
                    UMPDPremium = UMPDBasePremium;

                    rateFleet = Q.first(Default.CrossFleetRow.getLookup().items, x => x.CrossFleetId == (+ rateFleetId)).Value;
                    if (this.form.StateId.value == "43") {
                        rateMinor = 0;
                    } else {
                        rateMinor = Q.first(Default.CrossMinorRow.getLookup().items, x => x.CrossMinorId == k.MinorId).Value;
                    }

                    rateMajor = Q.first(Default.CrossMajorRow.getLookup().items, x => x.CrossMajorId == k.MajorId).Value;

                    k.CslLimit = CSLLimit;
                    k.MpLimit = 20000;
                    k.PipLimit = PIPLimit;
                    k.UmbiLimit = 60000;
                    k.UmpdLimit = UMPDLimit;

                    k.CslPremiumAmt = cslPremium;
                    k.MpPremiumAmt = MPPremium;
                    k.PipPremiumAmt = PIPPremium;
                    k.UmbiPremiumAmt = UMBIPremium;
                    k.UmpdPremiumAmt = UMPDPremium;

                    cslPremium = cslPremium + Math.round(cslPremium * rateYears / 100);
                    cslPremium = cslPremium + Math.round(cslPremium * rateLossHistory / 100);
                    cslPremium = cslPremium + Math.round(cslPremium * rateFleet / 100);
                    cslPremium = cslPremium + Math.round(cslPremium * rateMinor / 100);
                    cslPremium = cslPremium + Math.round(cslPremium * rateMajor / 100);

                    MPPremium = MPPremium + Math.round(MPPremium * rateYears / 100);
                    MPPremium = MPPremium + Math.round(MPPremium * rateLossHistory / 100);
                    MPPremium = MPPremium + Math.round(MPPremium * rateFleet / 100);
                    MPPremium = MPPremium + Math.round(MPPremium * rateMinor / 100);
                    MPPremium = MPPremium + Math.round(MPPremium * rateMajor / 100);

                    PIPPremium = PIPPremium + Math.round(PIPPremium * rateYears / 100);
                    PIPPremium = PIPPremium + Math.round(PIPPremium * rateLossHistory / 100);
                    PIPPremium = PIPPremium + Math.round(PIPPremium * rateFleet / 100);
                    PIPPremium = PIPPremium + Math.round(PIPPremium * rateMinor / 100);
                    PIPPremium = PIPPremium + Math.round(PIPPremium * rateMajor / 100);

                    UMBIPremium = UMBIPremium + Math.round(UMBIPremium * rateYears / 100);
                    UMBIPremium = UMBIPremium + Math.round(UMBIPremium * rateLossHistory / 100);
                    UMBIPremium = UMBIPremium + Math.round(UMBIPremium * rateFleet / 100);
                    UMBIPremium = UMBIPremium + Math.round(UMBIPremium * rateMinor / 100);
                    UMBIPremium = UMBIPremium + Math.round(UMBIPremium * rateMajor / 100);

                    UMPDPremium = UMPDPremium + Math.round(UMPDPremium * rateYears / 100);
                    UMPDPremium = UMPDPremium + Math.round(UMPDPremium * rateLossHistory / 100);
                    UMPDPremium = UMPDPremium + Math.round(UMPDPremium * rateFleet / 100);
                    UMPDPremium = UMPDPremium + Math.round(UMPDPremium * rateMinor / 100);
                    UMPDPremium = UMPDPremium + Math.round(UMPDPremium * rateMajor / 100);

                    k.CslPremiumAmt = cslPremium * k.NoOfVehicles;
                    k.MpPremiumAmt = MPPremium * k.NoOfVehicles;
                    k.PipPremiumAmt = PIPPremium * k.NoOfVehicles;
                    k.UmbiPremiumAmt = UMBIPremium * k.NoOfVehicles;
                    k.UmpdPremiumAmt = UMPDPremium * k.NoOfVehicles;

                    total += k.CslPremiumAmt + k.MpPremiumAmt + k.PipPremiumAmt + k.UmbiPremiumAmt + k.UmpdPremiumAmt || 0;
                }

                this.form.Premium.value = total;
            }
            catch (e) {
                return null;
            }
        }

Hope this helps.

Hi @gguadalupe

Thank you very much for the response. This should be quit helpful for me as I am not good in both Serernity and TypeScript.

I will try to use and will come back if I need more support.

Hi @gguadalupe
Thank you again

I was able to create the following code for getting serial number but for one part need help.

    getSerialNumber() {
        try {
            var totalRows = 0;

            var provinceAbbr = null;
            var currUser = 0;
            var year = 0;
            var getDate = new Date();

            var SrNo = null;


            provinceAbbr = Q.first(Geography.HealthfacilitiesRow.getLookup().items, x => x.UserId == this.form.UserId.value).PCodePAbbrv;
            currUser = Q.first(Geography.HealthfacilitiesRow.getLookup().items, x => x.UserId == this.form.UserId.value).PCode;
            year = getDate.getUTCFullYear();


            for (var k of Add.NutritionDataRow.getLookup().items) {
                totalRows = totalRows + 1;
            }

            SrNo = provinceAbbr + '-' + '0' + currUser + '-' + year + '0' + totalRows;
            this.form.SrNo = SrNo;

        }
        catch (e) {
            return null;
        }
    }

With the following I need to first filter records for a specif user and year and iterate over the filter record.

            for (var k of Add.NutritionDataRow.getLookup().items) {
                totalRows = totalRows + 1;
            }

But could not figure it how to make it.

Well, as a non web developer, I don't know how to do that.
BUT! as a good copy-paster, let's think about this!

Usually what I do is to "hunt" for the piece of code in the examples (#puregold!)
There is so many examples we can choose from, but one draw my attention.
Check in the basic examples, FilteredLookupInDetailDialog.ts
It is NOT exactly what you need, but it give you an idea of how to filter data in TS using the Serenity fw.

            this.form.CategoryID.change(e => {
                this.form.DetailList.categoryID = Q.toId(this.form.CategoryID.value);
            });

Also check the other example: ProduceSeafoodCategoryEditor.ts

        protected getItems(lookup: Q.Lookup<Northwind.CategoryRow>) {
            return super.getItems(lookup).filter(x =>
                x.CategoryName === 'Produce' || x.CategoryName === 'Seafood');
        }

Happy Hunting!

Thank you for the very good hunting tip. 馃憤

I am still stacked.
I have the following code

    getSerialNumber() {
        try {

            var provinceAbbr = null;

            var SrNo = null;

            provinceAbbr = Q.first(Geography.HealthfacilitiesRow.getLookup().items, x => x.FacilityId.toString() === this.form.FacilityId.value).FacilityId;
            if (provinceAbbr == null) {
                SrNo = null;
            }
            else {
                SrNo = provinceAbbr;
            }

            this.form.SrNo.value = SrNo;
        }
        catch (e) {
            return null;
        }

in this line of code

provinceAbbr = Q.first(Geography.HealthfacilitiesRow.getLookup().items, x => x.FacilityId.toString() === this.form.FacilityId.value).FacilityId;

when I add facilityId it returns a value but when I want to retrieve other like provinceabbrv it returns nothing. Am i doing anything wrong. I want to retreive is provinceabbrv.

I suppose you have the FacilityId in you dialog?
And also, you have a field called provinceAbbr in the HealthFacilitiesRow (also pay attention to the letter case of your code)

provinceAbbr = Q.first(Geography.HealthIS-THIS-UPPER-CASE-F?acilitiesRow.getLookup().items, x => x.FacilityId.toString() === this.form.FacilityId.value).provinceAbbr;

Also check your HealthFacilitiesRow object, you need to have the LookupScript attribute and the LookupInclude in the proviceAbbr.

Thank you @gguadalupe
The problem was with the LookupInclude.
It is working perfectly now and thank you for your support.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Amitloh picture Amitloh  路  3Comments

ahsansolution picture ahsansolution  路  3Comments

JohnRanger picture JohnRanger  路  3Comments

moostafaa picture moostafaa  路  3Comments

john20xdoe picture john20xdoe  路  3Comments