Friday, 9 November 2018

Unit conversions

Unit Conversion:



UnitOfMeasureConverter_Product is product specific unit conversion. It lookups defined conversions in the following order:
  1. Use direct product-specific conversion, if one exists.
  2. Use a chain of two product-specific conversions through one base unit of measure, if one exists.
  3. Use a chain of product-specific conversions through two base units of measure, if one exists.
  4. Use a chain of one product-specific and one standard conversions through one base unit of measure, if one exists.
  5. Use a chain of product-specific or standard conversions through two base units of measure, if one exists.
  6. Execute the calculation algorithm for the standard conversions.
Meanwhile, UnitOfMeasureConverter::convert is just a wrap method that will call UnitOfMeasureConverter_Product if ProductId is provided. If no ProductId specify, it just do standard conversion.
static void Job_UnitConversion(Args _args)
{
    #define.itemId('M0018')
     
    //Method one
    UnitOfMeasureConverter_Product  unitConverter   = UnitOfMeasureConverter_Product::construct();    
    unitConverter.parmProduct(InventTable::find(#itemId).Product);
    unitConverter.parmFromUnitOfMeasure(UnitOfMeasure::unitOfMeasureIdBySymbol('kg'));
    unitConverter.parmToUnitOfMeasure(UnitOfMeasure::unitOfMeasureIdBySymbol('ea'));
    unitConverter.parmRoundAbsoluteValue(NoYes::No);
    unitConverter.parmApplyRounding(NoYes::No);
    print unitConverter.convertValue(1010);
    //Method two
    print UnitOfMeasureConverter::convert(1010,
            UnitOfMeasure::unitOfMeasureIdBySymbol('kg'),
            UnitOfMeasure::unitOfMeasureIdBySymbol('ea'),
            NoYes::No,
            InventTable::itemProduct(#itemId),
            NoYes::No);
    pause;
}

===============================================================================================

Unit Conversion Check AX2009 Vs AX2012

AX 2009
if(!UnitConvert::canConvert(this.inventTableModuleInvent().UnitId, this.SAB_ProdPickingUOM, this.ItemId))

AX 2012
unitConvFound = UnitOfMeasureConverter::canBeConverted(UnitOfMeasure::findBySymbol(this.inventTableModuleInvent().UnitId).RecId,                                                             UnitOfMeasure::findBySymbol(this.SAB_SalesPickingUOM).RecId,

                                                                   this.Product);

Thursday, 8 November 2018

Date and TimeCalculations

1) Apply Datetime Offset :

 private void applyDateTimeOffset(
        FieldId         _fieldId,
        TransDate       _baseDate,
        int             _dateOffset,
        RORDateUnit     _dateUnit,
        boolean         _applyDateOffset,
        TimeOfDay       _baseTime,
        int             _timeOffset,
        RORTimeUnit     _timeUnit,
        boolean         _applyTimeOffset,
        boolean         _hasSystemTime = false)
    {
        int         timeZoneDifference = (DateTimeUtil::time(DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::getSystemDateTime(), DateTimeUtil::getUserPreferredTimeZone()))
                        - DateTimeUtil::time(DateTimeUtil::getSystemDateTime()));
        TransDate   rentDate = DateTimeUtil::date(DateTimeUtil::applyTimeZoneOffset(this.(_fieldId), DateTimeUtil::getUserPreferredTimeZone()));
        TimeOfDay   rentTime = DateTimeUtil::time(this.(_fieldId)) + timeZoneDifference;
 
        if (_applyDateOffset)
        {
            rentDate = RORRentalDateTimes::applyDateOffset(_baseDate, _dateOffset, _dateUnit);
        }
 
        if (_applyTimeOffset)
        {
            rentTime = RORRentalDateTimes::applyTimeOffset(_baseTime, _timeOffset, _timeUnit);
        }
 
        if (!rentDate && _hasSystemTime)
        {
            rentTime = rentTime + timeZoneDifference;
        }
 
        this.(_fieldId) = _hasSystemTime ? DateTimeUtil::newDateTime(rentDate, rentTime) : DateTimeUtil::newDateTime(rentDate, rentTime, DateTimeUtil::getUserPreferredTimeZone());
 
        this.modifiedField(_fieldId);
    }
=======================================================================
2) Check whether current date time exists in active calendar :

private boolean checkDateCalendar(FieldId _fieldId, CalendarId _calendarId = '')
    {
        boolean             ret = true;
        CalendarId          calendarId = _calendarId;
        WorkCalendarDate    workCalendarDate;
        TransDate           rentalDate = DateTimeUtil::date(this.(_fieldId));
   
        Query                       query;
        SysInfoAction_FormrunQuery  infoAction;
        ;
   
        if (rentalDate
        && this.RORRentalDateTimesHost::isWorksheetTable()
        && this.hostHeadingLineLevel() == HeadingLine::Line)
        {
            if (!calendarId)
            {
                calendarId = this.getHostBuffer().calendarId;
            }
   
            if (calendarId)
            {
                workCalendarDate = WorkCalendarDate::RORfind(calendarId, rentalDate);
   
                if (!workCalendarDate)
                {
                    query = new Query();
                    query.addDataSource(tablenum(WorkCalendarTable)).addRange(fieldnum(WorkCalendarTable, CalendarId)).value(queryValue(calendarId)) ;
   
                    infoAction = SysInfoAction_FormrunQuery::newFormnameQuery(formstr(WorkCalendarTable), query);
                    ret = checkFailed(strFmt("@DNR3262", calendarId, rentalDate), '', infoAction);
                }
            }
        }
   
        return ret;
    }


(or)

 private static boolean dateIsOpen(CalendarId _calendarId, TransDate _transDate, workCalendarSched _workCalendarSched = null)
    {
        WorkCalendarSched workCalendarSched = _workCalendarSched ? _workCalendarSched : new WorkCalendarSched(true);
        ;
    
        return workCalendarSched.isDateOpen(_calendarId, _transDate);
    }



Get no.of working days in selected calendar

static void WorkingdaysInPeriod_WD(Args _args)
{
    WorkCalendarSched workCalendarSched;
   date start;
   date end;
   counter workingdays;
   counter totaldays;
   CalendarId primCalendar="Standaard";
   ;
   start=str2date("1-11-2014",123);
   end= str2Date("1-12-2014",123);
    workCalendarSched = new workCalendarSched();
    while(start<end)
   {
    totaldays++;

       if(workCalendarSched.isdateopen(primCalendar,start)==true)
           {
               workingdays++;
           }
        start++;
    }
    info(strfmt("Total days: : %1",totaldays));
    info(strfmt("Total working days: : %1",workingdays));
}