Sunday, 10 February 2019

Create hour journals from time sheet lines D365

class INTC_HourJournalCreate
{
    public void getTimesheet(TransDate          _fromDate,
                             TransDate          _toDate,
                             TSTimesheetNbr     _timesheetNumber,
                             NoYesId            _posted)
    {
        TSTimesheetTable                        timesheetTable;
        TSTimesheetLine                         timesheetLine;
        TSTimesheetLineWeek                     timesheetLineWeek;
        real                                    hours;
        TransDate                               createdDate;

        if(_fromDate && _toDate && _timesheetNumber)
        {
            while select timesheetTable
                join timesheetLine
                where timesheetLine.TimesheetNbr == timesheetTable.TimesheetNbr
                join timesheetLineWeek
                where timesheetLine.RecId   ==  timesheetLineWeek.TSTimesheetLine
                && timesheetTable.ApprovalStatus == TSAppStatus::Approved
                && timesheetTable.TimesheetNbr == _timesheetNumber
                && timesheetTable.PeriodFrom >= _fromDate
                && timesheetTable.PeriodTo   <= _toDate
         
                {
                    createdDate =   DateTimeUtil::date(timesheetLine.CreatedDateTime);
                    this.createJournal(timesheetTable.TimesheetNbr,
                                       timesheetTable.Resource,
                                       createdDate,
                                       timesheetLine.ProjId,
                                       timesheetLine.CategoryId,
                                       timesheetLineWeek.totalHours(),
                                       timesheetLine.RecId,
                                       _posted);
                }
        }
        else if(!_fromDate && !_toDate && !_timesheetNumber)
        {
            while select timesheetTable
                join timesheetLine
                where timesheetLine.TimesheetNbr == timesheetTable.TimesheetNbr
                join timesheetLineWeek
                where timesheetLine.RecId   ==  timesheetLineWeek.TSTimesheetLine
                && timesheetTable.ApprovalStatus == TSAppStatus::Approved
            {
                createdDate =   DateTimeUtil::date(timesheetLine.CreatedDateTime);
                this.createJournal(timesheetTable.TimesheetNbr,
                                   timesheetTable.Resource,
                                   createdDate,
                                   timesheetLine.ProjId,
                                   timesheetLine.CategoryId,
                                   timesheetLineWeek.totalHours(),
                                   timesheetLine.RecId,
                                   _posted);
            }
        }

        else if(_fromDate && _toDate && !_timesheetNumber)
        {
            while select timesheetTable
                join timesheetLine
                where timesheetLine.TimesheetNbr == timesheetTable.TimesheetNbr
                join timesheetLineWeek
                where timesheetLine.RecId   ==  timesheetLineWeek.TSTimesheetLine
                && timesheetTable.ApprovalStatus == TSAppStatus::Approved
                && timesheetTable.PeriodFrom >= _fromDate
                && timesheetTable.PeriodTo   <= _toDate
            {
                createdDate =   DateTimeUtil::date(timesheetLine.CreatedDateTime);
                this.createJournal(timesheetTable.TimesheetNbr,
                                   timesheetTable.Resource,
                                   createdDate,
                                   timesheetLine.ProjId,
                                   timesheetLine.CategoryId,
                                   timesheetLineWeek.totalHours(),
                                   timesheetLine.RecId,
                                   _posted);
            }
        }
        else if(!_fromDate && !_toDate && _timesheetNumber)
        {
            while select timesheetTable
                join timesheetLine
                where timesheetLine.TimesheetNbr == timesheetTable.TimesheetNbr
                join timesheetLineWeek
                where timesheetLine.RecId   ==  timesheetLineWeek.TSTimesheetLine
                && timesheetTable.ApprovalStatus == TSAppStatus::Approved
                && timesheetTable.TimesheetNbr == _timesheetNumber
            {
                createdDate =   DateTimeUtil::date(timesheetLine.CreatedDateTime);
                this.createJournal(timesheetTable.TimesheetNbr,
                                   timesheetTable.Resource,
                                   createdDate,
                                   timesheetLine.ProjId,
                                   timesheetLine.CategoryId,
                                   timesheetLineWeek.totalHours(),
                                   timesheetLine.RecId,
                                   _posted);
            }
        }
    }

