Code Carriers

Rumman Ansari   2023-07-27   Developer   c programming language > Code Carriers   272 Share

/// <summary>
    /// This method will sent email to sales invoice
    /// </summary>
    public void sentEmailToInvoicePrint(RecId _jourRecId)
    {
        SalesInvoiceHeaderFooterTmp salesInvoiceHeaderTmpLoc;
        SalesInvoiceTmp salesInvoiceTmpLoc;
        CustInvoiceJour custInvoiceJourLoc1;
        SalesLine salesLineLoc;
        SalesId salesIdloc;
        int countSalesId,countInvoiceSalesId;
        CustInvoiceTrans custInvoiceTransLoc;
        SalesId salesIDMap;
        int   countPrintInvoiceMap;
        CustParameters custParameters;

        select firstonly custParameters;

        if (custParameters.ATNYLAInvoicePrintingNoYes == NoYes::Yes && custParameters.ATNYLAPrintEmail)
        {
            //retrieve from map
            mapEnumerator = map.getEnumerator();
            while (mapEnumerator.moveNext())
            {
                salesIDMap       = mapEnumerator.currentKey();
                countPrintInvoiceMap = mapEnumerator.currentValue();
            
                if (countPrintInvoiceMap > 0 && salesIDMap)
                {
           
                    select count(RecId) from custInvoiceTransLoc
                    join custInvoiceJourLoc1
                        where custInvoiceJourLoc1.salesid == salesIDMap
                        && custInvoiceJourLoc1.InvoiceId == custInvoiceTransLoc.InvoiceId
                        && custInvoiceJourLoc1.InvoiceDate == custInvoiceTransLoc.InvoiceDate;
                    
                    countSalesId = custInvoiceTransLoc.RecId;
               
                    select firstonly custInvoiceTransLoc
                    join custInvoiceJourLoc1
                        where custInvoiceJourLoc1.salesid == salesIDMap
                        && custInvoiceJourLoc1.InvoiceId == custInvoiceTransLoc.InvoiceId
                        && custInvoiceJourLoc1.InvoiceDate == custInvoiceTransLoc.InvoiceDate;
                    
                    salesIdloc = custInvoiceTransLoc.SalesId;

                    
                    if (countSalesId != countPrintInvoiceMap && salesIdloc)
                    {
                        ATNYLAEmailer::sendSMTPEmail(custParameters.ATNYLAPrintEmail,strFmt('@ATNYLA:MissInvLine',salesIdloc),strFmt('@ATNYLA:SOMissingPrint',salesIdloc));
                    }
                }
            }
        }

    }


class ATNYLAProgramProductsTableImport  extends RunBaseBatch
{
    DialogRunbase                               dialog;
    DialogField                                 dialogDefinitionGroupUpdate;
    DMFDefinitionGroupName                      definitionGroupUpdate;
    Filename                                    filename;
    System.IO.Stream                            fileStream;
    SharedServiceUnitFileID                     fileID;
    DMFDefinitionGroupName                      definitionGroupName;

    #define.definitionGroupName("Program products import")
    #define.CurrentVersion(3)
    #localmacro.CurrentList
        definitionGroupUpdate,
        filename, fileID
    #endmacro

        private const str OkButtonName = 'OkButton';
        private const str FileUploadName = 'FileUpload';

        /// <summary>
        ///
        /// </summary>
        public boolean canGoBatch()
        {
            return true;
        }

        /// <summary>
        ///
        /// </summary>
        protected boolean canGoBatchJournal()
        {
            return true;
        }

