コード例 #1
0
ファイル: QuoteChargeHandler.cs プロジェクト: weedkiller/dms
        //Created By : Jerome Anthony Gerero, Created On: 3/11/2016

        /*Modified By: Raphael Herrera, Modified On: 9/19/2016
         * Modification Purpose: Compute for Freight & Handling Charges
         */
        public Entity SetQuoteTotalChargesAmount(Entity quoteChargeEntity, String message)
        {
            _tracingService.Trace("Started SetQuoteTotalChargesAmount method..");

            var quoteId = quoteChargeEntity.GetAttributeValue <EntityReference>("gsc_quoteid") != null
                ? quoteChargeEntity.GetAttributeValue <EntityReference>("gsc_quoteid").Id
                : Guid.Empty;

            var actualcost = quoteChargeEntity.Contains("gsc_actualcost")
                ? quoteChargeEntity.GetAttributeValue <Money>("gsc_actualcost")
                : new Money(0);

            if (actualcost.Value == 0)
            {
                Entity quoteChargeToUpdate = _organizationService.Retrieve(quoteChargeEntity.LogicalName, quoteChargeEntity.Id,
                                                                           new ColumnSet("gsc_actualcost"));

                quoteChargeToUpdate["gsc_actualcost"] = new Money(0);

                _organizationService.Update(quoteChargeToUpdate);
            }

            Decimal totalChargesAmount = 0;
            Decimal totalFreightAmount = 0;

            //Retrieve Applied Charges records with the same Quote
            EntityCollection quoteChargeRecords = CommonHandler.RetrieveRecordsByOneValue("gsc_cmn_quotecharge", "gsc_quoteid", quoteId, _organizationService, null, OrderType.Ascending,
                                                                                          new[] { "gsc_free", "gsc_actualcost", "gsc_chargetype" });

            //Retrieve Quote record from Quote field value
            EntityCollection quoteRecords = CommonHandler.RetrieveRecordsByOneValue("quote", "quoteid", quoteId, _organizationService, null, OrderType.Ascending,
                                                                                    new[] { "gsc_totalchargesamount", "gsc_othercharges", "statecode", "gsc_totalcashoutlay", "gsc_downpayment", "gsc_chattelfee", "gsc_productid",
                                                                                            "gsc_insurance", "gsc_freightandhandling", "gsc_ccaddons", "gsc_totaldiscount", "gsc_unitprice", "gsc_colorprice", "customerid", "gsc_paymentmode",
                                                                                            "gsc_netprice", "gsc_accessories", "gsc_vatablesales", "gsc_vatexemptsales", "gsc_zeroratedsales", "gsc_totalsales", "gsc_vatamount", "gsc_totalamountdue",
                                                                                            "gsc_amountfinanced", "gsc_totalamountfinanced", "gsc_downpaymentamount", "gsc_lessdiscountaf" });

            _tracingService.Trace("Quote Charge records retrieved: " + quoteChargeRecords.Entities.Count);

            if (quoteChargeRecords != null && quoteChargeRecords.Entities.Count > 0)
            {
                foreach (var quoteCharge in quoteChargeRecords.Entities)
                {
                    if (quoteCharge.Contains("gsc_actualcost"))
                    {
                        // charge type is freight charges
                        if (quoteCharge.GetAttributeValue <OptionSetValue>("gsc_chargetype").Value == 100000000)
                        {
                            totalFreightAmount += quoteCharge.GetAttributeValue <Boolean>("gsc_free")
                            ? Decimal.Zero
                            : quoteCharge.GetAttributeValue <Money>("gsc_actualcost").Value;
                        }
                        //Other charge type
                        else
                        {
                            totalChargesAmount += quoteCharge.GetAttributeValue <Boolean>("gsc_free")
                            ? Decimal.Zero
                            : quoteCharge.GetAttributeValue <Money>("gsc_actualcost").Value;
                        }

                        _tracingService.Trace("Toal Freight: " + totalFreightAmount + " Total Charges:" + totalChargesAmount);
                    }
                }

                if (quoteChargeEntity.Contains("gsc_actualcost") && message.Equals("Delete"))
                {
                    _tracingService.Trace("Message is Delete...");
                    // charge type is freight charges
                    if (quoteChargeEntity.GetAttributeValue <OptionSetValue>("gsc_chargetype").Value == 100000000)
                    {
                        totalFreightAmount = totalFreightAmount - quoteChargeEntity.GetAttributeValue <Money>("gsc_actualcost").Value;
                    }
                    //Other charge type
                    else
                    {
                        totalChargesAmount = totalChargesAmount - quoteChargeEntity.GetAttributeValue <Money>("gsc_actualcost").Value;
                    }
                }
            }

            if (quoteRecords != null && quoteRecords.Entities.Count > 0 && quoteRecords.Entities[0].GetAttributeValue <OptionSetValue>("statecode").Value == 0)
            {
                QuoteHandler quoteHandler = new QuoteHandler(_organizationService, _tracingService);

                Entity quote = quoteRecords.Entities[0];

                quote["gsc_totalchargesamount"] = new Money(totalChargesAmount + totalFreightAmount);
                quote["gsc_othercharges"]       = new Money(totalChargesAmount);
                quote["gsc_freightandhandling"] = new Money(totalFreightAmount);

                var paymentmode = quote.Contains("gsc_paymentmode")
                  ? quote.GetAttributeValue <OptionSetValue>("gsc_paymentmode").Value
                  : Decimal.Zero;

                //Compute Net Price
                quote["gsc_netprice"] = new Money(quoteHandler.ComputeNetPrice(quote));

                //Compute VAT
                quote = quoteHandler.ComputeVAT(quote);

                quote["gsc_totalcashoutlay"] = new Money(quoteHandler.ComputeCashLayout(quote));

                var amountfinanced = Decimal.Zero;

                //Financing
                if (paymentmode == 100000001 || (paymentmode == 100000002 && quoteHandler.CheckifQuoteHasDownPayment(quote) == true))
                {
                    amountfinanced = quoteHandler.ComputeAmountFinanced(quote);
                    quote["gsc_amountfinanced"]      = new Money(amountfinanced);
                    quote["gsc_totalamountfinanced"] = new Money(amountfinanced);
                }
                else
                {
                    quote["gsc_amountfinanced"]      = null;
                    quote["gsc_totalamountfinanced"] = new Money(amountfinanced);
                }

                _organizationService.Update(quote);
                _tracingService.Trace("Updated quote entity...");

                return(quote);
            }
            _tracingService.Trace("Ended SetQuoteTotalChargesAmount method..");
            return(quoteChargeEntity);
        }
