merge
diff --git a/accounts/Print Format/SalesInvoice/SalesInvoice.html b/accounts/Print Format/SalesInvoice/SalesInvoice.html
index 3e24a50..6acdc52 100644
--- a/accounts/Print Format/SalesInvoice/SalesInvoice.html
+++ b/accounts/Print Format/SalesInvoice/SalesInvoice.html
@@ -70,10 +70,10 @@
 				<td style="width: 3%;">{{ row.idx }}</td>
 				<td style="width: 20%;">{{ row.item_name }}</td>
 				<td style="width: 37%;">{{ row.description }}</td>
-				<td style="width: 5%;">{{ row.qty }}</td>
+				<td style="width: 5%; text-align: right;">{{ row.qty }}</td>
 				<td style="width: 5%;">{{ row.stock_uom }}</td>
-				<td style="width: 15%;">{{ utils.fmt_money(row.export_rate) }}</td>
-				<td style="width: 15%;">{{ utils.fmt_money(row.export_amount) }}</td>
+				<td style="width: 15%; text-align: right;">{{ utils.fmt_money(row.export_rate, currency=doc.currency) }}</td>
+				<td style="width: 15%; text-align: right;">{{ utils.fmt_money(row.export_amount, currency=doc.currency) }}</td>
 			</tr>
 			{% endfor -%}
 		</tbody>
@@ -101,29 +101,25 @@
 					<table cellspacing=0 width=100%><tbody>
 						<tr>
 							<td>Net Total</td>
