Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @using System
- @using System.Collections.Generic
- @using System.Linq
- @using Resto.Front.PrintTemplates.Cheques.Razor
- @using Resto.Front.PrintTemplates.Cheques.Razor.TemplateModels
- @inherits TemplateBase<IBillCheque>
- @{
- var order = Model.Order;
- }
- <doc>
- @* Header (begin) *@
- <whitespace-preserve>@Raw(string.Join(Environment.NewLine, Model.Extensions.BeforeHeader))</whitespace-preserve>
- <center>
- <line symbols="*" />
- <split><whitespace-preserve>- = @Model.CommonInfo.Group.Name = -</whitespace-preserve></split><np />
- <line symbols="*" />
- </center>
- @if (@Model.CommonInfo.Group.Name == "ДАЧА") {
- <center>
- Реквизиты ДАЧА
- </center>
- } else {
- <center>
- Реквизиты Спартак
- </center><np />
- <line symbols="*" />
- }
- @if (Model.AdditionalServiceChequeInfo == null)
- {
- <center>
- @Resources.BillHeaderTitle
- </center>
- }
- <pair fit="right"
- left="@string.Format(Resources.BillHeaderWaiterPattern, order.Waiter.GetNameOrEmpty())"
- right="@string.Format(Resources.BillHeaderTablePattern, order.Table.Number)" />
- <pair fit="right"
- left="@string.Format(Resources.BillHeaderOrderOpenPattern, FormatLongDateTime(order.OpenTime))"
- right="@string.Format(Resources.BillHeaderOrderNumberPattern, order.Number)" />
- <pair fit="right"
- left='@string.Format("Пречек: {0}", FormatLongDateTime(Model.CommonInfo.CurrentTime))'
- right="@string.Format(Resources.BillHeaderSectionPattern, order.Table.Section.Name)" />
- @if (Model.AdditionalServiceChequeInfo != null)
- {
- <left>
- @string.Format(Resources.AdditionalServiceHeaderOrderItemsAddedPattern, FormatLongDateTime(Model.CommonInfo.CurrentTime))
- </left>
- }
- @foreach (var clientInfo in
- from discountItem in order.DiscountItems
- where discountItem.CardInfo != null
- select discountItem.CardInfo into cardInfo
- select string.IsNullOrWhiteSpace(cardInfo.MaskedCard) ? cardInfo.Owner : string.Format("{0} ({1})", cardInfo.Owner, cardInfo.MaskedCard) into clientInfo
- where !string.IsNullOrWhiteSpace(clientInfo)
- select clientInfo)
- {
- <left>
- @string.Format(Resources.ClientFormat, clientInfo)
- </left>
- }
- <whitespace-preserve>@Raw(string.Join(Environment.NewLine, Model.Extensions.AfterHeader))</whitespace-preserve>
- @if (Model.AdditionalServiceChequeInfo != null)
- {
- if (order.ClientBinding != null && !string.IsNullOrWhiteSpace(order.ClientBinding.CardNumber))
- {
- <left>
- @string.Format(Resources.CardPattern, order.ClientBinding.CardNumber)
- </left>
- }
- <np />
- <center>
- @Resources.AdditionalServiceHeaderTitle
- </center>
- }
- @* Header (end) *@
- @* Body (begin) *@
- <table>
- <columns>
- <column formatter="split"/>
- <column align="right" autowidth="" />
- <column width="0" />
- <column align="right" autowidth="" />
- </columns>
- <cells>
- @Guests()
- <linecell />
- @Summaries()
- </cells>
- </table>
- @* Body (end) *@
- @* Footer (begin) *@
- <np />
- <whitespace-preserve>@Raw(string.Join(Environment.NewLine, Model.Extensions.BeforeFooter))</whitespace-preserve>
- <center>
- <split>
- <whitespace-preserve>@Model.CommonInfo.CafeSetup.BillFooter</whitespace-preserve>
- </split>
- </center>
- <np />
- <np />
- <whitespace-preserve>@Raw(string.Join(Environment.NewLine, Model.Extensions.AfterFooter))</whitespace-preserve>
- <np />
- @* Footer (end) *@
- </doc>
- @helper Guests()
- {
- var order = Model.Order;
- Func<IOrderItem, bool> orderItemsFilter;
- if (Model.AdditionalServiceChequeInfo != null)
- {
- orderItemsFilter = orderItem => Model.AdditionalServiceChequeInfo.AddedOrderItems.Contains(orderItem);
- }
- else
- {
- orderItemsFilter = orderItem => orderItem.DeletionInfo == null;
- }
- var guestsWithItems = Model.Order.Table.Section.DisplayGuests
- ? order.Guests
- .Select(guest => new { Guest = guest, Items = guest.Items.Where(item => orderItemsFilter(item) && OrderItemsToPrintFilter(item, order.DiscountItems)) })
- .Where(guestWithItems => guestWithItems.Items.Any())
- .ToList()
- : Enumerable.Repeat(new { Guest = order.Guests.FirstOrDefault(), Items = order.Guests.SelectMany(g => g.Items.Where(item => orderItemsFilter(item) && OrderItemsToPrintFilter(item, order.DiscountItems))) }, 1)
- .Where(guestWithItems => guestWithItems.Items.Any())
- .ToList();
- if (!guestsWithItems.Any())
- {
- return;
- }
- <linecell />
- <ct>
- @Resources.NameColumnHeader
- </ct>
- <ct>
- @Resources.ProductAmount
- </ct>
- <ct />
- <ct>
- @Resources.ResultSum
- </ct>
- <linecell />
- if (guestsWithItems.Count == 1)
- {
- @SingleGuest(guestsWithItems.Single().Items)
- }
- else
- {
- @OneOfMultipleGuests(guestsWithItems.First().Guest, guestsWithItems.First().Items)
- foreach (var guestWithItems in guestsWithItems.Skip(1))
- {
- <linecell symbols=" " />
- @OneOfMultipleGuests(guestWithItems.Guest, guestWithItems.Items)
- }
- }
- }
- @helper SingleGuest(IEnumerable<IOrderItem> items)
- {
- foreach (var orderItemGroup in items.GroupBy(_ => _, CreateComparer<IOrderItem>(AreOrderItemsEqual)))
- {
- <ct>
- @orderItemGroup.Key.Product.Name
- </ct>
- <ct>
- <center>@string.Format("{0:#,0.0##}", orderItemGroup.Sum(orderItem => orderItem.Amount))</center>
- </ct>
- <ct />
- <ct>
- @FormatMoney(orderItemGroup.Sum(orderItem => orderItem.GetCost()))
- </ct>
- @CategorizedDiscountsForOrderEntryGroup(orderItemGroup)
- if (Model.Order.Table.Section.PrintProductItemCommentInCheque && orderItemGroup.Key is IProductItem)
- {
- var productItem = (IProductItem)orderItemGroup.Key;
- if (productItem.Comment != null && !productItem.Comment.Deleted)
- {
- <c>
- <table cellspacing="0">
- <columns>
- <column width="2" />
- <column />
- </columns>
- <cells>
- <c />
- <c>
- <split>
- - @productItem.Comment.Text
- </split>
- </c>
- </cells>
- </table>
- </c>
- <c colspan="3" />
- }
- }
- foreach (var orderEntry in orderItemGroup.Key.GetNotDeletedChildren().Where(orderEntry => OrderItemChildrenFilter(orderEntry, orderItemGroup.Key)))
- {
- <ct>
- <whitespace-preserve>@(" - " + orderEntry.Product.Name)</whitespace-preserve>
- </ct>
- if (orderEntry.Amount != 1m)
- {
- <ct>
- <center>@string.Format("{0:#,0.0##}", orderEntry.Amount)</center>
- </ct>
- }
- else
- {
- <ct />
- }
- <ct />
- if (orderEntry.Price != 0m)
- {
- <ct>
- @FormatMoney(orderEntry.GetCost())
- </ct>
- }
- else
- {
- <ct />
- }
- @CategorizedDiscountsForOrderEntryGroup(EnumerableEx.Return(orderEntry))
- }
- }
- }
- @helper CategorizedDiscountsForOrderEntryGroup(IEnumerable<IOrderEntry> entries)
- {
- var orderEntry = entries.First();
- if (orderEntry.Price != 0m)
- {
- var categorizedDiscounts =
- from discountItem in Model.Order.DiscountItems
- where discountItem.IsCategorized &&
- discountItem.PrintDetailedInPrecheque
- let discountSum = entries.Sum(entry => discountItem.GetDiscountSumFor(entry))
- where discountSum != 0m
- select new
- {
- IsDiscount = discountSum > 0m,
- Sum = Math.Abs(discountSum),
- Percent = Math.Abs(CalculatePercent(entries.Sum(entry => entry.GetCost()), discountSum)),
- Name = discountItem.Type.PrintableName,
- DiscountBySum = discountItem.Type.DiscountBySum
- } into discount
- orderby discount.IsDiscount descending
- select discount;
- foreach (var categorizedDiscount in categorizedDiscounts)
- {
- <c colspan="3">
- <whitespace-preserve>@GetFormattedDiscountDescriptionForOrderItem(categorizedDiscount.IsDiscount, categorizedDiscount.Name, categorizedDiscount.DiscountBySum, categorizedDiscount.Percent)</whitespace-preserve>
- </c>
- <ct>
- @GetFormattedDiscountSum(categorizedDiscount.IsDiscount, categorizedDiscount.Sum)
- </ct>
- }
- }
- }
- @helper OneOfMultipleGuests(IGuest guest, IEnumerable<IOrderItem> items)
- {
- <c colspan="0">
- @guest.Name
- </c>
- @SingleGuest(items)
- <c colspan="3" />
- <c>
- <line />
- </c>
- var includedEntries = items.SelectMany(item => item.ExpandIncludedEntries()).ToList();
- var total = includedEntries.Sum(orderEntry => orderEntry.GetCost());
- var totalWithoutCategorizedDiscounts = total -
- (from orderEntry in includedEntries
- from discountItem in Model.Order.DiscountItems
- where discountItem.IsCategorized
- select discountItem.GetDiscountSumFor(orderEntry)).Sum();
- var totalWithoutDiscounts = totalWithoutCategorizedDiscounts -
- (from orderEntry in includedEntries
- from discountItem in Model.Order.DiscountItems
- where !discountItem.IsCategorized
- select discountItem.GetDiscountSumFor(orderEntry)).Sum();
- if (totalWithoutCategorizedDiscounts != totalWithoutDiscounts)
- {
- <c colspan="3">
- @Resources.BillFooterTotalPlain
- </c>
- <ct>
- @FormatMoney(totalWithoutCategorizedDiscounts)
- </ct>
- var nonCategorizedDiscounts =
- from discountItem in Model.Order.DiscountItems
- where !discountItem.IsCategorized
- let discountSum = includedEntries.Sum(orderEntry => discountItem.GetDiscountSumFor(orderEntry))
- select new
- {
- IsDiscount = discountSum > 0m,
- Sum = Math.Abs(discountSum),
- Percent = Math.Abs(CalculatePercent(includedEntries.Sum(entry => entry.GetCost()), discountSum)),
- Name = discountItem.Type.PrintableName,
- DiscountBySum = discountItem.Type.DiscountBySum
- } into discount
- orderby discount.IsDiscount descending
- select discount;
- foreach (var nonCategorizedDiscount in nonCategorizedDiscounts)
- {
- <c colspan="3">
- @(nonCategorizedDiscount.DiscountBySum
- ? GetFormattedDiscountDescriptionShort(nonCategorizedDiscount.IsDiscount, nonCategorizedDiscount.Name)
- : GetFormattedDiscountDescriptionDetailed(nonCategorizedDiscount.IsDiscount, nonCategorizedDiscount.Name, nonCategorizedDiscount.Percent))
- </c>
- <ct>
- @GetFormattedDiscountSum(nonCategorizedDiscount.IsDiscount, nonCategorizedDiscount.Sum)
- </ct>
- }
- }
- <c colspan="3">
- @string.Format(Model.AdditionalServiceChequeInfo == null ? Resources.BillFooterTotalGuestPattern : Resources.AdditionalServiceFooterTotalGuestPattern, guest.Name)
- </c>
- <ct>
- @FormatMoney(totalWithoutDiscounts)
- </ct>
- }
- @helper Summaries()
- {
- var order = Model.Order;
- var fullSum =
- order.GetFullSum() -
- order.DiscountItems.Where(di => !di.Type.PrintProductItemInPrecheque).Sum(di => di.GetDiscountSum());
- var categorizedDiscountItems = new List<IDiscountItem>();
- var nonCategorizedDiscountItems = new List<IDiscountItem>();
- foreach (var discountItem in order.DiscountItems.Where(di => di.Type.PrintProductItemInPrecheque && di.DiscountSums.Count > 0))
- {
- if (discountItem.IsCategorized)
- {
- categorizedDiscountItems.Add(discountItem);
- }
- else
- {
- nonCategorizedDiscountItems.Add(discountItem);
- }
- }
- var subTotal = fullSum - categorizedDiscountItems.Sum(di => di.GetDiscountSum());
- var totalWithoutDiscounts = subTotal - nonCategorizedDiscountItems.Sum(di => di.GetDiscountSum());
- var prepay = order.PrePayments.Sum(prepayItem => prepayItem.Sum);
- var total = Math.Max(totalWithoutDiscounts + order.GetVatSumExcludedFromPrice() - prepay, 0m);
- if (Model.DiscountMarketingCampaigns != null)
- {
- total -= Model.DiscountMarketingCampaigns.TotalDiscount;
- totalWithoutDiscounts -= Model.DiscountMarketingCampaigns.TotalDiscount;
- }
- var vatSumsByVat =
- (Model.AdditionalServiceChequeInfo == null
- ? order.GetIncludedEntries()
- : Model.AdditionalServiceChequeInfo.AddedOrderItems.SelectMany(item => item.ExpandIncludedEntries()))
- .Where(orderEntry => !orderEntry.VatIncludedInPrice)
- .GroupBy(orderEntry => orderEntry.Vat)
- .Where(group => group.Key != 0m)
- .Select(group => new { Vat = group.Key, Sum = group.Sum(orderEntry => orderEntry.GetVatSumExcludedFromPriceForOrderEntry(order.DiscountItems)) })
- .ToList();
- var vatSum = vatSumsByVat.Sum(vatWithSum => vatWithSum.Sum);
- <c colspan="4">
- <f2>
- @(Model.AdditionalServiceChequeInfo == null ? Resources.BillFooterTotal : Resources.AdditionalServiceFooterTotalUpper)
- </f2>
- </c>
- <c colspan="4">
- <f2>
- <right>@FormatMoney(total)</right>
- </f2>
- </c>
- if ((prepay != 0m || fullSum != total) && Model.AdditionalServiceChequeInfo == null)
- {
- <c colspan="3">
- @Resources.BillFooterFullSum
- </c>
- <ct>
- @FormatMoney(fullSum)
- </ct>
- }
- @PrintOrderDiscounts(categorizedDiscountItems, fullSum)
- if (categorizedDiscountItems.Any())
- {
- <c colspan="3">
- @Resources.BillFooterTotalPlain
- </c>
- <ct>
- @FormatMoney(subTotal)
- </ct>
- }
- @PrintOrderDiscounts(nonCategorizedDiscountItems, fullSum)
- if (Model.DiscountMarketingCampaigns != null)
- {
- foreach (var discountMarketingCampaign in Model.DiscountMarketingCampaigns.Campaigns)
- {
- <c colspan="3">
- @discountMarketingCampaign.Name
- </c>
- <ct>
- @("-" + FormatMoney(discountMarketingCampaign.TotalDiscount))
- </ct>
- }
- }
- if (prepay != 0m && (categorizedDiscountItems.Any() || nonCategorizedDiscountItems.Any()))
- {
- <c colspan="3">
- @Resources.BillFooterTotalWithoutDiscounts
- </c>
- <ct>
- @FormatMoney(totalWithoutDiscounts)
- </ct>
- }
- if (vatSum != 0m)
- {
- foreach (var vatWithSum in vatSumsByVat)
- {
- <c colspan="3">
- @string.Format(Resources.VatFormat, vatWithSum.Vat)
- </c>
- <ct>
- @string.Format(FormatMoney(vatWithSum.Sum))
- </ct>
- }
- if (vatSumsByVat.Count > 1)
- {
- <c colspan="3">
- @Resources.VatSum
- </c>
- <ct>
- @FormatMoney(vatSum)
- </ct>
- }
- }
- if (Model.AdditionalServiceChequeInfo != null)
- {
- <c colspan="3">
- @Resources.AdditionalServiceAddedFooterTotalUpper
- </c>
- <ct>
- @FormatMoney(Model.AdditionalServiceChequeInfo.AddedOrderItems
- .SelectMany(item => item.ExpandIncludedEntries())
- .Sum(orderEntry => orderEntry.GetCost()) + vatSum)
- </ct>
- }
- if (prepay != 0m)
- {
- <c colspan="3">
- @Resources.Prepay
- </c>
- <ct>
- @FormatMoney(prepay)
- </ct>
- }
- if (Model.AdditionalServiceChequeInfo != null &&
- order.ClientBinding != null &&
- order.ClientBinding.PaymentLimit.HasValue)
- {
- <c colspan="3">
- @Resources.AdditionalServiceLimit
- </c>
- <ct>
- @FormatMoney(order.ClientBinding.PaymentLimit.Value - total)
- </ct>
- }
- if (Model.DiscountMarketingCampaigns != null)
- {
- foreach (var discountMarketingCampaign in Model.DiscountMarketingCampaigns.Campaigns.Where(campaign => !string.IsNullOrWhiteSpace(campaign.BillComment)))
- {
- <c colspan="4">
- @discountMarketingCampaign.BillComment
- </c>
- }
- }
- }
- @helper PrintOrderDiscounts(IEnumerable<IDiscountItem> discountItems, decimal fullSum)
- {
- foreach (var discountItem in discountItems.OrderByDescending(discountItem => discountItem.IsDiscount()))
- {
- <c colspan="3">
- @((!discountItem.IsCategorized || discountItem.PrintDetailedInPrecheque) && !discountItem.Type.DiscountBySum
- ? GetFormattedDiscountDescriptionDetailed(discountItem.IsDiscount(), discountItem.Type.PrintableName, Math.Abs(CalculatePercent(fullSum, discountItem.GetDiscountSum())))
- : GetFormattedDiscountDescriptionShort(discountItem.IsDiscount(), discountItem.Type.PrintableName))
- </c>
- <ct>
- @GetFormattedDiscountSum(discountItem.IsDiscount(), Math.Abs(discountItem.GetDiscountSum()))
- </ct>
- }
- }
- @functions
- {
- private static bool OrderItemsToPrintFilter(IOrderItem orderItem, IEnumerable<IDiscountItem> discountItems)
- {
- return !(orderItem is IProductItem) || discountItems
- .Where(discountItem => discountItem.DiscountSums.ContainsKey(orderItem))
- .All(discountItem => discountItem.Type.PrintProductItemInPrecheque);
- }
- private bool AreOrderItemsEqual(IOrderItem x, IOrderItem y)
- {
- if (ReferenceEquals(x, y))
- return true;
- if (x == null)
- return y == null;
- if (y == null)
- return false;
- var xProductItem = x as IProductItem;
- var yProductItem = y as IProductItem;
- if (xProductItem == null || yProductItem == null || !ProductItemCanBeMerged(xProductItem) || !ProductItemCanBeMerged(yProductItem))
- return false;
- if (xProductItem.Product.Name != yProductItem.Product.Name)
- return false;
- if (xProductItem.Price != yProductItem.Price)
- return false;
- if (xProductItem.Price == 0m)
- return true;
- var categorizedDiscounts = Model.Order.DiscountItems
- .Where(discountItem => discountItem.IsCategorized &&
- discountItem.PrintDetailedInPrecheque &&
- discountItem.DiscountSums.Count > 0)
- .ToList();
- var xCategorizedDiscountItems = categorizedDiscounts
- .Where(discountItem => discountItem.DiscountSums.ContainsKey(x));
- var yCategorizedDiscountItems = categorizedDiscounts
- .Where(discountItem => discountItem.DiscountSums.ContainsKey(y));
- return new HashSet<IDiscountItem>(xCategorizedDiscountItems).SetEquals(yCategorizedDiscountItems);
- }
- private bool ProductItemCanBeMerged(IProductItem productItem)
- {
- return
- productItem.Amount - Math.Truncate(productItem.Amount) == 0m &&
- productItem.GetNotDeletedChildren().Where(orderEntry => OrderItemChildrenFilter(orderEntry, productItem)).IsEmpty() &&
- (productItem.Comment == null || productItem.Comment.Deleted || !Model.Order.Table.Section.PrintProductItemCommentInCheque);
- }
- private static bool OrderItemChildrenFilter(IOrderEntry orderEntry, IOrderItem parent)
- {
- if (orderEntry.Price > 0m)
- return true;
- if (!orderEntry.Product.PrechequePrintable)
- return false;
- var modifierEntry = orderEntry as IModifierEntry;
- if (modifierEntry == null)
- return true;
- if (modifierEntry.ChildModifier == null || !modifierEntry.ChildModifier.HideIfDefaultAmount)
- return true;
- var amountPerItem = modifierEntry.ChildModifier.AmountIndependentOfParentAmount
- ? modifierEntry.Amount
- : modifierEntry.Amount / parent.Amount;
- return amountPerItem != modifierEntry.ChildModifier.DefaultAmount;
- }
- private static string GetFormattedDiscountDescriptionForOrderItem(bool isDiscount, string discountName, bool discountBySum, decimal absolutePercent)
- {
- return discountBySum
- ? string.Format(" {0}", discountName)
- : string.Format(isDiscount ? " {0} (-{1})" : " {0} (+{1})", discountName, FormatPercent(absolutePercent));
- }
- private static string GetFormattedDiscountDescriptionShort(bool isDiscount, string discountName)
- {
- return string.Format(isDiscount ? Resources.BillFooterDiscountNamePatternShort : Resources.BillFooterIncreaseNamePatternShort,
- discountName);
- }
- private static string GetFormattedDiscountDescriptionDetailed(bool isDiscount, string discountName, decimal absolutePercent)
- {
- return string.Format(isDiscount ? Resources.BillFooterDiscountNamePatternDetailed : Resources.BillFooterIncreaseNamePatternDetailed,
- discountName, FormatPercent(absolutePercent));
- }
- private static string GetFormattedDiscountSum(bool isDiscount, decimal absoluteSum)
- {
- return (isDiscount ? "-" : "+") + FormatMoney(absoluteSum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement