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