Tuesday, 1 February 2022

PO Partial packingslip, Invoice x++ D365

class TEST_POPackingSlip

{


    public static void main(Args _args)

    {

        PurchFormLetter             purchFormLetter, purchFormLetterInvoice;

        PurchFormletterParmData     purchFormLetterParmData;

        PurchParmUpdate             purchParmUpdate;

        PurchParmTable              purchParmTable;

        PurchParmLine               purchParmLine;

        TEST_POPartialLinesTmp      poPartialLinesTmp;

        PurchId                     purchId = 'purchaseOrderNum';

        PurchTable                  purchTable = PurchTable::find(purchId);

        PurchLine                   purchLine;

        Num                         packingSlipId = purchTable.PurchId;

        boolean                     isPackingslip = false;


        ttsBegin;

        if(purchTable.DocumentState != VersioningDocumentState::Confirmed)

        {

            purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);

            purchFormLetter.update(purchTable, strFmt("%1", purchTable.PurchId));

            Info(strFmt('Purchase order confirmed %1 ', purchTable.PurchId));

        }


        //partial quantity packingslip

        try

        {

            purchFormLetterParmData = PurchFormletterParmData::newData(DocumentStatus::PackingSlip,VersioningUpdateType::Initial);

            purchFormLetterParmData.parmOnlyCreateParmUpdate(true);

            purchFormLetterParmData.createData(false);

            purchParmUpdate = purchFormLetterParmData.parmParmUpdate();


            purchParmTable.clear();

            purchParmTable.TransDate                = SystemDateGet();

            purchParmTable.Ordering                 = DocumentStatus::PackingSlip;

            purchParmTable.ParmJobStatus            = ParmJobStatus::Waiting;

            purchParmTable.Num                      = packingSlipId;

            purchParmTable.PurchId                  = purchTable.PurchId;

            purchParmTable.PurchName                = purchTable.PurchName;

            purchParmTable.DeliveryName             = purchTable.DeliveryName;

            purchParmTable.DeliveryPostalAddress    = purchTable.DeliveryPostalAddress;

            purchParmTable.OrderAccount             = purchTable.OrderAccount;

            purchParmTable.CurrencyCode             = purchTable.CurrencyCode;

            purchParmTable.InvoiceAccount           = purchTable.InvoiceAccount;

            purchParmTable.ParmId                   = purchParmUpdate.ParmId;

            purchParmTable.insert();


            while select purchLine where purchLine.PurchId == purchId//purchTable.PurchId

            {

                select poPartialLinesTmp where poPartialLinesTmp.PurchId == purchLine.purchId && poPartialLinesTmp.ItemId == purchLine.ItemId;

                if(poPartialLinesTmp)

                {

                    purchParmLine.InitFromPurchLine(purchLine);


                    purchParmLine.ReceiveNow    = poPartialLinesTmp.Qty;

                    purchParmLine.ParmId        = purchParmTable.ParmId;

                    purchParmLine.TableRefId    = purchParmTable.TableRefId;

                    purchParmLine.setQty(DocumentStatus::PackingSlip, false, true);

                    purchParmLine.setLineAmount();

                    purchParmLine.insert();

                    isPackingslip = true;

                }

                else

                {

                    continue;

                }

                

            }

            if(isPackingslip == true)

            {

                purchFormLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);

                purchFormLetter.transDate(systemDateGet());

                purchFormLetter.proforma(false);

                purchFormLetter.specQty(PurchUpdate::All);

                purchFormLetter.purchTable(purchTable);

    

                purchFormLetter.parmParmTableNum(purchParmTable.ParmId);

                purchFormLetter.parmId(purchParmTable.ParmId);

                purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());

                purchFormLetter.run();

                

                Info(strFmt('Packingslip crated for purchase order %1', purchTable.PurchId));


                //for Invoice

                TEST_PurchInvoice purchInvoice = new TEST_PurchInvoice();

                purchInvoice.poInvoice(purchTable.PurchId);

            }

            ttsCommit;

        }

        catch

        {

            info(infolog.text());

        }


    }


}


---For Invoice


class TEST_PurchInvoice

{

    public static void main(Args _args)

    {

        TEST_PurchInvoice purchInvoice = new TEST_PurchInvoice();

        purchInvoice.poInvoice("purchId");

    }


    public void poInvoice(PurchId _purchId)

