Share purchase agreement to franchise company. - Song Nghia - Microsoft Dynamics Partner

Song Nghia - Microsoft Dynamics Partner

Song Nghia - Microsoft Dynamics Partner

Breaking

Tuesday, May 12, 2020

Share purchase agreement to franchise company.

Share purchase agreement to franchise company.


Song Nghia - Technical consulant

Idea: Franchise company want to get price from source company, so we can understand that franchise company have default vendor is source company.

class LAW_PurchaseTradeAgreementJournalShare extends RunBaseBatch
{
    DialogField fieldCompany;
    companyId   _companyId;
    CompanyId   _currentCompanyId;
    PriceDiscAdmTable   _currentPriceDiscAdmTable;
    Map mapTableGroupVendor = new Map(Types::String, Types::String);
    public Object dialog()
    {
        Dialog dialog;
        ;
        dialog = super();

        dialog.caption("Select company to share");

        fieldCompany   =  dialog.addField(extendedTypeStr(companyId), "Company Id");

        return dialog;
    }

    public boolean getFromDialog()
    {
        // Retrieve values from Dialog
        _companyId = fieldCompany.value();
        return super();
    }

    public PriceDiscAdmTable parmPriceDiscAdmTable(PriceDiscAdmTable priceDiscAdmTable)
    {
        _currentPriceDiscAdmTable = priceDiscAdmTable;
        return _currentPriceDiscAdmTable;
    }

    public void run()
    {
        PriceDiscAdmTable   destinationDiscAdmTable;
        PriceDiscAdmTrans   priceDiscAdmTrans, priceDiscAdmTransGroup, priceDiscAdmTransOrder, destinationPriceDiscAdmTrans;
        PriceDiscTable      priceDiscTable;
        container           contRecId;
        int i;

        _currentCompanyId = curExt();
        try
        {
            ttsbegin;

            changecompany(_companyId)
            {
                destinationDiscAdmTable = this.createPurchTradeAgreementTable(_currentPriceDiscAdmTable);
            }
        
            if(destinationDiscAdmTable)
            {
                //Get smallest price???

                while select ItemRelation from priceDiscAdmTransGroup
                    group by  priceDiscAdmTransGroup.ItemRelation
                     where priceDiscAdmTransGroup.JournalNum == _currentPriceDiscAdmTable.JournalNum
                {
                    select firstonly priceDiscAdmTransOrder
                        order by priceDiscAdmTransOrder.Amount asc
                        where priceDiscAdmTransOrder.JournalNum == _currentPriceDiscAdmTable.JournalNum
                        && priceDiscAdmTransOrder.ItemRelation == priceDiscAdmTransGroup.ItemRelation;

                    contRecId += priceDiscAdmTransOrder.RecId;
                }
                
                for(i = 1; i <= conLen(contRecId); i++)
                {
                    select firstonly priceDiscAdmTrans
                        where priceDiscAdmTrans.RecId == conPeek(contRecId, i);
                    if(priceDiscAdmTrans.PriceDiscTableRef)
                    {
                        //quay ve
                        select firstonly  priceDiscTable
                        where priceDiscTable.RecId == priceDiscAdmTrans.PriceDiscTableRef;
                    }
                    changecompany(_companyId)
                    {
                        destinationPriceDiscAdmTrans = this.createPurchTradeAgreementTrans(destinationDiscAdmTable, priceDiscAdmTrans, priceDiscTable);
                        this.insertTradeAdmTransMap(priceDiscAdmTrans, destinationPriceDiscAdmTrans);
                    }
                }

                /*while select priceDiscAdmTrans
                where priceDiscAdmTrans.JournalNum == _currentPriceDiscAdmTable.JournalNum
                {
                    if(priceDiscAdmTrans.PriceDiscTableRef)
                    {
                        //quay ve
                        select firstonly  priceDiscTable
                        where priceDiscTable.RecId == priceDiscAdmTrans.PriceDiscTableRef;
                    }
                    changecompany(_companyId)
                    {
                        destinationPriceDiscAdmTrans = this.createPurchTradeAgreementTrans(destinationDiscAdmTable, priceDiscAdmTrans, priceDiscTable);
                        this.insertTradeAdmTransMap(priceDiscAdmTrans, destinationPriceDiscAdmTrans);
                    }
                }*/
                changecompany(_companyId)
                {
                    this.priceDiscAdmCheckPost(destinationDiscAdmTable);
                }
            }

            this.updateSourceTradeAgreement(_currentPriceDiscAdmTable);
           
            ttscommit;
        }
        catch
        {
            throw error("@LAW:LAW_SelectCompany");
        }
    }

