Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | # License: GNU General Public License v3. See license.txt |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import webnotes |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 6 | from webnotes import msgprint, _ |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 7 | from webnotes.utils import getdate, flt, add_days |
| 8 | import json |
| 9 | |
| 10 | @webnotes.whitelist() |
| 11 | def get_item_details(args): |
| 12 | """ |
| 13 | args = { |
| 14 | "doctype": "", |
| 15 | "docname": "", |
| 16 | "item_code": "", |
| 17 | "warehouse": None, |
| 18 | "supplier": None, |
| 19 | "transaction_date": None, |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 20 | "conversion_rate": 1.0, |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 21 | "buying_price_list": None, |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 22 | "price_list_currency": None, |
| 23 | "plc_conversion_rate": 1.0, |
| 24 | "is_subcontracted": "Yes" / "No" |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 25 | } |
| 26 | """ |
| 27 | if isinstance(args, basestring): |
| 28 | args = json.loads(args) |
| 29 | |
| 30 | args = webnotes._dict(args) |
| 31 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 32 | item_bean = webnotes.bean("Item", args.item_code) |
| 33 | item = item_bean.doc |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 34 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 35 | _validate_item_details(args, item) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 36 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 37 | out = _get_basic_details(args, item_bean) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 38 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 39 | out.supplier_part_no = _get_supplier_part_no(args, item_bean) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 40 | |
Rushabh Mehta | 1bcc19e | 2013-07-01 11:46:07 +0530 | [diff] [blame] | 41 | if not out.warehouse: |
| 42 | out.warehouse = item_bean.doc.default_warehouse |
| 43 | |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 44 | if out.warehouse: |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 45 | out.projected_qty = get_projected_qty(item.name, out.warehouse) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 46 | |
| 47 | if args.transaction_date and item.lead_time_days: |
| 48 | out.schedule_date = out.lead_time_date = add_days(args.transaction_date, |
| 49 | item.lead_time_days) |
| 50 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 51 | meta = webnotes.get_doctype(args.doctype) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 52 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 53 | if meta.get_field("currency"): |
| 54 | out.purchase_ref_rate = out.discount_rate = out.purchase_rate = \ |
| 55 | out.import_ref_rate = out.import_rate = 0.0 |
| 56 | out.update(_get_price_list_rate(args, item_bean, meta)) |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 57 | |
| 58 | if args.doctype == "Material Request": |
| 59 | out.min_order_qty = flt(item.min_order_qty) |
| 60 | |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 61 | return out |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 62 | |
| 63 | def _get_basic_details(args, item_bean): |
| 64 | item = item_bean.doc |
| 65 | |
| 66 | out = webnotes._dict({ |
| 67 | "description": item.description_html or item.description, |
| 68 | "qty": 0.0, |
| 69 | "uom": item.stock_uom, |
| 70 | "conversion_factor": 1.0, |
| 71 | "warehouse": args.warehouse or item.default_warehouse, |
| 72 | "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in |
| 73 | item_bean.doclist.get({"parentfield": "item_tax"})))), |
| 74 | "batch_no": None, |
Rushabh Mehta | d9e7070 | 2013-07-08 18:01:46 +0530 | [diff] [blame] | 75 | "expense_head": item.purchase_account \ |
| 76 | or webnotes.conn.get_value("Company", args.company, "default_expense_account"), |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 77 | "cost_center": item.cost_center |
| 78 | }) |
| 79 | |
| 80 | for fieldname in ("item_name", "item_group", "brand", "stock_uom"): |
| 81 | out[fieldname] = item.fields.get(fieldname) |
| 82 | |
| 83 | return out |
| 84 | |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 85 | def _get_price_list_rate(args, item_bean, meta): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 86 | from utilities.transaction_base import validate_currency |
| 87 | item = item_bean.doc |
| 88 | out = webnotes._dict() |
| 89 | |
| 90 | # try fetching from price list |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 91 | if args.buying_price_list and args.price_list_currency: |
Akhilesh Darjee | db59ffb | 2013-09-11 13:05:24 +0530 | [diff] [blame] | 92 | price_list_rate = webnotes.conn.sql("""select ip.ref_rate from `tabItem Price` ip, |
| 93 | `tabPrice List` pl where ip.parent = pl.name and ip.parent=%s and |
| 94 | ip.item_code=%s and pl.buying_or_selling='Buying'""", |
| 95 | (args.buying_price_list, args.item_code), as_dict=1) |
| 96 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 97 | if price_list_rate: |
Akhilesh Darjee | db59ffb | 2013-09-11 13:05:24 +0530 | [diff] [blame] | 98 | from utilities.transaction_base import validate_currency |
| 99 | validate_currency(args, item_bean.doc, meta) |
| 100 | |
Nabin Hait | dd266faf | 2013-09-23 13:00:59 +0530 | [diff] [blame] | 101 | out.import_ref_rate = flt(price_list_rate[0].ref_rate) * \ |
| 102 | flt(args.plc_conversion_rate) / flt(args.conversion_rate) |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 103 | |
| 104 | # if not found, fetch from last purchase transaction |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 105 | if not out.import_ref_rate: |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 106 | last_purchase = get_last_purchase_details(item.name, args.docname, args.conversion_rate) |
| 107 | if last_purchase: |
| 108 | out.update(last_purchase) |
| 109 | |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 110 | if out.import_ref_rate or out.import_rate: |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 111 | validate_currency(args, item, meta) |
| 112 | |
| 113 | return out |
| 114 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 115 | def _get_supplier_part_no(args, item_bean): |
| 116 | item_supplier = item_bean.doclist.get({"parentfield": "item_supplier_details", |
| 117 | "supplier": args.supplier}) |
| 118 | |
| 119 | return item_supplier and item_supplier[0].supplier_part_no or None |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 120 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 121 | def _validate_item_details(args, item): |
| 122 | from utilities.transaction_base import validate_item_fetch |
| 123 | validate_item_fetch(args, item) |
| 124 | |
| 125 | # validate if purchase item or subcontracted item |
| 126 | if item.is_purchase_item != "Yes": |
| 127 | msgprint(_("Item") + (" %s: " % item.name) + _("not a purchase item"), |
| 128 | raise_exception=True) |
| 129 | |
| 130 | if args.is_subcontracted == "Yes" and item.is_sub_contracted_item != "Yes": |
| 131 | msgprint(_("Item") + (" %s: " % item.name) + |
| 132 | _("not a sub-contracted item.") + |
| 133 | _("Please select a sub-contracted item or do not sub-contract the transaction."), |
| 134 | raise_exception=True) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 135 | |
| 136 | def get_last_purchase_details(item_code, doc_name, conversion_rate=1.0): |
| 137 | """returns last purchase details in stock uom""" |
| 138 | # get last purchase order item details |
| 139 | last_purchase_order = webnotes.conn.sql("""\ |
| 140 | select po.name, po.transaction_date, po.conversion_rate, |
| 141 | po_item.conversion_factor, po_item.purchase_ref_rate, |
| 142 | po_item.discount_rate, po_item.purchase_rate |
| 143 | from `tabPurchase Order` po, `tabPurchase Order Item` po_item |
| 144 | where po.docstatus = 1 and po_item.item_code = %s and po.name != %s and |
| 145 | po.name = po_item.parent |
| 146 | order by po.transaction_date desc, po.name desc |
| 147 | limit 1""", (item_code, doc_name), as_dict=1) |
| 148 | |
| 149 | # get last purchase receipt item details |
| 150 | last_purchase_receipt = webnotes.conn.sql("""\ |
| 151 | select pr.name, pr.posting_date, pr.posting_time, pr.conversion_rate, |
| 152 | pr_item.conversion_factor, pr_item.purchase_ref_rate, pr_item.discount_rate, |
| 153 | pr_item.purchase_rate |
| 154 | from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item |
| 155 | where pr.docstatus = 1 and pr_item.item_code = %s and pr.name != %s and |
| 156 | pr.name = pr_item.parent |
| 157 | order by pr.posting_date desc, pr.posting_time desc, pr.name desc |
| 158 | limit 1""", (item_code, doc_name), as_dict=1) |
| 159 | |
| 160 | purchase_order_date = getdate(last_purchase_order and last_purchase_order[0].transaction_date \ |
| 161 | or "1900-01-01") |
| 162 | purchase_receipt_date = getdate(last_purchase_receipt and \ |
| 163 | last_purchase_receipt[0].posting_date or "1900-01-01") |
| 164 | |
| 165 | if (purchase_order_date > purchase_receipt_date) or \ |
| 166 | (last_purchase_order and not last_purchase_receipt): |
| 167 | # use purchase order |
| 168 | last_purchase = last_purchase_order[0] |
| 169 | purchase_date = purchase_order_date |
| 170 | |
| 171 | elif (purchase_receipt_date > purchase_order_date) or \ |
| 172 | (last_purchase_receipt and not last_purchase_order): |
| 173 | # use purchase receipt |
| 174 | last_purchase = last_purchase_receipt[0] |
| 175 | purchase_date = purchase_receipt_date |
| 176 | |
| 177 | else: |
| 178 | return webnotes._dict() |
| 179 | |
| 180 | conversion_factor = flt(last_purchase.conversion_factor) |
| 181 | out = webnotes._dict({ |
| 182 | "purchase_ref_rate": flt(last_purchase.purchase_ref_rate) / conversion_factor, |
| 183 | "purchase_rate": flt(last_purchase.purchase_rate) / conversion_factor, |
| 184 | "discount_rate": flt(last_purchase.discount_rate), |
| 185 | "purchase_date": purchase_date |
| 186 | }) |
| 187 | |
| 188 | conversion_rate = flt(conversion_rate) or 1.0 |
| 189 | out.update({ |
| 190 | "import_ref_rate": out.purchase_ref_rate / conversion_rate, |
| 191 | "import_rate": out.purchase_rate / conversion_rate, |
| 192 | "rate": out.purchase_rate |
| 193 | }) |
| 194 | |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 195 | return out |
| 196 | |
| 197 | @webnotes.whitelist() |
| 198 | def get_conversion_factor(item_code, uom): |
| 199 | return {"conversion_factor": webnotes.conn.get_value("UOM Conversion Detail", |
Nabin Hait | 9481e03 | 2013-07-22 17:28:11 +0530 | [diff] [blame] | 200 | {"parent": item_code, "uom": uom}, "conversion_factor")} |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 201 | |
| 202 | @webnotes.whitelist() |
| 203 | def get_projected_qty(item_code, warehouse): |
| 204 | return webnotes.conn.get_value("Bin", {"item_code": item_code, |
| 205 | "warehouse": warehouse}, "projected_qty") |