Exemplo n.º 1
0
def validate_currency(args, item, meta=None):
	from webnotes.model.meta import get_field_precision
	if not meta:
		meta = webnotes.get_doctype(args.doctype)
		
	# validate conversion rate
	if meta.get_field("currency"):
		validate_conversion_rate(args.currency, args.conversion_rate, 
			meta.get_label("conversion_rate"), args.company)
		
		# round it
		args.conversion_rate = flt(args.conversion_rate, 
			get_field_precision(meta.get_field("conversion_rate"), 
				webnotes._dict({"fields": args})))
	
	# validate price list conversion rate
	if meta.get_field("price_list_currency") and (args.selling_price_list or args.buying_price_list) \
		and args.price_list_currency:
		validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate, 
			meta.get_label("plc_conversion_rate"), args.company)
		
		# round it
		args.plc_conversion_rate = flt(args.plc_conversion_rate, 
			get_field_precision(meta.get_field("plc_conversion_rate"), 
				webnotes._dict({"fields": args})))
Exemplo n.º 2
0
def validate_currency(args, item, meta=None):
    from webnotes.model.meta import get_field_precision
    if not meta:
        meta = webnotes.get_doctype(args.doctype)

    # validate conversion rate
    if meta.get_field("currency"):
        validate_conversion_rate(args.currency, args.conversion_rate,
                                 meta.get_label("conversion_rate"),
                                 args.company)

        # round it
        args.conversion_rate = flt(
            args.conversion_rate,
            get_field_precision(meta.get_field("conversion_rate"),
                                webnotes._dict({"fields": args})))

    # validate price list conversion rate
    if meta.get_field("price_list_currency") and (args.selling_price_list or args.buying_price_list) \
     and args.price_list_currency:
        validate_conversion_rate(args.price_list_currency,
                                 args.plc_conversion_rate,
                                 meta.get_label("plc_conversion_rate"),
                                 args.company)

        # round it
        args.plc_conversion_rate = flt(
            args.plc_conversion_rate,
            get_field_precision(meta.get_field("plc_conversion_rate"),
                                webnotes._dict({"fields": args})))
Exemplo n.º 3
0
    def precision(self, fieldname, parentfield=None):
        parentfield = self._process(parentfield)

        if not hasattr(self, "_precision"):
            self._precision = webnotes._dict({
                "default":
                cint(webnotes.conn.get_default("float_precision")) or 3,
                "options": {}
            })

        if self._precision.setdefault(parentfield or "main",
                                      {}).get(fieldname) is None:
            df = self.meta.get_field(fieldname, parentfield=parentfield)

            if df.fieldtype == "Currency" and df.options and not self._precision.options.get(
                    df.options):
                self._precision.options[df.options] = get_field_precision(
                    df, self.doc)

            if df.fieldtype == "Currency":
                self._precision[parentfield or "main"][fieldname] = cint(self._precision.options.get(df.options)) or \
                 self._precision.default
            elif df.fieldtype == "Float":
                self._precision[parentfield
                                or "main"][fieldname] = self._precision.default

        return self._precision[parentfield or "main"][fieldname]
Exemplo n.º 4
0
	def precision(self, fieldname, parentfield=None):
		parentfield = self._process(parentfield)
		
		if not hasattr(self, "_precision"):
			self._precision = webnotes._dict({
				"default": cint(webnotes.conn.get_default("float_precision")) or 6,
				"options": {}
			})
		
		if self._precision.setdefault(parentfield or "main", {}).get(fieldname) is None:
			df = self.meta.get_field(fieldname, parentfield=parentfield)
			
			if df.fieldtype == "Currency" and df.options and not self._precision.options.get(df.options):
				self._precision.options[df.options] = get_field_precision(df, self.doc)
			
			self._precision[parentfield or "main"][fieldname] = cint(self._precision.options.get(df.options)) or \
				self._precision.default
		
		return self._precision[parentfield or "main"][fieldname]
