Utilities to query in retail transaction table - Microsoft Dynamics 365 Vietnam

Microsoft Dynamics 365 Vietnam

Song Nghia - Microsoft Dynamics 365 Vietnam

Breaking

Thursday, March 19, 2020

Utilities to query in retail transaction table

Utilities to query in retail transaction table
Song Nghia - Technical Consultant


var queryRetailTransactionTable = new SqlPagedQuery(QueryResultSettings.SingleRecord)

 public void getRegulerDataInShift(Receipt receipt, RequestContext context, Shift shift, SingleEntityDataServiceResponse deviceConfiguration)
 {
  XReadingData.STORE = shift.StoreId;
  XReadingData.TERMINALID = terminal.TerminalId;
  XReadingData.SHIFT = shift.ShiftId.ToString();
  XReadingData.BEGININGBALANCE = globalLastShiftBalance;
  XReadingData.XREPORTID = XReadingNumber;
  XReadingData.NOOFTRANSACTION = shift.SaleTransactionCount;
  XReadingData.ROUNDING = shift.RoundedAmountTotal;
  XReadingData.NOOFABORTED = shift.VoidedSalesTotal;
  XReadingData.DOCNUM = XReadingNumber;
  XReadingData.TIN = deviceConfiguration.Entity.TaxIdNumber;
  XReadingData.STAFFNAME = this.GetEmployeeByStaffId(shift).Entity.Name;
  XReadingData.DATAAREAID = originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;
  bool flag1 = shift.OverShortTotal < Decimal.Zero;
  if (flag1)
   XReadingData.SHORTAMOUNT = shift.OverShortTotal;
  else
   XReadingData.OVERAMOUNT = shift.OverShortTotal;
  XReadingData.COMPANYADDRESS = "11th Floor, Times Plaza Building, U.N. Avenue, corner Taft Ave, Manila, 1004 Metro Manila";
  XReadingData.COMPANYNAME = "LAWSON PHILIPPINES INC.";
  XReadingData.NOOFPAYINGCUST = shift.SaleTransactionCount + shift.CustomerCount;
  XReadingData.NETSALES = XReadingData.NETSALES + XReadingData.VATAMOUNT;
  XReadingData.ENDINGBALANCE = globalLastShiftBalance + XReadingData.NETSALES;//NS
  XReadingData.NOOFLOGON = shift.LogOnTransactionCount;
  XReadingData.GROSSSALES += XReadingData.RETURNAMOUNT;
 }
 public void getAllItemSoldAndReturn(Receipt receipt, RequestContext context, Shift shift)
 {
  var queryRetailTransactionTable = new SqlPagedQuery(QueryResultSettings.SingleRecord)
  {
   DatabaseSchema = "ax",
   Select = new ColumnSet(new string[] { "TRANSACTIONID", "NETAMOUNT", "TYPE", "ENTRYSTATUS", "RECEIPTID", "NUMBEROFITEMS" }),
   From = "RETAILTRANSACTIONTABLE",
   Where = "BATCHID = @BATCHID AND CHANNEL = @CHANNEL AND STORE = @STORE AND TERMINAL = @TERMINAL AND DATAAREAID = @DATAAREAID"
  };

  queryRetailTransactionTable.Parameters["@BATCHID"] = shift.ShiftId;
  queryRetailTransactionTable.Parameters["@TERMINAL"] = terminal.TerminalId;
  queryRetailTransactionTable.Parameters["@STORE"] = shift.StoreId;
  queryRetailTransactionTable.Parameters["@CHANNEL"] = terminal.ChannelId;
  queryRetailTransactionTable.Parameters["@DATAAREAID"] = originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;
  using (DatabaseContext databaseContext = new DatabaseContext(originalReceiptsRequest.RequestContext))
  {
   var extensions = databaseContext.ReadEntity(queryRetailTransactionTable);
   //LOOP all retail transaction in shift
   foreach (ExtensionsEntity extension in extensions)
   {
    //Get data in transaction
    string transactionId = extension.GetProperty("TRANSACTIONID").ToString();
    this.sumDiscountAmountWithDiscountType(transactionId);
    this.sumVatAdjustByGroupName(transactionId);
    this.GetSalesTypeBreakDownAndAmountTotal(transactionId);
    this.specialDiscountZeroRated(transactionId);
    #region RECEIPTID
    if (decimal.Parse(extension.GetProperty("ENTRYSTATUS").ToString()) == 0 && decimal.Parse(extension.GetProperty("TYPE").ToString()) == 2)
    {
     var queryTotalItemSold = new SqlPagedQuery(QueryResultSettings.SingleRecord)
     {
      DatabaseSchema = "ax",
      Select = new ColumnSet("QTY"),
      From = "RETAILTRANSACTIONSALESTRANS",
      Where = "QTY < 0 AND TRANSACTIONSTATUS = 0  AND CHANNEL = @CHANNEL AND STORE = @STORE AND TERMINALID = @TERMINALID AND TRANSACTIONID = @TRANSACTIONID AND DATAAREAID = @DATAAREAID"
     };

     queryTotalItemSold.Parameters["@CHANNEL"] = terminal.ChannelId; ;
     queryTotalItemSold.Parameters["@STORE"] = shift.StoreId;
     queryTotalItemSold.Parameters["@TERMINALID"] = shift.TerminalId;
     queryTotalItemSold.Parameters["@TRANSACTIONID"] = transactionId;
     queryTotalItemSold.Parameters["@DATAAREAID"] = originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;
     using (DatabaseContext databaseContextD = new DatabaseContext(originalReceiptsRequest.RequestContext))
     {
      foreach (ExtensionsEntity totalItemSold in databaseContextD.ReadEntity(queryTotalItemSold))
      {
       XReadingData.NUMBEROFITEMSOLD += (-1) * decimal.Parse(totalItemSold.GetProperty("QTY").ToString());
      }
     }
    }


    if (decimal.Parse(extension.GetProperty("NETAMOUNT").ToString()) > 0)
    {
     XReadingData.NOOFRETURN++;
    }
    #endregion
   }
  }
 }
 public void getEndingSalesInvoice(Receipt receipt, RequestContext context, Shift shift)
 {
  var queryReatailTransactionDESC = new SqlPagedQuery(QueryResultSettings.SingleRecord)
  {
   DatabaseSchema = "ax",
   Select = new ColumnSet("TRANSACTIONID", "TRANSDATE", "RECEIPTID"),
   From = "RETAILTRANSACTIONTABLE",
   OrderBy = "TRANSDATE DESC, TRANSTIME DESC",
   Where = "ENTRYSTATUS = 0  AND TYPE = 2 AND CHANNEL = @CHANNEL AND BATCHID = @BATCHID AND STORE = @STORE AND TERMINAL = @TERMINAL  AND  NETAMOUNT < 0 AND DATAAREAID = @DATAAREAID"
  };

  queryReatailTransactionDESC.Parameters["@CHANNEL"] = terminal.ChannelId;
  queryReatailTransactionDESC.Parameters["@BATCHID"] = shift.ShiftId;
  queryReatailTransactionDESC.Parameters["@STORE"] = shift.StoreId;
  queryReatailTransactionDESC.Parameters["@TERMINAL"] = terminal.TerminalId;
  queryReatailTransactionDESC.Parameters["@DATAAREAID"] = originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;

  using (DatabaseContext databaseContext = new DatabaseContext(originalReceiptsRequest.RequestContext))
  {
   ExtensionsEntity extensionsField = databaseContext.ReadEntity(queryReatailTransactionDESC).FirstOrDefault();
   if (extensionsField != null)
   {
    XReadingData.ENDINGSALESINVOICENO = extensionsField.GetProperty("RECEIPTID").ToString();
   }
  }

 }
 public void getBeginingSalesReturn(Receipt receipt, RequestContext context, Shift shift)
 {
  var queryReatailTransactionReturnASC = new SqlPagedQuery(QueryResultSettings.SingleRecord)
  {
   DatabaseSchema = "ax",
   Select = new ColumnSet("TRANSACTIONID", "TRANSDATE", "RECEIPTID"),
   From = "RETAILTRANSACTIONTABLE",
   OrderBy = "TRANSDATE ASC, TRANSTIME ASC",
   Where = "TYPE = 2 AND CHANNEL = @CHANNEL AND BATCHID = @BATCHID AND STORE = @STORE AND TERMINAL = @TERMINAL AND NETAMOUNT >= 0  AND DATAAREAID = @DATAAREAID"
  };

  queryReatailTransactionReturnASC.Parameters["@CHANNEL"] = terminal.ChannelId;
  queryReatailTransactionReturnASC.Parameters["@BATCHID"] = shift.ShiftId;
  queryReatailTransactionReturnASC.Parameters["@STORE"] = shift.StoreId;
  queryReatailTransactionReturnASC.Parameters["@TERMINAL"] = terminal.TerminalId;
  queryReatailTransactionReturnASC.Parameters["@DATAAREAID"] = originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;
  using (DatabaseContext databaseContext = new DatabaseContext(originalReceiptsRequest.RequestContext))
  {
   ExtensionsEntity extensionsField = databaseContext.ReadEntity(queryReatailTransactionReturnASC).FirstOrDefault();
   if (extensionsField != null)
   {
    XReadingData.BEGINNINGRETURNNO = extensionsField.GetProperty("RECEIPTID").ToString();
   }
  }
 }
 public void getEndingSalesReturn(Receipt receipt, RequestContext context, Shift shift)
 {
  var queryReatailTransactionReturnDESC = new SqlPagedQuery(QueryResultSettings.SingleRecord)
  {
   DatabaseSchema = "ax",
   Select = new ColumnSet("TRANSACTIONID", "TRANSDATE", "RECEIPTID"),
   From = "RETAILTRANSACTIONTABLE",
   OrderBy = "TRANSDATE DESC, TRANSTIME DESC",
   Where = "TYPE = 2 AND CHANNEL = @CHANNEL AND BATCHID = @BATCHID AND STORE = @STORE AND TERMINAL = @TERMINAL AND NETAMOUNT >= 0  AND DATAAREAID = @DATAAREAID"
  };

  queryReatailTransactionReturnDESC.Parameters["@CHANNEL"] = terminal.ChannelId;
  queryReatailTransactionReturnDESC.Parameters["@BATCHID"] = shift.ShiftId;
  queryReatailTransactionReturnDESC.Parameters["@STORE"] = shift.StoreId;
  queryReatailTransactionReturnDESC.Parameters["@TERMINAL"] = terminal.TerminalId;
  queryReatailTransactionReturnDESC.Parameters["@DATAAREAID"] = originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;
  using (DatabaseContext databaseContext = new DatabaseContext(originalReceiptsRequest.RequestContext))
  {
   ExtensionsEntity extensionsField = databaseContext.ReadEntity(queryReatailTransactionReturnDESC).FirstOrDefault();
   if (extensionsField != null)
   {
    XReadingData.ENDINGRETURNNO = extensionsField.GetProperty("RECEIPTID").ToString();
   }
  }
 }
 public void getBeginingSalesInvoice(Receipt receipt, RequestContext context, Shift shift)
 {
  var queryReatailTransactionASC = new SqlPagedQuery(QueryResultSettings.SingleRecord)
  {
   DatabaseSchema = "ax",
   Select = new ColumnSet("TRANSACTIONID", "TRANSDATE", "RECEIPTID"),
   From = "RETAILTRANSACTIONTABLE",
   OrderBy = "TRANSDATE ASC, TRANSTIME ASC",
   Where = "ENTRYSTATUS = 0 AND  TYPE = 2 AND CHANNEL = @CHANNEL AND BATCHID = @BATCHID AND STORE = @STORE AND TERMINAL = @TERMINAL AND  NETAMOUNT <0 and="" atabasecontext="" dataareaid="@DATAAREAID" databasecontext="" extensionsentity="" extensionsfield="databaseContext.ReadEntity<ExtensionsEntity" nventlocationdataareaid="" originalreceiptsrequest.requestcontext.getchannelconfiguration="" originalreceiptsrequest.requestcontext="" queryreatailtransactionasc.parameters="" shift.shiftid="" shift.storeid="" terminal.channelid="" terminal.terminalid="" using="">(queryReatailTransactionASC).FirstOrDefault();
   if (extensionsField != null)
   {
    XReadingData.BEGINNINGSALESINVOICENO = extensionsField.GetProperty("RECEIPTID").ToString();
   }
  }
 }
 public void getTerminalDetail(Receipt receipt, RequestContext context, Shift shift)
 {
  var queryRetailTerminal = new SqlPagedQuery(QueryResultSettings.SingleRecord)
  {
   DatabaseSchema = "ext",
   Select = new ColumnSet(new string[] {
   "LAW_MACHINEIDENTIFICATIONNUMBER",
   "LAW_MACHINESERIALNUMBER",
   "LAW_PERMITTOUSENUMBER",
   "LAW_PTUDATEISSUED",
   "LAW_PTUVALIDTO",
   "LAW_ACCREDITATIONNUMBER",
   "LAW_ACCDTNDATEISSUED",
   "LAW_ACCDTNVALIDTO",
   "TERMINALID" }),
   From = "LAW_RETAILTERMINALTABLE",
   Where = "TERMINALID = @TERMINALID"
  };

  queryRetailTerminal.Parameters["@TERMINALID"] = originalReceiptsRequest.RequestContext.GetTerminal().TerminalId;
  using (DatabaseContext databaseContext = new DatabaseContext(originalReceiptsRequest.RequestContext))
  {
   ExtensionsEntity extensions = databaseContext.ReadEntity(queryRetailTerminal).FirstOrDefault();

   if (extensions != null)
   {
    //Header 1.5 
    XReadingData.MIN = extensions.GetProperty("LAW_MACHINESERIALNUMBER").ToString();
    //Header 1.6
    XReadingData.SN = extensions.GetProperty("LAW_MACHINESERIALNUMBER").ToString();

   }
  }
 }
 public void getDateTimeShiftAndReportName(Receipt receipt, RequestContext context, Shift shift)
 {
  string str1 = string.Empty;
  string str2 = string.Empty;
  string str3 = string.Empty;
  string str4 = string.Empty;

  if (shift.StartDateTime.HasValue)
  {
   if (receipt.ReceiptType == ReceiptType.ZReport)
   {
    str1 = this.FormatDate(shift.StartDateTime, context);
    str2 = this.FormatTime(shift.StartDateTime, TimeFormattingType.SystemLongTime, context);
   }
   else
   {
    str1 = this.FormatDate(shift.StartDateTime, context);
    str2 = this.FormatTime(shift.StartDateTime, TimeFormattingType.SystemLongTime, context);
   }
  }
  XReadingData.ACTUALDATE = DateTime.Parse(str1);

  DateTime t = DateTime.ParseExact(str2, "h:mm:ss tt", CultureInfo.InvariantCulture);
  //if you really need a TimeSpan this will get the time elapsed since midnight:
  TimeSpan ts = t.TimeOfDay;

  XReadingData.ACTUALTIME = ts;
  if (shift.CloseDateTime.HasValue)
  {
   str3 = this.FormatDate(shift.CloseDateTime, context);
   str4 = this.FormatTime(shift.CloseDateTime, TimeFormattingType.SystemLongTime, context);
  }
  string empty = string.Empty;

  string str5;
  if (receipt.ReceiptType != ReceiptType.XReport)
  {
   if (receipt.ReceiptType != ReceiptType.ZReport)
    throw new NotSupportedException(string.Format("Unsupported Report Type '{0}'.", (object)receipt.ReceiptType));
   str5 = shift.StaffId;
   XReadingData.REPORTNAME = "X-Reading Report";
  }
  else
  {
   str5 = context.GetPrincipal().UserId;
   XReadingData.REPORTNAME = "Tender Report";
  }
 }
 public class DistcountEntity
 {
  public string DiscountStr { get; set; }
  public decimal DiscountDecimal { get; set; }

  public DistcountEntity(string _discountType, decimal _discountAmount)
  {
   DiscountStr = _discountType;
   DiscountDecimal = _discountAmount;
  }
 }
 public void sumVatAdjustByGroupName(string transactionId)
 {
  var queryReatailTransDiscountGroupName = new SqlPagedQuery(QueryResultSettings.AllRecords)
  {
   DatabaseSchema = "ext",
   Select = new ColumnSet("DISCOUNTGROUPNAME", "TRANSACTIONID", "SALELINENUM"),
   From = "LAW_RETAILTRANSDISCOUNTGROUPBYGROUPNAMEVIEW",
   Where = "DATAAREAID = @DATAAREAID AND TRANSACTIONID = @TRANSACTIONID AND TERMINALID = @TERMINALID AND STOREID = @STOREID AND CHANNEL = @CHANNEL"
  };
  queryReatailTransDiscountGroupName.Parameters["@TERMINALID"] = shift.TerminalId;
  queryReatailTransDiscountGroupName.Parameters["@STOREID"] = shift.StoreId;
  queryReatailTransDiscountGroupName.Parameters["@CHANNEL"] = terminal.ChannelId;
  queryReatailTransDiscountGroupName.Parameters["@TRANSACTIONID"] = transactionId;
  queryReatailTransDiscountGroupName.Parameters["@DATAAREAID"] = originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;

  using (DatabaseContext databaseContext = new DatabaseContext(originalReceiptsRequest.RequestContext))
  {
   var extensions = databaseContext.ReadEntity(queryReatailTransDiscountGroupName);

   //LOOP all record on RETAILTRANSACTIONDISCOUNTTRANSEXTEND
   foreach (ExtensionsEntity extension in extensions)
   {
    decimal SALELINENUM = decimal.Parse(extension.GetProperty("SALELINENUM").ToString());
    string discountGroupName = extension.GetProperty("DISCOUNTGROUPNAME").ToString();
    decimal DiscountValue = this.sumDataInRetailTransactionTableExt(transactionId, SALELINENUM);
    listVATAdjustDiscountGroup.Add(new DistcountEntity(discountGroupName, (-1) * DiscountValue));
   }
  }
 }
 public void specialDiscountZeroRated(string transactionId)
 {
  decimal zeroDiscount = 0;
  var queryReatailTableTrans = new SqlPagedQuery(QueryResultSettings.SingleRecord)
  {
   DatabaseSchema = "ext",
   Select = new ColumnSet("LAW_VATADJUSTMENT"),
   From = "LAW_RETAILTRANSACTIONSALESLINEEXTEND",
   Where = "LAW_ZERORATEDSALESAMOUNT != 0  AND DATAAREAID = @DATAAREAID AND CHANNEL = @CHANNEL AND TERMINALID = @TERMINALID AND TRANSACTIONID = @TRANSACTIONID AND STOREID = @STOREID"
  };

  queryReatailTableTrans.Parameters["@CHANNEL"] = terminal.ChannelId;
  queryReatailTableTrans.Parameters["@STOREID"] = shift.StoreId;
  queryReatailTableTrans.Parameters["@TERMINALID"] = terminal.TerminalId;
  queryReatailTableTrans.Parameters["@TRANSACTIONID"] = transactionId;
  queryReatailTableTrans.Parameters["@DATAAREAID"] = originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;

  using (DatabaseContext databaseContextField = new DatabaseContext(originalReceiptsRequest.RequestContext))
  {
   var extensionsFields = databaseContextField.ReadEntity(queryReatailTableTrans);
   foreach (ExtensionsEntity extensionsField in extensionsFields)
    zeroDiscount += decimal.Parse(extensionsField.GetProperty("LAW_VATADJUSTMENT").ToString());
  }
  XReadingData.ZERORATEDDISCOUNT += (-1) * zeroDiscount;


 }
 public decimal sumDataInRetailTransactionTableExt(string transactionId, decimal salesLineNum)
 {
  decimal VATAdjust = 0;
  var queryReatailTableTrans = new SqlPagedQuery(QueryResultSettings.SingleRecord)
  {
   DatabaseSchema = "ext",
   Select = new ColumnSet("LAW_VATADJUSTMENT"),
   From = "LAW_RETAILTRANSACTIONSALESLINEEXTEND",
   Where = "SALESLINENUM = @SALESLINENUM  AND DATAAREAID = @DATAAREAID AND CHANNEL = @CHANNEL AND TERMINALID = @TERMINALID AND TRANSACTIONID = @TRANSACTIONID AND STOREID = @STOREID"
  };

  queryReatailTableTrans.Parameters["@CHANNEL"] = terminal.ChannelId;
  queryReatailTableTrans.Parameters["@STOREID"] = shift.StoreId;
  queryReatailTableTrans.Parameters["@TERMINALID"] = terminal.TerminalId;
  queryReatailTableTrans.Parameters["@TRANSACTIONID"] = transactionId;
  queryReatailTableTrans.Parameters["@SALESLINENUM"] = salesLineNum;
  queryReatailTableTrans.Parameters["@DATAAREAID"] = originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;

  using (DatabaseContext databaseContextField = new DatabaseContext(originalReceiptsRequest.RequestContext))
  {
   ExtensionsEntity extensionsFields = databaseContextField.ReadEntity(queryReatailTableTrans).FirstOrDefault();

   VATAdjust = decimal.Parse(extensionsFields.GetProperty("LAW_VATADJUSTMENT").ToString());
  }
  return VATAdjust;
 }
 public void sumDiscountAmountWithDiscountType(string transactionId)
 {
  var queryReatailTransDiscountTransExtend = new SqlPagedQuery(QueryResultSettings.AllRecords)
  {
   DatabaseSchema = "ext",
   Select = new ColumnSet("DISCOUNTTYPE", "TRANSACTIONID", "SALELINENUM"),
   From = "LAW_RETAILTRANSDISCOUNTGROUPBYDISCOUNTTYPEVIEW",
   Where = "DATAAREAID = @DATAAREAID AND TRANSACTIONID = @TRANSACTIONID AND TERMINALID = @TERMINALID AND STOREID = @STOREID AND CHANNEL = @CHANNEL"
  };
  queryReatailTransDiscountTransExtend.Parameters["@TERMINALID"] = shift.TerminalId;
  queryReatailTransDiscountTransExtend.Parameters["@STOREID"] = shift.StoreId;
  queryReatailTransDiscountTransExtend.Parameters["@CHANNEL"] = terminal.ChannelId;
  queryReatailTransDiscountTransExtend.Parameters["@TRANSACTIONID"] = transactionId;
  queryReatailTransDiscountTransExtend.Parameters["@DATAAREAID"] = originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;

  using (DatabaseContext databaseContext = new DatabaseContext(originalReceiptsRequest.RequestContext))
  {
   var extensions = databaseContext.ReadEntity(queryReatailTransDiscountTransExtend);

   //LOOP all record on RETAILTRANSACTIONDISCOUNTTRANSEXTEND
   foreach (ExtensionsEntity extension in extensions)
   {
    decimal SALELINENUM = decimal.Parse(extension.GetProperty("SALELINENUM").ToString());
    string discountType = extension.GetProperty("DISCOUNTTYPE").ToString();
    decimal DiscountValue = this.getDiscountAmountBaseOnDiscountType(transactionId, SALELINENUM);
    listDiscountType.Add(new DistcountEntity(discountType, (-1) * DiscountValue));
   }
  }
 }
 public decimal getDiscountAmountBaseOnDiscountType(string transactionId, decimal salesLineNum)
 {
  decimal discountAmt = 0;

  var queryReatailTransSalesTransExt = new SqlPagedQuery(QueryResultSettings.SingleRecord)
  {
   DatabaseSchema = "ax",
   Select = new ColumnSet("TERMINALID", "TRANSACTIONID", "STORE", "LINENUM", "CHANNEL", "DISCAMOUNTWITHOUTTAX"),
   From = "RETAILTRANSACTIONSALESTRANS",
   Where = "LINENUM = @SALESLINENUM AND DATAAREAID = @DATAAREAID AND CHANNEL = @CHANNEL AND TERMINALID = @TERMINALID AND TRANSACTIONID = @TRANSACTIONID AND STORE = @STORE"
  };

  queryReatailTransSalesTransExt.Parameters["@CHANNEL"] = terminal.ChannelId;
  queryReatailTransSalesTransExt.Parameters["@STORE"] = shift.StoreId;
  queryReatailTransSalesTransExt.Parameters["@TERMINALID"] = terminal.TerminalId;
  queryReatailTransSalesTransExt.Parameters["@TRANSACTIONID"] = transactionId;
  queryReatailTransSalesTransExt.Parameters["@SALESLINENUM"] = salesLineNum;
  queryReatailTransSalesTransExt.Parameters["@DATAAREAID"] = originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;

  using (DatabaseContext databaseContextField = new DatabaseContext(originalReceiptsRequest.RequestContext))
  {
   ExtensionsEntity extensionsField = databaseContextField.ReadEntity(queryReatailTransSalesTransExt).FirstOrDefault();
   if (extensionsField != null)
   {
    //Get discount amount
    discountAmt = decimal.Parse(extensionsField.GetProperty("DISCAMOUNTWITHOUTTAX").ToString());
   }
  }
  return discountAmt;
 }
 public void GetSalesTypeBreakDownAndAmountTotal(string transactionId)
 {
  decimal ZeroRatedSalesAmount = 0;
  decimal VATableSalesAmount = 0;
  decimal VATExemptSalesAmount = 0;

  decimal ZeroRatedSalesVATAmount = 0;
  decimal VATableSales = 0;
  decimal VATExemptSales = 0;

  decimal NetSales = 0;
  decimal Grosssales = 0;
  decimal ReturnAmount = 0;

  var queryRetailTransactionTable = new SqlPagedQuery(QueryResultSettings.SingleRecord)
  {
   DatabaseSchema = "ext",
   Select = new ColumnSet(new string[] { "LAW_VATEXEMPTSALESVATAMOUNT", "LAW_ZERORATEDSALESVATAMOUNT", "LAW_VATABLESSALESAMOUNT", "LAW_VATABLESSALESVATAMOUNT", "TRANSACTIONID", "LAW_VATEXEMPTSALESAMOUNT", "LAW_ZERORATEDSALESAMOUNT", "LAW_NETSALES", "LAW_LPITOTAL" }),
   From = "LAW_RETAILTRANSACTIONTABLEEXTEND",
   Where = "TRANSACTIONID = @TRANSACTIONID AND CHANNEL = @CHANNEL AND STOREID = @STOREID  AND DATAAREAID = @DATAAREAID"
  };

  queryRetailTransactionTable.Parameters["@STOREID"] = shift.StoreId;
  queryRetailTransactionTable.Parameters["@TRANSACTIONID"] = transactionId;
  queryRetailTransactionTable.Parameters["@CHANNEL"] = terminal.ChannelId;
  queryRetailTransactionTable.Parameters["@DATAAREAID"] = originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;
  using (DatabaseContext databaseContextField = new DatabaseContext(originalReceiptsRequest.RequestContext))
  {
   var extensionsField = databaseContextField.ReadEntity(queryRetailTransactionTable);
   foreach (ExtensionsEntity extension in extensionsField)
   {
    //Get discount amount
    VATableSalesAmount += decimal.Parse(extension.GetProperty("LAW_VATABLESSALESAMOUNT").ToString());
    VATableSales += decimal.Parse(extension.GetProperty("LAW_VATABLESSALESVATAMOUNT").ToString());
    VATExemptSalesAmount += decimal.Parse(extension.GetProperty("LAW_VATEXEMPTSALESAMOUNT").ToString());
    VATExemptSales += decimal.Parse(extension.GetProperty("LAW_VATEXEMPTSALESVATAMOUNT").ToString());
    ZeroRatedSalesAmount += decimal.Parse(extension.GetProperty("LAW_ZERORATEDSALESAMOUNT").ToString());
    ZeroRatedSalesVATAmount += decimal.Parse(extension.GetProperty("LAW_ZERORATEDSALESVATAMOUNT").ToString());
    NetSales += decimal.Parse(extension.GetProperty("LAW_NETSALES").ToString());
    Grosssales += decimal.Parse(extension.GetProperty("LAW_LPITOTAL").ToString());

    if (decimal.Parse(extension.GetProperty("LAW_LPITOTAL").ToString()) > 0)
    {
     ReturnAmount += decimal.Parse(extension.GetProperty("LAW_LPITOTAL").ToString());
    }


   }

  }
  XReadingData.VATAMOUNT += (-1) * (VATableSales + VATExemptSales + ZeroRatedSalesVATAmount);

  //XReadingData.ZERORATEDSALESVATAMOUNT += (-1) * ZeroRatedSalesAmount;
  XReadingData.ZERORATEDSALES += (-1) * ZeroRatedSalesAmount;
  XReadingData.VATABLESALES += (-1) * VATableSalesAmount;
  XReadingData.VATEXEMPTSALES += (-1) * VATExemptSalesAmount;
  //nghia.song can remove code nay sao
  XReadingData.NETSALES += NetSales;
  XReadingData.GROSSSALES += (-1) * Grosssales;
  XReadingData.RETURNAMOUNT += ReturnAmount;




 }
 private SingleEntityDataServiceResponse GetEmployeeByStaffId(Shift shift)
 {
  SqlPagedQuery sqlPagedQuery = new SqlPagedQuery(QueryResultSettings.SingleRecord)
  {
   From = "GetEmployeeByStaffId(@nvc_StaffId, @nvc_DataAreaId)"
  };
  sqlPagedQuery.Parameters["@nvc_StaffId"] = shift.StaffId;
  sqlPagedQuery.Parameters["@nvc_DataAreaId"] = originalReceiptsRequest.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;
  Employee entity1;
  using (DatabaseContext databaseContext = new DatabaseContext(originalReceiptsRequest.RequestContext))
   entity1 = databaseContext.ReadEntity((IDatabaseQuery)sqlPagedQuery).SingleOrDefault();
  if (entity1 != null)
  {
   ChannelConfiguration channelConfiguration = originalReceiptsRequest.RequestContext.GetChannelConfiguration();
   if (channelConfiguration != null)
    entity1.Images = RichMediaHelper.PopulateEmployeeMediaInformation(entity1.StaffId, entity1.ImageUri, channelConfiguration.EmployeeDefaultImageTemplate);
   else if (originalReceiptsRequest.RequestContext.GetPrincipal().ChannelId != 0L)
   {
    GetChannelConfigurationDataRequest configurationDataRequest = new GetChannelConfigurationDataRequest(originalReceiptsRequest.RequestContext.GetPrincipal().ChannelId);
    ChannelConfiguration entity2 = originalReceiptsRequest.RequestContext.Execute>((Request)configurationDataRequest).Entity;
    entity1.Images = RichMediaHelper.PopulateEmployeeMediaInformation(entity1.StaffId, entity1.ImageUri, entity2.EmployeeDefaultImageTemplate);
   }
  }
  return new SingleEntityDataServiceResponse(entity1);
 }
