How Generate QR Code for any document in Dynamics 365 F&O

 

Step 1 create QRCode contract class, In this class we will create all parms mehtod for values which we want to appear in QR Code

// Contract class code start 

class QRCodeContract

{

  InvoiceDate   invoiceDate;

  TimeOfDay    time;

  InvoiceId    invoiceId;

  Amount     invoiceAmount;

  Amount     DocAmount;

  Name      sellerName;

  String50    DocNumber;

     public String50 parmDocNum(String50 _DocNumber = DocNumber)

  {

    DocNumber = _DocNumber;

    return DocNumber;

  }

  public InvoiceDate parmInvoiceDate(InvoiceDate _invoiceDate = invoiceDate)

  {

    invoiceDate = _invoiceDate;

    return invoiceDate;

  }

  public TimeOfDay parmTime(TimeOfDay _time = time)

  {

    time = _time;

    return time;

  }

  public InvoiceId parmInvoiceId(InvoiceId _invoiceId = invoiceId)

  {

    invoiceId = _invoiceId;

    return invoiceId;

  }

  public Amount parmInvoiceAmount(Amount _invoiceAmount = invoiceAmount)

  {

    invoiceAmount = _invoiceAmount;

    return invoiceAmount;

  }


  public Amount parmDocAmount(Amount _DocAmount = DocAmount)

  {

    DocAmount = _DocAmount;

    return DocAmount;

  }


  public Name parmSellerName(Name _sellerName = sellerName)

  {

    sellerName = _sellerName;

    return sellerName;

  }

}

// QR Code contract end

Step 2 : We will create a QRCode helper class to build QR Code

// Code start for QRCode Helper 

using QRCoder;

class QRCodeHelper

{

  QRCodeContract context;

  public static container getQRCode(str _value)

  {

    System.Drawing.Bitmap result;

    QRCodeGenerator.ECCLevel eccLevel;


    QRCodeGenerator qrGenerator = new QRCodeGenerator();

    QRCodeData qrCodeData = qrGenerator.CreateQrCode(_value, QRCodeGenerator.ECCLevel::M);

    QRCode qrCode = new QRCode(qrCodeData);

    result = qrCode.GetGraphic(20);

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

    result.Save(memoryStream, System.Drawing.Imaging.ImageFormat::get_Png());

    return Binary::constructFromMemoryStream(memoryStream).getContainer();

  }

  public QRCodeContract parmContract(QRCodeContract _context = context)

  {

    context = _context;

    return context;

  }

  public final str getString()

  {

    System.Byte[] byteArray = this.getByteArray();

    return System.Convert::ToBase64String(byteArray);

  }

  protected final str numFormat(real _num)

  {

    return num2Str(_num, 0, 2, DecimalSeparator ::Dot, ThousandSeparator::None);

  }

  public System.Byte[] getByteArray()

  {

    const System.Byte SellerNameTag = 1;

    const System.Byte SellerRegNumTag = 2;

    const System.Byte TimeStampTag = 3;

    const System.Byte TotalAmountTag = 4;

    const System.Byte TotalTaxAmountTag = 5;

    System.Byte[] encodedSellerName = this.createTLVField(SellerNameTag, context.parmSellerName());

    System.Byte[] encodedSellerRegNum = this.createTLVField(SellerRegNumTag, context.parmDocNum());

    System.Byte[] encodedTimeStamp = this.createTLVField(TimeStampTag, DateTimeUtil::toStr(

      DateTimeUtil::newDateTime(

        context.parmInvoiceDate(),

        context.parmTime())));

    System.Byte[] encodedTotalAmount = this.createTLVField(TotalAmountTag, this.numFormat(context.parmInvoiceAmount()));

    System.Byte[] encodedTotalTaxAmount = this.createTLVField(TotalTaxAmountTag, this.numFormat(context.parmDocAmount()));

    List byteArrayList = new List(Types::AnyType);

    byteArrayList.addEnd(encodedSellerName);

    byteArrayList.addEnd(encodedSellerRegNum);

    byteArrayList.addEnd(encodedTimeStamp);

    byteArrayList.addEnd(encodedTotalAmount);

    byteArrayList.addEnd(encodedTotalTaxAmount);

    return QRCodeHelper::concatBytes(byteArrayList);

  }

  protected final System.Byte[] createTLVField(System.Byte _tag, str _value)

  {

    System.Byte[] valueByteArray = new System.Text.UTF8Encoding().GetBytes(_value);

    System.Byte lengthByte = valueByteArray.Length;




    if (lengthByte != valueByteArray.Length)

    {

      throw error(error::wrongUseOfFunction(funcName()));

    }

    System.Byte[] tagArray = new System.Byte[1]();

    tagArray.SetValue(_tag, 0);

    System.Byte[] lengthArray = new System.Byte[1]();

    lengthArray.SetValue(lengthByte, 0);

    List byteArrayList = new List(Types::AnyType);

    byteArrayList.addEnd(tagArray);

    byteArrayList.addEnd(lengthArray);

    byteArrayList.addEnd(valueByteArray);

    return QRCodeHelper::concatBytes(byteArrayList);

  }

  protected static System.Byte[] concatBytes(List _byteArrayList)

  {

    System.Byte[] ret;

    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())

    {

      using (System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream))

      {

        ListEnumerator le = _byteArrayList.getEnumerator();

        while(le.moveNext())

        {

          System.Byte[] value = le.current();

          writer.Write(value);

        }

      }

      ret = stream.ToArray();

    }

    return ret;

  }

}


// QRCode helper class code ended

// Step 3 : By the following way we can get QR code from helper class and then can insert into table field, the field extended data type should be Bitmap.

// Code to get QRCode start

       QRCodeHelper qrHelper = new QRCodeHelper();

       QRCodeContract contractLocal = new QRCodeContract();

      contractLocal.parmSellerName(companyInfo.name);

      contractLocal.parmDocNum(companyInfo.CoRegNum);

      contractLocal.parmInvoiceDate(custInvoiceJour.InvoiceDate);

      contractLocal.parmTime(DateTimeUtil::time(DateTimeUtil::applyTimeZoneOffset(

                      custInvoiceJour.CreatedDateTime,

                      DateTimeUtil::getCompanyTimeZone())));

      contractLocal.parmInvoiceAmount(custInvoiceJour.InvoiceAmount);

      contractLocal.parmDocAmount(custInvoiceJour.SumTax);

      qrHelper.parmContract(contractLocal);

      //QR code

      table.QRCode      = QRCodeHelper::getQRCode(qrHelper.getString());

// QRCode getting logic ended

Comments