'use strict';
const { Model } = require('sequelize');
const { defaultKeys, modelDefaults, actionUsers } = require('../sequelize/defaults');
module.exports = (sequelize, DataTypes) => {
  class Collection extends Model {
    static associate(models) {
      actionUsers(Collection, models);
      Collection.belongsTo(models.Branch, {
        targetKey: "branch_id",
        foreignKey: "branch_id",
        as: "branches",
      });
      Collection.belongsTo(models.Order, {
        foreignKey: "order_id",
        targetKey: "order_id",
        as: "order",
      });
      Collection.belongsTo(models.Area, {
        targetKey: "area_id",
        foreignKey: "area_id",
        as: "area",
      });
    }
  }

  Collection.init({
    ...defaultKeys("collection_id"),
    tbl_ref_id: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    collection_date: {
      type: DataTypes.DATEONLY,
      allowNull: false,
    },
    payment_type: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: 'collection',
    },
    payment_mode: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    amount: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
    },
  }, modelDefaults(sequelize, 'collections'));
  return Collection;
};