blob: aa2af626fc377d33c3c62088be3294628de09755 [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 Doshi962c2aa2012-02-02 15:22:11 +053021
Rushabh Mehtad48417c2012-03-19 18:42:13 +053022install_docs = [
Anand Doshi24e18562012-04-17 11:45:35 +053023 {"doctype":"Role", "role_name":"Accounts Manager", "name":"Accounts Manager"},
24 {"doctype":"Role", "role_name":"Accounts User", "name":"Accounts User"}
Rushabh Mehtad48417c2012-03-19 18:42:13 +053025]
26
Rushabh Mehtaf17ce7b2012-02-13 16:50:52 +053027@webnotes.whitelist()
Anand Doshi962c2aa2012-02-02 15:22:11 +053028def get_default_bank_account():
29 """
30 Get default bank account for a company
31 """
32 company = webnotes.form_dict.get('company')
33 if not company: return
34 res = webnotes.conn.sql("""\
35 SELECT default_bank_account FROM `tabCompany`
36 WHERE name=%s AND docstatus<2""", company)
37
38 if res: return res[0][0]
Anand Doshi110047e2012-02-10 10:48:35 +053039
Rushabh Mehtaf17ce7b2012-02-13 16:50:52 +053040@webnotes.whitelist()
Anand Doshi110047e2012-02-10 10:48:35 +053041def get_new_jv_details():
42 """
43 Get details which will help create new jv on sales/purchase return
44 """
45 doclist = webnotes.form_dict.get('doclist')
46 fiscal_year = webnotes.form_dict.get('fiscal_year')
47 if not (isinstance(doclist, basestring) and isinstance(fiscal_year, basestring)): return
48
49 import json
50 doclist = json.loads(doclist)
51 doc, children = doclist[0], doclist[1:]
52
53 if doc.get('return_type')=='Sales Return':
54 if doc.get('sales_invoice_no'):
55 return get_invoice_details(doc, children, fiscal_year)
56 elif doc.get('delivery_note_no'):
57 return get_delivery_note_details(doc, children, fiscal_year)
58
59 elif doc.get('purchase_receipt_no'):
60 return get_purchase_receipt_details(doc, children, fiscal_year)
61
62
63def get_invoice_details(doc, children, fiscal_year):
64 """
65 Gets details from an invoice to make new jv
66 Returns [{
67 'account': ,
68 'balance': ,
69 'debit': ,
70 'credit': ,
71 'against_invoice': ,
72 'against_payable':
73 }, { ... }, ...]
74 """
75 if doc.get('return_type')=='Sales Return':
Anand Doshifedfd892012-03-30 12:29:06 +053076 obj = get_obj('Sales Invoice', doc.get('sales_invoice_no'), with_children=1)
Anand Doshi110047e2012-02-10 10:48:35 +053077 else:
Anand Doshifedfd892012-03-30 12:29:06 +053078 obj = get_obj('Purchase Invoice', doc.get('purchase_invoice_no'), with_children=1)
Anand Doshi110047e2012-02-10 10:48:35 +053079 if not obj.doc.docstatus==1: return
80
81 # Build invoice account jv detail record
82 invoice_rec = get_invoice_account_jv_record(doc, children, fiscal_year, obj)
83
84 # Build item accountwise jv detail records
85 item_accountwise_list = get_item_accountwise_jv_record(doc, children, fiscal_year, obj)
86
87 return [invoice_rec] + item_accountwise_list
88
89
90def get_invoice_account_jv_record(doc, children, fiscal_year, obj):
91 """
92 Build customer/supplier account jv detail record
93 """
94 # Calculate total return amount
95 total_amt = sum([(flt(ch.get('rate')) * flt(ch.get('returned_qty'))) for ch in children])
96
97 ret = {}
98
99 if doc.get('return_type')=='Sales Return':
100 account = obj.doc.debit_to
101 ret['against_invoice'] = doc.get('sales_invoice_no')
102 ret['credit'] = total_amt
103 else:
104 account = obj.doc.credit_to
105 ret['against_voucher'] = doc.get('purchase_invoice_no')
106 ret['debit'] = total_amt
107
108 ret.update({
109 'account': account,
110 'balance': get_obj('GL Control').get_bal(account + "~~~" + fiscal_year)
111 })
112
113 return ret
114
115
116def get_item_accountwise_jv_record(doc, children, fiscal_year, obj):
117 """
118 Build item accountwise jv detail records
119 """
120 if doc.get('return_type')=='Sales Return':
121 amt_field = 'debit'
122 ac_field = 'income_account'
123 else:
124 amt_field = 'credit'
125 ac_field = 'expense_head'
126
127 inv_children = dict([[ic.fields.get('item_code'), ic] for ic in obj.doclist if ic.fields.get('item_code')])
128
129 accwise_list = []
130
131 for ch in children:
132 inv_ch = inv_children.get(ch.get('item_code'))
133 if not inv_ch: continue
134
135 amount = flt(ch.get('rate')) * flt(ch.get('returned_qty'))
136
137 accounts = [[jvd['account'], jvd['cost_center']] for jvd in accwise_list]
138
139 if [inv_ch.fields.get(ac_field), inv_ch.fields.get('cost_center')] not in accounts:
140 rec = {
141 'account': inv_ch.fields.get(ac_field),
142 'cost_center': inv_ch.fields.get('cost_center'),
143 'balance': get_obj('GL Control').get_bal(inv_ch.fields.get(ac_field) + "~~~" + fiscal_year)
144 }
145 rec[amt_field] = amount
146 accwise_list.append(rec)
147 else:
148 rec = accwise_list[accounts.index([inv_ch.fields.get(ac_field), inv_ch.fields.get('cost_center')])]
149 rec[amt_field] = rec[amt_field] + amount
150
151 return accwise_list
152
153
154def get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list):
155 """
156 Get invoice details and make jv detail records
157 """
158 for inv in inv_list:
159 if not inv[0]: continue
160
161 if doc.get('return_type')=='Sales Return':
162 doc['sales_invoice_no'] = inv[0]
163 else:
164 doc['purchase_invoice_no'] = inv[0]
165
166 jv_details = get_invoice_details(doc, children, fiscal_year)
167
168 if jv_details and len(jv_details)>1: jv_details_list.extend(jv_details)
169
170 return jv_details_list
171
172
173def get_prev_doc_list(obj, prev_doctype):
174 """
175 Returns a list of previous doc's names
176 """
177 prevdoc_list = []
178 for ch in obj.doclist:
179 if ch.fields.get('prevdoc_docname') and ch.fields.get('prevdoc_doctype')==prev_doctype:
180 prevdoc_list.append(ch.fields.get('prevdoc_docname'))
181 return prevdoc_list
182
183
184def get_inv_list(table, field, value):
185 """
186 Returns invoice list
187 """
188 if isinstance(value, basestring):
189 return webnotes.conn.sql("""\
190 SELECT DISTINCT parent FROM `%s`
191 WHERE %s='%s' AND docstatus=1""" % (table, field, value))
192 elif isinstance(value, list):
193 return webnotes.conn.sql("""\
194 SELECT DISTINCT parent FROM `%s`
195 WHERE %s IN ("%s") AND docstatus=1""" % (table, field, '", "'.join(value)))
196 else:
197 return []
198
199
200def get_delivery_note_details(doc, children, fiscal_year):
201 """
202 Gets sales invoice numbers from delivery note details
203 and returns detail records for jv
204 """
205 jv_details_list = []
206
207 dn_obj = get_obj('Delivery Note', doc['delivery_note_no'], with_children=1)
208
Anand Doshifedfd892012-03-30 12:29:06 +0530209 inv_list = get_inv_list('tabSales Invoice Item', 'delivery_note', doc['delivery_note_no'])
Anand Doshi110047e2012-02-10 10:48:35 +0530210
211 if inv_list:
212 jv_details_list = get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list)
213
214 if not (inv_list and jv_details_list):
215 so_list = get_prev_doc_list(dn_obj, 'Sales Order')
Anand Doshifedfd892012-03-30 12:29:06 +0530216 inv_list = get_inv_list('tabSales Invoice Item', 'sales_order', so_list)
Anand Doshi110047e2012-02-10 10:48:35 +0530217 if inv_list:
218 jv_details_list = get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list)
219
220 return jv_details_list
221
222
223def get_purchase_receipt_details(doc, children, fiscal_year):
224 """
225 Gets purchase invoice numbers from purchase receipt details
226 and returns detail records for jv
227 """
228 jv_details_list = []
229
230 pr_obj = get_obj('Purchase Receipt', doc['purchase_receipt_no'], with_children=1)
231
Anand Doshifedfd892012-03-30 12:29:06 +0530232 inv_list = get_inv_list('tabPurchase Invoice Item', 'purchase_receipt', doc['purchase_receipt_no'])
Anand Doshi110047e2012-02-10 10:48:35 +0530233
234 if inv_list:
235 jv_details_list = get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list)
236
237 if not (inv_list and jv_details_list):
238 po_list = get_prev_doc_list(pr_obj, 'Purchase Order')
Anand Doshifedfd892012-03-30 12:29:06 +0530239 inv_list = get_inv_list('tabPurchase Invoice Item', 'purchase_order', po_list)
Anand Doshi110047e2012-02-10 10:48:35 +0530240 if inv_list:
241 jv_details_list = get_jv_details_from_inv_list(doc, children, fiscal_year, inv_list, jv_details_list)
242
243 return jv_details_list