    public void createJournal(TSTimesheetNbr    _timesheetNbr
                             ,RecId             _worker
                             ,TransDate         _date
                             ,ProjId            _projId
                             ,ProjCategoryId    _categoryId
                             ,real              _totalHours
                             ,RecId             _lineRecId
                             ,NoYesId           _posted)
    {
        ProjJournalTableData                    JournalTableData;
        ProjJournalTransData                    journalTransData;
        ProjJournalTable                        journalTable, journalTableUpdate, journalTableLocal;
        ProjJournalTrans                        journalTrans, journalTransLocal;
        ProjTable                               projTable;
        ProjInvoiceTable                        projInvoiceTable;
        NumberSeq                               numberSeq;
        ProjJournalCheckPost                    jourPost;
        ProjQtyEmpl                             qty;
        JournalNumOfLines                       numOfLines;
        ProjHourCostPrice                       projHourCostPrice;
        boolean                                 flag;
        boolean                                 validJournal;
     
        select journalTableLocal where journalTableLocal.INTC_TSTimesheetNbr == _timesheetNbr;
        select projHourCostPrice where projHourCostPrice.CategoryId ==  _categoryId;
     
        ttsBegin;
        journalTableData = JournalTableData::newTable(journalTable);
        journalTransData = journalTableData.journalStatic().newJournalTransData(journalTrans, journalTableData);
        // Init JournalTable -- header
        journalTable.clear();
        journalTable.JournalId              =   journalTableData.nextJournalId();
        journalTable.JournalType            =   ProjJournalType::Hour;
        journalTable.JournalNameId          =   "Hours";
        journalTable.Description            =   "Hour, Timesheet";
        journalTable.initFromProjJournalName(ProjJournalName::find(journalTable.JournalNameId));
        journalTable.INTC_TSTimesheetNbr    =   _timesheetNbr;
        journalTable.insert();

        // Init JournalTrans -- lines
        journalTableData.initFromJournalName(journalTableData.journalStatic().findJournalName(journalTable.JournalNameId));
        journalTrans.clear();
        journalTransData.initFromJournalTable();
        projTable                               =       ProjTable::find(_projId);
        projInvoiceTable                        =       projTable.projInvoice();
        journalTrans.setTransDate();
        journalTrans.TransDate                  =       systemDateGet();
        journalTrans.ProjTransDate              =       _date;
        journalTrans.ProjId                     =       projTable.ProjId;
        journalTrans.Qty                        =       _totalHours;
        journalTrans.CategoryId                 =       _categoryId;
        journalTrans.INTC_RevenueRecoveryPerc   =       projTable.INTC_ProjRecoveryPerc;
        if(projTable.INTC_ProjRecoveryPerc !=0)
            journalTrans.CostPrice              =       (projHourCostPrice.CostPrice*journalTrans.INTC_RevenueRecoveryPerc)/100;
        else
            journalTrans.CostPrice              =       projHourCostPrice.CostPrice;
        journalTrans.LinePropertyId             =       'Billable';
        journalTrans.Resource                   =       _worker;
        journalTrans.ActivityNumber             =       '';
        journalTrans.Txt                        =       'Hour Journal from Timesheet';
        journalTrans.DefaultDimension           =       projTable.DefaultDimension;
        journalTrans.TaxGroupId                 =       ProjParameters::taxGroupInvoice(projTable.ProjId);
        if (projInvoiceTable.CurrencyId)
            journalTrans.CurrencyId             =       projInvoiceTable.CurrencyId;
        else
            journalTrans.CurrencyId             =       Ledger::accountingCurrency(CompanyInfo::current());
        if (journalTrans.Worker)
        {
            journalTrans.setHourPrices();
            journalTrans.setPeriodDate();
        }
        numberSeq = NumberSeq::newGetVoucherFromId(journalTable.VoucherNumberSequenceTable, false);
        journalTrans.Voucher            =  numberSeq.voucher();
        journalTrans.INTC_RefRecid      =  _lineRecId;
        journalTransData.create();
        ttsCommit;
        info(strFmt("Approved Hour Journal : %1 Created", journalTable.JournalId));

        validJournal        =   this.checkPMSValue(journalTrans.ProjId);

        if(journalTable.Posted == NoYes::No && _posted == NoYes::Yes && validJournal == NoYes::Yes)
        {
            jourPost = ProjJournalCheckPost::newJournalCheckPost(true,true,JournalCheckPostType::Check,tableNum(ProjJournalTable),journalTable.JournalId);
            jourPost = ProjJournalCheckPost::newJournalCheckPost(true,true,JournalCheckPostType::Post,tableNum(ProjJournalTable), journalTable.JournalId);
            jourPost.run();
        }
    }

    public boolean checkPMSValue(ProjId  _projId)
    {
        ProjJournalTrans                projJournalTrans;
        FormDataSource                  projJournalTrans_ds;

        ProjJournalTable                journalTable, journalTableUpdate, journalTableLocal;
        ProjJournalTrans                journalTrans, journalTransLocal;
        real                            timesheetValue, contractValue;
        ProjTable                       projTable;
        ProjInvoiceTable                projInvoiceTable;
        PSAContractLineItems            projContractLine;
        Name                            projId, contractId;
        ProjOnAccTrans                  projOnAccTrans;

        boolean                         validJournal;


        while select journalTable
            join journalTrans
            where journalTable.JournalId == journalTrans.JournalId
            && journalTrans.ProjId == _projId
        {
            timesheetValue  +=   journalTrans.Qty * journalTrans.CostPrice;
        }
        while select projOnAccTrans
            where projOnAccTrans.ProjID ==  _projId
        {
            projId          =   projOnAccTrans.ProjId;
            contractValue   +=  projOnAccTrans.TotalSalesAmountCur;
        }

        if(timesheetValue > contractValue)
        {
            validJournal    =   false;
        }
        else
        {
            validJournal    =   true;
        }

        return validJournal;
    }

}

No comments:

Post a Comment