blob: 707203f50057fd569136d75c6313a034c6be7e5c [file] [log] [blame]
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05301# ERPNext - web based ERP (http://erpnext.com)
2# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
Anand Doshi486f9df2012-07-19 13:40:31 +053017from __future__ import unicode_literals
Anand Doshi962c2aa2012-02-02 15:22:11 +053018import webnotes
Anand Doshi110047e2012-02-10 10:48:35 +053019from webnotes.utils import flt
20from webnotes.model.code import get_obj
Anand Doshi440c1eb2012-09-28 16:03:11 +053021from accounts.utils import get_balance_on
Anand Doshi962c2aa2012-02-02 15:22:11 +053022
Rushabh Mehtaf17ce7b2012-02-13 16:50:52 +053023@webnotes.whitelist()
Anand Doshi962c2aa2012-02-02 15:22:11 +053024def get_default_bank_account():
25 """
26 Get default bank account for a company
27 """
28 company = webnotes.form_dict.get('company')
29 if not company: return
30 res = webnotes.conn.sql("""\
31 SELECT default_bank_account FROM `tabCompany`
32 WHERE name=%s AND docstatus<2""", company)
33
34 if res: return res[0][0]
Anand Doshi110047e2012-02-10 10:48:35 +053035
Rushabh Mehtaf17ce7b2012-02-13 16:50:52 +053036@webnotes.whitelist()
Anand Doshi110047e2012-02-10 10:48:35 +053037def get_new_jv_details():
38 """
39 Get details which will help create new jv on sales/purchase return
40 """
41 doclist = webnotes.form_dict.get('doclist')
42 fiscal_year = webnotes.form_dict.get('fiscal_year')
43 if not (isinstance(doclist, basestring) and isinstance(fiscal_year, basestring)): return
44
45 import json
46 doclist = json.loads(doclist)
47 doc, children = doclist[0], doclist[1:]
48
49 if doc.get('return_type')=='Sales Return':
50 if doc.get('sales_invoice_no'):
51 return get_invoice_details(doc, children, fiscal_year)
52 elif doc.get('delivery_note_no'):
53 return get_delivery_note_details(doc, children, fiscal_year)
54
55 elif doc.get('purchase_receipt_no'):
56 return get_purchase_receipt_details(doc, children, fiscal_year)
57
58
59def get_invoice_details(doc, children, fiscal_year):
60 """
61 Gets details from an invoice to make new jv
62 Returns [{
63 'account': ,
64 'balance': ,
65 'debit': ,
66 'credit': ,
67 'against_invoice': ,
68 'against_payable':
69 }, { ... }, ...]
70 """
71 if doc.get('return_type')=='Sales Return':
Anand Doshifedfd892012-03-30 12:29:06 +053072 obj = get_obj('Sales Invoice', doc.get('sales_invoice_no'), with_children=1)
Anand Doshi110047e2012-02-10 10:48:35 +053073 else:
Anand Doshifedfd892012-03-30 12:29:06 +053074 obj = get_obj('Purchase Invoice', doc.get('purchase_invoice_no'), with_children=1)
Anand Doshi110047e2012-02-10 10:48:35 +053075 if not obj.doc.docstatus==1: return
76
77 # Build invoice account jv detail record
78 invoice_rec = get_invoice_account_jv_record(doc, children, fiscal_year, obj)
79
80 # Build item accountwise jv detail records
81 item_accountwise_list = get_item_accountwise_jv_record(doc, children, fiscal_year, obj)
82
83 return [invoice_rec] + item_accountwise_list
84
85
86def get_invoice_account_jv_record(doc, children, fiscal_year, obj):
87 """
88 Build customer/supplier account jv detail record
89 """
90 # Calculate total return amount
91 total_amt = sum([(flt(ch.get('rate')) * flt(ch.get('returned_qty'))) for ch in children])
92
93 ret = {}
94
95 if doc.get('return_type')=='Sales Return':
96 account = obj.doc.debit_to
97 ret['against_invoice'] = doc.get('sales_invoice_no')
98 ret['credit'] = total_amt
99 else:
100 account = obj.doc.credit_to
101 ret['against_voucher'] = doc.get('purchase_invoice_no')
102 ret['debit'] = total_amt
103
104 ret.update({
105 'account': account,
Anand Doshi440c1eb2012-09-28 16:03:11 +0530106 'balance': get_balance_on(account, doc.get("return_date"))
Anand Doshi110047e2012-02-10 10:48:35 +0530107 })
108
109 return ret
110
111
112def get_item_accountwise_jv_record(doc, children, fiscal_year, obj):
113 """
114 Build item accountwise jv detail records
115 """
116 if doc.get('return_type')=='Sales Return':
117 amt_field = 'debit'
118 ac_field = 'income_account'
119 else:
120 amt_field = 'credit'
121 ac_field = 'expense_head'
122
123 inv_children = dict([[ic.fields.get('item_code'), ic] for ic in obj.doclist if ic.fields.get('item_code')])
124
125 accwise_list = []
126
127 for ch in children:
128 inv_ch = inv_children.get(ch.get('item_code'))
129 if not inv_ch: continue
130
131 amount = flt(ch.get('rate')) * flt(ch.get('returned_qty'))
132
133 accounts = [[jvd['account'], jvd['cost_center']] for jvd in accwise_list]
134
135 if [inv_ch.fields.get(ac_field), inv_ch.fields.get('cost_center')] not in accounts:
136 rec = {
137 'account': inv_ch.fields.get(ac_field),
138 'cost_center': inv_ch.fields.get('cost_center'),
Anand Doshi440c1eb2012-09-28 16:03:11 +0530139 'balance': get_balance_on(inv_ch.fields.get(ac_field),
140 doc.get("return_date"))
Anand Doshi110047e2012-02-10 10:48:35 +0530141 }
142 rec[amt_field] = amount
143 accwise_list.append(rec)
144 else:
145 rec = accwise_list[accounts.index([inv_ch.fields.get(ac_field), inv_ch.fields.get('cost_center')])]
146 rec[amt_field] = rec[amt_field] + amount
147
148 return accwise_list
149
150
151def get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list):
152 """
153 Get invoice details and make jv detail records
154 """
155 for inv in inv_list:
156 if not inv[0]: continue
157
158 if doc.get('return_type')=='Sales Return':
159 doc['sales_invoice_no'] = inv[0]
160 else:
161 doc['purchase_invoice_no'] = inv[0]
162
163 jv_details = get_invoice_details(doc, children, fiscal_year)
164
165 if jv_details and len(jv_details)>1: jv_details_list.extend(jv_details)
166
167 return jv_details_list
168
169
170def get_prev_doc_list(obj, prev_doctype):
171 """
172 Returns a list of previous doc's names
173 """
174 prevdoc_list = []
175 for ch in obj.doclist:
176 if ch.fields.get('prevdoc_docname') and ch.fields.get('prevdoc_doctype')==prev_doctype:
177 prevdoc_list.append(ch.fields.get('prevdoc_docname'))
178 return prevdoc_list
179
180
181def get_inv_list(table, field, value):
182 """
183 Returns invoice list
184 """
185 if isinstance(value, basestring):
186 return webnotes.conn.sql("""\
187 SELECT DISTINCT parent FROM `%s`
188 WHERE %s='%s' AND docstatus=1""" % (table, field, value))
189 elif isinstance(value, list):
190 return webnotes.conn.sql("""\
191 SELECT DISTINCT parent FROM `%s`
192 WHERE %s IN ("%s") AND docstatus=1""" % (table, field, '", "'.join(value)))
193 else:
194 return []
195
196
197def get_delivery_note_details(doc, children, fiscal_year):
198 """
199 Gets sales invoice numbers from delivery note details
200 and returns detail records for jv
201 """
202 jv_details_list = []
203
204 dn_obj = get_obj('Delivery Note', doc['delivery_note_no'], with_children=1)
205
Anand Doshifedfd892012-03-30 12:29:06 +0530206 inv_list = get_inv_list('tabSales Invoice Item', 'delivery_note', doc['delivery_note_no'])
Anand Doshi110047e2012-02-10 10:48:35 +0530207
208 if inv_list:
209 jv_details_list = get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list)
210
211 if not (inv_list and jv_details_list):
212 so_list = get_prev_doc_list(dn_obj, 'Sales Order')
Anand Doshifedfd892012-03-30 12:29:06 +0530213 inv_list = get_inv_list('tabSales Invoice Item', 'sales_order', so_list)
Anand Doshi110047e2012-02-10 10:48:35 +0530214 if inv_list:
215 jv_details_list = get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list)
216
217 return jv_details_list
218
219
220def get_purchase_receipt_details(doc, children, fiscal_year):
221 """
222 Gets purchase invoice numbers from purchase receipt details
223 and returns detail records for jv
224 """
225 jv_details_list = []
226
227 pr_obj = get_obj('Purchase Receipt', doc['purchase_receipt_no'], with_children=1)
228
Anand Doshifedfd892012-03-30 12:29:06 +0530229 inv_list = get_inv_list('tabPurchase Invoice Item', 'purchase_receipt', doc['purchase_receipt_no'])
Anand Doshi110047e2012-02-10 10:48:35 +0530230
231 if inv_list:
232 jv_details_list = get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list)
233
234 if not (inv_list and jv_details_list):
235 po_list = get_prev_doc_list(pr_obj, 'Purchase Order')
Anand Doshifedfd892012-03-30 12:29:06 +0530236 inv_list = get_inv_list('tabPurchase Invoice Item', 'purchase_order', po_list)
Anand Doshi110047e2012-02-10 10:48:35 +0530237 if inv_list:
238 jv_details_list = get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list)
239
240 return jv_details_list