import json
from datetime import datetime

from payrollservice.util.payrollutil import advancestatus, data_roundup, net_pay_calc, net_pay_calc_str


class EmployeeadvancedetailsResponse:
    id, employee_id, type, from_date, to_date, advance_status, actual_amount, payable_amount, reason,emi_amount, created_by, advance_amount,code = (None,)*13

    def get(self):
        return json.dumps(self, default=lambda o: o.__dict__,
                          sort_keys=True, indent=4)

    def set_id(self, id):
        self.id = id

    def set_employee_val(self, employee_id,arr):
        self.employee_data = next((i for i in arr if i.get('id') == employee_id), None)

    def set_type(self, type):
        self.type = type

    def set_from_date(self, from_date):
        from_date1 = datetime.strptime(str(from_date), "%Y-%m-%d")
        from_datem1 = str(from_date1.strftime("%Y-%m"))
        self.from_date = str(from_datem1)


    def set_to_date(self, to_date):
        to_date1 = datetime.strptime(str(to_date), "%Y-%m-%d")
        to_datem1 = str(to_date1.strftime("%Y-%m"))
        self.to_date = str(to_datem1)

    def set_advance_status(self, advance_status):
        self.advance_status = advancestatus(advance_status)

    def set_actual_amount(self, actual_amount):
        # self.actual_amount = str(actual_amount)
        self.actual_amount = data_roundup(actual_amount)

    def set_payable_amount(self, payable_amount):
        # self.payable_amount = str(payable_amount)
        self.payable_amount = data_roundup(payable_amount)

    def set_reason(self, reason):
        self.reason = reason

    def set_emi_amount(self, emi_amount):
        # self.emi_amount = emi_amount
        self.emi_amount = net_pay_calc_str(emi_amount)

    def set_employee_id(self, employee_id):
        self.employee_id = employee_id


    def set_created_by(self,created_by):
        self.created_by = created_by


    def set_employee_amount(self, employee_id, amount):
        self.employee_amount = None
        for i in amount:
            if i.advance_id == employee_id:
                self.employee_amount = i
                break


    def set_count_payment(self, count):
        self.No_Of_Installment = count

    def set_advance_amount(self, advance_amount):
        self.advance_amount = data_roundup(advance_amount)

    def set_code(self,code):
        self.code = code

    def set_department_val(self,employee_id,arr):
        self.department = None
        for emp_dept in arr:
            if emp_dept['emp_id'] == employee_id:
                self.department = emp_dept['department']['name']
                break