private SalesOrder GetSalesOrderForTransactionWithId(RequestContext requestContext, string transactionId)
{
 SalesOrder salesOrder = new SalesOrder();

 var getCartRequest = new GetSalesOrderDetailsByTransactionIdServiceRequest(transactionId, SearchLocation.Local);

 var getCartResponse = requestContext.Execute(getCartRequest);

 salesOrder = getCartResponse.SalesOrder;

 return salesOrder;
}

private string roundingCurrency(decimal value, string currencyCode, RequestContext context)
{
 GetRoundedValueServiceRequest roundingRequest = null;

 string currencySymbol = string.Empty;

 // Get the currency symbol.
 if (!string.IsNullOrWhiteSpace(currencyCode))
 {
  var getCurrenciesDataRequest = new GetCurrenciesDataRequest(currencyCode, QueryResultSettings.SingleRecord);
  Currency currency = context.Runtime.Execute>(getCurrenciesDataRequest, context).PagedEntityCollection.FirstOrDefault();
  currencySymbol = currency.CurrencySymbol;
 }

 roundingRequest = new GetRoundedValueServiceRequest(value, currencyCode, 0, false);

 decimal roundedValue = context.Execute(roundingRequest).RoundedValue;

 var formattingRequest = new GetFormattedCurrencyServiceRequest(roundedValue, currencySymbol);
 string formattedValue = context.Execute(formattingRequest).FormattedValue;
 return formattedValue;
}