Skip to content

Commit

Permalink
fix: Reverse provisional entries on Purchase Invoice cancel
Browse files Browse the repository at this point in the history
(cherry picked from commit 61fa4eb)
  • Loading branch information
deepeshgarg007 authored and mergify[bot] committed Jun 7, 2022
1 parent 149c603 commit 1fba432
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,10 @@ def on_submit(self):

def make_gl_entries(self, gl_entries=None, from_repost=False):
if not gl_entries:
gl_entries = self.get_gl_entries()
if self.docstatus == 1:
gl_entries = self.get_gl_entries()
else:
gl_entries = self.get_gl_entries(cancel=1)

if gl_entries:
update_outstanding = "No" if (cint(self.is_paid) or self.write_off_account) else "Yes"
Expand All @@ -540,7 +543,10 @@ def make_gl_entries(self, gl_entries=None, from_repost=False):
from_repost=from_repost,
)
elif self.docstatus == 2:
provisional_entries = [a for a in gl_entries if a.voucher_type == "Purchase Receipt"]
make_reverse_gl_entries(voucher_type=self.doctype, voucher_no=self.name)
if provisional_entries:
make_gl_entries(provisional_entries)

if update_outstanding == "No":
update_outstanding_amt(
Expand All @@ -554,7 +560,7 @@ def make_gl_entries(self, gl_entries=None, from_repost=False):
elif self.docstatus == 2 and cint(self.update_stock) and self.auto_accounting_for_stock:
make_reverse_gl_entries(voucher_type=self.doctype, voucher_no=self.name)

def get_gl_entries(self, warehouse_account=None):
def get_gl_entries(self, warehouse_account=None, cancel=0):
self.auto_accounting_for_stock = erpnext.is_perpetual_inventory_enabled(self.company)
if self.auto_accounting_for_stock:
self.stock_received_but_not_billed = self.get_company_default("stock_received_but_not_billed")
Expand All @@ -567,7 +573,7 @@ def get_gl_entries(self, warehouse_account=None):
gl_entries = []

self.make_supplier_gl_entry(gl_entries)
self.make_item_gl_entries(gl_entries)
self.make_item_gl_entries(gl_entries, cancel=cancel)
self.make_discount_gl_entries(gl_entries)

if self.check_asset_cwip_enabled():
Expand Down Expand Up @@ -634,7 +640,7 @@ def make_supplier_gl_entry(self, gl_entries):
)
)

def make_item_gl_entries(self, gl_entries):
def make_item_gl_entries(self, gl_entries, cancel=0):
# item gl entries
stock_items = self.get_stock_items()
if self.update_stock and self.auto_accounting_for_stock:
Expand Down Expand Up @@ -826,7 +832,7 @@ def make_item_gl_entries(self, gl_entries):
if expense_booked_in_pr:
# Intentionally passing purchase invoice item to handle partial billing
purchase_receipt_doc.add_provisional_gl_entry(
item, gl_entries, self.posting_date, provisional_account, reverse=1
item, gl_entries, self.posting_date, provisional_account, reverse=not cancel
)

if not self.is_internal_transfer():
Expand Down

0 comments on commit 1fba432

Please sign in to comment.