Exemplo n.º 5
0
def update_entries_after(args, verbose=1):
	"""
		update valution rate and qty after transaction 
		from the current time-bucket onwards
		
		args = {
			"item_code": "ABC",
			"warehouse": "XYZ",
			"posting_date": "2012-12-12",
			"posting_time": "12:00"
		}
	"""
	if not _exceptions:
		webnotes.local.stockledger_exceptions = []
	
	previous_sle = get_sle_before_datetime(args)
	
	qty_after_transaction = flt(previous_sle.get("qty_after_transaction"))
	valuation_rate = flt(previous_sle.get("valuation_rate"))
	stock_queue = json.loads(previous_sle.get("stock_queue") or "[]")
	stock_value = flt(previous_sle.get("stock_value"))
	prev_stock_value = flt(previous_sle.get("stock_value"))
	
	entries_to_fix = get_sle_after_datetime(previous_sle or \
		{"item_code": args["item_code"], "warehouse": args["warehouse"]}, for_update=True)

	valuation_method = get_valuation_method(args["item_code"])
	stock_value_difference = 0.0

	for sle in entries_to_fix:
		if sle.serial_no or not cint(webnotes.conn.get_default("allow_negative_stock")):
			# validate negative stock for serialized items, fifo valuation 
			# or when negative stock is not allowed for moving average
			if not validate_negative_stock(qty_after_transaction, sle):
				qty_after_transaction += flt(sle.actual_qty)
				continue

		if sle.serial_no:
			valuation_rate = get_serialized_values(qty_after_transaction, sle, valuation_rate)
		elif valuation_method == "Moving Average":
			valuation_rate = get_moving_average_values(qty_after_transaction, sle, valuation_rate)
		else:
			valuation_rate = get_fifo_values(qty_after_transaction, sle, stock_queue)
				
		qty_after_transaction += flt(sle.actual_qty)
		
		# get stock value
		if sle.serial_no:
			stock_value = qty_after_transaction * valuation_rate
		elif valuation_method == "Moving Average":
			stock_value = (qty_after_transaction > 0) and \
				(qty_after_transaction * valuation_rate) or 0
		else:
			stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue))
		
		# rounding as per precision
		from webnotes.model.meta import get_field_precision
		meta = webnotes.get_doctype("Stock Ledger Entry")
		
		stock_value = flt(stock_value, get_field_precision(meta.get_field("stock_value"), 
			webnotes._dict({"fields": sle})))
		
		stock_value_difference = stock_value - prev_stock_value
		prev_stock_value = stock_value
			
		# update current sle
		webnotes.conn.sql("""update `tabStock Ledger Entry`
			set qty_after_transaction=%s, valuation_rate=%s, stock_queue=%s,
			stock_value=%s, stock_value_difference=%s where name=%s""", 
			(qty_after_transaction, valuation_rate,
			json.dumps(stock_queue), stock_value, stock_value_difference, sle.name))
	
	if _exceptions:
		_raise_exceptions(args, verbose)
	
	# update bin
	if not webnotes.conn.exists({"doctype": "Bin", "item_code": args["item_code"], 
			"warehouse": args["warehouse"]}):
		bin_wrapper = webnotes.bean([{
			"doctype": "Bin",
			"item_code": args["item_code"],
			"warehouse": args["warehouse"],
		}])
		bin_wrapper.ignore_permissions = 1
		bin_wrapper.insert()
	
	webnotes.conn.sql("""update `tabBin` set valuation_rate=%s, actual_qty=%s,
		stock_value=%s, 
		projected_qty = (actual_qty + indented_qty + ordered_qty + planned_qty - reserved_qty)
		where item_code=%s and warehouse=%s""", (valuation_rate, qty_after_transaction,
		stock_value, args["item_code"], args["warehouse"]))
Exemplo n.º 6
0
def update_entries_after(args, verbose=1):
    """
		update valution rate and qty after transaction 
		from the current time-bucket onwards
		
		args = {
			"item_code": "ABC",
			"warehouse": "XYZ",
			"posting_date": "2012-12-12",
			"posting_time": "12:00"
		}
	"""
    if not _exceptions:
        webnotes.local.stockledger_exceptions = []

    previous_sle = get_sle_before_datetime(args)

    qty_after_transaction = flt(previous_sle.get("qty_after_transaction"))
    valuation_rate = flt(previous_sle.get("valuation_rate"))
    stock_queue = json.loads(previous_sle.get("stock_queue") or "[]")
    stock_value = flt(previous_sle.get("stock_value"))
    prev_stock_value = flt(previous_sle.get("stock_value"))

    entries_to_fix = get_sle_after_datetime(previous_sle or \
     {"item_code": args["item_code"], "warehouse": args["warehouse"]}, for_update=True)

    valuation_method = get_valuation_method(args["item_code"])
    stock_value_difference = 0.0

    for sle in entries_to_fix:
        if sle.serial_no or not cint(
                webnotes.conn.get_default("allow_negative_stock")):
            # validate negative stock for serialized items, fifo valuation
            # or when negative stock is not allowed for moving average
            if not validate_negative_stock(qty_after_transaction, sle):
                qty_after_transaction += flt(sle.actual_qty)
                continue

        if sle.serial_no:
            valuation_rate = get_serialized_values(qty_after_transaction, sle,
                                                   valuation_rate)
        elif valuation_method == "Moving Average":
            valuation_rate = get_moving_average_values(qty_after_transaction,
                                                       sle, valuation_rate)
        else:
            valuation_rate = get_fifo_values(qty_after_transaction, sle,
                                             stock_queue)

        qty_after_transaction += flt(sle.actual_qty)

        # get stock value
        if sle.serial_no:
            stock_value = qty_after_transaction * valuation_rate
        elif valuation_method == "Moving Average":
            stock_value = (qty_after_transaction > 0) and \
             (qty_after_transaction * valuation_rate) or 0
        else:
            stock_value = sum(
                (flt(batch[0]) * flt(batch[1]) for batch in stock_queue))

        # rounding as per precision
        from webnotes.model.meta import get_field_precision
        meta = webnotes.get_doctype("Stock Ledger Entry")

        stock_value = flt(
            stock_value,
            get_field_precision(meta.get_field("stock_value"),
                                webnotes._dict({"fields": sle})))

        stock_value_difference = stock_value - prev_stock_value
        prev_stock_value = stock_value

        # update current sle
        webnotes.conn.sql(
            """update `tabStock Ledger Entry`
			set qty_after_transaction=%s, valuation_rate=%s, stock_queue=%s,
			stock_value=%s, stock_value_difference=%s where name=%s""",
            (qty_after_transaction, valuation_rate, json.dumps(stock_queue),
             stock_value, stock_value_difference, sle.name))

    if _exceptions:
        _raise_exceptions(args, verbose)

    # update bin
    if not webnotes.conn.exists({
            "doctype": "Bin",
            "item_code": args["item_code"],
            "warehouse": args["warehouse"]
    }):
        bin_wrapper = webnotes.bean([{
            "doctype": "Bin",
            "item_code": args["item_code"],
            "warehouse": args["warehouse"],
        }])
        bin_wrapper.ignore_permissions = 1
        bin_wrapper.insert()

    webnotes.conn.sql(
        """update `tabBin` set valuation_rate=%s, actual_qty=%s,
		stock_value=%s, 
		projected_qty = (actual_qty + indented_qty + ordered_qty + planned_qty - reserved_qty)
		where item_code=%s and warehouse=%s""",
        (valuation_rate, qty_after_transaction, stock_value, args["item_code"],
         args["warehouse"]))