def set_expense_account(self, for_validate=False): auto_accounting_for_stock = cint(frappe.defaults.get_global_default("auto_accounting_for_stock")) if auto_accounting_for_stock: stock_not_billed_account = self.get_company_default("stock_received_but_not_billed") stock_items = self.get_stock_items() if self.update_stock: self.validate_item_code() self.validate_warehouse() warehouse_account = get_warehouse_account() for item in self.get("items"): # in case of auto inventory accounting, # expense account is always "Stock Received But Not Billed" for a stock item # except epening entry, drop-ship entry and fixed asset items if auto_accounting_for_stock and item.item_code in stock_items \ and self.is_opening == 'No' and not item.is_fixed_asset \ and (not item.po_detail or not frappe.db.get_value("Purchase Order Item", item.po_detail, "delivered_by_supplier")): if self.update_stock: item.expense_account = warehouse_account[item.warehouse]["name"] else: item.expense_account = stock_not_billed_account elif not item.expense_account and for_validate: throw(_("Expense account is mandatory for item {0}").format(item.item_code or item.item_name))
def execute(): if not cint(frappe.defaults.get_global_default("auto_accounting_for_stock")): return wh_account = get_warehouse_account() for pi in frappe.get_all("Purchase Invoice", filters={"docstatus": 1, "update_stock": 1}): pi_doc = frappe.get_doc("Purchase Invoice", pi.name) items, warehouses = pi_doc.get_items_and_warehouses() update_gl_entries_after(pi_doc.posting_date, pi_doc.posting_time, warehouses, items, wh_account) frappe.db.commit()
def make_item_gl_entries(self, gl_entries): # item gl entries stock_items = self.get_stock_items() expenses_included_in_valuation = self.get_company_default("expenses_included_in_valuation") warehouse_account = get_warehouse_account() for item in self.get("items"): if flt(item.base_net_amount): account_currency = get_account_currency(item.expense_account) if self.update_stock and self.auto_accounting_for_stock and item.item_code in stock_items: val_rate_db_precision = 6 if cint(item.precision("valuation_rate")) <= 6 else 9 # warehouse account warehouse_debit_amount = flt(flt(item.valuation_rate, val_rate_db_precision) * flt(item.qty) * flt(item.conversion_factor), item.precision("base_net_amount")) gl_entries.append( self.get_gl_dict({ "account": item.expense_account, "against": self.supplier, "debit": warehouse_debit_amount, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "cost_center": item.cost_center, "project": item.project }, account_currency) ) # Amount added through landed-cost-voucher if flt(item.landed_cost_voucher_amount): gl_entries.append(self.get_gl_dict({ "account": expenses_included_in_valuation, "against": item.expense_account, "cost_center": item.cost_center, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "credit": flt(item.landed_cost_voucher_amount), "project": item.project })) # sub-contracting warehouse if flt(item.rm_supp_cost): supplier_warehouse_account = warehouse_account[self.supplier_warehouse]["name"] gl_entries.append(self.get_gl_dict({ "account": supplier_warehouse_account, "against": item.expense_account, "cost_center": item.cost_center, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), "credit": flt(item.rm_supp_cost) }, warehouse_account[self.supplier_warehouse]["account_currency"])) else: gl_entries.append( self.get_gl_dict({ "account": item.expense_account, "against": self.supplier, "debit": flt(item.base_net_amount, item.precision("base_net_amount")), "debit_in_account_currency": (flt(item.base_net_amount, item.precision("base_net_amount")) if account_currency==self.company_currency else flt(item.net_amount, item.precision("net_amount"))), "cost_center": item.cost_center, "project": item.project }, account_currency) ) if self.auto_accounting_for_stock and self.is_opening == "No" and \ item.item_code in stock_items and item.item_tax_amount: # Post reverse entry for Stock-Received-But-Not-Billed if it is booked in Purchase Receipt if item.purchase_receipt: negative_expense_booked_in_pr = frappe.db.sql("""select name from `tabGL Entry` where voucher_type='Purchase Receipt' and voucher_no=%s and account=%s""", (item.purchase_receipt, self.expenses_included_in_valuation)) if not negative_expense_booked_in_pr: gl_entries.append( self.get_gl_dict({ "account": self.stock_received_but_not_billed, "against": self.supplier, "debit": flt(item.item_tax_amount, item.precision("item_tax_amount")), "remarks": self.remarks or "Accounting Entry for Stock" }) ) self.negative_expense_to_be_booked += flt(item.item_tax_amount, \ item.precision("item_tax_amount"))