        /// <summary>
        ///    Returns a class that contains the methods that are described by the <c>RunBaseDialogable</c>
        ///    interface.
        /// </summary>
        /// <returns>
        ///    A class that contains the methods that are described by the <c>RunBaseDialogable</c> interface.
        /// </returns>
        /// <remarks>
        ///    A dialog can be either built by using the <c>Dialog</c> class or by using a class that is created
        ///    in the Application Object Tree (AOT).
        /// </remarks>
        public Object dialog()
        {
            DialogGroup      dialogGroup;
            FormBuildControl formBuildControl;
            FileUploadBuild  dialogFileUpload;
       

            dialog = new DialogRunbase(ATNYLAProgramProductsTableImport::description(), this);

            dialogDefinitionGroupUpdate = dialog.addFieldValue(extendedTypeStr(DMFDefinitionGroupName), definitionGroupUpdate);
            dialogDefinitionGroupUpdate.registerOverrideMethod(methodStr(FormStringControl, lookup), methodStr(ATNYLAProgramProductsTableImport, lookUpDefinitionGroup), this);
            dialogDefinitionGroupUpdate.value(#definitionGroupName);

            dialogGroup = dialog.addGroup(ATNYLAProgramProductsTableImport::description());
            formBuildControl = dialog.formBuildDesign().control(dialogGroup.name());

            dialogFileUpload = formBuildControl.addControlEx(classstr(FileUpload), FileUploadName);
            dialogFileUpload.style(FileUploadStyle::MinimalWithFilename);
            dialogFileUpload.fileTypesAccepted('.xlsx,.xls,.xlsm');
            dialogFileUpload.baseFileUploadStrategyClassName(classstr(FileUploadTemporaryStorageStrategy));
            dialogFileUpload.fileNameLabel("@SYS308842");

            return dialog;
        }

        /// <summary>
        ///  Looks up the definition group name.
        /// </summary>
        /// <param name="_control">
        ///     The look up control.
        /// </param>
        public void lookUpDefinitionGroup(FormStringControl _control)
        {
            Query                   query;
            QueryBuildDataSource    qbds;
            SysTableLookup          lookup;
            container                   con;
            str                     rangeValue;
            DMFDefinitionGroupEntity    definitionGroupEntity;
            #define.Import('Program products')
        
            while select DefinitionGroup from definitionGroupEntity
            where definitionGroupEntity.Entity == #Import
            &&  definitionGroupEntity.DefinitionGroup == #definitionGroupName
            {
                con += definitionGroupEntity.DefinitionGroup;
            }

            rangeValue = con2Str(con, ',');

            query = new Query();
            qbds = query.addDataSource(tableNum(DMFDefinitionGroup));
            qbds.addRange(fieldNum(DMFDefinitionGroup, DefinitionGroupName)).value(rangeValue);

            lookup = SysTableLookup::newParameters(tableNum(DMFDefinitionGroup), _control, true);
            lookup.parmQuery(query);
            lookup.addLookupField(fieldNum(DMFDefinitionGroup, DefinitionGroupName), true);
            lookup.addLookupField(fieldNum(DMFDefinitionGroup, Description));
            lookup.performFormLookup();
        }

        /// <summary>
        /// Disables the dialog Ok button until the file upload is complete.
        /// </summary>
        /// <param name="_dialog">The <c>Runbase</c> dialog object.</param>
        public void dialogPostRun(DialogRunbase _dialog)
        {
            FileUpload fileUpload = this.getFormControl(_dialog, FileUploadName);
            fileUpload.notifyUploadCompleted += eventhandler(this.uploadCompleted);
            this.setDialogOkButtonEnabled(_dialog, false);
        }

        /// <summary>
        /// Displays the summary of the execution
        /// </summary>
        public void executionStatus()
        {
            DMFDefinitionGroupExecution     definitionGroupExecution;
            DMFExecutionSummaryStatus       summaryStatus;
            DMFExecution                    execution;
               
            select firstonly ExecutionId, NoOfRecords, NumOfTargetNew, NumOfTargetUpdated from definitionGroupExecution
            order by CreatedDateTime desc
            where definitionGroupExecution.DefinitionGroup == #definitionGroupName;
                
            execution = DMFExecution::find(definitionGroupExecution.ExecutionId);
        
            summaryStatus = execution.GetExecutionSummaryStatus();
        
            info(strFmt("@SYS90632",DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::utcNow(), DateTimeUtil::getUserPreferredTimeZone())));
            info(strfmt("@ATNYLA:FileReadFrom", filename));
            info(strfmt("@ATNYLA:RowsInserted", definitionGroupExecution.NumOfTargetNew));
            info(strfmt("@ATNYLA:RecordsUpdated", definitionGroupExecution.NumOfTargetUpdated));
            info(strFmt("@ATNYLA:ImportStatus", DMFExecutionStatusHelper::GetExecutionSummaryStatusText(summaryStatus)));
            info(strFmt("@ATNYLA:ExecutionId", definitionGroupExecution.ExecutionId));
            info("@ATNYLA:Importcompleted");
        }

        /// <summary>
        /// After the file has been uploaded, the Ok button is enabled.
        /// </summary>
        protected void uploadCompleted()
        {
            FileUpload fileUpload = this.getFormControl(dialog, FileUploadName);
            fileUpload.notifyUploadCompleted -= eventhandler(this.UploadCompleted);

            filename = fileUpload.fileName();

            this.setDialogOkButtonEnabled(dialog, true);
        }

