Create and Post Hour journals using X++

In this blog, we will explore how to create and post an hour journal using X++ code. I have taken examples from a staging table, where we can utilize the data to create hour journals using the following method.

  public boolean createHourJournal(DaxHourStaging stagingData)
  {
      ProjJournalTableData            JournalTableData;
      ProjJournalTransData            journalTransData;
      ProjJournalTable                journalTable, journalTableUpdate;
      ProjJournalTrans                journalTrans;
      ProjTable                       projTable;
      ProjInvoiceTable                projInvoiceTable;
      NumberSeq                       numberSeq;
      ProjJournalCheckPost            jourPost;
      ProjQtyEmpl                     qty;
      JournalNumOfLines               numOfLines;
      DataAreaId                      company = '';
      journalNum = '';
      DaxHourStaging  stagingDataHour;
      boolean ret = false;

      while select stagingDataHour
          where stagingDataHour.InvoiceNumber == stagingData.InvoiceNumber
              && stagingDataHour.TransID == stagingData.TransID
              && stagingDataHour.TransactionType == DAXTransactionType::Hour
              && stagingDataHour.DAXProcessedUnprocessed == DAXProcessedUnprocessed::UnProcessed
              && stagingDataHour.Type == DAXType::Invoice
      {
          select crosscompany projTable
              where projTable.DAXTransID == stagingDataHour.TransID;

          changecompany(projTable.DataAreaId)
          {
              company = projTable.DataAreaId;
              if (!journalNum)
              {
                  journalTableData = JournalTableData::newTable(journalTable);
                  journalTransData = journalTableData.journalStatic().newJournalTransData(journalTrans, journalTableData);
                  journalTable.clear();
                  journalTable.JournalId      = journalTableData.nextJournalId();
                  journalTable.JournalType    = ProjJournalType::Hour;
                  journalTable.JournalNameId  = ProjParameters::find().EmplJournalNameId;
                  journalTable.initFromProjJournalName(ProjJournalName::find(journalTable.JournalNameId));
                  journalTable.insert();
                  journalNum = journalTable.JournalId;
              }
              ResourceView            ResResourcesListView;
              ResResourceIdentifier   ResResourceIdentifier;
              ResourceCategoryView    ResourceCategoryView;
              WrkCtrTable             wrkctrTable;
              str                     resourceId = '';
              str                     resourceCompany = '';
              HcmWorker               hcmWorker;
              HcmEmployment           hcmEmployment;
              utcdatetime             now = DateTimeUtil::utcNow();
              CompanyInfo             companyInfo;

              select firstonly ResResourcesListView
                  where ResResourcesListView.ResourceId == stagingDataHour.DAXologyResourceId
                      && ResResourcesListView.ResourceCompanyId == projTable.DataAreaId;

              if (!ResResourcesListView.RecId)
              {
                  select ValidTimeState(now) hcmEmployment
                      join hcmWorker
                      where hcmWorker.RecId == hcmEmployment.Worker
                          && hcmWorker.PersonnelNumber == stagingDataHour.DAXologyResourceId
                      join companyInfo where companyInfo.RecId == hcmEmployment.LegalEntity;

                  if (hcmWorker.RecId)
                  {
                      resourceId      = hcmWorker.PersonnelNumber;
                      resourceCompany = companyInfo.DataArea;
                  }
              }
              select firstonly ResResourceIdentifier
                  where ResResourceIdentifier.RecId == ResResourcesListView.RecId;

              journalTableData.initFromJournalName(journalTableData.journalStatic().findJournalName(ProjJournalTable::find(journalNum).JournalNameId));
              journalTrans.clear();
              journalTransData.initFromJournalTable();

              projInvoiceTable    = projTable.projInvoice();
              journalTrans.setTransDate();
              journalTrans.TransDate          = stagingDataHour.TransDate;
              journalTrans.ProjTransDate      = stagingDataHour.TransDate;
              journalTrans.ProjId             = projTable.ProjId;
              journalTrans.Qty                = stagingDataHour.Quantity;
              journalTrans.DAXInvoiceId      = stagingDataHour.InvoiceNumber;
              journalTrans.DAXInvoiceDate    = stagingDataHour.InvoiceDate;
              journalTrans.DAXTransactionId  = stagingDataHour.TransactionId;
              journalTrans.CategoryId         = ProjParameters::find().EmplCategory;
              // journalTrans.Resource           = ResResourceIdentifier.RefRecId;
              journalTrans.Worker             = ResResourcesListView.Worker;
              journalTrans.LinePropertyId     = 'Chargeable';
              //journalTrans.DAXResourceCompany = resourceCompany;
              journalTrans.DAXWrkCtrId       = stagingDataHour.DAXologyResourceId;
              journalTrans.DAXResourceName   = stagingDataHour.DAXologyResourceName;
              journalTrans.DAXologyRoleName  = stagingDataHour.DAXologyRoleName;
              journalTrans.Txt                = stagingDataHour.Description;
              journalTrans.CurrencyId         = projInvoiceTable.CurrencyId;
              journalTrans.DefaultDimension   = projTable.DefaultDimension;
              journalTrans.TaxGroupId         = ProjParameters::taxGroupInvoice(projTable.ProjId);
              journalTrans.SalesPrice         = stagingDataHour.SalesPrice;
              InventTableModule   inventTableModule;

              select inventTableModule
                  where inventTableModule.ItemId == InventTable::find(DAX_ProjectHourJournalCreateService::getDimensionValueFromDefaultDimension(projTable.DefaultDimension)).ItemId
                      && inventTableModule.ModuleType == ModuleInventPurchSales::Sales;

              journalTrans.TaxItemGroupId     = inventTableModule.TaxItemGroupId;
              numberSeq = NumberSeq::newGetVoucherFromId(journalTable.VoucherNumberSequenceTable, false);

              journalTrans.Voucher            = numberSeq.voucher();
              journalTransData.create();
              if (TaxParameters::checkTaxParameters_IN())
              {
                  ProjJournalTransTaxExtensionIN     projJournalTransHourTaxExtensionIN = null;
                  projJournalTransHourTaxExtensionIN  = ProjJournalTransTaxExtensionIN::findByProjJournalTrans(journalTrans.RecId);
                  if (!projJournalTransHourTaxExtensionIN.RecId)
                  {
                      projJournalTransHourTaxExtensionIN.initValue();
                      projJournalTransHourTaxExtensionIN.ProjJournalTrans = journalTrans.RecId;
                      projJournalTransHourTaxExtensionIN.AssessableValueTransactionCurrency = journalTrans.Qty * journalTrans.SalesPrice;
                      projJournalTransHourTaxExtensionIN.insert();
                  }
              }
          }

      }
      try
      {
          changecompany(projTable.DataAreaId)
          {
              if (journalNum)
              {
                  jourPost = ProjJournalCheckPost::newJournalCheckPost(true,true,JournalCheckPostType::Post,tableNum(ProjJournalTable), journalNum);
                  jourPost.run();
                  ret = true;
                  ProjJournalTrans                projJournalTrans;
                  ProjJournalTable                projJournalTabeUpd;

                  projJournalTabeUpd = ProjJournalTable::find(journalNum,true);

                  select count(RecId), sum(Qty) from projJournalTrans
                              where projJournalTrans.JournalId == journalNum;

                  projJournalTabeUpd.NumOfLines = int642int(projJournalTrans.RecId);
                  projJournalTabeUpd.ProjQty = projJournalTrans.Qty;
                  projJournalTabeUpd.update();

                  DaxHourStaging  stagingGlobalUpd;
                  update_recordset stagingGlobalUpd setting DAXProcessedUnprocessed = DAXProcessedUnprocessed::Processed
                          where stagingGlobalUpd.InvoiceNumber == stagingData.InvoiceNumber
                              && stagingGlobalUpd.OrderItemId  == stagingData.OrderItemId
                              && stagingGlobalUpd.TransactionType == DAXTransactionType::Hour;

              }
          }
      }
      catch
      {
          //Posting exception
      }
      finally
      {
          // Can go with an final update here
      }
      return ret;
  }
Thanks !!

No comments:

Post a Comment