You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
7.0 KiB
170 lines
7.0 KiB
2 years ago
|
using Biskilog_Accounting.Server.POSModels;
|
||
|
using Biskilog_Accounting.Shared.CustomModels;
|
||
|
using Biskilog_Accounting.Shared.Enums;
|
||
|
using Biskilog_Accounting.Shared.Interfaces;
|
||
|
using Biskilog_Accounting.Shared.POSModels;
|
||
|
using Microsoft.EntityFrameworkCore;
|
||
|
using Microsoft.Net.Http.Headers;
|
||
|
using MySqlConnector;
|
||
|
using System.Drawing.Drawing2D;
|
||
|
|
||
|
namespace Biskilog_Accounting.Server.Services
|
||
|
{
|
||
|
public class SalesService : ISalesInterface
|
||
|
{
|
||
|
private readonly BiskAcdbContext m_context;
|
||
|
private readonly ITokenService m_tokenService;
|
||
|
private readonly HttpContext m_httpContext;
|
||
|
|
||
|
public event EventHandler TransactionsChanged;
|
||
|
public event EventHandler FetchComplete;
|
||
|
public event EventHandler FetchStart;
|
||
|
|
||
|
public SalesService(BiskAcdbContext a_context, ITokenService a_tokenService, IHttpContextAccessor a_httpContextAccessor)
|
||
|
{
|
||
|
m_context = a_context;
|
||
|
m_tokenService = a_tokenService;
|
||
|
m_httpContext = a_httpContextAccessor?.HttpContext;
|
||
|
}
|
||
|
|
||
|
public Task FetchRecentTransaction(int a_limit)
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
|
||
|
public IEnumerable<SaleItem> GetRecentTransaction()
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
|
||
|
public IEnumerable<SaleItem> GetTransactions(DateTime a_start, DateTime a_end)
|
||
|
{
|
||
|
string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!;
|
||
|
|
||
|
if (AuthEnums.Valid == m_tokenService.ValidateToken(token))
|
||
|
{
|
||
|
IEnumerable<string> accessiblebranches = m_tokenService.BranchIds(token);
|
||
|
|
||
|
using (var command = m_context.Database.GetDbConnection().CreateCommand())
|
||
|
{
|
||
|
command.CommandText = "CALL GetTransactionsByDate(@p0,@p1,@p2)";
|
||
|
command.Parameters.Add(new MySqlParameter("@p0", a_start.ToString("yyyy-MM-dd")));
|
||
|
command.Parameters.Add(new MySqlParameter("@p1", a_end.ToString("yyyy-MM-dd")));
|
||
|
command.Parameters.Add(new MySqlParameter("@p2", string.Join(", ", accessiblebranches.ToArray())));
|
||
|
|
||
|
m_context.Database.OpenConnection();
|
||
|
|
||
|
using (var reader = command.ExecuteReader())
|
||
|
{
|
||
|
while (reader.Read())
|
||
|
{
|
||
|
yield return new SaleItem
|
||
|
{
|
||
|
Transno = reader.GetString(0),
|
||
|
Total = (decimal)reader.GetDouble(1),
|
||
|
Date = reader.GetDateTime(2),
|
||
|
Cashier = reader.GetString(3),
|
||
|
BranchId = reader.GetString(4),
|
||
|
Customer = reader.GetString(5),
|
||
|
Status = reader.GetString(6),
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Task FetchTransaction(DateTime a_start, DateTime a_end)
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
|
||
|
public Task FetchReceipt(string a_receiptId)
|
||
|
{
|
||
|
throw new NotImplementedException();
|
||
|
}
|
||
|
|
||
|
public IEnumerable<SaleItem> GetReceipt(string a_receiptId)
|
||
|
{
|
||
|
string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!;
|
||
|
|
||
|
if (AuthEnums.Valid == m_tokenService.ValidateToken(token))
|
||
|
{
|
||
|
IEnumerable<string> accessiblebranches = m_tokenService.BranchIds(token);
|
||
|
|
||
|
using (var command = m_context.Database.GetDbConnection().CreateCommand())
|
||
|
{
|
||
|
command.CommandText = "CALL GetTransactionsById(@p0,@p1)";
|
||
|
command.Parameters.Add(new MySqlParameter("@p0", a_receiptId));
|
||
|
command.Parameters.Add(new MySqlParameter("@p1", string.Join(", ", accessiblebranches.ToArray())));
|
||
|
|
||
|
m_context.Database.OpenConnection();
|
||
|
|
||
|
using (var reader = command.ExecuteReader())
|
||
|
{
|
||
|
while (reader.Read())
|
||
|
{
|
||
|
yield return new SaleItem
|
||
|
{
|
||
|
Transno = reader.GetString(0),
|
||
|
Total = (decimal)reader.GetDouble(1),
|
||
|
Date = reader.GetDateTime(2),
|
||
|
Cashier = reader.GetString(3),
|
||
|
BranchId = reader.GetString(4),
|
||
|
Customer = reader.GetString(5),
|
||
|
Status = reader.GetString(6),
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Task<IEnumerable<Tblcart>> GetReceiptDetail(string a_receiptId)
|
||
|
{
|
||
|
List<Tblcart> details = new List<Tblcart>();
|
||
|
string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!;
|
||
|
|
||
|
if (AuthEnums.Valid == m_tokenService.ValidateToken(token))
|
||
|
{
|
||
|
IEnumerable<string> accessiblebranches = m_tokenService.BranchIds(token);
|
||
|
|
||
|
using (var command = m_context.Database.GetDbConnection().CreateCommand())
|
||
|
{
|
||
|
command.CommandText = "CALL GetReceiptDetails(@p0,@p1)";
|
||
|
command.Parameters.Add(new MySqlParameter("@p0", a_receiptId));
|
||
|
command.Parameters.Add(new MySqlParameter("@p1", string.Join(", ", accessiblebranches.ToArray())));
|
||
|
|
||
|
m_context.Database.OpenConnection();
|
||
|
|
||
|
using (var reader = command.ExecuteReader())
|
||
|
{
|
||
|
while (reader.Read())
|
||
|
{
|
||
|
details.Add(new Tblcart
|
||
|
{
|
||
|
Transno = a_receiptId,
|
||
|
Id = reader.GetString(0),
|
||
|
Quantity = reader.GetInt32(1),
|
||
|
Date = reader.GetDateTime(2),
|
||
|
Price = reader.GetDecimal(3),
|
||
|
Cashier = reader.GetString(4),
|
||
|
Status = reader.GetString(5),
|
||
|
Total = (decimal)reader.GetDouble(6),
|
||
|
Unit = reader.GetString(7),
|
||
|
Costprice = reader.GetDecimal(8),
|
||
|
BranchId = reader.GetString(9),
|
||
|
CountId = reader.GetString(10),
|
||
|
Tendered = reader.GetDecimal(11),
|
||
|
Balance = reader.GetDecimal(12),
|
||
|
ValueAddTax = reader.GetDecimal(13)
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return Task.FromResult(details.AsEnumerable());
|
||
|
}
|
||
|
}
|
||
|
}
|