BLVCKFX
2/26/2019 - 9:14 AM

SalesPackingList

[DataContractAttribute]
public class A_SalesPackingListContract extends TradeDocumentReportContract
{
    SalesId salesId;
}

[DataMemberAttribute('SalesId')]
public SalesId parmSalesId(SalesId _salesId = salesId)
{
    salesId = _salesId;
    return salesId;
}
public class A_SalesPackingListController extends TradeDocumentReportController
{
    CustPackingSlipJour                 custPackingSlipJour;
    SalesPackingSlipJournalPrint        salesPackingSlipJournalPrint;
}

private void convertToJournalList(RecordSortedList _argJourList)
{
    CustPackingSlipJour     localCustPackingSlipJour;

    if (_argJourList && _argJourList.first(localCustPackingSlipJour))
    {
        do
        {
            if (!journalList)
            {
                journalList = new RecordSortedList(tableNum(CustPackingSlipJour));
                journalList.sortOrder(fieldNum(CustPackingSlipJour, RecId));
            }
            journalList.ins(localCustPackingSlipJour);

        }
        while (_argJourList.next(localCustPackingSlipJour));
    }
}

public str  documentTitle()
{
    str documentTitle;

    documentTitle = "@RDX731";

    return documentTitle;
}

protected boolean getFirstJournal()
{
    return journalList.first(custPackingSlipJour);
}

protected RecId getRecordId()
{
    return custPackingSlipJour.RecId;
}

protected void initFormLetterReport()
{
    printCopyOriginal = this.parmArgs().parmEnum();

    if (  classIdGet(this.parmArgs().caller()) == classNum(SalesPackingSlipJournalPrint))
    {
        // Initiate from caller.
        salesPackingSlipJournalPrint = this.parmArgs().caller();
    }

    this.initializeJournalLists(this.parmArgs());

    formLetterReport = FormLetterReport::construct(PrintMgmtDocumentType::A_SalesPackingList);

    formLetterReport.parmPrintType(printCopyOriginal);

    if (salesPackingSlipJournalPrint)
    {
        // Initialize form letter print settings.
        formLetterReport.parmDefaultCopyPrintJobSettings(new SRSPrintDestinationSettings(salesPackingSlipJournalPrint.parmPrinterSettingsFormLetterCopy()));
        formLetterReport.parmDefaultOriginalPrintJobSettings(new SRSPrintDestinationSettings(salesPackingSlipJournalPrint.parmPrinterSettingsFormLetter()));
        formLetterReport.parmUsePrintMgmtDestinations(salesPackingSlipJournalPrint.a_parmUsePrintMgmt());
    }
    else if (printCopyOriginal == PrintCopyOriginal::OriginalPrint)
    {
        // Always use the print mgmt destinations when reprinting for the OriginalPrint case.
        formLetterReport.parmUsePrintMgmtDestinations(true);
    }

    super();
}

public void initializeJournalLists(Args _args)
{
    RecordSortedList    argJourList;
    Common              common = _args.record();

    if (common)
    {
        argJourList = FormLetter::createJournalListCopy(common);

        switch(common.TableId)
        {
            case tableNum(CustPackingSlipJour):
                this.convertToJournalList(argJourList);
                break;
            default:
                throw error(Error::wrongUseOfFunction(funcName()));
        }
    }
    else
    {
        this.convertToJournalList(_args.object());
    }
}

protected void output()
{
    LanguageId      reportLanguageId;

    reportLanguageId = this.reportLanguage();

    formLetterReport.loadPrintSettings(custPackingSlipJour, custPackingSlipJour.salesTable(), reportLanguageId);
    this.parmReportContract().parmRdlContract().parmLanguageId(reportLanguageId);

    super();
}

protected void preRunModifyContract()
{
    A_SalesPackingListContract   contractPackingList = this.parmReportContract().parmRdpContract()  as  A_SalesPackingListContract;

    contractPackingList.parmDocumentTitle(this.documentTitle());
    contractPackingList.parmRecordId(this.getRecordId());
    contractPackingList.parmSalesId(this.parmArgs().parm());

    contract = contractPackingList;
}

