Monday, 4 February 2019

Requirement in AX7 1

[ExtensionOf(formStr(RORSalesTable))]
final class EG002RORSalesTableForm_Extension
{
    /// <summary>
    /// Get Lookup to resources on the main and sub projects
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormControlEventHandler(formControlStr(RORSalesTable, SalesLine_EGLocationGrouping), FormControlEventType::Lookup)]
    public static void SalesLine_EGLocationGrouping_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        FormControl                 locationgroup;
        FormRun                     Form;
        SalesLine                   salesline;
        ProjTable                   projtable;
        FormDataSource              salesline_ds;
        projtable                   projtbl;
        BOBObjectTable              bobobjecttable;
        WrkCtrTable                 wrkctrtable;
        EG002MaintainResources      maintainresources;     
     
        //Get salesline project id
        locationgroup = sender as FormControl;
        Form    = locationgroup.formRun();     

        salesline_ds = Form.dataSource(formDataSourceStr(RORSalesTable,SalesLine)) as FormDataSource;
        salesline = salesline_ds.cursor();

        // build lookup for location group from related project Object id
        FormControlCancelableSuperEventArgs event = e as FormControlCancelableSuperEventArgs;
        sysTableLookup sysTableLookup = sysTableLookup::newParameters(tablenum(EG002MaintainResources), sender);

        //FormStringControl    projid = formRun.design(0).controlName("NCMR_OINCMRNum_011");
        Query                   query = new Query();
        QueryBuildDataSource    maintainResourcesDS;
        container               conobjs,conproj;
        maintainResourcesDS   =   query.addDataSource(tableNum(EG002MaintainResources));
        sysTableLookup.addLookupfield(fieldNum(EG002MaintainResources,ObjectId));
        sysTableLookup.addLookupfield(fieldNum(EG002MaintainResources,ObjectName));

        if(salesline.ProjId)
        {
            conproj += salesline.ProjId;
            while select projtable
                where projtable.ParentId == salesline.ProjId
            {
                conproj +=   projtable.ProjId;
            }
            for (int i = 0 ; i <= conLen(conproj) ; i++)
            {
                ProjId  projid;   
                projid  =   conPeek(conproj, i);
                while select maintainresources
                where maintainresources.ProjId  ==  projid
                {
                    if(maintainresources.LimitedAvailability)
                    {
                        if(maintainresources.SalesId == salesline.SalesId)
                        {
                            if(!conFind(conobjs,maintainresources.ObjectId))
                                conobjs += maintainresources.ObjectId;
                        }
                    }
                    else
                    {
                        if(!conFind(conobjs,maintainresources.ObjectId))
                        conobjs += maintainresources.ObjectId;
                    }
                }
            }

            maintainResourcesDS.addRange(fieldNum(EG002MaintainResources,ObjectId)).value(con2Str(conobjs));
        }
        else
            maintainResourcesDS.addRange(fieldNum(EG002MaintainResources,Projid)).value(salesline.ProjId);
     
        sysTableLookup.parmQuery(query);

        if(salesline.ProjId)
        {
            sysTableLookup.performFormLookup();
        }
        event.CancelSuperCall();
    }

    /// <summary>
    ///Get Additional product grouping lookup related to Production group field selected
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormControlEventHandler(formControlStr(RORSalesTable, SalesLine_EGAdditionalProductionGroup), FormControlEventType::Lookup)]
    public static void SalesLine_EGAdditionalProductionGroup_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        FormRun             formRun          = sender.formRun();
        FormControlCancelableSuperEventArgs event = e as FormControlCancelableSuperEventArgs;
        sysTableLookup sysTableLookup = sysTableLookup::newParameters(tablenum(ProjCategory), sender);
        Query                   query = new Query();
        QueryBuildDataSource    ProjCategoryDS;
        ProjCategoryDS   =   query.addDataSource(tableNum(ProjCategory));
        sysTableLookup.addLookupfield(fieldNum(ProjCategory,CategoryId));
        sysTableLookup.addLookupfield(fieldNum(ProjCategory,Name));
        ProjCategoryDS.addSortField(FieldNum(ProjCategory,CategoryType));
        FormStringControl    productiongroup = formRun.design(0).controlName("SalesLine_EGProductionGroup");
        if(productiongroup.valueStr() !="")
            ProjCategoryDS.addRange(fieldNum(ProjCategory,CategoryGroupId)).value(productiongroup.valuestr());
        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();
        event.CancelSuperCall();
    }

    //When item is modified get corresponding values from Productiongroup,Additional production group, Resourceproductiongroup
    [FormDataFieldEventHandler(formDataFieldStr(RORSalesTable, SalesLine, ItemId), FormDataFieldEventType::Modified)]
    public static void ItemId_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
    {
        ProjCategoryId  projCategoryId;
        FormDatasource SalesLine_ds = sender.datasource();

        SalesLine salesline = SalesLine_ds.cursor();

        projCategoryId = InventTable::find(salesline.itemid).projCategoryId;

        salesline.EGAdditionalProductionGroup = projCategoryId;
        salesline.EGProductionGroup = ProjCategory::find(projCategoryId).CategoryGroupId;
        EG002ResourceProductionGroup    resourceproductiongroup;
        select ItemId,ResourceproductionGroup from resourceproductiongroup
            where resourceproductiongroup.ItemId     == salesline.ItemId;
        salesline.EG002ResourceproductionGroup   =   resourceproductiongroup.ResourceproductionGroup;
    }

    /// <summary>
    ///Get Additional locationgroup value based on location group selected. If supplementary items exist in salesorder then get value corresponding to Main item.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormDataFieldEventHandler(formDataFieldStr(RORSalesTable, SalesLine, EGLocationGrouping), FormDataFieldEventType::Modified)]
    public static void EGLocationGrouping_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
    {
        SuppItemTable   suppitemtable;
        SalesLine       saleslinetmp;
        FormDatasource SalesLine_ds = sender.datasource();
        SalesLine salesline = SalesLine_ds.cursor();
        salesline.EGAdditionalLocationGroup = BOBObjectTable::findObjectId(salesline.EGLocationGrouping).Name;
        while select suppitemtable
            where suppitemtable.ItemRelation == salesline.ItemId
        {
            while select saleslinetmp
                where saleslinetmp.SalesId == salesline.SalesId
                  && saleslinetmp.ItemId == suppitemtable.SuppItemId
            {
                saleslinetmp.selectForUpdate(true);
                ttsbegin;
                saleslinetmp.EGLocationGrouping = salesline.EGLocationGrouping;
                saleslinetmp.EGAdditionalLocationGroup = BOBObjectTable::findObjectId(salesline.EGLocationGrouping).Name;
                saleslinetmp.update();
                ttscommit;
            }
        }
        SalesLine_ds.research();
        SalesLine_ds.refresh();
        //SalesLine_ds.executeQuery();     
    }

    /// <summary>
    /// Get layout grouping value based on projid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormControlEventHandler(formControlStr(RORSalesTable, SalesLine_EG002LayoutGrouping), FormControlEventType::Lookup)]
    public static void SalesLine_EG002LayoutGrouping_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        FormControl formcontrol = sender as FormControl;
        FormRun formrun = formcontrol.formrun();
        FormDatasource SalesLine_ds =  formrun.dataSource(formDataSourceStr(RORSalesTable, SalesLine)) as FormDataSource;
        SalesLine salesline                         =   SalesLine_ds.cursor();
        FormControlCancelableSuperEventArgs event   = e as FormControlCancelableSuperEventArgs;
        sysTableLookup sysTableLookup               = sysTableLookup::newParameters(tablenum(EG002LayoutGrouping), sender);
        Query                   query               = new Query();
        QueryBuildDataSource    layoutGroupingDS;
        layoutGroupingDS                            =   query.addDataSource(tableNum(EG002LayoutGrouping));
        sysTableLookup.addLookupfield(fieldNum(EG002LayoutGrouping,ID));
        sysTableLookup.addLookupfield(fieldNum(EG002LayoutGrouping,Sorting));
        layoutGroupingDS.addSortField(FieldNum(EG002LayoutGrouping, Sorting));//Sort order using Sorting Field
        layoutGroupingDS.addRange(fieldNum(EG002LayoutGrouping,ProjId)).value(salesline.projid);
        sysTableLookup.parmQuery(query);
        if(salesline.ProjId)
        {
            sysTableLookup.performFormLookup();
            event.CancelSuperCall();
        }
    }

    /// <summary>
    /// Lookup for Reproduction group assigned to Itemid.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormControlEventHandler(formControlStr(RORSalesTable, SalesLine_EG002ResourceproductionGroup), FormControlEventType::Lookup)]
    public static void SalesLine_EG002ResourceproductionGroup_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        FormControl formcontrol = sender as FormControl;
        FormRun formrun = formcontrol.formrun();
        FormDatasource SalesLine_ds =  formrun.dataSource(formDataSourceStr(RORSalesTable, SalesLine)) as FormDataSource;
        SalesLine salesline                         =   SalesLine_ds.cursor();
        FormControlCancelableSuperEventArgs event = e as FormControlCancelableSuperEventArgs;
        sysTableLookup sysTableLookup = sysTableLookup::newParameters(tablenum(EG002ResourceProductionGroup), sender);
        Query                   query = new Query();
        QueryBuildDataSource    eG002ResourceProductionGroupDS;
        eG002ResourceProductionGroupDS   =   query.addDataSource(tableNum(EG002ResourceProductionGroup));
        sysTableLookup.addLookupfield(fieldNum(EG002ResourceProductionGroup,ResourceproductionGroup));
        sysTableLookup.addLookupfield(fieldNum(EG002ResourceProductionGroup,ItemId));
        eG002ResourceProductionGroupDS.addRange(fieldNum(EG002ResourceProductionGroup,itemId)).value(salesline.itemid);
        sysTableLookup.parmQuery(query);
        if(salesline.ItemId)
        {
            sysTableLookup.performFormLookup();
            event.CancelSuperCall();
        }
    }

}
======================================================================
[ExtensionOf(formStr(RORSalesTable))]
final class EG002RORSalesTableForm_Extension
{
    /// <summary>
    /// Get Lookup to resources on the main and sub projects
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormControlEventHandler(formControlStr(RORSalesTable, SalesLine_EGLocationGrouping), FormControlEventType::Lookup)]
    public static void SalesLine_EGLocationGrouping_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        FormControl                 locationgroup;
        FormRun                     Form;
        SalesLine                   salesline;
        ProjTable                   projtable;
        FormDataSource              salesline_ds;
        projtable                   projtbl;
        BOBObjectTable              bobobjecttable;
        WrkCtrTable                 wrkctrtable;
        EG002MaintainResources      maintainresources;     
     
        //Get salesline project id
        locationgroup = sender as FormControl;
        Form    = locationgroup.formRun();     

        salesline_ds = Form.dataSource(formDataSourceStr(RORSalesTable,SalesLine)) as FormDataSource;
        salesline = salesline_ds.cursor();

        // build lookup for location group from related project Object id
        FormControlCancelableSuperEventArgs event = e as FormControlCancelableSuperEventArgs;
        sysTableLookup sysTableLookup = sysTableLookup::newParameters(tablenum(EG002MaintainResources), sender);

        //FormStringControl    projid = formRun.design(0).controlName("NCMR_OINCMRNum_011");
        Query                   query = new Query();
        QueryBuildDataSource    maintainResourcesDS;
        container               conobjs,conproj;
        maintainResourcesDS   =   query.addDataSource(tableNum(EG002MaintainResources));
        sysTableLookup.addLookupfield(fieldNum(EG002MaintainResources,ObjectId));
        sysTableLookup.addLookupfield(fieldNum(EG002MaintainResources,ObjectName));

        if(salesline.ProjId)
        {
            conproj += salesline.ProjId;
            while select projtable
                where projtable.ParentId == salesline.ProjId
            {
                conproj +=   projtable.ProjId;
            }
            for (int i = 0 ; i <= conLen(conproj) ; i++)
            {
                ProjId  projid;   
                projid  =   conPeek(conproj, i);
                while select maintainresources
                where maintainresources.ProjId  ==  projid
                {
                    if(maintainresources.LimitedAvailability)
                    {
                        if(maintainresources.SalesId == salesline.SalesId)
                        {
                            if(!conFind(conobjs,maintainresources.ObjectId))
                                conobjs += maintainresources.ObjectId;
                        }
                    }
                    else
                    {
                        if(!conFind(conobjs,maintainresources.ObjectId))
                        conobjs += maintainresources.ObjectId;
                    }
                }
            }

            maintainResourcesDS.addRange(fieldNum(EG002MaintainResources,ObjectId)).value(con2Str(conobjs));
        }
        else
            maintainResourcesDS.addRange(fieldNum(EG002MaintainResources,Projid)).value(salesline.ProjId);
     
        sysTableLookup.parmQuery(query);

        if(salesline.ProjId)
        {
            sysTableLookup.performFormLookup();
        }
        event.CancelSuperCall();
    }

    /// <summary>
    ///Get Additional product grouping lookup related to Production group field selected
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormControlEventHandler(formControlStr(RORSalesTable, SalesLine_EGAdditionalProductionGroup), FormControlEventType::Lookup)]
    public static void SalesLine_EGAdditionalProductionGroup_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        FormRun             formRun          = sender.formRun();
        FormControlCancelableSuperEventArgs event = e as FormControlCancelableSuperEventArgs;
        sysTableLookup sysTableLookup = sysTableLookup::newParameters(tablenum(ProjCategory), sender);
        Query                   query = new Query();
        QueryBuildDataSource    ProjCategoryDS;
        ProjCategoryDS   =   query.addDataSource(tableNum(ProjCategory));
        sysTableLookup.addLookupfield(fieldNum(ProjCategory,CategoryId));
        sysTableLookup.addLookupfield(fieldNum(ProjCategory,Name));
        ProjCategoryDS.addSortField(FieldNum(ProjCategory,CategoryType));
        FormStringControl    productiongroup = formRun.design(0).controlName("SalesLine_EGProductionGroup");
        if(productiongroup.valueStr() !="")
            ProjCategoryDS.addRange(fieldNum(ProjCategory,CategoryGroupId)).value(productiongroup.valuestr());
        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();
        event.CancelSuperCall();
    }

    //When item is modified get corresponding values from Productiongroup,Additional production group, Resourceproductiongroup
    [FormDataFieldEventHandler(formDataFieldStr(RORSalesTable, SalesLine, ItemId), FormDataFieldEventType::Modified)]
    public static void ItemId_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
    {
        ProjCategoryId  projCategoryId;
        FormDatasource SalesLine_ds = sender.datasource();

        SalesLine salesline = SalesLine_ds.cursor();

        projCategoryId = InventTable::find(salesline.itemid).projCategoryId;

        salesline.EGAdditionalProductionGroup = projCategoryId;
        salesline.EGProductionGroup = ProjCategory::find(projCategoryId).CategoryGroupId;
        EG002ResourceProductionGroup    resourceproductiongroup;
        select ItemId,ResourceproductionGroup from resourceproductiongroup
            where resourceproductiongroup.ItemId     == salesline.ItemId;
        salesline.EG002ResourceproductionGroup   =   resourceproductiongroup.ResourceproductionGroup;
    }

    /// <summary>
    ///Get Additional locationgroup value based on location group selected. If supplementary items exist in salesorder then get value corresponding to Main item.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormDataFieldEventHandler(formDataFieldStr(RORSalesTable, SalesLine, EGLocationGrouping), FormDataFieldEventType::Modified)]
    public static void EGLocationGrouping_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
    {
        SuppItemTable   suppitemtable;
        SalesLine       saleslinetmp;
        FormDatasource SalesLine_ds = sender.datasource();
        SalesLine salesline = SalesLine_ds.cursor();
        salesline.EGAdditionalLocationGroup = BOBObjectTable::findObjectId(salesline.EGLocationGrouping).Name;
        while select suppitemtable
            where suppitemtable.ItemRelation == salesline.ItemId
        {
            while select saleslinetmp
                where saleslinetmp.SalesId == salesline.SalesId
                  && saleslinetmp.ItemId == suppitemtable.SuppItemId
            {
                saleslinetmp.selectForUpdate(true);
                ttsbegin;
                saleslinetmp.EGLocationGrouping = salesline.EGLocationGrouping;
                saleslinetmp.EGAdditionalLocationGroup = BOBObjectTable::findObjectId(salesline.EGLocationGrouping).Name;
                saleslinetmp.update();
                ttscommit;
            }
        }
        SalesLine_ds.research();
        SalesLine_ds.refresh();
        //SalesLine_ds.executeQuery();     
    }

    /// <summary>
    /// Get layout grouping value based on projid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormControlEventHandler(formControlStr(RORSalesTable, SalesLine_EG002LayoutGrouping), FormControlEventType::Lookup)]
    public static void SalesLine_EG002LayoutGrouping_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        FormControl formcontrol = sender as FormControl;
        FormRun formrun = formcontrol.formrun();
        FormDatasource SalesLine_ds =  formrun.dataSource(formDataSourceStr(RORSalesTable, SalesLine)) as FormDataSource;
        SalesLine salesline                         =   SalesLine_ds.cursor();
        FormControlCancelableSuperEventArgs event   = e as FormControlCancelableSuperEventArgs;
        sysTableLookup sysTableLookup               = sysTableLookup::newParameters(tablenum(EG002LayoutGrouping), sender);
        Query                   query               = new Query();
        QueryBuildDataSource    layoutGroupingDS;
        layoutGroupingDS                            =   query.addDataSource(tableNum(EG002LayoutGrouping));
        sysTableLookup.addLookupfield(fieldNum(EG002LayoutGrouping,ID));
        sysTableLookup.addLookupfield(fieldNum(EG002LayoutGrouping,Sorting));
        layoutGroupingDS.addSortField(FieldNum(EG002LayoutGrouping, Sorting));//Sort order using Sorting Field
        layoutGroupingDS.addRange(fieldNum(EG002LayoutGrouping,ProjId)).value(salesline.projid);
        sysTableLookup.parmQuery(query);
        if(salesline.ProjId)
        {
            sysTableLookup.performFormLookup();
            event.CancelSuperCall();
        }
    }

    /// <summary>
    /// Lookup for Reproduction group assigned to Itemid.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormControlEventHandler(formControlStr(RORSalesTable, SalesLine_EG002ResourceproductionGroup), FormControlEventType::Lookup)]
    public static void SalesLine_EG002ResourceproductionGroup_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        FormControl formcontrol = sender as FormControl;
        FormRun formrun = formcontrol.formrun();
        FormDatasource SalesLine_ds =  formrun.dataSource(formDataSourceStr(RORSalesTable, SalesLine)) as FormDataSource;
        SalesLine salesline                         =   SalesLine_ds.cursor();
        FormControlCancelableSuperEventArgs event = e as FormControlCancelableSuperEventArgs;
        sysTableLookup sysTableLookup = sysTableLookup::newParameters(tablenum(EG002ResourceProductionGroup), sender);
        Query                   query = new Query();
        QueryBuildDataSource    eG002ResourceProductionGroupDS;
        eG002ResourceProductionGroupDS   =   query.addDataSource(tableNum(EG002ResourceProductionGroup));
        sysTableLookup.addLookupfield(fieldNum(EG002ResourceProductionGroup,ResourceproductionGroup));
        sysTableLookup.addLookupfield(fieldNum(EG002ResourceProductionGroup,ItemId));
        eG002ResourceProductionGroupDS.addRange(fieldNum(EG002ResourceProductionGroup,itemId)).value(salesline.itemid);
        sysTableLookup.parmQuery(query);
        if(salesline.ItemId)
        {
            sysTableLookup.performFormLookup();
            event.CancelSuperCall();
        }
    }

}

No comments:

Post a Comment