Nabin Hait | a76a068 | 2013-02-08 14:04:13 +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 |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 19 | from webnotes import msgprint, _ |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 20 | from webnotes.utils import flt, cint, comma_and |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 21 | import json |
Nabin Hait | a76a068 | 2013-02-08 14:04:13 +0530 | [diff] [blame] | 22 | |
| 23 | def get_customer_list(doctype, txt, searchfield, start, page_len, filters): |
| 24 | if webnotes.conn.get_default("cust_master_name") == "Customer Name": |
| 25 | fields = ["name", "customer_group", "territory"] |
| 26 | else: |
| 27 | fields = ["name", "customer_name", "customer_group", "territory"] |
| 28 | |
| 29 | return webnotes.conn.sql("""select %s from `tabCustomer` where docstatus < 2 |
| 30 | and (%s like %s or customer_name like %s) order by |
| 31 | case when name like %s then 0 else 1 end, |
| 32 | case when customer_name like %s then 0 else 1 end, |
| 33 | name, customer_name limit %s, %s""" % |
| 34 | (", ".join(fields), searchfield, "%s", "%s", "%s", "%s", "%s", "%s"), |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 35 | ("%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, start, page_len)) |
| 36 | |
| 37 | @webnotes.whitelist() |
| 38 | def get_item_details(args): |
| 39 | """ |
| 40 | args = { |
| 41 | "item_code": "", |
| 42 | "warehouse": None, |
| 43 | "customer": "", |
| 44 | "conversion_rate": 1.0, |
| 45 | "price_list_name": None, |
| 46 | "price_list_currency": None, |
| 47 | "plc_conversion_rate": 1.0 |
| 48 | } |
| 49 | """ |
| 50 | if isinstance(args, basestring): |
| 51 | args = json.loads(args) |
| 52 | args = webnotes._dict(args) |
| 53 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 54 | if args.barcode: |
| 55 | args.item_code = _get_item_code(args.barcode) |
| 56 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 57 | item_bean = webnotes.bean("Item", args.item_code) |
| 58 | |
| 59 | _validate_item_details(args, item_bean.doc) |
| 60 | |
| 61 | out = _get_basic_details(args, item_bean) |
| 62 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 63 | meta = webnotes.get_doctype(args.doctype) |
| 64 | if meta.get_field("currency"): |
| 65 | out.base_ref_rate = out.basic_rate = out.ref_rate = out.export_rate = 0.0 |
| 66 | |
| 67 | if args.price_list_name and args.price_list_currency: |
| 68 | out.update(_get_price_list_rate(args, item_bean, meta)) |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 69 | |
| 70 | if out.warehouse or out.reserved_warehouse: |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 71 | out.update(get_available_qty(args.item_code, out.warehouse or out.reserved_warehouse)) |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 72 | |
| 73 | out.customer_item_code = _get_customer_item_code(args, item_bean) |
| 74 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 75 | if cint(args.is_pos): |
| 76 | pos_settings = get_pos_settings(args.company) |
| 77 | out.update(apply_pos_settings(pos_settings, out)) |
| 78 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 79 | return out |
| 80 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 81 | def _get_item_code(barcode): |
| 82 | item_code = webnotes.conn.sql_list("""select name from `tabItem` where barcode=%s""", barcode) |
| 83 | |
| 84 | if not item_code: |
| 85 | msgprint(_("No Item found with Barcode") + ": %s" % barcode, raise_exception=True) |
| 86 | |
| 87 | elif len(item_code) > 1: |
| 88 | msgprint(_("Items") + " %s " % comma_and(item_code) + |
| 89 | _("have the same Barcode") + " %s" % barcode, raise_exception=True) |
| 90 | |
| 91 | return item_code[0] |
| 92 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 93 | def _validate_item_details(args, item): |
| 94 | from utilities.transaction_base import validate_item_fetch |
| 95 | validate_item_fetch(args, item) |
| 96 | |
| 97 | # validate if sales item or service item |
| 98 | if args.order_type == "Maintenance": |
| 99 | if item.is_service_item != "Yes": |
| 100 | msgprint(_("Item") + (" %s: " % item.name) + |
| 101 | _("not a service item.") + |
| 102 | _("Please select a service item or change the order type to Sales."), |
| 103 | raise_exception=True) |
| 104 | |
| 105 | elif item.is_sales_item != "Yes": |
| 106 | msgprint(_("Item") + (" %s: " % item.name) + _("not a sales item"), |
| 107 | raise_exception=True) |
| 108 | |
| 109 | def _get_basic_details(args, item_bean): |
| 110 | item = item_bean.doc |
| 111 | out = webnotes._dict({ |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 112 | "item_code": item.name, |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 113 | "description": item.description_html or item.description, |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 114 | "reserved_warehouse": item.default_warehouse or args.warehouse or args.reserved_warehouse, |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 115 | "warehouse": item.default_warehouse or args.warehouse, |
Rushabh Mehta | d9e7070 | 2013-07-08 18:01:46 +0530 | [diff] [blame] | 116 | "income_account": item.default_income_account or args.income_account \ |
| 117 | or webnotes.conn.get_value("Company", args.company, "default_income_account"), |
| 118 | "expense_account": item.purchase_account or args.expense_account \ |
| 119 | or webnotes.conn.get_value("Company", args.company, "default_expense_account"), |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 120 | "cost_center": item.default_sales_cost_center or args.cost_center, |
| 121 | "qty": 1.0, |
| 122 | "adj_rate": 0.0, |
| 123 | "export_amount": 0.0, |
| 124 | "amount": 0.0, |
| 125 | "batch_no": None, |
| 126 | "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in |
| 127 | item_bean.doclist.get({"parentfield": "item_tax"})))), |
| 128 | }) |
| 129 | |
| 130 | for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"): |
| 131 | out[fieldname] = item.fields.get(fieldname) |
| 132 | |
| 133 | return out |
| 134 | |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 135 | def _get_price_list_rate(args, item_bean, meta): |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 136 | base_ref_rate = item_bean.doclist.get({ |
| 137 | "parentfield": "ref_rate_details", |
| 138 | "price_list_name": args.price_list_name, |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 139 | "ref_currency": args.price_list_currency, |
Anand Doshi | 060d924 | 2013-06-12 17:40:36 +0530 | [diff] [blame] | 140 | "buying_or_selling": "Selling"}) |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 141 | |
| 142 | if not base_ref_rate: |
| 143 | return {} |
| 144 | |
| 145 | # found price list rate - now we can validate |
| 146 | from utilities.transaction_base import validate_currency |
| 147 | validate_currency(args, item_bean.doc, meta) |
| 148 | |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 149 | return {"ref_rate": flt(base_ref_rate[0].ref_rate * args.plc_conversion_rate / args.conversion_rate)} |
| 150 | |
| 151 | @webnotes.whitelist() |
| 152 | def get_available_qty(item_code, warehouse): |
| 153 | return webnotes.conn.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 154 | ["projected_qty", "actual_qty"], as_dict=True) or {} |
| 155 | |
| 156 | def _get_customer_item_code(args, item_bean): |
| 157 | customer_item_code = item_bean.doclist.get({"parentfield": "item_customer_details", |
| 158 | "customer_name": args.customer}) |
| 159 | |
| 160 | return customer_item_code and customer_item_code[0].ref_code or None |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 161 | |
| 162 | def get_pos_settings(company): |
| 163 | pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting` where user = %s |
| 164 | and company = %s""", (webnotes.session['user'], company), as_dict=1) |
| 165 | |
| 166 | if not pos_settings: |
| 167 | pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting` |
| 168 | where ifnull(user,'') = '' and company = %s""", company, as_dict=1) |
| 169 | |
| 170 | return pos_settings and pos_settings[0] or None |
| 171 | |
| 172 | def apply_pos_settings(pos_settings, opts): |
| 173 | out = {} |
| 174 | |
| 175 | for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"): |
| 176 | if not opts.get(fieldname): |
| 177 | out[fieldname] = pos_settings.get(fieldname) |
| 178 | |
| 179 | if out.get("warehouse"): |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 180 | out["actual_qty"] = get_available_qty(opts.item_code, out.get("warehouse")).get("actual_qty") |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 181 | |
| 182 | return out |