Skip to content

Commit

Permalink
ability to add multiple fees as a group
Browse files Browse the repository at this point in the history
alter table FeeCategories
add isGroupedFee bit not null default 0
  • Loading branch information
dangowans committed Jun 28, 2024
1 parent 86b82d8 commit f384ce7
Show file tree
Hide file tree
Showing 42 changed files with 687 additions and 231 deletions.
6 changes: 6 additions & 0 deletions database/addFeeCategory.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface AddFeeCategoryForm {
feeCategory: string;
isGroupedFee?: '1';
orderNumber?: number;
}
export default function addFeeCategory(feeCategoryForm: AddFeeCategoryForm, user: User): Promise<number>;
15 changes: 15 additions & 0 deletions database/addFeeCategory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { acquireConnection } from './pool.js';
export default async function addFeeCategory(feeCategoryForm, user) {
const database = await acquireConnection();
const rightNowMillis = Date.now();
const result = database
.prepare(`insert into FeeCategories (
feeCategory,
isGroupedFee, orderNumber,
recordCreate_userName, recordCreate_timeMillis,
recordUpdate_userName, recordUpdate_timeMillis)
values (?, ?, ?, ?, ?, ?, ?)`)
.run(feeCategoryForm.feeCategory, (feeCategoryForm.isGroupedFee ?? '') === '1' ? 1 : 0, feeCategoryForm.orderNumber ?? -1, user.userName, rightNowMillis, user.userName, rightNowMillis);
database.release();
return result.lastInsertRowid;
}
39 changes: 39 additions & 0 deletions database/addFeeCategory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { acquireConnection } from './pool.js'

export interface AddFeeCategoryForm {
feeCategory: string
isGroupedFee?: '1'
orderNumber?: number
}

export default async function addFeeCategory(
feeCategoryForm: AddFeeCategoryForm,
user: User
): Promise<number> {
const database = await acquireConnection()

const rightNowMillis = Date.now()

const result = database
.prepare(
`insert into FeeCategories (
feeCategory,
isGroupedFee, orderNumber,
recordCreate_userName, recordCreate_timeMillis,
recordUpdate_userName, recordUpdate_timeMillis)
values (?, ?, ?, ?, ?, ?, ?)`
)
.run(
feeCategoryForm.feeCategory,
(feeCategoryForm.isGroupedFee ?? '') === '1' ? 1 : 0,
feeCategoryForm.orderNumber ?? -1,
user.userName,
rightNowMillis,
user.userName,
rightNowMillis
)

database.release()

return result.lastInsertRowid as number
}
3 changes: 2 additions & 1 deletion database/addLotOccupancyFee.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { type PoolConnection } from 'better-sqlite-pool';
export interface AddLotOccupancyFeeForm {
lotOccupancyId: number | string;
feeId: number | string;
quantity: number | string;
feeAmount?: number | string;
taxAmount?: number | string;
}
export default function addLotOccupancyFee(lotOccupancyFeeForm: AddLotOccupancyFeeForm, user: User): Promise<boolean>;
export default function addLotOccupancyFee(lotOccupancyFeeForm: AddLotOccupancyFeeForm, user: User, connectedDatabase?: PoolConnection): Promise<boolean>;
116 changes: 60 additions & 56 deletions database/addLotOccupancyFee.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { calculateFeeAmount, calculateTaxAmount } from '../helpers/functions.fee
import getFee from './getFee.js';
import getLotOccupancy from './getLotOccupancy.js';
import { acquireConnection } from './pool.js';
export default async function addLotOccupancyFee(lotOccupancyFeeForm, user) {
const database = await acquireConnection();
export default async function addLotOccupancyFee(lotOccupancyFeeForm, user, connectedDatabase) {
const database = connectedDatabase ?? (await acquireConnection());
const rightNowMillis = Date.now();
// Calculate fee and tax (if not set)
let feeAmount;
Expand All @@ -24,62 +24,66 @@ export default async function addLotOccupancyFee(lotOccupancyFeeForm, user) {
? Number.parseFloat(lotOccupancyFeeForm.taxAmount)
: 0;
}
// Check if record already exists
const record = database
.prepare(`select feeAmount, taxAmount, recordDelete_timeMillis
from LotOccupancyFees
where lotOccupancyId = ?
and feeId = ?`)
.get(lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId);
if (record) {
if (record.recordDelete_timeMillis) {
database
.prepare(`delete from LotOccupancyFees
where recordDelete_timeMillis is not null
and lotOccupancyId = ?
and feeId = ?`)
.run(lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId);
try {
// Check if record already exists
const record = database
.prepare(`select feeAmount, taxAmount, recordDelete_timeMillis
from LotOccupancyFees
where lotOccupancyId = ?
and feeId = ?`)
.get(lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId);
if (record) {
if (record.recordDelete_timeMillis) {
database
.prepare(`delete from LotOccupancyFees
where recordDelete_timeMillis is not null
and lotOccupancyId = ?
and feeId = ?`)
.run(lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId);
}
else if (record.feeAmount === feeAmount &&
record.taxAmount === taxAmount) {
database
.prepare(`update LotOccupancyFees
set quantity = quantity + ?,
recordUpdate_userName = ?,
recordUpdate_timeMillis = ?
where lotOccupancyId = ?
and feeId = ?`)
.run(lotOccupancyFeeForm.quantity, user.userName, rightNowMillis, lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId);
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, user.userName, rightNowMillis, lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId);
return true;
}
}
else if (record.feeAmount === feeAmount &&
record.taxAmount === taxAmount) {
database
.prepare(`update LotOccupancyFees
set quantity = quantity + ?,
recordUpdate_userName = ?,
recordUpdate_timeMillis = ?
where lotOccupancyId = ?
and feeId = ?`)
.run(lotOccupancyFeeForm.quantity, user.userName, rightNowMillis, lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId);
database.release();
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, user.userName, rightNowMillis, lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId);
// Create new record
const result = database
.prepare(`insert into LotOccupancyFees (
lotOccupancyId, feeId,
quantity, feeAmount, taxAmount,
recordCreate_userName, recordCreate_timeMillis,
recordUpdate_userName, recordUpdate_timeMillis)
values (?, ?, ?, ?, ?, ?, ?, ?, ?)`)
.run(lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId, lotOccupancyFeeForm.quantity, feeAmount, taxAmount, user.userName, rightNowMillis, user.userName, rightNowMillis);
return result.changes > 0;
}
finally {
if (connectedDatabase === undefined) {
database.release();
return true;
}
}
// Create new record
const result = database
.prepare(`insert into LotOccupancyFees (
lotOccupancyId, feeId,
quantity, feeAmount, taxAmount,
recordCreate_userName, recordCreate_timeMillis,
recordUpdate_userName, recordUpdate_timeMillis)
values (?, ?, ?, ?, ?, ?, ?, ?, ?)`)
.run(lotOccupancyFeeForm.lotOccupancyId, lotOccupancyFeeForm.feeId, lotOccupancyFeeForm.quantity, feeAmount, taxAmount, user.userName, rightNowMillis, user.userName, rightNowMillis);
database.release();
return result.changes > 0;
}
Loading

0 comments on commit f384ce7

Please sign in to comment.