private static InvoiceFormattedData CreateDataAfterLinesUpdated(Invoice invoice, InvoiceFormattedData invoiceOldData) { var data = invoiceOldData; var cultureInfo = string.IsNullOrEmpty(invoice.Language) ? CultureInfo.CurrentCulture : CultureInfo.GetCultureInfo(invoice.Language); #region TableBodyRows, TableFooterRows, TableTotalRow data.TableBodyRows = new List<List<string>>(); var invoiceLines = invoice.GetInvoiceLines(); var invoiceTaxes = new Dictionary<int, decimal>(); decimal subtotal = 0; decimal discount = 0; decimal amount = 0; foreach (var line in invoiceLines) { var item = Global.DaoFactory.GetInvoiceItemDao().GetByID(line.InvoiceItemID); var tax1 = line.InvoiceTax1ID > 0 ? Global.DaoFactory.GetInvoiceTaxDao().GetByID(line.InvoiceTax1ID) : null; var tax2 = line.InvoiceTax2ID > 0 ? Global.DaoFactory.GetInvoiceTaxDao().GetByID(line.InvoiceTax2ID) : null; var subtotalValue = Math.Round(line.Quantity * line.Price, 2); var discountValue = Math.Round(subtotalValue * line.Discount / 100, 2); var rate = 0; if (tax1 != null) { rate += tax1.Rate; if (invoiceTaxes.ContainsKey(tax1.ID)) { invoiceTaxes[tax1.ID] = invoiceTaxes[tax1.ID] + Math.Round((subtotalValue - discountValue) * tax1.Rate / 100, 2); } else { invoiceTaxes.Add(tax1.ID, Math.Round((subtotalValue - discountValue) * tax1.Rate / 100, 2)); } } if (tax2 != null) { rate += tax2.Rate; if (invoiceTaxes.ContainsKey(tax2.ID)) { invoiceTaxes[tax2.ID] = invoiceTaxes[tax2.ID] + Math.Round((subtotalValue - discountValue) * tax2.Rate / 100, 2); } else { invoiceTaxes.Add(tax2.ID, Math.Round((subtotalValue - discountValue) * tax2.Rate / 100, 2)); } } decimal taxValue = Math.Round((subtotalValue - discountValue) * rate / 100, 2); decimal amountValue = Math.Round(subtotalValue - discountValue + taxValue, 2); subtotal += subtotalValue; discount += discountValue; amount += amountValue; data.TableBodyRows.Add(new List<string> { item.Title + (string.IsNullOrEmpty(line.Description) ? string.Empty : ": " +line.Description), line.Quantity.ToString(CultureInfo.InvariantCulture), line.Price.ToString(CultureInfo.InvariantCulture), line.Discount.ToString(CultureInfo.InvariantCulture), tax1 != null ? tax1.Name : string.Empty, tax2 != null ? tax2.Name : string.Empty, (subtotalValue-discountValue).ToString(CultureInfo.InvariantCulture) }); } data.TableFooterRows = new List<Tuple<string, string>>(); data.TableFooterRows.Add(new Tuple<string, string>(CRMInvoiceResource.ResourceManager.GetString("Subtotal", cultureInfo), (subtotal - discount).ToString(CultureInfo.InvariantCulture))); foreach (var invoiceTax in invoiceTaxes) { var iTax = Global.DaoFactory.GetInvoiceTaxDao().GetByID(invoiceTax.Key); data.TableFooterRows.Add(new Tuple<string, string>(string.Format("{0} ({1}%)", iTax.Name, iTax.Rate), invoiceTax.Value.ToString(CultureInfo.InvariantCulture))); } //data.TableFooterRows.Add(new Tuple<string, string>(CRMInvoiceResource.ResourceManager.GetString("Discount", cultureInfo), "-" + discount.ToString(CultureInfo.InvariantCulture))); data.TableTotalRow = new Tuple<string, string>(string.Format("{0} ({1})", CRMInvoiceResource.ResourceManager.GetString("Total", cultureInfo), invoice.Currency), amount.ToString(CultureInfo.InvariantCulture)); #endregion return data; }
private static InvoiceFormattedData CreateData(Invoice invoice, int billingAddressID, int deliveryAddressID) { var data = new InvoiceFormattedData(); var sb = new StringBuilder(); var list = new List<string>(); var cultureInfo = string.IsNullOrEmpty(invoice.Language) ? CultureInfo.CurrentCulture : CultureInfo.GetCultureInfo(invoice.Language); #region TemplateType data.TemplateType = (int)invoice.TemplateType; #endregion #region Seller, LogoBase64, LogoSrcFormat var invoiceSettings = Global.DaoFactory.GetInvoiceDao().GetSettings(); if (!string.IsNullOrEmpty(invoiceSettings.CompanyName)) { sb.Append(invoiceSettings.CompanyName); } if (!string.IsNullOrEmpty(invoiceSettings.CompanyAddress)) { var obj = JObject.Parse(invoiceSettings.CompanyAddress); var str = obj.Value<string>("country"); if (!string.IsNullOrEmpty(str)) list.Add(str); str = obj.Value<string>("state"); if (!string.IsNullOrEmpty(str)) list.Add(str); str = obj.Value<string>("city"); if (!string.IsNullOrEmpty(str)) list.Add(str); str = obj.Value<string>("street"); if (!string.IsNullOrEmpty(str)) list.Add(str); str = obj.Value<string>("zip"); if (!string.IsNullOrEmpty(str)) list.Add(str); if (list.Count > 0) { sb.AppendLine(); sb.Append(string.Join(", ", list)); } } data.Seller = new Tuple<string, string>(CRMInvoiceResource.ResourceManager.GetString("Seller", cultureInfo), sb.ToString()); if (invoiceSettings.CompanyLogoID != 0) { data.LogoBase64Id = invoiceSettings.CompanyLogoID; //data.LogoBase64 = OrganisationLogoManager.GetOrganisationLogoBase64(invoiceSettings.CompanyLogoID); data.LogoSrcFormat = OrganisationLogoManager.OrganisationLogoSrcFormat; } #endregion #region Number data.Number = new Tuple<string, string>(CRMInvoiceResource.ResourceManager.GetString("Invoice", cultureInfo), invoice.Number); #endregion #region Invoice data.Invoice = new List<Tuple<string, string>>(); data.Invoice.Add(new Tuple<string, string>(CRMInvoiceResource.ResourceManager.GetString("IssueDate", cultureInfo), invoice.IssueDate.ToShortDateString())); if (!string.IsNullOrEmpty(invoice.PurchaseOrderNumber)) { data.Invoice.Add(new Tuple<string, string>(CRMInvoiceResource.ResourceManager.GetString("PONumber", cultureInfo), invoice.PurchaseOrderNumber)); } data.Invoice.Add(new Tuple<string, string>(CRMInvoiceResource.ResourceManager.GetString("DueDate", cultureInfo), invoice.DueDate.ToShortDateString())); #endregion #region Customer var customer = Global.DaoFactory.GetContactDao().GetByID(invoice.ContactID); if (customer != null) { sb = new StringBuilder(); sb.Append(customer.GetTitle()); var billingAddress = billingAddressID != 0 ? Global.DaoFactory.GetContactInfoDao().GetByID(billingAddressID) : null; if (billingAddress != null && billingAddress.InfoType == ContactInfoType.Address && billingAddress.Category == (int)AddressCategory.Billing) { list = new List<string>(); var obj = JObject.Parse(billingAddress.Data); var str = obj.Value<string>("country"); if (!string.IsNullOrEmpty(str)) list.Add(str); str = obj.Value<string>("state"); if (!string.IsNullOrEmpty(str)) list.Add(str); str = obj.Value<string>("city"); if (!string.IsNullOrEmpty(str)) list.Add(str); str = obj.Value<string>("street"); if (!string.IsNullOrEmpty(str)) list.Add(str); str = obj.Value<string>("zip"); if (!string.IsNullOrEmpty(str)) list.Add(str); if (list.Count > 0) { sb.AppendLine(); sb.Append(string.Join(", ", list)); } } data.Customer = new Tuple<string, string>(CRMInvoiceResource.ResourceManager.GetString("BillTo", cultureInfo), sb.ToString()); } #endregion #region TableHeaderRow, TableBodyRows, TableFooterRows, TableTotalRow data.TableHeaderRow = new List<string> { CRMInvoiceResource.ResourceManager.GetString("ItemCol", cultureInfo), CRMInvoiceResource.ResourceManager.GetString("QuantityCol", cultureInfo), CRMInvoiceResource.ResourceManager.GetString("PriceCol", cultureInfo), CRMInvoiceResource.ResourceManager.GetString("DiscountCol", cultureInfo), CRMInvoiceResource.ResourceManager.GetString("TaxCol", cultureInfo), CRMInvoiceResource.ResourceManager.GetString("TaxCol", cultureInfo), CRMInvoiceResource.ResourceManager.GetString("AmountCol", cultureInfo) }; data.TableBodyRows = new List<List<string>>(); var invoiceLines = invoice.GetInvoiceLines(); var invoiceTaxes = new Dictionary<int, decimal>(); decimal subtotal = 0; decimal discount = 0; decimal amount = 0; foreach (var line in invoiceLines) { var item = Global.DaoFactory.GetInvoiceItemDao().GetByID(line.InvoiceItemID); var tax1 = line.InvoiceTax1ID > 0 ? Global.DaoFactory.GetInvoiceTaxDao().GetByID(line.InvoiceTax1ID) : null; var tax2 = line.InvoiceTax2ID > 0 ? Global.DaoFactory.GetInvoiceTaxDao().GetByID(line.InvoiceTax2ID) : null; var subtotalValue = Math.Round(line.Quantity * line.Price, 2); var discountValue = Math.Round(subtotalValue * line.Discount / 100, 2); var rate = 0; if (tax1 != null) { rate += tax1.Rate; if (invoiceTaxes.ContainsKey(tax1.ID)) { invoiceTaxes[tax1.ID] = invoiceTaxes[tax1.ID] + Math.Round((subtotalValue - discountValue) * tax1.Rate / 100, 2); } else { invoiceTaxes.Add(tax1.ID, Math.Round((subtotalValue - discountValue) * tax1.Rate / 100, 2)); } } if (tax2 != null) { rate += tax2.Rate; if (invoiceTaxes.ContainsKey(tax2.ID)) { invoiceTaxes[tax2.ID] = invoiceTaxes[tax2.ID] + Math.Round((subtotalValue - discountValue) * tax2.Rate / 100, 2); } else { invoiceTaxes.Add(tax2.ID, Math.Round((subtotalValue - discountValue) * tax2.Rate / 100, 2)); } } decimal taxValue = Math.Round((subtotalValue - discountValue) * rate / 100, 2); decimal amountValue = Math.Round(subtotalValue - discountValue + taxValue, 2); subtotal += subtotalValue; discount += discountValue; amount += amountValue; data.TableBodyRows.Add(new List<string> { item.Title + (string.IsNullOrEmpty(line.Description) ? string.Empty : ": " +line.Description), line.Quantity.ToString(CultureInfo.InvariantCulture), line.Price.ToString(CultureInfo.InvariantCulture), line.Discount.ToString(CultureInfo.InvariantCulture), tax1 != null ? tax1.Name : string.Empty, tax2 != null ? tax2.Name : string.Empty, (subtotalValue-discountValue).ToString(CultureInfo.InvariantCulture) }); } data.TableFooterRows = new List<Tuple<string, string>>(); data.TableFooterRows.Add(new Tuple<string, string>(CRMInvoiceResource.ResourceManager.GetString("Subtotal", cultureInfo), (subtotal - discount).ToString(CultureInfo.InvariantCulture))); foreach (var invoiceTax in invoiceTaxes) { var iTax = Global.DaoFactory.GetInvoiceTaxDao().GetByID(invoiceTax.Key); data.TableFooterRows.Add(new Tuple<string, string>(string.Format("{0} ({1}%)", iTax.Name, iTax.Rate), invoiceTax.Value.ToString(CultureInfo.InvariantCulture))); } //data.TableFooterRows.Add(new Tuple<string, string>(CRMInvoiceResource.ResourceManager.GetString("Discount", cultureInfo), "-" + discount.ToString(CultureInfo.InvariantCulture))); data.TableTotalRow = new Tuple<string, string>(string.Format("{0} ({1})", CRMInvoiceResource.ResourceManager.GetString("Total", cultureInfo), invoice.Currency), amount.ToString(CultureInfo.InvariantCulture)); #endregion #region Terms data.Terms = new Tuple<string, string>(CRMInvoiceResource.ResourceManager.GetString("Terms", cultureInfo), invoice.Terms); #endregion #region Notes if (!string.IsNullOrEmpty(invoice.Description)) { data.Notes = new Tuple<string, string>(CRMInvoiceResource.ResourceManager.GetString("ClientNotes", cultureInfo), invoice.Description); } #endregion #region Consignee var consignee = Global.DaoFactory.GetContactDao().GetByID(invoice.ConsigneeID); if (consignee != null) { sb = new StringBuilder(); sb.Append(consignee.GetTitle()); var deliveryAddress = deliveryAddressID != 0 ? Global.DaoFactory.GetContactInfoDao().GetByID(deliveryAddressID) : null; if (deliveryAddress != null && deliveryAddress.InfoType == ContactInfoType.Address && deliveryAddress.Category == (int)AddressCategory.Postal) { list = new List<string>(); var obj = JObject.Parse(deliveryAddress.Data); var str = obj.Value<string>("country"); if (!string.IsNullOrEmpty(str)) list.Add(str); str = obj.Value<string>("state"); if (!string.IsNullOrEmpty(str)) list.Add(str); str = obj.Value<string>("city"); if (!string.IsNullOrEmpty(str)) list.Add(str); str = obj.Value<string>("street"); if (!string.IsNullOrEmpty(str)) list.Add(str); str = obj.Value<string>("zip"); if (!string.IsNullOrEmpty(str)) list.Add(str); if (list.Count > 0) { sb.AppendLine(); sb.Append(string.Join(", ", list)); } } data.Consignee = new Tuple<string, string>(CRMInvoiceResource.ResourceManager.GetString("ShipTo", cultureInfo), sb.ToString()); } #endregion #region Addresses data.BillingAddressID = billingAddressID; data.DeliveryAddressID = deliveryAddressID; #endregion return data; }
private InvoiceWrapper ToInvoiceWrapper(Invoice invoice) { var result = new InvoiceWrapper(invoice); if (invoice.ContactID > 0) { result.Contact = ToContactBaseWithEmailWrapper(DaoFactory.GetContactDao().GetByID(invoice.ContactID)); } if (invoice.ConsigneeID > 0) { result.Consignee = ToContactBaseWithEmailWrapper(DaoFactory.GetContactDao().GetByID(invoice.ConsigneeID)); } if (invoice.EntityID > 0) { result.Entity = ToEntityWrapper(invoice.EntityType, invoice.EntityID); } result.Cost = invoice.GetInvoiceCost(); result.InvoiceLines = invoice.GetInvoiceLines().Select(ToInvoiceLineWrapper).ToList(); return result; }