public LanguageId reportLanguage()
{
    LanguageId                      reportLanguageId        = custPackingSlipJour.LanguageId;
    LogisticsPostalAddress          deliveryPostalAddress   = LogisticsPostalAddress::findRecId(custPackingSlipJour.DeliveryPostalAddress);
    LogisticsAddressCountryRegion   deliveryCountryRegion   = LogisticsAddressCountryRegion::find(deliveryPostalAddress.CountryRegionId);

    if (deliveryCountryRegion.A_DefaultLanguageId)
    {
        reportLanguageId = deliveryCountryRegion.A_DefaultLanguageId;
    }

    return reportLanguageId;
}

protected void setDataContractRecord(Common _common)
{
    custPackingSlipJour = CustPackingSlipJour::findRecId(_common.RecId);
}

public static A_SalesPackingListController construct()
{
    return new A_SalesPackingListController();
}

public static void main(Args _args)
{
    A_SalesPackingListController    controller = A_SalesPackingListController::construct();

    controller.parmArgs(_args);
    controller.parmReportName(ssrsReportStr(A_SalesPackingList, Report));
    controller.parmShowDialog(false);
    controller.setDataContractRecord(_args.record());

    controller.startOperation();
}
[
    SRSReportParameterAttribute(classStr(A_SalesPackingListContract))
]
public class A_SalesPackingListDP extends SrsReportDataProviderPreProcess
{
    CustPackingSlipJour                 custPackingSlipJour;
    CustPackingSlipTrans                custPackingSlipTrans;
    CustFormletterDocument              custFormLetterDocument;
    A_SalesPackingListHeaderFooterTmp   salesPackingListHeaderFooterTmp;
    A_SalesPackingListDetailsTmp        salesPackingListDetailsTmp;
    LanguageId                          languageId;

    DocuRefSearch                       docuRefSearchMainAbove;
    DocuRefSearch                       docuRefSearchAbove;
    DocuRefSearch                       docuRefSearchMain;
    DocuRefSearch                       docuRefSearch;
    Notes                               notesMainAbove;
    Notes                               notesMain;

    boolean                             doProcessHeaderDocuRef;
    boolean                             doProcessLineDocuRef;

    A_SalesPackingListContract          contract;

}

/// <summary>
/// Creates data for the report.
/// </summary>
/// <param name="_objContract">
/// An instance of <c>TradeDocumentReportContract</c> report contract class.
/// </param>
public void createData(TradeDocumentReportContract _objContract)
{

    RecId                       recId = _objContract.parmRecordId();
    QueryBuildDataSource        custPackingSlipDS;
    Query                       tradeLoopTransQuery;
    QueryRun                    tradeLoopTrans;
    QueryBuildDataSource        qbds;
    QueryBuildDataSource        qbdsInventTrans;
    QueryBuildDataSource        qbdsInventTransOrigin;
    QueryBuildRange             qbr;
    InventDim                   inventDim;
    InventTrans                 inventTrans;

    custPackingSlipJour  = CustPackingSlipJour::findRecId(recId);
    languageId           = custPackingSlipJour.A_reportLanguage();

    this.getDocumentHeaderNotes();
    this.setSalesPackingListHeaderFooterTmp();

    if ((custFormletterDocument.A_DocuOnPackingList == DocuOnFormular::Line) || (custFormletterDocument.A_DocuOnPackingList == DocuOnFormular::All))
    {
        doProcessLineDocuRef = true;
    }

    tradeLoopTransQuery = new TradeLoopTrans(custPackingSlipJour, tableNum(CustPackingSlipTrans)).buildQuery();
    custPackingSlipDS  = tradeLoopTransQuery.dataSourceTable(tableNum(CustPackingSlipTrans));
    qbr                = custPackingSlipDS.addRange(fieldNum(CustPackingSlipTrans, OrigSalesId));
    qbr.value(contract.parmSalesId());

    qbdsInventTransOrigin = custPackingSlipDS.addDataSource(tableNum(InventTransOrigin));
    qbdsInventTransOrigin.joinMode(JoinMode::InnerJoin);
    qbdsInventTransOrigin.fetchMode(QueryFetchMode::One2One);
    qbdsInventTransOrigin.addLink(fieldNum(CustPackingSlipTrans, InventTransId), fieldNum(InventTransOrigin, InventTransId));

    qbdsInventTrans = qbdsInventTransOrigin.addDataSource(tableNum(InventTrans));
    qbdsInventTrans.joinMode(JoinMode::InnerJoin);
    qbdsInventTrans.fetchMode(QueryFetchMode::One2One);
    qbdsInventTrans.addLink(fieldNum(InventTrans, InventTransOrigin), fieldNum(InventTransOrigin, RecId));

    tradeLoopTrans = new QueryRun(tradeLoopTransQuery);

    while (tradeLoopTrans.next())
    {
        custPackingSlipTrans = tradeLoopTrans.get(tableNum(CustPackingSlipTrans));
        inventTrans = tradeLoopTrans.get(tableNum(InventTrans));
        inventDim = inventDim::find(inventTrans.inventDimId);
        this.setSalesPackingListDetailsTmp(inventDim);
        if (doProcessLineDocuRef)
        {
            this.updateShippingNotes();
        }
    }
}

