public InvoiceViewModel Convert(XEROPurchasesResult purchase)
        {
            InvoiceViewModel invoiceViewModel = new InvoiceViewModel();

            invoiceViewModel.Number = purchase.InvoiceNumber;
            invoiceViewModel.AccountCode = purchase.AccountCode.ToString();

            try
            {
                invoiceViewModel.Contact = getContact(purchase.ContactName);
            }
            catch (ArgumentNullException)
            {
                throw new InvoiceValidationException("The supplier data of the invoice is not correct.");
            }
            catch (SupplierDoesNotExistException)
            {
                throw;
            }

            invoiceViewModel.CurrencyCode = purchase.Currency;
            invoiceViewModel.Date = purchase.InvoiceDate.GetValueOrDefault();
            invoiceViewModel.Description = purchase.Description;
            invoiceViewModel.DueDate = purchase.DueDate.GetValueOrDefault();
            invoiceViewModel.ItemCode = purchase.InventoryItemCode.ToString();
            invoiceViewModel.Quantity = purchase.Quantity;
            invoiceViewModel.TaxType = purchase.TaxType;
            invoiceViewModel.UnitAmount = purchase.UnitAmount.GetValueOrDefault();
            invoiceViewModel.typeOfInvoice = "ACCPAY";
            invoiceViewModel.TrackingName = purchase.TrackingName1;
            invoiceViewModel.TrackingOption = purchase.TrackingOption1;

            return invoiceViewModel;
        }
        public InvoiceViewModel Convert(Invoice invoiceToConvert)
        {
            ContactToContactViewModelConverter contactConverter = new ContactToContactViewModelConverter();

            InvoiceViewModel invoiceViewModel = new InvoiceViewModel();
            invoiceViewModel.AccountCode = invoiceToConvert.Items.FirstOrDefault().AccountCode;
            invoiceViewModel.Description = invoiceToConvert.Items.FirstOrDefault().Description;
            invoiceViewModel.ItemCode = invoiceToConvert.Items.FirstOrDefault().ItemCode;
            invoiceViewModel.Quantity = invoiceToConvert.Items.FirstOrDefault().Quantity.GetValueOrDefault();
            if (invoiceToConvert.Items.FirstOrDefault().TaxType == "OUTPUT2")
            {
                invoiceViewModel.TaxType = "15% GST on Income";
            }
            else if (invoiceToConvert.Items.FirstOrDefault().TaxType == "NONE")
            {
                invoiceViewModel.TaxType = "No GST";
            }
            else if (invoiceToConvert.Items.FirstOrDefault().TaxType == "INPUT2")
            {
                invoiceViewModel.TaxType = "15% GST on Expenses";
            }
            else
            {
                invoiceViewModel.TaxType = "";
            }

            if (invoiceToConvert.Items.FirstOrDefault().Tracking.Count > 0)
            {
                invoiceViewModel.TrackingName = invoiceToConvert.Items.FirstOrDefault().Tracking.FirstOrDefault().Name;
                invoiceViewModel.TrackingOption = invoiceToConvert.Items.FirstOrDefault().Tracking.FirstOrDefault().Option;
            }

            invoiceViewModel.UnitAmount = invoiceToConvert.Items.FirstOrDefault().UnitAmount.GetValueOrDefault();
            invoiceViewModel.DiscountRate = invoiceToConvert.Items.FirstOrDefault().DiscountRate.GetValueOrDefault();
            invoiceViewModel.BrandingThemeId = invoiceToConvert.BrandingThemeId.ToString();
            invoiceViewModel.Contact = contactConverter.Convert(invoiceToConvert.Contact);
            invoiceViewModel.CurrencyCode = invoiceToConvert.CurrencyCode;
            invoiceViewModel.Date = invoiceToConvert.Date.GetValueOrDefault();
            invoiceViewModel.DueDate = invoiceToConvert.DueDate.GetValueOrDefault();
            invoiceViewModel.Number = invoiceToConvert.Number;
            invoiceViewModel.Reference = invoiceToConvert.Reference;

            return invoiceViewModel;
        }
        public InvoiceViewModel Convert(AccountSalesServiceResult invoice)
        {
            InvoiceViewModel invoiceViewModel = new InvoiceViewModel();

            invoiceViewModel.AccountCode = invoice.AccountCode.ToString();
            invoiceViewModel.BrandingThemeId = getBrandingThemeId(invoice.TrackingOption1);

            try
            {
                invoiceViewModel.Contact = getContact(invoice.ContactName);
            }
            catch (ArgumentNullException)
            {
                throw new InvoiceValidationException("The customer data of invoice with invoice number "+ invoice.InvoiceNumber +" is not correct.");
            }
            catch(CustomerDoesNotExistException)
            {
                throw;
            }

            invoiceViewModel.CurrencyCode = "NZD";
            invoiceViewModel.Date = invoice.InvoiceDate.GetValueOrDefault();
            invoiceViewModel.Description = invoice.Description;
            invoiceViewModel.DiscountRate = invoice.Discount;
            invoiceViewModel.DueDate = invoice.DueDate.GetValueOrDefault();
            invoiceViewModel.ItemCode = invoice.InventoryItemCode.ToString();
            invoiceViewModel.Number = invoice.InvoiceNumber;
            invoiceViewModel.Quantity = invoice.Quantity.GetValueOrDefault();
            invoiceViewModel.Reference = invoice.Reference;
            invoiceViewModel.TaxType = invoice.TaxType;
            invoiceViewModel.UnitAmount = invoice.UnitAmount.GetValueOrDefault();
            invoiceViewModel.typeOfInvoice = "ACCREC";
            invoiceViewModel.TrackingName = invoice.TrackingName1;
            invoiceViewModel.TrackingOption = invoice.TrackingOption1;

            return invoiceViewModel;
        }
        public InvoiceViewModel Convert(XEROPaymentsResult payment)
        {
            InvoiceViewModel invoiceViewModel = new InvoiceViewModel();

            invoiceViewModel.Number = payment.InvoiceNumber;
            invoiceViewModel.AccountCode = payment.AccountCode.ToString();

            try
            {
                invoiceViewModel.Contact = getContact(payment.ContactName);
            }
            catch (ArgumentNullException)
            {
                throw new InvoiceValidationException("The customer data of the invoice is not correct.");
            }
            catch (CustomerDoesNotExistException)
            {
                throw;
            }

            invoiceViewModel.Date = payment.InvoiceDate.GetValueOrDefault();
            invoiceViewModel.Description = payment.Description;
            invoiceViewModel.DueDate = payment.DueDate.GetValueOrDefault();
            invoiceViewModel.ItemCode = payment.InventoryItemCode.ToString();
            invoiceViewModel.Quantity = payment.Quantity;
            invoiceViewModel.TaxType = payment.TaxType;
            invoiceViewModel.UnitAmount = payment.UnitAmount.GetValueOrDefault();
            invoiceViewModel.typeOfInvoice = "ACCPAY";
            invoiceViewModel.TrackingName = payment.TrackingName1;
            invoiceViewModel.TrackingOption = payment.TrackingOption1;
            invoiceViewModel.BrandingThemeId = getBrandingThemeId(payment.BrandingTheme);
            invoiceViewModel.DiscountRate = payment.Discount;
            invoiceViewModel.Reference = payment.Reference;

            return invoiceViewModel;
        }
        public Invoice convert(InvoiceViewModel invoiceViewModel)
        {
            ContactViewModelToContactConverter contactConverter = new ContactViewModelToContactConverter();
            Invoice invoice = new Invoice();
            if (!(string.IsNullOrEmpty(invoiceViewModel.BrandingThemeId)))
            {
                try
                {
                    invoice.BrandingThemeId = Guid.Parse(invoiceViewModel.BrandingThemeId);
                }
                catch (ArgumentNullException)
                {
                    throw new BrandingThemeIsNullException("Branding theme can not be empty");
                }
                catch (FormatException)
                {
                    throw new BrandingThemeIdIsNotVallid("Branding theme id is not a vallid branding theme id");
                }
            }
            invoice.Contact = contactConverter.Convert(invoiceViewModel.Contact);

            if(!(string.IsNullOrEmpty(invoiceViewModel.CurrencyCode)))
            {
                invoice.CurrencyCode = invoiceViewModel.CurrencyCode;
            }
            invoice.Date = invoiceViewModel.Date;
            invoice.DueDate = invoiceViewModel.DueDate;

            List<LineItem> lineItemList = new List<LineItem>();
            ItemTrackingCategory itemTrackingCategory = new ItemTrackingCategory();
            itemTrackingCategory.Name = invoiceViewModel.TrackingName;
            itemTrackingCategory.Option = invoiceViewModel.TrackingOption;

            ItemTracking itemTraking = new ItemTracking()
            {
               itemTrackingCategory
            };

            string taxType = null;

            if (invoiceViewModel.TaxType == "15% GST on Income")
            {
                taxType = "OUTPUT2";
            }

            if (invoiceViewModel.TaxType == "No GST")
            {
                taxType = "NONE";
            }

            if (invoiceViewModel.TaxType == "15% GST on Expenses")
            {
                taxType = "INPUT2";
            }

            LineItem lineItem = new LineItem();

            lineItem.Description = invoiceViewModel.Description;
            if (invoiceViewModel.DiscountRate > 0)
            {
                lineItem.DiscountRate = invoiceViewModel.DiscountRate;
            }
            //lineItem.ItemCode = invoiceViewModel.ItemCode;
            lineItem.ItemCode = "GB3-White";
            lineItem.Quantity = invoiceViewModel.Quantity;
            lineItem.TaxType = taxType;
            lineItem.Tracking = itemTraking;
            lineItem.UnitAmount = invoiceViewModel.UnitAmount;
            lineItem.AccountCode = invoiceViewModel.AccountCode;

            lineItemList.Add(lineItem);
            invoice.Items = lineItemList;
            invoice.Number = invoiceViewModel.Number;
            invoice.Reference = invoiceViewModel.Reference;
            invoice.Status = InvoiceStatus.Draft;
            if (invoiceViewModel.typeOfInvoice == "ACCPAY")
            {
                invoice.Type = InvoiceType.AccountsPayable;
            }

            if (invoiceViewModel.typeOfInvoice == "ACCREC")
            {
                invoice.Type = InvoiceType.AccountsReceivable;
            }

            if (invoiceViewModel.typeOfInvoice == "ACCREC")
            {
                invoice.Type = InvoiceType.AccountsReceivable;
                CreditNote cr = new CreditNote();

            }

            return invoice;
        }
 private InvoiceViewModel giveInvoice()
 {
     Random randomGenerator = new Random();
     int randomNumber = randomGenerator.Next(1, 99999);
     int randomNumber2 = randomGenerator.Next(1, 99999);
     InvoiceViewModel invoiceViewModel = new InvoiceViewModel();
     ContactViewModel contactViewModel = new ContactViewModel();
     invoiceViewModel.typeOfInvoice = "ACCREC";
     contactViewModel.name = "test";
     invoiceViewModel.Contact = contactViewModel;
     invoiceViewModel.Description = "test";
     invoiceViewModel.BrandingThemeId = "7889a0ac-262a-40e3-8a63-9a769b1a18af";
     invoiceViewModel.Number = randomNumber.ToString();
     invoiceViewModel.Date = DateTime.Now;
     invoiceViewModel.DueDate = DateTime.Now;
     invoiceViewModel.AccountCode = "200";
     invoiceViewModel.CurrencyCode = "NZD";
     invoiceViewModel.DiscountRate = 0;
     invoiceViewModel.ItemCode = "GB3-White";
     invoiceViewModel.Quantity = 2;
     invoiceViewModel.Reference = "test";
     invoiceViewModel.TaxType = "15% GST on Income";
     invoiceViewModel.TrackingName = "Region";
     invoiceViewModel.TrackingOption = "HUT";
     invoiceViewModel.UnitAmount = 2;
     return invoiceViewModel;
 }