synt_backend
Version:
Synt light-weight node backend service
67 lines (66 loc) • 1.99 kB
JavaScript
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class Purchase extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
Purchase.belongsTo(models.VME);
Purchase.belongsTo(models.Journal);
Purchase.belongsTo(models.Supplier);
Purchase.belongsToMany(models.User, { through: "PurchaseUser" });
Purchase.hasMany(models.PurchaseEntry);
Purchase.hasMany(models.PurchaseFile);
}
}
Purchase.init(
{
total_amount: DataTypes.DECIMAL,
vat_amount: DataTypes.DECIMAL,
description: DataTypes.STRING,
invoice_date: DataTypes.DATE,
due_date: DataTypes.DATE,
paid_at: DataTypes.DATE,
is_expense: DataTypes.BOOLEAN,
reference: {
type: DataTypes.VIRTUAL,
get() {
const id = this.getDataValue("id");
const date = new Date(this.getDataValue("invoice_date"));
return `AF${id}M${date.getMonth() + 1}J${date.getFullYear()}`;
},
},
period: {
type: DataTypes.VIRTUAL,
get() {
const date = new Date(this.getDataValue("invoice_date"));
return `M${date.getMonth() + 1} Y${date.getFullYear()}`;
},
},
payment: {
type: DataTypes.TEXT,
get() {
return typeof this.getDataValue("payment") === "string"
? JSON.parse(this.getDataValue("payment"))
: this.getDataValue("payment");
},
set(value) {
this.setDataValue(
"payment",
typeof value === "string" ? value : JSON.stringify(value)
);
},
},
statement: DataTypes.STRING,
},
{
sequelize,
modelName: "Purchase",
}
);
return Purchase;
};
;