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 |
Nabin Hait | 9daca10 | 2014-06-20 15:59:49 +0530 | [diff] [blame^] | 9 | from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item |
Nabin Hait | 98c2a05 | 2014-05-05 18:41:41 +0530 | [diff] [blame] | 10 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 11 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 12 | def get_item_details(args): |
| 13 | """ |
| 14 | args = { |
| 15 | "item_code": "", |
| 16 | "warehouse": None, |
| 17 | "customer": "", |
| 18 | "conversion_rate": 1.0, |
| 19 | "selling_price_list": None, |
| 20 | "price_list_currency": None, |
Nabin Hait | 615d342 | 2014-02-25 19:01:20 +0530 | [diff] [blame] | 21 | "plc_conversion_rate": 1.0, |
Nabin Hait | 9daca10 | 2014-06-20 15:59:49 +0530 | [diff] [blame^] | 22 | "parenttype": "", |
| 23 | "parent": "", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 24 | "supplier": None, |
| 25 | "transaction_date": None, |
| 26 | "conversion_rate": 1.0, |
| 27 | "buying_price_list": None, |
| 28 | "is_subcontracted": "Yes" / "No", |
Nabin Hait | 9daca10 | 2014-06-20 15:59:49 +0530 | [diff] [blame^] | 29 | "transaction_type": "selling", |
| 30 | "ignore_pricing_rule": 0/1 |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 31 | } |
| 32 | """ |
| 33 | |
| 34 | if isinstance(args, basestring): |
| 35 | args = json.loads(args) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 36 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 37 | args = frappe._dict(args) |
Nabin Hait | 0f23106 | 2014-02-20 11:27:47 +0530 | [diff] [blame] | 38 | |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 39 | if not args.get("transaction_type"): |
Nabin Hait | 9daca10 | 2014-06-20 15:59:49 +0530 | [diff] [blame^] | 40 | if args.get("parenttype")=="Material Request" or \ |
| 41 | frappe.get_meta(args.get("parenttype")).get_field("supplier"): |
Anand Doshi | 68d0b7c | 2014-04-22 19:30:49 +0530 | [diff] [blame] | 42 | args.transaction_type = "buying" |
| 43 | else: |
| 44 | args.transaction_type = "selling" |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 45 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 46 | if not args.get("price_list"): |
| 47 | args.price_list = args.get("selling_price_list") or args.get("buying_price_list") |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 48 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 49 | if args.barcode: |
| 50 | args.item_code = get_item_code(barcode=args.barcode) |
| 51 | elif not args.item_code and args.serial_no: |
| 52 | args.item_code = get_item_code(serial_no=args.serial_no) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 53 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 54 | item_doc = frappe.get_doc("Item", args.item_code) |
| 55 | item = item_doc |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 56 | |
| 57 | validate_item_details(args, item) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 58 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 59 | out = get_basic_details(args, item_doc) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 60 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 61 | get_party_item_code(args, item_doc, out) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 62 | |
| 63 | if out.get("warehouse"): |
| 64 | out.update(get_available_qty(args.item_code, out.warehouse)) |
| 65 | out.update(get_projected_qty(item.name, out.warehouse)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 66 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 67 | get_price_list_rate(args, item_doc, out) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 68 | |
| 69 | if args.transaction_type == "selling" and cint(args.is_pos): |
| 70 | out.update(get_pos_settings_item_details(args.company, args)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 71 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 72 | # update args with out, if key or value not exists |
| 73 | for key, value in out.iteritems(): |
| 74 | if args.get(key) is None: |
| 75 | args[key] = value |
| 76 | |
Nabin Hait | 9daca10 | 2014-06-20 15:59:49 +0530 | [diff] [blame^] | 77 | out.update(get_pricing_rule_for_item(args)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 78 | |
Nabin Hait | 9daca10 | 2014-06-20 15:59:49 +0530 | [diff] [blame^] | 79 | if args.get("parenttype") in ("Sales Invoice", "Delivery Note"): |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 80 | if item_doc.has_serial_no == "Yes" and not args.serial_no: |
| 81 | out.serial_no = get_serial_nos_by_fifo(args, item_doc) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 82 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 83 | if args.transaction_date and item.lead_time_days: |
| 84 | out.schedule_date = out.lead_time_date = add_days(args.transaction_date, |
| 85 | item.lead_time_days) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 86 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 87 | return out |
| 88 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 89 | def get_item_code(barcode=None, serial_no=None): |
| 90 | if barcode: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 91 | item_code = frappe.db.get_value("Item", {"barcode": barcode}) |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 92 | if not item_code: |
| 93 | frappe.throw(_("No Item with Barcode {0}").format(barcode)) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 94 | elif serial_no: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 95 | item_code = frappe.db.get_value("Serial No", serial_no, "item_code") |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 96 | if not item_code: |
| 97 | frappe.throw(_("No Item with Serial No {0}").format(serial_no)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 98 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 99 | return item_code |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 100 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 101 | def validate_item_details(args, item): |
| 102 | if not args.company: |
| 103 | throw(_("Please specify Company")) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 104 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 105 | from erpnext.stock.doctype.item.item import validate_end_of_life |
| 106 | validate_end_of_life(item.name, item.end_of_life) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 107 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 108 | if args.transaction_type == "selling": |
| 109 | # validate if sales item or service item |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 110 | if args.get("order_type") == "Maintenance": |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 111 | if item.is_service_item != "Yes": |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 112 | throw(_("Item {0} must be a Service Item.").format(item.name)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 113 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 114 | elif item.is_sales_item != "Yes": |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 115 | throw(_("Item {0} must be a Sales Item").format(item.name)) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 116 | |
Nabin Hait | 9daca10 | 2014-06-20 15:59:49 +0530 | [diff] [blame^] | 117 | elif args.transaction_type == "buying" and args.parenttype != "Material Request": |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 118 | # validate if purchase item or subcontracted item |
| 119 | if item.is_purchase_item != "Yes": |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 120 | throw(_("Item {0} must be a Purchase Item").format(item.name)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 121 | |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 122 | 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] | 123 | throw(_("Item {0} must be a Sub-contracted Item").format(item.name)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 124 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 125 | def get_basic_details(args, item_doc): |
| 126 | item = item_doc |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 127 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 128 | from frappe.defaults import get_user_default_as_list |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 129 | user_default_warehouse_list = get_user_default_as_list('warehouse') |
| 130 | user_default_warehouse = user_default_warehouse_list[0] \ |
| 131 | if len(user_default_warehouse_list)==1 else "" |
Nabin Hait | 36028bf | 2014-02-12 19:09:28 +0530 | [diff] [blame] | 132 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 133 | out = frappe._dict({ |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 134 | "item_code": item.name, |
| 135 | "item_name": item.item_name, |
| 136 | "description": item.description_html or item.description, |
| 137 | "warehouse": user_default_warehouse or args.warehouse or item.default_warehouse, |
Nabin Hait | eba1bdb | 2014-02-12 16:04:17 +0530 | [diff] [blame] | 138 | "income_account": item.income_account or args.income_account \ |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 139 | or frappe.db.get_value("Company", args.company, "default_income_account"), |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 140 | "expense_account": item.expense_account or args.expense_account \ |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 141 | or frappe.db.get_value("Company", args.company, "default_expense_account"), |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 142 | "cost_center": item.selling_cost_center \ |
Nabin Hait | 36028bf | 2014-02-12 19:09:28 +0530 | [diff] [blame] | 143 | if args.transaction_type == "selling" else item.buying_cost_center, |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 144 | "batch_no": None, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 145 | "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 146 | item_doc.get("item_tax")))), |
Nabin Hait | 139dc7b | 2014-02-12 14:53:18 +0530 | [diff] [blame] | 147 | "uom": item.stock_uom, |
Nabin Hait | 9daca10 | 2014-06-20 15:59:49 +0530 | [diff] [blame^] | 148 | "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] | 149 | "conversion_factor": 1.0, |
| 150 | "qty": 1.0, |
| 151 | "price_list_rate": 0.0, |
| 152 | "base_price_list_rate": 0.0, |
| 153 | "rate": 0.0, |
| 154 | "base_rate": 0.0, |
| 155 | "amount": 0.0, |
| 156 | "base_amount": 0.0, |
| 157 | "discount_percentage": 0.0 |
| 158 | }) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 159 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 160 | for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 161 | out[fieldname] = item.get(fieldname) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 162 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 163 | return out |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 164 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 165 | def get_price_list_rate(args, item_doc, out): |
Nabin Hait | 9daca10 | 2014-06-20 15:59:49 +0530 | [diff] [blame^] | 166 | meta = frappe.get_meta(args.parenttype) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 167 | |
| 168 | if meta.get_field("currency"): |
| 169 | validate_price_list(args) |
| 170 | validate_conversion_rate(args, meta) |
| 171 | |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 172 | price_list_rate = frappe.db.get_value("Item Price", |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 173 | {"price_list": args.price_list, "item_code": args.item_code}, "price_list_rate") |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 174 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 175 | if not price_list_rate: return {} |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 176 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 177 | out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \ |
| 178 | / flt(args.conversion_rate) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 179 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 180 | if not out.price_list_rate and args.transaction_type == "buying": |
| 181 | from erpnext.stock.doctype.item.item import get_last_purchase_details |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 182 | out.update(get_last_purchase_details(item_doc.name, |
Nabin Hait | 9daca10 | 2014-06-20 15:59:49 +0530 | [diff] [blame^] | 183 | args.parent, args.conversion_rate)) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 184 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 185 | def validate_price_list(args): |
| 186 | if args.get("price_list"): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 187 | if not frappe.db.get_value("Price List", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 188 | {"name": args.price_list, args.transaction_type: 1, "enabled": 1}): |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 189 | throw(_("Price List {0} is disabled").format(args.price_list)) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 190 | else: |
| 191 | throw(_("Price List not selected")) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 192 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 193 | def validate_conversion_rate(args, meta): |
| 194 | from erpnext.setup.doctype.currency.currency import validate_conversion_rate |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 195 | from frappe.model.meta import get_field_precision |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 196 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 197 | # validate currency conversion rate |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 198 | validate_conversion_rate(args.currency, args.conversion_rate, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 199 | meta.get_label("conversion_rate"), args.company) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 200 | |
| 201 | args.conversion_rate = flt(args.conversion_rate, |
| 202 | get_field_precision(meta.get_field("conversion_rate"), |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 203 | frappe._dict({"fields": args}))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 204 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 205 | # validate price list currency conversion rate |
| 206 | if not args.get("price_list_currency"): |
| 207 | throw(_("Price List Currency not selected")) |
| 208 | else: |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 209 | validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 210 | meta.get_label("plc_conversion_rate"), args.company) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 211 | |
| 212 | args.plc_conversion_rate = flt(args.plc_conversion_rate, |
| 213 | get_field_precision(meta.get_field("plc_conversion_rate"), |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 214 | frappe._dict({"fields": args}))) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 215 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 216 | def get_party_item_code(args, item_doc, out): |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 217 | if args.transaction_type == "selling": |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 218 | customer_item_code = item_doc.get("item_customer_details", {"customer_name": args.customer}) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 219 | out.customer_item_code = customer_item_code[0].ref_code if customer_item_code else None |
| 220 | else: |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 221 | item_supplier = item_doc.get("item_supplier_details", {"supplier": args.supplier}) |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 222 | 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] | 223 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 224 | |
| 225 | def get_pos_settings_item_details(company, args, pos_settings=None): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 226 | res = frappe._dict() |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 227 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 228 | if not pos_settings: |
| 229 | pos_settings = get_pos_settings(company) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 230 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 231 | if pos_settings: |
| 232 | for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"): |
Nabin Hait | c5e813f | 2014-06-18 19:51:38 +0530 | [diff] [blame] | 233 | if not args.get(fieldname) and pos_settings.get(fieldname): |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 234 | res[fieldname] = pos_settings.get(fieldname) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 235 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 236 | if res.get("warehouse"): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 237 | res.actual_qty = get_available_qty(args.item_code, |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 238 | res.warehouse).get("actual_qty") |
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 | return res |
| 241 | |
| 242 | def get_pos_settings(company): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 243 | pos_settings = frappe.db.sql("""select * from `tabPOS Setting` where user = %s |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 244 | and company = %s""", (frappe.session['user'], company), as_dict=1) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 245 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 246 | if not pos_settings: |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 247 | pos_settings = frappe.db.sql("""select * from `tabPOS Setting` |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 248 | where ifnull(user,'') = '' and company = %s""", company, as_dict=1) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 249 | |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 250 | return pos_settings and pos_settings[0] or None |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 251 | |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 252 | |
Rushabh Mehta | f14b809 | 2014-04-03 14:30:42 +0530 | [diff] [blame] | 253 | def get_serial_nos_by_fifo(args, item_doc): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 254 | return "\n".join(frappe.db.sql_list("""select name from `tabSerial No` |
| 255 | 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] | 256 | order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", { |
| 257 | "item_code": args.item_code, |
| 258 | "warehouse": args.warehouse, |
| 259 | "qty": cint(args.qty) |
| 260 | })) |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 261 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 262 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 263 | def get_conversion_factor(item_code, uom): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 264 | return {"conversion_factor": frappe.db.get_value("UOM Conversion Detail", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 265 | {"parent": item_code, "uom": uom}, "conversion_factor")} |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +0530 | [diff] [blame] | 266 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 267 | @frappe.whitelist() |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 268 | def get_projected_qty(item_code, warehouse): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 269 | return {"projected_qty": frappe.db.get_value("Bin", |
Nabin Hait | 436f526 | 2014-02-10 14:47:54 +0530 | [diff] [blame] | 270 | {"item_code": item_code, "warehouse": warehouse}, "projected_qty")} |
Nabin Hait | 0822215 | 2014-02-28 11:30:47 +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_available_qty(item_code, warehouse): |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 274 | return frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, |
| 275 | ["projected_qty", "actual_qty"], as_dict=True) or {} |