Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 3 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 4 | import frappe |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 5 | from frappe import _ |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 6 | import json |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 7 | from frappe.utils import flt, cstr, nowdate, add_days, cint |
| 8 | from frappe.defaults import get_global_default |
Anand Doshi | 4f0a428 | 2014-07-07 15:21:18 +0530 | [diff] [blame] | 9 | from erpnext.accounts.utils import get_fiscal_year, FiscalYearError |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 10 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 11 | class InvalidWarehouseCompany(frappe.ValidationError): pass |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 12 | |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 13 | def get_stock_balance_on(warehouse, posting_date=None): |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 14 | if not posting_date: posting_date = nowdate() |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 15 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 16 | stock_ledger_entries = frappe.db.sql(""" |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 17 | SELECT |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 18 | item_code, stock_value |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 19 | FROM |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 20 | `tabStock Ledger Entry` |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 21 | WHERE |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 22 | warehouse=%s AND posting_date <= %s |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 23 | ORDER BY timestamp(posting_date, posting_time) DESC, name DESC |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 24 | """, (warehouse, posting_date), as_dict=1) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 25 | |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 26 | sle_map = {} |
| 27 | for sle in stock_ledger_entries: |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 28 | sle_map.setdefault(sle.item_code, flt(sle.stock_value)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 29 | |
Nabin Hait | 625da79 | 2013-09-25 10:32:51 +0530 | [diff] [blame] | 30 | return sum(sle_map.values()) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 31 | |
Nabin Hait | 47dc318 | 2013-08-06 15:58:16 +0530 | [diff] [blame] | 32 | def get_latest_stock_balance(): |
| 33 | bin_map = {} |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 34 | for d in frappe.db.sql("""SELECT item_code, warehouse, stock_value as stock_value |
Nabin Hait | 47dc318 | 2013-08-06 15:58:16 +0530 | [diff] [blame] | 35 | FROM tabBin""", as_dict=1): |
Nabin Hait | 469ee71 | 2013-08-07 12:33:37 +0530 | [diff] [blame] | 36 | bin_map.setdefault(d.warehouse, {}).setdefault(d.item_code, flt(d.stock_value)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 37 | |
Nabin Hait | 47dc318 | 2013-08-06 15:58:16 +0530 | [diff] [blame] | 38 | return bin_map |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 39 | |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 40 | def get_bin(item_code, warehouse): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 41 | bin = frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 42 | if not bin: |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 43 | bin_obj = frappe.get_doc({ |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 44 | "doctype": "Bin", |
| 45 | "item_code": item_code, |
| 46 | "warehouse": warehouse, |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 47 | }) |
| 48 | bin_obj.ignore_permissions = 1 |
| 49 | bin_obj.insert() |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 50 | else: |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 51 | bin_obj = frappe.get_doc('Bin', bin) |
Anand Doshi | 094610d | 2014-04-16 19:56:53 +0530 | [diff] [blame] | 52 | bin_obj.ignore_permissions = True |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 53 | return bin_obj |
| 54 | |
| 55 | def update_bin(args): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 56 | is_stock_item = frappe.db.get_value('Item', args.get("item_code"), 'is_stock_item') |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 57 | if is_stock_item == 'Yes': |
| 58 | bin = get_bin(args.get("item_code"), args.get("warehouse")) |
| 59 | bin.update_stock(args) |
| 60 | return bin |
| 61 | else: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 62 | frappe.msgprint(_("Item {0} ignored since it is not a stock item").format(args.get("item_code"))) |
Nabin Hait | 0dd7be1 | 2013-08-02 11:45:43 +0530 | [diff] [blame] | 63 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 64 | def get_incoming_rate(args): |
| 65 | """Get Incoming Rate based on valuation method""" |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 66 | from erpnext.stock.stock_ledger import get_previous_sle |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 67 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 68 | in_rate = 0 |
Anand Doshi | 40a8ae2 | 2014-08-29 16:28:31 +0530 | [diff] [blame] | 69 | if (args.get("serial_no") or "").strip(): |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 70 | in_rate = get_avg_purchase_rate(args.get("serial_no")) |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 71 | else: |
| 72 | valuation_method = get_valuation_method(args.get("item_code")) |
| 73 | previous_sle = get_previous_sle(args) |
| 74 | if valuation_method == 'FIFO': |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 75 | if not previous_sle: |
| 76 | return 0.0 |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 77 | previous_stock_queue = json.loads(previous_sle.get('stock_queue', '[]') or '[]') |
Nabin Hait | 227db76 | 2014-05-08 19:06:01 +0530 | [diff] [blame] | 78 | in_rate = get_fifo_rate(previous_stock_queue, args.get("qty") or 0) if previous_stock_queue else 0 |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 79 | elif valuation_method == 'Moving Average': |
| 80 | in_rate = previous_sle.get('valuation_rate') or 0 |
Anand Doshi | 094610d | 2014-04-16 19:56:53 +0530 | [diff] [blame] | 81 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 82 | return in_rate |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 83 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 84 | def get_avg_purchase_rate(serial_nos): |
| 85 | """get average value of serial numbers""" |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 86 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 87 | serial_nos = get_valid_serial_nos(serial_nos) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 88 | return flt(frappe.db.sql("""select avg(ifnull(purchase_rate, 0)) from `tabSerial No` |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 89 | where name in (%s)""" % ", ".join(["%s"] * len(serial_nos)), |
| 90 | tuple(serial_nos))[0][0]) |
| 91 | |
| 92 | def get_valuation_method(item_code): |
| 93 | """get valuation method from item or default""" |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 94 | val_method = frappe.db.get_value('Item', item_code, 'valuation_method') |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 95 | if not val_method: |
Rushabh Mehta | 5117d9c | 2013-02-19 15:27:31 +0530 | [diff] [blame] | 96 | val_method = get_global_default('valuation_method') or "FIFO" |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 97 | return val_method |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 98 | |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 99 | def get_fifo_rate(previous_stock_queue, qty): |
| 100 | """get FIFO (average) Rate from Queue""" |
| 101 | if qty >= 0: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 102 | total = sum(f[0] for f in previous_stock_queue) |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 103 | return total and sum(f[0] * f[1] for f in previous_stock_queue) / flt(total) or 0.0 |
| 104 | else: |
Nabin Hait | 227db76 | 2014-05-08 19:06:01 +0530 | [diff] [blame] | 105 | available_qty_for_outgoing, outgoing_cost = 0, 0 |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 106 | qty_to_pop = abs(qty) |
Nabin Hait | 64d7c4b | 2013-01-17 12:22:59 +0530 | [diff] [blame] | 107 | while qty_to_pop and previous_stock_queue: |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 108 | batch = previous_stock_queue[0] |
| 109 | if 0 < batch[0] <= qty_to_pop: |
| 110 | # if batch qty > 0 |
| 111 | # not enough or exactly same qty in current batch, clear batch |
Nabin Hait | 227db76 | 2014-05-08 19:06:01 +0530 | [diff] [blame] | 112 | available_qty_for_outgoing += flt(batch[0]) |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 113 | outgoing_cost += flt(batch[0]) * flt(batch[1]) |
| 114 | qty_to_pop -= batch[0] |
| 115 | previous_stock_queue.pop(0) |
| 116 | else: |
| 117 | # all from current batch |
Nabin Hait | 227db76 | 2014-05-08 19:06:01 +0530 | [diff] [blame] | 118 | available_qty_for_outgoing += flt(qty_to_pop) |
Nabin Hait | 831207f | 2013-01-16 14:15:48 +0530 | [diff] [blame] | 119 | outgoing_cost += flt(qty_to_pop) * flt(batch[1]) |
| 120 | batch[0] -= qty_to_pop |
| 121 | qty_to_pop = 0 |
Anand Doshi | 094610d | 2014-04-16 19:56:53 +0530 | [diff] [blame] | 122 | |
Nabin Hait | 227db76 | 2014-05-08 19:06:01 +0530 | [diff] [blame] | 123 | return outgoing_cost / available_qty_for_outgoing |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 124 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 125 | def get_valid_serial_nos(sr_nos, qty=0, item_code=''): |
| 126 | """split serial nos, validate and return list of valid serial nos""" |
| 127 | # TODO: remove duplicates in client side |
| 128 | serial_nos = cstr(sr_nos).strip().replace(',', '\n').split('\n') |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 129 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 130 | valid_serial_nos = [] |
| 131 | for val in serial_nos: |
| 132 | if val: |
| 133 | val = val.strip() |
| 134 | if val in valid_serial_nos: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 135 | frappe.throw(_("Serial number {0} entered more than once").format(val)) |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 136 | else: |
| 137 | valid_serial_nos.append(val) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 138 | |
Nabin Hait | 9d0f636 | 2013-01-07 18:51:11 +0530 | [diff] [blame] | 139 | if qty and len(valid_serial_nos) != abs(qty): |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 140 | frappe.throw(_("{0} valid serial nos for Item {1}").format(abs(qty), item_code)) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 141 | |
Rushabh Mehta | 0dbe898 | 2013-02-04 13:56:50 +0530 | [diff] [blame] | 142 | return valid_serial_nos |
Nabin Hait | a72c512 | 2013-03-06 18:50:53 +0530 | [diff] [blame] | 143 | |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 144 | def validate_warehouse_company(warehouse, company): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 145 | warehouse_company = frappe.db.get_value("Warehouse", warehouse, "company") |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 146 | if warehouse_company and warehouse_company != company: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 147 | frappe.throw(_("Warehouse {0} does not belong to company {1}").format(warehouse, company), |
| 148 | InvalidWarehouseCompany) |
Rushabh Mehta | a65253b | 2013-08-27 10:32:56 +0530 | [diff] [blame] | 149 | |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 150 | def get_sales_bom_buying_amount(item_code, warehouse, voucher_type, voucher_no, voucher_detail_no, |
Nabin Hait | 94c90bd | 2013-08-30 22:48:19 +0530 | [diff] [blame] | 151 | stock_ledger_entries, item_sales_bom): |
| 152 | # sales bom item |
| 153 | buying_amount = 0.0 |
| 154 | for bom_item in item_sales_bom[item_code]: |
| 155 | if bom_item.get("parent_detail_docname")==voucher_detail_no: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 156 | buying_amount += get_buying_amount(voucher_type, voucher_no, voucher_detail_no, |
Nabin Hait | 94c90bd | 2013-08-30 22:48:19 +0530 | [diff] [blame] | 157 | stock_ledger_entries.get((bom_item.item_code, warehouse), [])) |
| 158 | |
| 159 | return buying_amount |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 160 | |
Nabin Hait | 94c90bd | 2013-08-30 22:48:19 +0530 | [diff] [blame] | 161 | def get_buying_amount(voucher_type, voucher_no, item_row, stock_ledger_entries): |
Anand Doshi | 5dd6b1d | 2013-08-07 19:27:30 +0530 | [diff] [blame] | 162 | # IMP NOTE |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 163 | # stock_ledger_entries should already be filtered by item_code and warehouse and |
Anand Doshi | 5dd6b1d | 2013-08-07 19:27:30 +0530 | [diff] [blame] | 164 | # sorted by posting_date desc, posting_time desc |
| 165 | for i, sle in enumerate(stock_ledger_entries): |
Nabin Hait | 0cfbc5f | 2013-03-12 11:34:56 +0530 | [diff] [blame] | 166 | if sle.voucher_type == voucher_type and sle.voucher_no == voucher_no and \ |
Anand Doshi | 8c45420 | 2013-03-28 16:40:30 +0530 | [diff] [blame] | 167 | sle.voucher_detail_no == item_row: |
Anand Doshi | 5dd6b1d | 2013-08-07 19:27:30 +0530 | [diff] [blame] | 168 | previous_stock_value = len(stock_ledger_entries) > i+1 and \ |
| 169 | flt(stock_ledger_entries[i+1].stock_value) or 0.0 |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 170 | buying_amount = previous_stock_value - flt(sle.stock_value) |
| 171 | |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 172 | return buying_amount |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 173 | return 0.0 |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 174 | |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 175 | |
| 176 | def reorder_item(): |
| 177 | """ Reorder item if stock reaches reorder level""" |
Nabin Hait | e6ddd28 | 2014-05-30 14:44:37 +0530 | [diff] [blame] | 178 | # if initial setup not completed, return |
| 179 | if not frappe.db.sql("select name from `tabFiscal Year` limit 1"): |
| 180 | return |
| 181 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 182 | if getattr(frappe.local, "auto_indent", None) is None: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 183 | frappe.local.auto_indent = cint(frappe.db.get_value('Stock Settings', None, 'auto_indent')) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 184 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 185 | if frappe.local.auto_indent: |
Anand Doshi | b8189d7 | 2014-07-16 19:24:53 +0530 | [diff] [blame] | 186 | _reorder_item() |
Anand Doshi | e673a66 | 2014-07-02 18:12:02 +0530 | [diff] [blame] | 187 | |
Anand Doshi | b8189d7 | 2014-07-16 19:24:53 +0530 | [diff] [blame] | 188 | def _reorder_item(): |
| 189 | # {"Purchase": {"Company": [{"item_code": "", "warehouse": "", "reorder_qty": 0.0}]}, "Transfer": {...}} |
| 190 | material_requests = {"Purchase": {}, "Transfer": {}} |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 191 | |
Anand Doshi | b8189d7 | 2014-07-16 19:24:53 +0530 | [diff] [blame] | 192 | item_warehouse_projected_qty = get_item_warehouse_projected_qty() |
| 193 | warehouse_company = frappe._dict(frappe.db.sql("""select name, company from `tabWarehouse`""")) |
Anand Doshi | d141cf7 | 2014-07-18 10:31:34 +0530 | [diff] [blame] | 194 | default_company = (frappe.defaults.get_defaults().get("company") or |
Anand Doshi | b8189d7 | 2014-07-16 19:24:53 +0530 | [diff] [blame] | 195 | frappe.db.sql("""select name from tabCompany limit 1""")[0][0]) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 196 | |
Anand Doshi | b8189d7 | 2014-07-16 19:24:53 +0530 | [diff] [blame] | 197 | def add_to_material_request(item_code, warehouse, reorder_level, reorder_qty, material_request_type): |
| 198 | if warehouse not in item_warehouse_projected_qty[item_code]: |
| 199 | # likely a disabled warehouse or a warehouse where BIN does not exist |
| 200 | return |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 201 | |
Anand Doshi | b8189d7 | 2014-07-16 19:24:53 +0530 | [diff] [blame] | 202 | reorder_level = flt(reorder_level) |
| 203 | reorder_qty = flt(reorder_qty) |
| 204 | projected_qty = item_warehouse_projected_qty[item_code][warehouse] |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 205 | |
Anand Doshi | b8189d7 | 2014-07-16 19:24:53 +0530 | [diff] [blame] | 206 | if reorder_level and projected_qty < reorder_level: |
| 207 | deficiency = reorder_level - projected_qty |
| 208 | if deficiency > reorder_qty: |
| 209 | reorder_qty = deficiency |
| 210 | |
| 211 | company = warehouse_company.get(warehouse) or default_company |
| 212 | |
| 213 | material_requests[material_request_type].setdefault(company, []).append({ |
| 214 | "item_code": item_code, |
| 215 | "warehouse": warehouse, |
| 216 | "reorder_qty": reorder_qty |
| 217 | }) |
| 218 | |
| 219 | for item_code in item_warehouse_projected_qty: |
| 220 | item = frappe.get_doc("Item", item_code) |
| 221 | if item.get("item_reorder"): |
| 222 | for d in item.get("item_reorder"): |
| 223 | add_to_material_request(item_code, d.warehouse, d.warehouse_reorder_level, |
| 224 | d.warehouse_reorder_qty, d.material_request_type) |
| 225 | |
| 226 | else: |
| 227 | # raise for default warehouse |
| 228 | add_to_material_request(item_code, item.default_warehouse, item.re_order_level, item.re_order_qty, "Purchase") |
| 229 | |
| 230 | if material_requests: |
| 231 | create_material_request(material_requests) |
| 232 | |
| 233 | def get_item_warehouse_projected_qty(): |
| 234 | item_warehouse_projected_qty = {} |
| 235 | |
| 236 | for item_code, warehouse, projected_qty in frappe.db.sql("""select item_code, warehouse, projected_qty |
| 237 | from tabBin where ifnull(item_code, '') != '' and ifnull(warehouse, '') != '' |
| 238 | and exists (select name from `tabItem` |
| 239 | where `tabItem`.name = `tabBin`.item_code and |
| 240 | is_stock_item='Yes' and (is_purchase_item='Yes' or is_sub_contracted_item='Yes') and |
| 241 | (ifnull(end_of_life, '0000-00-00')='0000-00-00' or end_of_life > %s)) |
| 242 | and exists (select name from `tabWarehouse` |
| 243 | where `tabWarehouse`.name = `tabBin`.warehouse |
| 244 | and ifnull(disabled, 0)=0)""", nowdate()): |
| 245 | |
| 246 | item_warehouse_projected_qty.setdefault(item_code, {})[warehouse] = flt(projected_qty) |
| 247 | |
| 248 | return item_warehouse_projected_qty |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 249 | |
| 250 | def create_material_request(material_requests): |
| 251 | """ Create indent on reaching reorder level """ |
| 252 | mr_list = [] |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 253 | defaults = frappe.defaults.get_defaults() |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 254 | exceptions_list = [] |
Anand Doshi | 4f0a428 | 2014-07-07 15:21:18 +0530 | [diff] [blame] | 255 | |
| 256 | def _log_exception(): |
| 257 | if frappe.local.message_log: |
| 258 | exceptions_list.extend(frappe.local.message_log) |
| 259 | frappe.local.message_log = [] |
| 260 | else: |
| 261 | exceptions_list.append(frappe.get_traceback()) |
| 262 | |
| 263 | try: |
| 264 | current_fiscal_year = get_fiscal_year(nowdate())[0] or defaults.fiscal_year |
| 265 | |
| 266 | except FiscalYearError: |
| 267 | _log_exception() |
| 268 | notify_errors(exceptions_list) |
| 269 | return |
| 270 | |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 271 | for request_type in material_requests: |
| 272 | for company in material_requests[request_type]: |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 273 | try: |
| 274 | items = material_requests[request_type][company] |
| 275 | if not items: |
| 276 | continue |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 277 | |
| 278 | mr = frappe.new_doc("Material Request") |
| 279 | mr.update({ |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 280 | "company": company, |
Akhilesh Darjee | b0a1b3a | 2013-11-26 14:45:27 +0530 | [diff] [blame] | 281 | "fiscal_year": current_fiscal_year, |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 282 | "transaction_date": nowdate(), |
Anand Doshi | 4a67d36 | 2013-11-13 14:53:09 +0530 | [diff] [blame] | 283 | "material_request_type": request_type |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 284 | }) |
| 285 | |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 286 | for d in items: |
Anand Doshi | b8189d7 | 2014-07-16 19:24:53 +0530 | [diff] [blame] | 287 | d = frappe._dict(d) |
Rushabh Mehta | b385ecf | 2014-03-28 16:44:37 +0530 | [diff] [blame] | 288 | item = frappe.get_doc("Item", d.item_code) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 289 | mr.append("indent_details", { |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 290 | "doctype": "Material Request Item", |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 291 | "item_code": d.item_code, |
| 292 | "schedule_date": add_days(nowdate(),cint(item.lead_time_days)), |
| 293 | "uom": item.stock_uom, |
| 294 | "warehouse": d.warehouse, |
| 295 | "item_name": item.item_name, |
| 296 | "description": item.description, |
| 297 | "item_group": item.item_group, |
| 298 | "qty": d.reorder_qty, |
| 299 | "brand": item.brand, |
| 300 | }) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 301 | |
| 302 | mr.insert() |
| 303 | mr.submit() |
| 304 | mr_list.append(mr) |
Anand Doshi | 6f6e91c | 2013-07-22 11:28:34 +0530 | [diff] [blame] | 305 | |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 306 | except: |
Anand Doshi | 4f0a428 | 2014-07-07 15:21:18 +0530 | [diff] [blame] | 307 | _log_exception() |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 308 | |
| 309 | if mr_list: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 310 | if getattr(frappe.local, "reorder_email_notify", None) is None: |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 311 | frappe.local.reorder_email_notify = cint(frappe.db.get_value('Stock Settings', None, |
Nabin Hait | bf5d44c | 2013-08-12 16:30:48 +0530 | [diff] [blame] | 312 | 'reorder_email_notify')) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 313 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 314 | if(frappe.local.reorder_email_notify): |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 315 | send_email_notification(mr_list) |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 316 | |
| 317 | if exceptions_list: |
| 318 | notify_errors(exceptions_list) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 319 | |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 320 | def send_email_notification(mr_list): |
| 321 | """ Notify user about auto creation of indent""" |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 322 | |
| 323 | email_list = frappe.db.sql_list("""select distinct r.parent |
Rushabh Mehta | 7c93200 | 2014-03-11 16:15:05 +0530 | [diff] [blame] | 324 | from tabUserRole r, tabUser p |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 325 | where p.name = r.parent and p.enabled = 1 and p.docstatus < 2 |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 326 | and r.role in ('Purchase Manager','Material Manager') |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 327 | and p.name not in ('Administrator', 'All', 'Guest')""") |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 328 | |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 329 | msg="""<h3>Following Material Requests has been raised automatically \ |
| 330 | based on item reorder level:</h3>""" |
| 331 | for mr in mr_list: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 332 | msg += "<p><b><u>" + mr.name + """</u></b></p><table class='table table-bordered'><tr> |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 333 | <th>Item Code</th><th>Warehouse</th><th>Qty</th><th>UOM</th></tr>""" |
Rushabh Mehta | d2b34dc | 2014-03-27 16:12:56 +0530 | [diff] [blame] | 334 | for item in mr.get("indent_details"): |
Nabin Hait | 62d0629 | 2013-05-22 16:19:10 +0530 | [diff] [blame] | 335 | msg += "<tr><td>" + item.item_code + "</td><td>" + item.warehouse + "</td><td>" + \ |
| 336 | cstr(item.qty) + "</td><td>" + cstr(item.uom) + "</td></tr>" |
| 337 | msg += "</table>" |
Rushabh Mehta | bf8715d | 2014-09-16 14:57:31 +0530 | [diff] [blame] | 338 | frappe.sendmail(recipients=email_list, subject='Auto Material Request Generation Notification', msg = msg) |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 339 | |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 340 | def notify_errors(exceptions_list): |
| 341 | subject = "[Important] [ERPNext] Error(s) while creating Material Requests based on Re-order Levels" |
Rushabh Mehta | bf8715d | 2014-09-16 14:57:31 +0530 | [diff] [blame] | 342 | content = """Dear System Manager, |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 343 | |
Anand Doshi | 4f0a428 | 2014-07-07 15:21:18 +0530 | [diff] [blame] | 344 | An error occured for certain Items while creating Material Requests based on Re-order level. |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 345 | |
Anand Doshi | 4f0a428 | 2014-07-07 15:21:18 +0530 | [diff] [blame] | 346 | Please rectify these issues: |
| 347 | --- |
| 348 | <pre> |
| 349 | %s |
| 350 | </pre> |
| 351 | --- |
| 352 | Regards, |
| 353 | Administrator""" % ("\n\n".join(exceptions_list),) |
Anand Doshi | ad6180e | 2013-06-17 11:57:04 +0530 | [diff] [blame] | 354 | |
Rushabh Mehta | bf8715d | 2014-09-16 14:57:31 +0530 | [diff] [blame] | 355 | from frappe.email import sendmail_to_system_managers |
| 356 | sendmail_to_system_managers(subject, content) |