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 @hasMany(() => History) declare histories: HasMany /** Kho hàng. */ @column() declare warehouse: string | null /** true nếu sản phẩm được sync từ ERP. */ get isSynced() { return this.erpId !== null && this.erpId !== undefined } }