Skip to content

Commit

Permalink
Enahnce the UT
Browse files Browse the repository at this point in the history
  • Loading branch information
alvachien committed Mar 9, 2022
1 parent f53deab commit 79f34ed
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 107 deletions.
70 changes: 33 additions & 37 deletions src/hihapi/Controllers/Finance/FinanceReportsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Microsoft.AspNetCore.OData.Formatter;
using System.Threading.Tasks;

namespace hihapi.Controllers.Finance
namespace hihapi.Controllers
{
[Authorize]
public class FinanceReportsController : ODataController
Expand Down Expand Up @@ -55,9 +55,7 @@ public IActionResult GetReportByTranType([FromBody] ODataActionParameters parame
{
usrName = HIHAPIUtility.GetUserID(this);
if (String.IsNullOrEmpty(usrName))
{
throw new UnauthorizedAccessException();
}
}
catch
{
Expand All @@ -67,9 +65,7 @@ public IActionResult GetReportByTranType([FromBody] ODataActionParameters parame
// 2. Check the Home ID
var hms = _context.HomeMembers.Where(p => p.HomeID == hid && p.User == usrName).Count();
if (hms <= 0)
{
throw new UnauthorizedAccessException();
}

// 3. Calculate the amount
DateTime dtlow = new DateTime(year, month == null ? 1 : month.Value, 1);
Expand Down Expand Up @@ -107,7 +103,15 @@ public IActionResult GetReportByTranType([FromBody] ODataActionParameters parame

return Ok(listResult);
}


/// <summary>
/// Get report by Account
/// </summary>
/// <param name="parameters">
/// HomeID: Home ID
/// </param>
/// <returns></returns>
/// <exception cref="UnauthorizedAccessException"></exception>
[HttpPost]
public IActionResult GetReportByAccount([FromBody]ODataActionParameters parameters)
{
Expand All @@ -133,9 +137,7 @@ public IActionResult GetReportByAccount([FromBody]ODataActionParameters paramete
{
usrName = HIHAPIUtility.GetUserID(this);
if (String.IsNullOrEmpty(usrName))
{
throw new UnauthorizedAccessException();
}
}
catch
{
Expand All @@ -145,9 +147,7 @@ public IActionResult GetReportByAccount([FromBody]ODataActionParameters paramete
// 2. Check the Home ID
var hms = _context.HomeMembers.Where(p => p.HomeID == hid && p.User == usrName).Count();
if (hms <= 0)
{
throw new UnauthorizedAccessException();
}

// 3. Calculate the amount
var results = (
Expand All @@ -157,7 +157,7 @@ on docitem.DocID equals docheader.ID
join trantype in _context.FinTransactionType
on docitem.TranType equals trantype.ID
join account in _context.FinanceAccount
on new { docitem.AccountID, IsNormal = true } equals new { AccountID = account.ID, IsNormal = account.Status == null || account.Status == (byte)FinanceAccountStatus.Normal }
on new { docitem.AccountID, IsNormal = true } equals new { AccountID = account.ID, IsNormal = account.Status == null || account.Status == (byte)FinanceAccountStatus.Normal }
where docheader.HomeID == hid
select new
{
Expand All @@ -181,7 +181,7 @@ into docitem2
UseCurr2 = docitem3.Key.UseCurr2,
ExgRate = docitem3.Key.ExgRate,
ExgRate2 = docitem3.Key.ExgRate2,
TranAmount = docitem3.Sum(p => p.TranAmount)
TranAmount = docitem3.Sum(p => (Double)p.TranAmount)
}).ToList();

List<FinanceReportByAccount> listResults = new List<FinanceReportByAccount>();
Expand All @@ -195,14 +195,14 @@ into docitem2
{
if (rst.ExgRate2 != null && rst.ExgRate2.GetValueOrDefault() > 0)
{
amountLC *= rst.ExgRate2.GetValueOrDefault();
amountLC *= (Double)rst.ExgRate2.GetValueOrDefault();
}
}
else
{
if (rst.ExgRate != null && rst.ExgRate.GetValueOrDefault() > 0)
{
amountLC *= rst.ExgRate.GetValueOrDefault();
amountLC *= (Double)rst.ExgRate.GetValueOrDefault();
}
}

Expand All @@ -214,25 +214,33 @@ into docitem2
nrst.HomeID = hid;
nrst.AccountID = rst.AccountID;
if (rst.IsExpense)
nrst.CreditBalance += amountLC;
nrst.CreditBalance += (Decimal)amountLC;
else
nrst.DebitBalance += amountLC;
nrst.DebitBalance += (Decimal)amountLC;
nrst.Balance = nrst.DebitBalance + nrst.CreditBalance;
listResults.Add(nrst);
}
else
{
if (rst.IsExpense)
listResults[acntidx].CreditBalance += amountLC;
listResults[acntidx].CreditBalance += (Decimal)amountLC;
else
listResults[acntidx].DebitBalance += amountLC;
listResults[acntidx].DebitBalance += (Decimal)amountLC;
listResults[acntidx].Balance = listResults[acntidx].DebitBalance + listResults[acntidx].CreditBalance;
}
}

return Ok(listResults);
}

/// <summary>
/// Get report by Control Center
/// </summary>
/// <param name="parameters">
/// HomeID: Home ID
/// </param>
/// <returns></returns>
/// <exception cref="UnauthorizedAccessException"></exception>
[HttpPost]
public IActionResult GetReportByControlCenter([FromBody] ODataActionParameters parameters)
{
Expand All @@ -258,9 +266,7 @@ public IActionResult GetReportByControlCenter([FromBody] ODataActionParameters p
{
usrName = HIHAPIUtility.GetUserID(this);
if (String.IsNullOrEmpty(usrName))
{
throw new UnauthorizedAccessException();
}
}
catch
{
Expand All @@ -270,9 +276,7 @@ public IActionResult GetReportByControlCenter([FromBody] ODataActionParameters p
// 2. Check the Home ID
var hms = _context.HomeMembers.Where(p => p.HomeID == hid && p.User == usrName).Count();
if (hms <= 0)
{
throw new UnauthorizedAccessException();
}

// 3. Calculate the amount
var results = (
Expand Down Expand Up @@ -304,7 +308,7 @@ into docitem2
UseCurr2 = docitem3.Key.UseCurr2,
ExgRate = docitem3.Key.ExgRate,
ExgRate2 = docitem3.Key.ExgRate2,
TranAmount = docitem3.Sum(p => p.TranAmount)
TranAmount = docitem3.Sum(p => (Double)p.TranAmount)
}).ToList();

List<FinanceReportByControlCenter> listResults = new List<FinanceReportByControlCenter>();
Expand All @@ -318,14 +322,14 @@ into docitem2
{
if (rst.ExgRate2 != null && rst.ExgRate2.GetValueOrDefault() > 0)
{
amountLC *= rst.ExgRate2.GetValueOrDefault();
amountLC *= (Double)rst.ExgRate2.GetValueOrDefault();
}
}
else
{
if (rst.ExgRate != null && rst.ExgRate.GetValueOrDefault() > 0)
{
amountLC *= rst.ExgRate.GetValueOrDefault();
amountLC *= (Double)rst.ExgRate.GetValueOrDefault();
}
}

Expand All @@ -337,18 +341,18 @@ into docitem2
nrst.HomeID = hid;
nrst.ControlCenterID = rst.ControlCenterID.GetValueOrDefault();
if (rst.IsExpense)
nrst.CreditBalance += amountLC;
nrst.CreditBalance += (Decimal)amountLC;
else
nrst.DebitBalance += amountLC;
nrst.DebitBalance += (Decimal)amountLC;
nrst.Balance = nrst.DebitBalance + nrst.CreditBalance;
listResults.Add(nrst);
}
else
{
if (rst.IsExpense)
listResults[ccidx].CreditBalance += amountLC;
listResults[ccidx].CreditBalance += (Decimal)amountLC;
else
listResults[ccidx].DebitBalance += amountLC;
listResults[ccidx].DebitBalance += (Decimal)amountLC;
listResults[ccidx].Balance = listResults[ccidx].DebitBalance + listResults[ccidx].CreditBalance;
}
}
Expand Down Expand Up @@ -384,9 +388,7 @@ public IActionResult GetReportByOrder([FromBody] ODataActionParameters parameter
{
usrName = HIHAPIUtility.GetUserID(this);
if (String.IsNullOrEmpty(usrName))
{
throw new UnauthorizedAccessException();
}
}
catch
{
Expand All @@ -396,9 +398,7 @@ public IActionResult GetReportByOrder([FromBody] ODataActionParameters parameter
// 2. Check the Home ID
var hms = _context.HomeMembers.Where(p => p.HomeID == hid && p.User == usrName).Count();
if (hms <= 0)
{
throw new UnauthorizedAccessException();
}

// 3. Calculate the amount
List<FinanceReportByOrder> listResults = new List<FinanceReportByOrder>();
Expand Down Expand Up @@ -590,9 +590,7 @@ public IActionResult GetFinanceOverviewKeyFigure([FromBody] ODataActionParameter
{
usrName = HIHAPIUtility.GetUserID(this);
if (String.IsNullOrEmpty(usrName))
{
throw new UnauthorizedAccessException();
}
}
catch
{
Expand All @@ -602,9 +600,7 @@ public IActionResult GetFinanceOverviewKeyFigure([FromBody] ODataActionParameter
// 2. Check the Home ID
var hms = _context.HomeMembers.Where(p => p.HomeID == hid && p.User == usrName).Count();
if (hms <= 0)
{
throw new UnauthorizedAccessException();
}

// 3. Calculate the key figure of current month.
FinanceOverviewKeyFigure keyfigure = new FinanceOverviewKeyFigure();
Expand Down
66 changes: 0 additions & 66 deletions src/hihapi/Utilities/IQueryableExtensions.cs

This file was deleted.

5 changes: 2 additions & 3 deletions test/hihapi.integrationtest/CustomWebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
{
// Utilities.InitializeDbForTests(db);
}
catch (Exception exp)
{

catch (Exception)
{
//logger.LogError(ex, "An error occurred seeding the " +
// "database with test messages. Error: {Message}", ex.Message);
}
Expand Down
Loading

0 comments on commit 79f34ed

Please sign in to comment.