示例#1
0
        public static void CalculateInvoiceChargeAmounts(Taxation tax, List <InvoiceItem> items,
                                                         out decimal totalAmount, out decimal subTotalAmount, out decimal taxAmount)
        {
            totalAmount = subTotalAmount = taxAmount = 0;
            // calculate all invoice items
            foreach (InvoiceItem item in items)
            {
                subTotalAmount += item.SubTotal;
            }
            //
            totalAmount = subTotalAmount;
            // tax applies
            if (tax != null)
            {
                switch (tax.Type)
                {
                case TaxationType.Fixed:
                    taxAmount    = tax.Amount;
                    totalAmount += taxAmount;
                    break;

                case TaxationType.Percentage:
                    taxAmount    = (subTotalAmount / 100) * tax.Amount;
                    totalAmount += taxAmount;
                    break;

                case TaxationType.TaxIncluded:
                    taxAmount       = totalAmount - totalAmount * 100 / (100M + tax.Amount);
                    subTotalAmount -= taxAmount;
                    break;
                }
            }
        }
示例#2
0
        public static int AddInvoice(string contractId, List <InvoiceItem> invoiceLines, KeyValueBunch extraArgs)
        {
            ContractAccount account = ContractSystem.ContractController.GetContractAccountSettings(contractId);
            // read customer tax
            Taxation tax        = GetCustomerTaxation(contractId, account[ContractAccount.COUNTRY], account[ContractAccount.STATE]);
            int      taxationId = (tax == null) ? -1 : tax.TaxationId;

            // Calculate invoice amounts
            decimal totalAmount = 0, subTotalAmount = 0, taxAmount = 0;

            CalculateInvoiceChargeAmounts(tax, invoiceLines, out totalAmount, out subTotalAmount, out taxAmount);

            // align svcs suspend date
            int[] svcs = new int[invoiceLines.Count];
            for (int i = 0; i < invoiceLines.Count; i++)
            {
                svcs[i] = invoiceLines[i].ServiceId;
            }
            DateTime sdateAligned = ServiceController.GetSvcsSuspendDateAligned(svcs, DateTime.Now);
            //
            StoreSettings settings = StorehouseController.GetStoreSettings(ES.SecurityContext.User.UserId,
                                                                           StoreSettings.SYSTEM_SETTINGS);
            // get invoice grace period in days
            int gracePeriod = Common.Utils.Utils.ParseInt(settings["InvoiceGracePeriod"], 0);

            //
            if (gracePeriod < 0)
            {
                gracePeriod = 0;
            }
            //
            DateTime created = DateTime.Now;
            DateTime dueDate = sdateAligned.AddDays(gracePeriod);

            //
            return(AddInvoice(contractId, created, dueDate, taxationId, totalAmount, subTotalAmount, taxAmount,
                              invoiceLines, extraArgs));
        }
示例#3
0
        internal static string GetCustomerInvoiceFormattedInternally(Contract contract, int invoiceId, ContractAccount accountSettings, string cultureName)
        {
            //
            Invoice invoiceInfo = GetCustomerInvoiceInternally(invoiceId);

            // impersonate
            ES.SecurityContext.SetThreadPrincipal(contract.ResellerId);
            // load settings
            StoreSettings settings = StorehouseController.GetStoreSettings(contract.ResellerId, StoreSettings.NEW_INVOICE);
            //
            string templateBody = settings["HtmlBody"];
            //
            Taxation invoiceTax = StorehouseController.GetTaxation(contract.ResellerId, invoiceInfo.TaxationId);
            //
            List <InvoiceItem>        invoiceLines    = GetCustomerInvoiceItems(invoiceId);
            Dictionary <int, Service> invoiceServices = ServiceController.GetServicesDictionary(invoiceLines);
            //
            Template tm = new Template(templateBody);

            tm["Invoice"]         = invoiceInfo;
            tm["InvoiceLines"]    = invoiceLines;
            tm["InvoiceServices"] = invoiceServices;
            tm["Customer"]        = accountSettings;
            tm["Tax"]             = invoiceTax;

            StringWriter writer = new StringWriter();

            try
            {
                // Preserve an original thread culture
                CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
                //
                if (!String.IsNullOrEmpty(cultureName))
                {
                    try
                    {
                        CultureInfo formattingCulture = new CultureInfo(cultureName);
                        // Empty currency symbol to supporty 3-letters ISO format
                        // hardcorded in HTML templates
                        formattingCulture.NumberFormat.CurrencySymbol = String.Empty;
                        // Set formatting culture
                        Thread.CurrentThread.CurrentCulture = formattingCulture;
                    }
                    catch (Exception ex)
                    {
                        TaskManager.WriteWarning("Wrong culture name has been provided. Culture name: {0}.", cultureName);
                        TaskManager.WriteWarning(ex.StackTrace);
                        TaskManager.WriteWarning(ex.Message);
                    }
                }

                // Process template
                tm.Evaluate(writer);
                templateBody = writer.ToString();

                // Revert the original thread's culture back
                Thread.CurrentThread.CurrentCulture = originalCulture;
            }
            catch (ParserException ex)
            {
                return(String.Format("Error in template (Line {0}, Column {1}): {2}",
                                     ex.Line, ex.Column, ex.Message));
            }

            return(templateBody);
        }
 public static void CalculateInvoiceChargeAmounts(Taxation tax, List<InvoiceItem> items,
     out decimal totalAmount, out decimal subTotalAmount, out decimal taxAmount)
 {
     totalAmount = subTotalAmount = taxAmount = 0;
     // calculate all invoice items
     foreach (InvoiceItem item in items)
         subTotalAmount += item.SubTotal;
     //
     totalAmount = subTotalAmount;
     // tax applies
     if (tax != null)
     {
         switch (tax.Type)
         {
             case TaxationType.Fixed:
                 taxAmount = tax.Amount;
                 totalAmount += taxAmount;
                 break;
             case TaxationType.Percentage:
                 taxAmount = (subTotalAmount / 100) * tax.Amount;
                 totalAmount += taxAmount;
                 break;
             case TaxationType.TaxIncluded:
                 taxAmount = totalAmount - totalAmount * 100 / (100M + tax.Amount);
                 subTotalAmount -= taxAmount;
                 break;
         }
     }
 }