    public PriceDiscAdmTrans createPurchTradeAgreementTrans(PriceDiscAdmTable newPriceDiscAdmTable, PriceDiscAdmTrans _priceDiscAdmTrans, PriceDiscTable _sourcePriceDiscTable)
    {
        PriceDiscAdmTrans           priceDiscAdmTrans, originalPriceDiscAdmTrans;
        PriceDiscTable              priceDiscTable;
        LAW_PriceDiscADmTransMap    priceDiscTransShare;
        MapEnumerator       mapEnumerator;
        int i = 1;
        //container contGroup = this.findGroupOfDefaultVendor(_priceDiscAdmTrans.relation);
        //container contDefault = this.findAllDefaultVendor();
        priceDiscAdmTrans.clear();
        priceDiscAdmTrans.JournalNum    = newPriceDiscAdmTable.JournalNum;

        //Relation same source company
        priceDiscAdmTrans.relation      = _priceDiscAdmTrans.relation;
        
        priceDiscAdmTrans.ItemCode      = _priceDiscAdmTrans.ItemCode;
        priceDiscAdmTrans.ItemRelation  = _priceDiscAdmTrans.ItemRelation;
        priceDiscAdmTrans.Amount        = _priceDiscAdmTrans.Amount;
        priceDiscAdmTrans.FromDate      = _priceDiscAdmTrans.FromDate;
        priceDiscAdmTrans.Currency      = _priceDiscAdmTrans.Currency;
        priceDiscAdmTrans.UnitId        = _priceDiscAdmTrans.UnitId;
        priceDiscAdmTrans.PriceUnit     = _priceDiscAdmTrans.PriceUnit;
        priceDiscAdmTrans.InventDimId   = _priceDiscAdmTrans.InventDimId;
        priceDiscAdmTrans.Agreement   = _priceDiscAdmTrans.Agreement;
        priceDiscAdmTrans.AgreementHeaderExt_RU   = _priceDiscAdmTrans.AgreementHeaderExt_RU;
        priceDiscAdmTrans.AllocateMarkup   = _priceDiscAdmTrans.AllocateMarkup;
        priceDiscAdmTrans.CalendarDays   = _priceDiscAdmTrans.CalendarDays;
        priceDiscAdmTrans.DeliveryTime   = _priceDiscAdmTrans.DeliveryTime;
        priceDiscAdmTrans.DifferentFromPosted   = _priceDiscAdmTrans.DifferentFromPosted;
        priceDiscAdmTrans.DisregardLeadTime   = _priceDiscAdmTrans.DisregardLeadTime;
        priceDiscAdmTrans.GenericCurrency   = _priceDiscAdmTrans.GenericCurrency;
        priceDiscAdmTrans.InventBaileeFreeDays_RU   = _priceDiscAdmTrans.InventBaileeFreeDays_RU;
        //priceDiscAdmTrans.LineNum   = _priceDiscAdmTrans.LineNum;
        priceDiscAdmTrans.Log   = _priceDiscAdmTrans.Log;
        priceDiscAdmTrans.Markup   = _priceDiscAdmTrans.Markup;
        priceDiscAdmTrans.MaximumRetailPrice_IN   = _priceDiscAdmTrans.MaximumRetailPrice_IN;
        priceDiscAdmTrans.MustBeDeleted   = _priceDiscAdmTrans.MustBeDeleted;
        priceDiscAdmTrans.PDSCalculationId   = _priceDiscAdmTrans.PDSCalculationId;
        priceDiscAdmTrans.Percent1   = _priceDiscAdmTrans.Percent1;
        priceDiscAdmTrans.Percent2   = _priceDiscAdmTrans.Percent2;
        priceDiscAdmTrans.QuantityAmountFrom   = _priceDiscAdmTrans.QuantityAmountFrom;
        priceDiscAdmTrans.QuantityAmountTo   = _priceDiscAdmTrans.QuantityAmountTo;
        priceDiscAdmTrans.ToDate   = _priceDiscAdmTrans.ToDate;


        //Check if case edit from another trade.
        if(_priceDiscAdmTrans.PriceDiscTableRef)
        {
            select firstonly priceDiscTransShare
                where priceDiscTransShare.SourceRecId == _sourcePriceDiscTable.OriginalPriceDiscAdmTransRecId;

            if(priceDiscTransShare)
            {
                select firstonly originalPriceDiscAdmTrans
                    where originalPriceDiscAdmTrans.RecId == priceDiscTransShare.DestinationRecId;

                if(originalPriceDiscAdmTrans)
                {
                    select firstonly   priceDiscTable
                        where priceDiscTable.OriginalPriceDiscAdmTransRecId == originalPriceDiscAdmTrans.RecId;

                    if(priceDiscTable)
                    {
                        priceDiscAdmTrans.PriceDiscTableRef = priceDiscTable.RecId;
                    }
                }
            }
            else
            {
                throw error("@LAW:LAW_ShareFailBecauseShared");
            }
            //find PriceDisTable

        }
        //case all => share price to all party
        //if(_priceDiscAdmTrans.AccountCode == PriceDiscPartyCodeType::All)
        //{
        //    priceDiscAdmTrans.AccountCode       = _priceDiscAdmTrans.AccountCode;
        //    priceDiscAdmTrans.AccountRelation   = _priceDiscAdmTrans.AccountRelation;
        //}
        //if(_priceDiscAdmTrans.AccountCode == PriceDiscPartyCodeType::GroupId)
        //{
        //    priceDiscAdmTrans.AccountCode       = _priceDiscAdmTrans.AccountCode;
        //    priceDiscAdmTrans.AccountRelation   = _priceDiscAdmTrans.AccountRelation;
        //}

        //Auto group all default vendor of store to group
        //If only default customer for all destination store
        /*if(conLen(contDefault) == 1 && contDefault)
        {
            priceDiscAdmTrans.AccountCode       = PriceDiscPartyCodeType::Table;
            priceDiscAdmTrans.AccountRelation   = conPeek(contDefault, 1);
            priceDiscAdmTrans.insert();
        }
        else
        {
            if(conLen(contGroup) >= 1)
            {
                for(i = 1 ; i< conLen(contGroup); i++)
                {
                    priceDiscAdmTrans.AccountCode       = PriceDiscPartyCodeType::GroupId;
                    priceDiscAdmTrans.AccountRelation   = conPeek(contGroup, i);
                    priceDiscAdmTrans.insert();
                }
            }
            else
            {
                for( i = 0; i < conLen(contDefault); i++)
                {
                    //priceDiscAdmTrans.AccountCode == PriceDiscPartyCodeType::Table;
                }
            }
        }*/

        this.findTableGroupOfDefaultVendor(_priceDiscAdmTrans.relation);
        mapEnumerator = mapTableGroupVendor.getEnumerator();

        while (mapEnumerator.moveNext())
        {
            if(mapEnumerator.currentValue() == "Table")
            {
                priceDiscAdmTrans.AccountCode       = PriceDiscPartyCodeType::Table;
                priceDiscAdmTrans.AccountRelation   = mapEnumerator.currentKey();
                priceDiscAdmTrans.insert();
            }
            
            if(mapEnumerator.currentValue() == "Group")
            {
                priceDiscAdmTrans.AccountCode       = PriceDiscPartyCodeType::GroupId;
                priceDiscAdmTrans.AccountRelation   = mapEnumerator.currentKey();
                priceDiscAdmTrans.insert();
            }
        }

        return priceDiscAdmTrans;
    }