-							<td></td>
-							<td width=38%>{{
-								utils.fmt_money(doc.net_total/doc.conversion_rate)
+							<td width=40% style="text-align: right;">{{
+								utils.fmt_money(doc.net_total/doc.conversion_rate, currency=doc.currency)
 							}}</td>
 						</tr>
 						{%- for charge in doclist.get({"doctype":"Sales Taxes and Charges"}) -%}
 						{%- if not charge.included_in_print_rate -%}
 						<tr>
 							<td>{{ charge.description }}</td>
-							<td></td>
-							<td>{{ utils.fmt_money(charge.tax_amount / doc.conversion_rate) }}</td>
+							<td style="text-align: right;">{{ utils.fmt_money(charge.tax_amount / doc.conversion_rate, currency=doc.currency) }}</td>
 						</tr>
 						{%- endif -%}
 						{%- endfor -%}
 						<tr>
 							<td>Grand Total</td>
-							<td></td>
-							<td>{{ utils.fmt_money(doc.grand_total_export) }}</td>
+							<td style="text-align: right;">{{ utils.fmt_money(doc.grand_total_export, currency=doc.currency) }}</td>
 						</tr>
 						<tr style='font-weight: bold'>
 							<td>Rounded Total</td>
-							<td>{{ doc.currency }}</td>
-							<td>{{ utils.fmt_money(doc.rounded_total_export) }}</td>
+							<td style="text-align: right;">{{ utils.fmt_money(doc.rounded_total_export, currency=doc.currency) }}</td>
 						</tr>
 					</tbody></table>
 					<br /><b>In Words</b><br />
diff --git a/accounts/__init__.py b/accounts/__init__.py
index 707203f..145ad6b 100644
--- a/accounts/__init__.py
+++ b/accounts/__init__.py
@@ -32,209 +32,3 @@
 		WHERE name=%s AND docstatus<2""", company)
 	
 	if res: return res[0][0]
-
-@webnotes.whitelist()
-def get_new_jv_details():
-	"""
-		Get details which will help create new jv on sales/purchase return
-	"""
-	doclist = webnotes.form_dict.get('doclist')
-	fiscal_year = webnotes.form_dict.get('fiscal_year')
-	if not (isinstance(doclist, basestring) and isinstance(fiscal_year, basestring)): return
-
-	import json
-	doclist = json.loads(doclist)
-	doc, children = doclist[0], doclist[1:]
-
-	if doc.get('return_type')=='Sales Return':
-		if doc.get('sales_invoice_no'):
-			return get_invoice_details(doc, children, fiscal_year)
-		elif doc.get('delivery_note_no'):
-			return get_delivery_note_details(doc, children, fiscal_year)
-
-	elif doc.get('purchase_receipt_no'):
-		return get_purchase_receipt_details(doc, children, fiscal_year)
-
-
-def get_invoice_details(doc, children, fiscal_year):
-	"""
-		Gets details from an invoice to make new jv
-		Returns [{
-			'account': ,
-			'balance': ,
-			'debit': ,
-			'credit': ,
-			'against_invoice': ,
-			'against_payable': 
-		}, { ... }, ...]
-	"""
-	if doc.get('return_type')=='Sales Return':
-		obj = get_obj('Sales Invoice', doc.get('sales_invoice_no'), with_children=1)
-	else:
-		obj = get_obj('Purchase Invoice', doc.get('purchase_invoice_no'), with_children=1)
-	if not obj.doc.docstatus==1: return
-
-	# Build invoice account jv detail record
-	invoice_rec = get_invoice_account_jv_record(doc, children, fiscal_year, obj)
-
-	# Build item accountwise jv detail records
-	item_accountwise_list = get_item_accountwise_jv_record(doc, children, fiscal_year, obj)
-
-	return [invoice_rec] + item_accountwise_list
-
-
-def get_invoice_account_jv_record(doc, children, fiscal_year, obj):
-	"""
-		Build customer/supplier account jv detail record
-	"""
-	# Calculate total return amount
-	total_amt = sum([(flt(ch.get('rate')) * flt(ch.get('returned_qty'))) for ch in children])
-
-	ret = {}
-
-	if doc.get('return_type')=='Sales Return':
-		account = obj.doc.debit_to
-		ret['against_invoice'] = doc.get('sales_invoice_no')
-		ret['credit'] = total_amt
-	else:
-		account = obj.doc.credit_to
-		ret['against_voucher'] = doc.get('purchase_invoice_no')
-		ret['debit'] = total_amt
-	
-	ret.update({
-		'account': account,
-		'balance': get_balance_on(account, doc.get("return_date"))
-	})
-
-	return ret
-
-
-def get_item_accountwise_jv_record(doc, children, fiscal_year, obj):
-	"""
-		Build item accountwise jv detail records
-	"""
-	if doc.get('return_type')=='Sales Return':
-		amt_field = 'debit'
-		ac_field = 'income_account'
-	else:
-		amt_field = 'credit'
-		ac_field = 'expense_head'
-	
-	inv_children = dict([[ic.fields.get('item_code'), ic] for ic in obj.doclist if ic.fields.get('item_code')])
-
-	accwise_list = []
-	
-	for ch in children:
-		inv_ch = inv_children.get(ch.get('item_code'))
-		if not inv_ch: continue
-
-		amount = flt(ch.get('rate')) * flt(ch.get('returned_qty'))
-
-		accounts = [[jvd['account'], jvd['cost_center']] for jvd in accwise_list]
-		
-		if [inv_ch.fields.get(ac_field), inv_ch.fields.get('cost_center')] not in accounts:
-			rec = {
-				'account': inv_ch.fields.get(ac_field),
-				'cost_center': inv_ch.fields.get('cost_center'),
-				'balance': get_balance_on(inv_ch.fields.get(ac_field),
-					doc.get("return_date"))
-			}
-			rec[amt_field] = amount
-			accwise_list.append(rec)
-		else:
-			rec = accwise_list[accounts.index([inv_ch.fields.get(ac_field), inv_ch.fields.get('cost_center')])]
-			rec[amt_field] = rec[amt_field] + amount
-		
-	return accwise_list
-
-
-def get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list):
-	"""
-		Get invoice details and make jv detail records
-	"""
-	for inv in inv_list:
-		if not inv[0]: continue
-
-		if doc.get('return_type')=='Sales Return':
-			doc['sales_invoice_no'] = inv[0]
-		else:
-			doc['purchase_invoice_no'] = inv[0]
-		
-		jv_details = get_invoice_details(doc, children, fiscal_year)
-		
-		if jv_details and len(jv_details)>1: jv_details_list.extend(jv_details)
-
-	return jv_details_list
-
-
-def get_prev_doc_list(obj, prev_doctype):
-	"""
-		Returns a list of previous doc's names
-	"""
-	prevdoc_list = []
-	for ch in obj.doclist:
-		if ch.fields.get('prevdoc_docname') and ch.fields.get('prevdoc_doctype')==prev_doctype:
-			prevdoc_list.append(ch.fields.get('prevdoc_docname'))
-	return prevdoc_list
-
-
-def get_inv_list(table, field, value):
-	"""
-		Returns invoice list
-	"""
-	if isinstance(value, basestring):
-		return webnotes.conn.sql("""\
-			SELECT DISTINCT parent FROM `%s`
-			WHERE %s='%s' AND docstatus=1""" % (table, field, value))
-	elif isinstance(value, list):
-		return webnotes.conn.sql("""\
-			SELECT DISTINCT parent FROM `%s`
-			WHERE %s IN ("%s") AND docstatus=1""" % (table, field, '", "'.join(value)))
-	else:
-		return []
-	
-
-def get_delivery_note_details(doc, children, fiscal_year):
-	"""
-		Gets sales invoice numbers from delivery note details
-		and returns detail records for jv
-	"""
-	jv_details_list = []
-	
-	dn_obj = get_obj('Delivery Note', doc['delivery_note_no'], with_children=1)
-	
-	inv_list = get_inv_list('tabSales Invoice Item', 'delivery_note', doc['delivery_note_no'])
-
-	if inv_list:
-		jv_details_list = get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list)
-	
-	if not (inv_list and jv_details_list):
-		so_list = get_prev_doc_list(dn_obj, 'Sales Order')
-		inv_list = get_inv_list('tabSales Invoice Item', 'sales_order', so_list)
-		if inv_list:
-			jv_details_list = get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list)
-
-	return jv_details_list
-
-
-def get_purchase_receipt_details(doc, children, fiscal_year):
-	"""
-		Gets purchase invoice numbers from purchase receipt details
-		and returns detail records for jv
-	"""
-	jv_details_list = []
-	
-	pr_obj = get_obj('Purchase Receipt', doc['purchase_receipt_no'], with_children=1)
-	
-	inv_list = get_inv_list('tabPurchase Invoice Item', 'purchase_receipt', doc['purchase_receipt_no'])
-
-	if inv_list:
-		jv_details_list = get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list)
-	
-	if not (inv_list and jv_details_list):
-		po_list = get_prev_doc_list(pr_obj, 'Purchase Order')
-		inv_list = get_inv_list('tabPurchase Invoice Item', 'purchase_order', po_list)
-		if inv_list:
-			jv_details_list = get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list)
-
-	return jv_details_list
diff --git a/accounts/doctype/account/account.py b/accounts/doctype/account/account.py
index 08bf80f..3cd131f 100644
--- a/accounts/doctype/account/account.py
+++ b/accounts/doctype/account/account.py
@@ -207,4 +207,4 @@
 		where group_or_ledger = 'Group' and docstatus != 2 and company = %s
 		and %s like %s order by name limit %s, %s""" % 
 		("%s", searchfield, "%s", "%s", "%s"), 
-		(filters["company"], "%%%s%%" % txt, start, page_len), as_list=1)
+		(filters["company"], "%%%s%%" % txt, start, page_len), as_list=1)
\ No newline at end of file
diff --git a/accounts/doctype/fiscal_year/fiscal_year.txt b/accounts/doctype/fiscal_year/fiscal_year.txt
index 18f20dd..935b76f 100644
--- a/accounts/doctype/fiscal_year/fiscal_year.txt
+++ b/accounts/doctype/fiscal_year/fiscal_year.txt
@@ -1,8 +1,8 @@
 [
  {
-  "creation": "2013-01-10 16:34:06", 
+  "creation": "2013-01-22 16:50:25", 
   "docstatus": 0, 
-  "modified": "2013-01-22 14:46:59", 
+  "modified": "2013-03-13 12:29:40", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -23,7 +23,6 @@
   "permlevel": 0
  }, 
  {
-  "amend": 0, 
   "cancel": 1, 
   "create": 1, 
   "doctype": "DocPerm", 
@@ -43,22 +42,6 @@
   "name": "Fiscal Year"
  }, 
  {
-  "doctype": "DocField", 
-  "fieldname": "year_details", 
-  "fieldtype": "Section Break", 
-  "label": "Fiscal Year Details", 
-  "oldfieldtype": "Section Break"
- }, 
- {
-  "doctype": "DocField", 
-  "fieldname": "trash_reason", 
-  "fieldtype": "Small Text", 
-  "label": "Trash Reason", 
-  "oldfieldname": "trash_reason", 
-  "oldfieldtype": "Small Text", 
-  "read_only": 1
- }, 
- {
   "description": "For e.g. 2012, 2012-13", 
   "doctype": "DocField", 
   "fieldname": "year", 
@@ -73,6 +56,7 @@
   "fieldname": "year_start_date", 
   "fieldtype": "Date", 
   "label": "Year Start Date", 
+  "no_copy": 1, 
   "oldfieldname": "year_start_date", 
   "oldfieldtype": "Date", 
   "reqd": 1
@@ -84,6 +68,7 @@
   "fieldname": "is_fiscal_year_closed", 
   "fieldtype": "Select", 
   "label": "Year Closed", 
+  "no_copy": 1, 
   "oldfieldname": "is_fiscal_year_closed", 
   "oldfieldtype": "Select", 
   "options": "\nNo\nYes", 
diff --git a/accounts/doctype/fiscal_year/test_fiscal_year.py b/accounts/doctype/fiscal_year/test_fiscal_year.py
index e031a19..b209b39 100644
--- a/accounts/doctype/fiscal_year/test_fiscal_year.py
+++ b/accounts/doctype/fiscal_year/test_fiscal_year.py
@@ -10,5 +10,15 @@
 		"doctype": "Fiscal Year",
 		"year": "_Test Fiscal Year 2014", 
 		"year_start_date": "2014-01-01"
-	}]
+	}],
+	[{
+		"doctype": "Fiscal Year",
+		"year": "_Test Fiscal Year 2015", 
+		"year_start_date": "2015-01-01"
+	}],
+	[{
+		"doctype": "Fiscal Year",
+		"year": "_Test Fiscal Year 2016", 
+		"year_start_date": "2016-01-01"
+	}],
 ]
