BLVCKFX
7/25/2019 - 10:05 AM

DLX_DeliveryPlanReport

Save report PDF. Send report by email.

// DLX_ACQ0026_DeliveryPlanReport - 20161012 - GR_SCA_L120
[DataContractAttribute]
class DLX_DeliveryPlanReportDC
{
    VendAccount         vendAccount;
    Inventsiteid        inventSiteId;                   // DLX_SCM_BUG_2839_PurchAgreement - Luca Gir8  - 20170731
    DLX_AlignmentType   alignmentType;
    DLX_ReminderType    reminderType;
    DLX_RecurrenceWeekDays      recurrenceWeekDays;     // DLX_SCM_BUG_2890_BatchVendReminder - Luca Gir8 - 20170609
}

// DLX_SCM_BUG_2839_PurchAgreement - Luca Gir8  - 20170731
[DataMemberAttribute]
public inventSiteId parmInventSiteId(InventSiteId _inventSiteId = inventSiteId)
{
    inventSiteId = _inventSiteId;

    return inventSiteId;
}

[DataMemberAttribute]
public VendAccount parmVendAccount(VendAccount _vendAccount = vendAccount)
{
    vendAccount = _vendAccount;

    return vendAccount;
}
class DLX_DeliveryPlanReportController extends SrsReportRunController
{
    FilePath                    filePath;
    DLX_DeliveryPlanVendorInfo  deliveryPlanVendorInfo;
    FilePath                    pathPdf;
}

