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