private void getDocumentHeaderNotes()
{
    SalesTable salesTable;

    if ((custFormletterDocument.A_DocuOnPackingList == DocuOnFormular::Head)
     || (custFormletterDocument.A_DocuOnPackingList == DocuOnFormular::All))
    {
        if (custPackingSlipJour.SalesId)
        {
            salesTable  = SalesTable::find(custPackingSlipJour.SalesId);

            if (salesTable.RecId)
            {
                docuRefSearchMain       = DocuRefSearch::newTypeIdRestrictionAndDocuPlacement(salesTable,
                                                                                              custFormletterDocument.A_DocuTypePackingList,
                                                                                              DocuRestriction::Internal,
                                                                                              DocuPlacement::Below);

                notesMain               = Docu::concatDocuRefNotes(docuRefSearchMain);


                docuRefSearchMainAbove  = DocuRefSearch::newTypeIdRestrictionAndDocuPlacement(salesTable,
                                                                                              custFormletterDocument.A_DocuTypePackingList,
                                                                                              DocuRestriction::Internal,
                                                                                              DocuPlacement::Above);

                notesMainAbove          = Docu::concatDocuRefNotes(docuRefSearchMainAbove);

                doProcessHeaderDocuRef  = true;
            }
        }
    }
}

[
    SRSReportDataSetAttribute(tableStr(A_SalesPackingListDetailsTmp))
]
public A_SalesPackingListDetailsTmp getSalesPackingListDetailsTmp()
{
    select salesPackingListDetailsTmp;
    return salesPackingListDetailsTmp;
}

[
    SRSReportDataSetAttribute(tableStr(A_SalesPackingListHeaderFooterTmp))
]
public A_SalesPackingListHeaderFooterTmp getSalesPackingListHeaderFooterTmp()
{
    select salesPackingListHeaderFooterTmp;
    return salesPackingListHeaderFooterTmp;
}

[SysEntryPointAttribute]
public void processReport()
{
    contract = this.parmDataContract() as A_SalesPackingListContract;

    custFormLetterDocument =  CustFormletterDocument::find();

    this.createData(contract);
}

/// <summary>
/// Fills the <c>A_SalesPackingListDetailsTmp</c> table with details.
/// </summary>
private void setSalesPackingListDetailsTmp(InventDim _inventDim)
{
    A_InventSerialExt         inventSerialExt   = A_InventSerialExt::find(custPackingSlipTrans.ItemId, _inventDim.inventSerialId);
    SalesLine                 salesLine;

    salesPackingListDetailsTmp.ItemId           = custPackingSlipTrans.ItemId;
    salesPackingListDetailsTmp.InventDimId      = _inventDim.inventDimId;
    salesPackingListDetailsTmp.InventSizeId     = _inventDim.InventSizeId;
    salesPackingListDetailsTmp.InventColorId    = _inventDim.InventColorId;
    salesPackingListDetailsTmp.PalletRef        = _inventDim.inventSerialId;
    salesPackingListDetailsTmp.GrossWeight      = inventSerialExt.GrossWeight;
    salesPackingListDetailsTmp.NumberOfSheet    = inventSerialExt.NumberOfPieces;
    salesPackingListDetailsTmp.NetWeight        = InventTrans::findByInventTransOrigin(InventTransOrigin::findByInventTransId(custPackingSlipTrans.InventTransId).RecId).Qty;

    if (doProcessLineDocuRef)
    {
        SalesLine                               = custPackingSlipTrans.salesLine();
        docuRefSearch                           = DocuRefSearch::newTypeIdRestrictionAndDocuPlacement(salesLine,
                                                                                                      custFormletterDocument.A_DocuTypePackingList,
                                                                                                      DocuRestriction::Internal,
                                                                                                      DocuPlacement::Below);
        docuRefSearchAbove                      = DocuRefSearch::newTypeIdRestrictionAndDocuPlacement(salesLine,
                                                                                                      custFormletterDocument.A_DocuTypePackingList,
                                                                                                      DocuRestriction::Internal,
                                                                                                      DocuPlacement::Above);
    }

    salesPackingListDetailsTmp.insert();
}

