feat(statistics): implement top alert stocks, top last sales, top supplier debts, and top selling products endpoints with SQL queries
This commit is contained in:
@@ -28,6 +28,35 @@ model BankAccount {
|
||||
branch BankBranch @relation("Bank_Accounts_branchId_fkey", fields: [branchId], references: [id])
|
||||
inventoryBankAccounts InventoryBankAccount[]
|
||||
purchaseReceiptPayments PurchaseReceiptPayments[]
|
||||
bankAccountTransactions BankAccountTransaction[]
|
||||
bankAccountBalances BankAccountBalance[]
|
||||
|
||||
@@map("Bank_Accounts")
|
||||
}
|
||||
|
||||
model BankAccountTransaction {
|
||||
id Int @id @default(autoincrement())
|
||||
bankAccountId Int
|
||||
type BankAccountTransactionType
|
||||
amount Decimal @db.Decimal(15, 2)
|
||||
balanceAfter Decimal @db.Decimal(15, 2)
|
||||
referenceId Int
|
||||
referenceType BankTransactionRefType
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
|
||||
bankAccount BankAccount @relation(fields: [bankAccountId], references: [id])
|
||||
|
||||
@@index([bankAccountId])
|
||||
@@map("Bank_Account_Transactions")
|
||||
}
|
||||
|
||||
model BankAccountBalance {
|
||||
bankAccountId Int @unique
|
||||
balance Decimal @db.Decimal(15, 2)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
|
||||
bankAccount BankAccount @relation(fields: [bankAccountId], references: [id])
|
||||
|
||||
@@map("Bank_Account_Balance")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
enum OrderStatus {
|
||||
PENDING
|
||||
REJECT
|
||||
REJECTED
|
||||
CANCELED
|
||||
DONE
|
||||
}
|
||||
|
||||
@@ -42,3 +43,17 @@ enum PurchaseReceiptStatus {
|
||||
PARTIALLY_PAID
|
||||
PAID
|
||||
}
|
||||
|
||||
enum BankAccountTransactionType {
|
||||
DEPOSIT
|
||||
WITHDRAWAL
|
||||
}
|
||||
|
||||
enum BankTransactionRefType {
|
||||
PURCHASE_PAYMENT
|
||||
PURCHASE_REFUND
|
||||
POS_SALE
|
||||
POS_REFUND
|
||||
BANK_TRANSFER
|
||||
MANUAL_ADJUSTMENT
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ model Inventory {
|
||||
counterStockMovements StockMovement[] @relation("StockMovement_CounterInventory")
|
||||
stockMovements StockMovement[] @relation("StockMovement_Inventory")
|
||||
inventoryBankAccounts InventoryBankAccount[]
|
||||
stockReservations StockReservation[]
|
||||
|
||||
@@map("Inventories")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
model Order {
|
||||
id Int @id @default(autoincrement())
|
||||
orderNumber String @unique @db.VarChar(100)
|
||||
status OrderStatus @default(PENDING)
|
||||
totalAmount Decimal @db.Decimal(15, 2)
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
customerId Int?
|
||||
customer Customer? @relation(fields: [customerId], references: [id], onUpdate: NoAction)
|
||||
orderItems OrderItem[]
|
||||
|
||||
@@index([customerId])
|
||||
@@map("Orders")
|
||||
}
|
||||
|
||||
model OrderItem {
|
||||
id Int @id @default(autoincrement())
|
||||
quantity Decimal @db.Decimal(10, 0)
|
||||
unitPrice Decimal @db.Decimal(15, 2)
|
||||
totalAmount Decimal @db.Decimal(15, 2)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
orderId Int
|
||||
productId Int
|
||||
|
||||
order Order @relation(fields: [orderId], references: [id], onUpdate: NoAction)
|
||||
product Product @relation(fields: [productId], references: [id], onUpdate: NoAction)
|
||||
|
||||
@@index([orderId])
|
||||
@@index([productId])
|
||||
@@map("Order_Items")
|
||||
}
|
||||
@@ -19,57 +19,6 @@ model Customer {
|
||||
@@map("Customers")
|
||||
}
|
||||
|
||||
model Order {
|
||||
id Int @id @default(autoincrement())
|
||||
orderNumber String @unique @db.VarChar(100)
|
||||
status OrderStatus @default(PENDING)
|
||||
paymentMethod PaymentMethodType @default(CARD)
|
||||
totalAmount Decimal @db.Decimal(15, 2)
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
deletedAt DateTime? @db.Timestamp(0)
|
||||
customerId Int
|
||||
customer Customer @relation(fields: [customerId], references: [id], onUpdate: NoAction)
|
||||
|
||||
@@index([customerId])
|
||||
@@map("Orders")
|
||||
}
|
||||
|
||||
model SalesInvoice {
|
||||
id Int @id @default(autoincrement())
|
||||
code String @unique @db.VarChar(100)
|
||||
totalAmount Decimal @db.Decimal(15, 2)
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
customerId Int?
|
||||
posAccountId Int
|
||||
items SalesInvoiceItem[]
|
||||
customer Customer? @relation(fields: [customerId], references: [id])
|
||||
posAccount PosAccount @relation(fields: [posAccountId], references: [id])
|
||||
|
||||
@@index([customerId])
|
||||
@@index([posAccountId])
|
||||
@@map("Sales_Invoices")
|
||||
}
|
||||
|
||||
model SalesInvoiceItem {
|
||||
id Int @id @default(autoincrement())
|
||||
count Decimal @db.Decimal(10, 0)
|
||||
fee Decimal @db.Decimal(15, 2)
|
||||
total Decimal @db.Decimal(15, 2)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
invoiceId Int
|
||||
productId Int
|
||||
invoice SalesInvoice @relation(fields: [invoiceId], references: [id])
|
||||
product Product @relation(fields: [productId], references: [id])
|
||||
|
||||
@@index([invoiceId])
|
||||
@@index([productId])
|
||||
@@map("Sales_Invoice_Items")
|
||||
}
|
||||
|
||||
model TriggerLog {
|
||||
id Int @id @default(autoincrement())
|
||||
message String @db.Text
|
||||
|
||||
@@ -43,6 +43,8 @@ model Product {
|
||||
stockBalances StockBalance[] @relation("StockBalance_Product")
|
||||
stockMovements StockMovement[] @relation("StockMovement_Product")
|
||||
salesInvoiceItems SalesInvoiceItem[]
|
||||
stockReservations StockReservation[]
|
||||
orderItems OrderItem[]
|
||||
|
||||
@@index([brandId], map: "Products_brandId_fkey")
|
||||
@@index([categoryId], map: "Products_categoryId_fkey")
|
||||
|
||||
@@ -22,8 +22,8 @@ model PurchaseReceipt {
|
||||
model PurchaseReceiptItem {
|
||||
id Int @id @default(autoincrement())
|
||||
count Decimal @db.Decimal(10, 0)
|
||||
fee Decimal @db.Decimal(15, 2)
|
||||
total Decimal @db.Decimal(15, 2)
|
||||
unitPrice Decimal @db.Decimal(15, 2)
|
||||
totalAmount Decimal @db.Decimal(15, 2)
|
||||
receiptId Int
|
||||
productId Int
|
||||
description String? @db.Text
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
model SalesInvoice {
|
||||
id Int @id @default(autoincrement())
|
||||
code String @unique @db.VarChar(100)
|
||||
totalAmount Decimal @db.Decimal(15, 2)
|
||||
description String? @db.Text
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
updatedAt DateTime @updatedAt @db.Timestamp(0)
|
||||
customerId Int?
|
||||
posAccountId Int
|
||||
customer Customer? @relation(fields: [customerId], references: [id])
|
||||
posAccount PosAccount @relation(fields: [posAccountId], references: [id])
|
||||
items SalesInvoiceItem[]
|
||||
salesInvoicePayments SalesInvoicePayment[]
|
||||
|
||||
@@index([customerId])
|
||||
@@index([posAccountId])
|
||||
@@map("Sales_Invoices")
|
||||
}
|
||||
|
||||
model SalesInvoiceItem {
|
||||
id Int @id @default(autoincrement())
|
||||
count Decimal @db.Decimal(10, 0)
|
||||
unitPrice Decimal @default(0.00) @db.Decimal(15, 2)
|
||||
totalAmount Decimal @default(0.00) @db.Decimal(15, 2)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
invoiceId Int
|
||||
productId Int
|
||||
invoice SalesInvoice @relation(fields: [invoiceId], references: [id])
|
||||
product Product @relation(fields: [productId], references: [id])
|
||||
|
||||
@@index([invoiceId])
|
||||
@@index([productId])
|
||||
@@map("Sales_Invoice_Items")
|
||||
}
|
||||
|
||||
model SalesInvoicePayment {
|
||||
id Int @id @default(autoincrement())
|
||||
invoiceId Int
|
||||
amount Decimal @db.Decimal(15, 2)
|
||||
paymentMethod PaymentMethodType
|
||||
paidAt DateTime
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
invoice SalesInvoice @relation(fields: [invoiceId], references: [id])
|
||||
|
||||
@@index([invoiceId])
|
||||
@@map("Sales_Invoice_Payments")
|
||||
}
|
||||
@@ -2,7 +2,7 @@ model StockMovement {
|
||||
id Int @id @default(autoincrement())
|
||||
type MovementType
|
||||
quantity Decimal @db.Decimal(10, 0)
|
||||
fee Decimal @db.Decimal(15, 2)
|
||||
unitPrice Decimal @db.Decimal(15, 2)
|
||||
totalCost Decimal @db.Decimal(15, 2)
|
||||
referenceType MovementReferenceType
|
||||
referenceId String
|
||||
@@ -59,3 +59,19 @@ model StockAdjustment {
|
||||
@@index([productId], map: "Stock_Adjustments_productId_fkey")
|
||||
@@map("Stock_Adjustments")
|
||||
}
|
||||
|
||||
model StockReservation {
|
||||
id Int @id @default(autoincrement())
|
||||
quantity Decimal @db.Decimal(10, 0)
|
||||
expiresAt DateTime @db.Timestamp(0)
|
||||
createdAt DateTime @default(now()) @db.Timestamp(0)
|
||||
productId Int
|
||||
inventoryId Int
|
||||
orderId Int
|
||||
inventory Inventory @relation(fields: [inventoryId], references: [id])
|
||||
product Product @relation(fields: [productId], references: [id])
|
||||
|
||||
@@index([inventoryId])
|
||||
@@index([productId])
|
||||
@@map("Stock_Reservations")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user