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