brotherton-erpnext/erpnext/patches/v7_0/convert_timelog_to_timesheet.py

68 lines
2.1 KiB
Python
Raw Normal View History

import frappe
from erpnext.manufacturing.doctype.production_order.production_order \
import make_timesheet, add_timesheet_detail
def execute():
frappe.reload_doc('projects', 'doctype', 'task')
2016-07-14 09:02:15 +00:00
frappe.reload_doc('projects', 'doctype', 'timesheet')
2016-09-07 11:01:26 +00:00
if not frappe.db.table_exists("Time Log"):
return
for data in frappe.db.sql("select * from `tabTime Log`", as_dict=1):
2016-08-16 13:01:09 +00:00
if data.task:
company = frappe.db.get_value("Task", data.task, "company")
elif data.production_order:
company = frappe.db.get_value("Prodction Order", data.production_order, "company")
else:
company = frappe.db.get_single_value('Global Defaults', 'default_company')
time_sheet = make_timesheet(data.production_order)
args = get_timelog_data(data)
add_timesheet_detail(time_sheet, args)
if data.docstatus == 2:
time_sheet.docstatus = 0
else:
time_sheet.docstatus = data.docstatus
time_sheet.employee = data.employee
2016-08-16 13:01:09 +00:00
time_sheet.note = data.note
time_sheet.company = company
2016-09-02 06:54:54 +00:00
2016-08-16 13:01:09 +00:00
time_sheet.set_status()
2016-09-02 06:54:54 +00:00
time_sheet.set_dates()
2016-08-16 13:01:09 +00:00
time_sheet.update_cost()
time_sheet.calculate_total_amounts()
time_sheet.flags.ignore_validate = True
2016-09-23 05:38:02 +00:00
time_sheet.flags.ignore_links = True
2016-08-16 13:01:09 +00:00
time_sheet.save(ignore_permissions=True)
# To ignore validate_mandatory_fields function
if data.docstatus == 1:
time_sheet.db_set("docstatus", 1)
for d in time_sheet.get("time_logs"):
d.db_set("docstatus", 1)
time_sheet.update_production_order(time_sheet.name)
time_sheet.update_task_and_project()
if data.docstatus == 2:
time_sheet.db_set("docstatus", 2)
for d in time_sheet.get("time_logs"):
d.db_set("docstatus", 2)
def get_timelog_data(data):
return {
'billable': data.billable,
'from_time': data.from_time,
'hours': data.hours,
'to_time': data.to_time,
'project': data.project,
'task': data.task,
'activity_type': data.activity_type,
'operation': data.operation,
'operation_id': data.operation_id,
'workstation': data.workstation,
'completed_qty': data.completed_qty,
'billing_rate': data.billing_rate,
'billing_amount': data.billing_amount,
'costing_rate': data.costing_rate,
'costing_amount': data.costing_amount
}