Sunday, 8 March 2015

Create Purchase Order through code

static void purchorder(Args _args)
{

    PurchTable      purchTable;
    PurchLine       purchLine;
    VendTable       vendTable = VendTable::find("3008");
    AxPurchTable    axPurchTable;
    AxPurchLine     axPurchLine;
    PurchFormLetter purchFormLetter;

    //Create Purchase order
    purchTable.initFromVendTable(vendTable);

    axPurchTable = axPurchTable::newPurchTable(purchTable);
    axPurchTable.parmPurchaseType(PurchaseType::Purch);
    axPurchTable.parmDocumentStatus(DocumentStatus::PurchaseOrder);
    axPurchTable.parmAccountingDate(systemDateGet());
    axPurchTable.parmDeliveryDate(01\06\2012);
    axPurchTable.parmPurchStatus(PurchStatus::Backorder);
    axPurchTable.doSave();

    //Create PurchLine for item 1000
    purchLine.initFromPurchTable(purchTable);

    axPurchLine = AxPurchLine::newPurchLine(purchLine);
    axpurchLine.parmItemId("1000");
    axPurchLine.parmPurchQty(10);
    axPurchLine.parmPurchPrice(1900);
    axPurchLine.doSave();
    info(strFmt("%1",purchTable.PurchId));

    //posting po confirmation,i guess its mandatory
    //you cannot do invoice without doing po confirm
    //purchtable = axpurchtable.purchtable();
    //purchformletter = purchformletter::construct(documentstatus::purchaseorder);
    //purchformletter.update(purchtable, strfmt("inv_%1", purchtable.purchid));
//
    //posting po invoice
    //purchformletter = purchformletter::construct(documentstatus::invoice);
    //purchformletter.update(purchtable, strfmt("inv_%1", purchtable.purchid));

Sunday, 1 March 2015

Create Rental Revenue Journal

static void createRentalRevenueJournal()
          {
              NumberSequenceTable                 numberSequenceTable;

              Voucher                             voucher;
              Amount                              amountCurDebit,amountCurCredit;

              AxLedgerJournalTrans                trans  = new AxLedgerJournalTrans();
              AxLedgerJournalTable                header ;//= new AxLedgerJournalTable();


              str                                 costcenter,department,assetcode,customer,description,IntegPostingProf;


              DimensionStorage                    DimensionStorage;
              LedgerJournalNameId                 LedgerJournalNameId;
              AX_CustomerPaymentsStagingTable_IN  RentalRevenueStagingTable;
              CurrencyCode                        currencycode;
              TransDate                           TransDate = today();
              args                                args;
              LedgerAccountContract               ledgerAccountContract,OffledgerAccountContract;
              MainAccountNum                      mainAccount;
              mainAccountNum                      OffmainAccount ;
              DimensionAttributeValueContract     attributeValueContract;
              DimensionMapping                    dimensionMappingRg,dimensionMappingBr;
              LedgerJournalCheckPost              ledgerJournalCheckPost;

              boolean     errorCheck;
              CustomerPaymentsRefunds customerPaymentsRefunds;



                      numberSequenceTable = NumberSequenceTable::find(LedgerJournalName::find(LedgerJournalNameId).NumberSequenceTable);

                      voucher =  NumberSeq::newGetVoucherFromCode(numberSequenceTable.NumberSequence).voucher();


                      amountCurDebit = 0;
                      amountCurCredit = 0;

                      description  = "Accrual Revenue";
                      currencycode = CompanyInfo::standardCurrency();

                      amountCurDebit = RentalRevenueStagingTable.Amount;

                      amountCurCredit = 0;

                      mainAccount = IntegPostingProf.RentalRevenue;
                      OffmainAccount  = IntegPostingProf.Revenue;



                      costcenter = dimensionMappingRg.DimValue;
                      department = dimensionMappingBr.DimValue;
                      assetcode  = RentalRevenueStagingTable.AssetCode;
                      customer   = RentalRevenueStagingTable.AccountNum;

                      trans.parmJournalNum(header.ledgerJournalTable().JournalNum);

                      //AccountNum
                      ledgerAccountContract = new LedgerAccountContract();
                      ledgerAccountContract.parmValues(new List(Types::Class));


                      ledgerAccountContract.parmMainAccount(mainAccount);
                      if (department)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("@SYS850");
                          attributeValueContract.parmValue(department);

                          ledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }
                      if (costcenter)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("@SYS343410");
                          attributeValueContract.parmValue(costcenter);

                          ledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }
                      if (assetcode)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("@SYS343374");
                          attributeValueContract.parmValue(assetcode);

                          ledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }
                      if (customer)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("Customer");
                          attributeValueContract.parmValue(customer);

                          ledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }

                      dimensionStorage = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(ledgerAccountContract);

                      trans.parmLedgerDimension(dimensionStorage.save());
                      //-------------------------------------
                      //OffsetAccount
                      OffledgerAccountContract = new LedgerAccountContract();
                      OffledgerAccountContract.parmValues(new List(Types::Class));


                      OffledgerAccountContract.parmMainAccount(OffmainAccount);
                      if (department)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("@SYS850");
                          attributeValueContract.parmValue(department);

                          OffledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }
                      if (costcenter)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("@SYS343410");
                          attributeValueContract.parmValue(costcenter);

                          OffledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }
                      if (assetcode)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("@SYS343374");
                          attributeValueContract.parmValue(assetcode);

                          OffledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }
                      if (customer)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("Customer");
                          attributeValueContract.parmValue(customer);

                          OffledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }

                      dimensionStorage = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(OffledgerAccountContract);

                      trans.parmOffsetLedgerDimension(dimensionStorage.save());
                      //------------------------------------
                      trans.parmTxt(description);

                      trans.parmDocumentDate(RentalRevenueStagingTable.Date);
                      trans.parmTransDate(RentalRevenueStagingTable.Date);

                      trans.parmAmountCurDebit(AmountCurDebit);

                      trans.parmAmountCurCredit(AmountCurCredit) ;

                      trans.parmCompany(curext());
                      trans.parmCurrencyCode(currencycode);
                      trans.parmVoucher(voucher);
                      trans.parmExchRate(1);

                      // Customized fields
                      trans.parmKRContractNo(RentalRevenueStagingTable.KR_ContractNo);
                      trans.parmKRCustomerCode(RentalRevenueStagingTable.KR_CustomerCode);
                      trans.parmKRCustomerName(RentalRevenueStagingTable.KR_CustomerName);
                      trans.parmKROperationalActivity(RentalRevenueStagingTable.KR_OperationalActivity);
                      trans.parmKRRevenueType(RentalRevenueStagingTable.KR_TypeId);
                      trans.parmKRContractstatus(RentalRevenueStagingTable.ContractStatus);
                      trans.parmKRInvoiceNo(RentalRevenueStagingTable.KR_InvoiceNumber);
                      // Customized fields
                      trans.currentRecord().insert();

                      trans.save();

                      //ledgerJournalCheckPost= ledgerJournalCheckPost::newLedgerJournalTable(header.ledgerJournalTable(),NoYes::Yes);
                      //ledgerJournalCheckPost.run();

                      // create Free text invoice



                      ttsCommit;
                  }


                  next RentalRevenueStagingTable;
              }

          }

