How to get all data in shift by CRT - Microsoft Dynamics 365 Vietnam

Microsoft Dynamics 365 Vietnam

Song Nghia - Microsoft Dynamics 365 Vietnam

Breaking

Thursday, May 21, 2020

How to get all data in shift by CRT

How to get all data in shift by CRT

Song Nghia - Technical Consultant

In this class, You can learn how to get object shift and linking data to object shift like: Tax, Tender, ActiveLine.

public static class LAW_ShiftCalculator
{
 public static void Calculate(
   RequestContext context,
   Shift currentShift,
   string shiftTerminalId,
   long shiftId)
 {
  GetEndOfDayShiftDetailsDataRequest detailsDataRequest = new GetEndOfDayShiftDetailsDataRequest(shiftTerminalId, shiftId, context.GetChannelConfiguration().PriceIncludesSalesTax);
  Shift entity1 = context.Runtime.Execute>((Request)detailsDataRequest, context).Entity;
  LAW_ShiftCalculator.SetShiftSalesTotals(currentShift, entity1);
  LAW_ShiftCalculator.SetShiftTenderLine(currentShift, entity1);
  LAW_ShiftCalculator.SetShiftAccountLines(currentShift, entity1);
  GetShiftTransactionsCountDataRequest countDataRequest = new GetShiftTransactionsCountDataRequest(shiftTerminalId, shiftId);
  Shift entity2 = context.Runtime.Execute>((Request)countDataRequest, context).Entity;
  LAW_ShiftCalculator.SetRetailTransactionCount(currentShift, entity2);
  GetShiftTenderedAmountDataRequest amountDataRequest = new GetShiftTenderedAmountDataRequest(shiftTerminalId, shiftId, QueryResultSettings.AllRecords);
  ReadOnlyCollection results = context.Runtime.Execute>((Request)amountDataRequest, context).PagedEntityCollection.Results;
  LAW_ShiftCalculator.SetShiftTenderLineTenderAmounts(currentShift, (IEnumerable)results);
  if (context.GetChannelConfiguration().CountryRegionISOCode != CountryRegionISOCode.SE)
   return;
  GetEndOfDayShiftDetailsSwedenDataRequest swedenDataRequest = new GetEndOfDayShiftDetailsSwedenDataRequest(currentShift.StoreId, shiftTerminalId, shiftId, context.GetChannelConfiguration().PriceIncludesSalesTax);
  Shift entity3 = context.Runtime.Execute>((Request)swedenDataRequest, context).Entity;
  LAW_ShiftCalculator.SetShiftSalesTotals_SE(currentShift, entity3);
  LAW_ShiftCalculator.SetShiftTaxLine(currentShift, entity3);
 }

 private static void SetShiftSalesTotals(Shift currentShift, Shift shiftTotals)
 {
  currentShift.SalesTotal = shiftTotals.SalesTotal;
  currentShift.ReturnsTotal = shiftTotals.ReturnsTotal;
  currentShift.DiscountTotal = shiftTotals.DiscountTotal;
  currentShift.TaxTotal = shiftTotals.TaxTotal;
  currentShift.PaidToAccountTotal = shiftTotals.PaidToAccountTotal;
  currentShift.SuspendedTransactionCount = shiftTotals.SuspendedTransactionCount;
  currentShift.DepositCollected = shiftTotals.DepositCollected;
  currentShift.DepositRedeemed = shiftTotals.DepositRedeemed;
  currentShift.DepositRefunded = shiftTotals.DepositRefunded;
  currentShift.CancellationCharges = shiftTotals.CancellationCharges;
  currentShift.CustomerOrdersPlaced = shiftTotals.CustomerOrdersPlaced;
  currentShift.CustomerOrdersCanceled = shiftTotals.CustomerOrdersCanceled;
  currentShift.CustomerOrdersVoidedLines = shiftTotals.CustomerOrdersVoidedLines;
  currentShift.CustomerOrdersEdited = shiftTotals.CustomerOrdersEdited;
  currentShift.GiftCardsTotal = shiftTotals.GiftCardsTotal;
  currentShift.GiftCardCashOutTotal = shiftTotals.GiftCardCashOutTotal;
  currentShift.VoidedSalesTotal = shiftTotals.VoidedSalesTotal;
  currentShift.ChargeTotal = shiftTotals.ChargeTotal;
 }

