added extra query
This commit is contained in:
+98
-98
@@ -1,98 +1,98 @@
|
||||
from app.extensions import db
|
||||
from datetime import datetime, timezone
|
||||
from app.utils.logger import logger
|
||||
|
||||
class Salary(db.Model):
|
||||
__tablename__ = "salaries"
|
||||
|
||||
id = db.Column(
|
||||
db.Integer,
|
||||
primary_key=True,
|
||||
autoincrement=True,
|
||||
)
|
||||
customer_id = db.Column(db.String(50), nullable=False)
|
||||
account_id = db.Column(db.String(50), nullable=False)
|
||||
amount = db.Column(db.Float, nullable=True, default=0.0)
|
||||
status = db.Column(db.String(20), nullable=True)
|
||||
created_at = db.Column(db.DateTime, default=datetime.now)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
|
||||
salary_date = db.Column(db.DateTime, nullable=True)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Salary {self.id}>'
|
||||
|
||||
def to_dict(self):
|
||||
"""
|
||||
Convert the Salary object to a dictionary format for JSON serialization.
|
||||
"""
|
||||
return {
|
||||
'id': self.id,
|
||||
'customerId': self.customer_id,
|
||||
'accountId' : self.account_id,
|
||||
'salaryAmount': self.amount,
|
||||
'status': self.status,
|
||||
'createdAt': self.created_at.isoformat() if self.created_at else None,
|
||||
'updatedAt': self.updated_at.isoformat() if self.updated_at else None,
|
||||
'salaryDate': self.salary_date.isoformat() if self.salary_date else None,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def add_salary_data(cls, data):
|
||||
"""
|
||||
Add a new salary data entry.
|
||||
"""
|
||||
logger.info(f"Received data:{data}")
|
||||
try:
|
||||
new_data = cls(
|
||||
customer_id=data.get('customerId'),
|
||||
amount=data.get('salaryAmount', 0.0),
|
||||
status='START',
|
||||
salary_date = datetime.strptime(data.get('salaryDate'), "%Y-%m-%d").date() if data.get('salaryDate') else None,
|
||||
account_id=data.get('accountId')
|
||||
)
|
||||
db.session.add(new_data)
|
||||
db.session.commit()
|
||||
logger.info("Salary data has been committed.")
|
||||
return new_data
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
logger.info(f"error : {str(e)}")
|
||||
raise Exception(f"Error adding salary data: {str(e)}")
|
||||
|
||||
@classmethod
|
||||
def get_pending_salaries(cls):
|
||||
"""
|
||||
Retrieve all salary entries with status 'START', ordered by ID ascending.
|
||||
"""
|
||||
try:
|
||||
return cls.query.filter_by(status='START').order_by(cls.id.asc()).all()
|
||||
except Exception as e:
|
||||
logger.error(f"Error fetching pending salaries: {str(e)}")
|
||||
return []
|
||||
@classmethod
|
||||
def update_status(cls, salary_id, status):
|
||||
"""
|
||||
Update the status of the salary record with the given salary_id.
|
||||
"""
|
||||
try:
|
||||
# Retrieve salary record
|
||||
salary = cls.query.get(salary_id)
|
||||
|
||||
if not salary:
|
||||
raise ValueError(f"Salary with ID {salary_id} does not exist.")
|
||||
|
||||
if salary.status == status:
|
||||
return salary.to_dict() # Still return the current state if no change
|
||||
|
||||
# Update status and timestamp
|
||||
salary.status = status
|
||||
salary.updated_at = datetime.now(timezone.utc) # Manually update timestamp if not auto-updating
|
||||
db.session.commit()
|
||||
|
||||
logger.info("Salary status updated and committed.")
|
||||
return salary.to_dict()
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
logger.error(f"Error updating salary status: {e}")
|
||||
raise Exception(f"Error updating salary status: {str(e)}")
|
||||
from app.extensions import db
|
||||
from datetime import datetime, timezone
|
||||
from app.utils.logger import logger
|
||||
|
||||
class Salary(db.Model):
|
||||
__tablename__ = "salaries"
|
||||
|
||||
id = db.Column(
|
||||
db.Integer,
|
||||
primary_key=True,
|
||||
autoincrement=True,
|
||||
)
|
||||
customer_id = db.Column(db.String(50), nullable=False)
|
||||
account_id = db.Column(db.String(50), nullable=False)
|
||||
amount = db.Column(db.Float, nullable=True, default=0.0)
|
||||
status = db.Column(db.String(20), nullable=True)
|
||||
created_at = db.Column(db.DateTime, default=datetime.now)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
|
||||
salary_date = db.Column(db.DateTime, nullable=True)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Salary {self.id}>'
|
||||
|
||||
def to_dict(self):
|
||||
"""
|
||||
Convert the Salary object to a dictionary format for JSON serialization.
|
||||
"""
|
||||
return {
|
||||
'id': self.id,
|
||||
'customerId': self.customer_id,
|
||||
'accountId' : self.account_id,
|
||||
'salaryAmount': self.amount,
|
||||
'status': self.status,
|
||||
'createdAt': self.created_at.isoformat() if self.created_at else None,
|
||||
'updatedAt': self.updated_at.isoformat() if self.updated_at else None,
|
||||
'salaryDate': self.salary_date.isoformat() if self.salary_date else None,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def add_salary_data(cls, data):
|
||||
"""
|
||||
Add a new salary data entry.
|
||||
"""
|
||||
logger.info(f"Received data:{data}")
|
||||
try:
|
||||
new_data = cls(
|
||||
customer_id=data.get('customerId'),
|
||||
amount=data.get('salaryAmount', 0.0),
|
||||
status='START',
|
||||
salary_date = datetime.strptime(data.get('salaryDate'), "%Y-%m-%d").date() if data.get('salaryDate') else None,
|
||||
account_id=data.get('accountId')
|
||||
)
|
||||
db.session.add(new_data)
|
||||
db.session.commit()
|
||||
logger.info("Salary data has been committed.")
|
||||
return new_data
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
logger.info(f"error : {str(e)}")
|
||||
raise Exception(f"Error adding salary data: {str(e)}")
|
||||
|
||||
@classmethod
|
||||
def get_pending_salaries(cls):
|
||||
"""
|
||||
Retrieve all salary entries with status 'START', ordered by ID ascending.
|
||||
"""
|
||||
try:
|
||||
return cls.query.filter_by(status='START').order_by(cls.id.asc()).all()
|
||||
except Exception as e:
|
||||
logger.error(f"Error fetching pending salaries: {str(e)}")
|
||||
return []
|
||||
@classmethod
|
||||
def update_status(cls, salary_id, status):
|
||||
"""
|
||||
Update the status of the salary record with the given salary_id.
|
||||
"""
|
||||
try:
|
||||
# Retrieve salary record
|
||||
salary = cls.query.get(salary_id)
|
||||
|
||||
if not salary:
|
||||
raise ValueError(f"Salary with ID {salary_id} does not exist.")
|
||||
|
||||
if salary.status == status:
|
||||
return salary.to_dict() # Still return the current state if no change
|
||||
|
||||
# Update status and timestamp
|
||||
salary.status = status
|
||||
salary.updated_at = datetime.now(timezone.utc) # Manually update timestamp if not auto-updating
|
||||
db.session.commit()
|
||||
|
||||
logger.info("Salary status updated and committed.")
|
||||
return salary.to_dict()
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
logger.error(f"Error updating salary status: {e}")
|
||||
raise Exception(f"Error updating salary status: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user