Listing_SuggestPrice/backend/app/models/product.ts

77 lines
2.0 KiB
TypeScript

import { DateTime } from 'luxon'
import { BaseModel, column, hasMany } from '@adonisjs/lucid/orm'
import type { HasMany } from '@adonisjs/lucid/types/relations'
import Log from '#models/log'
import History from '#models/history'
/**
* Một product có thể đến từ ERP (sync) hoặc nhập thủ công.
* - erpId != null -> sản phẩm sync từ ERP
* - erpId == null -> sản phẩm manual / import excel
*/
export default class Product extends BaseModel {
@column({ isPrimary: true })
declare id: number
@column()
declare sku: string
@column()
declare condition: string
@column()
declare qty: number
/** Giá bán hiện tại (đang áp dụng). */
@column()
declare price: number
/** Giá do AI gợi ý gần nhất (có thể chưa được áp dụng). */
@column()
declare aiPrice: number | null
/** Giá vốn / chi phí theo nhiều currency. */
@column({
prepare: (value) => (value == null ? null : JSON.stringify(value)),
consume: (value) => (typeof value === 'string' ? JSON.parse(value) : value),
})
declare costs: Array<{ currency: string; price: number }> | null
/** Số lượng đơn vị trong 1 package. */
@column()
declare packageContain: string | null
/** Loại / nhóm sản phẩm. */
@column()
declare type: string | null
/** ID tham chiếu bên ERP (null = manual). */
@column()
declare erpId: string | null
@column.dateTime({ autoCreate: true })
declare createdAt: DateTime
@column.dateTime({ autoCreate: true, autoUpdate: true })
declare updatedAt: DateTime
@hasMany(() => Log)
declare logs: HasMany<typeof Log>
@hasMany(() => History)
declare histories: HasMany<typeof History>
/** Kho hàng. */
@column()
declare warehouse: string | null
/** true = sản phẩm rác / không cần gợi ý giá (bỏ qua khi chạy gợi ý giá). */
@column()
declare noListing: boolean
/** true nếu sản phẩm được sync từ ERP. */
get isSynced() {
return this.erpId !== null && this.erpId !== undefined
}
}