コード例 #2
0
        //Created By: Raphael Herrera, Created On: 9/19/2016

        /*Purpose: Set CC Add-Ons Amount in Quote Entity
         * Registration Details:
         * Event/Message:
         *      Post/Create: QuoteCabChassis
         *      Pre/Delete: QuoteCabChassis
         *      Post/Update: QuoteCabChassis
         *      Post/Update: quote gsc_paymentmode
         * Primary Entity: gsc_sls_quotecabchassis
         */
        public void SetCCAddOnAmount(Entity quoteCabChassis, String message)
        {
            _tracingService.Trace("Started SetCCAddOnAmount Method...");
            var quoteId = quoteCabChassis.Contains("gsc_quoteid") ? quoteCabChassis.GetAttributeValue <EntityReference>("gsc_quoteid").Id
                : Guid.Empty;
            Decimal ccAddOns = 0;

            EntityCollection quoteCollection = CommonHandler.RetrieveRecordsByOneValue("quote", "quoteid", quoteId, _organizationService, null, OrderType.Ascending,
                                                                                       new[] { "gsc_ccaddons", "gsc_netprice", "gsc_totaldiscount", "gsc_unitprice", "gsc_colorprice", "gsc_freightandhandling", "gsc_paymentmode", "gsc_branchid",
                                                                                               "gsc_vatablesales", "gsc_vatexemptsales", "gsc_zeroratedsales", "gsc_totalsales", "gsc_vatamount", "gsc_totalamountdue", "gsc_insurance", "gsc_othercharges", "customerid"
                                                                                               , "gsc_accessories", "gsc_productid" });

            _tracingService.Trace("Quote Records retrieved: " + quoteCollection.Entities.Count);
            if (quoteCollection.Entities.Count > 0)
            {
                Entity quoteEntity = quoteCollection.Entities[0];

                Decimal paymentMode = quoteEntity.Contains("gsc_paymentmode") ? quoteEntity.GetAttributeValue <OptionSetValue>("gsc_paymentmode").Value
                    : 0;

                //Retrieve all quote cab chassis related to quote
                EntityCollection quoteCabChassisCollection = CommonHandler.RetrieveRecordsByOneValue("gsc_sls_quotecabchassis", "gsc_quoteid", quoteId, _organizationService,
                                                                                                     null, OrderType.Ascending, new[] { "gsc_amount", "gsc_financing" });

                _tracingService.Trace("Quote Cab Chassis Records Retrieved: " + quoteCabChassisCollection.Entities.Count);
                if (quoteCabChassisCollection.Entities.Count > 0)
                {
                    //Get total cc add on price that are for financing...
                    foreach (Entity quoteCabChassisEntity in quoteCabChassisCollection.Entities)
                    {
                        if (quoteCabChassisEntity.Contains("gsc_amount"))
                        {
                            ccAddOns += quoteCabChassisEntity.GetAttributeValue <Money>("gsc_amount").Value;
                            _tracingService.Trace("CC Add Ons Amount: " + ccAddOns);
                        }
                    }

                    //Subtract sellprice of deleted.
                    if (quoteCabChassis.Contains("gsc_amount") && message.Equals("Delete"))
                    {
                        _tracingService.Trace("Message is Delete...");
                        ccAddOns = ccAddOns - (Decimal)quoteCabChassis.GetAttributeValue <Money>("gsc_amount").Value;
                    }

                    quoteEntity["gsc_ccaddons"] = new Money(ccAddOns);

                    #region Recalculate net price

                    QuoteHandler quoteHandler = new QuoteHandler(_organizationService, _tracingService);
                    quoteEntity["gsc_netprice"] = new Money(quoteHandler.ComputeNetPrice(quoteEntity));
                    quoteEntity = quoteHandler.ComputeVAT(quoteEntity);

                    var paymentmode = quoteEntity.Contains("gsc_paymentmode")
                        ? quoteEntity.GetAttributeValue <OptionSetValue>("gsc_paymentmode").Value
                        : Decimal.Zero;
                    var amountfinanced = Decimal.Zero;

                    //Financing
                    if (paymentmode == 100000001 || paymentmode == 100000001)
                    {
                        amountfinanced = quoteHandler.ComputeAmountFinanced(quoteEntity);
                        quoteEntity["gsc_amountfinanced"]      = new Money(amountfinanced);
                        quoteEntity["gsc_totalamountfinanced"] = new Money(amountfinanced);
                    }

                    #endregion

                    if (ccAddOns >= 0)
                    {
                        _organizationService.Update(quoteEntity);
                        _tracingService.Trace("CC Add Ons Amount:" + ccAddOns + ". Updated quote entity...");
                    }
                    else
                    {
                        _tracingService.Trace("CC add on is negative...");
                    }
                }
            }
            _tracingService.Trace("ending SetCCAddOnAmount Entity");
        }