Sunday, 22 February 2015

Create Employee in Ax

private void createEmployee(System.Data.SqlClient.SqlDataReader   dr1)
{
    CompanyInfo                             companyInfo;
    HcmEmploymentRecId                      newEmploymentRecId;
    ValidFromDateTime                       employmentStartDateTime;
    ValidToDateTime                         employmentEndDateTime;
    HcmWorker                               newHcmWorker;
    DirPerson                               dirPerson;
    DirPersonName                           dirPersonName;
    HcmEmploymentType                       hcmEmploymentType = HcmEmploymentType::Employee;
    NumberSeq                               numberSeqPersonnelNum;
    HcmPersonPrivateDetails                 HcmPersonPrivateDetails;
    HcmEmployment                           hcmEmployment;

    HcmWorkerTitle  hcmWorkerTitle;

    DirParty                        dirparty;
    DirPartyContactInfoView         contactView;
    HcmPersonDetails                persondetails;
    HcmPersonIdentificationNumber   hcmPersonIdentificationNumber;

    utcdatetime                     _validFrom = DateTimeUtil::utcNow();

    Struct              struct;
    container           ledgerDimension;
    DimensionDefault    DimensionDefault;
    int                 k;


    TransDate       HireDate,DOB;
    HcmPersonID     Employeecode;
    str             FirstName,Title,MobilePhone,email,MaritalStatus,NationalityCode,NationalityName,PaymentMode,MiddleName,LastName;

    str             DepartmentCode,DepartmentName,SubDivisionCode,SubDivisionName,DivisionCode,DivisionName ,SectorCode, SectorName;
    str             DesignationCode,DesignationName,SponsorCode,SponsorDesc,MOLNo,PASSPORTNUMBER,Gender,BloodGroup,ReligionCategory,EmployeeCategory,Emiratization;


    ;

    Employeecode    = dr1.get_Item('Employeecode');     FirstName       = dr1.get_Item('FirstName'); MiddleName = dr1.get_Item('Middlename'); LastName = dr1.get_Item('Lastname');
    Title           = dr1.get_Item('Title');            MobilePhone     = dr1.get_Item('MobilePhone');
    email          = dr1.get_Item('email');            DOB             = dr1.get_Item('DOB');
    MaritalStatus   = dr1.get_Item('MaritalStatus');    NationalityCode = dr1.get_Item('NationalityCode');
    NationalityName = dr1.get_Item('NationalityName');  HireDate        = dr1.get_Item('HireDate');
    PaymentMode     = dr1.get_Item('PaymentMode');      DepartmentCode  = dr1.get_Item('DepartmentCode');
    DepartmentName  = dr1.get_Item('DepartmentName');   //SubDivisionCode = dr1.get_Item('SubDivisionCode');
  //  SubDivisionName = dr1.get_Item('SubDivisionName'); // DivisionCode    = dr1.get_Item('DivisionCode');
  //  DivisionName    = dr1.get_Item('DivisionName');    // SectorCode      = dr1.get_Item('SectorCode');
   // SectorName      = dr1.get_Item('SectorName');       DesignationCode = dr1.get_Item('DesignationCode');
    DesignationName  = dr1.get_Item('DesignationName'); //SponsorCode  = dr1.get_Item('SponsorCode');
    SponsorDesc     = dr1.get_Item('SponsorName');
    MOLNo           = dr1.get_Item('MOLNo');
    PASSPORTNUMBER  = dr1.get_Item('PASSPORTNUMBER');
    Gender          = dr1.get_Item('Gender');
    BloodGroup        = dr1.get_Item('BloodGroup');
    ReligionCategory  = dr1.get_Item('ReligionCategory');
    EmployeeCategory  = dr1.get_Item('EmployeeCategory');
    Emiratization   = dr1.get_Item('Emiratization');





    companyInfo = companyInfo::find();

    newHcmWorker = HcmWorker::findByPersonnelNumber(Employeecode);

    if(! newHcmWorker)
    {
        employmentStartDateTime = datetobeginUtcDateTime(HireDate, DateTimeUtil::getUserPreferredTimeZone());
        employmentEndDateTime   = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::maxValue(), DateTimeUtil::getUserPreferredTimeZone());

        dirPersonName.FirstName     = FirstName;
        dirPersonName.MiddleName    = Middlename;
        dirPersonName.LastName      = LastName;
        newHcmWorker = HcmWorker::find(HcmWorkerTransition::newCreateHcmWorker(dirPersonName
                                                                               , Employeecode
                                                                               , companyInfo.RecId
                                                                               , hcmEmploymentType
                                                                               , employmentStartDateTime
                                                                               , employmentEndDateTime));
    }
    else
    {

        dirPersonName               = DirPersonName::find(newHcmWorker.Person,true);
        dirPersonName.FirstName     = FirstName;
        dirPersonName.MiddleName    = Middlename;
        dirPersonName.LastName      = LastName;
        dirPersonName.update();
    }

    hcmPersonPrivateDetails                     = HcmPersonPrivateDetails::findByPerson(newHcmWorker.Person,true);
    hcmPersonPrivateDetails.initValue();
    hcmPersonPrivateDetails.Person              = dirPersonName.Person;
    hcmPersonPrivateDetails.BirthDate           = DOB;
    hcmPersonPrivateDetails.Gender              = SQLIntegration::convGender(Gender);
    hcmPersonPrivateDetails.CitizenshipCountryRegion = NationalityCode;
    hcmPersonPrivateDetails.BloodGroup          = SQLIntegration::convBlood(BloodGroup);
    hcmPersonPrivateDetails.ReligionCategory    = SQLIntegration::convReligion(ReligionCategory);
    hcmPersonPrivateDetails.EmployeeCategory    = SQLIntegration::convEmployeeCategory(EmployeeCategory);
    hcmPersonPrivateDetails.Emiratization       = SQLIntegration::convEmiratization(Emiratization);
    if(hcmPersonPrivateDetails)
        hcmPersonPrivateDetails.update();
    else
        hcmPersonPrivateDetails.insert();

    persondetails               = HcmPersonDetails::findByPerson(newHcmWorker.Person);
    persondetails.initValue();
    persondetails.Person        = dirPersonName.Person;
    persondetails.MaritalStatus = SQLIntegration::convMarital(MaritalStatus);
    persondetails.ValidFrom     = DateTimeUtil::utcNow();
    persondetails.ValidTo       = DateTimeUtil::maxValue();
    //if (persondetails)
    //{
        //persondetails.selectForUpdate(true);
        //persondetails.update();
    //}
    //else
    //{
       persondetails.insert();
    //}


    hcmWorkerTitle          = HcmWorkerTitle::findByWorker(newHcmWorker.RecId);
    hcmWorkerTitle.initValue();
    hcmWorkerTitle.Worker   = newHcmWorker.RecId;
    hcmWorkerTitle.Title    = HcmTitle::findByTitle(DesignationCode).RecId;
    hcmWorkerTitle.ValidFrom = DateTimeUtil::utcNow();
    hcmWorkerTitle.ValidTo  = DateTimeUtil::maxValue();
    //if(hcmWorkerTitle)
    //{
        //hcmWorkerTitle.selectForUpdate(true);
        //hcmWorkerTitle.update();
    //}
    //else
    //{
        hcmWorkerTitle.insert();
    //}

        hcmPersonIdentificationNumber = HcmPersonIdentificationNumber::findByPersonAndType(newHcmWorker.RecId,HcmIdentificationType::findByIdentificationType("SponsorCode").RecId);
        hcmPersonIdentificationNumber.Person = dirPersonName.Person;
        hcmPersonIdentificationNumber.IdentificationType =  HcmIdentificationType::findByIdentificationType("SponsorCode").RecId;
        hcmPersonIdentificationNumber.IdentificationNumber = SponsorCode;
        hcmPersonIdentificationNumber.Description = SponsorDesc;
    if(hcmPersonIdentificationNumber)
    {
        hcmPersonIdentificationNumber.selectForUpdate(true);
        hcmPersonIdentificationNumber.update();
    }
    else
    {
        hcmPersonIdentificationNumber.insert();
    }

          hcmPersonIdentificationNumber.clear();
        hcmPersonIdentificationNumber = HcmPersonIdentificationNumber::findByPersonAndType(newHcmWorker.RecId,HcmIdentificationType::findByIdentificationType("Passport").RecId);


        hcmPersonIdentificationNumber.Person = dirPersonName.Person;
        hcmPersonIdentificationNumber.IdentificationType =  HcmIdentificationType::findByIdentificationType("Passport").RecId;
        hcmPersonIdentificationNumber.IdentificationNumber = PASSPORTNUMBER;

     if(hcmPersonIdentificationNumber)
    {
        hcmPersonIdentificationNumber.selectForUpdate(true);
        hcmPersonIdentificationNumber.update();
    }
    else
    {
        hcmPersonIdentificationNumber.insert();
    }

        hcmPersonIdentificationNumber.clear();
         hcmPersonIdentificationNumber = HcmPersonIdentificationNumber::findByPersonAndType(newHcmWorker.RecId,HcmIdentificationType::findByIdentificationType("EmpPersonNo").RecId);


        hcmPersonIdentificationNumber.Person = dirPersonName.Person;
        hcmPersonIdentificationNumber.IdentificationType =  HcmIdentificationType::findByIdentificationType("EmpPersonNo").RecId;
        hcmPersonIdentificationNumber.IdentificationNumber = MOLNo;

    if(hcmPersonIdentificationNumber)
    {
        hcmPersonIdentificationNumber.selectForUpdate(true);
        hcmPersonIdentificationNumber.update();
    }
    else
    {
        hcmPersonIdentificationNumber.insert();
    }



    dirPerson                   = dirPerson::find(dirPersonName.Person, true);
    dirPerson.PersonalTitle     = DirNameAffix::find(DirNameAffixType::PersonalPrefix ,Title).RecId;
    dirPerson.update();

    //select firstonly ValidTimeState(_validFrom) hcmEmployment
                //where hcmEmployment.Worker == newHcmWorker.RecId
                //&&    hcmEmployment.LegalEntity == companyInfo.RecId;