    public void insertTradeAdmTransMap(PriceDiscAdmTrans _sourceCompany, PriceDiscAdmTrans _destinationCompany)
    {
        LAW_PriceDiscAdmTransMap    priceDiscAdmsMap;
        //After insert data in destination company, need to map data 2 trade agreement line
        priceDiscAdmsMap.SourceRecId = _sourceCompany.RecId;
        priceDiscAdmsMap.DestinationRecId = _destinationCompany.RecId;
        priceDiscAdmsMap.insert();
        //--end
    }

    public PriceDiscAdmTable createPurchTradeAgreementTable(PriceDiscAdmTable _currentDiscAdmTable)
    {
        PriceDiscTable              priceDiscTable;
        
        PriceDiscAdmTable           priceDiscAdmTable;

        priceDiscAdmTable.clear();
        priceDiscAdmTable.JournalName     = _currentDiscAdmTable.JournalName;
        priceDiscAdmTable.Name            = "@LAW:LAW_InsertTradeDescription";
        priceDiscAdmTable.DefaultRelation = _currentDiscAdmTable.DefaultRelation;
        priceDiscAdmTable.LAW_SourceCompany = _currentCompanyId;
        priceDiscAdmTable.LAW_Shared      = NoYes::Yes;
        priceDiscAdmTable.insert();

        return priceDiscAdmTable;
    }

