project QadFinancials > class BBudget > method BudgetPeriodCreateByStartDate

Description

This method will create some budget periods based on the input information (StartDate, Month/Week and Ocuurences) and return a temp-table


Parameters


iiBudgetIDinputintegerBudget ID
icIntervalTypeinputcharacterIntervalType:
W: Week
M: Month
itStartDateinputdateStartDate
iiOccurencesinputintegerOccurences
tNewBudgetPeriodoutputtemp-tableNewBudgetPeriod: temp-table containing the newly created period records
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program6/bbudget.p)

/* ================================================================================================= */                                                                                                           
    /* Note; in case iiBudgetID = 0 then this method is called from the Form for the BudgetCopy-activity */
    /* This means only the output-table will be filled but no actions on the instance-data are performed */
    /* ================================================================================================= */
    
    /* ===================================================================================================== */
    /* Reset the output temp-table                                                                           */
    /* ===================================================================================================== */
    empty temp-table tNewBudgetPeriod.
    if iiBudgetID = ? then assign iiBudgetID = 0.

    /* ========================= */
    /* Validations               */
    /* Find the tBudget record   */
    /* ========================= */
    if icIntervalType <> "W":U and 
       icIntervalType <> "M":U and 
       icIntervalType <> "Q":U /* This set correspond with the list in method GetLists */
    then do :
        assign oiReturnStatus = -3
               vcMessage      = trim(substitute(#T-11'The interval type must be &1 (&2), &3 (&4), or &5 (&6).':255(652)t-11#,trim(#T-12'Week':20(653)t-12#),"'W'":U,trim(#T-13'Month':20(654)t-13#),"'M'":U,trim(#T-14'Quarter':20(655)t-14#),"'Q'":U)).
        <M-1 run SetMessage (input  vcMessage (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-447':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BBudget>
        Return.
    end. /* if icIntervalType <> "W":U and */
    if iiOccurences = 0 or iiOccurences = ?
    then do :
        assign oiReturnStatus = -3
               vcMessage      = trim(#T-15'You must specify the number of budget periods to create, based on the starting date.':255(656)t-15#).
        <M-6 run SetMessage (input  vcMessage (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-449':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BBudget>
        Return.
    end. /* if icIntervalType <> "W":U and */
    else do :
        if iiOccurences > 54
        then do :
            assign oiReturnStatus = -3
                   vcMessage      = trim(substitute(#T-16'The maximum number of occurrences is 54 (&1).':255(657)T-16#,string(iiOccurences))).
            <M-7 run SetMessage (input  vcMessage (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-450':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BBudget>
            Return.
        end. /* if icIntervalType <> "W":U and */
    end. /* not if icIntervalType <> "W":U and */
    if iiBudgetID <> 0 
    then do :   
        find tbudget where 
             tBudget.Budget_ID = iiBudgetID
             no-lock no-error.
        if not available tBudget
        then do :
            assign oiReturnStatus = -3
                   vcMessage      = trim(substitute(#T-17'Cannot find the budget &1.':255(658)t-17#,string(iiBudgetID))).
            <M-3 run SetMessage (input  vcMessage (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-448':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BBudget>
            Return.
        end. /* if not available tBudget */
    end. /* if iiBudgetID <> 0 */
    
    /* ============================================================================================ */
    /* Addtional validation; intervals Month and Quarter does not allow a start-date with date > 28 */
    /* ============================================================================================ */
    if (icIntervalType = "M":U or icIntervalType = "Q":U) and 
       day(itStartDate) > 28
    then do :
        assign oiReturnStatus = -3
               vcMessage      = trim(substitute(#T-18'For this interval type, the maximum value for the day of the starting date is 28.':255(659)t-18#,day(itStartDate))).
        <M-10 run SetMessage (input  vcMessage (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':U (icFieldValue),
                     input  'E':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QADFIN-452':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BBudget>
        Return.
    end. /* if icIntervalType = "M":U or */
    
    /* ===================================================================================================== */
    /* Reset the output temp-table                                                                           */
    /* Create records in our instance and mark them with tc_status = NN (is reset further on in this method) */
    /* ===================================================================================================== */
    case icIntervalType :
        when "W":U
        then do :
            assign vtPreviousTillDate = itStartDate - 1.
            do viCounter = 1 to iiOccurences by 1 :
                if iiBudgetID <> 0 and
                   (not can-find(first bBudgetPeriod where
                                       bBudgetPeriod.BudgetPeriodFromDate = vtPreviousTillDate + 1 and
                                       bBudgetPeriod.BudgetPeriodTillDate = vtPreviousTillDate + 7 and
                                       bBudgetPeriod.tc_ParentRowid       = tBudget.tc_Rowid       and
                                       bBudgetPeriod.tc_Status           <> 'D':U))
                then do :
                    <M-8 run AddDetailLine (input  'BudgetPeriod':U (icTable), 
                            input  (if iiBudgetID = 0 then '':U else tBudget.tc_Rowid) (icParentRowid), 
                            output viFcReturnSuper (oiReturnStatus)) in BBudget>
                    assign tBudgetPeriod.BudgetPeriodFromDate = vtPreviousTillDate + 1
                           tBudgetPeriod.BudgetPeriodTillDate = vtPreviousTillDate + 7
                           tBudgetPeriod.BudgetPeriodCode     = trim(#T-19'Week':10(660)t-19#) + " ":U + string(viCounter)
                           vtPreviousTillDate                 = tBudgetPeriod.BudgetPeriodTillDate.
                    create tNewBudgetPeriod.
                    buffer-copy tBudgetPeriod to tNewBudgetPeriod.
                end. /* if iiBudgetID <> 0  */
                if iiBudgetID = 0 
                then do:
                    create tNewBudgetPeriod.
                    assign tNewBudgetPeriod.BudgetPeriodFromDate = vtPreviousTillDate + 1
                           tNewBudgetPeriod.BudgetPeriodTillDate = vtPreviousTillDate + 7
                           tNewBudgetPeriod.BudgetPeriodCode     = trim(#T-20'Week':10(660)t-20#) + " ":U + string(viCounter)
                           vtPreviousTillDate                    = tNewBudgetPeriod.BudgetPeriodTillDate
                           tNewBudgetPeriod.tc_Rowid             = tNewBudgetPeriod.BudgetPeriodCode + string(tNewBudgetPeriod.BudgetPeriodTillDate)
                           tNewBudgetPeriod.tc_Status            = "N":U.
                end. /* if iiBudgetID = 0  */
            end. /* do viCounter */
        end. /* when "W":U */
        when "M":U or 
        when "Q":U
        then do :
            assign vtNextFromDate = itStartDate
                   viMonthJumps   = (if icIntervalType = "M":U then 1 else 3).
            do viCounter = 1 to iiOccurences by 1 :
                if iiBudgetID <> 0  and
                   (not can-find(first bBudgetPeriod where
                                       bBudgetPeriod.BudgetPeriodFromDate <= vtNextFromDate  and
                                       bBudgetPeriod.BudgetPeriodTillDate >= vtNextFromDate  and
                                       bBudgetPeriod.tc_ParentRowid       = tBudget.tc_Rowid and
                                       bBudgetPeriod.tc_Status           <> 'D':U)) /* Check if fromdate is within an existing period */
                then do :
                    <M-9 run AddDetailLine (input  'BudgetPeriod':U (icTable), 
                            input  (if iiBudgetID = 0 then '':U else tBudget.tc_Rowid) (icParentRowid), 
                            output viFcReturnSuper (oiReturnStatus)) in BBudget>
                    assign tBudgetPeriod.BudgetPeriodFromDate = vtNextFromDate
                           vtNextFromDate                     = date(IF month(tBudgetPeriod.BudgetPeriodFromDate) + viMonthJumps > 12 
                                                                     THEN month(tBudgetPeriod.BudgetPeriodFromDate) + viMonthJumps - 12 
                                                                     ELSE month(tBudgetPeriod.BudgetPeriodFromDate) + viMonthJumps,
                                                                     day(tBudgetPeriod.BudgetPeriodFromDate),
                                                                     if month(tBudgetPeriod.BudgetPeriodFromDate) + viMonthJumps > 12  
                                                                     then year(tBudgetPeriod.BudgetPeriodFromDate) + 1 
                                                                     else year(tBudgetPeriod.BudgetPeriodFromDate))
                           tBudgetPeriod.BudgetPeriodTillDate = vtNextFromDate - 1
                           viPeriodInYear                     = (if month(tBudgetPeriod.BudgetPeriodFromDate) <= viMonthJumps
                                                                 then 1 
                                                                 else viPeriodInYear + 1)
                           tBudgetPeriod.BudgetPeriodCode     = (if icIntervalType = "M":U 
                                                                 then trim(#T-21'Month':10(661)t-21#) 
                                                                 else trim(#T-22'Quarter':10(662)t-22#)) + " ":U + 
                                                                string(year(tBudgetPeriod.BudgetPeriodFromDate),"9999":U) + "/":U + string(viPeriodInYear,"99":U).
                    create tNewBudgetPeriod.
                    buffer-copy tBudgetPeriod to tNewBudgetPeriod.
                end. /* if iiBudgetID <> 0  */
                if iiBudgetID = 0 
                then do:
                    create tNewBudgetPeriod.
                    assign tNewBudgetPeriod.BudgetPeriodFromDate = vtNextFromDate
                           vtNextFromDate                        = date(IF month(tNewBudgetPeriod.BudgetPeriodFromDate) + viMonthJumps > 12 
                                                                        THEN month(tNewBudgetPeriod.BudgetPeriodFromDate) + viMonthJumps - 12 
                                                                        ELSE month(tNewBudgetPeriod.BudgetPeriodFromDate) + viMonthJumps,
                                                                        day(tNewBudgetPeriod.BudgetPeriodFromDate),
                                                                        if month(tNewBudgetPeriod.BudgetPeriodFromDate) + viMonthJumps > 12  
                                                                        then year(tNewBudgetPeriod.BudgetPeriodFromDate) + 1 
                                                                        else year(tNewBudgetPeriod.BudgetPeriodFromDate))
                           tNewBudgetPeriod.BudgetPeriodTillDate = vtNextFromDate - 1
                           viPeriodInYear                        = (if month(tNewBudgetPeriod.BudgetPeriodFromDate) <= viMonthJumps
                                                                    then 1 
                                                                    else viPeriodInYear + 1)
                           tNewBudgetPeriod.BudgetPeriodCode     = (if icIntervalType = "M":U 
                                                                    then trim(#T-23'Month':10(661)t-23#) 
                                                                    else trim(#T-24'Quarter':10(662)t-24#)) + " ":U + 
                                                                    string(year(tNewBudgetPeriod.BudgetPeriodFromDate),"9999":U) + "/":U + string(viPeriodInYear,"99":U)
                           tNewBudgetPeriod.tc_Rowid             = tNewBudgetPeriod.BudgetPeriodCode + string(tNewBudgetPeriod.BudgetPeriodTillDate)
                           tNewBudgetPeriod.tc_Status            = "N":U.
                end. /* if iiBudgetID = 0  */
            end. /* do viCounter */
        end. /* when "M":U or "Q":U */
    end case.