//
    ////Department Code    Department Name    Sub Division Code    Sub Division Name    Division Code    Division Name    Sector Code
//
    //this.findorCreateDimension('Department',DepartmentCode,DepartmentName);
    //this.findorCreateDimension('SubDivision',SubDivisionCode,SubDivisionName);
    //this.findorCreateDimension('Division',DivisionCode,DivisionName);
    //this.findorCreateDimension('Sector',SectorCode,SectorName);
//
//
    //struct = new Struct();
    //struct.add('Department',DepartmentCode);
    //struct.add('Division', DivisionCode);
    //struct.add('SubDivision', SubDivisionCode);
    //struct.add('Sector', SectorCode);
//
    //ledgerDimension = conNull();
    //ledgerDimension += struct.fields();
//
    //for (k = 1; k <= struct.fields(); k++)
    //{
        //ledgerDimension += struct.fieldName(k);
        //ledgerDimension += struct.valueIndex(k);
    //}
//
    //DimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(ledgerDimension);
    //hcmEmployment.DefaultDimension = DimensionDefault;
    //hcmEmployment.update();



    DirParty = DirParty::constructFromPartyRecId(dirPerson.recid);

    if(email && (email != "NA"))
    {
        contactView.LocationName = "Email";
        contactView.Locator      = strLRTrim(email);
        contactView.Type         = LogisticsElectronicAddressMethodType::Email;
        contactView.Party        = dirPerson.recid;
        contactView.IsPrimary    = NoYes::Yes;
        dirParty.createOrUpdateContactInfo(contactView);
    }
    if(MobilePhone && MobilePhone != "NA")
    {
        contactView.LocationName = "Phone";
        contactView.Locator      = strLRTrim(MobilePhone);
        contactView.Type         = LogisticsElectronicAddressMethodType::Phone;
        contactView.Party        = dirPerson.recid;
        contactView.IsPrimary    = NoYes::Yes;
        dirParty.createOrUpdateContactInfo(contactView);

    }
}