\ No newline at end of file
diff --git a/accounts/doctype/journal_voucher/journal_voucher.py b/accounts/doctype/journal_voucher/journal_voucher.py
index 2acf08e..f4bd55c 100644
--- a/accounts/doctype/journal_voucher/journal_voucher.py
+++ b/accounts/doctype/journal_voucher/journal_voucher.py
@@ -63,6 +63,9 @@
 		self.make_gl_entries()
 
 	def on_cancel(self):
+		from accounts.utils import remove_against_link_from_jv
+		remove_against_link_from_jv(self.doc.doctype, self.doc.name, "against_jv")
+		
 		self.make_gl_entries(cancel=1)
 
 	def validate_debit_credit(self):
diff --git a/accounts/doctype/journal_voucher/test_journal_voucher.py b/accounts/doctype/journal_voucher/test_journal_voucher.py
index bb846d1..7cfeb59 100644
--- a/accounts/doctype/journal_voucher/test_journal_voucher.py
+++ b/accounts/doctype/journal_voucher/test_journal_voucher.py
@@ -19,8 +19,34 @@
 import unittest
 import webnotes
 
-test_records = [[
-	{
+class TestJournalVoucher(unittest.TestCase):
+	def test_journal_voucher_with_against_jv(self):
+		jv_invoice = webnotes.bean(copy=test_records[2])
+		jv_invoice.insert()
+		jv_invoice.submit()
+		
+		self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_jv=%s""", jv_invoice.doc.name))
+		
+		jv_payment = webnotes.bean(copy=test_records[0])
+		jv_payment.doclist[1].against_jv = jv_invoice.doc.name
+		jv_payment.insert()
+		jv_payment.submit()
+		
+		self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_jv=%s""", jv_invoice.doc.name))
+			
+		self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_jv=%s and credit=400""", jv_invoice.doc.name))
+		
+		# cancel jv_invoice
+		jv_invoice.cancel()
+		
+		self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_jv=%s""", jv_invoice.doc.name))
+
+test_records = [
+	[{
 		"company": "_Test Company", 
 		"doctype": "Journal Voucher", 
 		"fiscal_year": "_Test Fiscal Year 2013", 
@@ -44,8 +70,59 @@
 		"debit": 400.0,
 		"credit": 0.0,
 		"parentfield": "entries"
-	}
-]]
+	}],
+	[{
+		"company": "_Test Company", 
+		"doctype": "Journal Voucher", 
+		"fiscal_year": "_Test Fiscal Year 2013", 
+		"naming_series": "_T-Journal Voucher-",
+		"posting_date": "2013-02-14", 
+		"user_remark": "test",
+		"voucher_type": "Bank Voucher",
+		"cheque_no": "33",
+		"cheque_date": "2013-02-14"
+	}, 
+	{
+		"account": "_Test Supplier - _TC", 
+		"doctype": "Journal Voucher Detail", 
+		"credit": 0.0,
+		"debit": 400.0,
+		"parentfield": "entries"
+	}, 
+	{
+		"account": "_Test Account Bank Account - _TC", 
+		"doctype": "Journal Voucher Detail", 
+		"debit": 0.0,
+		"credit": 400.0,
+		"parentfield": "entries"
+	}],
+	[{
+		"company": "_Test Company", 
+		"doctype": "Journal Voucher", 
+		"fiscal_year": "_Test Fiscal Year 2013", 
+		"naming_series": "_T-Journal Voucher-",
+		"posting_date": "2013-02-14", 
+		"user_remark": "test",
+		"voucher_type": "Bank Voucher",
+		"cheque_no": "33",
+		"cheque_date": "2013-02-14"
+	}, 
+	{
+		"account": "_Test Customer - _TC", 
+		"doctype": "Journal Voucher Detail", 
+		"credit": 0.0,
+		"debit": 400.0,
+		"parentfield": "entries"
+	}, 
+	{
+		"account": "Sales - _TC", 
+		"doctype": "Journal Voucher Detail", 
+		"credit": 400.0,
+		"debit": 0.0,
+		"parentfield": "entries",
+		"cost_center": "_Test Cost Center - _TC"
+	}],
+]
 
 
 
diff --git a/accounts/doctype/pos_setting/test_pos_setting.py b/accounts/doctype/pos_setting/test_pos_setting.py
new file mode 100644
index 0000000..2c45c4d
--- /dev/null
+++ b/accounts/doctype/pos_setting/test_pos_setting.py
@@ -0,0 +1,14 @@
+test_records = [
+	[{
+		"doctype": "POS Setting",
+		"name": "_Test POS Setting",
+		"currency": "INR",
+		"conversion_rate": 1.0,
+		"price_list_name": "_Test Price List",
+		"company": "_Test Company",
+		"warehouse": "_Test Warehouse",
+		"cash_bank_account": "_Test Account Bank Account - _TC",
+		"income_account": "Sales - _TC",
+		"cost_center": "_Test Cost Center - _TC",
+	}]
+]
\ No newline at end of file
diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.js b/accounts/doctype/purchase_invoice/purchase_invoice.js
index f4a2e68..4a1cbba 100644
--- a/accounts/doctype/purchase_invoice/purchase_invoice.js
+++ b/accounts/doctype/purchase_invoice/purchase_invoice.js
@@ -238,8 +238,11 @@
 	refresh_field('entries');
 }
 
