Пример #1
0
        /// <summary>
        /// List of invoice items.
        /// This is used for free-form invoices.
        /// </summary>
        /// <returns>The list of invoices.</returns>
        public IList <InvoiceItem> ListLineItems()
        {
            if (_listLineItems == null)
            {
                _listLineItems = new List <InvoiceItem>();

                // Null check
                if (string.IsNullOrEmpty(CsvLineItems))
                {
                    return(_listLineItems);
                }

                // Get memory stream of data
                using (var stream =
                           new MemoryStream(System.Text.Encoding.UTF8.GetBytes(CsvLineItems)))
                    using (var streamReader = new StreamReader(stream))
                        using (var parser = new CsvReader(streamReader))
                        {
                            var headers   = new List <string>();
                            var lineindex = 0;

                            while (parser.Read())
                            {
                                var         fields      = GetFields(parser);
                                var         fieldindex  = 0;
                                InvoiceItem invocieitem = null;

                                foreach (var field in fields)
                                {
                                    // Header, or content?
                                    if (lineindex == 0)
                                    {
                                        headers.Add(field);
                                    }
                                    else
                                    {
                                        if (invocieitem == null)
                                        {
                                            invocieitem = new InvoiceItem();
                                        }

                                        // Parse by header name
                                        switch (headers[fieldindex])
                                        {
                                        case "kind":
                                            invocieitem.Kind = field;

                                            break;

                                        case "description":
                                            invocieitem.Description = field;

                                            break;

                                        case "quantity":
                                            invocieitem.Quantity = decimal.Parse(field,
                                                                                 System.Globalization.CultureInfo.InvariantCulture);

                                            break;

                                        case "unit_price":
                                            invocieitem.UnitPrice = decimal.Parse(field,
                                                                                  System.Globalization.CultureInfo.InvariantCulture);

                                            break;

                                        case "amount":
                                            invocieitem.Amount = decimal.Parse(field,
                                                                               System.Globalization.CultureInfo.InvariantCulture);

                                            break;

                                        case "taxed":
                                            invocieitem.Taxed = bool.Parse(field);

                                            break;

                                        case "taxed2":
                                            invocieitem.Taxed2 = bool.Parse(field);

                                            break;

                                        case "project_id":
                                            invocieitem.ProjectId = string.IsNullOrEmpty(field) ? 0 : long.Parse(field);

                                            break;
                                        }
                                    }

                                    fieldindex++;
                                }

                                lineindex++;

                                if (invocieitem != null)
                                {
                                    _listLineItems.Add(invocieitem);
                                }
                            }
                        }
            }

            return(_listLineItems);
        }
Пример #2
0
 /// <summary>
 /// Method to set invoice items of the invoice options.
 /// </summary>
 /// <param name="items">
 /// List of items which should be added to the invoice options.
 /// </param>
 public void SetInvoiceItems(IList <InvoiceItem> items)
 {
     CsvLineItems = InvoiceItem.GetHeaders() + "\n" + string.Join("\n", items.Select(ii => ii.ToString()));
 }