def update_stock_ledger(self): """ find difference between current and expected entries and create stock ledger entries based on the difference""" from epaas.stock.stock_ledger import get_previous_sle for row in self.items: previous_sle = get_previous_sle({ "item_code": row.item_code, "warehouse": row.warehouse, "posting_date": self.posting_date, "posting_time": self.posting_time }) if previous_sle: if row.qty in ("", None): row.qty = previous_sle.get("qty_after_transaction", 0) if row.valuation_rate in ("", None): row.valuation_rate = previous_sle.get("valuation_rate", 0) if row.qty and not row.valuation_rate: dataent.throw( _("Valuation Rate required for Item in row {0}").format( row.idx)) if ((previous_sle and row.qty == previous_sle.get("qty_after_transaction") and (row.valuation_rate == previous_sle.get("valuation_rate") or row.qty == 0)) or (not previous_sle and not row.qty)): continue self.insert_entries(row)
def get_stock_qty(item_code, warehouse): return get_previous_sle({ "item_code": item_code, "warehouse": warehouse, "posting_date": nowdate(), "posting_time": nowtime() }).get("qty_after_transaction") or 0
def get_incoming_rate(args, raise_error_if_no_rate=True): """Get Incoming Rate based on valuation method""" from epaas.stock.stock_ledger import get_previous_sle, get_valuation_rate if isinstance(args, string_types): args = json.loads(args) in_rate = 0 if (args.get("serial_no") or "").strip(): in_rate = get_avg_purchase_rate(args.get("serial_no")) else: valuation_method = get_valuation_method(args.get("item_code")) previous_sle = get_previous_sle(args) if valuation_method == 'FIFO': if previous_sle: previous_stock_queue = json.loads(previous_sle.get('stock_queue', '[]') or '[]') in_rate = get_fifo_rate(previous_stock_queue, args.get("qty") or 0) if previous_stock_queue else 0 elif valuation_method == 'Moving Average': in_rate = previous_sle.get('valuation_rate') or 0 if not in_rate: voucher_no = args.get('voucher_no') or args.get('name') in_rate = get_valuation_rate(args.get('item_code'), args.get('warehouse'), args.get('voucher_type'), voucher_no, args.get('allow_zero_valuation'), currency=epaas.get_company_currency(args.get('company')), company=args.get('company'), raise_error_if_no_rate=True) return in_rate
def get_qty_after_transaction(**args): args = dataent._dict(args) last_sle = get_previous_sle({ "item_code": args.item_code or "_Test Item", "warehouse": args.warehouse or "_Test Warehouse - _TC", "posting_date": args.posting_date or nowdate(), "posting_time": args.posting_time or nowtime() }) return flt(last_sle.get("qty_after_transaction"))
def test_delivery_note_no_gl_entry(self): company = dataent.db.get_value('Warehouse', '_Test Warehouse - _TC', 'company') set_perpetual_inventory(0, company) make_stock_entry(target="_Test Warehouse - _TC", qty=5, basic_rate=100) stock_queue = json.loads(get_previous_sle({ "item_code": "_Test Item", "warehouse": "_Test Warehouse - _TC", "posting_date": nowdate(), "posting_time": nowtime() }).stock_queue or "[]") dn = create_delivery_note() sle = dataent.get_doc("Stock Ledger Entry", {"voucher_type": "Delivery Note", "voucher_no": dn.name}) self.assertEqual(sle.stock_value_difference, -1*stock_queue[0][1]) self.assertFalse(get_gl_entries("Delivery Note", dn.name))
def get_stock_balance(item_code, warehouse, posting_date=None, posting_time=None, with_valuation_rate=False): """Returns stock balance quantity at given warehouse on given posting date or current date. If `with_valuation_rate` is True, will return tuple (qty, rate)""" from epaas.stock.stock_ledger import get_previous_sle if not posting_date: posting_date = nowdate() if not posting_time: posting_time = nowtime() last_entry = get_previous_sle({ "item_code": item_code, "warehouse":warehouse, "posting_date": posting_date, "posting_time": posting_time }) if with_valuation_rate: return (last_entry.qty_after_transaction, last_entry.valuation_rate) if last_entry else (0.0, 0.0) else: return last_entry.qty_after_transaction if last_entry else 0.0
def get_opening_balance(filters, columns): if not (filters.item_code and filters.warehouse and filters.from_date): return from epaas.stock.stock_ledger import get_previous_sle last_entry = get_previous_sle({ "item_code": filters.item_code, "warehouse_condition": get_warehouse_condition(filters.warehouse), "posting_date": filters.from_date, "posting_time": "00:00:00" }) row = {} row["item_code"] = _("'Opening'") for dummy, v in ((9, 'qty_after_transaction'), (11, 'valuation_rate'), (12, 'stock_value')): row[v] = last_entry.get(v, 0) return row
def _test_reco_sle_gle(self, valuation_method): set_perpetual_inventory() # [[qty, valuation_rate, posting_date, # posting_time, expected_stock_value, bin_qty, bin_valuation]] input_data = [[50, 1000, "2012-12-26", "12:00"], [25, 900, "2012-12-26", "12:00"], ["", 1000, "2012-12-20", "12:05"], [20, "", "2012-12-26", "12:05"], [0, "", "2012-12-31", "12:10"]] for d in input_data: set_valuation_method("_Test Item", valuation_method) last_sle = get_previous_sle({ "item_code": "_Test Item", "warehouse": "_Test Warehouse - _TC", "posting_date": d[2], "posting_time": d[3] }) # submit stock reconciliation stock_reco = create_stock_reconciliation(qty=d[0], rate=d[1], posting_date=d[2], posting_time=d[3]) # check stock value sle = dataent.db.sql("""select * from `tabStock Ledger Entry` where voucher_type='Stock Reconciliation' and voucher_no=%s""", stock_reco.name, as_dict=1) qty_after_transaction = flt(d[0]) if d[0] != "" else flt( last_sle.get("qty_after_transaction")) valuation_rate = flt(d[1]) if d[1] != "" else flt( last_sle.get("valuation_rate")) if qty_after_transaction == last_sle.get("qty_after_transaction") \ and valuation_rate == last_sle.get("valuation_rate"): self.assertFalse(sle) else: self.assertEqual(sle[0].qty_after_transaction, qty_after_transaction) self.assertEqual(sle[0].stock_value, qty_after_transaction * valuation_rate) # no gl entries self.assertTrue( dataent.db.get_value( "Stock Ledger Entry", { "voucher_type": "Stock Reconciliation", "voucher_no": stock_reco.name })) self.assertFalse( get_stock_and_account_difference( ["_Test Account Stock In Hand - _TC"])) stock_reco.cancel() self.assertFalse( dataent.db.get_value( "Stock Ledger Entry", { "voucher_type": "Stock Reconciliation", "voucher_no": stock_reco.name })) self.assertFalse( dataent.db.get_value( "GL Entry", { "voucher_type": "Stock Reconciliation", "voucher_no": stock_reco.name })) set_perpetual_inventory(0)