Config is not properly working with .env file as of now Cost Centre is mostly working, going to work on Accounts now
180 lines
5.5 KiB
Python
180 lines
5.5 KiB
Python
import datetime
|
|
import uuid
|
|
from decimal import Decimal
|
|
|
|
from sqlalchemy import or_, func
|
|
|
|
from brewman.models.master import Employee, AttendanceType, Account
|
|
from brewman.models.operations import journals_valid
|
|
from brewman.models.voucher import (
|
|
Journal,
|
|
Voucher,
|
|
VoucherType,
|
|
Attendance,
|
|
ServiceCharge,
|
|
)
|
|
from brewman.routers.services.session import get_first_day
|
|
|
|
|
|
def service_charge_create_voucher(json, user, dbsession):
|
|
date = datetime.datetime.strptime(json["date"], "%d-%b-%Y")
|
|
|
|
voucher = Voucher(date=date, user_id=user.id, type=VoucherType.by_id(13))
|
|
dbsession.add(voucher)
|
|
|
|
employees = get_employees(
|
|
get_first_day(date),
|
|
date + datetime.timedelta(1),
|
|
json["incentives"],
|
|
dbsession=dbsession,
|
|
)
|
|
amount = balance(date, dbsession=dbsession) * Decimal(0.9)
|
|
|
|
total_points = 0
|
|
for item in employees:
|
|
total_points += item["points"] * item["daysWorked"]
|
|
point_value = round(amount / total_points, 2)
|
|
total_amount = 0
|
|
|
|
for item in employees:
|
|
item_amount = round(item["points"] * item["daysWorked"] * point_value)
|
|
employee = item["employee"]
|
|
journal = Journal(
|
|
amount=item_amount,
|
|
debit=-1,
|
|
account_id=employee.id,
|
|
cost_centre_id=employee.cost_centre_id,
|
|
)
|
|
sc = ServiceCharge(
|
|
journal=journal, days_worked=item["daysWorked"], points=item["points"]
|
|
)
|
|
voucher.journals.append(journal)
|
|
voucher.service_charges.append(sc)
|
|
dbsession.add(journal)
|
|
dbsession.add(sc)
|
|
total_amount += item_amount
|
|
|
|
sc = (
|
|
dbsession.query(Account)
|
|
.filter(Account.id == Account.service_charge_id())
|
|
.first()
|
|
)
|
|
journal = Journal(
|
|
amount=total_amount, debit=1, account_id=sc.id, cost_centre_id=sc.cost_centre_id
|
|
)
|
|
voucher.journals.append(journal)
|
|
dbsession.add(journal)
|
|
journals_valid(voucher)
|
|
return voucher
|
|
|
|
|
|
def service_charge_update_voucher(voucher, json, user, dbsession):
|
|
date = datetime.datetime.strptime(json["date"], "%d-%b-%Y")
|
|
voucher.date = date
|
|
voucher.user_id = user.id
|
|
voucher.posted = False
|
|
voucher.last_edit_date = datetime.datetime.utcnow()
|
|
|
|
employees = get_employees(
|
|
get_first_day(date),
|
|
date + datetime.timedelta(1),
|
|
json["incentives"],
|
|
voucher.journals,
|
|
dbsession=dbsession,
|
|
)
|
|
amount = balance(date, voucher.id, dbsession) * Decimal(0.9)
|
|
|
|
total_points = 0
|
|
for item in employees:
|
|
total_points += item["points"] * item["daysWorked"]
|
|
point_value = round(amount / total_points, 2)
|
|
total_amount = 0
|
|
|
|
for item in voucher.service_charges:
|
|
employee = [
|
|
e for e in employees if e["employee"].id == item.journal.account_id
|
|
][0]
|
|
item_amount = round(employee["points"] * employee["daysWorked"] * point_value)
|
|
item.days_worked = employee["daysWorked"]
|
|
item.points = employee["points"]
|
|
item.journal.amount = item_amount
|
|
total_amount += item_amount
|
|
|
|
journal = [
|
|
j for j in voucher.journals if j.account_id == Account.service_charge_id()
|
|
][0]
|
|
journal.amount = total_amount
|
|
|
|
journals_valid(voucher)
|
|
return voucher
|
|
|
|
|
|
def get_employees(
|
|
start_date, finish_date, json, voucher_employees=None, dbsession=None
|
|
):
|
|
details = []
|
|
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()
|
|
)
|
|
|
|
check_if_employees_changed(json, employees, voucher_employees)
|
|
|
|
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))
|
|
details.append({"employee": employee, "daysWorked": round(Decimal(att), 2)})
|
|
for item in details:
|
|
j = [x for x in json if uuid.UUID(x["id"]) == item["employee"].id][0]
|
|
item["points"] = round(Decimal(j["points"]), 2)
|
|
json.remove(j)
|
|
return details
|
|
|
|
|
|
def balance(date, voucher_id=None, dbsession=None):
|
|
amount = (
|
|
dbsession.query(func.sum(Journal.amount * Journal.debit))
|
|
.join(Journal.voucher)
|
|
.filter(Voucher.date < date + datetime.timedelta(1))
|
|
.filter(Voucher.type != VoucherType.by_name("Issue").id)
|
|
.filter(Journal.account_id == Account.service_charge_id())
|
|
)
|
|
if voucher_id is not None:
|
|
amount = amount.filter(Voucher.id != voucher_id)
|
|
amount = amount.scalar()
|
|
return 0 if amount is None else amount * -1
|
|
|
|
|
|
def check_if_employees_changed(json, db, voucher):
|
|
json = set([uuid.UUID(x["id"]) for x in json])
|
|
db = set([x.id for x in db])
|
|
voucher = (
|
|
set(
|
|
[
|
|
x.account_id
|
|
for x in voucher
|
|
if x.account_id != Account.service_charge_id()
|
|
]
|
|
)
|
|
if voucher is not None
|
|
else None
|
|
)
|
|
if voucher is None:
|
|
if len(json ^ db) != 0:
|
|
raise ValueError("Employee missing in json data")
|
|
else:
|
|
if len(json ^ db) != 0 or len(db ^ voucher) != 0:
|
|
raise ValueError("Employee missing in json data")
|