Monday, September 12, 2016

Sales Order Total Discount ax 2012

//Sales Order Total Discount ax 2012
//Scenario -1 - spliting in line level
switch (_fieldId)
    {
        case fieldNum(SalesTable, TotalSalesDiscount):
            if(this.TotalSalesDiscount > 0)
            {
                this.write();
                sumSalesLine.clear();
                Select sum(SalesPrice),sum(SalesQty),sum(LineAmount) from sumSalesLine
                        where sumSalesLine.SalesId == this.SalesId;
                ttsBegin;
                salesline.clear();
                while select forUpdate salesline
                    where salesline.SalesId == this.SalesId
                        && salesline.SalesStatus == SalesStatus::Backorder
                {
                    salesline.FccSalesPrice = decRound((this.TotalSalesDiscount) * ((((salesline.LineAmount)/sumSalesLine.LineAmount)*100)/100),3);
                    salesline.update();
                }
                ttsCommit;
                sumSalesLine.clear();
                Select sum(SalesPrice),sum(SalesQty),sum(FccSalesPrice),sum(LineAmount) from sumSalesLine
                        where sumSalesLine.SalesId == this.SalesId;
                saleeslinerecid.clear();
                select RecId from saleeslinerecid order by RecId Desc;
                ttsBegin;
                salesline.clear();
                while select forUpdate salesline
                    where salesline.SalesId == this.SalesId
                        && salesline.SalesStatus == SalesStatus::Backorder
                {
                    if(sumSalesLine.FccSalesPrice != this.TotalSalesDiscount)
                    {
                        if(saleeslinerecid.RecId == salesline.recid)
                        {
                            salesline.FccSalesPrice = salesline.FccSalesPrice +(this.TotalSalesDiscount - sumSalesLine.FccSalesPrice);
                        }
                        else
                        {
                            salesline.FccSalesPrice = salesline.FccSalesPrice;
                        }
                    }
                    salesline.LinePercent = 0;
                    salesline.LineDisc = salesline.FccSalesPrice/salesline.SalesQty;
                    salesline.modifiedField(fieldNum(SalesLine,LineDisc));
                    salesline.update();
                }
                ttsCommit;
                this.dataSource().reread();
            }
            else
            {
                this.write();
                ttsBegin;
                while select forUpdate salesline
                    where salesline.SalesId == this.SalesId
                            && salesline.SalesStatus == SalesStatus::Backorder
                {
                    salesline.LinePercent = 0;
                    salesline.LineDisc = 0;
                    salesline.FccSalesPrice = 0;
                    salesline.modifiedField(fieldNum(SalesLine,LineDisc));
                    salesline.update();
                }
                ttsCommit;
                this.dataSource().reread();
            }
            break;
    }
//Scenario -2
Populating total discout Amount by finding discount percent - Default Total disc percent field to affect header discount
//Scenario -3
switch (_fieldId)
    {
        case fieldNum(SalesTable, TotalSalesDiscount):
                if(this.TotalSalesDiscount > 0)
                {
                    Select sum(LineAmount) from sumSalesLine
                            where sumSalesLine.SalesId == this.SalesId;
                    ttsBegin;
                    while select forUpdate salesline
                        where salesline.SalesId == this.SalesId
                    {
                        salesline.LineDisc = (this.TotalSalesDiscount) * ((((salesline.LineAmount)/sumSalesLine.LineAmount)*100)/100);
                        salesline.LineAmount = (salesline.SalesQty * salesline.SalesPrice) - salesline.LineDisc;
                        salesline.update();
                    }
                    ttsCommit;
                    this.dataSource().research(true);
                }
            break;
    }

No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...