        /// <summary>
        /// Enables or disables the dialog Ok button.
        /// </summary>
        /// <param name = "_dialog">The <c>Runbase</c> dialog object.</param>
        /// <param name = "_isEnabled">Indicates to enable or disable the Ok button.</param>
        protected void setDialogOkButtonEnabled(DialogRunbase _dialog, boolean _isEnabled)
        {
            FormControl okButtonControl = this.getFormControl(_dialog, OkButtonName);

            if (okButtonControl)
            {
                okButtonControl.enabled(_isEnabled);
            }
        }

        /// <summary>
        /// Gets the form control
        /// </summary>
        /// <Returns> Returns the form control </Returns>
        protected FormControl getFormControl(DialogRunbase _dialog, str _controlName)
        {
            return _dialog.formRun().control(_dialog.formRun().controlId( _controlName));
        }

        /// <summary>
        /// Gets the user input values from dialog
        /// </summary>
        ///  /// <Returns> Returns True/False </Returns>
        public boolean getFromDialog()
        {
            boolean ret;
            ret = super();

            definitionGroupUpdate = dialogDefinitionGroupUpdate.value();

            FileUpload fileUploadControl = this.getFormControl(dialog, FileUploadName);

            FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();

            if (fileUploadResult != null && fileUploadResult.getUploadStatus())
            {
                fileID = fileUploadResult.getFileId();
            }
        
            return ret;
        }

        /// <summary>
        ///    Contains the code that does the actual job of the class.
        /// </summary>
        public void run()
        {
            DMFExecutionId              executionId;
            DMFDefinitionGroup          definitionGroup;
            DMFDefinitionGroupEntity    definitionGroupEntity;
            boolean                     executeTargetDuringInitialImport;
            DMFDefinitionGroupExecution definitionGroupExecution;
            DMFExecutionSummaryStatus   summaryStatus;
            DMFExecution                execution;
        
            try
            {
                definitionGroup = ATNYLAProgramProductsTableImport::findDMFDefinitionGroup(definitionGroupUpdate);
                ATNYLAProgramProductsTableImport::applyTransforms(fileID, definitionGroup);

                definitionGroupEntity = ATNYLAProgramProductsTableImport::findDMFDefinitionGroupEntity(definitionGroup);
                executionId = DMFUtil::setupNewExecution(definitionGroup.DefinitionGroupName);

                // Determine if source to target should happen on initial import based on if we are in batch or not.
                executeTargetDuringInitialImport = !this.parmInBatch();

                //  Find execution
                definitionGroupExecution = DMFDefinitionGroupExecution::find(definitionGroup.DefinitionGroupName,
                                                                                 definitionGroupEntity.Entity, executionId, true);

                summaryStatus = definitionGroupExecution.GetExecutionSummaryStatus();

                execution = DMFExecution::find(definitionGroupExecution.ExecutionId);
                //To showing info related to count
                definitionGroupExecution.FilePath = fileID;
                definitionGroupExecution.IsTransformed = NoYes::Yes;
                definitionGroupExecution.ExecuteTargetStep = executeTargetDuringInitialImport;
                definitionGroupExecution.update();

                DMFQuickImportExport::doPGImport(definitionGroup.DefinitionGroupName, executionId, executeTargetDuringInitialImport);

                // If we still need to move from source to target, kick off that process now.
                if (!executeTargetDuringInitialImport)
                {
                    DMFDefinitionGroupExecution executionSourceToTarget;

                    update_recordset executionSourceToTarget
                    setting IsSelected = NoYes::Yes
                    where executionSourceToTarget.ExecutionId == executionId;

                    DMFWriteExecutionParameters executionParameters = DMFWriteExecutionParameters::construct();
                    executionParameters.parmDefinitionGroup(definitionGroup.DefinitionGroupName);
                    executionParameters.parmsourceTarget(DMFSourceTarget::Source);
                    executionParameters.parmSelectedData(NoYes::No);
                    executionParameters.parmErrorStatus(NoYes::No);
                    executionParameters.parmSkipFormNavigation(NoYes::Yes);

                    DMFExecution dmfExecution = DMFExecution::find(executionId, true);
                    Args args = new Args();
                    args.record(dmfExecution);
                    args.parmObject(executionParameters);
                    args.parmEnum(NoYes::No);

                    new MenuFunction(menuItemActionStr(DMFEntityWriterBatch), MenuItemType::Action).run(args);
                }
                this.executionStatus();
            }
            catch(Exception::Error)
            {
                info(strFmt("%1",Exception::Error));
            }
        }

        /// <summary>
        ///
        /// </summary>
        public boolean runsImpersonated()
        {
            return true;
        }

