public InvoiceListResponse GetInvoicesNewerThan(int companyId, DateTime?lastUpdateTime)
        {
            InvoiceListResponse response = new InvoiceListResponse();

            try
            {
                if (lastUpdateTime != null)
                {
                    response.Invoices = unitOfWork.GetInvoiceRepository()
                                        .GetInvoicesNewerThan(companyId, (DateTime)lastUpdateTime)
                                        .ConvertToInvoiceViewModelList();
                }
                else
                {
                    response.Invoices = unitOfWork.GetInvoiceRepository()
                                        .GetInvoices(companyId)
                                        .ConvertToInvoiceViewModelList();
                }
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Invoices = new List <InvoiceViewModel>();
                response.Success  = false;
                response.Message  = ex.Message;
            }

            return(response);
        }
        public InvoiceListResponse Sync(SyncInvoiceRequest request)
        {
            InvoiceListResponse response = new InvoiceListResponse();

            try
            {
                response.Invoices = new List <InvoiceViewModel>();

                if (request.LastUpdatedAt != null)
                {
                    response.Invoices.AddRange(unitOfWork.GetInvoiceRepository()
                                               .GetInvoicesNewerThan(request.CompanyId, (DateTime)request.LastUpdatedAt)
                                               ?.ConvertToInvoiceViewModelList() ?? new List <InvoiceViewModel>());
                }
                else
                {
                    response.Invoices.AddRange(unitOfWork.GetInvoiceRepository()
                                               .GetInvoices(request.CompanyId)
                                               ?.ConvertToInvoiceViewModelList() ?? new List <InvoiceViewModel>());
                }

                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Invoices = new List <InvoiceViewModel>();
                response.Success  = false;
                response.Message  = ex.Message;
            }

            return(response);
        }
        public InvoiceListResponse Sync(SyncInvoiceRequest request)
        {
            InvoiceListResponse response = new InvoiceListResponse();

            try
            {
                response = WpfApiHandler.SendToApi <SyncInvoiceRequest, InvoiceViewModel, InvoiceListResponse>(request, "Sync");
            }
            catch (Exception ex)
            {
                response.Invoices = new List <InvoiceViewModel>();
                response.Success  = false;
                response.Message  = ex.Message;
            }

            return(response);
        }
        public InvoiceListResponse GetInvoicesForPopup(int companyId, string filterString)
        {
            InvoiceListResponse     response = new InvoiceListResponse();
            List <InvoiceViewModel> Invoices = new List <InvoiceViewModel>();

            using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
            {
                db.Open();
                try
                {
                    SqliteCommand selectCommand = new SqliteCommand(
                        SqlCommandSelectPart +
                        "FROM Invoices " +
                        "WHERE (@InvoiceNumber IS NULL OR @InvoiceNumber = '' OR InvoiceNumber LIKE @InvoiceNumber) " +
                        "AND CompanyId = @CompanyId " +
                        "ORDER BY IsSynced, Id DESC " +
                        "LIMIT @ItemsPerPage;", db);

                    selectCommand.Parameters.AddWithValue("@InvoiceNumber", ((object)filterString) != null ? "%" + filterString + "%" : "");
                    selectCommand.Parameters.AddWithValue("@CompanyId", ((object)filterString) != null ? companyId : 0);
                    selectCommand.Parameters.AddWithValue("@ItemsPerPage", 100);

                    SqliteDataReader query = selectCommand.ExecuteReader();

                    while (query.Read())
                    {
                        InvoiceViewModel dbEntry = Read(query);
                        Invoices.Add(dbEntry);
                    }
                }
                catch (SqliteException error)
                {
                    MainWindow.ErrorMessage = error.Message;
                    response.Success        = false;
                    response.Message        = error.Message;
                    response.Invoices       = new List <InvoiceViewModel>();
                    return(response);
                }
                db.Close();
            }
            response.Success  = true;
            response.Invoices = Invoices;
            return(response);
        }
Example #5
0
        public JsonResult GetInvoices(int companyId)
        {
            InvoiceListResponse response = new InvoiceListResponse();

            try
            {
                response = invoiceService.GetInvoices(companyId);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
                Console.WriteLine(ex.Message);
            }
            return(Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting = Newtonsoft.Json.Formatting.Indented
            }));
        }
