Saturday, 18 February 2017

Create custom service customer details

create  custom service  customer  details  


[DataContractAttribute]

class AIFCustomerSContract

   str     accountNum;
    str     custGroup;
    str     currency;
    str     segmentId;
    str     subsegmentId;
    str     paymMode;
    str     deliveryMode;
    str     dlvTerm;
    str     PaymTerm;


[DataMemberAttribute('accountNum')]
public str accountNum(str _accountNum = accountNum)
{
    accountNum  = _accountNum;
    return  accountNum;
}


[DataMemberAttribute('Name')]
public str Name(str _Name = Name)
{
    Name  = _Name;
    return  Name;
}


[DataMemberAttribute('paymMode')]
public str paymMode(str _paymMode =paymMode)
{
    paymMode  = _paymMode;
    return  paymMode;
}


class AIFCustomerSService
{
}


AIF Customer services





[SysEntryPointAttribute(true), AifCollectionTypeAttribute('_cust',Types::Class)]
public void CreateAndUpDateCustomers(AIFCustomerSContract _cust)
{

    CustTable                   custTable;
    str                         ErrorMessage;
    DirParty                    dirParty;
    DirPartyTable               dirPartyTable;
    DirPerson                   dirperson;
    DirPersonName               dirPersonName;
    DirPartyType                partyType;


    ErrorMessage = this.ValidateCustomer(_cust);
    if(ErrorMessage != "")
    {
        throw Error(strFmtLB(ErrorMessage));
    }

    try
            changeCompany (_cust.Company())
    {
            ttsBegin;
            custTable = CustTable::find(_cust.AccountNum(), true);
            if(!custTable.RecId)  //If CustAccount is not existing the initialize
            {
                custTable.initValue();
                custTable.AccountNum            = _cust.AccountNum();
                custTable.CustGroup             = _cust.CustGroup();
                custTable.Currency              = _cust.Currency();
                custTable.PaymTermId            = _cust.PaymTerm();
                custTable.PaymMode              = _cust.PaymMode();
                custTable.PaymTermId            = _cust.PaymTerm();
                custTable.PaymMode              = _cust.PaymMode();
                custTable.SegmentId             = _cust.segmentId();
                custTable.SubsegmentId          = _cust.subsegmentId();
                custTable.dlvTerm               = _cust.dlvTerm();
                custTable.DlvMode               = _cust.DlvMode();
                custTable.LineOfBusinessId      = _cust.LineOfBusinessId();
                custTable.IdentificationNumber  = _cust.identificationNumber();
                dirParty = DirParty::constructFromCommon(custTable);
                custTable.IsIntegration     = NoYes::Yes;
                custTable.insert(_cust.PartyType(), _cust.Name());
            }
            else // For Updating existing Customer
            {
                custTable.CustGroup             = _cust.CustGroup();
                custTable.Currency              = _cust.Currency();
                custTable.PaymTermId            = _cust.PaymTerm();
                custTable.PaymMode              = _cust.PaymMode();
                custTable.PaymTermId            = _cust.PaymTerm();
                custTable.PaymMode              = _cust.PaymMode();
                custTable.SegmentId             = _cust.segmentId();
                custTable.SubsegmentId          = _cust.subsegmentId();
                custTable.dlvTerm               = _cust.dlvTerm();
                custTable.DlvMode               = _cust.DlvMode();
                custTable.LineOfBusinessId      = _cust.LineOfBusinessId();
                custTable.IdentificationNumber  = _cust.identificationNumber();
                custTable.update();
            }

            // fill Names
            if(!custTable.Party)
            {
              custTable.Party         = DirPartyTable::createNew(_cust.PartyType(), _cust.Name()).RecId;
            }

            dirPartyTable = DirPartyTable::findRec(custTable.Party, true, _cust.PartyType());
            // For Updating Person Name if the Party Type is Person.
            if (dirPartyTable.RecId && _cust.PartyType() == DirPartyType::Organization)
            {
                dirPartyTable.Name      = _cust.Name();
                dirPartyTable.NameAlias = _cust.Name();
                dirPartyTable.update();
            }
            // For Updating Person Name if the Party Type is Person.
            else if (dirPartyTable.RecId && _cust.PartyType() == DirPartyType::Person)
            {
                dirPerson = DirPerson::find(dirPartyTable.RecId,true);
                dirPerson.Name = _cust.Name();
                dirPerson.NameAlias = _cust.Name();
                dirPerson.update();

                //Update Person Name individually
                dirPersonName =  DirPersonName::find(dirPerson.RecId,true);
                [dirPersonName.FirstName, dirPersonName.MiddleName, dirPersonName.LastName] = DirPerson::splitNameParts(_cust.Name());
                dirPersonName.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
                dirPersonName.ValidFrom = DirUtility::getCurrentDateTime();
                dirPersonName.ValidTo = DateTimeUtil::maxValue();
                if (dirPersonName.validateWrite())
                {
                    dirPersonName.update();
                }
            }
        // calling  for  Customers Address
            this.CustomerAddress(_cust);

            ttsCommit;

    }
    catch (Exception::Error)
    {
        ttsAbort;
        throw Exception::Error;
    }
 }



customer address details



