[Cleanup] Sales Common deprecated

This commit is contained in:
Nabin Hait
2013-10-18 12:29:11 +05:30
parent d05a3b3b5c
commit d1fd1e2d89
42 changed files with 219 additions and 727 deletions

View File

@@ -6,7 +6,6 @@ import webnotes
from webnotes.utils import cstr, getdate
from webnotes.model.bean import getlist
from webnotes.model.code import get_obj
from webnotes import msgprint
from stock.utils import get_valid_serial_nos
@@ -36,8 +35,9 @@ class DocType(TransactionBase):
self.validate_fiscal_year()
self.validate_installation_date()
self.check_item_table()
sales_com_obj = get_obj(dt = 'Sales Common')
sales_com_obj.check_active_sales_items(self)
from controllers.selling_controller import check_active_sales_items
check_active_sales_items(self)
def validate_fiscal_year(self):
from accounts.utils import validate_fiscal_year

View File

@@ -59,16 +59,6 @@ class DocType(TransactionBase):
msgprint("Customer : %s does not exist in system." % (name))
raise Exception
def get_contact_details(self, arg):
arg = eval(arg)
contact = webnotes.conn.sql("select contact_no, email_id from `tabContact` where contact_name = '%s' and customer_name = '%s'" %(arg['contact_person'],arg['customer']), as_dict = 1)
ret = {
'contact_no' : contact and contact[0]['contact_no'] or '',
'email_id' : contact and contact[0]['email_id'] or ''
}
return ret
def on_update(self):
self.add_calendar_event()

View File

