Merge remote-tracking branch 'frappe/develop' into model-cleanup

Conflicts:
	erpnext/accounts/doctype/payment_to_invoice_matching_tool/payment_to_invoice_matching_tool.py
	erpnext/accounts/doctype/sales_invoice/sales_invoice.py
	erpnext/controllers/stock_controller.py
This commit is contained in:
Anand Doshi 2014-03-28 13:41:59 +05:30
commit 67d6a4e3aa
5 changed files with 143 additions and 98 deletions

View File

@ -1,9 +1,6 @@
// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt
// Booking Entry Id
// --------------------
cur_frm.add_fetch("account", "company", "company")
cur_frm.cscript.onload_post_render = function(doc) {
@ -25,26 +22,15 @@ cur_frm.fields_dict.voucher_no.get_query = function(doc) {
else {
return {
doctype: doc.voucher_type,
query: "accounts.doctype.payment_to_invoice_matching_tool.payment_to_invoice_matching_tool.gl_entry_details",
query: "erpnext.accounts.doctype.payment_to_invoice_matching_tool.payment_to_invoice_matching_tool.gl_entry_details",
filters: {
"dt": doc.voucher_type,
"acc": doc.account,
"account_type": doc.account_type
"acc": doc.account
}
}
}
}
cur_frm.cscript.voucher_no =function(doc, cdt, cdn) {
cur_frm.cscript.voucher_no = function(doc, cdt, cdn) {
return get_server_fields('get_voucher_details', '', '', doc, cdt, cdn, 1)
}
cur_frm.cscript.account = function(doc, cdt, cdn) {
return frappe.call({
doc: this.frm.doc,
method: "set_account_type",
callback: function(r) {
if(!r.exc) refresh_field("account_type");
}
});
}

View File

@ -6,32 +6,22 @@ import frappe
from frappe.utils import flt
from frappe.model.bean import getlist
from frappe import msgprint
from frappe import msgprint, _
from frappe.model.document import Document
class PaymentToInvoiceMatchingTool(Document):
def set_account_type(self):
self.doc.account_type = ""
if self.doc.account:
root_type = frappe.db.get_value("Account", self.doc.account, "root_type")
self.doc.account_type = "debit" if root_type in ["Asset", "Income"] else "credit"
def get_voucher_details(self):
total_amount = frappe.db.sql("""select sum(%s) from `tabGL Entry`
total_amount = frappe.db.sql("""select sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))
from `tabGL Entry`
where voucher_type = %s and voucher_no = %s
and account = %s""" %
(self.doc.account_type, '%s', '%s', '%s'),
(self.doc.voucher_type, self.doc.voucher_no, self.doc.account))
and account = %s""", (self.doc.voucher_type, self.doc.voucher_no, self.doc.account))
total_amount = total_amount and flt(total_amount[0][0]) or 0
reconciled_payment = frappe.db.sql("""
select sum(ifnull(%s, 0)) - sum(ifnull(%s, 0)) from `tabGL Entry` where
select abs(sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))) from `tabGL Entry` where
against_voucher = %s and voucher_no != %s
and account = %s""" %
((self.doc.account_type == 'debit' and 'credit' or 'debit'), self.doc.account_type,
'%s', '%s', '%s'), (self.doc.voucher_no, self.doc.voucher_no, self.doc.account))
and account = %s""", (self.doc.voucher_no, self.doc.voucher_no, self.doc.account))
reconciled_payment = reconciled_payment and flt(reconciled_payment[0][0]) or 0
ret = {
@ -53,25 +43,26 @@ class PaymentToInvoiceMatchingTool(Document):
def get_gl_entries(self):
self.validate_mandatory()
dc = self.doc.account_type == 'debit' and 'credit' or 'debit'
cond = self.doc.from_date and " and t1.posting_date >= '" + self.doc.from_date + "'" or ""
cond += self.doc.to_date and " and t1.posting_date <= '" + self.doc.to_date + "'"or ""
cond += self.doc.amt_greater_than and \
' and t2.' + dc+' >= ' + self.doc.amt_greater_than or ''
cond += self.doc.amt_less_than and \
' and t2.' + dc+' <= ' + self.doc.amt_less_than or ''
if self.doc.amt_greater_than:
cond += ' and abs(ifnull(t2.debit, 0) - ifnull(t2.credit, 0)) >= ' + \
self.doc.amt_greater_than
if self.doc.amt_less_than:
cond += ' and abs(ifnull(t2.debit, 0) - ifnull(t2.credit, 0)) >= ' + \
self.doc.amt_less_than
gle = frappe.db.sql("""
select t1.name as voucher_no, t1.posting_date, t1.total_debit as total_amt,
sum(ifnull(t2.credit, 0)) - sum(ifnull(t2.debit, 0)) as amt_due, t1.remark,
abs(sum(ifnull(t2.credit, 0)) - sum(ifnull(t2.debit, 0))) as amt_due, t1.remark,
t2.against_account, t2.name as voucher_detail_no
from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2
where t1.name = t2.parent and t1.docstatus = 1 and t2.account = %s
and ifnull(t2.against_voucher, '')='' and ifnull(t2.against_invoice, '')=''
and ifnull(t2.against_jv, '')='' and t2.%s > 0 %s group by t1.name, t2.name """ %
('%s', dc, cond), self.doc.account, as_dict=1)
and ifnull(t2.against_jv, '')='' and t1.name != %s %s group by t1.name, t2.name """ %
('%s', '%s', cond), (self.doc.account, self.doc.voucher_no), as_dict=1)
return gle
@ -80,8 +71,7 @@ class PaymentToInvoiceMatchingTool(Document):
ch = self.doc.append('ir_payment_details', {})
ch.voucher_no = d.get('voucher_no')
ch.posting_date = d.get('posting_date')
ch.amt_due = self.doc.account_type == 'debit' and flt(d.get('amt_due')) \
or -1*flt(d.get('amt_due'))
ch.amt_due = flt(d.get('amt_due'))
ch.total_amt = flt(d.get('total_amt'))
ch.against_account = d.get('against_account')
ch.remarks = d.get('remark')
@ -100,7 +90,7 @@ class PaymentToInvoiceMatchingTool(Document):
"""
if not self.doc.voucher_no or not frappe.db.sql("""select name from `tab%s`
where name = %s""" % (self.doc.voucher_type, '%s'), self.doc.voucher_no):
msgprint("Please select valid Voucher No to proceed", raise_exception=1)
frappe.throw(_("Please select valid Voucher No to proceed"))
lst = []
for d in self.get('ir_payment_details'):
@ -112,7 +102,7 @@ class PaymentToInvoiceMatchingTool(Document):
'against_voucher' : self.doc.voucher_no,
'account' : self.doc.account,
'is_advance' : 'No',
'dr_or_cr' : self.doc.account_type=='debit' and 'credit' or 'debit',
# 'dr_or_cr' : self.doc.account_type=='debit' and 'credit' or 'debit',
'unadjusted_amt' : flt(d.amt_due),
'allocated_amt' : flt(d.amt_to_be_reconciled)
}
@ -128,15 +118,13 @@ class PaymentToInvoiceMatchingTool(Document):
def gl_entry_details(doctype, txt, searchfield, start, page_len, filters):
from erpnext.controllers.queries import get_match_cond
return frappe.db.sql("""select gle.voucher_no, gle.posting_date,
gle.%(account_type)s from `tabGL Entry` gle
gle.debit, gle.credit from `tabGL Entry` gle
where gle.account = '%(acc)s'
and gle.voucher_type = '%(dt)s'
and gle.voucher_no like '%(txt)s'
and (ifnull(gle.against_voucher, '') = ''
or ifnull(gle.against_voucher, '') = gle.voucher_no )
and ifnull(gle.%(account_type)s, 0) > 0
and (select ifnull(abs(sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))), 0)
from `tabGL Entry`
where account = '%(acc)s'
@ -151,7 +139,6 @@ def gl_entry_details(doctype, txt, searchfield, start, page_len, filters):
limit %(start)s, %(page_len)s""" % {
"dt":filters["dt"],
"acc":filters["acc"],
"account_type": filters['account_type'],
'mcond':get_match_cond(doctype),
'txt': "%%%s%%" % txt,
"start": start,

View File

@ -8,7 +8,7 @@ import frappe.defaults
from frappe.utils import add_days, cint, cstr, date_diff, flt, getdate, nowdate, \
get_first_day, get_last_day
from frappe.utils import comma_and, get_url
from frappe.utils import comma_and
from frappe.model.naming import make_autoname
from frappe.model.bean import getlist
from frappe.model.code import get_obj
@ -459,7 +459,7 @@ class SalesInvoice(SellingController):
self.make_sl_entries(sl_entries)
def make_gl_entries(self, update_gl_entries_after=True):
def make_gl_entries(self, repost_future_gle=True):
gl_entries = self.get_gl_entries()
if gl_entries:
@ -470,9 +470,12 @@ class SalesInvoice(SellingController):
make_gl_entries(gl_entries, cancel=(self.doc.docstatus == 2),
update_outstanding=update_outstanding, merge_entries=False)
if update_gl_entries_after and cint(self.doc.update_stock) \
if repost_future_gle and cint(self.doc.update_stock) \
and cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
self.update_gl_entries_after()
items, warehouse_account = self.get_items_and_warehouse_accounts()
from controllers.stock_controller import update_gl_entries_after
update_gl_entries_after(self.doc.posting_date, self.doc.posting_time,
warehouse_account, items)
def get_gl_entries(self, warehouse_account=None):
from erpnext.accounts.general_ledger import merge_similar_entries
@ -738,8 +741,9 @@ def send_notification(new_rv):
def notify_errors(inv, customer, owner):
from frappe.utils.user import get_system_managers
recipients=get_system_managers()
frappe.sendmail(recipients=get_system_managers() + [frappe.db.get_value("User", owner, "email")],
frappe.sendmail(recipients + [frappe.db.get_value("User", owner, "email")],
subject="[Urgent] Error while creating recurring invoice for %s" % inv,
message = frappe.get_template("template/emails/recurring_invoice_failed.html").render({
"name": inv,

View File

@ -162,6 +162,8 @@ def create_party_account(party, party_type, company):
if not frappe.db.exists("Account", (party + " - " + company_details.abbr)):
parent_account = company_details.receivables_group \
if party_type=="Customer" else company_details.payables_group
if not parent_account:
frappe.throw(_("Please enter Account Receivable/Payable group in company master"))
# create
account = frappe.bean({
@ -172,7 +174,8 @@ def create_party_account(party, party_type, company):
'company': company,
'master_type': party_type,
'master_name': party,
"freeze_account": "No"
"freeze_account": "No",
"report_type": "Balance Sheet"
}).insert(ignore_permissions=True)
frappe.msgprint(_("Account Created") + ": " + account.doc.name)

View File

@ -11,7 +11,7 @@ from erpnext.controllers.accounts_controller import AccountsController
from erpnext.accounts.general_ledger import make_gl_entries, delete_gl_entries
class StockController(AccountsController):
def make_gl_entries(self, update_gl_entries_after=True):
def make_gl_entries(self, repost_future_gle=True):
if self.doc.docstatus == 2:
delete_gl_entries(voucher_type=self.doc.doctype, voucher_no=self.doc.name)
@ -22,14 +22,16 @@ class StockController(AccountsController):
gl_entries = self.get_gl_entries(warehouse_account)
make_gl_entries(gl_entries)
if update_gl_entries_after:
self.update_gl_entries_after(warehouse_account)
if repost_future_gle:
items, warehouse_account = self.get_items_and_warehouse_accounts(warehouse_account)
update_gl_entries_after(self.doc.posting_date, self.doc.posting_time,
warehouse_account, items)
def get_gl_entries(self, warehouse_account=None, default_expense_account=None,
default_cost_center=None):
from erpnext.accounts.general_ledger import process_gl_map
if not warehouse_account:
warehouse_account = self.get_warehouse_account()
warehouse_account = get_warehouse_account()
stock_ledger = self.get_stock_ledger_details()
voucher_details = self.get_voucher_details(stock_ledger, default_expense_account,
@ -82,6 +84,33 @@ class StockController(AccountsController):
return details
def get_items_and_warehouse_accounts(self, warehouse_account=None):
items, warehouses = [], []
if not warehouse_account:
warehouse_account = get_warehouse_account()
if hasattr(self, "fname"):
item_doclist = self.doclist.get({"parentfield": self.fname})
elif self.doc.doctype == "Stock Reconciliation":
import json
item_doclist = []
data = json.loads(self.doc.reconciliation_json)
for row in data[data.index(self.head_row)+1:]:
d = frappe._dict(zip(["item_code", "warehouse", "qty", "valuation_rate"], row))
item_doclist.append(d)
if item_doclist:
for d in item_doclist:
if d.item_code and d.item_code not in items:
items.append(d.item_code)
if d.warehouse and d.warehouse not in warehouses:
warehouses.append(d.warehouse)
warehouse_account = {wh: warehouse_account[wh] for wh in warehouses
if warehouse_account.get(wh)}
return items, warehouse_account
def get_stock_ledger_details(self):
stock_ledger = {}
for sle in frappe.db.sql("""select warehouse, stock_value_difference, voucher_detail_no
@ -120,7 +149,7 @@ class StockController(AccountsController):
if not matched:
self.delete_gl_entries(voucher_type, voucher_no)
voucher_obj.make_gl_entries(update_gl_entries_after=False)
voucher_obj.make_gl_entries(repost_future_gle=False)
else:
self.delete_gl_entries(voucher_type, voucher_no)
@ -229,41 +258,77 @@ class StockController(AccountsController):
from erpnext.stock.stock_ledger import make_sl_entries
make_sl_entries(sl_entries, is_amended)
def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
out = {}
if not (item_list and warehouse_list):
item_list, warehouse_list = self.get_distinct_item_warehouse()
if item_list and warehouse_list:
res = frappe.db.sql("""select item_code, voucher_type, voucher_no,
voucher_detail_no, posting_date, posting_time, stock_value,
warehouse, actual_qty as qty from `tabStock Ledger Entry`
where company = %s and item_code in (%s) and warehouse in (%s)
order by item_code desc, warehouse desc, posting_date desc,
posting_time desc, name desc""" %
('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
for r in res:
if (r.item_code, r.warehouse) not in out:
out[(r.item_code, r.warehouse)] = []
out[(r.item_code, r.warehouse)].append(r)
return out
def get_distinct_item_warehouse(self):
item_list = []
warehouse_list = []
for item in self.get(self.fname) \
+ self.get("packing_details"):
item_list.append(item.item_code)
warehouse_list.append(item.warehouse)
return list(set(item_list)), list(set(warehouse_list))
def make_cancel_gl_entries(self):
if frappe.db.sql("""select name from `tabGL Entry` where voucher_type=%s
and voucher_no=%s""", (self.doc.doctype, self.doc.name)):
self.make_gl_entries()
def update_gl_entries_after(posting_date, posting_time, warehouse_account=None, for_items=None):
def _delete_gl_entries(voucher_type, voucher_no):
frappe.db.sql("""delete from `tabGL Entry`
where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
if not warehouse_account:
warehouse_account = get_warehouse_account()
future_stock_vouchers = get_future_stock_vouchers(posting_date, posting_time,
warehouse_account, for_items)
gle = get_voucherwise_gl_entries(future_stock_vouchers, posting_date)
for voucher_type, voucher_no in future_stock_vouchers:
existing_gle = gle.get((voucher_type, voucher_no), [])
voucher_obj = frappe.get_obj(voucher_type, voucher_no)
expected_gle = voucher_obj.get_gl_entries(warehouse_account)
if expected_gle:
if not existing_gle or not compare_existing_and_expected_gle(existing_gle,
expected_gle):
_delete_gl_entries(voucher_type, voucher_no)
voucher_obj.make_gl_entries(repost_future_gle=False)
else:
_delete_gl_entries(voucher_type, voucher_no)
def compare_existing_and_expected_gle(existing_gle, expected_gle):
matched = True
for entry in expected_gle:
for e in existing_gle:
if entry.account==e.account and entry.against_account==e.against_account \
and entry.cost_center==e.cost_center \
and (entry.debit != e.debit or entry.credit != e.credit):
matched = False
break
return matched
def get_future_stock_vouchers(posting_date, posting_time, warehouse_account=None, for_items=None):
future_stock_vouchers = []
condition = ""
if for_items:
condition = ''.join([' and item_code in (\'', '\', \''.join(for_items) ,'\')'])
if warehouse_account:
condition += ''.join([' and warehouse in (\'', '\', \''.join(warehouse_account.keys()) ,'\')'])
for d in frappe.db.sql("""select distinct sle.voucher_type, sle.voucher_no
from `tabStock Ledger Entry` sle
where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s
order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" %
('%s', '%s', condition), (posting_date, posting_time),
as_dict=True):
future_stock_vouchers.append([d.voucher_type, d.voucher_no])
return future_stock_vouchers
def get_voucherwise_gl_entries(future_stock_vouchers, posting_date):
gl_entries = {}
if future_stock_vouchers:
for d in frappe.db.sql("""select * from `tabGL Entry`
where posting_date >= %s and voucher_no in (%s)""" %
('%s', ', '.join(['%s']*len(future_stock_vouchers))),
tuple([posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
return gl_entries
def get_warehouse_account():
warehouse_account = dict(frappe.db.sql("""select master_name, name from tabAccount
where account_type = 'Warehouse' and ifnull(master_name, '') != ''"""))
return warehouse_account