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
1 change: 1 addition & 0 deletions rmax_custom/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
"before_validate": "rmax_custom.branch_defaults.override_cost_center_from_branch",
"validate": "rmax_custom.inter_branch.auto_set_branch_from_warehouse",
"before_submit": "rmax_custom.api.delivery_note.before_submit_return_dn_guard",
"on_submit": "rmax_custom.inter_company_dn.on_delivery_note_submit",
"on_cancel": "rmax_custom.api.delivery_note.clear_consolidated_return_dn_stamp",
},
"Purchase Receipt": {
Expand Down
66 changes: 66 additions & 0 deletions rmax_custom/inter_company_dn.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,72 @@ def _validate_batch(dns):
)


def on_delivery_note_submit(doc, method=None):
"""Auto-create Inter Company Purchase Receipt on DN submit.

Triggered for DNs with custom_is_inter_company=1 that don't already
have a linked PR (inter_company_reference). Runs with ignore_permissions
so branch users don't need supplier/warehouse/company access.
"""
if not doc.custom_is_inter_company:
return
if doc.inter_company_reference:
return

try:
from erpnext.stock.doctype.delivery_note.delivery_note import (
make_inter_company_purchase_receipt,
)

_prev_user = frappe.session.user
frappe.session.user = "Administrator"
try:
pr = make_inter_company_purchase_receipt(doc.name)
finally:
frappe.session.user = _prev_user

# Inject Purchase Taxes from the Inter Company Branch master
inter_company_branch = doc.get("custom_inter_company_branch")
if inter_company_branch:
taxes_template = frappe.db.get_value(
"Inter Company Branch", inter_company_branch, "purchase_taxes_and_charges"
)
if taxes_template:
template_doc = frappe.get_doc("Purchase Taxes and Charges Template", taxes_template)
pr.taxes_and_charges = taxes_template
pr.set("taxes", [])
for tax in template_doc.taxes:
pr.append("taxes", {
"charge_type": tax.charge_type,
"account_head": tax.account_head,
"description": tax.description,
"rate": tax.rate,
"cost_center": tax.cost_center,
"included_in_print_rate": tax.included_in_print_rate,
})

pr.flags.ignore_permissions = True
pr.insert(ignore_permissions=True)

doc.db_set("inter_company_reference", pr.name)

frappe.msgprint(
frappe._('Purchase Receipt <a href="/app/purchase-receipt/{0}">{0}</a> created').format(pr.name),
alert=True,
indicator="green",
)
except Exception:
frappe.log_error(
title="Inter-Company PR auto-creation failed",
message=frappe.get_traceback(),
)
frappe.msgprint(
frappe._("Could not auto-create Purchase Receipt. Please create manually."),
indicator="orange",
alert=True,
)


def _normalise_names(delivery_note_names) -> List[str]:
if isinstance(delivery_note_names, str):
import json
Expand Down
1 change: 1 addition & 0 deletions rmax_custom/public/js/branch_user_restrict.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"Purchase Invoice",
"Material Request",
"Stock Transfer",
"Inter Branch Stock Transfer",
"Damage Slip",
"Damage Transfer",
"Supplier Code",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
// Copyright (c) 2026, Enfono and contributors
// For license information, please see license.txt

frappe.listview_settings['Inter Branch Stock Transfer'] = {
add_fields: ['status'],
get_indicator: function (doc) {
if (doc.status === 'Return') return [__('Return'), 'grey', 'status,=,Return'];
}
};