private void createPDF(DLX_DeliveryPlanVendorInfo _deliveryPlanVendorInfo, inventSiteId _inventSiteId)
{

    DLX_DeliveryPlanReportDC        contract    = this.parmReportContract().parmRdpContract() as DLX_DeliveryPlanReportDC;
    //DLX_SCMParametersDim            parameters  = DLX_SCMParametersDim::findBySiteId(_inventSiteId);
    DLX_SCMParameters               parameters  = DLX_SCMParameters::find();
    SRSPrintDestinationSettings     settings;
    str                             labelStr;
    NumberSeq                       numberSeq   = NumberSeq::newGetNum(VendParameters::NumRefFax_dlx());
    str                             currentDate = date2str(DateTimeUtil::date(DateTimeUtil::getSystemDateTime()),
                                                           123,
                                                           DateDay::Digits2,
                                                           DateSeparator::None,
                                                           DateMonth::Digits2,
                                                           DateSeparator::None,
                                                           DateYear::Digits4,
                                                           1);
    #define.Backslash("\\")
    #define.pdf(".pdf")
    #define.doc(".doc");

    /*
    if(contract.parmAlignmentType())
        labelStr = #Alignment;

    if(contract.parmReminderType())
        labelStr = #Reminder;
    */

    if (parameters.DeliveryPlan)
        labelStr = strFmt("%1%2%1%3%1",#Backslash,parameters.DeliveryPlan,_inventSiteId);
    else
        labelStr = '';

    settings =  this.parmReportContract().parmPrintSettings();
    settings.printMediumType(SRSPrintMediumType::File);
    settings.overwriteFile(true);

    settings.fileFormat(SRSReportFileFormat::PDF);
    pathPdf = parameters.DeliveryPlanReportFilePath +
            labelStr +
            _deliveryPlanVendorInfo.vendAccount +
            "_" + VendTable::find(_deliveryPlanVendorInfo.vendAccount).name() + "_" + currentDate + "_" + NumberSeq.num() + #pdf;

    this.parmFilePath(pathPdf);

    settings.fileName(pathPdf);
    this.parmReportContract().parmPrintSettings(settings);
}

private str getDate()
{
        str dd, mm, yy, dt;

    dt = date2Str(systemDateGet(), 123, DateDay::Digits2, DateSeparator::Slash, DateMonth::Digits2,
           DateSeparator::Slash, DateYear::Digits4);

    dd = substr(dt, 0, 2);
    mm = substr(dt, 4, 2);
    yy = substr(dt, 7, 4);

    dt = dd + '/' + mm + '/' + yy;

    return dt;
}

public FilePath parmFilePath(FilePath _filePath = filePath)
{
    filePath = _filePath;
    return filePath;
}

protected void preRunModifyContract()
{
    DLX_DeliveryPlanReportDC    contract;

    contract = this.parmReportContract().parmRdpContract() as DLX_DeliveryPlanReportDC;

    if (this.parmArgs().record() is DLX_DeliveryPlanVendorInfo)
    {
        deliveryPlanVendorInfo = this.parmArgs().record();

        contract.parmVendAccount(deliveryPlanVendorInfo.VendAccount);
        contract.parmInventSiteId(deliveryPlanVendorInfo.InventSiteId);  // DLX_SCM_BUG_2839_PurchAgreement - Luca Gir8  - 20170731

    }
}

private void returnToPreview()
{
    SRSPrintDestinationSettings     settings;
    ;

    settings =  this.parmReportContract().parmPrintSettings();
    settings.printMediumType(SRSPrintMediumType::Screen);
    settings.overwriteFile(true);
    this.parmReportContract().parmPrintSettings(settings);
}

public void runReport()
{
    InventSiteId        inventSiteId;
    NoYes               showReport;
    SCM_Fax             faxClass    = new SCM_Fax();
    ;


    deliveryPlanVendorInfo  = this.parmArgs().record();
    showReport              = this.parmArgs().parmEnum();
    // DLX_SCM_BUG_2839_PurchAgreement - Luca Gir8  - 20170731 - Begin
    //select firstOnly inventSiteId from deliveryPlan
    //order by runNum desc;

    //inventSiteId = deliveryPlan.InventSiteId;
    inventSiteId           = deliveryPlanVendorInfo.InventSiteId;
    // DLX_SCM_BUG_2839_PurchAgreement - Luca Gir8  - 20170731 - End

    if (!showReport)
    {

        if ((deliveryPlanVendorInfo.SendEmail && deliveryPlanVendorInfo.Email != "0") ||
            (deliveryPlanVendorInfo.SendFax && deliveryPlanVendorInfo.Fax != "0"))
        {
            this.createPDF(deliveryPlanVendorInfo, inventSiteId);// vendTable::find(deliveryPlanVendorInfo.VendAccount).InventSiteId);

            if (deliveryPlanVendorInfo.SendEmail)
            {
                this.parmReportContract().parmReportName(ssrsReportStr(DLX_DeliveryPlanReport, Report));
                super();
                this.sendMail(filePath, deliveryPlanVendorInfo.email, inventSiteId);// vendTable::find(deliveryPlanVendorInfo.VendAccount).InventSiteId);
            }

            if (deliveryPlanVendorInfo.SendFax)
            {
                this.parmReportContract().parmReportName(ssrsReportStr(DLX_DeliveryPlanReport, PortraitReport));
                super();
                faxClass.Send(/*vendTable::find(deliveryPlanVendorInfo.VendAccount).InventSiteId,*/ inventSiteId, filePath , deliveryPlanVendorInfo.fax);
            }
        }
    }
    else
    {
        this.parmReportContract().parmReportName(ssrsReportStr(DLX_DeliveryPlanReport, Report));
        this.returnToPreview();
        super();
    }

}

private void sendMail(FilePath _pdfFilePath, Email  _emailTo, InventSiteId _inventSiteId)
{
    str                                     Body = strFmtLB("@DLX1567");
    str                                     Subject = strFmt("@DLX3356", InventSite::find(_inventSiteId).Name, this.getDate()); /*strFmt("@DLX1568", this.getDate());*/ //DLX_SCM_4551_MailSubjectWithSite - 21022018 - MSakollari
    //SysINetMail                             mail = new SysINetMail(); NOT USED // DLX_BUG2196_GoLiveFixesZanin - 20170411 - Mirco Z

    str                                     sender = HcmWorker::find(DLX_SCMParametersDim::findBySiteId(_inventsiteId).DeliveryPlanPlanner).email();
    str                                     mailServer;
    int                                     mailServerPort;
    System.Net.Mail.SmtpClient              mailClient;
    System.Net.Mail.MailMessage             mailMessage;
    System.Net.Mail.MailAddress             mailFrom;
    System.Net.Mail.MailAddress             mailTo;
    System.Net.Mail.MailAddressCollection   mailToCollection;
    System.Net.Mail.AttachmentCollection    mailAttachementCollection;
    System.Net.Mail.Attachment              mailAttachment;
    ;

    try
    {
        //_EmailTo = 'lucianaMilo10@gmail.com';
        //sender = 'dcartella@outlook.it';
        mailServer = SysEmaiLParameters::find(false).SMTPRelayServerName;
        mailServerPort = SysEmaiLParameters::find(false).SMTPPortNumber;
        mailClient = new System.Net.Mail.SmtpClient(mailServer, mailServerPort);

        mailFrom = new System.Net.Mail.MailAddress(sender);
        mailTo  = new System.Net.Mail.MailAddress(_emailTo);
        mailMessage = new System.Net.Mail.MailMessage(mailFrom, mailTo);

        mailToCollection = mailMessage.get_To();
        mailToCollection.Add(_emailTo);

        mailAttachementCollection = mailMessage.get_Attachments();
        mailAttachment = new System.Net.Mail.Attachment(@_pdfFilePath);
        mailAttachementCollection.Add(mailAttachment);

        mailMessage.set_Priority(System.Net.Mail.MailPriority::High);
        mailMessage.set_Subject(Subject);
        mailMessage.set_Body(body);

        mailClient.Send(mailMessage);
        mailMessage.Dispose();

        CodeAccessPermission::revertAssert();

        info(strFmt("@DLX1713", _emailTo));     // DLX_BUG2196_GoLiveFixesZanin - 20170411 - Mirco Z
    }
    catch(Exception::CLRError)
    {
        // Handle the exception and display message in the InfoLog.
       // error(AifUtil::getClrErrorMessage());
        infolog.clear();

    }
}

public SysOperationStartResult startOperation()
{
    SysOperationStartResult ret;

    ret = super();

    return ret;
}

public static void main(Args _args)
{
    DLX_DeliveryPlanReportController deliveryPlanReportController = new DLX_DeliveryPlanReportController();
    ;

    deliveryPlanReportController.parmArgs(_args);
    deliveryPlanReportController.parmReportName(ssrsReportStr(DLX_DeliveryPlanReport, Report));
    deliveryPlanReportController.parmShowDialog(false);

    deliveryPlanReportController.startOperation();

}
// DLX_ACQ0026_DeliveryPlanReport - 20161012 - GR_SCA_L120
[
SRSReportParameterAttribute(classstr(DLX_DeliveryPlanReportDC))
]
class DLX_DeliveryPlanReportDP extends
                            SRSReportDataProviderBase
                            //SRSReportDataProviderPreProcess
{
    DLX_DeliveryPlanReportDC            contract;
    VendAccount                         vendAccount;
    DLX_DeliveryPlanReportHeaderTmp     deliveryPlanReportHeaderTmp;
    DLX_DeliveryPlanReportTmp           deliveryPlanTmp;

    DLX_DeliveryPlan                    deliveryPlan;
    Args                                args;
    Qty                                 schedQty, receivedQty, delayedQty, remainedQty, agreedTotQty;
    InventDim                           inventDim;
    HcmWorker                           planner;
    int                                 maxRun;
    InventSiteId                        siteId;
}

private void calculateRemainedQty(ItemId _itemId, PurchAgreementId _agreeNum, RefRecid _agreementLineRefRecId)
{
    DLX_DeliveryPlanWeeks deliveryPlanWeeks;
    remainedQty = 0;

    while select deliveryPlanWeeks
        where deliveryPlanWeeks.AgreeNum == _agreeNum
        //&& deliveryPlanWeeks.ItemId == _itemId
        && deliveryPlanWeeks.AgreementLine == _agreementLineRefRecId
        && deliveryPlanWeeks.RunNum == maxRun
    {
        if (deliveryPlanWeeks.QtyPlan - deliveryPlanWeeks.QtyRelease > 0)
            remainedQty += deliveryPlanWeeks.QtyPlan - deliveryPlanWeeks.QtyRelease;
    }
}

private str formatName(str _name)
{
    #define.CL("_CL")

    if(subStr(_name, strLen(_name) - 2, 3) == #CL)
        _name = subStr(_name, 0, strLen(_name) - 3);

    return _name;
}

[SRSReportDataSetAttribute(tablestr("DLX_DeliveryPlanReportHeaderTmp"))]
public DLX_DeliveryPlanReportHeaderTmp getDeliveryPlanReportHeaderTmp()
{
    select * from deliveryPlanReportHeaderTmp;
    return deliveryPlanReportHeaderTmp;
}

[SRSReportDataSetAttribute(tablestr("DLX_DeliveryPlanReportTmp"))]
public DLX_DeliveryPlanReportTmp getDeliveryPlanReportTmp()
{
    select * from deliveryPlanTmp;
    return deliveryPlanTmp;
}

private void initFromContract()
{
    vendAccount     = contract.parmVendAccount();
    siteId          = contract.parmInventSiteId(); // DLX_SCM_BUG_2839_PurchAgreement - Luca Gir8  - 20170731

}

private void populateHeader()
{
    deliveryPlanReportHeaderTmp.Logo            = DLX_CompanyInfoDim::companyLogo(DLX_CompanyInfoDim::findBySiteId(curext(), siteId)); // DLX_SCM_BUG_2839_PurchAgreement - Luca Gir8 - 20170801
    deliveryPlanReportHeaderTmp.VendId          = vendAccount;
    deliveryPlanReportHeaderTmp.VendorName      = VendTable::find(VendAccount).name();
    deliveryPlanReportHeaderTmp.RunDate         = systemDateGet();
    //deliveryPlanReportHeaderTmp.PrimaryAddress  = CompanyInfo::find().primaryAddress();

    deliveryPlanReportHeaderTmp.companyName      = InventSite::find(siteId).Name;
    deliveryPlanReportHeaderTmp.PrimaryAddress   = InventSite::find(siteId).getAddress();

    //planner = HcmWorker::find(DLX_SCMParametersDim::findBySiteId(VendTable::find(vendAccount).InventSiteId).DeliveryPlanPlanner);

    planner = HcmWorker::find(DLX_SCMParametersDim::findBySiteId(siteId).DeliveryPlanPlanner);

    deliveryPlanReportHeaderTmp.PlannerName     = planner.name();
    deliveryPlanReportHeaderTmp.PlannerEmail    = planner.email();
    deliveryPlanReportHeaderTmp.PlannerTel      = planner.phone();
    deliveryPlanReportHeaderTmp.PlannerFax      = planner.DirPerson().primaryFax();

    deliveryPlanReportHeaderTmp.insert();
}

[SysEntryPointAttribute(true)]
public void processReport()
{
    maxRun = 0;

    contract            = this.parmDataContract() as DLX_DeliveryPlanReportDC;

    this.initFromContract();

    // DLX_SCM_BUG_2839_PurchAgreement - Luca Gir8  - 20170731
    select maxOf(RunNum) from deliveryPlan
    where deliveryPlan.InventSiteId == siteId; // DLX_SCM_BUG_2839_PurchAgreement - Luca Gir8  - 20170731

    maxRun = deliveryPlan.RunNum;
    deliveryPlan.clear();


    while select deliveryPlan
        where deliveryPlan.Vendor == vendAccount
        && deliveryPlan.RunNum == maxRun
        && (deliveryPlan.QtyAgreeTot != deliveryPlan.QtyReceived)
    {
        deliveryPlanTmp.clear();
        buf2BufbyName_dlx(deliveryPlan, deliveryPlanTmp);
        deliveryPlanTmp.AgreementLine = int642str(deliveryPlan.AgreementLine);
        deliveryPlanTmp.AgreementLineRefRecId = any2int(AgreementLine::find(deliveryPlan.AgreementLine).LineNumber);    // Fix Go live Rimini - 20190114 - Alberto
        //this.calculateRemainedQty(deliveryPlan.Item, deliveryPlan.AgreeNum, deliveryPlanTmp.AgreementLineRefRecId);
        //deliveryPlanTmp.RemainedQty = deliveryPlanTmp.QtyAgreeTot - deliveryPlanTmp.QtyOrd - remainedQty;
        deliveryPlanTmp.Item        = this.formatName(deliveryPlanTmp.Item);
        deliveryPlanTmp.ItemName    = this.formatName(deliveryPlanTmp.ItemName);
        deliveryPlanTmp.insert();

        //if(!siteId)
        //    siteId = deliveryPlan.InventSiteId;
    }

    this.populateHeader();
}