    {

        PurchFormLetter             purchFormLetter;

        PurchTable                  purchTable = PurchTable::find(_purchId);

        PurchLine                   purchLine;

        VendPackingSlipJour         vendPackingSlipJour;

        VendPackingSlipTrans        vendPackingSlipTrans;

        VendInvoiceInfoTable        vendInvoiceInfoTable;

        VendInvoiceInfoLine         vendInvoiceInfoLine;

        VendInvoiceInfoSubTable     vendInvoiceInfoSubTable;

        VendInvoiceInfoSubLine      vendInvoiceInfoSubLine;

        InvoiceId                   invoiceId;

        TransDate                   invoiceDate;

        VendInvoiceJour             vendInvoiceJour;

        InventTrans                 inventTransQty;

        str errorLog;

        boolean isInvoiced;

        InvoiceId                   invoiceIdLocal;

        

        select vendPackingSlipJour where vendPackingSlipJour.PurchId == purchTable.PurchId;

        ttsBegin;

        if(vendPackingSlipJour.PackingSlipId)

        {

            invoiceId = strFmt("INV_%1", vendPackingSlipJour.PackingSlipId);

            invoiceDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());


            purchTable = vendPackingSlipJour.purchTable();

            vendInvoiceInfoTable.clear();

            vendInvoiceInfoTable.initValue();

            vendInvoiceInfoTable.initFromPurchTable(purchTable);


            vendInvoiceInfoTable.DocumentOrigin         = DocumentOrigin::Manual;

            vendInvoiceInfoTable.CurrencyCode           = purchTable.CurrencyCode;

            vendInvoiceInfoTable.DeliveryName           = purchTable.DeliveryName;

            vendInvoiceInfoTable.Num                    = invoiceId;

            vendInvoiceInfoTable.PurchName              = purchTable.PurchName;

            vendInvoiceInfoTable.VendInvoiceSaveStatus  = VendInvoiceSaveStatus::Pending;

            vendInvoiceInfoTable.TransDate              = invoiceDate;

            vendInvoiceInfoTable.DocumentDate           = invoiceDate;

            vendInvoiceInfoTable.LastMatchVariance      = LastMatchVarianceOptions::OK;

            vendInvoiceInfoTable.ParmJobStatus          = ParmJobStatus::Waiting;

            vendInvoiceInfoTable.DefaultDimension       = vendInvoiceInfoTable.copyDimension(purchTable.DefaultDimension);

            vendInvoiceInfoTable.defaultRow(purchTable);

            vendInvoiceInfoTable.insert();


            if(vendInvoiceInfoTable)

            {

                vendInvoiceInfoSubTable.clear();

                vendInvoiceInfoSubTable.initValue();

                vendInvoiceInfoSubTable.defaultRow();

                vendInvoiceInfoSubTable.ParmId      = vendInvoiceInfoTable.ParmId;

                vendInvoiceInfoSubTable.OrigPurchId = vendInvoiceInfoTable.PurchId;

                vendInvoiceInfoSubTable.PurchName   = vendInvoiceInfoTable.PurchName;

                vendInvoiceInfoSubTable.TableRefId  = vendInvoiceInfoTable.TableRefId;


                vendInvoiceInfoSubTable.insert();

            }


            while select vendPackingSlipTrans

                where vendPackingSlipTrans.PackingSlipId == vendPackingSlipJour.PackingSlipId

                && vendPackingSlipTrans.VendPackingSlipJour == vendPackingSlipJour.RecId

            {

                purchLine = vendPackingSlipTrans.purchLine();

                vendInvoiceInfoLine.clear();

                vendInvoiceInfoLine.initValue();

                vendInvoiceInfoLine.initFromPurchLine(purchLine);


                vendInvoiceInfoLine.DeliveryName    = vendInvoiceInfoTable.DeliveryName;

                vendInvoiceInfoLine.ParmId          = vendInvoiceInfoTable.ParmId;

                vendInvoiceInfoLine.TableRefId      = vendInvoiceInfoTable.TableRefId;

                vendInvoiceInfoLine.currencyCode    = vendInvoiceInfoTable.CurrencyCode;

                vendInvoiceInfoLine.LineNum         = any2int(purchLine.LineNumber);

                vendInvoiceInfoLine.InvoiceAccount  = vendInvoiceInfoTable.InvoiceAccount;

                vendInvoiceInfoLine.InventDimId     = vendPackingSlipTrans.InventDimId;

                vendInvoiceInfoLine.OrderAccount    = vendInvoiceInfoTable.OrderAccount;

                vendInvoiceInfoLine.ItemId          = vendPackingSlipTrans.ItemId;

                vendInvoiceInfoLine.InventTransId   = vendPackingSlipTrans.InventTransId;

                vendInvoiceInfoLine.PurchPrice      = purchLine.PurchPrice;

                vendInvoiceInfoLine.InventNow       = vendInvoiceInfoLine.ReceiveNow;

                vendInvoiceInfoLine.DocumentOrigin  = DocumentOrigin::Manual;

                vendInvoiceInfoLine.ReceiveNow      = vendPackingSlipTrans.Qty;

                vendInvoiceInfoLine.modifiedReceiveNow();

                if(purchLine.PurchQty != 0)

                {

                    vendInvoiceInfoLine.LineAmount = (purchLine.LineAmount / purchLine.PurchQty) * vendInvoiceInfoLine.ReceiveNow;

                }

                vendInvoiceInfoLine.DefaultDimension = purchLine.DefaultDimension;

                vendInvoiceInfoLine.insert();


                if(vendInvoiceInfoLine.RecId)

                {

                    vendInvoiceInfoSubLine.clear();

                    vendInvoiceInfoSubLine.initValue();

                    vendInvoiceInfoSubLine.defaultRow();

                    vendInvoiceInfoSubLine.ParmId               = vendInvoiceInfoTable.ParmId;

                    vendInvoiceInfoSubLine.LineRefRecId         = vendInvoiceInfoLine.RecId;

                    vendInvoiceInfoSubLine.ReceiveNow           = vendInvoiceInfoLine.ReceiveNow;

                    vendInvoiceInfoSubLine.InventNow            = vendInvoiceInfoLine.InventNow ;

                    vendInvoiceInfoSubLine.JournalRefRecId      = vendPackingSlipTrans.RecId;

                    vendInvoiceInfoSubLine.JournalRefTableId    = vendPackingSlipTrans.TableId;

                    vendInvoiceInfoSubLine.DocumentId           = vendPackingSlipTrans.PackingSlipId;

                    vendInvoiceInfoSubLine.insert();

                }

            }

            purchFormLetter = PurchFormLetter_Invoice::newFromSavedInvoice(vendInvoiceInfoTable);

            purchFormLetter.update(vendInvoiceInfoTable.purchTable(),vendInvoiceInfoTable.Num);

        }

