Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 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 |
Nabin Hait | 2244ac4 | 2015-01-12 17:35:14 +0530 | [diff] [blame] | 7 | from frappe.utils import flt, cint, add_days, cstr |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 8 | import json |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 9 | from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 10 | from erpnext.setup.utils import get_exchange_rate |
Rushabh Mehta | 66e08e3 | 2014-09-29 12:17:03 +0530 | [diff] [blame] | 11 | from frappe.model.meta import get_field_precision |
Nabin Hait | 98c2a05 | 2014-05-05 18:41:41 +0530 | [diff] [blame] | 12 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 13 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 14 | def get_item_details(args): |
| 15 | """ |
| 16 | args = { |
| 17 | "item_code": "", |
| 18 | "warehouse": None, |
| 19 | "customer": "", |
| 20 | "conversion_rate": 1.0, |
| 21 | "selling_price_list": None, |
| 22 | "price_list_currency": None, |
Nabin Hait | 615d342 | 2014-02-25 19:01:20 +0530 | [diff] [blame] | 23 | "plc_conversion_rate": 1.0, |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 24 | "parenttype": "", |
| 25 | "parent": "", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 26 | "supplier": None, |
| 27 | "transaction_date": None, |
| 28 | "conversion_rate": 1.0, |
| 29 | "buying_price_list": None, |
| 30 | "is_subcontracted": "Yes" / "No", |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 31 | "transaction_type": "selling", |
| 32 | "ignore_pricing_rule": 0/1 |
Anand Doshi | 43f087c | 2014-08-26 14:25:53 +0530 | [diff] [blame] | 33 | "project_name": "", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 34 | } |
| 35 | """ |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 36 | args = process_args(args) |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 37 | item_doc = frappe.get_doc("Item", args.item_code) |
| 38 | item = item_doc |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 39 | |
Neil Trini Lasrado | caba531 | 2014-08-20 17:20:29 +0530 | [diff] [blame] | 40 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 41 | validate_item_details(args, item) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 42 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 43 | out = get_basic_details(args, item) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 44 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 45 | get_party_item_code(args, item_doc, out) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 46 | |
| 47 | if out.get("warehouse"): |
| 48 | out.update(get_available_qty(args.item_code, out.warehouse)) |
| 49 | out.update(get_projected_qty(item.name, out.warehouse)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 50 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 51 | get_price_list_rate(args, item_doc, out) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 52 | |
| 53 | if args.transaction_type == "selling" and cint(args.is_pos): |
| 54 | out.update(get_pos_settings_item_details(args.company, args)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 55 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 56 | # update args with out, if key or value not exists |
| 57 | for key, value in out.iteritems(): |
| 58 | if args.get(key) is None: |
| 59 | args[key] = value |
| 60 | |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 61 | out.update(get_pricing_rule_for_item(args)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 62 | |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 63 | if args.get("parenttype") in ("Sales Invoice", "Delivery Note"): |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 64 | if item_doc.has_serial_no == "Yes" and not args.serial_no: |
| 65 | out.serial_no = get_serial_nos_by_fifo(args, item_doc) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 66 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 67 | if args.transaction_date and item.lead_time_days: |
| 68 | out.schedule_date = out.lead_time_date = add_days(args.transaction_date, |
| 69 | item.lead_time_days) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 70 | |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 71 | if args.get("is_subcontracted") == "Yes": |
| 72 | out.bom = get_default_bom(args.item_code) |
| 73 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 74 | return out |
| 75 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 76 | def process_args(args): |
| 77 | if isinstance(args, basestring): |
| 78 | args = json.loads(args) |
| 79 | |
| 80 | args = frappe._dict(args) |
| 81 | |
| 82 | if not args.get("transaction_type"): |
| 83 | if args.get("parenttype")=="Material Request" or \ |
| 84 | frappe.get_meta(args.get("parenttype")).get_field("supplier"): |
| 85 | args.transaction_type = "buying" |
| 86 | else: |
| 87 | args.transaction_type = "selling" |
| 88 | |
| 89 | if not args.get("price_list"): |
| 90 | args.price_list = args.get("selling_price_list") or args.get("buying_price_list") |
| 91 | |
| 92 | if args.barcode: |
| 93 | args.item_code = get_item_code(barcode=args.barcode) |
| 94 | elif not args.item_code and args.serial_no: |
| 95 | args.item_code = get_item_code(serial_no=args.serial_no) |
| 96 | |
| 97 | return args |
| 98 | |
Neil Trini Lasrado | 4abda67 | 2015-02-25 17:34:27 +0530 | [diff] [blame] | 99 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 100 | def get_item_code(barcode=None, serial_no=None): |
| 101 | if barcode: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 102 | item_code = frappe.db.get_value("Item", {"barcode": barcode}) |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 103 | if not item_code: |
| 104 | frappe.throw(_("No Item with Barcode {0}").format(barcode)) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 105 | elif serial_no: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 106 | item_code = frappe.db.get_value("Serial No", serial_no, "item_code") |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 107 | if not item_code: |
| 108 | frappe.throw(_("No Item with Serial No {0}").format(serial_no)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 109 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 110 | return item_code |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 111 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 112 | def validate_item_details(args, item): |
| 113 | if not args.company: |
| 114 | throw(_("Please specify Company")) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 115 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 116 | from erpnext.stock.doctype.item.item import validate_end_of_life |
| 117 | validate_end_of_life(item.name, item.end_of_life) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 118 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 119 | if args.transaction_type == "selling": |
| 120 | # validate if sales item or service item |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 121 | if args.get("order_type") == "Maintenance": |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 122 | if item.is_service_item != "Yes": |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 123 | throw(_("Item {0} must be a Service Item.").format(item.name)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 124 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 125 | elif item.is_sales_item != "Yes": |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 126 | throw(_("Item {0} must be a Sales Item").format(item.name)) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 127 | |
Rushabh Mehta | a1da88a | 2015-02-23 20:18:38 +0530 | [diff] [blame] | 128 | if cint(item.has_variants): |
| 129 | throw(_("Item {0} is a template, please select one of its variants").format(item.name)) |
| 130 | |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 131 | elif args.transaction_type == "buying" and args.parenttype != "Material Request": |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 132 | # validate if purchase item or subcontracted item |
| 133 | if item.is_purchase_item != "Yes": |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 134 | throw(_("Item {0} must be a Purchase Item").format(item.name)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 135 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 136 | if args.get("is_subcontracted") == "Yes" and item.is_sub_contracted_item != "Yes": |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 137 | throw(_("Item {0} must be a Sub-contracted Item").format(item.name)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 138 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 139 | def get_basic_details(args, item): |
| 140 | if not item: |
| 141 | item = frappe.get_doc("Item", args.get("item_code")) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 142 | |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 143 | if item.variant_of: |
| 144 | item.update_template_tables() |
| 145 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 146 | from frappe.defaults import get_user_default_as_list |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 147 | user_default_warehouse_list = get_user_default_as_list('warehouse') |
| 148 | user_default_warehouse = user_default_warehouse_list[0] \ |
| 149 | if len(user_default_warehouse_list)==1 else "" |
Nabin Hait | 36028bf | 2014-02-12 19:09:28 +0530 | [diff] [blame] | 150 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 151 | out = frappe._dict({ |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 152 | "item_code": item.name, |
| 153 | "item_name": item.item_name, |
Neil Trini Lasrado | c48f06c | 2015-02-10 16:49:16 +0530 | [diff] [blame] | 154 | "description": cstr(item.description).strip(), |
| 155 | "image": cstr(item.image).strip(), |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 156 | "warehouse": user_default_warehouse or args.warehouse or item.default_warehouse, |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 157 | "income_account": get_default_income_account(args, item), |
| 158 | "expense_account": get_default_expense_account(args, item), |
| 159 | "cost_center": get_default_cost_center(args, item), |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 160 | "batch_no": None, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 161 | "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 162 | item.get("taxes")))), |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 163 | "uom": item.stock_uom, |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 164 | "min_order_qty": flt(item.min_order_qty) if args.parenttype == "Material Request" else "", |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 165 | "conversion_factor": 1.0, |
Anand Doshi | 9b955fe | 2015-01-05 16:19:12 +0530 | [diff] [blame] | 166 | "qty": args.qty or 0.0, |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 167 | "stock_qty": 0.0, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 168 | "price_list_rate": 0.0, |
| 169 | "base_price_list_rate": 0.0, |
| 170 | "rate": 0.0, |
| 171 | "base_rate": 0.0, |
| 172 | "amount": 0.0, |
| 173 | "base_amount": 0.0, |
Nabin Hait | 82e3e25 | 2015-02-23 16:58:30 +0530 | [diff] [blame] | 174 | "net_rate": 0.0, |
| 175 | "net_amount": 0.0, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 176 | "discount_percentage": 0.0 |
| 177 | }) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 178 | |
Nabin Hait | b09ed41 | 2015-02-17 10:33:58 +0530 | [diff] [blame] | 179 | # if default specified in item is for another company, fetch from company |
| 180 | for d in [["Account", "income_account", "default_income_account"], ["Account", "expense_account", "default_expense_account"], |
| 181 | ["Cost Center", "cost_center", "cost_center"], ["Warehouse", "warehouse", ""]]: |
Nabin Hait | 98a8fae | 2015-02-26 11:37:07 +0530 | [diff] [blame] | 182 | company = frappe.db.get_value(d[0], out.get(d[1]), "company") |
| 183 | if not out[d[1]] or (company and args.company != company): |
Nabin Hait | b09ed41 | 2015-02-17 10:33:58 +0530 | [diff] [blame] | 184 | out[d[1]] = frappe.db.get_value("Company", args.company, d[2]) if d[2] else None |
| 185 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 186 | for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 187 | out[fieldname] = item.get(fieldname) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 188 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 189 | return out |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 190 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 191 | def get_default_income_account(args, item): |
| 192 | return (item.income_account |
| 193 | or args.income_account |
Anand Doshi | d35354c | 2015-02-24 18:12:17 +0530 | [diff] [blame] | 194 | or frappe.db.get_value("Item Group", item.item_group, "default_income_account")) |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 195 | |
| 196 | def get_default_expense_account(args, item): |
| 197 | return (item.expense_account |
| 198 | or args.expense_account |
Anand Doshi | d35354c | 2015-02-24 18:12:17 +0530 | [diff] [blame] | 199 | or frappe.db.get_value("Item Group", item.item_group, "default_expense_account")) |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 200 | |
| 201 | def get_default_cost_center(args, item): |
Neil Trini Lasrado | 0683fc33e | 2014-10-22 14:48:40 +0530 | [diff] [blame] | 202 | return (frappe.db.get_value("Project", args.get("project_name"), "cost_center") |
| 203 | or (item.selling_cost_center if args.get("transaction_type") == "selling" else item.buying_cost_center) |
Anand Doshi | d35354c | 2015-02-24 18:12:17 +0530 | [diff] [blame] | 204 | or frappe.db.get_value("Item Group", item.item_group, "default_cost_center")) |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 205 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 206 | def get_price_list_rate(args, item_doc, out): |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 207 | meta = frappe.get_meta(args.parenttype) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 208 | |
| 209 | if meta.get_field("currency"): |
| 210 | validate_price_list(args) |
| 211 | validate_conversion_rate(args, meta) |
| 212 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 213 | price_list_rate = get_price_list_rate_for(args, item_doc.name) |
| 214 | if not price_list_rate and item_doc.variant_of: |
| 215 | price_list_rate = get_price_list_rate_for(args, item_doc.variant_of) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 216 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 217 | if not price_list_rate: |
| 218 | return {} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 219 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 220 | out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \ |
| 221 | / flt(args.conversion_rate) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 222 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 223 | if not out.price_list_rate and args.transaction_type == "buying": |
| 224 | from erpnext.stock.doctype.item.item import get_last_purchase_details |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 225 | out.update(get_last_purchase_details(item_doc.name, |
Nabin Hait | 444f956 | 2014-06-20 15:59:49 +0530 | [diff] [blame] | 226 | args.parent, args.conversion_rate)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 227 | |
Rushabh Mehta | 1a76374 | 2014-10-03 16:36:43 +0530 | [diff] [blame] | 228 | def get_price_list_rate_for(args, item_code): |
| 229 | return frappe.db.get_value("Item Price", |
| 230 | {"price_list": args.price_list, "item_code": item_code}, "price_list_rate") |
| 231 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 232 | def validate_price_list(args): |
| 233 | if args.get("price_list"): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 234 | if not frappe.db.get_value("Price List", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 235 | {"name": args.price_list, args.transaction_type: 1, "enabled": 1}): |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 236 | throw(_("Price List {0} is disabled").format(args.price_list)) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 237 | else: |
| 238 | throw(_("Price List not selected")) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 239 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 240 | def validate_conversion_rate(args, meta): |
Rushabh Mehta | 66e08e3 | 2014-09-29 12:17:03 +0530 | [diff] [blame] | 241 | from erpnext.controllers.accounts_controller import validate_conversion_rate |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 242 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 243 | # validate currency conversion rate |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 244 | validate_conversion_rate(args.currency, args.conversion_rate, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 245 | meta.get_label("conversion_rate"), args.company) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 246 | |
| 247 | args.conversion_rate = flt(args.conversion_rate, |
| 248 | get_field_precision(meta.get_field("conversion_rate"), |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 249 | frappe._dict({"fields": args}))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 250 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 251 | # validate price list currency conversion rate |
| 252 | if not args.get("price_list_currency"): |
| 253 | throw(_("Price List Currency not selected")) |
| 254 | else: |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 255 | validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 256 | meta.get_label("plc_conversion_rate"), args.company) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 257 | |
| 258 | args.plc_conversion_rate = flt(args.plc_conversion_rate, |
| 259 | get_field_precision(meta.get_field("plc_conversion_rate"), |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 260 | frappe._dict({"fields": args}))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 261 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 262 | def get_party_item_code(args, item_doc, out): |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 263 | if args.transaction_type == "selling": |
Nabin Hait | 79f091e | 2014-12-26 11:41:15 +0530 | [diff] [blame] | 264 | customer_item_code = item_doc.get("customer_items", {"customer_name": args.customer}) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 265 | out.customer_item_code = customer_item_code[0].ref_code if customer_item_code else None |
| 266 | else: |
Nabin Hait | 79f091e | 2014-12-26 11:41:15 +0530 | [diff] [blame] | 267 | item_supplier = item_doc.get("supplier_items", {"supplier": args.supplier}) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 268 | out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 269 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 270 | def get_pos_settings_item_details(company, args, pos_settings=None): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 271 | res = frappe._dict() |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 272 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 273 | if not pos_settings: |
| 274 | pos_settings = get_pos_settings(company) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 275 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 276 | if pos_settings: |
| 277 | for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"): |
Nabin Hait | faf176d | 2014-06-18 19:51:38 +0530 | [diff] [blame] | 278 | if not args.get(fieldname) and pos_settings.get(fieldname): |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 279 | res[fieldname] = pos_settings.get(fieldname) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 280 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 281 | if res.get("warehouse"): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 282 | res.actual_qty = get_available_qty(args.item_code, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 283 | res.warehouse).get("actual_qty") |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 284 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 285 | return res |
| 286 | |
| 287 | def get_pos_settings(company): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 288 | pos_settings = frappe.db.sql("""select * from `tabPOS Setting` where user = %s |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 289 | and company = %s""", (frappe.session['user'], company), as_dict=1) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 290 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 291 | if not pos_settings: |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 292 | pos_settings = frappe.db.sql("""select * from `tabPOS Setting` |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 293 | where ifnull(user,'') = '' and company = %s""", company, as_dict=1) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 294 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 295 | return pos_settings and pos_settings[0] or None |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 296 | |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 297 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 298 | def get_serial_nos_by_fifo(args, item_doc): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 299 | return "\n".join(frappe.db.sql_list("""select name from `tabSerial No` |
| 300 | where item_code=%(item_code)s and warehouse=%(warehouse)s and status='Available' |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 301 | order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", { |
| 302 | "item_code": args.item_code, |
| 303 | "warehouse": args.warehouse, |
| 304 | "qty": cint(args.qty) |
| 305 | })) |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 306 | |
Sambhaji Kolate | 98dbccd | 2015-03-10 15:04:28 +0530 | [diff] [blame] | 307 | def get_actual_batch_qty(batch_no,warehouse,item_code): |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 308 | actual_batch_qty = 0 |
| 309 | if batch_no: |
| 310 | actual_batch_qty = flt(frappe.db.sql("""select sum(actual_qty) |
| 311 | from `tabStock Ledger Entry` |
| 312 | where warehouse=%s and item_code=%s and batch_no=%s""", |
| 313 | (warehouse, item_code, batch_no))[0][0]) |
| 314 | return actual_batch_qty |
Sambhaji Kolate | 98dbccd | 2015-03-10 15:04:28 +0530 | [diff] [blame] | 315 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 316 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 317 | def get_conversion_factor(item_code, uom): |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 318 | variant_of = frappe.db.get_value("Item", item_code, "variant_of") |
| 319 | filters = {"parent": item_code, "uom": uom} |
| 320 | if variant_of: |
| 321 | filters = {"parent": ("in", (item_code, variant_of))} |
| 322 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 323 | return {"conversion_factor": frappe.db.get_value("UOM Conversion Detail", |
Rushabh Mehta | 724f9e5 | 2014-10-07 15:29:58 +0530 | [diff] [blame] | 324 | filters, "conversion_factor")} |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 325 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 326 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 327 | def get_projected_qty(item_code, warehouse): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 328 | return {"projected_qty": frappe.db.get_value("Bin", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 329 | {"item_code": item_code, "warehouse": warehouse}, "projected_qty")} |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 330 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 331 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 332 | def get_available_qty(item_code, warehouse): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 333 | return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, |
| 334 | ["projected_qty", "actual_qty"], as_dict=True) or {} |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 335 | |
| 336 | @frappe.whitelist() |
Sambhaji Kolate | 98dbccd | 2015-03-10 15:04:28 +0530 | [diff] [blame] | 337 | def get_batch_qty(batch_no,warehouse,item_code): |
| 338 | actual_batch_qty = get_actual_batch_qty(batch_no,warehouse,item_code) |
| 339 | if batch_no: |
| 340 | return {'actual_batch_qty': actual_batch_qty} |
| 341 | |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 342 | @frappe.whitelist() |
| 343 | def apply_price_list(args): |
| 344 | """ |
| 345 | args = { |
| 346 | "item_list": [{"doctype": "", "name": "", "item_code": "", "brand": "", "item_group": ""}, ...], |
| 347 | "conversion_rate": 1.0, |
| 348 | "selling_price_list": None, |
| 349 | "price_list_currency": None, |
| 350 | "plc_conversion_rate": 1.0, |
| 351 | "parenttype": "", |
| 352 | "parent": "", |
| 353 | "supplier": None, |
| 354 | "transaction_date": None, |
| 355 | "conversion_rate": 1.0, |
| 356 | "buying_price_list": None, |
| 357 | "transaction_type": "selling", |
| 358 | "ignore_pricing_rule": 0/1 |
| 359 | } |
| 360 | """ |
| 361 | args = process_args(args) |
| 362 | |
| 363 | parent = get_price_list_currency_and_exchange_rate(args) |
| 364 | children = [] |
| 365 | |
| 366 | if "item_list" in args: |
| 367 | item_list = args.get("item_list") |
| 368 | del args["item_list"] |
| 369 | |
| 370 | args.update(parent) |
| 371 | |
| 372 | for item in item_list: |
| 373 | args_copy = frappe._dict(args.copy()) |
| 374 | args_copy.update(item) |
| 375 | item_details = apply_price_list_on_item(args_copy) |
| 376 | children.append(item_details) |
| 377 | |
| 378 | return { |
| 379 | "parent": parent, |
| 380 | "children": children |
| 381 | } |
| 382 | |
| 383 | def apply_price_list_on_item(args): |
| 384 | item_details = frappe._dict() |
| 385 | item_doc = frappe.get_doc("Item", args.item_code) |
| 386 | get_price_list_rate(args, item_doc, item_details) |
nabinhait | 89b5e3e | 2014-07-08 16:00:25 +0530 | [diff] [blame] | 387 | item_details.discount_percentage = 0.0 |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 388 | item_details.update(get_pricing_rule_for_item(args)) |
| 389 | return item_details |
| 390 | |
| 391 | def get_price_list_currency(price_list): |
Neil Trini Lasrado | adb1b2c | 2015-03-18 11:40:39 +0530 | [diff] [blame] | 392 | if price_list: |
| 393 | result = frappe.db.get_value("Price List", {"name": price_list, |
| 394 | "enabled": 1}, ["name", "currency"], as_dict=True) |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 395 | |
Neil Trini Lasrado | adb1b2c | 2015-03-18 11:40:39 +0530 | [diff] [blame] | 396 | if not result: |
| 397 | throw(_("Price List {0} is disabled").format(price_list)) |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 398 | |
Neil Trini Lasrado | adb1b2c | 2015-03-18 11:40:39 +0530 | [diff] [blame] | 399 | return result.currency |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 400 | |
| 401 | def get_price_list_currency_and_exchange_rate(args): |
| 402 | price_list_currency = get_price_list_currency(args.price_list) |
| 403 | plc_conversion_rate = args.plc_conversion_rate |
| 404 | |
| 405 | if (not plc_conversion_rate) or (price_list_currency != args.price_list_currency): |
| 406 | plc_conversion_rate = get_exchange_rate(price_list_currency, args.currency) \ |
| 407 | or plc_conversion_rate |
| 408 | |
| 409 | return { |
| 410 | "price_list_currency": price_list_currency, |
| 411 | "plc_conversion_rate": plc_conversion_rate |
| 412 | } |
ankitjavalkarwork | 5edae0e | 2014-11-05 20:14:53 +0530 | [diff] [blame] | 413 | |
| 414 | @frappe.whitelist() |
| 415 | def get_default_bom(item_code=None): |
| 416 | if item_code: |
| 417 | bom = frappe.db.get_value("BOM", {"docstatus": 1, "is_default": 1, "is_active": 1, "item": item_code}) |
| 418 | if bom: |
| 419 | return bom |
| 420 | else: |
| 421 | frappe.throw(_("No default BOM exists for Item {0}").format(item_code)) |