public void DisplayData()
        {
            InvoiceDataLoading = true;

            InvoiceListResponse response = new InvoiceSQLiteRepository()
                                           .GetInvoicesByPage(MainWindow.CurrentCompanyId, InvoiceSearchObject, currentPage, itemsPerPage);

            if (response.Success)
            {
                InvoicesFromDB = new ObservableCollection <InvoiceViewModel>(response.Invoices ?? new List <InvoiceViewModel>());
                totalItems     = response.TotalItems;
            }
            else
            {
                InvoicesFromDB          = new ObservableCollection <InvoiceViewModel>();
                totalItems              = 0;
                MainWindow.ErrorMessage = response.Message;
            }

            int itemFrom = totalItems != 0 ? (currentPage - 1) * itemsPerPage + 1 : 0;
            int itemTo   = currentPage * itemsPerPage < totalItems ? currentPage * itemsPerPage : totalItems;

            PaginationDisplay = itemFrom + " - " + itemTo + " od " + totalItems;

            InvoiceDataLoading = false;
        }
        private void BtnSubmit_Click(object sender, RoutedEventArgs e)
        {
            #region Validation

            if (InvoiceItemsFromDB == null || InvoiceItemsFromDB.Count == 0)
            {
                MainWindow.WarningMessage = ((string)Application.Current.FindResource("Ne_postoje_stavke_za_proknjižavanje"));
                return;
            }

            #endregion

            Thread td = new Thread(() => {
                SubmitButtonContent = ((string)Application.Current.FindResource("Čuvanje_u_tokuTriTacke"));
                SubmitButtonEnabled = false;


                CurrentInvoice.TotalPrice  = (double)InvoiceItemsFromDB.Sum(x => x.Amount);
                CurrentInvoice.TotalPDV    = (double)InvoiceItemsFromDB.Sum(x => x.PDV);
                CurrentInvoice.TotalRebate = (double)InvoiceItemsFromDB.Sum(x => x.TotalDiscount);


                var response = new InvoiceSQLiteRepository().Create(CurrentInvoice);
                if (!response.Success)
                {
                    MainWindow.ErrorMessage = ((string)Application.Current.FindResource("Greška_kod_lokalnog_čuvanjaUzvičnik"));
                    SubmitButtonContent     = ((string)Application.Current.FindResource("Proknjiži"));
                    SubmitButtonEnabled     = true;
                }

                CurrentInvoice.InvoiceItems = InvoiceItemsFromDB;
                response = invoiceService.Create(CurrentInvoice);
                if (!response.Success)
                {
                    MainWindow.ErrorMessage = ((string)Application.Current.FindResource("Greška_kod_čuvanja_na_serveruUzvičnik"));
                    SubmitButtonContent     = ((string)Application.Current.FindResource("Proknjiži"));
                    SubmitButtonEnabled     = true;
                }

                if (response.Success)
                {
                    MainWindow.SuccessMessage = ((string)Application.Current.FindResource("Podaci_su_uspešno_sačuvaniUzvičnik"));
                    SubmitButtonContent       = ((string)Application.Current.FindResource("Proknjiži"));
                    SubmitButtonEnabled       = true;

                    InvoiceCreatedUpdated();

                    Application.Current.Dispatcher.BeginInvoke(
                        System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(() =>
                    {
                        FlyoutHelper.CloseFlyout(this);
                    })
                        );
                }
            });
            td.IsBackground = true;
            td.Start();
        }
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentInvoice == null)
            {
                MainWindow.WarningMessage = ((string)Application.Current.FindResource("Morate_odabrati_fakturu_za_brisanjeUzvičnik"));
                return;
            }

            if (CurrentInvoice.Id < 1)
            {
                var localResult = new InvoiceSQLiteRepository().Delete(CurrentInvoice.Identifier);
                if (localResult.Success)
                {
                    foreach (var item in InvoiceItemsFromDB)
                    {
                        if (item.Invoice != null && item.Invoice.Identifier == CurrentInvoice.Identifier)
                        {
                            var localResultItem = new InvoiceItemSQLiteRepository().Delete(item.Identifier);
                            if (!localResultItem.Success)
                            {
                                MainWindow.ErrorMessage = localResultItem.Message;
                            }
                        }
                    }

                    MainWindow.SuccessMessage = ((string)Application.Current.FindResource("Podaci_su_uspešno_obrisaniUzvičnik"));

                    Thread displayThread = new Thread(() => SyncData());
                    displayThread.IsBackground = true;
                    displayThread.Start();
                }
                else
                {
                    MainWindow.ErrorMessage = localResult.Message;
                }

                return;
            }
            else
            {
                // Delete data
                var result = invoiceService.Delete(CurrentInvoice.Identifier);
                if (result.Success)
                {
                    MainWindow.SuccessMessage = ((string)Application.Current.FindResource("Podaci_su_uspešno_obrisaniUzvičnik"));

                    Thread displayThread = new Thread(() => SyncData());
                    displayThread.IsBackground = true;
                    displayThread.Start();
                }
                else
                {
                    MainWindow.ErrorMessage = result.Message;
                }
            }
        }
        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentInvoice == null)
            {
                MainWindow.WarningMessage = ((string)Application.Current.FindResource("Morate_odabrati_fakturu_za_kopiranjeUzvičnik"));
                return;
            }

            var button = ((Button)sender);

            if (btnSemaphore.Wait(0) && button != null)
            {
                button.IsEnabled = false;
                Thread td = new Thread(() =>
                {
                    if (!CanCopyInvoice)
                    {
                        MainWindow.WarningMessage = ((string)Application.Current.FindResource("Morate_odabrati_fakturu_za_kopiranjeUzvičnik"));
                        return;
                    }

                    bool success = true;

                    var newInvoice = new InvoiceViewModel()
                    {
                        Id         = 0,
                        Identifier = Guid.NewGuid(),
                        Code       = "",
                        Address    = CurrentInvoice.Address,
                        Buyer      = CurrentInvoice.Buyer,
                        BuyerName  = CurrentInvoice.BuyerName,
                        City       = CurrentInvoice.City,
                        Company    = new CompanyViewModel()
                        {
                            Id = MainWindow.CurrentCompanyId
                        },
                        CurrencyCode         = CurrentInvoice.CurrencyCode,
                        CurrencyExchangeRate = CurrentInvoice.CurrencyExchangeRate,
                        CreatedBy            = new UserViewModel()
                        {
                            Id = MainWindow.CurrentUserId
                        },
                        DateOfPayment = CurrentInvoice.DateOfPayment,
                        Description   = CurrentInvoice.Description,
                        Discount      = CurrentInvoice.Discount,
                        InvoiceDate   = CurrentInvoice.InvoiceDate,
                        DueDate       = CurrentInvoice.DueDate,
                        IsSynced      = false,
                        IsActive      = true,
                        Municipality  = CurrentInvoice.Municipality,
                        PdvType       = CurrentInvoice.PdvType,
                        Status        = ItemStatus.Added,
                        StatusDate    = DateTime.Now,
                        TotalPDV      = CurrentInvoice.TotalPDV,
                        TotalPrice    = CurrentInvoice.TotalPrice,
                        TotalRebate   = CurrentInvoice.TotalRebate,
                        Vat           = CurrentInvoice.Vat
                    };

                    var response = new InvoiceSQLiteRepository().Create(newInvoice);
                    if (response.Success)
                    {
                        var itemResponse = new InvoiceItemSQLiteRepository().GetInvoiceItemsByInvoice(MainWindow.CurrentCompanyId, CurrentInvoice.Identifier);
                        if (itemResponse.Success)
                        {
                            var itemsToCopy = itemResponse?.InvoiceItems ?? new List <InvoiceItemViewModel>();
                            foreach (var item in itemsToCopy)
                            {
                                var newItem = new InvoiceItemViewModel()
                                {
                                    Invoice = newInvoice,
                                    Amount  = item.Amount,
                                    Code    = item.Code,
                                    Company = new CompanyViewModel()
                                    {
                                        Id = MainWindow.CurrentCompanyId
                                    },
                                    CreatedBy = new UserViewModel()
                                    {
                                        Id = MainWindow.CurrentUserId
                                    },
                                    CurrencyCode         = item.CurrencyCode,
                                    CurrencyPriceWithPDV = item.CurrencyPriceWithPDV,
                                    Discount             = item.Discount,
                                    ExchangeRate         = item.ExchangeRate,
                                    Id              = 0,
                                    Identifier      = Guid.NewGuid(),
                                    IsActive        = true,
                                    IsSynced        = false,
                                    ItemStatus      = ItemStatus.Submited,
                                    Name            = item.Name,
                                    PDV             = item.PDV,
                                    PDVPercent      = item.PDVPercent,
                                    PriceWithoutPDV = item.PriceWithoutPDV,
                                    PriceWithPDV    = item.PriceWithPDV,
                                    Quantity        = item.Quantity,
                                    UnitOfMeasure   = item.UnitOfMeasure,
                                };
                                var itemCreateResponse = new InvoiceItemSQLiteRepository().Create(newItem);
                                if (!itemCreateResponse.Success)
                                {
                                    MainWindow.ErrorMessage = itemCreateResponse.Message;
                                    success = false;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            MainWindow.ErrorMessage = itemResponse.Message;
                            success = false;
                        }
                    }
                    else
                    {
                        MainWindow.ErrorMessage = response.Message;
                        success = false;
                    }

                    if (success)
                    {
                        MainWindow.SuccessMessage = (string)Application.Current.FindResource("Kopiranje_je_uspešno_izvršenoUzvičnik");
                    }
                    else
                    {
                        MainWindow.WarningMessage = (string)Application.Current.FindResource("Došlo_je_do_greške_pri_kopiranju_proverite_stanje_faktureUzvičnik");
                    }

                    DisplayData();

                    Dispatcher.BeginInvoke((Action)(() =>
                    {
                        btnSemaphore.Release();
                        if (button != null)
                        {
                            button.IsEnabled = true;
                        }
                    }));
                });

                td.IsBackground = true;
                td.Start();
            }
        }
        private void BtnSubmit_Click(object sender, RoutedEventArgs e)
        {
            #region Validation

            if (CurrentBusinessPartnerInvoice == null)
            {
                MainWindow.WarningMessage = ((string)Application.Current.FindResource("Obavezno_polje_poslovni_partner"));
                return;
            }

            #endregion

            Thread td = new Thread(() => {
                SubmitButtonContent     = ((string)Application.Current.FindResource("Čuvanje_u_tokuTriTacke"));
                SubmitButtonEnabled     = false;
                CurrentInvoice.Buyer    = CurrentBusinessPartnerInvoice;
                CurrentInvoice.IsSynced = false;
                CurrentInvoice.Company  = new CompanyViewModel()
                {
                    Id = MainWindow.CurrentCompanyId
                };
                CurrentInvoice.CreatedBy = new UserViewModel()
                {
                    Id = MainWindow.CurrentUserId
                };

                var itemsResponse = new InvoiceItemSQLiteRepository().GetInvoiceItemsByInvoice(MainWindow.CurrentCompanyId, CurrentInvoice.Identifier);

                if (itemsResponse.InvoiceItems != null)
                {
                    CurrentInvoice.InvoiceItems = new ObservableCollection <InvoiceItemViewModel>(itemsResponse?.InvoiceItems ?? new List <InvoiceItemViewModel>());
                    foreach (var item in CurrentInvoice.InvoiceItems)
                    {
                        item.PDVPercent = CurrentInvoice.Vat?.Amount ?? 0;
                        item.ItemStatus = ItemStatus.Edited;
                        new InvoiceItemSQLiteRepository().Delete(item.Identifier);
                        new InvoiceItemSQLiteRepository().Create(item);
                    }
                }

                CurrentInvoice.TotalPrice = CurrentInvoice.InvoiceItems.Sum(x => (double)x.PriceWithPDV);

                InvoiceResponse response = new InvoiceSQLiteRepository().Create(CurrentInvoice);
                if (!response.Success)
                {
                    MainWindow.ErrorMessage = ((string)Application.Current.FindResource("Greška_kod_lokalnog_čuvanjaUzvičnik"));
                    SubmitButtonContent     = ((string)Application.Current.FindResource("Proknjiži"));
                    SubmitButtonEnabled     = true;
                }

                response = outputInvoiceService.Create(CurrentInvoice);
                if (!response.Success)
                {
                    MainWindow.ErrorMessage = ((string)Application.Current.FindResource("Podaci_su_sačuvani_u_lokaluUzvičnikTačka_Greška_kod_čuvanja_na_serveruUzvičnik")) + response.Message;
                    SubmitButtonContent     = ((string)Application.Current.FindResource("Proknjiži"));
                    SubmitButtonEnabled     = true;
                }

                if (response.Success)
                {
                    MainWindow.SuccessMessage = ((string)Application.Current.FindResource("Podaci_su_uspešno_sačuvaniUzvičnik"));
                    SubmitButtonContent       = ((string)Application.Current.FindResource("Proknjiži"));
                    SubmitButtonEnabled       = true;

                    new InvoiceSQLiteRepository().Sync(outputInvoiceService);

                    InvoiceCreatedUpdated();

                    Application.Current.Dispatcher.BeginInvoke(
                        System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(() =>
                    {
                        FlyoutHelper.CloseFlyout(this);
                    })
                        );
                }
            });
            td.IsBackground = true;
            td.Start();
        }