Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +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 | |
| 17 | from __future__ import unicode_literals |
| 18 | import webnotes |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 19 | from webnotes.utils import cint |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 20 | from setup.utils import get_company_currency |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 21 | from webnotes import msgprint, _ |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 22 | |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 23 | from controllers.stock_controller import StockController |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 24 | |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 25 | class SellingController(StockController): |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 26 | def validate(self): |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 27 | super(SellingController, self).validate() |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 28 | self.set_total_in_words() |
| 29 | |
| 30 | def set_total_in_words(self): |
| 31 | from webnotes.utils import money_in_words |
| 32 | company_currency = get_company_currency(self.doc.company) |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 33 | |
| 34 | disable_rounded_total = cint(webnotes.conn.get_value("Global Defaults", None, |
| 35 | "disable_rounded_total")) |
| 36 | |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 37 | if self.meta.get_field("in_words"): |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 38 | self.doc.in_words = money_in_words(disable_rounded_total and |
| 39 | self.doc.grand_total or self.doc.rounded_total, company_currency) |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 40 | if self.meta.get_field("in_words_export"): |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 41 | self.doc.in_words_export = money_in_words(disable_rounded_total and |
Nabin Hait | 8c7234f | 2013-03-11 16:32:33 +0530 | [diff] [blame] | 42 | self.doc.grand_total_export or self.doc.rounded_total_export, self.doc.currency) |
Nabin Hait | a0e7c15 | 2013-03-21 18:37:06 +0530 | [diff] [blame] | 43 | |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 44 | def set_buying_amount(self, stock_ledger_entries = None): |
Anand Doshi | 8c5844e | 2013-03-21 20:24:10 +0530 | [diff] [blame] | 45 | from stock.utils import get_buying_amount |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 46 | if not stock_ledger_entries: |
| 47 | stock_ledger_entries = self.get_stock_ledger_entries() |
Anand Doshi | 8c5844e | 2013-03-21 20:24:10 +0530 | [diff] [blame] | 48 | |
| 49 | item_sales_bom = {} |
| 50 | for d in self.doclist.get({"parentfield": "packing_details"}): |
| 51 | new_d = webnotes._dict(d.fields.copy()) |
| 52 | new_d.total_qty = -1 * d.qty |
| 53 | item_sales_bom.setdefault(d.parent_item, []).append(new_d) |
Nabin Hait | a0e7c15 | 2013-03-21 18:37:06 +0530 | [diff] [blame] | 54 | |
| 55 | if stock_ledger_entries: |
| 56 | for item in self.doclist.get({"parentfield": self.fname}): |
| 57 | if item.item_code in self.stock_items or \ |
| 58 | (item_sales_bom and item_sales_bom.get(item.item_code)): |
| 59 | buying_amount = get_buying_amount(item.item_code, item.warehouse, -1*item.qty, |
| 60 | self.doc.doctype, self.doc.name, item.name, stock_ledger_entries, |
| 61 | item_sales_bom) |
Nabin Hait | f2d4df9 | 2013-05-07 13:12:02 +0530 | [diff] [blame] | 62 | |
| 63 | item.buying_amount = buying_amount >= 0.01 and buying_amount or 0 |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 64 | webnotes.conn.set_value(item.doctype, item.name, "buying_amount", |
| 65 | item.buying_amount) |
| 66 | |
| 67 | def check_expense_account(self, item): |
| 68 | if item.buying_amount and not item.expense_account: |
| 69 | msgprint(_("""Expense account is mandatory for item: """) + item.item_code, |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 70 | raise_exception=1) |
| 71 | |
| 72 | if item.buying_amount and not item.cost_center: |
| 73 | msgprint(_("""Cost Center is mandatory for item: """) + item.item_code, |
Nabin Hait | 557d858 | 2013-03-25 18:28:16 +0530 | [diff] [blame] | 74 | raise_exception=1) |