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