synt_backend
Version:
Synt light-weight node backend service
117 lines (116 loc) • 3.33 kB
JavaScript
const { Model } = require("sequelize");
const MonthNames = [
"Jan",
"Feb",
"Maa",
"Apr",
"Mei",
"Jun",
"Jul",
"Aug",
"Sep",
"Okt",
"Nov",
"Dec",
];
module.exports = (sequelize, DataTypes) => {
class Provision 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
Provision.belongsTo(models.VME);
Provision.belongsTo(models.GeneralLedgerAccount);
Provision.belongsTo(models.Lot);
Provision.belongsTo(models.Journal);
Provision.belongsTo(models.PaymentCondition);
Provision.belongsTo(models.FinancialYear);
Provision.hasOne(models.ProvisionFile);
Provision.belongsToMany(models.User, { through: "ProvisionUser" });
}
}
Provision.init(
{
type: DataTypes.ENUM(
"guarantee_fund",
"working_capital",
"reserve_capital",
"exceptional_capital"
),
amount: DataTypes.DECIMAL(7, 2),
correction: DataTypes.DECIMAL(7, 2),
invoice_date: DataTypes.DATE,
due_date: DataTypes.DATE,
paid_at: DataTypes.DATE,
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)
);
},
},
is_cancelled: DataTypes.BOOLEAN,
status: {
type: DataTypes.VIRTUAL,
get() {
return this.getDataValue("paid_at") ? "Betaald" : "Openstaand";
},
},
reference: {
type: DataTypes.VIRTUAL,
get() {
const type = this.getDataValue("type");
switch (type) {
case "working_capital":
return `PWK${this.getDataValue("id")}`;
case "exceptional_capital":
return `UWK${this.getDataValue("id")}`;
case "reserve_capital":
return `RK${this.getDataValue("id")}`;
case "guarantee_fund":
return `WF${this.getDataValue("id")}`;
default:
return `P${this.getDataValue("id")}`;
}
},
},
description: DataTypes.STRING,
email_message: DataTypes.TEXT,
period: {
type: DataTypes.VIRTUAL,
get() {
const date = new Date(this.getDataValue("invoice_date"));
if (this.getDataValue("type") === "working_capital") {
return `${MonthNames[date.getMonth()]} J${date.getFullYear()}`;
}
if (
this.getDataValue("type") === "reserve_capital" ||
this.getDataValue("type") === "guarantee_fund"
) {
return `${date.getFullYear()}`;
} else {
return `${date.getDay() + 1} ${
MonthNames[date.getMonth()]
} J${date.getFullYear()}`;
}
},
},
},
{
sequelize,
modelName: "Provision",
}
);
return Provision;
};
;