correct adding multiple fees

deepsource-autofix-76c6eb20
Dan Gowans 2022-08-25 10:36:34 -04:00
parent ed2af5f23d
commit 37c2f39ea0
2 changed files with 68 additions and 27 deletions

View File

@ -18,7 +18,7 @@ export const addLotOccupancyFee = (lotOccupancyFeeForm, requestSession) => {
feeAmount = calculateFeeAmount(fee, lotOccupancy); feeAmount = calculateFeeAmount(fee, lotOccupancy);
taxAmount = calculateTaxAmount(fee, feeAmount); taxAmount = calculateTaxAmount(fee, feeAmount);
} }
const record = database.prepare("select recordDelete_timeMillis" + const record = database.prepare("select feeAmount, taxAmount, recordDelete_timeMillis" +
" from LotOccupancyFees" + " from LotOccupancyFees" +
" where lotOccupancyId = ?" + " where lotOccupancyId = ?" +
" and feeId = ?") " and feeId = ?")
@ -31,18 +31,32 @@ export const addLotOccupancyFee = (lotOccupancyFeeForm, requestSession) => {
" and feeId = ?") " and feeId = ?")
.run(lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId); .run(lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId);
} }
else { else if (record.feeAmount === feeAmount && record.taxAmount === taxAmount) {
database.prepare("update LotOccupancyFees" + database.prepare("update LotOccupancyFees" +
" set quantity = quantity + ?," + " set quantity = quantity + ?," +
" feeAmount = feeAmount + ?," +
" taxAmount = taxAmount + ?," +
" recordUpdate_userName = ?," + " recordUpdate_userName = ?," +
" recordUpdate_timeMillis = ?" + " recordUpdate_timeMillis = ?" +
" where lotOccupancyId = ?" + " where lotOccupancyId = ?" +
" and feeId = ?") " and feeId = ?")
.run(lotOccupancyFeeForm.quantity, feeAmount, taxAmount, requestSession.user.userName, rightNowMillis, lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId); .run(lotOccupancyFeeForm.quantity, requestSession.user.userName, rightNowMillis, lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId);
database.close(); database.close();
return false; return true;
}
else {
const quantity = typeof (lotOccupancyFeeForm.quantity) === "string" ?
Number.parseFloat(lotOccupancyFeeForm.quantity) :
lotOccupancyFeeForm.quantity;
database.prepare("update LotOccupancyFees" +
" set feeAmount = (feeAmount * quantity) + ?," +
" taxAmount = (taxAmount * quantity) + ?," +
" quantity = 1," +
" recordUpdate_userName = ?," +
" recordUpdate_timeMillis = ?" +
" where lotOccupancyId = ?" +
" and feeId = ?")
.run((feeAmount * quantity), (taxAmount * quantity), requestSession.user.userName, rightNowMillis, lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId);
database.close();
return true;
} }
} }
const result = database const result = database

View File

@ -33,7 +33,7 @@ export const addLotOccupancyFee =
(lotOccupancyFeeForm: AddLotOccupancyFeeForm, requestSession: recordTypes.PartialSession): boolean => { (lotOccupancyFeeForm: AddLotOccupancyFeeForm, requestSession: recordTypes.PartialSession): boolean => {
const database = sqlite(databasePath); const database = sqlite(databasePath);
const rightNowMillis = Date.now(); const rightNowMillis = Date.now();
// Calculate fee and tax (if not set) // Calculate fee and tax (if not set)
@ -42,8 +42,8 @@ export const addLotOccupancyFee =
let taxAmount: number; let taxAmount: number;
if (lotOccupancyFeeForm.feeAmount) { if (lotOccupancyFeeForm.feeAmount) {
feeAmount = typeof(lotOccupancyFeeForm.feeAmount) === "string" ? Number.parseFloat(lotOccupancyFeeForm.feeAmount) : feeAmount; feeAmount = typeof (lotOccupancyFeeForm.feeAmount) === "string" ? Number.parseFloat(lotOccupancyFeeForm.feeAmount) : feeAmount;
taxAmount = typeof(lotOccupancyFeeForm.taxAmount) === "string" ? Number.parseFloat(lotOccupancyFeeForm.taxAmount) : taxAmount; taxAmount = typeof (lotOccupancyFeeForm.taxAmount) === "string" ? Number.parseFloat(lotOccupancyFeeForm.taxAmount) : taxAmount;
} else { } else {
const lotOccupancy = getLotOccupancy(lotOccupancyFeeForm.lotOccupancyId); const lotOccupancy = getLotOccupancy(lotOccupancyFeeForm.lotOccupancyId);
@ -56,8 +56,10 @@ export const addLotOccupancyFee =
// Check if record already exists // Check if record already exists
const record: { const record: {
feeAmount ? : number;
taxAmount ? : number;
recordDelete_timeMillis ? : number recordDelete_timeMillis ? : number
} = database.prepare("select recordDelete_timeMillis" + } = database.prepare("select feeAmount, taxAmount, recordDelete_timeMillis" +
" from LotOccupancyFees" + " from LotOccupancyFees" +
" where lotOccupancyId = ?" + " where lotOccupancyId = ?" +
" and feeId = ?") " and feeId = ?")
@ -71,28 +73,53 @@ export const addLotOccupancyFee =
" and lotOccupancyId = ?" + " and lotOccupancyId = ?" +
" and feeId = ?") " and feeId = ?")
.run(lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId); .run(lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId);
} else {
} else if (record.feeAmount === feeAmount && record.taxAmount === taxAmount) {
database.prepare("update LotOccupancyFees" + database.prepare("update LotOccupancyFees" +
" set quantity = quantity + ?," + " set quantity = quantity + ?," +
" feeAmount = feeAmount + ?," + " recordUpdate_userName = ?," +
" taxAmount = taxAmount + ?," + " recordUpdate_timeMillis = ?" +
" recordUpdate_userName = ?," + " where lotOccupancyId = ?" +
" recordUpdate_timeMillis = ?" + " and feeId = ?")
" where lotOccupancyId = ?" + .run(
" and feeId = ?") lotOccupancyFeeForm.quantity,
.run( requestSession.user.userName,
lotOccupancyFeeForm.quantity, rightNowMillis,
feeAmount, lotOccupancyFeeForm.lotOccupancyId,
taxAmount, lotOccupancyFeeForm.feeId);
requestSession.user.userName,
rightNowMillis,
lotOccupancyFeeForm.lotOccupancyId,
lotOccupancyFeeForm.feeId);
database.close(); database.close();
return false;
return true;
} else {
const quantity = typeof (lotOccupancyFeeForm.quantity) === "string" ?
Number.parseFloat(lotOccupancyFeeForm.quantity) :
lotOccupancyFeeForm.quantity;
database.prepare("update LotOccupancyFees" +
" set feeAmount = (feeAmount * quantity) + ?," +
" taxAmount = (taxAmount * quantity) + ?," +
" quantity = 1," +
" recordUpdate_userName = ?," +
" recordUpdate_timeMillis = ?" +
" where lotOccupancyId = ?" +
" and feeId = ?")
.run(
(feeAmount * quantity),
(taxAmount * quantity),
requestSession.user.userName,
rightNowMillis,
lotOccupancyFeeForm.lotOccupancyId,
lotOccupancyFeeForm.feeId);
database.close();
return true;
} }
} }
// Create new record // Create new record