private void BindInvoice()
        {
            if ((_invoice != null))
            {
                var serviceInvoiceSvc = new ServiceInvoiceService(MyConfiguration,
                                                                  null,
                                                                  MyOAuthKeyService);
                ServiceInvoice serviceInvoice = serviceInvoiceSvc.Get(MyCompanyFile, _invoice.UID, MyCredentials);

                //Set the default value
                var customers =
                    CmboCustomer.DataSource as PagedCollection <Customer>;

                if (customers != null)
                {
                    for (int i = 0; i <= customers.Count; i++)
                    {
                        Customer customer = customers.Items[i];
                        if (customer.UID == serviceInvoice.Customer.UID)
                        {
                            CmboCustomer.SelectedIndex = i;
                            break;
                        }
                    }
                }
                BsServiceInvoice.DataSource = serviceInvoice;
                GrdServiceLines.DataSource  = FlattenLines(serviceInvoice.Lines);
            }
        }
        public void FetchData(Action<Exception> onError)
        {
            IsLoading = true;
            var service = new AccountService(new ApiConfiguration(), null, _keyService);
            var cService = new CustomerService(new ApiConfiguration(), null, _keyService);
            var iService = new ServiceInvoiceService(new ApiConfiguration(), null, _keyService);

            Task.WhenAll(new[]
                {
                    service.GetRangeAsync(CompanyFile.CompanyFile, null, CompanyFile.Authentication[0])
                        .ContinueWith(t => DisplayAccounts(t.Result.Items), TaskScheduler.FromCurrentSynchronizationContext()),
                    cService.GetRangeAsync(CompanyFile.CompanyFile, null, CompanyFile.Authentication[0])
                        .ContinueWith(t => DisplayCustomers(t.Result.Items), TaskScheduler.FromCurrentSynchronizationContext()),
                    iService.GetRangeAsync(CompanyFile.CompanyFile, null, CompanyFile.Authentication[0])
                        .ContinueWith(t => DisplayInvoices(t.Result.Items), TaskScheduler.FromCurrentSynchronizationContext())
                })
                .ContinueWith(t =>
                    {
                        IsLoading = false;
                    }, TaskScheduler.FromCurrentSynchronizationContext());
        }
        private void BindInvoice()
        {
            if ((_invoice != null))
            {
                var serviceInvoiceSvc = new ServiceInvoiceService(MyConfiguration,
                                                                  null,
                                                                  MyOAuthKeyService);
                ServiceInvoice serviceInvoice = serviceInvoiceSvc.Get(MyCompanyFile, _invoice.UID, MyCredentials);

                //Set the default value
                var customers =
                    CmboCustomer.DataSource as PagedCollection<Customer>;

                if (customers != null)
                {
                    for (int i = 0; i <= customers.Count; i++)
                    {
                        Customer customer = customers.Items[i];
                        if (customer.UID == serviceInvoice.Customer.UID)
                        {
                            CmboCustomer.SelectedIndex = i;
                            break;
                        }
                    }
                }
                BsServiceInvoice.DataSource = serviceInvoice;
                GrdServiceLines.DataSource = FlattenLines(serviceInvoice.Lines);
            }
        }
        private void BtnRecordClick(Object sender, EventArgs e)
        {
            var serviceInvoiceSvc = new ServiceInvoiceService(MyConfiguration, null,
                                                              MyOAuthKeyService);
            var serviceInvoice = new ServiceInvoice();

            if ((_invoice == null))
            {
                var customerLnk = new CustomerLink {UID = (Guid) CmboCustomer.SelectedValue};
                serviceInvoice.Customer = customerLnk;
                serviceInvoice.ShipToAddress = TxtAddress.Text;
                serviceInvoice.Number = TxtInvoiceNo.Text;
                serviceInvoice.Date = DtDate.Value;
                serviceInvoice.IsTaxInclusive = ChkTaxInclusive.Checked;

                var lines = new List<ServiceInvoiceLine>();

                foreach (DataGridViewRow row in GrdServiceLines.Rows)
                {
                    if (!row.IsNewRow)
                    {
                        var line = new ServiceInvoiceLine
                                       {
                                           Type = InvoiceLineType.Transaction,
                                           Description = (string) row.Cells["ColDescription"].Value,
                                           Total = Convert.ToDecimal(row.Cells["ColAmount"].Value)
                                       };

                        if ((row.Cells["ColAccount"].Value == null))
                        {
                            MessageBox.Show("you must select an account on each row");
                            return;
                        }
                        var accountlnk = new AccountLink {UID = (Guid) row.Cells["ColAccount"].Value};
                        line.Account = accountlnk;

                        if ((row.Cells["ColTax"].Value == null))
                        {
                            MessageBox.Show("you must select a taxcode on each row");
                            return;
                        }
                        var taxcodelnk = new TaxCodeLink {UID = (Guid) row.Cells["ColTax"].Value};
                        line.TaxCode = taxcodelnk;

                        if ((row.Cells["ColJob"].Value != null))
                        {
                            var joblnk = new JobLink {UID = (Guid) row.Cells["ColJob"].Value};
                            line.Job = joblnk;
                        }

                        lines.Add(line);
                    }
                }

                serviceInvoice.Lines = lines;
                serviceInvoiceSvc.Insert(MyCompanyFile, serviceInvoice, MyCredentials, OnSaveComplete, OnError);
                ShowSpinner();
            }
        }
        private void BtnRecordClick(Object sender, EventArgs e)
        {
            var serviceInvoiceSvc = new ServiceInvoiceService(MyConfiguration, null,
                                                              MyOAuthKeyService);
            var serviceInvoice = new ServiceInvoice();

            if ((_invoice == null))
            {
                var customerLnk = new CustomerLink {
                    UID = (Guid)CmboCustomer.SelectedValue
                };
                serviceInvoice.Customer       = customerLnk;
                serviceInvoice.ShipToAddress  = TxtAddress.Text;
                serviceInvoice.Number         = TxtInvoiceNo.Text;
                serviceInvoice.Date           = DtDate.Value;
                serviceInvoice.IsTaxInclusive = ChkTaxInclusive.Checked;

                var lines = new List <ServiceInvoiceLine>();

                foreach (DataGridViewRow row in GrdServiceLines.Rows)
                {
                    if (!row.IsNewRow)
                    {
                        var line = new ServiceInvoiceLine
                        {
                            Type        = InvoiceLineType.Transaction,
                            Description = (string)row.Cells["ColDescription"].Value,
                            Total       = Convert.ToDecimal(row.Cells["ColAmount"].Value)
                        };

                        if ((row.Cells["ColAccount"].Value == null))
                        {
                            MessageBox.Show("you must select an account on each row");
                            return;
                        }
                        var accountlnk = new AccountLink {
                            UID = (Guid)row.Cells["ColAccount"].Value
                        };
                        line.Account = accountlnk;

                        if ((row.Cells["ColTax"].Value == null))
                        {
                            MessageBox.Show("you must select a taxcode on each row");
                            return;
                        }
                        var taxcodelnk = new TaxCodeLink {
                            UID = (Guid)row.Cells["ColTax"].Value
                        };
                        line.TaxCode = taxcodelnk;

                        if ((row.Cells["ColJob"].Value != null))
                        {
                            var joblnk = new JobLink {
                                UID = (Guid)row.Cells["ColJob"].Value
                            };
                            line.Job = joblnk;
                        }

                        lines.Add(line);
                    }
                }


                serviceInvoice.Lines = lines;
                serviceInvoiceSvc.Insert(MyCompanyFile, serviceInvoice, MyCredentials, OnSaveComplete, OnError);
                ShowSpinner();
            }
        }
        private void createinvoice()
        {
            var serviceInvoiceSvc = new ServiceInvoiceService(configuration, null,
                                                              MyOAuthKeyService);
            var serviceInvoice = new ServiceInvoice();

            if ((_invoice == null))
            {
                var customerLnk = new CustomerLink {
                    UID = (Guid)CmboCustomer.SelectedValue
                };
                serviceInvoice.Customer       = customerLnk;
                serviceInvoice.Date           = DateTime.Parse;
                serviceInvoice.IsTaxInclusive = ChkTaxInclusive.Checked;

                var lines = new List <ServiceInvoiceLine>();

                foreach (GridView1 row in GrdServiceLines.Rows)
                {
                    if (!row.IsNewRow)
                    {
                        var line = new ServiceInvoiceLine
                        {
                            Type        = InvoiceLineType.Transaction,
                            Description = (string)row.Cells["ColDescription"].Value,
                            Total       = Convert.ToDecimal(row.Cells["ColAmount"].Value)
                        };

                        if ((row.Cells["ColAccount"].Value == null))
                        {
                            MessageBox.Show("you must select an account on each row");
                            return;
                        }
                        var accountlnk = new AccountLink {
                            UID = (Guid)row.Cells["ColAccount"].Value
                        };
                        line.Account = accountlnk;

                        if ((row.Cells["ColTax"].Value == null))
                        {
                            MessageBox.Show("you must select a taxcode on each row");
                            return;
                        }
                        var taxcodelnk = new TaxCodeLink {
                            UID = (Guid)row.Cells["ColTax"].Value
                        };
                        line.TaxCode = taxcodelnk;

                        if ((row.Cells["ColJob"].Value != null))
                        {
                            var joblnk = new JobLink {
                                UID = (Guid)row.Cells["ColJob"].Value
                            };
                            line.Job = joblnk;
                        }

                        lines.Add(line);
                    }
                }
            }
        }
        public async void ShowPdf(Invoice invoice)
        {
            if (new Version(CompanyFile.CompanyFile.ProductVersion) < new Version("2013.4"))
            {
                MessageBox.Show("Company file does not support PDF creation!");
                return;
            }
            IsLoading = true;

            var pdfName = invoice.Number + ".pdf";
            var iService = new ServiceInvoiceService(new ApiConfiguration(), null, _keyService);

            try
            {
                var pdf =
                    await
                    iService.GetInvoiceFormAsPdfAsync(CompanyFile.CompanyFile, invoice.UID,
                                                      CompanyFile.Authentication[0], null);

                var localFolder = ApplicationData.Current.LocalFolder;
                var storageFile = await localFolder.CreateFileAsync(pdfName, CreationCollisionOption.ReplaceExisting);
                using (var outputStream = await storageFile.OpenStreamForWriteAsync())
                {
                    await pdf.CopyToAsync(outputStream);
                }
                Windows.System.Launcher.LaunchFileAsync(storageFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                IsLoading = false;
            }
        }