    public void updateSourceTradeAgreement(PriceDiscAdmTable _currentDiscAdmTable)
    {
        _currentDiscAdmTable.selectForUpdate(true);
        _currentDiscAdmTable.LAW_Shared      =  NoYes::Yes;
        _currentDiscAdmTable.LAW_SourceCompany = curExt();
        _currentDiscAdmTable.LAW_SharedCompany = strFmt("%1,%2",  _currentDiscAdmTable.LAW_SharedCompany, _companyId);
        _currentDiscAdmTable.update();
    }

    public void priceDiscAdmCheckPost(PriceDiscAdmTable priceDiscAdmTable)
    {
        PriceDiscAdmCheckPost   priceDiscAdmCheckPost = new PriceDiscAdmCheckPost(false);
        priceDiscAdmCheckPost.initJournalNum(priceDiscAdmTable.JournalNum);
        priceDiscAdmCheckPost.run();
        
    }

    public container findAllDefaultVendor()
    {
        VendTable venTable;
        RetailStoreTable retailStoreTable;
        
        container   contVendor = conNull();
        //Step 1. get all vendor in all store, check duplicate vendor
        changecompany(_companyId)
        {
            while select retailStoreTable
                where retailStoreTable.LAW_IsFranchise
                    && retailStoreTable.LAW_DefaultVendor
            {
                if(!conFind(contVendor, retailStoreTable.LAW_DefaultVendor))
                {
                    contVendor += retailStoreTable.LAW_DefaultVendor;
                }
            }
        }
        return contVendor;
    }

    public container findTableGroupOfDefaultVendor(PriceType _priceType)
    {
        VendTable venTable;
        RetailStoreTable retailStoreTable;
        container   contVendor = conNull();
        //Step 1. get all vendor in all store, check duplicate vendor
        //mapTableGroupVendor
        changecompany(_companyId)
        {
            while select retailStoreTable
                where retailStoreTable.LAW_IsFranchise
                    && retailStoreTable.LAW_DefaultVendor
            {
                if(_priceType == PriceType::EndDiscPurch)
                {
                    if(VendTable::find(retailStoreTable.LAW_DefaultVendor).EndDisc)
                        mapTableGroupVendor.insert(VendTable::find(retailStoreTable.LAW_DefaultVendor).EndDisc, "Group");
                    else
                        mapTableGroupVendor.insert(retailStoreTable.LAW_DefaultVendor, "Table");
                }
                if(_priceType == PriceType::LineDiscPurch)
                {
                    if(VendTable::find(retailStoreTable.LAW_DefaultVendor).LineDisc)
                        mapTableGroupVendor.insert(VendTable::find(retailStoreTable.LAW_DefaultVendor).LineDisc ,  "Group");
                    else
                        mapTableGroupVendor.insert(retailStoreTable.LAW_DefaultVendor, "Table");
                }
                if(_priceType == PriceType::MultiLineDiscPurch)
                {
                    if(VendTable::find(retailStoreTable.LAW_DefaultVendor).MultiLineDisc)
                        mapTableGroupVendor.insert( VendTable::find(retailStoreTable.LAW_DefaultVendor).MultiLineDisc, "Group");
                    else
                        mapTableGroupVendor.insert( retailStoreTable.LAW_DefaultVendor, "Table");
                }
                if(_priceType == PriceType::PricePurch)
                {
                    if(VendTable::find(retailStoreTable.LAW_DefaultVendor).PriceGroup)
                        mapTableGroupVendor.insert(VendTable::find(retailStoreTable.LAW_DefaultVendor).PriceGroup, "Group");
                    else
                        mapTableGroupVendor.insert(retailStoreTable.LAW_DefaultVendor, "Table");
                }

                
            }
        }
        return contVendor;
    }

