Config is not properly working with .env file as of now Cost Centre is mostly working, going to work on Accounts now
88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
import calendar
|
|
import datetime
|
|
import uuid
|
|
|
|
from sqlalchemy import or_
|
|
from ....models.auth import User
|
|
from ....models.master import Employee, AttendanceType, Account
|
|
from ....models.voucher import Voucher, VoucherType, Attendance, Journal
|
|
from ..session import get_first_day, get_last_day
|
|
|
|
|
|
@router.post("/api/credit-salary") # "Attendance"
|
|
def credit_salary(request):
|
|
user = (
|
|
request.dbsession.query(User)
|
|
.filter(User.id == uuid.UUID(request.authenticated_userid))
|
|
.one()
|
|
)
|
|
month = datetime.datetime.strptime(request.json_body["month"], "%d-%b-%Y")
|
|
|
|
start_date = get_first_day(month)
|
|
finish_date = get_last_day(month)
|
|
voucher = Voucher(
|
|
date=finish_date,
|
|
narration="Auto Generated Salary Entry",
|
|
user_id=user.id,
|
|
type=VoucherType.by_name("Journal"),
|
|
posted=True,
|
|
poster_id=user.id,
|
|
)
|
|
request.dbsession.add(voucher)
|
|
for item in salary_journals(start_date, finish_date, request.dbsession):
|
|
voucher.journals.append(item)
|
|
request.dbsession.add(item)
|
|
transaction.commit()
|
|
return {"message": "Salary Entry created"}
|
|
|
|
|
|
def salary_journals(start_date, finish_date, dbsession):
|
|
days = calendar.monthrange(start_date.year, start_date.month)[1]
|
|
finish_date = finish_date + datetime.timedelta(1)
|
|
amount = 0
|
|
journals = []
|
|
employees = (
|
|
dbsession.query(Employee)
|
|
.filter(Employee.joining_date <= finish_date)
|
|
.filter(or_(Employee.is_active, Employee.leaving_date >= start_date))
|
|
.order_by(Employee.cost_centre_id)
|
|
.order_by(Employee.designation)
|
|
.order_by(Employee.name)
|
|
.all()
|
|
)
|
|
for employee in employees:
|
|
att = (
|
|
dbsession.query(Attendance)
|
|
.filter(Attendance.employee_id == employee.id)
|
|
.filter(Attendance.date >= start_date)
|
|
.filter(Attendance.date < finish_date)
|
|
.filter(Attendance.is_valid == True)
|
|
.all()
|
|
)
|
|
att = sum(map(lambda x: AttendanceType.by_id(x.attendance_type).value, att))
|
|
att = round(att * employee.salary / days)
|
|
if att != 0:
|
|
amount += att
|
|
journals.append(
|
|
Journal(
|
|
amount=att,
|
|
debit=-1,
|
|
account_id=employee.id,
|
|
cost_centre_id=employee.cost_centre_id,
|
|
)
|
|
)
|
|
salary = (
|
|
dbsession.query(Account)
|
|
.filter(Account.id == uuid.UUID(Account.salary()["id"]))
|
|
.first()
|
|
)
|
|
journals.append(
|
|
Journal(
|
|
amount=amount,
|
|
debit=1,
|
|
account_id=salary.id,
|
|
cost_centre_id=salary.cost_centre_id,
|
|
)
|
|
)
|
|
return journals
|