Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 64 additions & 13 deletions virtual_pro/events/sales_invoice.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import frappe
from frappe import _
from frappe.utils import flt, nowdate


def update_service_request(doc, method):
sp_name = frappe.db.get_value("Service Request", {"enquiry": doc.custom_enquiry}, "name")

if sp_name:
sp = frappe.get_doc("Service Request", sp_name)
if doc.custom_enquiry:
sp_name = frappe.db.get_value("Service Request", {"enquiry": doc.custom_enquiry}, "name")

if doc.docstatus == 1:
sp.db_set('status', "Completed")
sp.db_set('sales_invoice', doc.name)
elif doc.docstatus == 2:
sp.db_set('status', "To Sales Invoice")
sp.db_set('sales_invoice', " ")

sp.save()
if sp_name:
sp = frappe.get_doc("Service Request", sp_name)

if doc.docstatus == 1:
sp.db_set('status', "Completed")
sp.db_set('sales_invoice', doc.name)
elif doc.docstatus == 2:
sp.db_set('status', "To Sales Invoice")
sp.db_set('sales_invoice', " ")

sp.save()


def create_tasks(doc, method):
Expand Down Expand Up @@ -197,4 +199,53 @@ def create_child_task(child, parent_task_name=None):

create_task_and_todos()

frappe.msgprint(_("Tasks and ToDos created and assigned by email/role."))
frappe.msgprint(_("Tasks and ToDos created and assigned by email/role."))

@frappe.whitelist()
def create_journal_entry_for_cost_difference(doc, method):
total_cost_difference = 0
company = frappe.get_doc("Company", doc.company)
income_account = company.default_income_account

if not income_account:
frappe.throw("Please set a Default Income Account in Company settings.")

journal = frappe.new_doc("Journal Entry")
journal.voucher_type = "Journal Entry"
journal.posting_date = nowdate()
journal.company = doc.company
journal.remark = f"Cost Difference Journal Entry for Sales Invoice {doc.name}"

for item in doc.items:
cost_diff = flt(item.custom_cost_difference)
if cost_diff > 0 and item.expense_account:
total_cost_difference += cost_diff

journal.append("accounts", {
"account": item.expense_account,
"debit_in_account_currency": cost_diff
})

if total_cost_difference > 0:
journal.append("accounts", {
"account": income_account,
"credit_in_account_currency": total_cost_difference
})

journal.insert()
journal.submit()

doc.db_set("custom_ref_journal_entry", journal.name)


@frappe.whitelist()
def cancel_journal_entry_for_cost_difference(doc, method):
if doc.custom_ref_journal_entry:
try:
je = frappe.get_doc("Journal Entry", doc.custom_ref_journal_entry)
if je.docstatus == 1:
je.cancel()
except frappe.DoesNotExistError:
frappe.msgprint(f"Journal Entry {doc.custom_ref_journal_entry} not found.")

doc.db_set("custom_ref_journal_entry", None)
10 changes: 7 additions & 3 deletions virtual_pro/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
# page_js = {"page" : "public/js/file.js"}

# include js in doctype views
doctype_js = {"Quotation" : "public/js/quotation.js",}
doctype_js = {"Quotation" : "public/js/quotation.js",
"Sales Invoice": "public/js/sales_invoice.js"
}
doctype_list_js = {"Task" : "public/js/task_list.js"}
# doctype_tree_js = {"doctype" : "public/js/doctype_tree.js"}
# doctype_calendar_js = {"doctype" : "public/js/doctype_calendar.js"}
Expand Down Expand Up @@ -145,10 +147,12 @@
"Sales Invoice": {
"on_submit": ["virtual_pro.events.sales_invoice.create_tasks",
"virtual_pro.events.sales_invoice.update_service_request",
"virtual_pro.events.quotation.update_quotation_status"],
"virtual_pro.events.quotation.update_quotation_status",
"virtual_pro.events.sales_invoice.create_journal_entry_for_cost_difference"],

"on_cancel": ["virtual_pro.events.sales_invoice.update_service_request",
"virtual_pro.events.quotation.update_quotation_status"]
"virtual_pro.events.quotation.update_quotation_status",
"virtual_pro.events.sales_invoice.cancel_journal_entry_for_cost_difference"]
},
"ToDo": {
"before_save":"virtual_pro.events.todo.before_save"
Expand Down
17 changes: 17 additions & 0 deletions virtual_pro/public/js/sales_invoice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
frappe.ui.form.on('Sales Invoice Item', {
amount: function (frm, cdt, cdn) {
calculate_cost_difference(frm, cdt, cdn);
},
custom_cost: function (frm, cdt, cdn) {
calculate_cost_difference(frm, cdt, cdn);
}
});

function calculate_cost_difference(frm, cdt, cdn) {
let row = locals[cdt][cdn];
let amount = flt(row.amount);
let cost = flt(row.custom_cost);

row.custom_cost_difference = amount - cost;
frm.refresh_field("items");
}
32 changes: 32 additions & 0 deletions virtual_pro/virtual_pro/custom/packed_item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"custom_fields": [],
"custom_perms": [],
"doctype": "Packed Item",
"links": [],
"property_setters": [
{
"_assign": null,
"_comments": null,
"_liked_by": null,
"_user_tags": null,
"creation": "2025-06-16 13:47:52.346508",
"default_value": null,
"doc_type": "Packed Item",
"docstatus": 0,
"doctype_or_field": "DocField",
"field_name": "rate",
"idx": 0,
"is_system_generated": 0,
"modified": "2025-06-16 13:47:52.346508",
"modified_by": "Administrator",
"module": null,
"name": "Packed Item-rate-read_only",
"owner": "Administrator",
"property": "read_only",
"property_type": "Check",
"row_name": null,
"value": "1"
}
],
"sync_on_migrate": 1
}
Loading