/// <summary>
/// Fills the <c>A_SalesPackingListHeaderFooterTmp</c> table with header and footer details.
/// </summary>
private void setSalesPackingListHeaderFooterTmp()
{
    SalesTable                              salesTable          = SalesTable::find(contract.parmSalesId());//custPackingSlipJour.salesTable();
    InventSite                              inventSite          = inventSite::find(salesTable.InventSiteId);
    CustTable                               custTable           = CustTable::find(salesTable.CustAccount);
    LogisticsLocation                       logisticsLocation;
    InventSiteLogisticsLocation             inventSiteLogisticsLocation;
    LogisticsPostalAddress                  logisticsPostalAddress;
    LogisticsEntityPostalAddressView        logisticsEntityPostalAddressView;

     #xppTexts

    //header
    salesPackingListHeaderFooterTmp.HeaderLogo                 = CompanyImage::find(inventSite.dataAreaId,
                                                                                    inventSite.TableId,
                                                                                    inventSite.RecId,
                                                                                    false,
                                                                                    DateTimeUtil::Date(custPackingSlipJour.createdDateTime),
                                                                                    A_ImageType::None
                                                                                    ).Image;

    select firstOnly logisticsLocation
    join Location, Site from inventSiteLogisticsLocation
        where  inventSiteLogisticsLocation.Location == logisticsLocation.RecId
            && inventSiteLogisticsLocation.Site     == inventSite.RecId;

    salesPackingListHeaderFooterTmp.ExporterShipperDescription = logisticsLocation.Description;

    select firstOnly Address from  logisticsPostalAddress
        where logisticsPostalAddress.Location             == logisticsLocation.RecId
    join Address from logisticsEntityPostalAddressView
        where  logisticsEntityPostalAddressView.Address   == logisticsPostalAddress.Address
            && logisticsEntityPostalAddressView.IsPrimary == NoYes::Yes;

    salesPackingListHeaderFooterTmp.ExporterShipperAddress     = logisticsPostalAddress.Address;
    salesPackingListHeaderFooterTmp.CustName                   = custTable.name();
    salesPackingListHeaderFooterTmp.CustAddress                = DirParty::primaryPostalAddress(custTable.Party).Address;

    logisticsPostalAddress                                     = LogisticsPostalAddress::findRecId(custPackingSlipJour.DeliveryPostalAddress);
    logisticsLocation                                          = LogisticsLocation::find(logisticsPostalAddress.Location);
    salesPackingListHeaderFooterTmp.ShipToDescription          = logisticsLocation.Description;
    salesPackingListHeaderFooterTmp.ShipToAddress              = logisticsPostalAddress.Address;

    salesPackingListHeaderFooterTmp.DeliveryNote               = custPackingSlipJour.PackingSlipId;
    salesPackingListHeaderFooterTmp.DeliveryDate               = custPackingSlipJour.createdDateTime;
    salesPackingListHeaderFooterTmp.OrderNumber                = salesTable.SalesId;
    salesPackingListHeaderFooterTmp.CustRef                    = custPackingSlipJour.PurchaseOrder;
    salesPackingListHeaderFooterTmp.DlvTermTxt                 = DlvTerm::find(salesTable.DlvTerm).A_LanguageTxt(languageId);

    if (doProcessHeaderDocuRef)
    {
        salesPackingListHeaderFooterTmp.ShippingNotes          = notesMainAbove + notesMain + #newLine;
    }

    //footer
    salesPackingListHeaderFooterTmp.FooterLogo                 = CompanyImage::find(inventSite.dataAreaId,
                                                                                    inventSite.TableId,
                                                                                    inventSite.RecId,
                                                                                    false,
                                                                                    DateTimeUtil::Date(custPackingSlipJour.createdDateTime),
                                                                                    A_ImageType::Footer
                                                                                    ).Image;
    salesPackingListHeaderFooterTmp.insert();
}

private void updateShippingNotes()
{
    salesPackingListHeaderFooterTmp.ShippingNotes += Docu::concatDocuRefNotes(docuRefSearchAbove);
    salesPackingListHeaderFooterTmp.ShippingNotes += Docu::concatDocuRefNotes(docuRefSearch);

    salesPackingListHeaderFooterTmp.update();
}