private static string ReplaceDocumentVars(string document, Ticket ticket, int orderNo, string userName) { string result = document; if (string.IsNullOrEmpty(document)) return ""; result = FormatData(result, TagNames.TicketDate, () => ticket.Date.ToShortDateString()); result = FormatData(result, TagNames.TicketTime, () => ticket.Date.ToShortTimeString()); result = FormatData(result, TagNames.Date, () => DateTime.Now.ToShortDateString()); result = FormatData(result, TagNames.Time, () => DateTime.Now.ToShortTimeString()); result = FormatData(result, TagNames.TicketId, () => ticket.Id.ToString()); result = FormatData(result, TagNames.TicketNo, () => ticket.TicketNumber); result = FormatData(result, TagNames.OrderNo, orderNo.ToString); result = FormatData(result, TagNames.TicketTag, ticket.GetTagData); result = FormatDataIf(true, result, TagNames.Department, () => GetDepartmentName(ticket.DepartmentId)); const string ticketTagPattern = TagNames.TicketTag2 + "[^}]+}"; while (Regex.IsMatch(result, ticketTagPattern)) { var value = Regex.Match(result, ticketTagPattern).Groups[0].Value; var tags = ""; try { var tag = value.Trim('{', '}').Split(':')[1]; tags = tag.Split(',').Aggregate(tags, (current, t) => current + (!string.IsNullOrEmpty(ticket.GetTagValue(t.Trim())) ? (t + ": " + ticket.GetTagValue(t.Trim()) + "\r") : "")); result = FormatData(result.Trim('\r'), value, () => tags); } catch (Exception) { result = FormatData(result, value, () => ""); } } const string ticketTag2Pattern = TagNames.TicketTag3 + "[^}]+}"; while (Regex.IsMatch(result, ticketTag2Pattern)) { var value = Regex.Match(result, ticketTag2Pattern).Groups[0].Value; var tag = value.Trim('{', '}').Split(':')[1]; var tagValue = ticket.GetTagValue(tag); try { result = FormatData(result, value, () => tagValue); } catch (Exception) { result = FormatData(result, value, () => ""); } } var title = userName; result = FormatData(result, TagNames.LocationUser, () => title); result = FormatData(result, TagNames.UserName, () => userName); result = FormatData(result, TagNames.Note, () => ticket.Note); result = FormatData(result, TagNames.AccName, () => ticket.AccountName); if (ticket.AccountId > 0 && (result.Contains(TagNames.AccAddress) || result.Contains(TagNames.AccPhone))) { var account = Dao.SingleWithCache<Resource>(x => x.Id == ticket.AccountId); result = FormatData(result, TagNames.AccPhone, () => account.SearchString); } result = RemoveTag(result, TagNames.AccAddress); result = RemoveTag(result, TagNames.AccPhone); var payment = ticket.GetPaymentAmount(); var remaining = ticket.GetRemainingAmount(); var plainTotal = ticket.GetPlainSum(); var preTaxServices = ticket.GetPreTaxServicesTotal(); var taxAmount = ticket.CalculateTax(plainTotal, preTaxServices); //GetTaxTotal(ticket.Orders, plainTotal, ticket.GetDiscountTotal()); var servicesTotal = ticket.GetPostTaxServicesTotal(); //ticket.CalculateTax(plainTotal, preTaxServices); result = FormatDataIf(taxAmount > 0 || preTaxServices > 0 || servicesTotal > 0, result, TagNames.PlainTotal, () => plainTotal.ToString("#,#0.00")); result = FormatDataIf(preTaxServices > 0, result, TagNames.DiscountTotal, () => preTaxServices.ToString("#,#0.00")); result = FormatDataIf(taxAmount > 0, result, TagNames.TaxTotal, () => taxAmount.ToString("#,#0.00")); result = FormatDataIf(taxAmount > 0, result, TagNames.TaxDetails, () => GetTaxDetails(ticket.Orders, plainTotal, preTaxServices)); result = FormatDataIf(servicesTotal > 0, result, TagNames.CalculationDetails, () => GetServiceDetails(ticket)); result = FormatDataIf(payment > 0, result, TagNames.IfPaid, () => string.Format(Resources.RemainingAmountIfPaidValue_f, payment.ToString("#,#0.00"), remaining.ToString("#,#0.00"))); result = FormatDataIf(preTaxServices > 0, result, TagNames.IfDiscount, () => string.Format(Resources.DiscountTotalAndTicketTotalValue_f, (plainTotal).ToString("#,#0.00"), preTaxServices.ToString("#,#0.00"))); result = FormatDataIf(preTaxServices < 0, result, TagNames.IfFlatten, () => string.Format(Resources.IfNegativeDiscountValue_f, preTaxServices.ToString("#,#0.00"))); result = FormatData(result, TagNames.TicketTotal, () => ticket.GetSum().ToString("#,#0.00")); result = FormatData(result, TagNames.PaymentTotal, () => ticket.GetPaymentAmount().ToString("#,#0.00")); result = FormatData(result, TagNames.Balance, () => ticket.GetRemainingAmount().ToString("#,#0.00")); result = FormatData(result, TagNames.TotalText, () => HumanFriendlyInteger.CurrencyToWritten(ticket.GetSum())); result = FormatData(result, TagNames.Totaltext, () => HumanFriendlyInteger.CurrencyToWritten(ticket.GetSum(), true)); result = _settingReplacer.ReplaceSettingValue("{SETTING:([^}]+)}", result); return result; }