        ttsCommit;

        PurchTable  purchTableLocal = PurchTable::find(purchTable.PurchId);

        PurchTable  purchTableUpdate;


        if (purchTableLocal.DocumentStatus == DocumentStatus::Invoice)

        {

            info(strFmt("Purchase order invoiced %1 ",purchTableLocal.PurchId));

            isInvoiced = true;

        }

        if(isInvoiced == true)

        {

            while select vendInvoiceJour where vendInvoiceJour.PurchId == purchTable.PurchId

            {

                invoiceIdLocal += vendInvoiceJour.InvoiceId+', ';

            }

            select forupdate purchTableUpdate where purchTableUpdate.PurchId == purchTableLocal.PurchId;

            if(purchTableUpdate)

            {

                ttsbegin;

                purchTableUpdate.Invoiced = invoiceIdLocal;

                purchTableUpdate.doUpdate();

                ttscommit;

            }

        }

    }


}

Tuesday, 28 December 2021

Cust Tax registration number x++ D365

 public str getCRNum(CustAccount   _custAccount)

    {

        CustTable           custTable   =   CustTable::find(_custAccount);

        DirPartyTable       dirPartyTable;

        DirPartyLocation    dirPartyLocation;

        TaxRegistration     taxRegistration;

        TaxRegistrationType taxRegistrationType;

        TaxRegistrationTypeApplicabilityRule    taxRegistrationTypeApplicabilityRule;


        select taxRegistration

            join taxRegistrationTypeApplicabilityRule

            join taxRegistrationType

            join dirPartyLocation

            join dirPartyTable

            join custTable

            where taxRegistration.DirPartyLocation  ==  dirPartyLocation.RecId

            && taxRegistration.TaxRegistrationTypeApplicabilityRule ==  taxRegistrationTypeApplicabilityRule.RecId

            && taxRegistrationTypeApplicabilityRule.TaxRegistrationType ==  taxRegistrationType.RecId

            && taxRegistrationType.TaxRegstrationType == "CR"

            && dirPartyLocation.Party   ==  dirPartyTable.RecId

            && dirPartyTable.RecId  ==  custTable.Party;

        

        return taxRegistration.RegistrationNumber;

    }

QR Text output x++

qrString        = strFmt("Supplier name: %1", CompanyInfo::findDataArea(_custInvoiceJour.DataAreaId).Name);

                qrString        += strFmt("\nSupplier VAT: %1", CompanyInfo::findDataArea(_custInvoiceJour.DataAreaId).CoRegNum);

                qrString        += strFmt("\nInvoice number: %1", _custInvoiceJour.InvoiceId);

                qrString        += strFmt("\nInvoice Date: %1", _custInvoiceJour.InvoiceDate);

                qrString        += strFmt("\nTotal amount with VAT: %1", _custInvoiceJour.InvoiceAmount);

                qrString        += strFmt("\nVAT amount: %1", _custInvoiceJour.SumTax);


public Bitmap getQR(String255 _qrString)

    {

        str                                 QrCodeBase64String;

        Microsoft.Dynamics.ApplicationSuite.QRCode.Encoder   qrCode;

        System.String                       netString;

        str                                 tempFileName;

        System.Drawing.Bitmap               netBitmap;

        Bitmap                              imageQR;

        FileIOPermission                    perm;

        BinData                             binData;

        container                           imageContainer;

        Str1260                             qrString;

        real                                taxWithoutVat;

        QRCodeGenerator qrGenerator = new QRCodeGenerator();


        qrCode          = new Microsoft.Dynamics.ApplicationSuite.QRCode.Encoder();

        

        netBitmap       = qrCode.Encode(_qrString);

        binData         = new binData();

        QrCodeBase64String  = _qrString;

        tempFileName        = qrCode.GetTempFile(QrCodeBase64String);

        perm  = new FileIOPermission(tempFileName,'RW');

        perm.assert();

        binData.loadFile(tempFileName);

        imageContainer          = binData.getData();


        return imageContainer;

    }


Use bitmap to display QR (Image/bmp) 