[SysEntryPointAttribute(true), AifCollectionTypeAttribute('_cust',Types::Class)]
public void CustomerAddress(AIFCustomerSContract _cust)
{
        CustTable                   custTable;
        NumberSeq                   numberSeq;
        str                         ErrorMessage;
        DirParty                    dirParty;
        DirPartyPostalAddressView   dirPartyPostalAddressView;
        DirPartyContactInfoView     dirPartyContactInfo;
        DirPartyTable               dirPartyTable;


             try
         {
             ttsBegin;
            custTable = CustTable::find(_cust.AccountNum(), true);
            if(!custTable.RecId)
            {
                custTable.initValue();
            }

            dirParty = DirParty::constructFromCommon(custTable);
             // Fill address
            select dirPartyPostalAddressView
            where  dirPartyPostalAddressView.IsPrimary   == NoYes::Yes
                && dirPartyPostalAddressView.CountryRegionId == _cust.CountryCode()
                && dirPartyPostalAddressView.Party == custTable.Party;


            dirPartyPostalAddressView.LocationName      = _cust.Name();
            dirPartyPostalAddressView.City              = _cust.City();
            dirPartyPostalAddressView.Street            = _cust.Street();
            dirPartyPostalAddressView.StreetNumber      = _cust.StreetNumber();
            dirPartyPostalAddressView.CountryRegionId   = _cust.CountryCode();
            dirPartyPostalAddressView.Party             = custTable.Party;

            dirParty.createOrUpdatePostalAddress(dirPartyPostalAddressView);

            // Fill Phone Number
            dirPartyContactInfo.clear();
              select dirPartyContactInfo
            where dirPartyContactInfo.IsPrimary       == NoYes::Yes
                && dirPartyContactInfo.Type            == LogisticsElectronicAddressMethodType::Phone
                && dirPartyContactInfo.Party == custTable.Party;

            dirPartyContactInfo.LocationName    =_cust.Name();
            dirPartyContactInfo.Locator         =_cust.Phone();
            dirPartyContactInfo.Type            = LogisticsElectronicAddressMethodType::Phone;
            dirPartyContactInfo.IsPrimary       = NoYes::Yes;
            dirPartyContactInfo.Party             =custTable.Party;
            // Fill Contacts
            dirParty.createOrUpdateContactInfo(dirPartyContactInfo);

            //Fill EmailId
            dirPartyContactInfo.clear();
            select dirPartyContactInfo
            where dirPartyContactInfo.IsPrimary == NoYes::Yes
                && dirPartyContactInfo.Type     == LogisticsElectronicAddressMethodType::Email
                && dirPartyContactInfo.Party    == custTable.Party
                ;
            dirPartyContactInfo.LocationName    = _cust.Name();
            dirPartyContactInfo.Locator         = _cust.Email();
            dirPartyContactInfo.Type            = LogisticsElectronicAddressMethodType::Email;
            dirPartyContactInfo.IsPrimary       = NoYes::Yes;
            dirPartyContactInfo.Party           = custTable.Party;
            dirParty.createOrUpdateContactInfo(dirPartyContactInfo);
            // Marks the end of transaction.
            ttsCommit;

     }
    catch (Exception::Error)
    {
        ttsAbort;
        throw Exception::Error;
    }


}




Validate Customer

private str ValidateCustomer(AIFCustomerSContract _cust)
{

       boolean valid = true;

    str Message = "";
    if(_cust.Phone() == "")
    {
        Message = Message + "Phone number is required!\n";
        valid = false;
    }
    if(_cust.Name() == "")
    {
        Message = Message + "Name  is required!\n";
        valid = false;
    }

    if(_cust.AccountNum() == "")
    {
        Message = Message + "AccountNum  is required!\n";
        valid = false;
    }

    if(_cust.Email() == "")
    {
        Message = Message + "EmailID  is required!\n";
        valid = false;
    }

    if(_cust.CountryCode() == "")
    {
        Message = Message + "CountryCode  is required!\n";
        valid = false;
    }

    //if(!CustGroup::exist(_cust.CustGroup()))
    //{
        //Message = Message + "Customer group does not exits!\n";
        //valid = false;
    //}

    if(_cust.PartyType() == DirPartyType::None )
    {
        Message = Message + "Party Type is required!\n";
        valid = false;
    }
    return Message;
}




Calling   for job


static void  job_CustomerInsertAndUpdate(Args _args)
{


    AIFCustomerSContract      _AIFCustomerSContract = new AIFCustomerSContract();
    AIFCustomerSService _AIFCustomerSService = new AIFCustomerSService();

    _AIFCustomerSContract.AccountNum("");
    _AIFCustomerSContract.Currency("");
    _AIFCustomerSContract.CustGroup("");
    _AIFCustomerSContract.segmentId("");
    _AIFCustomerSContract.subsegmentId("");
    _AIFCustomerSContract.paymMode("");
    _AIFCustomerSContract.PaymTerm("");
    _AIFCustomerSContract.dlvTerm("");
    _AIFCustomerSContract.DlvMode("");
    _AIFCustomerSContract.lineOfBusinessId('');
    _AIFCustomerSContract.identificationNumber('');

    _AIFCustomerSContract.Name("");

    _AIFCustomerSContract.CountryCode('');
    _AIFCustomerSContract.CountryRegionId('');
    _AIFCustomerSContract.Street('');
    _AIFCustomerSContract.City('');
    _AIFCustomerSContract.Phone("");
    _AIFCustomerSContract.ZipCode("");
    _AIFCustomerSContract.Email('');
    _AIFCustomerSContract.Company("");
    _AIFCustomerSContract.PartyType(DirPartyType::Person);



    _AIFCustomerSService.CreateAndUpDateCustomers(_AIFCustomerSContract);


    info(_AIFCustomerSContract.AccountNum());





}