@@ -10,7 +10,7 @@ cur_frm.cscript.sales_team_fname = "sales_team";
wn.require('app/accounts/doctype/sales_taxes_and_charges_master/sales_taxes_and_charges_master.js');
wn.require('app/utilities/doctype/sms_control/sms_control.js');
wn.require('app/selling/doctype/sales_common/sales_common.js');
wn.require('app/selling/sales_common.js');
wn.require('app/accounts/doctype/sales_invoice/pos.js');
erpnext.selling.QuotationController = erpnext.selling.SellingController.extend({

View File

@@ -19,46 +19,10 @@ class DocType(SellingController):
self.doclist = doclist
self.tname = 'Quotation Item'
self.fname = 'quotation_details'
# Get contact person details based on customer selected
# ------------------------------------------------------
def get_contact_details(self):
return get_obj('Sales Common').get_contact_details(self,0)
# Get Item Details
# -----------------
def get_item_details(self, args=None):
import json
args = args and json.loads(args) or {}
if args.get('item_code'):
return get_obj('Sales Common').get_item_details(args, self)
else:
obj = get_obj('Sales Common')
for doc in self.doclist:
if doc.fields.get('item_code'):
arg = {
'item_code': doc.fields.get('item_code'),
'income_account': doc.fields.get('income_account'),
'cost_center': doc.fields.get('cost_center'),
'warehouse': doc.fields.get('warehouse')
}
res = obj.get_item_details(arg, self) or {}
for r in res:
if not doc.fields.get(r):
doc.fields[r] = res[r]
def has_sales_order(self):
return webnotes.conn.get_value("Sales Order Item", {"prevdoc_docname": self.doc.name, "docstatus": 1})
# Re-calculates Basic Rate & amount based on Price List Selected
# --------------------------------------------------------------
def get_adj_percent(self, arg=''):
get_obj('Sales Common').get_adj_percent(self)
# Does not allow same item code to be entered twice
# -------------------------------------------------
def validate_for_items(self):
chk_dupl_itm = []
for d in getlist(self.doclist,'quotation_details'):
@@ -68,9 +32,6 @@ class DocType(SellingController):
else:
chk_dupl_itm.append([cstr(d.item_code),cstr(d.description)])
#do not allow sales item in maintenance quotation and service item in sales quotation
#-----------------------------------------------------------------------------------------------
def validate_order_type(self):
super(DocType, self).validate_order_type()
@@ -96,15 +57,8 @@ class DocType(SellingController):
self.set_status()
self.validate_order_type()
self.validate_for_items()
self.validate_uom_is_integer("stock_uom", "qty")
sales_com_obj = get_obj('Sales Common')
sales_com_obj.check_active_sales_items(self)
sales_com_obj.validate_max_discount(self,'quotation_details')
#update enquiry
#------------------
def update_opportunity(self):
for opportunity in self.doclist.get_distinct_values("prevdoc_docname"):
webnotes.bean("Opportunity", opportunity).get_controller().set_status(update=True)

View File

@@ -1 +0,0 @@
Common scripts used in Sales Cycle.

View File

@@ -1 +0,0 @@
from __future__ import unicode_literals

View File

@@ -1,317 +0,0 @@
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import webnotes
from webnotes.utils import cint, cstr, flt
from webnotes.model.doc import addchild
from webnotes.model.bean import getlist
from webnotes.model.code import get_obj
from webnotes import msgprint, _
get_value = webnotes.conn.get_value
from utilities.transaction_base import TransactionBase
class DocType(TransactionBase):
def __init__(self,d,dl):
self.doc, self.doclist = d, dl
def get_contact_details(self, obj = '', primary = 0):
cond = " and contact_name = '"+cstr(obj.doc.contact_person)+"'"
if primary: cond = " and is_primary_contact = 'Yes'"
contact = webnotes.conn.sql("select contact_name, contact_no, email_id, contact_address from `tabContact` where customer = '%s' and docstatus != 2 %s" %(obj.doc.customer, cond), as_dict = 1)
if not contact:
return
c = contact[0]
obj.doc.contact_person = c['contact_name'] or ''
obj.doc.contact_no = c['contact_no'] or ''
obj.doc.email_id = c['email_id'] or ''
obj.doc.customer_mobile_no = c['contact_no'] or ''
if c['contact_address']:
obj.doc.customer_address = c['contact_address']
def get_invoice_details(self, obj = ''):
if obj.doc.company:
acc_head = webnotes.conn.sql("select name from `tabAccount` where name = '%s' and docstatus != 2" % (cstr(obj.doc.customer) + " - " + webnotes.conn.get_value('Company', obj.doc.company, 'abbr')))
obj.doc.debit_to = acc_head and acc_head[0][0] or ''
def get_tax_details(self, item_code, obj):
import json
tax = webnotes.conn.sql("select tax_type, tax_rate from `tabItem Tax` where parent = %s" , item_code)
t = {}
for x in tax: t[x[0]] = flt(x[1])
ret = {
'item_tax_rate' : tax and json.dumps(t) or ''
}
return ret
def get_serial_details(self, serial_no, obj):
import json
item = webnotes.conn.sql("select item_code, make, label,brand, description from `tabSerial No` where name = '%s' and docstatus != 2" %(serial_no), as_dict=1)
tax = webnotes.conn.sql("select tax_type, tax_rate from `tabItem Tax` where parent = %s" , item[0]['item_code'])
t = {}
for x in tax: t[x[0]] = flt(x[1])
ret = {
'item_code' : item and item[0]['item_code'] or '',
'make' : item and item[0]['make'] or '',
'label' : item and item[0]['label'] or '',
'brand' : item and item[0]['brand'] or '',
'description' : item and item[0]['description'] or '',
'item_tax_rate' : json.dumps(t)
}
return ret
# To verify whether rate entered in details table does not exceed max discount %
# =======================================================================================
def validate_max_discount(self,obj, detail_table):
for d in getlist(obj.doclist, detail_table):
discount = webnotes.conn.sql("select max_discount from tabItem where name = '%s'" %(d.item_code),as_dict = 1)
if discount and discount[0]['max_discount'] and (flt(d.adj_rate)>flt(discount[0]['max_discount'])):
msgprint("You cannot give more than " + cstr(discount[0]['max_discount']) + " % discount on Item Code : "+cstr(d.item_code))
raise Exception
def get_item_list(self, obj, is_stopped=0):
"""get item list"""
il = []
for d in getlist(obj.doclist, obj.fname):
reserved_warehouse = ""
reserved_qty_for_main_item = 0
if obj.doc.doctype == "Sales Order":
if (webnotes.conn.get_value("Item", d.item_code, "is_stock_item") == 'Yes' or
self.has_sales_bom(d.item_code)) and not d.reserved_warehouse:
webnotes.throw(_("Please enter Reserved Warehouse for item ") +
d.item_code + _(" as it is stock Item or packing item"))
reserved_warehouse = d.reserved_warehouse
if flt(d.qty) > flt(d.delivered_qty):
reserved_qty_for_main_item = flt(d.qty) - flt(d.delivered_qty)
if obj.doc.doctype == "Delivery Note" and d.against_sales_order:
# if SO qty is 10 and there is tolerance of 20%, then it will allow DN of 12.
# But in this case reserved qty should only be reduced by 10 and not 12
already_delivered_qty = self.get_already_delivered_qty(obj.doc.name,
d.against_sales_order, d.prevdoc_detail_docname)
so_qty, reserved_warehouse = self.get_so_qty_and_warehouse(d.prevdoc_detail_docname)
if already_delivered_qty + d.qty > so_qty:
reserved_qty_for_main_item = -(so_qty - already_delivered_qty)
else:
reserved_qty_for_main_item = -flt(d.qty)
if self.has_sales_bom(d.item_code):
for p in getlist(obj.doclist, 'packing_details'):
if p.parent_detail_docname == d.name and p.parent_item == d.item_code:
# the packing details table's qty is already multiplied with parent's qty
il.append(webnotes._dict({
'warehouse': p.warehouse,
'reserved_warehouse': reserved_warehouse,
'item_code': p.item_code,
'qty': flt(p.qty),
'reserved_qty': (flt(p.qty)/flt(d.qty)) * reserved_qty_for_main_item,
'uom': p.uom,
'batch_no': cstr(p.batch_no).strip(),
'serial_no': cstr(p.serial_no).strip(),
'name': d.name
}))
else:
il.append(webnotes._dict({
'warehouse': d.warehouse,
'reserved_warehouse': reserved_warehouse,
'item_code': d.item_code,
'qty': d.qty,
'reserved_qty': reserved_qty_for_main_item,
'uom': d.stock_uom,
'batch_no': cstr(d.batch_no).strip(),
'serial_no': cstr(d.serial_no).strip(),
'name': d.name
}))
return il
def get_already_delivered_qty(self, dn, so, so_detail):
qty = webnotes.conn.sql("""select sum(qty) from `tabDelivery Note Item`
where prevdoc_detail_docname = %s and docstatus = 1
and against_sales_order = %s
and parent != %s""", (so_detail, so, dn))
return qty and flt(qty[0][0]) or 0.0
def get_so_qty_and_warehouse(self, so_detail):
so_item = webnotes.conn.sql("""select qty, reserved_warehouse from `tabSales Order Item`
where name = %s and docstatus = 1""", so_detail, as_dict=1)
so_qty = so_item and flt(so_item[0]["qty"]) or 0.0
so_warehouse = so_item and so_item[0]["reserved_warehouse"] or ""
return so_qty, so_warehouse
def has_sales_bom(self, item_code):
return webnotes.conn.sql("select name from `tabSales BOM` where new_item_code=%s and docstatus != 2", item_code)
def get_sales_bom_items(self, item_code):
return webnotes.conn.sql("""select t1.item_code, t1.qty, t1.uom
from `tabSales BOM Item` t1, `tabSales BOM` t2
where t2.new_item_code=%s and t1.parent = t2.name""", item_code, as_dict=1)
def get_packing_item_details(self, item):
return webnotes.conn.sql("select item_name, description, stock_uom from `tabItem` where name = %s", item, as_dict = 1)[0]
def get_bin_qty(self, item, warehouse):
det = webnotes.conn.sql("select actual_qty, projected_qty from `tabBin` where item_code = '%s' and warehouse = '%s'" % (item, warehouse), as_dict = 1)
return det and det[0] or ''
def update_packing_list_item(self,obj, packing_item_code, qty, warehouse, line):
bin = self.get_bin_qty(packing_item_code, warehouse)
item = self.get_packing_item_details(packing_item_code)
# check if exists
exists = 0
for d in getlist(obj.doclist, 'packing_details'):
if d.parent_item == line.item_code and d.item_code == packing_item_code and d.parent_detail_docname == line.name:
pi, exists = d, 1
break
if not exists:
pi = addchild(obj.doc, 'packing_details', 'Delivery Note Packing Item',
obj.doclist)
pi.parent_item = line.item_code
pi.item_code = packing_item_code
pi.item_name = item['item_name']
pi.parent_detail_docname = line.name
pi.description = item['description']
pi.uom = item['stock_uom']
pi.qty = flt(qty)
pi.actual_qty = bin and flt(bin['actual_qty']) or 0
pi.projected_qty = bin and flt(bin['projected_qty']) or 0
if not pi.warehouse:
pi.warehouse = warehouse
if not pi.batch_no:
pi.batch_no = cstr(line.batch_no)
pi.idx = self.packing_list_idx
# saved, since this function is called on_update of delivery note
pi.save()
self.packing_list_idx += 1
def make_packing_list(self, obj, fname):
"""make packing list for sales bom item"""
self.packing_list_idx = 0
parent_items = []
for d in getlist(obj.doclist, fname):
warehouse = fname == "sales_order_details" and d.reserved_warehouse or d.warehouse
if self.has_sales_bom(d.item_code):
for i in self.get_sales_bom_items(d.item_code):
self.update_packing_list_item(obj, i['item_code'], flt(i['qty'])*flt(d.qty), warehouse, d)
if [d.item_code, d.name] not in parent_items:
parent_items.append([d.item_code, d.name])
obj.doclist = self.cleanup_packing_list(obj, parent_items)
return obj.doclist
def cleanup_packing_list(self, obj, parent_items):
"""Remove all those child items which are no longer present in main item table"""
delete_list = []
for d in getlist(obj.doclist, 'packing_details'):
if [d.parent_item, d.parent_detail_docname] not in parent_items:
# mark for deletion from doclist
delete_list.append(d.name)
if not delete_list:
return obj.doclist
# delete from doclist
obj.doclist = webnotes.doclist(filter(lambda d: d.name not in delete_list, obj.doclist))
# delete from db
webnotes.conn.sql("""\
delete from `tabDelivery Note Packing Item`
where name in (%s)"""
% (", ".join(["%s"] * len(delete_list))),
tuple(delete_list))
return obj.doclist
def get_month(self,date):
"""Get month based on date (required in sales person and sales partner)"""
month_list = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
month_idx = cint(cstr(date).split('-')[1])-1
return month_list[month_idx]
# **** Check for Stop SO as no transactions can be made against Stopped SO. Need to unstop it. ***
def check_stop_sales_order(self,obj):
for d in getlist(obj.doclist,obj.fname):
ref_doc_name = ''
if d.fields.has_key('against_sales_order') and d.against_sales_order:
ref_doc_name = d.against_sales_order
elif d.fields.has_key('sales_order') and d.sales_order and not d.delivery_note:
ref_doc_name = d.sales_order
if ref_doc_name:
so_status = webnotes.conn.sql("select status from `tabSales Order` where name = %s",ref_doc_name)
so_status = so_status and so_status[0][0] or ''
if so_status == 'Stopped':
msgprint("You cannot do any transaction against Sales Order : '%s' as it is Stopped." %(ref_doc_name))
raise Exception
def check_active_sales_items(self,obj):
for d in getlist(obj.doclist, obj.fname):
if d.item_code:
item = webnotes.conn.sql("""select docstatus, is_sales_item,
is_service_item, default_income_account from tabItem where name = %s""",
d.item_code, as_dict=True)[0]
if item.is_sales_item == 'No' and item.is_service_item == 'No':
msgprint("Item : '%s' is neither Sales nor Service Item" % (d.item_code))
raise Exception
if d.income_account and not item.default_income_account:
webnotes.conn.set_value("Item", d.item_code, "default_income_account", d.income_account)
def check_credit(self,obj,grand_total):
acc_head = webnotes.conn.sql("select name from `tabAccount` where company = '%s' and master_name = '%s'"%(obj.doc.company, obj.doc.customer))
if acc_head:
tot_outstanding = 0
dbcr = webnotes.conn.sql("""select sum(debit), sum(credit) from `tabGL Entry`
where account = %s""", acc_head[0][0])
if dbcr:
tot_outstanding = flt(dbcr[0][0])-flt(dbcr[0][1])
exact_outstanding = flt(tot_outstanding) + flt(grand_total)
get_obj('Account',acc_head[0][0]).check_credit_limit(acc_head[0][0], obj.doc.company, exact_outstanding)
def get_batch_no(doctype, txt, searchfield, start, page_len, filters):
from controllers.queries import get_match_cond
if filters.has_key('warehouse'):
return webnotes.conn.sql("""select batch_no from `tabStock Ledger Entry` sle
where item_code = '%(item_code)s'
and warehouse = '%(warehouse)s'
and batch_no like '%(txt)s'
and exists(select * from `tabBatch`
where name = sle.batch_no
and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
and docstatus != 2)
%(mcond)s
group by batch_no having sum(actual_qty) > 0
order by batch_no desc
limit %(start)s, %(page_len)s """ % {'item_code': filters['item_code'],
'warehouse': filters['warehouse'], 'posting_date': filters['posting_date'],
'txt': "%%%s%%" % txt, 'mcond':get_match_cond(doctype, searchfield),
'start': start, 'page_len': page_len})
else:
return webnotes.conn.sql("""select name from tabBatch
where docstatus != 2
and item = '%(item_code)s'
and (ifnull(expiry_date, '')='' or expiry_date >= '%(posting_date)s')
and name like '%(txt)s'
%(mcond)s
order by name desc
limit %(start)s, %(page_len)s""" % {'item_code': filters['item_code'],
'posting_date': filters['posting_date'], 'txt': "%%%s%%" % txt,
'mcond':get_match_cond(doctype, searchfield),'start': start,
'page_len': page_len})

View File

@@ -1,19 +0,0 @@
[
{
"creation": "2012-03-27 14:36:14",
"docstatus": 0,
"modified": "2013-07-10 14:54:19",
"modified_by": "Administrator",
"owner": "Administrator"
},
{
"doctype": "DocType",
"issingle": 1,
"module": "Selling",
"name": "__common__"
},
{
"doctype": "DocType",
"name": "Sales Common"
}
]

View File

@@ -9,7 +9,7 @@ cur_frm.cscript.other_fname = "other_charges";
cur_frm.cscript.sales_team_fname = "sales_team";
wn.require('app/selling/doctype/sales_common/sales_common.js');
wn.require('app/selling/sales_common.js');
wn.require('app/accounts/doctype/sales_taxes_and_charges_master/sales_taxes_and_charges_master.js');
wn.require('app/utilities/doctype/sms_control/sms_control.js');
wn.require('app/accounts/doctype/sales_invoice/pos.js');

View File

@@ -11,8 +11,6 @@ from webnotes.model.code import get_obj
from webnotes import msgprint
from webnotes.model.mapper import get_mapped_doclist
from controllers.selling_controller import SellingController
class DocType(SellingController):
@@ -26,18 +24,6 @@ class DocType(SellingController):
self.partner_tname = 'Partner Target Detail'
self.territory_tname = 'Territory Target Detail'
def get_contact_details(self):
get_obj('Sales Common').get_contact_details(self,0)
def get_comm_rate(self, sales_partner):
return get_obj('Sales Common').get_comm_rate(sales_partner, self)
def get_adj_percent(self, arg=''):
get_obj('Sales Common').get_adj_percent(self)
def get_available_qty(self,args):
return get_obj('Sales Common').get_available_qty(eval(args))
def validate_mandatory(self):
# validate transaction date v/s delivery date
if self.doc.delivery_date:
@@ -124,12 +110,9 @@ class DocType(SellingController):
self.validate_uom_is_integer("stock_uom", "qty")
self.validate_for_items()
self.validate_warehouse()
sales_com_obj = get_obj(dt = 'Sales Common')
sales_com_obj.check_active_sales_items(self)
sales_com_obj.validate_max_discount(self,'sales_order_details')
self.doclist = sales_com_obj.make_packing_list(self,'sales_order_details')
from stock.doctype.packed_item.packed_item import make_packing_list
self.doclist = make_packing_list(self,'sales_order_details')
self.validate_with_previous_doc()
@@ -172,14 +155,14 @@ class DocType(SellingController):
for quotation in self.doclist.get_distinct_values("prevdoc_docname"):
bean = webnotes.bean("Quotation", quotation)
if bean.doc.docstatus==2:
webnotes.throw(d.prevdoc_docname + ": " + webnotes._("Quotation is cancelled."))
webnotes.throw(quotation + ": " + webnotes._("Quotation is cancelled."))
bean.get_controller().set_status(update=True)
def on_submit(self):
self.update_stock_ledger(update_stock = 1)
get_obj('Sales Common').check_credit(self,self.doc.grand_total)
self.check_credit(self.doc.grand_total)
get_obj('Authorization Control').validate_approving_authority(self.doc.doctype, self.doc.grand_total, self)
@@ -235,21 +218,21 @@ class DocType(SellingController):
def stop_sales_order(self):
self.check_modified_date()
self.update_stock_ledger(update_stock = -1,is_stopped = 1)
self.update_stock_ledger(-1)
webnotes.conn.set(self.doc, 'status', 'Stopped')
msgprint("""%s: %s has been Stopped. To make transactions against this Sales Order
you need to Unstop it.""" % (self.doc.doctype, self.doc.name))
def unstop_sales_order(self):
self.check_modified_date()
self.update_stock_ledger(update_stock = 1,is_stopped = 1)
self.update_stock_ledger(1)
webnotes.conn.set(self.doc, 'status', 'Submitted')
msgprint("%s: %s has been Unstopped" % (self.doc.doctype, self.doc.name))
def update_stock_ledger(self, update_stock, is_stopped = 0):
def update_stock_ledger(self, update_stock):
from stock.utils import update_bin
for d in self.get_item_list(is_stopped):
for d in self.get_item_list():
if webnotes.conn.get_value("Item", d['item_code'], "is_stock_item") == "Yes":
args = {
"item_code": d['item_code'],
@@ -261,10 +244,6 @@ class DocType(SellingController):
"is_amended": self.doc.amended_from and 'Yes' or 'No'
}
update_bin(args)
def get_item_list(self, is_stopped):
return get_obj('Sales Common').get_item_list( self, is_stopped)
def on_update(self):
pass
@@ -438,4 +417,4 @@ def make_maintenance_visit(source_name, target_doclist=None):
}
}, target_doclist)
return [d.fields for d in doclist]
return [d.fields for d in doclist]