frappe.ui.form.on('Inter Branch Stock Transfer', {

onload: function (frm) {
_setup_warehouse_queries(frm);
_setup_price_list_query(frm);
_setup_taxes_and_charges_query(frm);
if (frm.is_new()) {
if (frm.is_new() && !frm.doc.is_return) {
_set_default_source_warehouse(frm);
frm.set_value('to_warehouse', 'HQ Awtad - CL');
frm.set_value('price_list', 'Inter Company Price');
}
_lock_system_fields(frm);
},

refresh: function (frm) {
_setup_warehouse_queries(frm);
_setup_price_list_query(frm);
_setup_taxes_and_charges_query(frm);
_setup_buttons(frm);
_toggle_posting_time(frm);
_lock_system_fields(frm);
},

set_posting_time: function (frm) {
Expand All @@ -28,6 +37,12 @@ frappe.ui.form.on('Inter Branch Stock Transfer', {
_setup_warehouse_queries(frm);
_setup_taxes_and_charges_query(frm);
_set_default_taxes_and_charges(frm);
if (!frm.doc.is_return) {
frm.set_value('to_warehouse', 'HQ Awtad - CL');
}
if (!frm.doc.price_list) {
frm.set_value('price_list', 'Inter Company Price');
}
},

from_warehouse: function (frm) {
Expand Down Expand Up @@ -139,6 +154,7 @@ function _toggle_posting_time(frm) {

function _setup_buttons(frm) {
frm.remove_custom_button(__('Create Delivery Note'));
frm.remove_custom_button(__('Create Return'));

if (frm.doc.docstatus !== 1) return;

Expand All @@ -161,28 +177,37 @@ function _setup_buttons(frm) {
);
}, __('Create'));
}

// Return button — only if no return created yet and not itself a return
if (!frm.doc.return_ibst && !frm.doc.is_return) {
frm.add_custom_button(__('Create Return'), function () {
frappe.confirm(
__('Create a return document for <b>{0}</b>?', [frm.doc.name]),
function () {
frappe.call({
method: 'rmax_custom.rmax_custom.doctype.inter_branch_stock_transfer.inter_branch_stock_transfer.create_ibst_return_doc',
args: { ibst_name: frm.doc.name },
freeze: true,
freeze_message: __('Creating return document...'),
callback: function (r) {
if (r.message) {
frappe.set_route('Form', 'Inter Branch Stock Transfer', r.message);
}
}
});
}
);
}, __('Create'));
}
}

function _setup_warehouse_queries(frm) {
frm.set_query('from_warehouse', function () {
return { filters: { company: frm.doc.company, is_group: 0 } };
});
frm.set_query('to_warehouse', function () {
return {
ignore_user_permissions: 1,
filters: {
company: frm.doc.company,
is_group: 0,
name: ['!=', frm.doc.from_warehouse || '']
}
};
});
frm.fields_dict['items'].grid.get_field('s_warehouse').get_query = function (doc) {
return { filters: { company: doc.company, is_group: 0 } };
};
frm.fields_dict['items'].grid.get_field('t_warehouse').get_query = function (doc) {
return { ignore_user_permissions: 1, filters: { company: doc.company, is_group: 0 } };
};
}

function _set_default_source_warehouse(frm) {
Expand Down Expand Up @@ -257,12 +282,6 @@ function _recalculate_row_amount(cdt, cdn) {
frappe.model.set_value(cdt, cdn, 'transfer_qty', flt(row.qty) * flt(row.conversion_factor || 1));
}

function _setup_price_list_query(frm) {
frm.set_query('price_list', function () {
return { filters: { selling: 1, enabled: 1 } };
});
}

function _setup_taxes_and_charges_query(frm) {
frm.set_query('taxes_and_charges', function () {
return { filters: { company: frm.doc.company, disabled: 0 } };
Expand Down Expand Up @@ -310,3 +329,12 @@ function _fetch_price_list_rates_for_all_items(frm) {
}
});
}

function _lock_system_fields(frm) {
// Target warehouse is always HQ — never user-editable (both regular and return)
frm.set_df_property('to_warehouse', 'read_only', 1);
// Price list is always Inter Company Price — never user-editable
frm.set_df_property('price_list', 'read_only', 1);
// Lock t_warehouse in item rows (always pushed from header to_warehouse)
frm.fields_dict['items'] && frm.fields_dict['items'].grid.toggle_enable('t_warehouse', false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"naming_series",
"company",
"amended_from",
"is_return",
"return_against",
"status",
"column_break_header",
"posting_date",
"posting_time",
Expand All @@ -28,7 +31,8 @@
"stock_entry",
"journal_entry",
"column_break_refs",
"delivery_note"
"delivery_note",
"return_ibst"
],
"fields": [
{
Expand Down Expand Up @@ -63,6 +67,37 @@
"print_hide": 1,
"read_only": 1
},
{
"default": "0",
"fieldname": "is_return",
"fieldtype": "Check",
"label": "Is Return",
"no_copy": 1,
"print_hide": 1,
"read_only": 1
},
{
"default": "Submitted",
"fieldname": "status",
"fieldtype": "Select",
"hidden": 1,
"in_list_view": 1,
"label": "Status",
"no_copy": 1,
"options": "Submitted\nReturn",
"print_hide": 1,
"read_only": 1
},
{
"depends_on": "eval:doc.is_return",
"fieldname": "return_against",
"fieldtype": "Link",
"label": "Return Against",
"no_copy": 1,
"options": "Inter Branch Stock Transfer",
"print_hide": 1,
"read_only": 1
},
{
"fieldname": "column_break_header",
"fieldtype": "Column Break"
Expand Down Expand Up @@ -113,6 +148,7 @@
"ignore_user_permissions": 1,
"label": "Set Target Warehouse",
"options": "Warehouse",
"read_only": 1,
"reqd": 1
},
{
Expand All @@ -135,10 +171,12 @@
"read_only": 1
},
{
"default": "Inter Company Price",
"fieldname": "price_list",
"fieldtype": "Link",
"label": "Price List",
"options": "Price List"
"options": "Price List",
"read_only": 1
},
{
"fieldname": "taxes_and_charges",
Expand Down Expand Up @@ -193,6 +231,17 @@
"options": "Delivery Note",
"print_hide": 1,
"read_only": 1
},
{
"depends_on": "eval:doc.return_ibst",
"fieldname": "return_ibst",
"fieldtype": "Link",
"in_list_view": 1,
"label": "Return IBST",
"no_copy": 1,
"options": "Inter Branch Stock Transfer",
"print_hide": 1,
"read_only": 1
}
],
"grid_page_length": 50,
Expand Down
Loading
Loading