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 |
| 19 | from webnotes.utils import getdate, flt, add_days |
| 20 | import json |
| 21 | |
| 22 | @webnotes.whitelist() |
| 23 | def get_item_details(args): |
| 24 | """ |
| 25 | args = { |
| 26 | "doctype": "", |
| 27 | "docname": "", |
| 28 | "item_code": "", |
| 29 | "warehouse": None, |
| 30 | "supplier": None, |
| 31 | "transaction_date": None, |
| 32 | "conversion_rate": 1.0 |
| 33 | } |
| 34 | """ |
| 35 | if isinstance(args, basestring): |
| 36 | args = json.loads(args) |
| 37 | |
| 38 | args = webnotes._dict(args) |
| 39 | |
Rushabh Mehta | c53231a | 2013-02-18 18:24:28 +0530 | [diff] [blame] | 40 | item_wrapper = webnotes.bean("Item", args.item_code) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 41 | item = item_wrapper.doc |
| 42 | |
| 43 | from stock.utils import validate_end_of_life |
| 44 | validate_end_of_life(item.name, item.end_of_life) |
| 45 | |
| 46 | # fetch basic values |
| 47 | out = webnotes._dict() |
| 48 | out.update({ |
| 49 | "item_name": item.item_name, |
| 50 | "item_group": item.item_group, |
| 51 | "brand": item.brand, |
| 52 | "description": item.description, |
| 53 | "qty": 0, |
| 54 | "stock_uom": item.stock_uom, |
| 55 | "uom": item.stock_uom, |
Anand Doshi | 1bfec3a | 2013-02-28 19:17:46 +0530 | [diff] [blame] | 56 | "conversion_factor": 1.0, |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 57 | "warehouse": args.warehouse or item.default_warehouse, |
| 58 | "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in |
Nabin Hait | d98b653 | 2013-01-21 18:54:01 +0530 | [diff] [blame] | 59 | item_wrapper.doclist.get({"parentfield": "item_tax"})))), |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 60 | "batch_no": None, |
| 61 | "expense_head": item.purchase_account, |
| 62 | "cost_center": item.cost_center |
| 63 | }) |
| 64 | |
| 65 | if args.supplier: |
| 66 | item_supplier = item_wrapper.doclist.get({"parentfield": "item_supplier_details", |
| 67 | "supplier": args.supplier}) |
| 68 | if item_supplier: |
| 69 | out["supplier_part_no"] = item_supplier[0].supplier_part_no |
| 70 | |
| 71 | if out.warehouse: |
| 72 | out.projected_qty = webnotes.conn.get_value("Bin", {"item_code": item.name, |
| 73 | "warehouse": out.warehouse}, "projected_qty") |
| 74 | |
| 75 | if args.transaction_date and item.lead_time_days: |
| 76 | out.schedule_date = out.lead_time_date = add_days(args.transaction_date, |
| 77 | item.lead_time_days) |
| 78 | |
| 79 | # set zero |
| 80 | out.purchase_ref_rate = out.discount_rate = out.purchase_rate = \ |
| 81 | out.import_ref_rate = out.import_rate = 0.0 |
| 82 | |
Nabin Hait | 396f47d | 2013-01-18 16:29:30 +0530 | [diff] [blame] | 83 | if args.doctype in ["Purchase Order", "Purchase Invoice", "Purchase Receipt", |
| 84 | "Supplier Quotation"]: |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 85 | # try fetching from price list |
Anand Doshi | f8f0c0d | 2013-01-17 20:20:56 +0530 | [diff] [blame] | 86 | if args.price_list_name and args.price_list_currency: |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 87 | rates_as_per_price_list = get_rates_as_per_price_list(args, item_wrapper.doclist) |
| 88 | if rates_as_per_price_list: |
| 89 | out.update(rates_as_per_price_list) |
| 90 | |
| 91 | # if not found, fetch from last purchase transaction |
| 92 | if not out.purchase_rate: |
| 93 | last_purchase = get_last_purchase_details(item.name, args.docname, args.conversion_rate) |
| 94 | if last_purchase: |
| 95 | out.update(last_purchase) |
| 96 | |
| 97 | return out |
| 98 | |
| 99 | def get_rates_as_per_price_list(args, item_doclist=None): |
| 100 | if not item_doclist: |
Rushabh Mehta | c53231a | 2013-02-18 18:24:28 +0530 | [diff] [blame] | 101 | item_doclist = webnotes.bean("Item", args.item_code).doclist |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 102 | |
| 103 | result = item_doclist.get({"parentfield": "ref_rate_details", |
Nabin Hait | 396f47d | 2013-01-18 16:29:30 +0530 | [diff] [blame] | 104 | "price_list_name": args.price_list_name, "ref_currency": args.price_list_currency, |
| 105 | "buying": 1}) |
Anand Doshi | 756dca7 | 2013-01-15 18:39:21 +0530 | [diff] [blame] | 106 | |
| 107 | if result: |
| 108 | purchase_ref_rate = flt(result[0].ref_rate) * flt(args.plc_conversion_rate) |
| 109 | conversion_rate = flt(args.conversion_rate) or 1.0 |
| 110 | return webnotes._dict({ |
| 111 | "purchase_ref_rate": purchase_ref_rate, |
| 112 | "purchase_rate": purchase_ref_rate, |
| 113 | "rate": purchase_ref_rate, |
| 114 | "discount_rate": 0, |
| 115 | "import_ref_rate": purchase_ref_rate / conversion_rate, |
| 116 | "import_rate": purchase_ref_rate / conversion_rate |
| 117 | }) |
| 118 | else: |
| 119 | return webnotes._dict() |
| 120 | |
| 121 | def get_last_purchase_details(item_code, doc_name, conversion_rate=1.0): |
| 122 | """returns last purchase details in stock uom""" |
| 123 | # get last purchase order item details |
| 124 | last_purchase_order = webnotes.conn.sql("""\ |
| 125 | select po.name, po.transaction_date, po.conversion_rate, |
| 126 | po_item.conversion_factor, po_item.purchase_ref_rate, |
| 127 | po_item.discount_rate, po_item.purchase_rate |
| 128 | from `tabPurchase Order` po, `tabPurchase Order Item` po_item |
| 129 | where po.docstatus = 1 and po_item.item_code = %s and po.name != %s and |
| 130 | po.name = po_item.parent |
| 131 | order by po.transaction_date desc, po.name desc |
| 132 | limit 1""", (item_code, doc_name), as_dict=1) |
| 133 | |
| 134 | # get last purchase receipt item details |
| 135 | last_purchase_receipt = webnotes.conn.sql("""\ |
| 136 | select pr.name, pr.posting_date, pr.posting_time, pr.conversion_rate, |
| 137 | pr_item.conversion_factor, pr_item.purchase_ref_rate, pr_item.discount_rate, |
| 138 | pr_item.purchase_rate |
| 139 | from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item |
| 140 | where pr.docstatus = 1 and pr_item.item_code = %s and pr.name != %s and |
| 141 | pr.name = pr_item.parent |
| 142 | order by pr.posting_date desc, pr.posting_time desc, pr.name desc |
| 143 | limit 1""", (item_code, doc_name), as_dict=1) |
| 144 | |
| 145 | purchase_order_date = getdate(last_purchase_order and last_purchase_order[0].transaction_date \ |
| 146 | or "1900-01-01") |
| 147 | purchase_receipt_date = getdate(last_purchase_receipt and \ |
| 148 | last_purchase_receipt[0].posting_date or "1900-01-01") |
| 149 | |
| 150 | if (purchase_order_date > purchase_receipt_date) or \ |
| 151 | (last_purchase_order and not last_purchase_receipt): |
| 152 | # use purchase order |
| 153 | last_purchase = last_purchase_order[0] |
| 154 | purchase_date = purchase_order_date |
| 155 | |
| 156 | elif (purchase_receipt_date > purchase_order_date) or \ |
| 157 | (last_purchase_receipt and not last_purchase_order): |
| 158 | # use purchase receipt |
| 159 | last_purchase = last_purchase_receipt[0] |
| 160 | purchase_date = purchase_receipt_date |
| 161 | |
| 162 | else: |
| 163 | return webnotes._dict() |
| 164 | |
| 165 | conversion_factor = flt(last_purchase.conversion_factor) |
| 166 | out = webnotes._dict({ |
| 167 | "purchase_ref_rate": flt(last_purchase.purchase_ref_rate) / conversion_factor, |
| 168 | "purchase_rate": flt(last_purchase.purchase_rate) / conversion_factor, |
| 169 | "discount_rate": flt(last_purchase.discount_rate), |
| 170 | "purchase_date": purchase_date |
| 171 | }) |
| 172 | |
| 173 | conversion_rate = flt(conversion_rate) or 1.0 |
| 174 | out.update({ |
| 175 | "import_ref_rate": out.purchase_ref_rate / conversion_rate, |
| 176 | "import_rate": out.purchase_rate / conversion_rate, |
| 177 | "rate": out.purchase_rate |
| 178 | }) |
| 179 | |
| 180 | return out |