    //public PriceDiscGroup createGroupAndSetPurchaseOrderDetail(PriceType _priceType, ItemId _itemid)
    //{
    //    PriceDiscGroup  priceDiscGroup;

    //    if(_priceType == PriceType::EndDiscPurch)
    //    {
    //        priceDiscGroup = this.insertPriceDiscGroup(PriceGroupType::EndDiscGroup, _itemid);
    //    }
    //    if(_priceType == PriceType::LineDiscPurch)
    //    {
    //        priceDiscGroup = this.insertPriceDiscGroup(PriceGroupType::LineDiscGroup, _itemid);
    //    }
    //    if(_priceType == PriceType::MultiLineDiscPurch)
    //    {
    //        priceDiscGroup = this.insertPriceDiscGroup(PriceGroupType::MultiLineDiscGroup, _itemid);
    //    }
    //    if(_priceType == PriceType::PricePurch)
    //    {
    //        priceDiscGroup = this.insertPriceDiscGroup(PriceGroupType::PriceGroup, _itemid);
    //    }

    //    return priceDiscGroup;
    //}

    //public PriceDiscGroup insertPriceDiscGroup(PriceGroupType _PriceGroupType, ItemId _itemId)
    //{
    //    PriceDiscGroup priceDiscGroup;
    //    container contVendorDestnation = this.findAndCreateGroupVendor();
    //    VendTable   vendTable;
    //    int i = 1;
    //    //Step 1: Create price disc group

    //    changecompany(_companyId)
    //    {
    //        try
    //        {
    //            priceDiscGroup.clear();
    //            priceDiscGroup.Module = ModuleInventCustVend::Vend;
    //            priceDiscGroup.Name = strFmt("Share_%1_%2_%3", _companyId, _itemId, guid2Str(newGuid()));
    //            priceDiscGroup.Type = _PriceGroupType;
    //            priceDiscGroup.GroupId = strFmt("Share_%1_%2_%3", _companyId, _itemId, guid2Str(newGuid()));
    //            priceDiscGroup.MCRPriceDiscGroupType = MCRPriceDiscGroupType::Standard;
    //            priceDiscGroup.doInsert();

    //            //Step 2: set puchase order default for vendor
    //            if(priceDiscGroup)
    //            {
    //                for(i = 1; i <= conLen( contVendorDestnation); i++)
    //                {
    //                    while select vendTable
    //                    where vendTable.AccountNum == conPeek(contVendorDestnation, i)
    //                    {
    //                        vendTable.selectForUpdate(true);
    //                        if(_PriceGroupType == PriceGroupType::EndDiscGroup)
    //                        vendTable.EndDisc = priceDiscGroup.GroupId;
    //                        if(_PriceGroupType == PriceGroupType::LineDiscGroup)
    //                        vendTable.LineDisc = priceDiscGroup.GroupId;
    //                        if(_PriceGroupType == PriceGroupType::MultiLineDiscGroup)
    //                        vendTable.MultiLineDisc = priceDiscGroup.GroupId;
    //                        if(_PriceGroupType == PriceGroupType::PriceGroup)
    //                        vendTable.PriceGroup = priceDiscGroup.GroupId;
    //                        vendTable.update();
    //                    }
    //                }
    //            }