Example #6
0
        public JsonResult Sync([FromBody] SyncInvoiceRequest request)
        {
            InvoiceListResponse response = new InvoiceListResponse();

            try
            {
                response = this.invoiceService.Sync(request);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }

            return(Json(response, new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting = Newtonsoft.Json.Formatting.Indented
            }));
        }
        public InvoiceListResponse GetInvoices(int companyId)
        {
            InvoiceListResponse response = new InvoiceListResponse();

            try
            {
                response.Invoices = unitOfWork.GetInvoiceRepository().GetInvoices(companyId)
                                    .ConvertToInvoiceViewModelList();
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Invoices = new List <InvoiceViewModel>();
                response.Success  = false;
                response.Message  = ex.Message;
            }

            return(response);
        }
        public InvoiceListResponse GetInvoices(int companyId)
        {
            InvoiceListResponse response = new InvoiceListResponse();

            try
            {
                response = WpfApiHandler.GetFromApi <List <InvoiceViewModel>, InvoiceListResponse>("GetInvoices", new Dictionary <string, string>()
                {
                    { "CompanyId", companyId.ToString() }
                });
            }
            catch (Exception ex)
            {
                response.Invoices = new List <InvoiceViewModel>();
                response.Success  = false;
                response.Message  = ex.Message;
            }

            return(response);
        }
        public void Sync(IInvoiceService invoiceService, Action <int, int> callback = null)
        {
            try
            {
                SyncInvoiceRequest request = new SyncInvoiceRequest();
                request.CompanyId     = MainWindow.CurrentCompanyId;
                request.LastUpdatedAt = GetLastUpdatedAt(MainWindow.CurrentCompanyId);

                int toSync      = 0;
                int syncedItems = 0;

                InvoiceListResponse response = invoiceService.Sync(request);
                if (response.Success)
                {
                    toSync = response?.Invoices?.Count ?? 0;
                    var items = new List <InvoiceViewModel>(response.Invoices);

                    using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
                    {
                        db.Open();
                        using (var transaction = db.BeginTransaction())
                        {
                            SqliteCommand deleteCommand = db.CreateCommand();
                            deleteCommand.CommandText = "DELETE FROM Invoices WHERE Identifier = @Identifier";

                            SqliteCommand insertCommand = db.CreateCommand();
                            insertCommand.CommandText = SqlCommandInsertPart;

                            foreach (var item in items)
                            {
                                deleteCommand.Parameters.AddWithValue("@Identifier", item.Identifier);
                                deleteCommand.ExecuteNonQuery();
                                deleteCommand.Parameters.Clear();

                                if (item.IsActive)
                                {
                                    item.IsSynced = true;

                                    insertCommand = AddCreateParameters(insertCommand, item);
                                    insertCommand.ExecuteNonQuery();
                                    insertCommand.Parameters.Clear();

                                    syncedItems++;
                                    callback?.Invoke(syncedItems, toSync);
                                }
                            }

                            transaction.Commit();
                        }
                        db.Close();
                    }
                }
                else
                {
                    throw new Exception(response.Message);
                }
            }
            catch (Exception ex)
            {
                MainWindow.ErrorMessage = ex.Message;
            }
        }
        public InvoiceListResponse GetInvoicesByPage(int companyId, InvoiceViewModel InvoiceSearchObject, int currentPage = 1, int itemsPerPage = 50)
        {
            InvoiceListResponse     response = new InvoiceListResponse();
            List <InvoiceViewModel> Invoices = new List <InvoiceViewModel>();

            using (SqliteConnection db = new SqliteConnection("Filename=SirmiumERPGFC.db"))
            {
                db.Open();
                try
                {
                    SqliteCommand selectCommand = new SqliteCommand(
                        SqlCommandSelectPart +
                        "FROM Invoices " +
                        "WHERE (@BuyerName IS NULL OR @BuyerName = '' OR BuyerName LIKE @BuyerName) " +
                        "AND (@InvoiceNumber IS NULL OR @InvoiceNumber = '' OR InvoiceNumber LIKE @InvoiceNumber) " +
                        "AND (@DateTo IS NULL OR @DateTo = '' OR DATE(InvoiceDate) <= DATE(@DateTo)) " +
                        "AND (@DateFrom IS NULL OR @DateFrom = '' OR DATE(InvoiceDate) >= DATE(@DateFrom)) " +
                        "AND (@DateOfPaymentTo IS NULL OR @DateOfPaymentTo = '' OR DATE(DateOfPayment) <=  DATE(@DateOfPaymentTo)) " +
                        "AND (@DateOfPaymentFrom IS NULL OR @DateOfPaymentFrom = '' OR DATE(DateOfPayment) >= DATE(@DateOfPaymentFrom)) " +
                        "AND CompanyId = @CompanyId " +
                        "ORDER BY IsSynced, Id DESC " +
                        "LIMIT @ItemsPerPage OFFSET @Offset;", db);

                    selectCommand.Parameters.AddWithValue("@BuyerName", ((object)InvoiceSearchObject.SearchBy_BusinessPartner) != null ? "%" + InvoiceSearchObject.SearchBy_BusinessPartner + "%" : "");
                    selectCommand.Parameters.AddWithValue("@InvoiceNumber", ((object)InvoiceSearchObject.SearchBy_InvoiceNumber) != null ? "%" + InvoiceSearchObject.SearchBy_InvoiceNumber + "%" : "");
                    selectCommand.Parameters.AddWithValue("@DateFrom", ((object)InvoiceSearchObject.SearchBy_InvoiceDateFrom) ?? DBNull.Value);
                    selectCommand.Parameters.AddWithValue("@DateTo", ((object)InvoiceSearchObject.SearchBy_InvoiceDateTo) ?? DBNull.Value);
                    selectCommand.Parameters.AddWithValue("@DateOfPaymentFrom", ((object)InvoiceSearchObject.SearchBy_DateOfPaymentFrom) ?? DBNull.Value);
                    selectCommand.Parameters.AddWithValue("@DateOfPaymentTo", ((object)InvoiceSearchObject.SearchBy_DateOfPaymentTo) ?? DBNull.Value);
                    selectCommand.Parameters.AddWithValue("@CompanyId", companyId);
                    selectCommand.Parameters.AddWithValue("@ItemsPerPage", itemsPerPage);
                    selectCommand.Parameters.AddWithValue("@Offset", (currentPage - 1) * itemsPerPage);

                    SqliteDataReader query = selectCommand.ExecuteReader();

                    while (query.Read())
                    {
                        InvoiceViewModel dbEntry = Read(query);
                        Invoices.Add(dbEntry);
                    }


                    selectCommand = new SqliteCommand(
                        "SELECT Count(*) " +
                        "FROM Invoices " +
                        "WHERE(@BuyerName IS NULL OR @BuyerName = '' OR BuyerName LIKE @BuyerName) " +
                        "AND (@InvoiceNumber IS NULL OR @InvoiceNumber = '' OR InvoiceNumber LIKE @InvoiceNumber) " +
                        "AND (@InvoiceNumber IS NULL OR @InvoiceNumber = '' OR InvoiceNumber LIKE @InvoiceNumber) " +
                        "AND (@DateTo IS NULL OR @DateTo = '' OR DATE(InvoiceDate) <= DATE(@DateTo)) " +
                        "AND (@DateFrom IS NULL OR @DateFrom = '' OR DATE(InvoiceDate) >= DATE(@DateFrom)) " +
                        "AND (@DateOfPaymentTo IS NULL OR @DateOfPaymentTo = '' OR DATE(DateOfPayment) <=  DATE(@DateOfPaymentTo)) " +
                        "AND (@DateOfPaymentFrom IS NULL OR @DateOfPaymentFrom = '' OR DATE(DateOfPayment) >= DATE(@DateOfPaymentFrom)) " +
                        "AND CompanyId = @CompanyId;", db);

                    selectCommand.Parameters.AddWithValue("@BuyerName", ((object)InvoiceSearchObject.SearchBy_BusinessPartner) != null ? "%" + InvoiceSearchObject.SearchBy_BusinessPartner + "%" : "");
                    selectCommand.Parameters.AddWithValue("@InvoiceNumber", ((object)InvoiceSearchObject.SearchBy_InvoiceNumber) != null ? "%" + InvoiceSearchObject.SearchBy_InvoiceNumber + "%" : "");
                    selectCommand.Parameters.AddWithValue("@DateFrom", ((object)InvoiceSearchObject.SearchBy_InvoiceDateFrom) ?? "");
                    selectCommand.Parameters.AddWithValue("@DateTo", ((object)InvoiceSearchObject.SearchBy_InvoiceDateTo) ?? "");
                    selectCommand.Parameters.AddWithValue("@DateOfPaymentFrom", ((object)InvoiceSearchObject.SearchBy_DateOfPaymentFrom) ?? "");
                    selectCommand.Parameters.AddWithValue("@DateOfPaymentTo", ((object)InvoiceSearchObject.SearchBy_DateOfPaymentTo) ?? "");
                    selectCommand.Parameters.AddWithValue("@CompanyId", companyId);

                    query = selectCommand.ExecuteReader();

                    if (query.Read())
                    {
                        response.TotalItems = query.GetInt32(0);
                    }
                }
                catch (SqliteException error)
                {
                    MainWindow.ErrorMessage = error.Message;
                    response.Success        = false;
                    response.Message        = error.Message;
                    response.Invoices       = new List <InvoiceViewModel>();
                    return(response);
                }
                db.Close();
            }
            response.Success  = true;
            response.Invoices = Invoices;
            return(response);
        }