# Copyright (c) 2022, InfluxERP
# License: GNU General Public License v3. See license.txt

import frappe
from frappe.query_builder.functions import Max, Min, Sum
from frappe.utils import flt

from erpnext.projects.doctype.project.project import Project


class EmployeeProject(Project):
	def calculate_gross_margin(self):
		expense_amount = (
			flt(self.total_costing_amount)
			# add expense claim amount
			+ flt(self.total_expense_claim)
			+ flt(self.total_purchase_cost)
			+ flt(self.get("total_consumed_material_cost", 0))
		)

		self.gross_margin = flt(self.total_billed_amount) - expense_amount
		if self.total_billed_amount:
			self.per_gross_margin = (self.gross_margin / flt(self.total_billed_amount)) * 100

	def update_costing(self):
		ExpenseClaim = frappe.qb.DocType("Expense Claim")
		self.total_expense_claim = (
			frappe.qb.from_(ExpenseClaim)
			.select(Sum(ExpenseClaim.total_sanctioned_amount))
			.where((ExpenseClaim.docstatus == 1) & (ExpenseClaim.project == self.name))
		).run()[0][0]

		TimesheetDetail = frappe.qb.DocType("Timesheet Detail")
		from_time_sheet = (
			frappe.qb.from_(TimesheetDetail)
			.select(
				Sum(TimesheetDetail.costing_amount).as_("costing_amount"),
				Sum(TimesheetDetail.billing_amount).as_("billing_amount"),
				Min(TimesheetDetail.from_time).as_("start_date"),
				Max(TimesheetDetail.to_time).as_("end_date"),
				Sum(TimesheetDetail.hours).as_("time"),
			)
			.where((TimesheetDetail.project == self.name) & (TimesheetDetail.docstatus == 1))
		).run(as_dict=True)[0]

		self.actual_start_date = from_time_sheet.start_date
		self.actual_end_date = from_time_sheet.end_date

		self.total_costing_amount = from_time_sheet.costing_amount
		self.total_billable_amount = from_time_sheet.billing_amount
		self.actual_time = from_time_sheet.time

		self.update_purchase_costing()
		self.update_sales_amount()
		self.update_billed_amount()
		self.calculate_gross_margin()
