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