    //        }
    //        catch
    //        {
    //            throw error("Can not create proce disc group");
    //        }
    //    }
    //    return priceDiscGroup;
    //}

    public boolean ValidateDataDestinationCompany()
    {
        PriceDiscAdmName   destinationDiscAdmName;
        PriceDiscAdmTrans   priceDiscAdmTrans;
        boolean ret = true;

        changeCompany(_companyId)
        {
            select firstonly destinationDiscAdmName
                where destinationDiscAdmName.JournalName == _currentPriceDiscAdmTable.JournalName;

            if(!destinationDiscAdmName)
                ret = checkFailed("@LAW:LAW_TradeAgreementNotExist");
        }

        while select priceDiscAdmTrans
            where priceDiscAdmTrans.JournalNum == _currentPriceDiscAdmTable.JournalNum
            && priceDiscAdmTrans.ItemRelation
        {
            //Check data in destination company
            changeCompany(_companyId)
            {
                if(//TH1. Table check item still exist in inventtable destination company
                    (!InventTable::find(priceDiscAdmTrans.ItemRelation) && priceDiscAdmTrans.ItemCode == PriceDiscProductCodeType::Table)
                    //TH2. Check Group item relation still exist in destination company
                    || ((!PriceDiscGroup::find(ModuleInventCustVend::Vend, PriceGroupType::EndDiscGroup, priceDiscAdmTrans.ItemRelation)
                         || !PriceDiscGroup::find(ModuleInventCustVend::Vend, PriceGroupType::LineDiscGroup, priceDiscAdmTrans.ItemRelation)
                         || !PriceDiscGroup::find(ModuleInventCustVend::Vend, PriceGroupType::MCRPostageDisc, priceDiscAdmTrans.ItemRelation)
                         || !PriceDiscGroup::find(ModuleInventCustVend::Vend, PriceGroupType::MultiLineDiscGroup, priceDiscAdmTrans.ItemRelation)
                         || !PriceDiscGroup::find(ModuleInventCustVend::Vend, PriceGroupType::PriceGroup, priceDiscAdmTrans.ItemRelation))
                         && priceDiscAdmTrans.ItemCode == PriceDiscProductCodeType::GroupId))
                    //&& (priceDiscAdmTrans.relation == PriceType::PricePurch || priceDiscAdmTrans.relation == PriceType::LineDiscPurch
                    //|| priceDiscAdmTrans.relation == PriceType::MultiLineDiscPurch||priceDiscAdmTrans.relation == PriceType::EndDiscPurch)))
                {
                    ret = checkFailed(strFmt("@LAW:LAW_ItemShareNotExist", priceDiscAdmTrans.ItemRelation));
                }
            }
        }


        //retrict share current company
        if(_companyId == curExt())
        {
            ret = checkFailed("@LAW:LAW_CannotShareCurrentCom");
        }

        //Only share data if journal had posted

        if(_currentPriceDiscAdmTable.Posted == NoYes::No)
        {
            ret = checkFailed("@LAW:LAW_TradeAgreeNotPost");
        }

        //Can not share data if data shared

        if(_currentPriceDiscAdmTable.LAW_Shared == NoYes::Yes)
        {
            ret = checkFailed("@LAW:LAW_BeenShared");
        }
        return ret;
    }

    public boolean validate(Object calledFrom = null)
    {
        boolean ret;
    
        ret = super(calledFrom);
        if(!_companyId)
            ret = ret && checkFailed('@LAW:LAW_SelectCompany');

        ret = this.ValidateDataDestinationCompany();

        return ret;
    }

    public static void main(Args _args)
    {
        LAW_PurchaseTradeAgreementJournalShare tradeAgreementShare = new LAW_PurchaseTradeAgreementJournalShare();
        if(_args && _args.dataset() == tableNum(PriceDiscAdmTable))
        {
            tradeAgreementShare.parmPriceDiscAdmTable(_args.record());

            if(tradeAgreementShare.prompt())
            {
                tradeAgreementShare.run();
            }
        }
        
    }

}