mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-15 11:09:17 +00:00
Merge branch 'frappe:develop' into rounded-row-wise-tax
This commit is contained in:
@@ -169,7 +169,6 @@ class AccountsController(TransactionBase):
|
||||
self.validate_value("base_grand_total", ">=", 0)
|
||||
|
||||
validate_return(self)
|
||||
self.set_total_in_words()
|
||||
|
||||
self.validate_all_documents_schedule()
|
||||
|
||||
@@ -201,22 +200,82 @@ class AccountsController(TransactionBase):
|
||||
# apply tax withholding only if checked and applicable
|
||||
self.set_tax_withholding()
|
||||
|
||||
validate_regional(self)
|
||||
|
||||
validate_einvoice_fields(self)
|
||||
with temporary_flag("company", self.company):
|
||||
validate_regional(self)
|
||||
validate_einvoice_fields(self)
|
||||
|
||||
if self.doctype != "Material Request" and not self.ignore_pricing_rule:
|
||||
apply_pricing_rule_on_transaction(self)
|
||||
|
||||
self.set_total_in_words()
|
||||
|
||||
def before_cancel(self):
|
||||
validate_einvoice_fields(self)
|
||||
|
||||
def _remove_references_in_unreconcile(self):
|
||||
upe = frappe.qb.DocType("Unreconcile Payment Entries")
|
||||
rows = (
|
||||
frappe.qb.from_(upe)
|
||||
.select(upe.name, upe.parent)
|
||||
.where((upe.reference_doctype == self.doctype) & (upe.reference_name == self.name))
|
||||
.run(as_dict=True)
|
||||
)
|
||||
|
||||
if rows:
|
||||
references_map = frappe._dict()
|
||||
for x in rows:
|
||||
references_map.setdefault(x.parent, []).append(x.name)
|
||||
|
||||
for doc, rows in references_map.items():
|
||||
unreconcile_doc = frappe.get_doc("Unreconcile Payments", doc)
|
||||
for row in rows:
|
||||
unreconcile_doc.remove(unreconcile_doc.get("allocations", {"name": row})[0])
|
||||
|
||||
unreconcile_doc.flags.ignore_validate_update_after_submit = True
|
||||
unreconcile_doc.flags.ignore_links = True
|
||||
unreconcile_doc.save(ignore_permissions=True)
|
||||
|
||||
# delete docs upon parent doc deletion
|
||||
unreconcile_docs = frappe.db.get_all("Unreconcile Payments", filters={"voucher_no": self.name})
|
||||
for x in unreconcile_docs:
|
||||
_doc = frappe.get_doc("Unreconcile Payments", x.name)
|
||||
if _doc.docstatus == 1:
|
||||
_doc.cancel()
|
||||
_doc.delete()
|
||||
|
||||
def _remove_references_in_repost_doctypes(self):
|
||||
repost_doctypes = ["Repost Payment Ledger Items", "Repost Accounting Ledger Items"]
|
||||
|
||||
for _doctype in repost_doctypes:
|
||||
dt = frappe.qb.DocType(_doctype)
|
||||
rows = (
|
||||
frappe.qb.from_(dt)
|
||||
.select(dt.name, dt.parent, dt.parenttype)
|
||||
.where((dt.voucher_type == self.doctype) & (dt.voucher_no == self.name))
|
||||
.run(as_dict=True)
|
||||
)
|
||||
|
||||
if rows:
|
||||
references_map = frappe._dict()
|
||||
for x in rows:
|
||||
references_map.setdefault((x.parenttype, x.parent), []).append(x.name)
|
||||
|
||||
for doc, rows in references_map.items():
|
||||
repost_doc = frappe.get_doc(doc[0], doc[1])
|
||||
|
||||
for row in rows:
|
||||
if _doctype == "Repost Payment Ledger Items":
|
||||
repost_doc.remove(repost_doc.get("repost_vouchers", {"name": row})[0])
|
||||
else:
|
||||
repost_doc.remove(repost_doc.get("vouchers", {"name": row})[0])
|
||||
|
||||
repost_doc.flags.ignore_validate_update_after_submit = True
|
||||
repost_doc.flags.ignore_links = True
|
||||
repost_doc.save(ignore_permissions=True)
|
||||
|
||||
def on_trash(self):
|
||||
# delete references in 'Repost Payment Ledger'
|
||||
rpi = frappe.qb.DocType("Repost Payment Ledger Items")
|
||||
frappe.qb.from_(rpi).delete().where(
|
||||
(rpi.voucher_type == self.doctype) & (rpi.voucher_no == self.name)
|
||||
).run()
|
||||
self._remove_references_in_repost_doctypes()
|
||||
self._remove_references_in_unreconcile()
|
||||
|
||||
# delete sl and gl entries on deletion of transaction
|
||||
if frappe.db.get_single_value("Accounts Settings", "delete_linked_ledger_entries"):
|
||||
@@ -935,7 +994,7 @@ class AccountsController(TransactionBase):
|
||||
party_type, party, party_account, amount_field, order_doctype, order_list, include_unallocated
|
||||
)
|
||||
|
||||
payment_entries = get_advance_payment_entries(
|
||||
payment_entries = get_advance_payment_entries_for_regional(
|
||||
party_type, party, party_account, order_doctype, order_list, include_unallocated
|
||||
)
|
||||
|
||||
@@ -1023,6 +1082,44 @@ class AccountsController(TransactionBase):
|
||||
)
|
||||
)
|
||||
|
||||
def gain_loss_journal_already_booked(
|
||||
self,
|
||||
gain_loss_account,
|
||||
exc_gain_loss,
|
||||
ref2_dt,
|
||||
ref2_dn,
|
||||
ref2_detail_no,
|
||||
) -> bool:
|
||||
"""
|
||||
Check if gain/loss is booked
|
||||
"""
|
||||
if res := frappe.db.get_all(
|
||||
"Journal Entry Account",
|
||||
filters={
|
||||
"docstatus": 1,
|
||||
"account": gain_loss_account,
|
||||
"reference_type": ref2_dt, # this will be Journal Entry
|
||||
"reference_name": ref2_dn,
|
||||
"reference_detail_no": ref2_detail_no,
|
||||
},
|
||||
pluck="parent",
|
||||
):
|
||||
# deduplicate
|
||||
res = list({x for x in res})
|
||||
if exc_vouchers := frappe.db.get_all(
|
||||
"Journal Entry",
|
||||
filters={"name": ["in", res], "voucher_type": "Exchange Gain Or Loss"},
|
||||
fields=["voucher_type", "total_debit", "total_credit"],
|
||||
):
|
||||
booked_voucher = exc_vouchers[0]
|
||||
if (
|
||||
booked_voucher.total_debit == exc_gain_loss
|
||||
and booked_voucher.total_credit == exc_gain_loss
|
||||
and booked_voucher.voucher_type == "Exchange Gain Or Loss"
|
||||
):
|
||||
return True
|
||||
return False
|
||||
|
||||
def make_exchange_gain_loss_journal(self, args: dict = None) -> None:
|
||||
"""
|
||||
Make Exchange Gain/Loss journal for Invoices and Payments
|
||||
@@ -1051,27 +1148,37 @@ class AccountsController(TransactionBase):
|
||||
|
||||
reverse_dr_or_cr = "debit" if dr_or_cr == "credit" else "credit"
|
||||
|
||||
je = create_gain_loss_journal(
|
||||
self.company,
|
||||
arg.get("party_type"),
|
||||
arg.get("party"),
|
||||
party_account,
|
||||
if not self.gain_loss_journal_already_booked(
|
||||
gain_loss_account,
|
||||
difference_amount,
|
||||
dr_or_cr,
|
||||
reverse_dr_or_cr,
|
||||
arg.get("against_voucher_type"),
|
||||
arg.get("against_voucher"),
|
||||
arg.get("idx"),
|
||||
self.doctype,
|
||||
self.name,
|
||||
arg.get("idx"),
|
||||
)
|
||||
frappe.msgprint(
|
||||
_("Exchange Gain/Loss amount has been booked through {0}").format(
|
||||
get_link_to_form("Journal Entry", je)
|
||||
arg.get("referenced_row"),
|
||||
):
|
||||
posting_date = frappe.db.get_value(arg.voucher_type, arg.voucher_no, "posting_date")
|
||||
je = create_gain_loss_journal(
|
||||
self.company,
|
||||
posting_date,
|
||||
arg.get("party_type"),
|
||||
arg.get("party"),
|
||||
party_account,
|
||||
gain_loss_account,
|
||||
difference_amount,
|
||||
dr_or_cr,
|
||||
reverse_dr_or_cr,
|
||||
arg.get("against_voucher_type"),
|
||||
arg.get("against_voucher"),
|
||||
arg.get("idx"),
|
||||
self.doctype,
|
||||
self.name,
|
||||
arg.get("referenced_row"),
|
||||
arg.get("cost_center"),
|
||||
)
|
||||
frappe.msgprint(
|
||||
_("Exchange Gain/Loss amount has been booked through {0}").format(
|
||||
get_link_to_form("Journal Entry", je)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
if self.get("doctype") == "Payment Entry":
|
||||
# For Payment Entry, exchange_gain_loss field in the `references` table is the trigger for journal creation
|
||||
@@ -1131,6 +1238,7 @@ class AccountsController(TransactionBase):
|
||||
|
||||
je = create_gain_loss_journal(
|
||||
self.company,
|
||||
self.posting_date,
|
||||
self.party_type,
|
||||
self.party,
|
||||
party_account,
|
||||
@@ -1144,6 +1252,7 @@ class AccountsController(TransactionBase):
|
||||
self.doctype,
|
||||
self.name,
|
||||
d.idx,
|
||||
self.cost_center,
|
||||
)
|
||||
frappe.msgprint(
|
||||
_("Exchange Gain/Loss amount has been booked through {0}").format(
|
||||
@@ -1381,8 +1490,8 @@ class AccountsController(TransactionBase):
|
||||
{
|
||||
"account": self.additional_discount_account,
|
||||
"against": supplier_or_customer,
|
||||
dr_or_cr: self.discount_amount,
|
||||
"cost_center": self.cost_center,
|
||||
dr_or_cr: self.base_discount_amount,
|
||||
"cost_center": self.cost_center or erpnext.get_default_cost_center(self.company),
|
||||
},
|
||||
item=self,
|
||||
)
|
||||
@@ -1653,6 +1762,7 @@ class AccountsController(TransactionBase):
|
||||
and party_account_currency != self.company_currency
|
||||
and self.currency != party_account_currency
|
||||
):
|
||||
|
||||
frappe.throw(
|
||||
_("Accounting Entry for {0}: {1} can only be made in currency: {2}").format(
|
||||
party_type, party, party_account_currency
|
||||
@@ -2101,6 +2211,45 @@ class AccountsController(TransactionBase):
|
||||
_("Select finance book for the item {0} at row {1}").format(item.item_code, item.idx)
|
||||
)
|
||||
|
||||
def check_if_fields_updated(self, fields_to_check, child_tables):
|
||||
# Check if any field affecting accounting entry is altered
|
||||
doc_before_update = self.get_doc_before_save()
|
||||
accounting_dimensions = get_accounting_dimensions() + ["cost_center", "project"]
|
||||
|
||||
# Check if opening entry check updated
|
||||
needs_repost = doc_before_update.get("is_opening") != self.is_opening
|
||||
|
||||
if not needs_repost:
|
||||
# Parent Level Accounts excluding party account
|
||||
fields_to_check += accounting_dimensions
|
||||
for field in fields_to_check:
|
||||
if doc_before_update.get(field) != self.get(field):
|
||||
needs_repost = 1
|
||||
break
|
||||
|
||||
if not needs_repost:
|
||||
# Check for child tables
|
||||
for table in child_tables:
|
||||
needs_repost = check_if_child_table_updated(
|
||||
doc_before_update.get(table), self.get(table), child_tables[table]
|
||||
)
|
||||
if needs_repost:
|
||||
break
|
||||
|
||||
return needs_repost
|
||||
|
||||
@frappe.whitelist()
|
||||
def repost_accounting_entries(self):
|
||||
if self.repost_required:
|
||||
repost_ledger = frappe.new_doc("Repost Accounting Ledger")
|
||||
repost_ledger.company = self.company
|
||||
repost_ledger.append("vouchers", {"voucher_type": self.doctype, "voucher_no": self.name})
|
||||
repost_ledger.insert()
|
||||
repost_ledger.submit()
|
||||
self.db_set("repost_required", 0)
|
||||
else:
|
||||
frappe.throw(_("No updates pending for reposting"))
|
||||
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_tax_rate(account_head):
|
||||
@@ -2325,6 +2474,11 @@ def get_advance_journal_entries(
|
||||
return list(journal_entries)
|
||||
|
||||
|
||||
@erpnext.allow_regional
|
||||
def get_advance_payment_entries_for_regional(*args, **kwargs):
|
||||
return get_advance_payment_entries(*args, **kwargs)
|
||||
|
||||
|
||||
def get_advance_payment_entries(
|
||||
party_type,
|
||||
party,
|
||||
@@ -2386,7 +2540,8 @@ def get_common_query(
|
||||
limit,
|
||||
condition,
|
||||
):
|
||||
payment_type = "Receive" if party_type == "Customer" else "Pay"
|
||||
account_type = frappe.db.get_value("Party Type", party_type, "account_type")
|
||||
payment_type = "Receive" if account_type == "Receivable" else "Pay"
|
||||
payment_entry = frappe.qb.DocType("Payment Entry")
|
||||
|
||||
q = (
|
||||
@@ -2403,7 +2558,7 @@ def get_common_query(
|
||||
.where(payment_entry.docstatus == 1)
|
||||
)
|
||||
|
||||
if party_type == "Customer":
|
||||
if payment_type == "Receive":
|
||||
q = q.select((payment_entry.paid_from_account_currency).as_("currency"))
|
||||
q = q.select(payment_entry.paid_from)
|
||||
q = q.where(payment_entry.paid_from.isin(party_account))
|
||||
@@ -2418,6 +2573,9 @@ def get_common_query(
|
||||
q = q.select((payment_entry.target_exchange_rate).as_("exchange_rate"))
|
||||
|
||||
if condition:
|
||||
if condition.get("name", None):
|
||||
q = q.where(payment_entry.name.like(f"%{condition.get('name')}%"))
|
||||
|
||||
q = q.where(payment_entry.company == condition["company"])
|
||||
q = (
|
||||
q.where(payment_entry.posting_date >= condition["from_payment_date"])
|
||||
@@ -2855,6 +3013,27 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
|
||||
|
||||
return update_supplied_items
|
||||
|
||||
def validate_fg_item_for_subcontracting(new_data, is_new):
|
||||
if is_new:
|
||||
if not new_data.get("fg_item"):
|
||||
frappe.throw(
|
||||
_("Finished Good Item is not specified for service item {0}").format(new_data["item_code"])
|
||||
)
|
||||
else:
|
||||
is_sub_contracted_item, default_bom = frappe.db.get_value(
|
||||
"Item", new_data["fg_item"], ["is_sub_contracted_item", "default_bom"]
|
||||
)
|
||||
|
||||
if not is_sub_contracted_item:
|
||||
frappe.throw(
|
||||
_("Finished Good Item {0} must be a sub-contracted item").format(new_data["fg_item"])
|
||||
)
|
||||
elif not default_bom:
|
||||
frappe.throw(_("Default BOM not found for FG Item {0}").format(new_data["fg_item"]))
|
||||
|
||||
if not new_data.get("fg_item_qty"):
|
||||
frappe.throw(_("Finished Good Item {0} Qty can not be zero").format(new_data["fg_item"]))
|
||||
|
||||
data = json.loads(trans_items)
|
||||
|
||||
any_qty_changed = False # updated to true if any item's qty changes
|
||||
@@ -2886,6 +3065,7 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
|
||||
|
||||
prev_rate, new_rate = flt(child_item.get("rate")), flt(d.get("rate"))
|
||||
prev_qty, new_qty = flt(child_item.get("qty")), flt(d.get("qty"))
|
||||
prev_fg_qty, new_fg_qty = flt(child_item.get("fg_item_qty")), flt(d.get("fg_item_qty"))
|
||||
prev_con_fac, new_con_fac = flt(child_item.get("conversion_factor")), flt(
|
||||
d.get("conversion_factor")
|
||||
)
|
||||
@@ -2898,6 +3078,7 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
|
||||
|
||||
rate_unchanged = prev_rate == new_rate
|
||||
qty_unchanged = prev_qty == new_qty
|
||||
fg_qty_unchanged = prev_fg_qty == new_fg_qty
|
||||
uom_unchanged = prev_uom == new_uom
|
||||
conversion_factor_unchanged = prev_con_fac == new_con_fac
|
||||
any_conversion_factor_changed |= not conversion_factor_unchanged
|
||||
@@ -2907,6 +3088,7 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
|
||||
if (
|
||||
rate_unchanged
|
||||
and qty_unchanged
|
||||
and fg_qty_unchanged
|
||||
and conversion_factor_unchanged
|
||||
and uom_unchanged
|
||||
and date_unchanged
|
||||
@@ -2917,6 +3099,17 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
|
||||
if flt(child_item.get("qty")) != flt(d.get("qty")):
|
||||
any_qty_changed = True
|
||||
|
||||
if (
|
||||
parent.doctype == "Purchase Order"
|
||||
and parent.is_subcontracted
|
||||
and not parent.is_old_subcontracting_flow
|
||||
):
|
||||
validate_fg_item_for_subcontracting(d, new_child_flag)
|
||||
child_item.fg_item_qty = flt(d["fg_item_qty"])
|
||||
|
||||
if new_child_flag:
|
||||
child_item.fg_item = d["fg_item"]
|
||||
|
||||
child_item.qty = flt(d.get("qty"))
|
||||
rate_precision = child_item.precision("rate") or 2
|
||||
conv_fac_precision = child_item.precision("conversion_factor") or 2
|
||||
@@ -3020,11 +3213,20 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
|
||||
parent.update_ordered_qty()
|
||||
parent.update_ordered_and_reserved_qty()
|
||||
parent.update_receiving_percentage()
|
||||
if parent.is_old_subcontracting_flow:
|
||||
if should_update_supplied_items(parent):
|
||||
parent.update_reserved_qty_for_subcontract()
|
||||
parent.create_raw_materials_supplied()
|
||||
parent.save()
|
||||
|
||||
if parent.is_subcontracted:
|
||||
if parent.is_old_subcontracting_flow:
|
||||
if should_update_supplied_items(parent):
|
||||
parent.update_reserved_qty_for_subcontract()
|
||||
parent.create_raw_materials_supplied()
|
||||
parent.save()
|
||||
else:
|
||||
if not parent.can_update_items():
|
||||
frappe.throw(
|
||||
_(
|
||||
"Items cannot be updated as Subcontracting Order is created against the Purchase Order {0}."
|
||||
).format(frappe.bold(parent.name))
|
||||
)
|
||||
else: # Sales Order
|
||||
parent.validate_warehouse()
|
||||
parent.update_reserved_qty()
|
||||
@@ -3048,7 +3250,26 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
|
||||
|
||||
if has_reserved_stock(parent.doctype, parent.name):
|
||||
cancel_stock_reservation_entries(parent.doctype, parent.name)
|
||||
parent.create_stock_reservation_entries()
|
||||
|
||||
if parent.per_picked == 0:
|
||||
parent.create_stock_reservation_entries()
|
||||
|
||||
|
||||
def check_if_child_table_updated(
|
||||
child_table_before_update, child_table_after_update, fields_to_check
|
||||
):
|
||||
accounting_dimensions = get_accounting_dimensions() + ["cost_center", "project"]
|
||||
# Check if any field affecting accounting entry is altered
|
||||
for index, item in enumerate(child_table_after_update):
|
||||
for field in fields_to_check:
|
||||
if child_table_before_update[index].get(field) != item.get(field):
|
||||
return True
|
||||
|
||||
for dimension in accounting_dimensions:
|
||||
if child_table_before_update[index].get(dimension) != item.get(dimension):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
@erpnext.allow_regional
|
||||
|
||||
@@ -190,10 +190,13 @@ class BuyingController(SubcontractingController):
|
||||
purchase_doc_field = (
|
||||
"purchase_receipt" if self.doctype == "Purchase Receipt" else "purchase_invoice"
|
||||
)
|
||||
not_cancelled_asset = [
|
||||
d.name
|
||||
for d in frappe.db.get_all("Asset", {purchase_doc_field: self.return_against, "docstatus": 1})
|
||||
]
|
||||
not_cancelled_asset = []
|
||||
if self.return_against:
|
||||
not_cancelled_asset = [
|
||||
d.name
|
||||
for d in frappe.db.get_all("Asset", {purchase_doc_field: self.return_against, "docstatus": 1})
|
||||
]
|
||||
|
||||
if self.is_return and len(not_cancelled_asset):
|
||||
frappe.throw(
|
||||
_(
|
||||
@@ -415,6 +418,10 @@ class BuyingController(SubcontractingController):
|
||||
item.bom = None
|
||||
|
||||
def set_qty_as_per_stock_uom(self):
|
||||
allow_to_edit_stock_qty = frappe.db.get_single_value(
|
||||
"Stock Settings", "allow_to_edit_stock_uom_qty_for_purchase"
|
||||
)
|
||||
|
||||
for d in self.get("items"):
|
||||
if d.meta.get_field("stock_qty"):
|
||||
# Check if item code is present
|
||||
@@ -429,6 +436,11 @@ class BuyingController(SubcontractingController):
|
||||
d.conversion_factor, d.precision("conversion_factor")
|
||||
)
|
||||
|
||||
if allow_to_edit_stock_qty:
|
||||
d.stock_qty = flt(d.stock_qty, d.precision("stock_qty"))
|
||||
if d.get("received_stock_qty"):
|
||||
d.received_stock_qty = flt(d.received_stock_qty, d.precision("received_stock_qty"))
|
||||
|
||||
def validate_purchase_return(self):
|
||||
for d in self.get("items"):
|
||||
if self.is_return and flt(d.rejected_qty) != 0:
|
||||
@@ -436,24 +448,6 @@ class BuyingController(SubcontractingController):
|
||||
|
||||
# validate rate with ref PR
|
||||
|
||||
def validate_rejected_warehouse(self):
|
||||
for item in self.get("items"):
|
||||
if flt(item.rejected_qty) and not item.rejected_warehouse:
|
||||
if self.rejected_warehouse:
|
||||
item.rejected_warehouse = self.rejected_warehouse
|
||||
|
||||
if not item.rejected_warehouse:
|
||||
frappe.throw(
|
||||
_("Row #{0}: Rejected Warehouse is mandatory for the rejected Item {1}").format(
|
||||
item.idx, item.item_code
|
||||
)
|
||||
)
|
||||
|
||||
if item.get("rejected_warehouse") and (item.get("rejected_warehouse") == item.get("warehouse")):
|
||||
frappe.throw(
|
||||
_("Row #{0}: Accepted Warehouse and Rejected Warehouse cannot be same").format(item.idx)
|
||||
)
|
||||
|
||||
# validate accepted and rejected qty
|
||||
def validate_accepted_rejected_qty(self):
|
||||
for d in self.get("items"):
|
||||
|
||||
@@ -345,6 +345,8 @@ def make_return_doc(
|
||||
elif doctype == "Purchase Invoice":
|
||||
# look for Print Heading "Debit Note"
|
||||
doc.select_print_heading = frappe.get_cached_value("Print Heading", _("Debit Note"))
|
||||
if source.tax_withholding_category:
|
||||
doc.set_onload("supplier_tds", source.tax_withholding_category)
|
||||
|
||||
for tax in doc.get("taxes") or []:
|
||||
if tax.charge_type == "Actual":
|
||||
|
||||
@@ -194,11 +194,17 @@ class SellingController(StockController):
|
||||
frappe.throw(_("Maximum discount for Item {0} is {1}%").format(d.item_code, discount))
|
||||
|
||||
def set_qty_as_per_stock_uom(self):
|
||||
allow_to_edit_stock_qty = frappe.db.get_single_value(
|
||||
"Stock Settings", "allow_to_edit_stock_uom_qty_for_sales"
|
||||
)
|
||||
|
||||
for d in self.get("items"):
|
||||
if d.meta.get_field("stock_qty"):
|
||||
if not d.conversion_factor:
|
||||
frappe.throw(_("Row {0}: Conversion Factor is mandatory").format(d.idx))
|
||||
d.stock_qty = flt(d.qty) * flt(d.conversion_factor)
|
||||
if allow_to_edit_stock_qty:
|
||||
d.stock_qty = flt(d.stock_qty, d.precision("stock_qty"))
|
||||
|
||||
def validate_selling_price(self):
|
||||
def throw_message(idx, item_name, rate, ref_rate_field):
|
||||
@@ -282,7 +288,9 @@ class SellingController(StockController):
|
||||
last_valuation_rate_in_sales_uom = last_valuation_rate * (item.conversion_factor or 1)
|
||||
|
||||
if flt(item.base_net_rate) < flt(last_valuation_rate_in_sales_uom):
|
||||
throw_message(item.idx, item.item_name, last_valuation_rate_in_sales_uom, "valuation rate")
|
||||
throw_message(
|
||||
item.idx, item.item_name, last_valuation_rate_in_sales_uom, "valuation rate (Moving Average)"
|
||||
)
|
||||
|
||||
def get_item_list(self):
|
||||
il = []
|
||||
@@ -388,7 +396,7 @@ class SellingController(StockController):
|
||||
for d in self.get("items"):
|
||||
if d.get(ref_fieldname):
|
||||
status = frappe.db.get_value("Sales Order", d.get(ref_fieldname), "status")
|
||||
if status in ("Closed", "On Hold"):
|
||||
if status in ("Closed", "On Hold") and not self.is_return:
|
||||
frappe.throw(_("Sales Order {0} is {1}").format(d.get(ref_fieldname), status))
|
||||
|
||||
def update_reserved_qty(self):
|
||||
@@ -404,7 +412,9 @@ class SellingController(StockController):
|
||||
if so and so_item_rows:
|
||||
sales_order = frappe.get_doc("Sales Order", so)
|
||||
|
||||
if sales_order.status in ["Closed", "Cancelled"]:
|
||||
if (sales_order.status == "Closed" and not self.is_return) or sales_order.status in [
|
||||
"Cancelled"
|
||||
]:
|
||||
frappe.throw(
|
||||
_("{0} {1} is cancelled or closed").format(_("Sales Order"), so), frappe.InvalidStatusError
|
||||
)
|
||||
|
||||
@@ -599,6 +599,7 @@ class StockController(AccountsController):
|
||||
inspection_fieldname_map = {
|
||||
"Purchase Receipt": "inspection_required_before_purchase",
|
||||
"Purchase Invoice": "inspection_required_before_purchase",
|
||||
"Subcontracting Receipt": "inspection_required_before_purchase",
|
||||
"Sales Invoice": "inspection_required_before_delivery",
|
||||
"Delivery Note": "inspection_required_before_delivery",
|
||||
}
|
||||
|
||||
@@ -55,6 +55,23 @@ class SubcontractingController(StockController):
|
||||
else:
|
||||
super(SubcontractingController, self).validate()
|
||||
|
||||
def validate_rejected_warehouse(self):
|
||||
for item in self.get("items"):
|
||||
if flt(item.rejected_qty) and not item.rejected_warehouse:
|
||||
if self.rejected_warehouse:
|
||||
item.rejected_warehouse = self.rejected_warehouse
|
||||
else:
|
||||
frappe.throw(
|
||||
_("Row #{0}: Rejected Warehouse is mandatory for the rejected Item {1}").format(
|
||||
item.idx, item.item_code
|
||||
)
|
||||
)
|
||||
|
||||
if item.get("rejected_warehouse") and (item.get("rejected_warehouse") == item.get("warehouse")):
|
||||
frappe.throw(
|
||||
_("Row #{0}: Accepted Warehouse and Rejected Warehouse cannot be same").format(item.idx)
|
||||
)
|
||||
|
||||
def remove_empty_rows(self):
|
||||
for key in ["service_items", "items", "supplied_items"]:
|
||||
if self.get(key):
|
||||
@@ -80,23 +97,27 @@ class SubcontractingController(StockController):
|
||||
if not is_stock_item:
|
||||
frappe.throw(_("Row {0}: Item {1} must be a stock item.").format(item.idx, item.item_name))
|
||||
|
||||
if not is_sub_contracted_item:
|
||||
frappe.throw(
|
||||
_("Row {0}: Item {1} must be a subcontracted item.").format(item.idx, item.item_name)
|
||||
)
|
||||
if not item.get("is_scrap_item"):
|
||||
if not is_sub_contracted_item:
|
||||
frappe.throw(
|
||||
_("Row {0}: Item {1} must be a subcontracted item.").format(item.idx, item.item_name)
|
||||
)
|
||||
|
||||
if item.bom:
|
||||
bom = frappe.get_doc("BOM", item.bom)
|
||||
if not bom.is_active:
|
||||
frappe.throw(
|
||||
_("Row {0}: Please select an active BOM for Item {1}.").format(item.idx, item.item_name)
|
||||
)
|
||||
if bom.item != item.item_code:
|
||||
frappe.throw(
|
||||
_("Row {0}: Please select an valid BOM for Item {1}.").format(item.idx, item.item_name)
|
||||
)
|
||||
if item.bom:
|
||||
is_active, bom_item = frappe.get_value("BOM", item.bom, ["is_active", "item"])
|
||||
|
||||
if not is_active:
|
||||
frappe.throw(
|
||||
_("Row {0}: Please select an active BOM for Item {1}.").format(item.idx, item.item_name)
|
||||
)
|
||||
if bom_item != item.item_code:
|
||||
frappe.throw(
|
||||
_("Row {0}: Please select an valid BOM for Item {1}.").format(item.idx, item.item_name)
|
||||
)
|
||||
else:
|
||||
frappe.throw(_("Row {0}: Please select a BOM for Item {1}.").format(item.idx, item.item_name))
|
||||
else:
|
||||
frappe.throw(_("Row {0}: Please select a BOM for Item {1}.").format(item.idx, item.item_name))
|
||||
item.bom = None
|
||||
|
||||
def __get_data_before_save(self):
|
||||
item_dict = {}
|
||||
@@ -783,7 +804,7 @@ class SubcontractingController(StockController):
|
||||
{
|
||||
"item_code": item.rm_item_code,
|
||||
"warehouse": self.supplier_warehouse,
|
||||
"actual_qty": -1 * flt(item.consumed_qty),
|
||||
"actual_qty": -1 * flt(item.consumed_qty, item.precision("consumed_qty")),
|
||||
"dependant_sle_voucher_detail_no": item.reference_name,
|
||||
},
|
||||
)
|
||||
@@ -874,19 +895,24 @@ class SubcontractingController(StockController):
|
||||
|
||||
if self.total_additional_costs:
|
||||
if self.distribute_additional_costs_based_on == "Amount":
|
||||
total_amt = sum(flt(item.amount) for item in self.get("items"))
|
||||
total_amt = sum(
|
||||
flt(item.amount) for item in self.get("items") if not item.get("is_scrap_item")
|
||||
)
|
||||
for item in self.items:
|
||||
item.additional_cost_per_qty = (
|
||||
(item.amount * self.total_additional_costs) / total_amt
|
||||
) / item.qty
|
||||
if not item.get("is_scrap_item"):
|
||||
item.additional_cost_per_qty = (
|
||||
(item.amount * self.total_additional_costs) / total_amt
|
||||
) / item.qty
|
||||
else:
|
||||
total_qty = sum(flt(item.qty) for item in self.get("items"))
|
||||
total_qty = sum(flt(item.qty) for item in self.get("items") if not item.get("is_scrap_item"))
|
||||
additional_cost_per_qty = self.total_additional_costs / total_qty
|
||||
for item in self.items:
|
||||
item.additional_cost_per_qty = additional_cost_per_qty
|
||||
if not item.get("is_scrap_item"):
|
||||
item.additional_cost_per_qty = additional_cost_per_qty
|
||||
else:
|
||||
for item in self.items:
|
||||
item.additional_cost_per_qty = 0
|
||||
if not item.get("is_scrap_item"):
|
||||
item.additional_cost_per_qty = 0
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_current_stock(self):
|
||||
|
||||
@@ -193,7 +193,9 @@ class calculate_taxes_and_totals(object):
|
||||
|
||||
item.net_rate = item.rate
|
||||
|
||||
if not item.qty and self.doc.get("is_return"):
|
||||
if (
|
||||
not item.qty and self.doc.get("is_return") and self.doc.get("doctype") != "Purchase Receipt"
|
||||
):
|
||||
item.amount = flt(-1 * item.rate, item.precision("amount"))
|
||||
elif not item.qty and self.doc.get("is_debit_note"):
|
||||
item.amount = flt(item.rate, item.precision("amount"))
|
||||
|
||||
@@ -55,6 +55,7 @@ class TestAccountsController(FrappeTestCase):
|
||||
10 series - Sales Invoice against Payment Entries
|
||||
20 series - Sales Invoice against Journals
|
||||
30 series - Sales Invoice against Credit Notes
|
||||
40 series - Company default Cost center is unset
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
@@ -941,6 +942,60 @@ class TestAccountsController(FrappeTestCase):
|
||||
self.assertEqual(exc_je_for_si, [])
|
||||
self.assertEqual(exc_je_for_je, [])
|
||||
|
||||
def test_24_journal_against_multiple_invoices(self):
|
||||
si1 = self.create_sales_invoice(qty=1, conversion_rate=80, rate=1)
|
||||
si2 = self.create_sales_invoice(qty=1, conversion_rate=80, rate=1)
|
||||
|
||||
# Payment
|
||||
je = self.create_journal_entry(
|
||||
acc1=self.debit_usd,
|
||||
acc1_exc_rate=75,
|
||||
acc2=self.cash,
|
||||
acc1_amount=-2,
|
||||
acc2_amount=-150,
|
||||
acc2_exc_rate=1,
|
||||
)
|
||||
je.accounts[0].party_type = "Customer"
|
||||
je.accounts[0].party = self.customer
|
||||
je = je.save().submit()
|
||||
|
||||
pr = self.create_payment_reconciliation()
|
||||
pr.get_unreconciled_entries()
|
||||
self.assertEqual(len(pr.invoices), 2)
|
||||
self.assertEqual(len(pr.payments), 1)
|
||||
invoices = [x.as_dict() for x in pr.invoices]
|
||||
payments = [x.as_dict() for x in pr.payments]
|
||||
pr.allocate_entries(frappe._dict({"invoices": invoices, "payments": payments}))
|
||||
pr.reconcile()
|
||||
self.assertEqual(len(pr.invoices), 0)
|
||||
self.assertEqual(len(pr.payments), 0)
|
||||
|
||||
si1.reload()
|
||||
si2.reload()
|
||||
|
||||
self.assertEqual(si1.outstanding_amount, 0)
|
||||
self.assertEqual(si2.outstanding_amount, 0)
|
||||
self.assert_ledger_outstanding(si1.doctype, si1.name, 0.0, 0.0)
|
||||
self.assert_ledger_outstanding(si2.doctype, si2.name, 0.0, 0.0)
|
||||
|
||||
# Exchange Gain/Loss Journal should've been created
|
||||
# remove payment JE from list
|
||||
exc_je_for_si1 = [x for x in self.get_journals_for(si1.doctype, si1.name) if x.parent != je.name]
|
||||
exc_je_for_si2 = [x for x in self.get_journals_for(si2.doctype, si2.name) if x.parent != je.name]
|
||||
exc_je_for_je = [x for x in self.get_journals_for(je.doctype, je.name) if x.parent != je.name]
|
||||
self.assertEqual(len(exc_je_for_si1), 1)
|
||||
self.assertEqual(len(exc_je_for_si2), 1)
|
||||
self.assertEqual(len(exc_je_for_je), 2)
|
||||
|
||||
si1.cancel()
|
||||
# Gain/Loss JE of si1 should've been cancelled
|
||||
exc_je_for_si1 = [x for x in self.get_journals_for(si1.doctype, si1.name) if x.parent != je.name]
|
||||
exc_je_for_si2 = [x for x in self.get_journals_for(si2.doctype, si2.name) if x.parent != je.name]
|
||||
exc_je_for_je = [x for x in self.get_journals_for(je.doctype, je.name) if x.parent != je.name]
|
||||
self.assertEqual(len(exc_je_for_si1), 0)
|
||||
self.assertEqual(len(exc_je_for_si2), 1)
|
||||
self.assertEqual(len(exc_je_for_je), 1)
|
||||
|
||||
def test_30_cr_note_against_sales_invoice(self):
|
||||
"""
|
||||
Reconciling Cr Note against Sales Invoice, both having different exchange rates
|
||||
@@ -997,3 +1052,139 @@ class TestAccountsController(FrappeTestCase):
|
||||
si.reload()
|
||||
self.assertEqual(si.outstanding_amount, 1)
|
||||
self.assert_ledger_outstanding(si.doctype, si.name, 80.0, 1.0)
|
||||
|
||||
def test_40_cost_center_from_payment_entry(self):
|
||||
"""
|
||||
Gain/Loss JE should inherit cost center from payment if company default is unset
|
||||
"""
|
||||
# remove default cost center
|
||||
cc = frappe.db.get_value("Company", self.company, "cost_center")
|
||||
frappe.db.set_value("Company", self.company, "cost_center", None)
|
||||
|
||||
rate_in_account_currency = 1
|
||||
si = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_submit=True)
|
||||
si.cost_center = None
|
||||
si.save().submit()
|
||||
|
||||
pe = get_payment_entry(si.doctype, si.name)
|
||||
pe.source_exchange_rate = 75
|
||||
pe.received_amount = 75
|
||||
pe.cost_center = self.cost_center
|
||||
pe = pe.save().submit()
|
||||
|
||||
# Exchange Gain/Loss Journal should've been created.
|
||||
exc_je_for_si = self.get_journals_for(si.doctype, si.name)
|
||||
exc_je_for_pe = self.get_journals_for(pe.doctype, pe.name)
|
||||
self.assertNotEqual(exc_je_for_si, [])
|
||||
self.assertEqual(len(exc_je_for_si), 1)
|
||||
self.assertEqual(len(exc_je_for_pe), 1)
|
||||
self.assertEqual(exc_je_for_si[0], exc_je_for_pe[0])
|
||||
|
||||
self.assertEqual(
|
||||
[self.cost_center, self.cost_center],
|
||||
frappe.db.get_all(
|
||||
"Journal Entry Account", filters={"parent": exc_je_for_si[0].parent}, pluck="cost_center"
|
||||
),
|
||||
)
|
||||
frappe.db.set_value("Company", self.company, "cost_center", cc)
|
||||
|
||||
def test_41_cost_center_from_journal_entry(self):
|
||||
"""
|
||||
Gain/Loss JE should inherit cost center from payment if company default is unset
|
||||
"""
|
||||
# remove default cost center
|
||||
cc = frappe.db.get_value("Company", self.company, "cost_center")
|
||||
frappe.db.set_value("Company", self.company, "cost_center", None)
|
||||
|
||||
rate_in_account_currency = 1
|
||||
si = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_submit=True)
|
||||
si.cost_center = None
|
||||
si.save().submit()
|
||||
|
||||
je = self.create_journal_entry(
|
||||
acc1=self.debit_usd,
|
||||
acc1_exc_rate=75,
|
||||
acc2=self.cash,
|
||||
acc1_amount=-1,
|
||||
acc2_amount=-75,
|
||||
acc2_exc_rate=1,
|
||||
)
|
||||
je.accounts[0].party_type = "Customer"
|
||||
je.accounts[0].party = self.customer
|
||||
je.accounts[0].cost_center = self.cost_center
|
||||
je = je.save().submit()
|
||||
|
||||
# Reconcile
|
||||
pr = self.create_payment_reconciliation()
|
||||
pr.get_unreconciled_entries()
|
||||
self.assertEqual(len(pr.invoices), 1)
|
||||
self.assertEqual(len(pr.payments), 1)
|
||||
invoices = [x.as_dict() for x in pr.invoices]
|
||||
payments = [x.as_dict() for x in pr.payments]
|
||||
pr.allocate_entries(frappe._dict({"invoices": invoices, "payments": payments}))
|
||||
pr.reconcile()
|
||||
self.assertEqual(len(pr.invoices), 0)
|
||||
self.assertEqual(len(pr.payments), 0)
|
||||
|
||||
# Exchange Gain/Loss Journal should've been created.
|
||||
exc_je_for_si = [x for x in self.get_journals_for(si.doctype, si.name) if x.parent != je.name]
|
||||
exc_je_for_je = [x for x in self.get_journals_for(je.doctype, je.name) if x.parent != je.name]
|
||||
self.assertNotEqual(exc_je_for_si, [])
|
||||
self.assertEqual(len(exc_je_for_si), 1)
|
||||
self.assertEqual(len(exc_je_for_je), 1)
|
||||
self.assertEqual(exc_je_for_si[0], exc_je_for_je[0])
|
||||
|
||||
self.assertEqual(
|
||||
[self.cost_center, self.cost_center],
|
||||
frappe.db.get_all(
|
||||
"Journal Entry Account", filters={"parent": exc_je_for_si[0].parent}, pluck="cost_center"
|
||||
),
|
||||
)
|
||||
frappe.db.set_value("Company", self.company, "cost_center", cc)
|
||||
|
||||
def test_42_cost_center_from_cr_note(self):
|
||||
"""
|
||||
Gain/Loss JE should inherit cost center from payment if company default is unset
|
||||
"""
|
||||
# remove default cost center
|
||||
cc = frappe.db.get_value("Company", self.company, "cost_center")
|
||||
frappe.db.set_value("Company", self.company, "cost_center", None)
|
||||
|
||||
rate_in_account_currency = 1
|
||||
si = self.create_sales_invoice(qty=1, rate=rate_in_account_currency, do_not_submit=True)
|
||||
si.cost_center = None
|
||||
si.save().submit()
|
||||
|
||||
cr_note = self.create_sales_invoice(qty=-1, conversion_rate=75, rate=1, do_not_save=True)
|
||||
cr_note.cost_center = self.cost_center
|
||||
cr_note.is_return = 1
|
||||
cr_note.save().submit()
|
||||
|
||||
# Reconcile
|
||||
pr = self.create_payment_reconciliation()
|
||||
pr.get_unreconciled_entries()
|
||||
self.assertEqual(len(pr.invoices), 1)
|
||||
self.assertEqual(len(pr.payments), 1)
|
||||
invoices = [x.as_dict() for x in pr.invoices]
|
||||
payments = [x.as_dict() for x in pr.payments]
|
||||
pr.allocate_entries(frappe._dict({"invoices": invoices, "payments": payments}))
|
||||
pr.reconcile()
|
||||
self.assertEqual(len(pr.invoices), 0)
|
||||
self.assertEqual(len(pr.payments), 0)
|
||||
|
||||
# Exchange Gain/Loss Journal should've been created.
|
||||
exc_je_for_si = self.get_journals_for(si.doctype, si.name)
|
||||
exc_je_for_cr_note = self.get_journals_for(cr_note.doctype, cr_note.name)
|
||||
self.assertNotEqual(exc_je_for_si, [])
|
||||
self.assertEqual(len(exc_je_for_si), 2)
|
||||
self.assertEqual(len(exc_je_for_cr_note), 2)
|
||||
self.assertEqual(exc_je_for_si, exc_je_for_cr_note)
|
||||
|
||||
for x in exc_je_for_si + exc_je_for_cr_note:
|
||||
with self.subTest(x=x):
|
||||
self.assertEqual(
|
||||
[self.cost_center, self.cost_center],
|
||||
frappe.db.get_all("Journal Entry Account", filters={"parent": x.parent}, pluck="cost_center"),
|
||||
)
|
||||
|
||||
frappe.db.set_value("Company", self.company, "cost_center", cc)
|
||||
|
||||
@@ -1090,7 +1090,7 @@ def get_subcontracting_order(**args):
|
||||
po = frappe.get_doc("Purchase Order", args.get("po_name"))
|
||||
|
||||
if po.is_subcontracted:
|
||||
return create_subcontracting_order(po_name=po.name, **args)
|
||||
return create_subcontracting_order(**args)
|
||||
|
||||
if not args.service_items:
|
||||
service_items = [
|
||||
|
||||
Reference in New Issue
Block a user