-cur_frm.fields_dict['entries'].grid.get_field("cost_center").get_query = function(doc) {
-	return 'SELECT `tabCost Center`.`name` FROM `tabCost Center` WHERE `tabCost Center`.`company_name` = "' +doc.company+'" AND `tabCost Center`.%(key)s LIKE "%s" AND `tabCost Center`.`group_or_ledger` = "Ledger" AND `tabCost Center`.docstatus != 2 ORDER BY	`tabCost Center`.`name` ASC LIMIT 50';
+cur_frm.fields_dict["entries"].grid.get_field("cost_center").get_query = function(doc) {
+	return {
+		query: "accounts.utils.get_cost_center_list",
+		filters: { company_name: doc.company}
+	}
 }
 
 cur_frm.cscript.cost_center = function(doc, cdt, cdn){
diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.py b/accounts/doctype/purchase_invoice/purchase_invoice.py
index 7722c98..f535d56 100644
--- a/accounts/doctype/purchase_invoice/purchase_invoice.py
+++ b/accounts/doctype/purchase_invoice/purchase_invoice.py
@@ -444,6 +444,9 @@
 					
 		# item gl entries
 		stock_item_and_auto_inventory_accounting = False
+		if auto_inventory_accounting:
+			stock_acocunt = self.get_default_account("stock_received_but_not_billed")
+			
 		for item in self.doclist.get({"parentfield": "entries"}):
 			if auto_inventory_accounting and item.item_code in self.stock_items:
 				if flt(item.valuation_rate):
@@ -455,7 +458,7 @@
 					
 					gl_entries.append(
 						self.get_gl_dict({
-							"account": "Stock Received But Not Billed - %s" % (self.company_abbr,),
+							"account": stock_acocunt,
 							"against": self.doc.credit_to,
 							"debit": flt(item.valuation_rate) * flt(item.conversion_factor) \
 								*  flt(item.qty),
@@ -480,7 +483,7 @@
 			# this will balance out valuation amount included in cost of goods sold
 			gl_entries.append(
 				self.get_gl_dict({
-					"account": "Expenses Included In Valuation - %s" % (self.company_abbr,),
+					"account": self.get_default_account("expenses_included_in_valuation"),
 					"cost_center": "Auto Inventory Accounting - %s" % self.company_abbr,
 					"against": self.doc.credit_to,
 					"credit": valuation_tax,
@@ -504,18 +507,13 @@
 		if gl_entries:
 			make_gl_entries(gl_entries, cancel=is_cancel)
 
-	def check_next_docstatus(self):
-		submit_jv = sql("select t1.name from `tabJournal Voucher` t1,`tabJournal Voucher Detail` t2 where t1.name = t2.parent and t2.against_voucher = '%s' and t1.docstatus = 1" % (self.doc.name))
-		if submit_jv:
-			msgprint("Journal Voucher : " + cstr(submit_jv[0][0]) + " has been created against " + cstr(self.doc.doctype) + ". So " + cstr(self.doc.doctype) + " cannot be Cancelled.")
-			raise Exception, "Validation Error."
-	
 	def on_cancel(self):
-		self.check_next_docstatus()
+		from accounts.utils import remove_against_link_from_jv
+		remove_against_link_from_jv(self.doc.doctype, self.doc.name, "against_voucher")
 		
 		self.make_gl_entries(is_cancel=1)
 		get_obj(dt = 'Purchase Common').update_prevdoc_detail(self, is_submit = 0)
-
+		
 	def on_update(self):
 		pass
 		
diff --git a/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/accounts/doctype/purchase_invoice/test_purchase_invoice.py
index b9f7ec9..6d9cfca 100644
--- a/accounts/doctype/purchase_invoice/test_purchase_invoice.py
+++ b/accounts/doctype/purchase_invoice/test_purchase_invoice.py
@@ -115,6 +115,42 @@
 		for i, item in enumerate(wrapper.doclist.get({"parentfield": "entries"})):
 			self.assertEqual(item.item_code, expected_values[i][0])
 			self.assertEqual(item.item_tax_amount, expected_values[i][1])
+			
+	def test_purchase_invoice_with_advance(self):
+		from accounts.doctype.journal_voucher.test_journal_voucher \
+			import test_records as jv_test_records
+			
+		jv = webnotes.bean(copy=jv_test_records[1])
+		jv.insert()
+		jv.submit()
+		
+		pi = webnotes.bean(copy=test_records[0])
+		pi.doclist.append({
+			"doctype": "Purchase Invoice Advance",
+			"parentfield": "advance_allocation_details",
+			"journal_voucher": jv.doc.name,
+			"jv_detail_no": jv.doclist[1].name,
+			"advance_amount": 400,
+			"allocated_amount": 300,
+			"remarks": jv.doc.remark
+		})
+		pi.run_method("calculate_taxes_and_totals")
+		pi.insert()
+		pi.submit()
+		pi.load_from_db()
+		
+		self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_voucher=%s""", pi.doc.name))
+		
+		self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_voucher=%s and debit=300""", pi.doc.name))
+			
+		self.assertEqual(pi.doc.outstanding_amount, 1212.30)
+		
+		pi.cancel()
+		
+		self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_voucher=%s""", pi.doc.name))
 	
 test_records = [
 	[
diff --git a/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.txt b/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.txt
index 201bb53..6b31684 100644
--- a/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.txt
+++ b/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.txt
@@ -1,8 +1,8 @@
 [
  {
-  "creation": "2013-02-22 01:27:40", 
+  "creation": "2013-03-08 15:36:46", 
   "docstatus": 0, 
-  "modified": "2013-03-07 07:03:26", 
+  "modified": "2013-03-20 16:52:12", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -40,7 +40,7 @@
  {
   "doctype": "DocField", 
   "fieldname": "jv_detail_no", 
-  "fieldtype": "Date", 
+  "fieldtype": "Data", 
   "hidden": 1, 
   "label": "Journal Voucher Detail No", 
   "oldfieldname": "jv_detail_no", 
diff --git a/accounts/doctype/sales_invoice/sales_invoice.js b/accounts/doctype/sales_invoice/sales_invoice.js
index a24e256..5cc09a0 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/accounts/doctype/sales_invoice/sales_invoice.js
@@ -271,8 +271,6 @@
 	if (doc.is_opening == 'Yes') unhide_field('aging_date');
 }
 
-/* **************************** TRIGGERS ********************************** */
-
 // Get Items based on SO or DN Selected
 cur_frm.cscript.get_items = function(doc, dt, dn) {
 	var callback = function(r,rt) {
@@ -371,6 +369,18 @@
 	return 'SELECT tabAccount.name FROM tabAccount WHERE (tabAccount.debit_or_credit="Credit" OR tabAccount.account_type = "Income Account") AND tabAccount.group_or_ledger="Ledger" AND tabAccount.docstatus!=2 AND tabAccount.company="'+doc.company+'" AND tabAccount.%(key)s LIKE "%s"';	
 })
 
+// expense account
+cur_frm.fields_dict['entries'].grid.get_field('expense_account').get_query = function(doc) {
+	return {
+		"query": "accounts.utils.get_account_list", 
+		"filters": {
+			"is_pl_account": "Yes",
+			"debit_or_credit": "Debit",
+			"company": doc.company
+		}
+	}
+}
+
 // warehouse in detail table
 //----------------------------
 cur_frm.fields_dict['entries'].grid.get_field('warehouse').get_query= function(doc, cdt, cdn) {
@@ -380,8 +390,11 @@
 
 // Cost Center in Details Table
 // -----------------------------
-cur_frm.fields_dict.entries.grid.get_field("cost_center").get_query = function(doc) {
-	return 'SELECT `tabCost Center`.`name` FROM `tabCost Center` WHERE `tabCost Center`.`company_name` = "' +doc.company+'" AND `tabCost Center`.%(key)s LIKE "%s" AND `tabCost Center`.`group_or_ledger` = "Ledger" AND `tabCost Center`.`docstatus`!= 2 ORDER BY	`tabCost Center`.`name` ASC LIMIT 50';
+cur_frm.fields_dict["entries"].grid.get_field("cost_center").get_query = function(doc) {
+	return {
+		query: "accounts.utils.get_cost_center_list",
+		filters: { company_name: doc.company}
+	}
 }
 
 // Sales Order
diff --git a/accounts/doctype/sales_invoice/sales_invoice.py b/accounts/doctype/sales_invoice/sales_invoice.py
index de3ee95..22ac845 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/accounts/doctype/sales_invoice/sales_invoice.py
@@ -17,7 +17,9 @@
 from __future__ import unicode_literals
 import webnotes
 
-from webnotes.utils import add_days, cint, cstr, date_diff, flt, getdate, nowdate
+from webnotes.utils import add_days, cint, cstr, date_diff, flt, getdate, nowdate, \
+	get_first_day, get_last_day
+
 from webnotes.utils.email_lib import sendmail
 from webnotes.utils import comma_and
 from webnotes.model.doc import make_autoname
@@ -47,6 +49,7 @@
 	def validate(self):
 		super(DocType, self).validate()
 		
+		self.validate_posting_time()
 		self.so_dn_required()
 		self.validate_proj_cust()
 		sales_com_obj = get_obj('Sales Common')
@@ -126,11 +129,14 @@
 		
 		sales_com_obj = get_obj(dt = 'Sales Common')
 		sales_com_obj.check_stop_sales_order(self)
-		self.check_next_docstatus()
+		
+		from accounts.utils import remove_against_link_from_jv
+		remove_against_link_from_jv(self.doc.doctype, self.doc.name, "against_invoice")
+
 		sales_com_obj.update_prevdoc_detail(0, self)
 		
 		self.make_gl_entries()
-
+		
 	def on_update_after_submit(self):
 		self.validate_recurring_invoice()
 		self.convert_to_recurring()
@@ -398,8 +404,7 @@
 		if lst:
 			from accounts.utils import reconcile_against_document
 			reconcile_against_document(lst)
-	
-	
+			
 	def validate_customer(self):
 		"""	Validate customer name with SO and DN"""
 		for d in getlist(self.doclist,'entries'):
@@ -636,10 +641,9 @@
 
 				# Reduce actual qty from warehouse
 				self.make_sl_entry( d, d['warehouse'], - flt(d['qty']) , 0, update_stock)
-				
+		
 		get_obj('Stock Ledger', 'Stock Ledger').update_stock(self.values)
-	
-
+		
 	def get_actual_qty(self,args):
 		args = eval(args)
 		actual_qty = webnotes.conn.sql("select actual_qty from `tabBin` where item_code = '%s' and warehouse = '%s'" % (args['item_code'], args['warehouse']), as_dict=1)
@@ -704,9 +708,9 @@
 			
 		if auto_inventory_accounting:
 			if cint(self.doc.is_pos) and cint(self.doc.update_stock):
-				stock_account = self.get_stock_in_hand_account()
+				stock_account = self.get_default_account("stock_in_hand_account")
 			else:
-				stock_account = "Stock Delivered But Not Billed - %s" % (self.company_abbr,)
+				stock_account = self.get_default_account("stock_delivered_but_not_billed")
 		
 		for item in self.doclist.get({"parentfield": "entries"}):
 			# income account gl entries
@@ -794,7 +798,8 @@
 			stock_ledger_entries = item_sales_bom = None
 			
 		for item in self.doclist.get({"parentfield": "entries"}):
-			if item.item_code in self.stock_items:
+			if item.item_code in self.stock_items or \
+					(item_sales_bom and item_sales_bom.get(item.item_code)):
 				item.buying_amount = self.get_item_buying_amount(item, stock_ledger_entries, 
 					item_sales_bom)
 				webnotes.conn.set_value("Sales Invoice Item", item.name, 
@@ -804,8 +809,9 @@
 		item_buying_amount = 0
 		if stock_ledger_entries:
 			# is pos and update stock
-			item_buying_amount = get_buying_amount(item.item_code, item.warehouse, item.qty, 
+			item_buying_amount = get_buying_amount(item.item_code, item.warehouse, -1*item.qty, 
 				self.doc.doctype, self.doc.name, item.name, stock_ledger_entries, item_sales_bom)
+			item.buying_amount = item_buying_amount > 0 and item_buying_amount or 0
 		elif item.delivery_note and item.dn_detail:
 			# against delivery note
 			dn_item = webnotes.conn.get_value("Delivery Note Item", item.dn_detail, 
@@ -828,12 +834,6 @@
 				grand_total = %s where invoice_no = %s and parent = %s""", 
 				(self.doc.name, self.doc.amended_from, self.doc.c_form_no))
 
-	def check_next_docstatus(self):
-		submit_jv = webnotes.conn.sql("select t1.name from `tabJournal Voucher` t1,`tabJournal Voucher Detail` t2 where t1.name = t2.parent and t2.against_invoice = '%s' and t1.docstatus = 1" % (self.doc.name))
-		if submit_jv:
-			msgprint("Journal Voucher : " + cstr(submit_jv[0][0]) + " has been created against " + cstr(self.doc.doctype) + ". So " + cstr(self.doc.doctype) + " cannot be Cancelled.")
-			raise Exception, "Validation Error."
-	
 	@property
 	def meta(self):
 		if not hasattr(self, "_meta"):
@@ -893,25 +893,18 @@
 		
 		next_date = get_next_date(self.doc.posting_date,
 			month_map[self.doc.recurring_type], cint(self.doc.repeat_on_day_of_month))
+		
 		webnotes.conn.set(self.doc, 'next_date', next_date)
 	
 def get_next_date(dt, mcount, day=None):
-	import datetime
-	month = getdate(dt).month + mcount
-	year = getdate(dt).year
-	if not day:
-		day = getdate(dt).day
-	if month > 12:
-		month, year = month-12, year+1
-	try:
-		next_month_date = datetime.date(year, month, day)
-	except:
-		import calendar
-		last_day = calendar.monthrange(year, month)[1]
-		next_month_date = datetime.date(year, month, last_day)
-	return next_month_date.strftime("%Y-%m-%d")
-
-def manage_recurring_invoices(next_date=None):
+	dt = getdate(dt)
+	
+	from dateutil.relativedelta import relativedelta
+	dt += relativedelta(months=mcount, day=day)
+	
+	return dt
+	
+def manage_recurring_invoices(next_date=None, commit=True):
 	""" 
 		Create recurring invoices on specific date by copying the original one
 		and notify the concerned people
@@ -931,19 +924,22 @@
 				ref_wrapper = webnotes.bean('Sales Invoice', ref_invoice)
 				new_invoice_wrapper = make_new_invoice(ref_wrapper, next_date)
 				send_notification(new_invoice_wrapper)
-				webnotes.conn.commit()
+				if commit:
+					webnotes.conn.commit()
 			except:
-				webnotes.conn.rollback()
+				if commit:
+					webnotes.conn.rollback()
 
-				webnotes.conn.begin()
-				webnotes.conn.sql("update `tabSales Invoice` set \
-					convert_into_recurring_invoice = 0 where name = %s", ref_invoice)
-				notify_errors(ref_invoice, ref_wrapper.doc.owner)
-				webnotes.conn.commit()
+					webnotes.conn.begin()
+					webnotes.conn.sql("update `tabSales Invoice` set \
+						convert_into_recurring_invoice = 0 where name = %s", ref_invoice)
+					notify_errors(ref_invoice, ref_wrapper.doc.owner)
+					webnotes.conn.commit()
 
 				exception_list.append(webnotes.getTraceback())
 			finally:
-				webnotes.conn.begin()
+				if commit:
+					webnotes.conn.begin()
 			
 	if exception_list:
 		exception_message = "\n\n".join([cstr(d) for d in exception_list])
@@ -955,19 +951,27 @@
 	new_invoice = clone(ref_wrapper)
 	
 	mcount = month_map[ref_wrapper.doc.recurring_type]
-		
+	
+	invoice_period_from_date = get_next_date(ref_wrapper.doc.invoice_period_from_date, mcount)
+	
+	# get last day of the month to maintain period if the from date is first day of its own month 
+	# and to date is the last day of its own month
+	if (cstr(get_first_day(ref_wrapper.doc.invoice_period_from_date)) == \
+			cstr(ref_wrapper.doc.invoice_period_from_date)) and \
+		(cstr(get_last_day(ref_wrapper.doc.invoice_period_to_date)) == \
+			cstr(ref_wrapper.doc.invoice_period_to_date)):
+		invoice_period_to_date = get_last_day(get_next_date(ref_wrapper.doc.invoice_period_to_date,
+			mcount))
+	else:
+		invoice_period_to_date = get_next_date(ref_wrapper.doc.invoice_period_to_date, mcount)
+	
 	new_invoice.doc.fields.update({
 		"posting_date": posting_date,
 		"aging_date": posting_date,
-		
 		"due_date": add_days(posting_date, cint(date_diff(ref_wrapper.doc.due_date,
 			ref_wrapper.doc.posting_date))),
-			
-		"invoice_period_from_date": \
-			get_next_date(ref_wrapper.doc.invoice_period_from_date, mcount),
-			
-		"invoice_period_to_date": \
-			get_next_date(ref_wrapper.doc.invoice_period_to_date, mcount),
+		"invoice_period_from_date": invoice_period_from_date,
+		"invoice_period_to_date": invoice_period_to_date,
 		"fiscal_year": get_fiscal_year(posting_date)[0],
 		"owner": ref_wrapper.doc.owner,
 	})
diff --git a/accounts/doctype/sales_invoice/sales_invoice.txt b/accounts/doctype/sales_invoice/sales_invoice.txt
index c7c8fba..462565c 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.txt
+++ b/accounts/doctype/sales_invoice/sales_invoice.txt
@@ -1,8 +1,8 @@
 [
  {
-  "creation": "2013-01-29 17:54:09", 
+  "creation": "2013-03-20 17:01:58", 
   "docstatus": 0, 
-  "modified": "2013-01-29 18:22:52", 
+  "modified": "2013-03-20 19:17:38", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -77,7 +77,6 @@
   "print_hide": 1
  }, 
  {
-  "default": "1", 
   "depends_on": "eval:doc.is_pos==1", 
   "doctype": "DocField", 
   "fieldname": "update_stock", 
@@ -1094,7 +1093,7 @@
   "description": "The day of the month on which auto invoice will be generated e.g. 05, 28 etc ", 
   "doctype": "DocField", 
   "fieldname": "repeat_on_day_of_month", 
-  "fieldtype": "Data", 
+  "fieldtype": "Int", 
   "label": "Repeat on Day of Month", 
   "no_copy": 1, 
   "print_hide": 1
diff --git a/accounts/doctype/sales_invoice/test_sales_invoice.py b/accounts/doctype/sales_invoice/test_sales_invoice.py
index 84eddea..92feae8 100644
--- a/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -281,6 +281,7 @@
 		return dn
 		
 	def _insert_pos_settings(self):
+		webnotes.conn.sql("""delete from `tabPOS Setting`""")
 		ps = webnotes.bean([
 			{
 				"cash_bank_account": "_Test Account Bank Account - _TC", 
@@ -297,7 +298,178 @@
 		])
 		ps.insert()
 		
-test_dependencies = ["Journal Voucher"]
+	def test_sales_invoice_with_advance(self):
+		from accounts.doctype.journal_voucher.test_journal_voucher \
+			import test_records as jv_test_records
+			
+		jv = webnotes.bean(copy=jv_test_records[0])
+		jv.insert()
+		jv.submit()
+		
+		si = webnotes.bean(copy=test_records[0])
+		si.doclist.append({
+			"doctype": "Sales Invoice Advance",
+			"parentfield": "advance_adjustment_details",
+			"journal_voucher": jv.doc.name,
+			"jv_detail_no": jv.doclist[1].name,
+			"advance_amount": 400,
+			"allocated_amount": 300,
+			"remarks": jv.doc.remark
+		})
+		si.insert()
+		si.submit()
+		si.load_from_db()
+		
+		self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_invoice=%s""", si.doc.name))
+		
+		self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_invoice=%s and credit=300""", si.doc.name))
+			
+		self.assertEqual(si.doc.outstanding_amount, 261.8)
+		
+		si.cancel()
+		
+		self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail`
+			where against_invoice=%s""", si.doc.name))
+			
+	def test_recurring_invoice(self):
+		from webnotes.utils import now_datetime, get_first_day, get_last_day, add_to_date
+		today = now_datetime().date()
+		
+		base_si = webnotes.bean(copy=test_records[0])
+		base_si.doc.fields.update({
+			"convert_into_recurring_invoice": 1,
+			"recurring_type": "Monthly",
+			"notification_email_address": "test@example.com, test1@example.com, test2@example.com",
+			"repeat_on_day_of_month": today.day,
+			"posting_date": today,
+			"invoice_period_from_date": get_first_day(today),
+			"invoice_period_to_date": get_last_day(today)
+		})
+		
+		# monthly
+		si1 = webnotes.bean(copy=base_si.doclist)
+		si1.insert()
+		si1.submit()
+		self._test_recurring_invoice(si1, True)
+		
+		# monthly without a first and last day period
+		si2 = webnotes.bean(copy=base_si.doclist)
+		si2.doc.fields.update({
+			"invoice_period_from_date": today,
+			"invoice_period_to_date": add_to_date(today, days=30)
+		})
+		si2.insert()
+		si2.submit()
+		self._test_recurring_invoice(si2, False)
+		
+		# quarterly
+		si3 = webnotes.bean(copy=base_si.doclist)
+		si3.doc.fields.update({
+			"recurring_type": "Quarterly",
+			"invoice_period_from_date": get_first_day(today),
+			"invoice_period_to_date": get_last_day(add_to_date(today, months=3))
+		})
+		si3.insert()
+		si3.submit()
+		self._test_recurring_invoice(si3, True)
+		
+		# quarterly without a first and last day period
+		si4 = webnotes.bean(copy=base_si.doclist)
+		si4.doc.fields.update({
+			"recurring_type": "Quarterly",
+			"invoice_period_from_date": today,
+			"invoice_period_to_date": add_to_date(today, months=3)
+		})
+		si4.insert()
+		si4.submit()
+		self._test_recurring_invoice(si4, False)
+		
+		# yearly
+		si5 = webnotes.bean(copy=base_si.doclist)
+		si5.doc.fields.update({
+			"recurring_type": "Yearly",
+			"invoice_period_from_date": get_first_day(today),
+			"invoice_period_to_date": get_last_day(add_to_date(today, years=1))
+		})
+		si5.insert()
+		si5.submit()
+		self._test_recurring_invoice(si5, True)
+		
+		# yearly without a first and last day period
+		si6 = webnotes.bean(copy=base_si.doclist)
+		si6.doc.fields.update({
+			"recurring_type": "Yearly",
+			"invoice_period_from_date": today,
+			"invoice_period_to_date": add_to_date(today, years=1)
+		})
+		si6.insert()
+		si6.submit()
+		self._test_recurring_invoice(si6, False)
+		
+		# change posting date but keep recuring day to be today
+		si7 = webnotes.bean(copy=base_si.doclist)
+		si7.doc.fields.update({
+			"posting_date": add_to_date(today, days=-3)
+		})
+		si7.insert()
+		si7.submit()
+		
+		# setting so that _test function works
+		si7.doc.posting_date = today
+		self._test_recurring_invoice(si7, True)
+			
+	def _test_recurring_invoice(self, base_si, first_and_last_day):
+		from webnotes.utils import add_months, get_last_day, getdate
+		from accounts.doctype.sales_invoice.sales_invoice import manage_recurring_invoices
+		
+		no_of_months = ({"Monthly": 1, "Quarterly": 3, "Yearly": 12})[base_si.doc.recurring_type]
+		
+		def _test(i):
+			self.assertEquals(i+1, webnotes.conn.sql("""select count(*) from `tabSales Invoice`
+				where recurring_id=%s and docstatus=1""", base_si.doc.recurring_id)[0][0])
+				
+			next_date = add_months(base_si.doc.posting_date, no_of_months)
+			
+			manage_recurring_invoices(next_date=next_date, commit=False)
+			
+			recurred_invoices = webnotes.conn.sql("""select name from `tabSales Invoice`
+				where recurring_id=%s and docstatus=1 order by name desc""", base_si.doc.recurring_id)
+			
+			self.assertEquals(i+2, len(recurred_invoices))
+			
+			new_si = webnotes.bean("Sales Invoice", recurred_invoices[0][0])
+			
+			for fieldname in ["convert_into_recurring_invoice", "recurring_type",
+				"repeat_on_day_of_month", "notification_email_address"]:
+					self.assertEquals(base_si.doc.fields.get(fieldname),
+						new_si.doc.fields.get(fieldname))
+
+			self.assertEquals(new_si.doc.posting_date, unicode(next_date))
+			
+			self.assertEquals(new_si.doc.invoice_period_from_date,
+				unicode(add_months(base_si.doc.invoice_period_from_date, no_of_months)))
+			
+			if first_and_last_day:
+				self.assertEquals(new_si.doc.invoice_period_to_date, 
+					unicode(get_last_day(add_months(base_si.doc.invoice_period_to_date,
+						no_of_months))))
+			else:
+				self.assertEquals(new_si.doc.invoice_period_to_date, 
+					unicode(add_months(base_si.doc.invoice_period_to_date, no_of_months)))
+					
+			self.assertEquals(getdate(new_si.doc.posting_date).day, 
+				base_si.doc.repeat_on_day_of_month)
+			
+			return new_si
+		
+		# if yearly, test 3 repetitions, else test 13 repetitions
+		count = no_of_months == 12 and 3 or 13
+		for i in xrange(count):
+			base_si = _test(i)
+		
+test_dependencies = ["Journal Voucher", "POS Setting"]
 
 test_records = [
 	[
diff --git a/accounts/doctype/sales_invoice_item/sales_invoice_item.txt b/accounts/doctype/sales_invoice_item/sales_invoice_item.txt
index ca078b5..6f6ad39 100644
--- a/accounts/doctype/sales_invoice_item/sales_invoice_item.txt
+++ b/accounts/doctype/sales_invoice_item/sales_invoice_item.txt
@@ -2,7 +2,7 @@
  {
   "creation": "2013-03-07 11:42:55", 
   "docstatus": 0, 
-  "modified": "2013-03-11 14:58:50", 
+  "modified": "2013-03-18 15:41:19", 
   "modified_by": "Administrator", 
   "owner": "Administrator"
  }, 
@@ -207,14 +207,16 @@
   "width": "120px"
  }, 
  {
+  "depends_on": "eval:sys_defaults.auto_inventory_accounting", 
   "doctype": "DocField", 
   "fieldname": "expense_account", 
   "fieldtype": "Link", 
-  "hidden": 1, 
+  "hidden": 0, 
   "in_filter": 1, 
   "label": "Expense Account", 
   "options": "Account", 
-  "print_hide": 1
+  "print_hide": 1, 
+  "width": "120px"
  }, 
  {
   "doctype": "DocField", 
diff --git a/accounts/doctype/sales_taxes_and_charges_master/sales_taxes_and_charges_master.js b/accounts/doctype/sales_taxes_and_charges_master/sales_taxes_and_charges_master.js
index 7d441ab..b1cbbdc 100644
--- a/accounts/doctype/sales_taxes_and_charges_master/sales_taxes_and_charges_master.js
+++ b/accounts/doctype/sales_taxes_and_charges_master/sales_taxes_and_charges_master.js
@@ -45,8 +45,7 @@
 	var make_row = function(title,val,bold){
 		var bstart = '<b>'; var bend = '</b>';
 		return '<tr><td style="width:50%;">'+(bold?bstart:'')+title+(bold?bend:'')+'</td>'
-		 +'<td style="width:25%;text-align:right;"></td>'
-		 +'<td style="width:25%;text-align:right;">'+format_currency(val, doc.currency)+'</td>'
+		 +'<td style="width:50%;text-align:right;">'+format_currency(val, doc.currency)+'</td>'
 		 +'</tr>'
 	}
 
diff --git a/accounts/page/accounts_home/accounts_home.js b/accounts/page/accounts_home/accounts_home.js
index f2faf93..403d7bd 100644
--- a/accounts/page/accounts_home/accounts_home.js
+++ b/accounts/page/accounts_home/accounts_home.js
@@ -64,12 +64,6 @@
 				description: "Close Balance Sheet and book Profit or Loss."
 			},
 			{
-				"route":"Form/Sales and Purchase Return Tool/Sales and Purchase Return Tool",
-				"label": wn._("Sales and Purchase Return Tool"),
-				description: wn._("Manage sales or purchase returns"),
-				"doctype": "Sales and Purchase Return Tool"
-			},
-			{
 				"page":"voucher-import-tool",
 				"label": wn._("Voucher Import Tool"),
 				"description": "Import accounting entries from CSV."
diff --git a/accounts/report/gross_profit/gross_profit.py b/accounts/report/gross_profit/gross_profit.py
index f1ae00e..2480e17 100644
--- a/accounts/report/gross_profit/gross_profit.py
+++ b/accounts/report/gross_profit/gross_profit.py
@@ -24,8 +24,10 @@
 	data = []
 	for row in delivery_note_items:
 		selling_amount = flt(row.amount)
-		buying_amount = get_buying_amount(row.item_code, row.warehouse, 
-			row.qty, "Delivery Note", row.name, row.item_row, stock_ledger_entries, item_sales_bom)
+		buying_amount = get_buying_amount(row.item_code, row.warehouse, -1*row.qty, 
+			"Delivery Note", row.name, row.item_row, stock_ledger_entries, item_sales_bom)
+		buying_amount = buying_amount > 0 and buying_amount or 0
+		
 		if selling_amount:
 			gross_profit = selling_amount - buying_amount
 			gross_profit_percent = (gross_profit / selling_amount) * 100.0
diff --git a/accounts/utils.py b/accounts/utils.py
index 14ceb4e..051cdd1 100644
--- a/accounts/utils.py
+++ b/accounts/utils.py
@@ -17,7 +17,7 @@
 from __future__ import unicode_literals
 
 import webnotes
-from webnotes.utils import nowdate, cstr, flt
+from webnotes.utils import nowdate, cstr, flt, now
 from webnotes.model.doc import addchild
 from webnotes import msgprint, _
 from webnotes.utils import formatdate
@@ -233,4 +233,18 @@
 	return webnotes.conn.sql("""select name, parent_cost_center from `tabCost Center` 
 		where docstatus < 2 %s and %s like %s order by name limit %s, %s""" % 
 		(conditions, searchfield, "%s", "%s", "%s"), 
-		tuple(filter_values + ["%%%s%%" % txt, start, page_len]))
\ No newline at end of file
+		tuple(filter_values + ["%%%s%%" % txt, start, page_len]))
+		
+def remove_against_link_from_jv(ref_type, ref_no, against_field):
+	webnotes.conn.sql("""update `tabJournal Voucher Detail` set `%s`=null,
+		modified=%s, modified_by=%s
+		where `%s`=%s and docstatus < 2""" % (against_field, "%s", "%s", against_field, "%s"), 
+		(now(), webnotes.session.user, ref_no))
+	
+	webnotes.conn.sql("""update `tabGL Entry`
+		set against_voucher_type=null, against_voucher=null,
+		modified=%s, modified_by=%s
+		where against_voucher_type=%s and against_voucher=%s
+		and voucher_no != ifnull(against_voucher, "")
+		and ifnull(is_cancelled, "No")="No" """,
+		(now(), webnotes.session.user, ref_type, ref_no))