const { DataTypes } = require('sequelize'); const sequelize = require('../config/db'); const Rental = require('./Rental'); const Room = require('./Room'); const Renter = require('./Renter'); const Bill = sequelize.define('Bill', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, comment: '账单ID' }, billNo: { type: DataTypes.STRING(50), allowNull: false, unique: true, comment: '账单编号' }, roomId: { type: DataTypes.INTEGER, allowNull: true, references: { model: Room, key: 'id' }, comment: '房间ID(可选)' }, renterId: { type: DataTypes.INTEGER, allowNull: true, references: { model: Renter, key: 'id' }, comment: '租客ID(可选)' }, rentalId: { type: DataTypes.INTEGER, allowNull: true, references: { model: Rental, key: 'id' }, comment: '租赁ID(可选)' }, type: { type: DataTypes.ENUM('income', 'expense'), allowNull: false, comment: '账单类型(income:收入,expense:支出)' }, category: { type: DataTypes.STRING(50), allowNull: false, comment: '分类(rent-租金, water-水费, electricity-电费, gas-燃气费, deposit-押金, maintenance-维修费, property_fee-物业费, agency_fee-中介费, penalty-违约金, other_income-其他收入, other_expense-其他支出)' }, receivableAmount: { type: DataTypes.DECIMAL(10, 2), allowNull: false, comment: '应收金额' }, receivedAmount: { type: DataTypes.DECIMAL(10, 2), allowNull: false, defaultValue: 0, comment: '已收金额' }, status: { type: DataTypes.ENUM('unpaid', 'partial', 'paid', 'cancelled'), allowNull: false, defaultValue: 'unpaid', comment: '状态(unpaid-未收, partial-部分收款, paid-已收清, cancelled-已取消)' }, billMonth: { type: DataTypes.STRING(7), allowNull: true, comment: '账单月份(格式:YYYY-MM)' }, billDate: { type: DataTypes.DATEONLY, allowNull: false, comment: '账单日期' }, sourceType: { type: DataTypes.STRING(50), allowNull: true, comment: '来源类型(meter_reading-抄表记录)' }, sourceId: { type: DataTypes.INTEGER, allowNull: true, comment: '来源ID' }, paymentMethod: { type: DataTypes.STRING(50), allowNull: true, comment: '支付方式(cash-现金, bank_transfer-银行转账, alipay-支付宝, wechat-微信支付)' }, remark: { type: DataTypes.TEXT, allowNull: true, comment: '备注' }, createBy: { type: DataTypes.INTEGER, allowNull: true, comment: '创建人ID' }, createTime: { type: DataTypes.DATE, defaultValue: DataTypes.NOW, comment: '创建时间' }, updateBy: { type: DataTypes.INTEGER, allowNull: true, comment: '修改人ID' }, updateTime: { type: DataTypes.DATE, defaultValue: DataTypes.NOW, onUpdate: DataTypes.NOW, comment: '更新时间' }, tenantId: { type: DataTypes.INTEGER, allowNull: false, comment: '租户ID' }, isDeleted: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: '删除状态(0:未删除,1:已删除)' } }, { tableName: 'bills', timestamps: false }); // 建立关联 Bill.belongsTo(Room, { foreignKey: 'roomId' }); Bill.belongsTo(Renter, { foreignKey: 'renterId' }); Bill.belongsTo(Rental, { foreignKey: 'rentalId' }); module.exports = Bill;