public String ApplyBudgetTemplate(Budget budget, Int32 budgetDocumentTemplateId) { var addressManager = new AddressManager(this); DocumentTemplate documentTemplate = new CompanyManager(this).GetDocumentTemplate(budgetDocumentTemplateId); string template = documentTemplate == null ? GetTempBudgetTemplate() : documentTemplate.Content; bool isRtf = documentTemplate.FileName.EndsWith("rtf", StringComparison.OrdinalIgnoreCase); var stringBuilder = new StringBuilder(template); var tempStringBuilder = new StringBuilder(); if (budget.BudgetItems == null) budget = GetBudget(budget.BudgetId, budget.CompanyId); stringBuilder.Replace("[NumeroDoOrcamento]", budget.BudgetCode); stringBuilder.Replace("[DataEmissao]", budget.ModifiedDate.ToShortDateString() + (isRtf ? "<br />".ToRtf() : "<br />")); //header with informations of current date string header = budget.Company.LegalEntityProfile.Address.State + " , " + DateTime.Now.ToLongDateString().Split(',').ElementAt(1); stringBuilder.Replace("[Cabecalho]", header); #region CompanyInformations stringBuilder.Replace("[NomeDaEmpresa]", budget.Company.LegalEntityProfile.CompanyName); stringBuilder.Replace("[TelefoneDaEmpresa]", budget.Company.LegalEntityProfile.Phone); stringBuilder.Replace("[Endereco-Empresa]", budget.Company.LegalEntityProfile.Address.Name.ToCapitalize()); stringBuilder.Replace("[Endereco-Complemento-Empresa]", String.Empty); if (!String.IsNullOrEmpty(budget.Company.LegalEntityProfile.AddressComp)) stringBuilder.Replace("[Endereco-Complemento-Empresa]", budget.Company.LegalEntityProfile.AddressComp.ToCapitalize()); stringBuilder.Replace("[Endereco-Numero-Empresa]", budget.Company.LegalEntityProfile.AddressNumber); stringBuilder.Replace("[Endereco-Cep-Empresa]", budget.Company.LegalEntityProfile.PostalCode); stringBuilder.Replace("[Endereco-Cidade-Empresa]", budget.Company.LegalEntityProfile.Address.City.ToCapitalize()); stringBuilder.Replace("[Endereco-Estado-Empresa]", addressManager.GetAcronymState(budget.Company.LegalEntityProfile.Address.State)); stringBuilder.Replace("[Endereco-Bairro-Empresa]", budget.Company.LegalEntityProfile.Address.Neighborhood.ToCapitalize()); #endregion //Customer if (budget.CustomerId.HasValue || !String.IsNullOrEmpty(budget.CustomerName)) { #region Customer Data stringBuilder.Replace("[NomeDoCliente]", String.IsNullOrEmpty(budget.CustomerName) ? budget.Customer.Name : budget.CustomerName); stringBuilder.Replace("[EmailDoClente]", budget.Customer != null ? budget.Customer.Email : budget.CustomerMail); stringBuilder.Replace("[TelefoneDoCliente]", String.IsNullOrEmpty(budget.CustomerPhone) ? budget.Customer.Phone : budget.CustomerPhone); #endregion if (budget.Customer != null) { #region Customer Address if (budget.Customer.Address != null) { stringBuilder.Replace("[EnderecoDoCliente]", String.Empty); if (!String.IsNullOrEmpty(budget.Customer.Address.Name)) stringBuilder.Replace("[EnderecoDoCliente]", budget.Customer.Address.Name.ToCapitalize()); stringBuilder.Replace("[Endereco-Complemento]", String.Empty); if (!String.IsNullOrEmpty(budget.Customer.AddressComp)) stringBuilder.Replace("[Endereco-Complemento]", budget.Customer.AddressComp.ToCapitalize()); stringBuilder.Replace("[Endereco-Bairro]", String.Empty); if (!String.IsNullOrEmpty(budget.Customer.Address.Neighborhood)) stringBuilder.Replace("[Endereco-Bairro]", budget.Customer.Address.Neighborhood.ToCapitalize()); stringBuilder.Replace("[Endereco-Cidade]", String.Empty); if (!String.IsNullOrEmpty(budget.Customer.Address.City)) stringBuilder.Replace("[Endereco-Cidade]", budget.Customer.Address.City.ToCapitalize()); stringBuilder.Replace("[Endereco-Numero]", String.Empty); if (!String.IsNullOrEmpty(budget.Customer.AddressNumber)) stringBuilder.Replace("[Endereco-Numero]", budget.Customer.AddressNumber); stringBuilder.Replace("[Endereco-Cep]", String.Empty); if (!String.IsNullOrEmpty(budget.Customer.Address.PostalCode)) stringBuilder.Replace("[Endereco-Cep]", budget.Customer.Address.PostalCode); stringBuilder.Replace("[Endereco-Estado]", addressManager.GetAcronymState(budget.Company.LegalEntityProfile.Address.State)); } #endregion string phone2 = "", phone3 = ""; if (budget.Customer.LegalEntityProfile != null) { phone2 = budget.Customer.LegalEntityProfile.Phone2; phone3 = budget.Customer.LegalEntityProfile.Phone3; } stringBuilder.Replace("[Telefone2]", phone2); stringBuilder.Replace("[Telefone3]", phone3); stringBuilder.Replace("[CNPJ]", budget.Customer.LegalEntityProfile != null ? "CNPJ: " + budget.Customer.LegalEntityProfile.CNPJ : String.Empty); stringBuilder.Replace("[CPF]", budget.Customer.Profile != null ? "CPF: " + budget.Customer.Profile.CPF : String.Empty); stringBuilder.Replace("[IE]", budget.Customer.LegalEntityProfile != null ? "IE: " + budget.Customer.LegalEntityProfile.IE : String.Empty); stringBuilder.Replace("[RG]", budget.Customer.Profile != null ? "RG: " + budget.Customer.Profile.RG : String.Empty); } else { stringBuilder.Replace("[EnderecoDoCliente]", String.Empty); stringBuilder.Replace("[Endereco-Complemento]", String.Empty); stringBuilder.Replace("[Endereco-Numero]", String.Empty); stringBuilder.Replace("[Endereco-Cep]", String.Empty); stringBuilder.Replace("[Endereco-Cidade]", String.Empty); stringBuilder.Replace("[Endereco-Estado]", String.Empty); stringBuilder.Replace("[Telefone2]", String.Empty); stringBuilder.Replace("[Telefone3]", String.Empty); } } #region BudgetItems tempStringBuilder = new StringBuilder(); //Header //Body Int32 itemCount = 0; Decimal totalValue = Decimal.Zero; foreach (BudgetItem item in GetBudgetItemByBudget(budget.BudgetId, budget.CompanyId)) { itemCount++; string itemName = item.SpecialProductName; tempStringBuilder.AppendFormat(@" <fieldset> <table> <tr> <td style='white-space:nowrap; width:1%;' width='1%'>Código:</td> <td class='code' colspan='2'>{0}</td> </tr> <tr> <td style='white-space:nowrap'>Qtd:</td> <td class='qtd' colspan='2'>{1}</td> </tr> <tr> <td style='white-space:nowrap'>Desc:</td> <td class='description' colspan='2'>{2}</td> </tr> <tr> <td style='white-space:nowrap'>Vlr Unit:</td> <td>{3}</td> <td style='text-align:right'>Valor: {4}</td> </tr> </table> </fieldset>", item.ProductCode + " | " + itemName, item.Quantity, item.ProductDescription, item.UnitPrice.Value.ToString("c"), (item.Quantity * item.UnitPrice.Value).ToString("c")); totalValue += (item.Quantity * item.UnitPrice.Value); } // Subtotal tempStringBuilder.AppendFormat(@"<table width='100%'><tr> <td style='white-space:nowrap'><b>Subtotal (R$): </b></td> <td style='white-space:nowrap; text-align:right'>{0}<td> </tr>", totalValue); // AdditionalCost if (budget.AdditionalCost.HasValue) { tempStringBuilder.AppendFormat(@"<tr> <td style='white-space:nowrap'><b>Adicional (R$): </b></td> <td style='white-space:nowrap; text-align:right'>{0}<td> </tr>", budget.AdditionalCost.Value.ToString("c")); totalValue += budget.AdditionalCost.Value; } // Discount if (budget.Discount.HasValue) { tempStringBuilder.AppendFormat(@"<tr> <td style='white-space:nowrap'><b>Desconto (R$): </b></td> <td style='white-space:nowrap; text-align:right'>{0}<td> </tr>", budget.Discount.Value.ToString("c")); totalValue -= budget.Discount.Value; } //Footer tempStringBuilder.AppendFormat(@"<tr> <td style='white-space:nowrap'><b>Valor Total da Proposta (R$): </b></td> <td style='white-space:nowrap; text-align:right'> <b>{0}<b></td> </tr> <tr> <td style='white-space:nowrap; text-align:center' colspan='2'><b>({1})<b></td> </tr> </table>", totalValue.ToString("c"), totalValue.AtFull().ToUpper()); stringBuilder.Replace("[Items]", isRtf ? tempStringBuilder.ToString().ToRtf() : tempStringBuilder.ToString()); stringBuilder.Replace("[TotalDaProposta]", totalValue.ToString()); stringBuilder.Replace("[ValorTotalPorExtenso]", totalValue.AtFull().ToUpper()); #endregion #region Others stringBuilder.Replace("[Contato]", budget.ContactName); stringBuilder.Replace("[DataEntrega]", budget.DeliveryDate); stringBuilder.Replace("[Garantia]", budget.Warranty); stringBuilder.Replace("[Validade]", budget.ExpirationDate.ToString()); stringBuilder.Replace("[FormaPagamento]", budget.PaymentMethod); stringBuilder.Replace("[Observacao]", budget.Observation); stringBuilder.Replace("[Vendedor]", budget.Employee.Profile.AbreviatedName); stringBuilder.Replace("[EmailVendedor]", budget.Employee.Profile.Email); stringBuilder.Replace("[FormaEntrega]", budget.DeliveryDescription); stringBuilder.Replace("[Pintura/Tratamento]", budget.Treatment); #endregion return stringBuilder.ToString(); }