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