def journal_entry_purchase(self): jv = frappe.new_doc('Journal Entry') jv.voucher_type = 'Journal Entry' jv.company = self.company jv.posting_date = self.purchase_date jv.user_remark = 'Fixed Asset Purchase' td1 = jv.append("accounts"); from erpnext.accounts.party import get_party_account td1.account = get_party_account('Supplier', self.purchased_from, self.company) td1.party = self.purchased_from td1.party_type = 'Supplier' td1.set("debit_in_account_currency", self.gross_purchase_value) td1.set("is_advance", 'No') td2 = jv.append("accounts") td2.account = self.fixed_asset_account td2.set('credit_in_account_currency', self.gross_purchase_value) td2.set('is_advance', 'No') jv.insert() jv.submit() self.journal_ref = jv.name self.save() return jv
def post_journal_entry(self, customer): open_balance = float(self.process_node['OPENINGBALANCE']) jv = frappe.new_doc("Journal Entry"); jv.voucher_type = 'Opening Entry' jv.company = frappe.defaults.get_user_default("Company") jv.posting_date = self.open_date jv.user_remark = 'Customer Opening Balance' td1 = jv.append("accounts") from erpnext.accounts.party import get_party_account td1.account = get_party_account('Customer', customer.name, jv.company) td1.party = customer.name td1.party_type = 'Customer' if open_balance < 0: td1.set("debit_in_account_currency", abs(open_balance)) else: td1.set("credit_in_account_currency", abs(open_balance)) td2 = jv.append("accounts") td2.account = frappe.get_value("Account",filters={"account_name": 'Temporary Opening'}) if open_balance < 0: td2.set("credit_in_account_currency", abs(open_balance)) else: td2.set("debit_in_account_currency", abs(open_balance)) jv.save() jv.submit()
def get_payment_entry_against_order(dt, dn): ref_doc = frappe.get_doc(dt, dn) if flt(ref_doc.per_billed, 2) > 0: frappe.throw(_("Can only make payment against unbilled {0}").format(dt)) if dt == "Sales Order": party_type = "Customer" amount_field_party = "credit_in_account_currency" amount_field_bank = "debit_in_account_currency" else: party_type = "Supplier" amount_field_party = "debit_in_account_currency" amount_field_bank = "credit_in_account_currency" party_account = get_party_account(party_type, ref_doc.get(party_type.lower()), ref_doc.company) party_account_currency = get_account_currency(party_account) if party_account_currency == ref_doc.company_currency: amount = flt(ref_doc.base_grand_total) - flt(ref_doc.advance_paid) else: amount = flt(ref_doc.grand_total) - flt(ref_doc.advance_paid) return get_payment_entry(ref_doc, { "party_type": party_type, "party_account": party_account, "party_account_currency": party_account_currency, "amount_field_party": amount_field_party, "amount_field_bank": amount_field_bank, "amount": amount, "remarks": 'Advance Payment received against {0} {1}'.format(dt, dn), "is_advance": "Yes" })
def set_missing_values(self, for_validate=False): if not self.credit_to: self.credit_to = get_party_account(self.company, self.supplier, "Supplier") if not self.due_date: self.due_date = get_due_date(self.posting_date, "Supplier", self.supplier, self.company) super(PurchaseInvoice, self).set_missing_values(for_validate)
def get_payment_entry_from_purchase_order(purchase_order): """Returns new Journal Entry document as dict for given Sales Order""" from erpnext.accounts.utils import get_balance_on from erpnext.accounts.party import get_party_account po = frappe.get_doc("Purchase Order", purchase_order) if flt(po.per_billed, 2) != 0.0: frappe.throw(_("Can only make payment against unbilled Sales Order")) jv = get_payment_entry(po) jv.remark = 'Advance payment made against Purchase Order {0}.'.format(po.name) party_account = get_party_account(po.company, po.supplier, "Supplier") amount = flt(po.base_grand_total) - flt(po.advance_paid) # credit customer jv.get("accounts")[0].account = party_account jv.get("accounts")[0].party_type = "Supplier" jv.get("accounts")[0].party = po.supplier jv.get("accounts")[0].balance = get_balance_on(party_account) jv.get("accounts")[0].party_balance = get_balance_on(party=po.supplier, party_type="Supplier") jv.get("accounts")[0].debit = amount jv.get("accounts")[0].reference_type = po.doctype jv.get("accounts")[0].reference_name = po.name jv.get("accounts")[0].is_advance = "Yes" # debit bank jv.get("accounts")[1].credit = amount return jv.as_dict()
def set_missing_values(self, for_validate=False): if not self.credit_to: self.credit_to = get_party_account("Supplier", self.supplier, self.company) self.party_account_currency = frappe.db.get_value("Account", self.credit_to, "account_currency", cache=True) if not self.due_date: self.due_date = get_due_date(self.posting_date, "Supplier", self.supplier, self.company, self.bill_date) super(PurchaseInvoice, self).set_missing_values(for_validate)
def get_party_details(party_type, party, company): party_account = get_party_account(party_type, party, company) # frappe.msgprint("Customer Account is {0}" .format(customer_account)) if party_account: party_currency = get_account_currency(party_account) else: party_currency = None return party_account, party_currency
def get_party_account_and_balance(company, party_type, party): from erpnext.accounts.party import get_party_account account = get_party_account(company, party, party_type) account_balance = get_balance_on(account=account) party_balance = get_balance_on(party_type=party_type, party=party) return {"account": account, "balance": account_balance, "party_balance": party_balance}
def set_missing_values(self, for_validate=False): self.set_pos_fields(for_validate) if not self.debit_to: self.debit_to = get_party_account(self.company, self.customer, "Customer") if not self.due_date and self.customer: self.due_date = get_due_date(self.posting_date, "Customer", self.customer, self.company) super(SalesInvoice, self).set_missing_values(for_validate)
def set_missing_values(self, for_validate=False): if not self.credit_to: self.credit_to = get_party_account("Supplier", self.supplier, self.company) if not self.due_date: self.due_date = get_due_date(self.posting_date, "Supplier", self.supplier, self.company) super(PurchaseInvoice, self).set_missing_values(for_validate)
def set_missing_values(self, for_validate=False): self.set_pos_fields(for_validate) if not self.debit_to: self.debit_to = get_party_account(self.company, self.customer, "Customer") if not self.due_date: self.due_date = get_due_date(self.posting_date, "Customer", self.customer, self.company) super(SalesInvoice, self).set_missing_values(for_validate)
def get_party_account_and_balance(company, party_type, party): if not frappe.has_permission("Account"): frappe.msgprint(_("No Permission"), raise_exception=1) account = get_party_account(party_type, party, company) account_balance = get_balance_on(account=account) party_balance = get_balance_on(party_type=party_type, party=party) return {"account": account, "balance": account_balance, "party_balance": party_balance}
def _make_new_pe(member): pe = frappe.new_doc("Payment Entry") pe.payment_type = "Receive" pe.party_type = "Customer" pe.party = member.customer pe.party_name = member.member_name company = frappe.db.get_value("Gym Settings", None, "default_company") pe.party_account = get_party_account("Customer", member.customer, company) pe.paid_from = pe.party_account return pe
def update_gl(self): buyer_account = get_party_account(self.company, self.to_customer, "Customer") seller_account = get_party_account(self.company, self.from_customer, "Customer") gl_entries = [] remark = "Sale of {} {} from {} to {}".format(self.qty, self.item, self.from_customer, self.to_customer) gl_entries.append( self.get_gl_dict({ "account": buyer_account, "debit": self.sale_amount, "remarks": remark })) gl_entries.append( self.get_gl_dict({ "account": seller_account, "credit": self.purchase_amount, "remarks": remark })) income = self.sale_amount - self.purchase_amount if income != 0: adjustment_gl_entry = self.get_gl_dict({ "account": "Sales - AL", "remarks": remark, "cost_center": "Main - AL" }) adjustment_gl_entry['credit' if income else 'debit'] = income gl_entries.append(adjustment_gl_entry) make_gl_entries(gl_entries, cancel=(self.docstatus == 2), update_outstanding='Yes', merge_entries=False)
def _make_new_pe(member): pe = frappe.new_doc('Payment Entry') pe.payment_type = 'Receive' pe.party_type = 'Customer' pe.party = member.customer pe.party_name = member.member_name company = frappe.db.get_value('Gym Settings', None, 'default_company') pe.party_account = get_party_account('Customer', member.customer, company) pe.paid_from = pe.party_account return pe
def create_payment_entry(self, submit=True): """create entry""" frappe.flags.ignore_account_permission = True ref_doc = frappe.get_doc(self.reference_doctype, self.reference_name) if self.reference_doctype == "Sales Invoice": party_account = ref_doc.debit_to elif self.reference_doctype == "Purchase Invoice": party_account = ref_doc.credit_to else: party_account = get_party_account("Customer", ref_doc.get("customer"), ref_doc.company) party_account_currency = ref_doc.get( "party_account_currency") or get_account_currency(party_account) bank_amount = self.grand_total if party_account_currency == ref_doc.company_currency and party_account_currency != self.currency: party_amount = ref_doc.base_grand_total else: party_amount = self.grand_total payment_entry = get_payment_entry(self.reference_doctype, self.reference_name, party_amount=party_amount, bank_account=self.payment_account, bank_amount=bank_amount) payment_entry.update({ "reference_no": self.name, "reference_date": nowdate(), "remarks": "Payment Entry against {0} {1} via Payment Request {2}".format( self.reference_doctype, self.reference_name, self.name) }) if payment_entry.difference_amount: company_details = get_company_defaults(ref_doc.company) payment_entry.append( "deductions", { "account": company_details.exchange_gain_loss_account, "cost_center": company_details.cost_center, "amount": payment_entry.difference_amount }) if submit: payment_entry.insert(ignore_permissions=True) payment_entry.submit() return payment_entry
def _get_account_(self, company, account): """ Hack to get account, will be refactored when erp will get ability. :param company: :param account: :return: """ acc = get_party_account(company, account, "Supplier") if not acc: acc = get_party_account(company, account, "Customer") if not acc: abbr = frappe.db.get_value("Company", company, "abbr") if frappe.db.exists("Account", "{} - {}".format(account, abbr)): acc = "{} - {}".format(account, abbr) if not acc: frappe.throw("Account not found for {} in {}".format( account, abbr)) return acc
def set_missing_values(self, for_validate=False): if not self.doc.credit_to: self.doc.credit_to = get_party_account(self.doc.company, self.doc.supplier, "Supplier") if not self.doc.due_date: self.doc.due_date = get_due_date(self.doc.posting_date, self.doc.supplier, "Supplier", self.doc.credit_to, self.doc.company) super(DocType, self).set_missing_values(for_validate)
def set_missing_values(self, for_validate=False): if not self.credit_to: self.credit_to = get_party_account("Supplier", self.supplier, self.company) self.party_account_currency = frappe.db.get_value( "Account", self.credit_to, "account_currency", cache=True) if not self.due_date: self.due_date = get_due_date(self.posting_date, "Supplier", self.supplier, self.company, self.bill_date) super(PurchaseInvoice, self).set_missing_values(for_validate)
def on_submit(self): company = erpnext.get_default_company() currency = frappe.db.get_value('Company', company, 'default_currency') from erpnext.accounts.party import get_party_account account = frappe.db.sql( "select default_account from `tabMode of Payment Account` where parent='%s' and company='%s'" % (self.mode_of_payment, company)) if not account: frappe.throw( _("Please Select default account in mode of payment for {0}". format(self.mode_of_payment))) if account: je = frappe.new_doc("Journal Entry") je.voucher_type = "Journal Entry" je.posting_date = nowdate() je.company = company je.remark = "Entry against {0} worth {1}".format( self.name, self.rent) je.append( "accounts", { "account": get_party_account("Customer", self.customer, company), "credit_in_account_currency": self.rent, "reference_type": "Hall Contract", "reference_name": self.name, "party": self.customer, "party_type": "Customer" }) je.append( "accounts", { "account": account[0][0], "debit_in_account_currency": self.rent, "reference_type": "Hall Contract", "reference_name": self.name, "cost_center": frappe.db.get_value('Company', company, 'cost_center') }) je.flags.ignore_permissions = True je.submit() frappe.db.set_value("Hall Contract", self.name, 'payment_received_against', je.name)
def set_missing_values(self, for_validate=False): pos = self.set_pos_fields(for_validate) if not self.debit_to: self.debit_to = get_party_account("Customer", self.customer, self.company) if not self.due_date and self.customer: self.due_date = get_due_date(self.posting_date, "Customer", self.customer, self.company) super(SalesInvoice, self).set_missing_values(for_validate) if pos: return {"print_format": pos.get("print_format") }
def get_party_account_and_balance(company, party_type, party): from erpnext.accounts.party import get_party_account account = get_party_account(company, party, party_type) account_balance = get_balance_on(account=account) party_balance = get_balance_on(party_type=party_type, party=party) return { "account": account, "balance": account_balance, "party_balance": party_balance }
def get_payment_entry_from_sales_order(sales_order): """Returns new Journal Entry document as dict for given Sales Order""" from erpnext.accounts.utils import get_balance_on from erpnext.accounts.party import get_party_account so = frappe.get_doc("Sales Order", sales_order) if flt(so.per_billed, 2) != 0.0: frappe.throw(_("Can only make payment against unbilled Sales Order")) jv = get_payment_entry(so) jv.remark = 'Advance payment received against Sales Order {0}.'.format( so.name) party_account = get_party_account("Customer", so.customer, so.company) party_account_currency = get_account_currency(party_account) exchange_rate = get_exchange_rate(party_account, party_account_currency, so.company) if party_account_currency == so.company_currency: amount = flt(so.base_grand_total) - flt(so.advance_paid) else: amount = flt(so.grand_total) - flt(so.advance_paid) # credit customer row1 = jv.get("accounts")[0] row1.account = party_account row1.account_currency = party_account_currency row1.party_type = "Customer" row1.party = so.customer row1.balance = get_balance_on(party_account) row1.party_balance = get_balance_on(party=so.customer, party_type="Customer") row1.credit_in_account_currency = amount row1.reference_type = so.doctype row1.reference_name = so.name row1.is_advance = "Yes" row1.exchange_rate = exchange_rate row1.account_type = "Receivable" # debit bank row2 = jv.get("accounts")[1] if row2.account_currency == party_account_currency: row2.debit_in_account_currency = amount else: row2.debit_in_account_currency = amount * exchange_rate # set multi currency check if row1.account_currency != so.company_currency or row2.account_currency != so.company_currency: jv.multi_currency = 1 return jv.as_dict()
def make_payment_entry(dt, dn): doc = frappe.get_doc(dt, dn) doc.validate_party_type_for_payment_entry() payment_entry = frappe.new_doc("Payment Entry") default_company = frappe.defaults.get_global_default("company") company_currency = frappe.get_value("Company", default_company, "default_currency") party_account = get_party_account(doc.party_type, doc.party or doc.other_party, default_company) party_name = frappe.get_value(doc.party_type, doc.party, "{}_name".format(doc.party_type.lower())) payment_entry.payment_type = "Pay" payment_entry.company = default_company payment_entry.posting_date = nowdate() payment_entry.mode_of_payment = doc.get("mode_of_payment") payment_entry.party_type = doc.party_type payment_entry.party_balance = get_balance_on(party_account, nowdate(), doc.party_type, doc.party, default_company) payment_entry.party = doc.party or doc.other_party payment_entry.party_name = party_name or doc.other_party payment_entry.paid_from = doc.disbursement_account payment_entry.paid_to = party_account payment_entry.paid_from_account_currency = get_account_currency(doc.disbursement_account) or doc.currency payment_entry.paid_to_account_currency = get_account_currency(party_account) or doc.currency payment_entry.paid_from_account_balance = get_balance_on(doc.disbursement_account, nowdate()) payment_entry.paid_to_account_balance = get_balance_on(party_account, nowdate(), company=default_company) payment_entry.paid_amount = doc.approved_amount payment_entry.received_amount = doc.approved_amount payment_entry.allocate_payment_amount = 1 payment_entry.payment_request = doc.name # payment_entry.letter_head = doc.get("letter_head") if doc.transaction_type and doc.transaction_name: if doc.transaction_type == "Purchase Order": trans = frappe.get_doc(doc.transaction_type, doc.transaction_name) payment_entry.append("references", { "reference_doctype": doc.transaction_type, "reference_name": doc.transaction_name, "bill_no": doc.get("reference_name"), "due_date": doc.get("reference_date"), "total_amount": trans.total, "outstanding_amount": 0.000, "allocated_amount": trans.total }) payment_entry.setup_party_account_field() # payment_entry.set_missing_values() payment_entry.set_exchange_rate() payment_entry.set_amounts() return payment_entry.as_dict()
def set_missing_values_custom(self): from frappe import scrub from erpnext.accounts.utils import get_account_currency, get_balance_on from erpnext.accounts.party import get_party_account from erpnext.accounts.doctype.payment_entry.payment_entry import get_account_details if self.payment_type == "Internal Transfer": for field in ("party", "party_balance", "total_allocated_amount", "base_total_allocated_amount", "unallocated_amount"): self.set(field, None) self.references = [] else: if not self.party_type: frappe.throw(_("Party Type is mandatory")) if not self.party: frappe.throw(_("Party is mandatory")) _party_name = "title" if self.party_type in ( "Student", "Shareholder") else scrub(self.party_type.lower()) + "_name" self.party_name = frappe.db.get_value(self.party_type, self.party, _party_name) if self.party: if not self.party_balance: self.party_balance = get_balance_on(party_type=self.party_type, party=self.party, date=self.posting_date, company=self.company) if not self.party_account: party_account = get_party_account(self.party_type, self.party, self.company) self.set(self.party_account_field, party_account) self.party_account = party_account if self.paid_from and not (self.paid_from_account_currency or self.paid_from_account_balance): acc = get_account_details(self.paid_from, self.posting_date, self.cost_center) self.paid_from_account_currency = acc.account_currency self.paid_from_account_balance = acc.account_balance if self.paid_to and not (self.paid_to_account_currency or self.paid_to_account_balance): acc = get_account_details(self.paid_to, self.posting_date, self.cost_center) self.paid_to_account_currency = acc.account_currency self.paid_to_account_balance = acc.account_balance self.party_account_currency = self.paid_from_account_currency \ if self.payment_type=="Receive" else self.paid_to_account_currency self.set_missing_ref_details()
def create_journal_entry(self): """create entry""" frappe.flags.ignore_account_permission = True ref_doc = frappe.get_doc(self.reference_doctype, self.reference_name) party_account = get_party_account("Customer", ref_doc.get("customer"), ref_doc.company) party_account_currency = get_account_currency(party_account) debit_in_account_currency = 0.0 if party_account_currency == ref_doc.company_currency: amount = flt( flt(self.grand_total) * flt(ref_doc.conversion_rate, ref_doc.precision("conversion_rate")), ref_doc.precision("base_grand_total"), ) if self.currency != ref_doc.company_currency: debit_in_account_currency = self.grand_total else: amount = debit_in_account_currency = self.grand_total if self.reference_doctype == "Sales Order": jv = get_payment_entry_against_order( self.reference_doctype, self.reference_name, amount=amount, debit_in_account_currency=debit_in_account_currency, journal_entry=True, bank_account=self.payment_account, ) if self.reference_doctype == "Sales Invoice": jv = get_payment_entry_against_invoice( self.reference_doctype, self.reference_name, amount=amount, debit_in_account_currency=debit_in_account_currency, journal_entry=True, bank_account=self.payment_account, ) jv.update({"voucher_type": "Journal Entry", "posting_date": nowdate()}) jv.insert(ignore_permissions=True) jv.submit() # set status as paid for Payment Request frappe.db.set_value(self.doctype, self.name, "status", "Paid") return jv
def set_missing_values(self, for_validate=False): if not self.credit_to: self.credit_to = get_party_account("Supplier", self.supplier, self.company) self.party_account_currency = frappe.db.get_value("Account", self.credit_to, "account_currency", cache=True) if not self.due_date: self.due_date = get_due_date(self.posting_date, "Supplier", self.supplier, self.company, self.bill_date) tds_category = frappe.db.get_value("Supplier", self.supplier, "tax_withholding_category") if tds_category and not for_validate: self.apply_tds = 1 self.tax_withholding_category = tds_category super(PurchaseInvoice, self).set_missing_values(for_validate)
def create_journal_entry(doc): if not doc.fees: return amount = doc.base_net_total debit_account = frappe.get_value("Fees", doc.fees, "sales_invoice_income_account") party_account = get_party_account("Customer", doc.customer, doc.company) jl_rows = [] debit_row = dict( account=debit_account, debit_in_account_currency=flt(amount, doc.precision("base_net_total")), exchange_rate=1, cost_center=doc.cost_center, ) jl_rows.append(debit_row) credit_row = dict( party_type="customer", party=doc.customer, account=party_account, credit_in_account_currency=flt(amount, doc.precision("base_net_total")), exchange_rate=1, cost_center=doc.cost_center, reference_type="Sales Invoice", reference_name=doc.name, ) jl_rows.append(credit_row) user_remark = "Against Sales Inoice " + doc.name + " For Customer " + doc.customer jv_doc = frappe.get_doc( dict( doctype="Journal Entry", posting_date=nowdate(), accounts=jl_rows, company=doc.company, multi_currency=0, user_remark=user_remark, )) jv_doc.flags.ignore_permissions = True frappe.flags.ignore_account_permission = True jv_doc.save() jv_doc.submit() jv_url = frappe.utils.get_url_to_form(jv_doc.doctype, jv_doc.name) si_msgprint = _("Journal Entry Created <a href='{0}'>{1}</a>").format( jv_url, jv_doc.name) frappe.msgprint(si_msgprint) return jv_doc.name
def submit_rent_receipt(employee_list, account): records = eval(employee_list) from frappe.utils import money_in_words company = erpnext.get_default_company() currency = frappe.db.get_value('Company', company, 'default_currency') from erpnext.accounts.party import get_party_account for record in records: rr = frappe.get_doc("Rent Receipt", record['name']) rr.paid_amount = record['final_rent_amount'] rr.paid_date = nowdate() je = frappe.new_doc("Journal Entry") je.voucher_type = "Journal Entry" je.posting_date = nowdate() je.company = company je.remark = "Entry against {0} worth {1}".format( record['name'], record['final_rent_amount']) je.append( "accounts", { "account": get_party_account("Customer", record['customer'], company), "credit_in_account_currency": record['final_rent_amount'], "reference_type": "Rent Receipt", "reference_name": record['name'], "party": record["customer"], "party_type": "Customer" }) je.append( "accounts", { "account": account, "debit_in_account_currency": record['final_rent_amount'], "reference_type": "Rent Receipt", "reference_name": record['name'], "cost_center": frappe.db.get_value('Company', company, 'cost_center') }) je.flags.ignore_permissions = True je.submit() rr.rent_received_against = je.name rr.submit()
def create_journal_entry(self): """create entry""" frappe.flags.ignore_account_permission = True ref_doc = frappe.get_doc(self.reference_doctype, self.reference_name) party_account = get_party_account("Customer", ref_doc.get("customer"), ref_doc.company) party_account_currency = get_account_currency(party_account) debit_in_account_currency = 0.0 if party_account_currency == ref_doc.company_currency: amount = flt(flt(self.grand_total) * \ flt(ref_doc.conversion_rate, ref_doc.precision("conversion_rate")), \ ref_doc.precision("base_grand_total")) if self.currency != ref_doc.company_currency: debit_in_account_currency = self.grand_total else: amount = debit_in_account_currency = self.grand_total if self.reference_doctype == "Sales Order": jv = get_payment_entry_against_order( self.reference_doctype, self.reference_name, amount=amount, debit_in_account_currency=debit_in_account_currency, journal_entry=True, bank_account=self.payment_account) if self.reference_doctype == "Sales Invoice": jv = get_payment_entry_against_invoice( self.reference_doctype, self.reference_name, amount=amount, debit_in_account_currency=debit_in_account_currency, journal_entry=True, bank_account=self.payment_account) jv.update({"voucher_type": "Journal Entry", "posting_date": nowdate()}) jv.insert(ignore_permissions=True) jv.submit() #set status as paid for Payment Request frappe.db.set_value(self.doctype, self.name, "status", "Paid") return jv
def get_payment_entry_from_sales_order(sales_order): """Returns new Journal Entry document as dict for given Sales Order""" from erpnext.accounts.utils import get_balance_on from erpnext.accounts.party import get_party_account so = frappe.get_doc("Sales Order", sales_order) if flt(so.per_billed, 2) != 0.0: frappe.throw(_("Can only make payment against unbilled Sales Order")) jv = get_payment_entry(so) jv.remark = 'Advance payment received against Sales Order {0}.'.format(so.name) party_account = get_party_account("Customer", so.customer, so.company) party_account_currency = get_account_currency(party_account) exchange_rate = get_exchange_rate(party_account, party_account_currency, so.company) if party_account_currency == so.company_currency: amount = flt(so.base_grand_total) - flt(so.advance_paid) else: amount = flt(so.grand_total) - flt(so.advance_paid) # credit customer row1 = jv.get("accounts")[0] row1.account = party_account row1.account_currency = party_account_currency row1.party_type = "Customer" row1.party = so.customer row1.balance = get_balance_on(party_account) row1.party_balance = get_balance_on(party=so.customer, party_type="Customer") row1.credit_in_account_currency = amount row1.reference_type = so.doctype row1.reference_name = so.name row1.is_advance = "Yes" row1.exchange_rate = exchange_rate row1.account_type = "Receivable" # debit bank row2 = jv.get("accounts")[1] if row2.account_currency == party_account_currency: row2.debit_in_account_currency = amount else: row2.debit_in_account_currency = amount * exchange_rate # set multi currency check if row1.account_currency != so.company_currency or row2.account_currency != so.company_currency: jv.multi_currency = 1 return jv.as_dict()
def get_party_account_and_balance(company, party_type, party): if not frappe.has_permission("Account"): frappe.msgprint(_("No Permission"), raise_exception=1) account = get_party_account(party_type, party, company) account_balance = get_balance_on(account=account) party_balance = get_balance_on(party_type=party_type, party=party) return { "account": account, "balance": account_balance, "party_balance": party_balance }
def make_payment(name, payment_id, payment_date, payment_amount, reverse=0, parent_cancel=0): r = frappe.get_doc('Wire Transaction', name) cancel = reverse or parent_cancel if cancel: for entry in r.payments: if entry.get_value('payment_id') == payment_id: r.remove(entry) break else: r.append( 'payments', { 'payment_id': payment_id, 'payment_date': payment_date, 'payment_amount': payment_amount }) if not parent_cancel: r.save() r.posting_date = payment_date r.company = get_default_company() party_account = get_party_account('Customer', r.customer, r.company) gl_entries = [ r.get_gl_dict({ 'account': party_account, 'account_currency': get_account_currency(party_account), 'credit_in_account_currency': payment_amount, 'credit': payment_amount, 'voucher_type': 'Wire Transaction Payment', 'voucher_no': payment_id, 'party_type': 'Customer', 'party': r.customer, 'against': r.debit_to, 'against_voucher_type': 'Wire Transaction', 'against_voucher': name, }), r.get_gl_dict({ 'account': r.debit_to, 'account_currency': get_account_currency(r.debit_to), 'debit_in_account_currency': payment_amount, 'debit': payment_amount, 'voucher_type': 'Wire Transaction Payment', 'voucher_no': payment_id, 'against': r.customer, }), ] make_gl_entries(gl_entries, cancel=cancel, adv_adj=0)
def get_payment_entry_from_purchase_order(purchase_order): """Returns new Journal Entry document as dict for given Sales Order""" from erpnext.accounts.utils import get_balance_on from erpnext.accounts.party import get_party_account po = frappe.get_doc("Purchase Order", purchase_order) if flt(po.per_billed, 2) != 0.0: frappe.throw(_("Can only make payment against unbilled Sales Order")) jv = get_payment_entry(po) jv.remark = "Advance payment made against Purchase Order {0}.".format(po.name) party_account = get_party_account(po.company, po.supplier, "Supplier") party_account_currency = frappe.db.get_value("Account", party_account, "account_currency") exchange_rate = get_exchange_rate(party_account, party_account_currency, po.company) if party_account_currency == po.company_currency: amount = flt(po.base_grand_total) - flt(po.advance_paid) else: amount = flt(po.grand_total) - flt(po.advance_paid) # credit customer row1 = jv.get("accounts")[0] row1.account = party_account row1.party_type = "Supplier" row1.party = po.supplier row1.balance = get_balance_on(party_account) row1.party_balance = get_balance_on(party=po.supplier, party_type="Supplier") row1.debit_in_account_currency = amount row1.reference_type = po.doctype row1.reference_name = po.name row1.is_advance = "Yes" row1.exchange_rate = exchange_rate row1.account_type = "Payable" # debit bank row2 = jv.get("accounts")[1] if row2.account_currency == party_account_currency: row2.credit_in_account_currency = amount else: row2.credit_in_account_currency = amount * exchange_rate # set multi currency check if row1.account_currency != po.company_currency or row2.account_currency != po.company_currency: jv.multi_currency = 1 return jv.as_dict()
def _get_empty_payment_entry(customer): company = frappe.defaults.get_user_default("company") mode_of_payment = "Cash" company_account = get_default_bank_cash_account( company, "Cash", mode_of_payment=mode_of_payment, ) party_account = get_party_account("Customer", customer, company) party_account_currency = get_account_currency(party_account) pe = frappe.new_doc("Payment Entry") pe.update({ "doctype": "Payment Entry", "payment_type": "Receive", "company": company, "cost_center": frappe.get_cached_value("Company", company, "cost_center"), "posting_date": frappe.utils.nowdate(), "mode_of_payment": mode_of_payment, "party_type": "Customer", "party": customer, "paid_from": party_account, "paid_to": company_account.account, "paid_from_account_currency": party_account_currency, "paid_to_account_currency": company_account.account_currency, "paid_amount": 0, "received_amount": 0, }) pe.setup_party_account_field() pe.set_missing_values() if party_account and company_account: pe.set_exchange_rate() pe.set_amounts() return pe
def get_party_account_and_balance(company, party_type, party, cost_center=None): if not frappe.has_permission("Account"): frappe.msgprint(_("No Permission"), raise_exception=1) account = get_party_account(party_type, party, company) account_balance = get_balance_on(account=account, cost_center=cost_center) party_balance = get_balance_on(party_type=party_type, party=party, company=company, cost_center=cost_center) return { "account": account, "balance": account_balance, "party_balance": party_balance, "account_currency": frappe.db.get_value("Account", account, "account_currency") }
def get_party_details(company, party_type, party, date): if not frappe.db.exists(party_type, party): frappe.throw(_("Invalid {0}: {1}").format(party_type, party)) party_account = get_party_account(party_type, party, company) account_currency = get_account_currency(party_account) account_balance = get_balance_on(party_account, date) party_balance = get_balance_on(party_type=party_type, party=party) return { "party_account": party_account, "party_account_currency": account_currency, "party_balance": party_balance, "account_balance": account_balance }
def get_payment_entry_against_order(dt, dn, amount=None, debit_in_account_currency=None, journal_entry=False, bank_account=None): ref_doc = frappe.get_doc(dt, dn) if flt(ref_doc.per_billed, 2) > 0: frappe.throw( _("Can only make payment against unbilled {0}").format(dt)) if dt == "Sales Order": party_type = "Customer" amount_field_party = "credit_in_account_currency" amount_field_bank = "debit_in_account_currency" else: party_type = "Supplier" amount_field_party = "debit_in_account_currency" amount_field_bank = "credit_in_account_currency" party_account = get_party_account(party_type, ref_doc.get(party_type.lower()), ref_doc.company) party_account_currency = get_account_currency(party_account) if not amount: if party_account_currency == ref_doc.company_currency: amount = flt(ref_doc.base_grand_total) - flt(ref_doc.advance_paid) else: amount = flt(ref_doc.grand_total) - flt(ref_doc.advance_paid) return get_payment_entry( ref_doc, { "party_type": party_type, "party_account": party_account, "party_account_currency": party_account_currency, "amount_field_party": amount_field_party, "amount_field_bank": amount_field_bank, "amount": amount, "debit_in_account_currency": debit_in_account_currency, "remarks": 'Advance Payment received against {0} {1}'.format( dt, dn), "is_advance": "Yes", "bank_account": bank_account, "journal_entry": journal_entry })
def set_missing_values(self): if self.payment_type == "Internal Transfer": for field in ("party", "party_balance", "total_allocated_amount", "base_total_allocated_amount", "unallocated_amount"): self.set(field, None) self.references = [] else: if not self.party_type: frappe.throw(_("Party Type is mandatory")) if not self.party: frappe.throw(_("Party is mandatory")) self.party_name = frappe.db.get_value( self.party_type, self.party, self.party_type.lower() + "_name") if self.party: if not self.party_balance: self.party_balance = get_balance_on(party_type=self.party_type, party=self.party, date=self.posting_date, company=self.company) if not self.party_account: party_account = get_party_account(self.party_type, self.party, self.company) self.set(self.party_account_field, party_account) self.party_account = party_account if self.paid_from and not (self.paid_from_account_currency or self.paid_from_account_balance): acc = get_account_details(self.paid_from, self.posting_date) self.paid_from_account_currency = acc.account_currency self.paid_from_account_balance = acc.account_balance if self.paid_to and not (self.paid_to_account_currency or self.paid_to_account_balance): acc = get_account_details(self.paid_to, self.posting_date) self.paid_to_account_currency = acc.account_currency self.paid_to_account_balance = acc.account_balance self.party_account_currency = self.paid_from_account_currency \ if self.payment_type=="Receive" else self.paid_to_account_currency self.set_missing_ref_details()
def set_party_account(self): from erpnext.accounts.party import get_party_account if self.party_account: return if self.party_type in ("Customer", "Supplier"): self.party_account = get_party_account(self.party_type, self.party, self.company) else: default_receivable = frappe.get_value("Company", self.company, "default_receivable_account") first_receivable = frappe.get_value("Account", { "account_type": "Receivable", "company": self.company, "account_currency": self.currency }) self.party_account = default_receivable or first_receivable
def create_payment_entry(self, submit=True): """create entry""" frappe.flags.ignore_account_permission = True ref_doc = frappe.get_doc(self.reference_doctype, self.reference_name) if self.reference_doctype == "Sales Invoice": party_account = ref_doc.debit_to elif self.reference_doctype == "Purchase Invoice": party_account = ref_doc.credit_to else: party_account = get_party_account("Customer", ref_doc.get("customer"), ref_doc.company) party_account_currency = ref_doc.get("party_account_currency") or get_account_currency(party_account) bank_amount = self.grand_total if party_account_currency == ref_doc.company_currency and party_account_currency != self.currency: party_amount = ref_doc.base_grand_total else: party_amount = self.grand_total payment_entry = get_payment_entry(self.reference_doctype, self.reference_name, party_amount=party_amount, bank_account=self.payment_account, bank_amount=bank_amount) payment_entry.update({ "reference_no": self.name, "reference_date": nowdate(), "remarks": "Payment Entry against {0} {1} via Payment Request {2}".format(self.reference_doctype, self.reference_name, self.name) }) if payment_entry.difference_amount: company_details = get_company_defaults(ref_doc.company) payment_entry.append("deductions", { "account": company_details.exchange_gain_loss_account, "cost_center": company_details.cost_center, "amount": payment_entry.difference_amount }) if submit: payment_entry.insert(ignore_permissions=True) payment_entry.submit() return payment_entry
def get_payer_account(company, supplier, customer, payment_type): if 'hpcl' in supplier.lower() or payment_type.lower() == "direct": acc_head = frappe.db.get_value( "Account", { "master_type": "Customer", "master_name": customer, "company": company, "account_type": "Payer" }) if not acc_head: # create company_details = frappe.db.get_value( "Company", company, ["abbr", "receivables_group", "payables_group"], as_dict=True) account = frappe.get_doc({ 'doctype': 'Account', 'account_name': '{} a/c {}'.format(supplier.split(' ')[0].title(), customer), 'parent_account': company_details.payables_group, 'group_or_ledger': 'Ledger', 'company': company, 'master_type': "Customer", 'master_name': customer, "freeze_account": "No", "report_type": "Balance Sheet", "account_type": "Payer" }).insert(ignore_permissions=True) acc_head = account.name return acc_head acc_head = get_party_account(company, supplier, "Supplier") return acc_head
def get_party_details(company, party_type, party, date): if not frappe.db.exists(party_type, party): frappe.throw(_("Invalid {0}: {1}").format(party_type, party)) party_account = get_party_account(party_type, party, company) account_currency = get_account_currency(party_account) account_balance = get_balance_on(party_account, date) _party_name = "title" if party_type == "Student" else party_type.lower() + "_name" party_name = frappe.db.get_value(party_type, party, _party_name) party_balance = get_balance_on(party_type=party_type, party=party) return { "party_account": party_account, "party_name": party_name, "party_account_currency": account_currency, "party_balance": party_balance, "account_balance": account_balance }
def journal_entry(self): self.PRECISION = 2 from fa_depreciation.fixed_asset_depreciation.doctype.fixed_asset_account.fixed_asset_account import validate_default_accounts validate_default_accounts(self.company) jv = frappe.new_doc('Journal Entry') jv.voucher_type = 'Journal Entry' jv.company = self.company jv.posting_date = self.posting_date jv.user_remark = 'Fixed Asset Sale' td1 = jv.append("accounts"); td1.account = frappe.db.get_value("Fixed Asset Account", self.fixed_asset_account,"fixed_asset_account") td1.set("credit_in_account_currency", flt(self.asset_purchase_cost,self.PRECISION)) td2 = jv.append("accounts") from erpnext.accounts.party import get_party_account td2.account = get_party_account('Customer',self.sold_to, self.company) td2.party = self.sold_to td2.party_type = 'Customer' td2.set('debit_in_account_currency', flt(self.sales_amount,self.PRECISION)) td5 = jv.append("accounts") td5.account = frappe.get_doc("Company", self.company).default_accumulated_depreciation_account td5.set('debit_in_account_currency', flt(self.accumulated_depreciation,self.PRECISION)) if self.profit_or_loss == "Loss": td3 = jv.append("accounts") td3.account = self.booking_account td3.set('debit_in_account_currency', flt(self.difference,self.PRECISION)) elif self.profit_or_loss == "Profit": td4 = jv.append("accounts") td4.account = self.booking_account td4.set('credit_in_account_currency', flt(self.difference,self.PRECISION)) jv.insert() jv.submit() self.journal_ref = jv.name self.db_update() return jv
def set_missing_values(self): if self.payment_type == "Internal Transfer": for field in ("party", "party_balance", "total_allocated_amount", "base_total_allocated_amount", "unallocated_amount"): self.set(field, None) self.references = [] else: if not self.party_type: frappe.throw(_("Party Type is mandatory")) if not self.party: frappe.throw(_("Party is mandatory")) self.party_name = frappe.db.get_value(self.party_type, self.party, self.party_type.lower() + "_name") if self.party: if not self.party_balance: self.party_balance = get_balance_on(party_type=self.party_type, party=self.party, date=self.posting_date, company=self.company) if not self.party_account: party_account = get_party_account(self.party_type, self.party, self.company) self.set(self.party_account_field, party_account) self.party_account = party_account if self.paid_from and not (self.paid_from_account_currency or self.paid_from_account_balance): acc = get_account_details(self.paid_from, self.posting_date) self.paid_from_account_currency = acc.account_currency self.paid_from_account_balance = acc.account_balance if self.paid_to and not (self.paid_to_account_currency or self.paid_to_account_balance): acc = get_account_details(self.paid_to, self.posting_date) self.paid_to_account_currency = acc.account_currency self.paid_to_account_balance = acc.account_balance self.party_account_currency = self.paid_from_account_currency \ if self.payment_type=="Receive" else self.paid_to_account_currency self.set_missing_ref_details()
def get_party_details(company, party_type, party, date, cost_center=None): bank_account = '' if not frappe.db.exists(party_type, party): frappe.throw(_("Invalid {0}: {1}").format(party_type, party)) party_account = get_party_account(party_type, party, company) account_currency = get_account_currency(party_account) account_balance = get_balance_on(party_account, date, cost_center=cost_center) _party_name = "title" if party_type in ("Student", "Shareholder") else party_type.lower() + "_name" party_name = frappe.db.get_value(party_type, party, _party_name) party_balance = get_balance_on(party_type=party_type, party=party, cost_center=cost_center) if party_type in ["Customer", "Supplier"]: bank_account = get_party_bank_account(party_type, party) return { "party_account": party_account, "party_name": party_name, "party_account_currency": account_currency, "party_balance": party_balance, "account_balance": account_balance, "bank_account": bank_account }
def get_payment_entry(dt, dn, party_amount=None, bank_account=None, bank_amount=None): doc = frappe.get_doc(dt, dn) if dt in ("Sales Order", "Purchase Order") and flt(doc.per_billed, 2) > 0: frappe.throw(_("Can only make payment against unbilled {0}").format(dt)) if dt in ("Sales Invoice", "Sales Order"): party_type = "Customer" elif dt in ("Purchase Invoice", "Purchase Order"): party_type = "Supplier" elif dt in ("Expense Claim", "Employee Advance"): party_type = "Employee" elif dt in ("Fees"): party_type = "Student" # party account if dt == "Sales Invoice": party_account = doc.debit_to elif dt == "Purchase Invoice": party_account = doc.credit_to elif dt == "Fees": party_account = doc.receivable_account elif dt == "Employee Advance": party_account = doc.advance_account else: party_account = get_party_account(party_type, doc.get(party_type.lower()), doc.company) party_account_currency = doc.get("party_account_currency") or get_account_currency(party_account) # payment type if (dt == "Sales Order" or (dt in ("Sales Invoice", "Fees") and doc.outstanding_amount > 0)) \ or (dt=="Purchase Invoice" and doc.outstanding_amount < 0): payment_type = "Receive" else: payment_type = "Pay" # amounts grand_total = outstanding_amount = 0 if party_amount: grand_total = outstanding_amount = party_amount elif dt in ("Sales Invoice", "Purchase Invoice"): if party_account_currency == doc.company_currency: grand_total = doc.base_rounded_total or doc.base_grand_total else: grand_total = doc.rounded_total or doc.grand_total outstanding_amount = doc.outstanding_amount elif dt in ("Expense Claim"): grand_total = doc.total_sanctioned_amount outstanding_amount = doc.total_sanctioned_amount \ - doc.total_amount_reimbursed - flt(doc.total_advance_amount) elif dt == "Employee Advance": grand_total = doc.advance_amount outstanding_amount = flt(doc.advance_amount) - flt(doc.paid_amount) elif dt == "Fees": grand_total = doc.grand_total outstanding_amount = doc.outstanding_amount else: if party_account_currency == doc.company_currency: grand_total = flt(doc.get("base_rounded_total") or doc.base_grand_total) else: grand_total = flt(doc.get("rounded_total") or doc.grand_total) outstanding_amount = grand_total - flt(doc.advance_paid) # bank or cash bank = get_default_bank_cash_account(doc.company, "Bank", mode_of_payment=doc.get("mode_of_payment"), account=bank_account) paid_amount = received_amount = 0 if party_account_currency == bank.account_currency: paid_amount = received_amount = abs(outstanding_amount) elif payment_type == "Receive": paid_amount = abs(outstanding_amount) if bank_amount: received_amount = bank_amount else: received_amount = abs(outstanding_amount) if bank_amount: paid_amount = bank_amount pe = frappe.new_doc("Payment Entry") pe.payment_type = payment_type pe.company = doc.company pe.posting_date = nowdate() pe.mode_of_payment = doc.get("mode_of_payment") pe.party_type = party_type pe.party = doc.get(scrub(party_type)) pe.paid_from = party_account if payment_type=="Receive" else bank.account pe.paid_to = party_account if payment_type=="Pay" else bank.account pe.paid_from_account_currency = party_account_currency \ if payment_type=="Receive" else bank.account_currency pe.paid_to_account_currency = party_account_currency if payment_type=="Pay" else bank.account_currency pe.paid_amount = paid_amount pe.received_amount = received_amount pe.allocate_payment_amount = 1 pe.letter_head = doc.get("letter_head") pe.append("references", { 'reference_doctype': dt, 'reference_name': dn, "bill_no": doc.get("bill_no"), "due_date": doc.get("due_date"), 'total_amount': grand_total, 'outstanding_amount': outstanding_amount, 'allocated_amount': outstanding_amount }) pe.setup_party_account_field() pe.set_missing_values() if party_account and bank: pe.set_exchange_rate() pe.set_amounts() return pe
def get_payment_entry(dt, dn, party_amount=None, bank_account=None, bank_amount=None): doc = frappe.get_doc(dt, dn) if dt in ("Sales Order", "Purchase Order") and flt(doc.per_billed, 2) > 0: frappe.throw(_("Can only make payment against unbilled {0}").format(dt)) party_type = "Customer" if dt in ("Sales Invoice", "Sales Order") else "Supplier" # party account if dt == "Sales Invoice": party_account = doc.debit_to elif dt == "Purchase Invoice": party_account = doc.credit_to else: party_account = get_party_account(party_type, doc.get(party_type.lower()), doc.company) party_account_currency = doc.get("party_account_currency") or get_account_currency(party_account) # payment type if (dt == "Sales Order" or (dt=="Sales Invoice" and doc.outstanding_amount > 0)) \ or (dt=="Purchase Invoice" and doc.outstanding_amount < 0): payment_type = "Receive" else: payment_type = "Pay" # amounts grand_total = outstanding_amount = 0 if party_amount: grand_total = outstanding_amount = party_amount elif dt in ("Sales Invoice", "Purchase Invoice"): grand_total = doc.grand_total outstanding_amount = doc.outstanding_amount else: total_field = "base_grand_total" if party_account_currency == doc.company_currency else "grand_total" grand_total = flt(doc.get(total_field)) outstanding_amount = grand_total - flt(doc.advance_paid) # bank or cash bank = get_default_bank_cash_account(doc.company, "Bank", mode_of_payment=doc.get("mode_of_payment"), account=bank_account) paid_amount = received_amount = 0 if party_account_currency == bank.account_currency: paid_amount = received_amount = abs(outstanding_amount) elif payment_type == "Receive": paid_amount = abs(outstanding_amount) if bank_amount: received_amount = bank_amount else: received_amount = abs(outstanding_amount) if bank_amount: paid_amount = bank_amount pe = frappe.new_doc("Payment Entry") pe.payment_type = payment_type pe.company = doc.company pe.posting_date = nowdate() pe.mode_of_payment = doc.get("mode_of_payment") pe.party_type = party_type pe.party = doc.get(scrub(party_type)) pe.paid_from = party_account if payment_type=="Receive" else bank.account pe.paid_to = party_account if payment_type=="Pay" else bank.account pe.paid_from_account_currency = party_account_currency \ if payment_type=="Receive" else bank.account_currency pe.paid_to_account_currency = party_account_currency if payment_type=="Pay" else bank.account_currency pe.paid_amount = paid_amount pe.received_amount = received_amount pe.allocate_payment_amount = 1 pe.append("references", { "reference_doctype": dt, "reference_name": dn, "due_date": doc.get("due_date"), "total_amount": grand_total, "outstanding_amount": outstanding_amount, "allocated_amount": outstanding_amount }) pe.setup_party_account_field() pe.set_missing_values() if party_account and bank: pe.set_exchange_rate() pe.set_amounts() return pe