Feat ERP 2
This commit is contained in:
37
src/SIGCM.Infrastructure/Repositories/ReportRepository.cs
Normal file
37
src/SIGCM.Infrastructure/Repositories/ReportRepository.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Dapper;
|
||||
using SIGCM.Domain.Interfaces;
|
||||
using SIGCM.Infrastructure.Data;
|
||||
|
||||
namespace SIGCM.Infrastructure.Repositories;
|
||||
|
||||
public class ReportRepository : IReportRepository
|
||||
{
|
||||
private readonly IDbConnectionFactory _db;
|
||||
|
||||
public ReportRepository(IDbConnectionFactory db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<SettlementItem>> GetInterCompanySettlementAsync(DateTime from, DateTime to)
|
||||
{
|
||||
using var conn = _db.CreateConnection();
|
||||
var sql = @"
|
||||
SELECT
|
||||
b.Name as BillingCompany,
|
||||
s.Name as ServiceCompany,
|
||||
COUNT(*) as TransactionCount,
|
||||
SUM(oi.SubTotal) as TotalAmount
|
||||
FROM OrderItems oi
|
||||
JOIN Orders o ON oi.OrderId = o.Id
|
||||
JOIN Companies b ON oi.BillingCompanyId = b.Id
|
||||
JOIN Companies s ON oi.ServiceCompanyId = s.Id
|
||||
WHERE oi.BillingCompanyId <> oi.ServiceCompanyId
|
||||
AND o.PaymentStatus = 'Paid' -- Solo liquidamos dinero efectivamente cobrado
|
||||
AND o.CreatedAt >= @From AND o.CreatedAt <= @To
|
||||
GROUP BY b.Name, s.Name
|
||||
ORDER BY b.Name, s.Name";
|
||||
|
||||
return await conn.QueryAsync<SettlementItem>(sql, new { From = from, To = to });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user