QR base64 D365 x++

 public Bitmap hexaDecimalQRCode(Name            _companyName,

                                    VATNum          _vatNum,

                                    CustInvoiceId   _invoiceId,

                                    CreatedDateTime _transdate,

                                    AmountCurNet    _netAmt,

                                    Amount          _vatAmt)

    {

        str             hexaDecimalSellerTag,hexaDecimalVATNumberTag,hexaDecimalTimeStampTag,hexaDecimalInvoiceTotalTag,hexaDecimalVATTotalTag;

        int             hexaDecimalSellerLength,hexaDecimalVATNumberLength,hexaDecimalTimeStampLength,hexaDecimalInvoiceTotalLength,hexaDecimalVATTotalLength;

        str             hexaDecimalSellerValue,hexaDecimalVATNumberValue,hexaDecimalTimeStampValue,hexaDecimalInvoiceTotalValue,hexaDecimalVATTotalValue;

        str             hexaDecimalSeller,hexaDecimalVATNumber,hexaDecimalTimeStamp,hexaDecimalInvoiceTotal,hexaDecimalVATTotal;

        Str             Seller,VATNumber,TimeStamp,InvoiceTotal,VATTotal,hexaDecimal,tempValue,base64Text;

        System.Byte[]   byte;

        bindata         bindata;

        container       con;

        int             i;

        container       qrCode,conStore;

        Microsoft.Dynamics.ApplicationSuite.QRCode.Encoder  qrCode1 = new Microsoft.Dynamics.ApplicationSuite.QRCode.Encoder();

        System.Drawing.Bitmap bitmap;

        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();



        container      imageContainer;

        hexaDecimalSellerTag        = "01";

        hexaDecimalVATNumberTag     = "02";

        hexaDecimalTimeStampTag     = "03";

        hexaDecimalInvoiceTotalTag  = "04";

        hexaDecimalVATTotalTag      = "05";


        Seller          = _companyName;

        VATNumber       = strFmt("%1",_vatNum);

        TimeStamp       = "2021-12-22 09:26:31";//DateTimeUtil::toStr(_transdate);//"2021-11-01 11:30:28";//"3/26/2018 01:28:18";//strFmt("%1",_transdate);//"2021-11-01 21:30:00";

        InvoiceTotal    = strFmt("%1",_netAmt);

        VATTotal        = strFmt("%1",_vatAmt);


        hexaDecimalSellerLength         = strLen(Seller);

        hexaDecimalVATNumberLength      = strLen(VATNumber);

        hexaDecimalTimeStampLength      = strLen(TimeStamp);

        hexaDecimalInvoiceTotalLength   = strLen(InvoiceTotal);

        hexaDecimalVATTotalLength       = strLen(VATTotal);


        hexaDecimalSellerValue          = int2Hex(hexaDecimalSellerLength);

        hexaDecimalVATNumberValue       = int2Hex(hexaDecimalVATNumberLength);

        hexaDecimalTimeStampValue       = int2Hex(hexaDecimalTimeStampLength);

        hexaDecimalInvoiceTotalValue    = int2Hex(hexaDecimalInvoiceTotalLength);

        hexaDecimalVATTotalValue        = int2Hex(hexaDecimalVATTotalLength);



        if(strLen(hexaDecimalSellerValue) == 1)

        {

            hexaDecimalSellerValue          = "0" + hexaDecimalSellerValue;

        }

        if(strLen(hexaDecimalVATNumberValue) == 1)

        {

            hexaDecimalVATNumberValue       = "0" + hexaDecimalVATNumberValue;

        }

        if(strLen(hexaDecimalTimeStampValue) == 1)

        {

            hexaDecimalTimeStampValue       = "0" + hexaDecimalTimeStampValue;

        }

        if(strLen(hexaDecimalInvoiceTotalValue) == 1)

        {

            hexaDecimalInvoiceTotalValue    = "0" + hexaDecimalInvoiceTotalValue;

        }

        if(strLen(hexaDecimalVATTotalValue) == 1)

        {

            hexaDecimalVATTotalValue    = "0" + hexaDecimalVATTotalValue;

        }


        for (i=1;i<=hexaDecimalSellerLength ;i++)

        {

            hexaDecimalSeller    += int2Hex(char2num(Seller,i));

        }

        for (i=1;i<=hexaDecimalVATNumberLength ;i++)

        {

            hexaDecimalVATNumber    += int2Hex(char2num(VATNumber,i));

        }

        for (i=1;i<=hexaDecimalTimeStampLength ;i++)

        {

            hexaDecimalTimeStamp    += int2Hex(char2num(TimeStamp,i));

        }

        for (i=1;i<=hexaDecimalInvoiceTotalLength ;i++)

        {

            hexaDecimalInvoiceTotal    += int2Hex(char2num(InvoiceTotal,i));

        }

        for (i=1;i<=hexaDecimalVATTotalLength ;i++)

        {

            hexaDecimalVATTotal    += int2Hex(char2num(VATTotal,i));

        }


        hexaDecimal = hexaDecimalSellerTag + hexaDecimalSellerValue +hexaDecimalSeller + hexaDecimalVATNumberTag + hexaDecimalVATNumberValue +  hexaDecimalVATNumber + hexaDecimalTimeStampTag

            + hexaDecimalTimeStampValue + hexaDecimalTimeStamp + hexaDecimalInvoiceTotalTag + hexaDecimalInvoiceTotalValue + hexaDecimalInvoiceTotal + hexaDecimalVATTotalTag + hexaDecimalVATTotalValue + hexaDecimalVATTotal;



        //hexaDecimal =   "011153656465722048656164204f6666696365020f3330303034393035353231303030330313323032312d31322d32322030393a32363a3331040331313105063734342e3135";


        con         = BinData::stringToData(hexadecimal);

        bindata     = new bindata();

        bindata.setData(con);

        base64Text = bindata.base64Encode();

        //base64Text  =   "ARFTZWRlciBIZWFkIE9mZmljZQIPMzAwMDQ5MDU1MjEwMDAzAxMyMDIxLTEyLTIyIDA5OjI2OjMxBAMxMTEFBjc0NC4xNQ==";


        bitmap=qrCode1.Encode(base64Text);


        bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat::get_Jpeg());//get_Jpeg

        imageContainer=Binary::constructFromMemoryStream(memoryStream).getContainer();


        


        qrCode = imageContainer;

        return qrCode;

    }


    public bitmap qrBitMap(str _qrString)

    {

        Microsoft.Dynamics.ApplicationSuite.QRCode.Encoder  qrCode1 = new Microsoft.Dynamics.ApplicationSuite.QRCode.Encoder();

        System.Drawing.Bitmap bitmap;

        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

        container           imageContainer;

        container           qrCode,conStore;


        bitmap=qrCode1.Encode(_qrString);


        bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat::get_Jpeg());//get_Jpeg

        imageContainer=Binary::constructFromMemoryStream(memoryStream).getContainer();


        


        qrCode = imageContainer;

        return qrCode;

    }


Use bitmap to display QR (Image/bmp)