Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 1 | # ERPNext - web based ERP (http://erpnext.com) |
| 2 | # Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | from __future__ import unicode_literals |
| 18 | import webnotes |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 19 | from webnotes import msgprint, _ |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 20 | from webnotes.utils import getdate, flt, add_days |
| 21 | import json |
| 22 | |
| 23 | @webnotes.whitelist() |
| 24 | def get_item_details(args): |
| 25 | """ |
| 26 | args = { |
| 27 | "doctype": "", |
| 28 | "docname": "", |
| 29 | "item_code": "", |
| 30 | "warehouse": None, |
| 31 | "supplier": None, |
| 32 | "transaction_date": None, |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 33 | "conversion_rate": 1.0, |
| 34 | "price_list_name": None, |
| 35 | "price_list_currency": None, |
| 36 | "plc_conversion_rate": 1.0, |
| 37 | "is_subcontracted": "Yes" / "No" |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 38 | } |
| 39 | """ |
| 40 | if isinstance(args, basestring): |
| 41 | args = json.loads(args) |
| 42 | |
| 43 | args = webnotes._dict(args) |
| 44 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 45 | item_bean = webnotes.bean("Item", args.item_code) |
| 46 | item = item_bean.doc |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 47 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 48 | _validate_item_details(args, item) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 49 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 50 | out = _get_basic_details(args, item_bean) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 51 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 52 | out.supplier_part_no = _get_supplier_part_no(args, item_bean) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 53 | |
| 54 | if out.warehouse: |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 55 | out.projected_qty = get_projected_qty(item.name, out.warehouse) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 56 | |
| 57 | if args.transaction_date and item.lead_time_days: |
| 58 | out.schedule_date = out.lead_time_date = add_days(args.transaction_date, |
| 59 | item.lead_time_days) |
| 60 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 61 | meta = webnotes.get_doctype(args.doctype) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 62 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 63 | if meta.get_field("currency"): |
| 64 | out.purchase_ref_rate = out.discount_rate = out.purchase_rate = \ |
| 65 | out.import_ref_rate = out.import_rate = 0.0 |
| 66 | out.update(_get_price_list_rate(args, item_bean, meta)) |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 67 | |
| 68 | if args.doctype == "Material Request": |
| 69 | out.min_order_qty = flt(item.min_order_qty) |
| 70 | |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 71 | return out |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 72 | |
| 73 | def _get_basic_details(args, item_bean): |
| 74 | item = item_bean.doc |
| 75 | |
| 76 | out = webnotes._dict({ |
| 77 | "description": item.description_html or item.description, |
| 78 | "qty": 0.0, |
| 79 | "uom": item.stock_uom, |
| 80 | "conversion_factor": 1.0, |
| 81 | "warehouse": args.warehouse or item.default_warehouse, |
| 82 | "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in |
| 83 | item_bean.doclist.get({"parentfield": "item_tax"})))), |
| 84 | "batch_no": None, |
| 85 | "expense_head": item.purchase_account, |
| 86 | "cost_center": item.cost_center |
| 87 | }) |
| 88 | |
| 89 | for fieldname in ("item_name", "item_group", "brand", "stock_uom"): |
| 90 | out[fieldname] = item.fields.get(fieldname) |
| 91 | |
| 92 | return out |
| 93 | |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 94 | def _get_price_list_rate(args, item_bean, meta): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 95 | from utilities.transaction_base import validate_currency |
| 96 | item = item_bean.doc |
| 97 | out = webnotes._dict() |
| 98 | |
| 99 | # try fetching from price list |
| 100 | if args.price_list_name and args.price_list_currency: |
| 101 | price_list_rate = item_bean.doclist.get({ |
| 102 | "parentfield": "ref_rate_details", |
| 103 | "price_list_name": args.price_list_name, |
| 104 | "ref_currency": args.price_list_currency, |
| 105 | "buying": 1}) |
| 106 | if price_list_rate: |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 107 | out.import_ref_rate = \ |
| 108 | flt(price_list_rate[0].ref_rate * args.plc_conversion_rate / args.conversion_rate) |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 109 | |
| 110 | # if not found, fetch from last purchase transaction |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 111 | if not out.import_ref_rate: |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 112 | last_purchase = get_last_purchase_details(item.name, args.docname, args.conversion_rate) |
| 113 | if last_purchase: |
| 114 | out.update(last_purchase) |
| 115 | |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 116 | if out.import_ref_rate or out.import_rate: |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 117 | validate_currency(args, item, meta) |
| 118 | |
| 119 | return out |
| 120 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 121 | def _get_supplier_part_no(args, item_bean): |
| 122 | item_supplier = item_bean.doclist.get({"parentfield": "item_supplier_details", |
| 123 | "supplier": args.supplier}) |
| 124 | |
| 125 | return item_supplier and item_supplier[0].supplier_part_no or None |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 126 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 127 | def _validate_item_details(args, item): |
| 128 | from utilities.transaction_base import validate_item_fetch |
| 129 | validate_item_fetch(args, item) |
| 130 | |
| 131 | # validate if purchase item or subcontracted item |
| 132 | if item.is_purchase_item != "Yes": |
| 133 | msgprint(_("Item") + (" %s: " % item.name) + _("not a purchase item"), |
| 134 | raise_exception=True) |
| 135 | |
| 136 | if args.is_subcontracted == "Yes" and item.is_sub_contracted_item != "Yes": |
| 137 | msgprint(_("Item") + (" %s: " % item.name) + |
| 138 | _("not a sub-contracted item.") + |
| 139 | _("Please select a sub-contracted item or do not sub-contract the transaction."), |
| 140 | raise_exception=True) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 141 | |
| 142 | def get_last_purchase_details(item_code, doc_name, conversion_rate=1.0): |
| 143 | """returns last purchase details in stock uom""" |
| 144 | # get last purchase order item details |
| 145 | last_purchase_order = webnotes.conn.sql("""\ |
| 146 | select po.name, po.transaction_date, po.conversion_rate, |
| 147 | po_item.conversion_factor, po_item.purchase_ref_rate, |
| 148 | po_item.discount_rate, po_item.purchase_rate |
| 149 | from `tabPurchase Order` po, `tabPurchase Order Item` po_item |
| 150 | where po.docstatus = 1 and po_item.item_code = %s and po.name != %s and |
| 151 | po.name = po_item.parent |
| 152 | order by po.transaction_date desc, po.name desc |
| 153 | limit 1""", (item_code, doc_name), as_dict=1) |
| 154 | |
| 155 | # get last purchase receipt item details |
| 156 | last_purchase_receipt = webnotes.conn.sql("""\ |
| 157 | select pr.name, pr.posting_date, pr.posting_time, pr.conversion_rate, |
| 158 | pr_item.conversion_factor, pr_item.purchase_ref_rate, pr_item.discount_rate, |
| 159 | pr_item.purchase_rate |
| 160 | from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item |
| 161 | where pr.docstatus = 1 and pr_item.item_code = %s and pr.name != %s and |
| 162 | pr.name = pr_item.parent |
| 163 | order by pr.posting_date desc, pr.posting_time desc, pr.name desc |
| 164 | limit 1""", (item_code, doc_name), as_dict=1) |
| 165 | |
| 166 | purchase_order_date = getdate(last_purchase_order and last_purchase_order[0].transaction_date \ |
| 167 | or "1900-01-01") |
| 168 | purchase_receipt_date = getdate(last_purchase_receipt and \ |
| 169 | last_purchase_receipt[0].posting_date or "1900-01-01") |
| 170 | |
| 171 | if (purchase_order_date > purchase_receipt_date) or \ |
| 172 | (last_purchase_order and not last_purchase_receipt): |
| 173 | # use purchase order |
| 174 | last_purchase = last_purchase_order[0] |
| 175 | purchase_date = purchase_order_date |
| 176 | |
| 177 | elif (purchase_receipt_date > purchase_order_date) or \ |
| 178 | (last_purchase_receipt and not last_purchase_order): |
| 179 | # use purchase receipt |
| 180 | last_purchase = last_purchase_receipt[0] |
| 181 | purchase_date = purchase_receipt_date |
| 182 | |
| 183 | else: |
| 184 | return webnotes._dict() |
| 185 | |
| 186 | conversion_factor = flt(last_purchase.conversion_factor) |
| 187 | out = webnotes._dict({ |
| 188 | "purchase_ref_rate": flt(last_purchase.purchase_ref_rate) / conversion_factor, |
| 189 | "purchase_rate": flt(last_purchase.purchase_rate) / conversion_factor, |
| 190 | "discount_rate": flt(last_purchase.discount_rate), |
| 191 | "purchase_date": purchase_date |
| 192 | }) |
| 193 | |
| 194 | conversion_rate = flt(conversion_rate) or 1.0 |
| 195 | out.update({ |
| 196 | "import_ref_rate": out.purchase_ref_rate / conversion_rate, |
| 197 | "import_rate": out.purchase_rate / conversion_rate, |
| 198 | "rate": out.purchase_rate |
| 199 | }) |
| 200 | |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 201 | return out |
| 202 | |
| 203 | @webnotes.whitelist() |
| 204 | def get_conversion_factor(item_code, uom): |
| 205 | return {"conversion_factor": webnotes.conn.get_value("UOM Conversion Detail", |
| 206 | {"parent": item_code, "uom": uom})} |
| 207 | |
| 208 | @webnotes.whitelist() |
| 209 | def get_projected_qty(item_code, warehouse): |
| 210 | return webnotes.conn.get_value("Bin", {"item_code": item_code, |
| 211 | "warehouse": warehouse}, "projected_qty") |