2022-07-25 17:02:36 +02:00
|
|
|
|
using RMDataManagerLibrary.Internal.DataAccess;
|
|
|
|
|
using RMDataManagerLibrary.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace RMDataManagerLibrary.DataAcccess
|
|
|
|
|
{
|
|
|
|
|
public class SaleData
|
|
|
|
|
{
|
|
|
|
|
//TODO: Make this SOLID/DRY/BETTER
|
|
|
|
|
public void SaveSale(SaleModel saleInfo, string cashierId)
|
|
|
|
|
{
|
|
|
|
|
List<SaleDetailDBModel> details = new List<SaleDetailDBModel>();
|
|
|
|
|
ProductData products = new ProductData();
|
|
|
|
|
decimal taxRate = ConfigHelper.GetTaxRate()/100;
|
|
|
|
|
|
|
|
|
|
foreach (var item in saleInfo.SaleDetails)
|
|
|
|
|
{
|
|
|
|
|
var detail = new SaleDetailDBModel
|
|
|
|
|
{
|
|
|
|
|
ProductId = item.ProductId,
|
|
|
|
|
Quantity = item.Quantity
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// get the information about this product
|
|
|
|
|
var productInfo = products.GetProductById(item.ProductId);
|
|
|
|
|
|
|
|
|
|
if (productInfo == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Product with Id {item.ProductId} could not be found in DB");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
detail.PurchasePrice = productInfo.RetailPrice * detail.Quantity;
|
|
|
|
|
|
|
|
|
|
if (productInfo.IsTaxable)
|
|
|
|
|
{
|
|
|
|
|
detail.Tax = detail.PurchasePrice * taxRate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
details.Add(detail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SaleDBModel sale = new SaleDBModel
|
|
|
|
|
{
|
|
|
|
|
SubTotal = details.Sum(x => x.PurchasePrice),
|
|
|
|
|
Tax = details.Sum(x => x.Tax),
|
|
|
|
|
CashierId = cashierId
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sale.Total = sale.SubTotal + sale.Tax;
|
|
|
|
|
|
|
|
|
|
//save
|
2022-07-30 15:26:54 +02:00
|
|
|
|
using (SqlDataAccess sql = new SqlDataAccess())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
sql.StartTransaction("RMData");
|
2022-07-25 17:02:36 +02:00
|
|
|
|
|
2022-07-30 15:26:54 +02:00
|
|
|
|
sql.SaveDataInTransaction("spSaleInsert", sale);
|
2022-07-25 17:02:36 +02:00
|
|
|
|
|
2022-07-30 15:26:54 +02:00
|
|
|
|
sale.Id = sql.LoadDataInTransaction<int, dynamic>("spSaleLookUp", new { sale.CashierId, sale.SaleDate }).FirstOrDefault();
|
2022-07-25 17:02:36 +02:00
|
|
|
|
|
2022-07-30 15:26:54 +02:00
|
|
|
|
// finish filling up sale details
|
|
|
|
|
foreach (var item in details)
|
|
|
|
|
{
|
|
|
|
|
item.SaleId = sale.Id;
|
2022-07-25 17:02:36 +02:00
|
|
|
|
|
2022-07-30 15:26:54 +02:00
|
|
|
|
sql.SaveDataInTransaction("dbo.spSaleDetailInsert", item);
|
|
|
|
|
}
|
2022-08-02 17:49:27 +02:00
|
|
|
|
|
|
|
|
|
sql.CommitTransaction();
|
2022-07-30 15:26:54 +02:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
sql.RollBackTransaction();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2022-07-25 17:02:36 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-02 19:32:39 +02:00
|
|
|
|
|
|
|
|
|
public List<SaleReportModel> GetSaleReport()
|
|
|
|
|
{
|
|
|
|
|
SqlDataAccess sql = new SqlDataAccess();
|
|
|
|
|
|
|
|
|
|
var output = sql.LoadData<SaleReportModel, dynamic>("dbo.spSaleSaleReport", new { }, "RMData");
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
2022-07-25 17:02:36 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-30 15:26:54 +02:00
|
|
|
|
|