View File

@@ -361,7 +361,7 @@
"label": "Packing Details",
"oldfieldname": "packing_details",
"oldfieldtype": "Table",
"options": "Delivery Note Packing Item",
"options": "Packed Item",
"print_hide": 1,
"read_only": 1
},

View File

@@ -73,23 +73,15 @@ erpnext.selling.SellingController = erpnext.TransactionController.extend({
if(!item.item_code) {
wn.throw("Please enter Item Code to get batch no");
} else {
if(item.warehouse) {
return {
query : "selling.doctype.sales_common.sales_common.get_batch_no",
filters: {
'item_code': item.item_code,
'warehouse': item.warehouse,
'posting_date': me.frm.doc.posting_date
}
}
} else {
return{
query : "selling.doctype.sales_common.sales_common.get_batch_no",
filters: {
'item_code': item.item_code,
'posting_date': me.frm.doc.posting_date
}
}
filters = {
'item_code': item.item_code,
'posting_date': me.frm.doc.posting_date,
}
if(item.warehouse) filters["warehouse"] = item.warehouse
return {
query : "controllers.queries.get_batch_no",
filters: filters
}
}
});
@@ -628,7 +620,7 @@ erpnext.selling.SellingController = erpnext.TransactionController.extend({
// Help for Sales BOM items
var set_sales_bom_help = function(doc) {
if(!cur_frm.fields_dict.packing_list) return;
if (getchildren('Delivery Note Packing Item', doc.name, 'packing_details').length) {
if (getchildren('Packed Item', doc.name, 'packing_details').length) {
$(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {