Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
| 6 | from frappe import _, throw |
| 7 | from frappe.utils import flt, cint, add_days |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 8 | import json |
| 9 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 10 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 11 | def get_item_details(args): |
| 12 | """ |
| 13 | args = { |
| 14 | "item_code": "", |
| 15 | "warehouse": None, |
| 16 | "customer": "", |
| 17 | "conversion_rate": 1.0, |
| 18 | "selling_price_list": None, |
| 19 | "price_list_currency": None, |
| 20 | "plc_conversion_rate": 1.0 |
| 21 | "doctype": "", |
| 22 | "docname": "", |
| 23 | "supplier": None, |
| 24 | "transaction_date": None, |
| 25 | "conversion_rate": 1.0, |
| 26 | "buying_price_list": None, |
| 27 | "is_subcontracted": "Yes" / "No", |
| 28 | "transaction_type": "selling" |
| 29 | } |
| 30 | """ |
| 31 | |
| 32 | if isinstance(args, basestring): |
| 33 | args = json.loads(args) |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 34 | args = frappe._dict(args) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 35 | |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 36 | if not args.get("transaction_type"): |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 37 | args.transaction_type = "buying" if args.get("supplier") else "selling" |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 38 | |
| 39 | if not args.get("price_list"): |
| 40 | args.price_list = args.get("selling_price_list") or args.get("buying_price_list") |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 41 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 42 | if args.barcode: |
| 43 | args.item_code = get_item_code(barcode=args.barcode) |
| 44 | elif not args.item_code and args.serial_no: |
| 45 | args.item_code = get_item_code(serial_no=args.serial_no) |
| 46 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 47 | item_bean = frappe.bean("Item", args.item_code) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 48 | item = item_bean.doc |
| 49 | |
| 50 | validate_item_details(args, item) |
| 51 | |
| 52 | out = get_basic_details(args, item_bean) |
| 53 | |
| 54 | get_party_item_code(args, item_bean, out) |
| 55 | |
| 56 | if out.get("warehouse"): |
| 57 | out.update(get_available_qty(args.item_code, out.warehouse)) |
| 58 | out.update(get_projected_qty(item.name, out.warehouse)) |
| 59 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 60 | get_price_list_rate(args, item_bean, out) |
| 61 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 62 | out.update(get_item_discount(out.item_group, args.customer)) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 63 | |
| 64 | if args.transaction_type == "selling" and cint(args.is_pos): |
| 65 | out.update(get_pos_settings_item_details(args.company, args)) |
| 66 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 67 | if args.get("doctype") in ("Sales Invoice", "Delivery Note"): |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 68 | if item_bean.doc.has_serial_no == "Yes" and not args.serial_no: |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 69 | out.serial_no = get_serial_nos_by_fifo(args, item_bean) |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 70 | |
| 71 | if args.transaction_date and item.lead_time_days: |
| 72 | out.schedule_date = out.lead_time_date = add_days(args.transaction_date, |
| 73 | item.lead_time_days) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 74 | |
| 75 | return out |
| 76 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 77 | def get_item_code(barcode=None, serial_no=None): |
| 78 | if barcode: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 79 | item_code = frappe.conn.get_value("Item", {"barcode": barcode}) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 80 | elif serial_no: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 81 | item_code = frappe.conn.get_value("Serial No", serial_no, "item_code") |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 82 | |
| 83 | if not item_code: |
| 84 | throw(_("No Item found with ") + _("Barcode") if barcode else _("Serial No") + |
| 85 | ": %s" % (barcode or serial_no)) |
| 86 | |
| 87 | return item_code |
| 88 | |
| 89 | def validate_item_details(args, item): |
| 90 | if not args.company: |
| 91 | throw(_("Please specify Company")) |
| 92 | |
| 93 | from erpnext.stock.doctype.item.item import validate_end_of_life |
| 94 | validate_end_of_life(item.name, item.end_of_life) |
| 95 | |
| 96 | if args.transaction_type == "selling": |
| 97 | # validate if sales item or service item |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 98 | if args.get("order_type") == "Maintenance": |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 99 | if item.is_service_item != "Yes": |
| 100 | throw(_("Item") + (" %s: " % item.name) + |
| 101 | _("not a service item.") + |
| 102 | _("Please select a service item or change the order type to Sales.")) |
| 103 | |
| 104 | elif item.is_sales_item != "Yes": |
| 105 | throw(_("Item") + (" %s: " % item.name) + _("not a sales item")) |
| 106 | |
| 107 | elif args.transaction_type == "buying": |
| 108 | # validate if purchase item or subcontracted item |
| 109 | if item.is_purchase_item != "Yes": |
| 110 | throw(_("Item") + (" %s: " % item.name) + _("not a purchase item")) |
| 111 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 112 | if args.get("is_subcontracted") == "Yes" and item.is_sub_contracted_item != "Yes": |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 113 | throw(_("Item") + (" %s: " % item.name) + |
| 114 | _("not a sub-contracted item.") + |
| 115 | _("Please select a sub-contracted item or do not sub-contract the transaction.")) |
| 116 | |
| 117 | def get_basic_details(args, item_bean): |
| 118 | item = item_bean.doc |
| 119 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 120 | from frappe.defaults import get_user_default_as_list |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 121 | user_default_warehouse_list = get_user_default_as_list('warehouse') |
| 122 | user_default_warehouse = user_default_warehouse_list[0] \ |
| 123 | if len(user_default_warehouse_list)==1 else "" |
Nabin Hait | 36028bf | 2014-02-12 19:09:28 +0530 | [diff] [blame] | 124 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 125 | out = frappe._dict({ |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 126 | "item_code": item.name, |
| 127 | "item_name": item.item_name, |
| 128 | "description": item.description_html or item.description, |
| 129 | "warehouse": user_default_warehouse or args.warehouse or item.default_warehouse, |
Nabin Hait | eba1bdb | 2014-02-12 16:04:17 +0530 | [diff] [blame] | 130 | "income_account": item.income_account or args.income_account \ |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 131 | or frappe.conn.get_value("Company", args.company, "default_income_account"), |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 132 | "expense_account": item.expense_account or args.expense_account \ |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 133 | or frappe.conn.get_value("Company", args.company, "default_expense_account"), |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 134 | "cost_center": item.selling_cost_center \ |
Nabin Hait | 36028bf | 2014-02-12 19:09:28 +0530 | [diff] [blame] | 135 | if args.transaction_type == "selling" else item.buying_cost_center, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 136 | "batch_no": None, |
| 137 | "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in |
| 138 | item_bean.doclist.get({"parentfield": "item_tax"})))), |
| 139 | "uom": item.stock_uom, |
| 140 | "min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "", |
| 141 | "conversion_factor": 1.0, |
| 142 | "qty": 1.0, |
| 143 | "price_list_rate": 0.0, |
| 144 | "base_price_list_rate": 0.0, |
| 145 | "rate": 0.0, |
| 146 | "base_rate": 0.0, |
| 147 | "amount": 0.0, |
| 148 | "base_amount": 0.0, |
| 149 | "discount_percentage": 0.0 |
| 150 | }) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 151 | |
| 152 | for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"): |
| 153 | out[fieldname] = item.fields.get(fieldname) |
| 154 | |
| 155 | return out |
| 156 | |
| 157 | def get_price_list_rate(args, item_bean, out): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 158 | meta = frappe.get_doctype(args.doctype) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 159 | |
| 160 | if meta.get_field("currency"): |
| 161 | validate_price_list(args) |
| 162 | validate_conversion_rate(args, meta) |
| 163 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 164 | price_list_rate = frappe.conn.get_value("Item Price", |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 165 | {"price_list": args.price_list, "item_code": args.item_code}, "price_list_rate") |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 166 | |
| 167 | if not price_list_rate: return {} |
| 168 | |
| 169 | out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \ |
| 170 | / flt(args.conversion_rate) |
| 171 | |
| 172 | if not out.price_list_rate and args.transaction_type == "buying": |
| 173 | from erpnext.stock.doctype.item.item import get_last_purchase_details |
| 174 | out.update(get_last_purchase_details(item_bean.doc.name, |
| 175 | args.docname, args.conversion_rate)) |
| 176 | |
| 177 | def validate_price_list(args): |
| 178 | if args.get("price_list"): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 179 | if not frappe.conn.get_value("Price List", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 180 | {"name": args.price_list, args.transaction_type: 1, "enabled": 1}): |
| 181 | throw(_("Price List is either disabled or for not ") + _(args.transaction_type)) |
| 182 | else: |
| 183 | throw(_("Price List not selected")) |
| 184 | |
| 185 | def validate_conversion_rate(args, meta): |
| 186 | from erpnext.setup.doctype.currency.currency import validate_conversion_rate |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 187 | from frappe.model.meta import get_field_precision |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 188 | |
| 189 | # validate currency conversion rate |
| 190 | validate_conversion_rate(args.currency, args.conversion_rate, |
| 191 | meta.get_label("conversion_rate"), args.company) |
| 192 | |
| 193 | args.conversion_rate = flt(args.conversion_rate, |
| 194 | get_field_precision(meta.get_field("conversion_rate"), |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 195 | frappe._dict({"fields": args}))) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 196 | |
| 197 | # validate price list currency conversion rate |
| 198 | if not args.get("price_list_currency"): |
| 199 | throw(_("Price List Currency not selected")) |
| 200 | else: |
| 201 | validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate, |
| 202 | meta.get_label("plc_conversion_rate"), args.company) |
| 203 | |
| 204 | args.plc_conversion_rate = flt(args.plc_conversion_rate, |
| 205 | get_field_precision(meta.get_field("plc_conversion_rate"), |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 206 | frappe._dict({"fields": args}))) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 207 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 208 | def get_item_discount(item_group, customer): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 209 | parent_item_groups = [x[0] for x in frappe.conn.sql("""SELECT parent.name |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 210 | FROM `tabItem Group` AS node, `tabItem Group` AS parent |
| 211 | WHERE parent.lft <= node.lft and parent.rgt >= node.rgt and node.name = %s |
| 212 | GROUP BY parent.name |
| 213 | ORDER BY parent.lft desc""", (item_group,))] |
| 214 | |
| 215 | discount = 0 |
| 216 | for d in parent_item_groups: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 217 | res = frappe.conn.sql("""select discount, name from `tabCustomer Discount` |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 218 | where parent = %s and item_group = %s""", (customer, d)) |
| 219 | if res: |
| 220 | discount = flt(res[0][0]) |
| 221 | break |
| 222 | |
| 223 | return {"discount_percentage": discount} |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 224 | |
| 225 | def get_party_item_code(args, item_bean, out): |
| 226 | if args.transaction_type == "selling": |
| 227 | customer_item_code = item_bean.doclist.get({"parentfield": "item_customer_details", |
| 228 | "customer_name": args.customer}) |
| 229 | out.customer_item_code = customer_item_code[0].ref_code if customer_item_code else None |
| 230 | else: |
| 231 | item_supplier = item_bean.doclist.get({"parentfield": "item_supplier_details", |
| 232 | "supplier": args.supplier}) |
| 233 | out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None |
| 234 | |
| 235 | |
| 236 | def get_pos_settings_item_details(company, args, pos_settings=None): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 237 | res = frappe._dict() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 238 | |
| 239 | if not pos_settings: |
| 240 | pos_settings = get_pos_settings(company) |
| 241 | |
| 242 | if pos_settings: |
| 243 | for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"): |
| 244 | if not args.get(fieldname): |
| 245 | res[fieldname] = pos_settings.get(fieldname) |
| 246 | |
| 247 | if res.get("warehouse"): |
| 248 | res.actual_qty = get_available_qty(args.item_code, |
| 249 | res.warehouse).get("actual_qty") |
| 250 | |
| 251 | return res |
| 252 | |
| 253 | def get_pos_settings(company): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 254 | pos_settings = frappe.conn.sql("""select * from `tabPOS Setting` where user = %s |
| 255 | and company = %s""", (frappe.session['user'], company), as_dict=1) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 256 | |
| 257 | if not pos_settings: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 258 | pos_settings = frappe.conn.sql("""select * from `tabPOS Setting` |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 259 | where ifnull(user,'') = '' and company = %s""", company, as_dict=1) |
| 260 | |
| 261 | return pos_settings and pos_settings[0] or None |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 262 | |
| 263 | def get_serial_nos_by_fifo(args, item_bean): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 264 | return "\n".join(frappe.conn.sql_list("""select name from `tabSerial No` |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 265 | where item_code=%(item_code)s and warehouse=%(warehouse)s and status='Available' |
| 266 | order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", { |
| 267 | "item_code": args.item_code, |
| 268 | "warehouse": args.warehouse, |
| 269 | "qty": cint(args.qty) |
| 270 | })) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 271 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 272 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 273 | def get_conversion_factor(item_code, uom): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 274 | return {"conversion_factor": frappe.conn.get_value("UOM Conversion Detail", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 275 | {"parent": item_code, "uom": uom}, "conversion_factor")} |
| 276 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 277 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 278 | def get_projected_qty(item_code, warehouse): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 279 | return {"projected_qty": frappe.conn.get_value("Bin", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 280 | {"item_code": item_code, "warehouse": warehouse}, "projected_qty")} |
| 281 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 282 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 283 | def get_available_qty(item_code, warehouse): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 284 | return frappe.conn.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 285 | ["projected_qty", "actual_qty"], as_dict=True) or {} |