        /// <summary>
        ///    Packs the state of the object
        /// </summary>
        /// <returns>
        ///    Returns the packed container
        /// </returns>
        public container pack()
        {
            return [#CurrentVersion, #CurrentList];
        }

        /// <summary>
        ///   Unpacks the received contaienr object
        /// </summary>
        /// <parameter> Container with the packed status</parameter>
        /// <returns>
        ///    Returns the packed container
        /// </returns>
        public boolean unpack(container _packedClass)
        {
            Integer version = conPeek(_packedClass, 1);

            switch (version)
            {
                case #CurrentVersion:
                    [version, #CurrentList] = _packedClass;
                    break;
                default:
                    return false;
            }

            return true;
        }

        /// <summary>
        ///  Validates the user input information
        /// </summary>
        /// <parm = _calledFrom>The caller object</parm>
        /// <returns>
        ///  Returns True/False
        /// </returns>
        public boolean validate(Object _calledFrom = null)
        {
            boolean ret = true;

            if (!definitionGroupUpdate)
            {
                ret = checkFailed("@DMF1432");
            }

            if (!filename)
            {
                ret = checkFailed("@SYS18624");
            }

            return ret;
        }

        /// <summary>
        /// Constructs the ATNYLAProductUOMConversionsImport object
        /// </summary>
        /// <returns>Returns the ATNYLAProductUOMConversionsImport object</returns>
        public static ATNYLAProgramProductsTableImport construct()
        {
            return new ATNYLAProgramProductsTableImport();
        }

        /// <summary>
        /// Provides caption for the dialog
        /// </summary>
        /// <returns>Reutrns the caption of the dialog</returns>
        public static ClassDescription description()
        {
            return ("Program products import");
        }

        /// <summary>
        /// Provides the entry point for the ATNYLAPurchaseOrdersImportInsertDialog dialog class
        /// </summary>
        /// <param name = "args"></param>
        public static void main(Args args)
        {
            ATNYLAProgramProductsTableImport   programProductsTableImport;
            programProductsTableImport =    ATNYLAProgramProductsTableImport::construct();

            if (programProductsTableImport.prompt())
            {
                programProductsTableImport.runOperation();
            }
        }

        /// <summary>
        ///
        /// </summary>
        protected boolean canRunInNewSession()
        {
            return false;
        }

        /// <summary>
        ///
        /// </summary>
        private static DMFLocalFilePath applyTransforms(SharedServiceUnitFileID _uploadedStatement, DMFDefinitionGroup definitionGroup)
        {
            DMFDefinitionGroupEntity    definitionGroupEntity = ATNYLAProgramProductsTableImport::findDMFDefinitionGroupEntity(definitionGroup);
            DMFExecutionId              executionId = DMFUtil::setupNewExecution(definitionGroup.DefinitionGroupName);

            DMFDefinitionGroupExecution execution = DMFDefinitionGroupExecution::find(
            definitionGroup.DefinitionGroupName,
            definitionGroupEntity.Entity,
            executionId,
            true);

            execution.IsTransformed = NoYes::No;
            DMFLocalFilePath filePath = execution.applyTransforms(_uploadedStatement);

            DMFExecution e = DMFExecution::find(executionId, true);
            e.delete();

            return filePath;
        }

        /// <summary>
        ///
        /// </summary>
        private static DMFDefinitionGroupEntity findDMFDefinitionGroupEntity(DMFDefinitionGroup _definitionGroup)
        {
            DMFDefinitionGroupEntity definitionGroupEntity;
            DMFEntity dmfEntity;

            select firstonly RecId, Entity from definitionGroupEntity exists join dmfEntity
            where definitionGroupEntity.DefinitionGroup == _definitionGroup.DefinitionGroupName
                && dmfEntity.EntityName == definitionGroupEntity.Entity
                && dmfEntity.TargetEntity == dataentityviewstr(ATNYLAProgramProductsTableEntity);

            if (!definitionGroupEntity)
            {
                throw error(strFmt("@DMF:DMFNoEntityExists", _definitionGroup.DefinitionGroupName));
            }

            return definitionGroupEntity;
        }

        /// <summary>
        ///
        /// </summary>
        private static DMFDefinitionGroup findDMFDefinitionGroup(DMFDefinitionGroupName     definitionGroupName)
        {
            DMFDefinitionGroup definitionGroup;
        
            select firstonly definitionGroup
            where definitionGroup.DefinitionGroupName == definitionGroupName; //PurchParameters::find().ATNYLAProcessingGroupDisplayUpdate;//DMF import data project

            return definitionGroup;
        }

}