 private static void SetShiftSalesTotals_SE(Shift currentShift, Shift shiftTotals)
 {
  currentShift.ReceiptsCount = shiftTotals.ReceiptsCount;
  currentShift.ReturnsCount = shiftTotals.ReturnsCount;
  currentShift.GoodsSoldQty = shiftTotals.GoodsSoldQty;
  currentShift.ServicesSoldQty = shiftTotals.ServicesSoldQty;
  currentShift.ReceiptCopiesCount = shiftTotals.ReceiptCopiesCount;
  currentShift.ReceiptCopiesTotal = shiftTotals.ReceiptCopiesTotal;
  currentShift.PriceOverrideTotal = shiftTotals.PriceOverrideTotal;
  currentShift.SuspendedTotal = shiftTotals.SuspendedTotal;
  currentShift.SalesTotalIncludingTax = shiftTotals.SalesTotalIncludingTax;
  currentShift.SalesTotalExcludingTax = shiftTotals.SalesTotalExcludingTax;
  currentShift.SalesGrandTotal = shiftTotals.SalesGrandTotal;
  currentShift.ReturnsGrandTotal = shiftTotals.ReturnsGrandTotal;
 }

 private static void SetShiftTenderLine(Shift currentShift, Shift endOfDayShiftDetails)
 {
  currentShift.TenderLines.AddRange((IEnumerable)endOfDayShiftDetails.TenderLines);
 }

 private static void SetShiftAccountLines(Shift currentShift, Shift endOfDayShiftDetails)
 {
  currentShift.AccountLines.AddRange((IEnumerable)endOfDayShiftDetails.AccountLines);
 }

 private static void SetShiftTaxLine(Shift currentShift, Shift endOfDayShiftDetails)
 {
  currentShift.TaxLines.AddRange((IEnumerable)endOfDayShiftDetails.TaxLines);
 }

 private static void SetShiftTenderLineTenderAmounts(
   Shift currentShift,
   IEnumerable shiftTenderLines)
 {
  foreach (ShiftTenderLine shiftTenderLine in shiftTenderLines)
  {
   ShiftTenderLine orCreate = currentShift.TenderLines.FindOrCreate(shiftTenderLine);
   orCreate.CountingRequired = shiftTenderLine.CountingRequired;
   orCreate.TenderCurrency = shiftTenderLine.TenderCurrency;
   orCreate.ChangeLine = shiftTenderLine.ChangeLine;
   if (orCreate.ChangeLine)
   {
    orCreate.ChangeAmountOfTenderCurrency = Decimal.Negate(shiftTenderLine.TenderedAmountOfTenderCurrency);
    orCreate.ChangeAmountOfStoreCurrency = Decimal.Negate(shiftTenderLine.TenderedAmountOfStoreCurrency);
   }
   else
   {
    orCreate.TenderedAmountOfTenderCurrency = shiftTenderLine.TenderedAmountOfTenderCurrency;
    orCreate.TenderedAmountOfStoreCurrency = shiftTenderLine.TenderedAmountOfStoreCurrency;
    orCreate.Count = shiftTenderLine.Count;
   }
  }
 }

 private static ShiftTenderLine FindOrCreate(
   this IList shiftTenderLines,
   ShiftTenderLine tenderLine)
 {
  string tenderTypeId = tenderLine.TenderTypeId;
  string tenderCurrency = tenderLine.TenderCurrency;
  string tenderTypeName = tenderLine.TenderTypeName;
  ShiftTenderLine shiftTenderLine = shiftTenderLines.FirstOrDefault((Func)(p =>
  {
   if (p.TenderTypeId == tenderTypeId)
    return p.TenderCurrency == tenderCurrency;
   return false;
  }));
  if (shiftTenderLine == null)
  {
   shiftTenderLine = new ShiftTenderLine()
   {
    TenderTypeId = tenderTypeId,
    CardTypeId = string.Empty,
    TenderCurrency = tenderCurrency,
    TenderTypeName = tenderTypeName
   };
   shiftTenderLines.Add(shiftTenderLine);
  }
  return shiftTenderLine;
 }

 private static void SetRetailTransactionCount(Shift currentShift, Shift endOfDayShift)
 {
  currentShift.VoidTransactionCount = endOfDayShift.VoidTransactionCount;
  currentShift.NoSaleTransactionCount = endOfDayShift.NoSaleTransactionCount;
  currentShift.RoundedAmountTotal = endOfDayShift.RoundedAmountTotal;
  currentShift.TransactionCount = endOfDayShift.TransactionCount;
  currentShift.CustomerCount = endOfDayShift.CustomerCount;
  currentShift.SaleTransactionCount = endOfDayShift.SaleTransactionCount;
 }
}