Beispiel #1
0
    def validate_item(self, item_code, row_num):
        from epaas.stock.doctype.item.item import validate_end_of_life, \
         validate_is_stock_item, validate_cancelled_item

        # using try except to catch all validation msgs and display together

        try:
            item = dataent.get_doc("Item", item_code)

            # end of life and stock item
            validate_end_of_life(item_code,
                                 item.end_of_life,
                                 item.disabled,
                                 verbose=0)
            validate_is_stock_item(item_code, item.is_stock_item, verbose=0)

            # item should not be serialized
            if item.has_serial_no == 1:
                raise dataent.ValidationError(
                    _("Serialized Item {0} cannot be updated using Stock Reconciliation, please use Stock Entry"
                      ).format(item_code))

            # item managed batch-wise not allowed
            if item.has_batch_no == 1:
                raise dataent.ValidationError(
                    _("Batched Item {0} cannot be updated using Stock Reconciliation, instead use Stock Entry"
                      ).format(item_code))

            # docstatus should be < 2
            validate_cancelled_item(item_code, item.docstatus, verbose=0)

        except Exception as e:
            self.validation_messages.append(
                _("Row # ") + ("%d: " % (row_num)) + cstr(e))
Beispiel #2
0
    def check_docstatus_transition(self, docstatus):
        """Ensures valid `docstatus` transition.
		Valid transitions are (number in brackets is `docstatus`):

		- Save (0) > Save (0)
		- Save (0) > Submit (1)
		- Submit (1) > Submit (1)
		- Submit (1) > Cancel (2)

		"""
        if not self.docstatus:
            self.docstatus = 0
        if docstatus == 0:
            if self.docstatus == 0:
                self._action = "save"
            elif self.docstatus == 1:
                self._action = "submit"
                self.check_permission("submit")
            else:
                raise dataent.DocstatusTransitionError(
                    _("Cannot change docstatus from 0 to 2"))

        elif docstatus == 1:
            if self.docstatus == 1:
                self._action = "update_after_submit"
                self.check_permission("submit")
            elif self.docstatus == 2:
                self._action = "cancel"
                self.check_permission("cancel")
            else:
                raise dataent.DocstatusTransitionError(
                    _("Cannot change docstatus from 1 to 0"))

        elif docstatus == 2:
            raise dataent.ValidationError(_("Cannot edit cancelled document"))
Beispiel #3
0
 def get_access_token(self):
     if not self.refresh_token:
         raise dataent.ValidationError(
             _("Google GSuite is not configured."))
     data = {
         'client_id':
         self.client_id,
         'client_secret':
         self.get_password(fieldname='client_secret',
                           raise_exception=False),
         'refresh_token':
         self.get_password(fieldname='refresh_token',
                           raise_exception=False),
         'grant_type':
         "refresh_token",
         'scope':
         SCOPES
     }
     try:
         r = requests.post('https://www.googleapis.com/oauth2/v4/token',
                           data=data).json()
     except requests.exceptions.HTTPError:
         dataent.throw(
             _("Something went wrong during the token generation. Please request again an authorization code."
               ))
     return r.get('access_token')
Beispiel #4
0
	def get_next_mapping_name(self):
		mappings = [m for m in self.get_plan().mappings if m.enabled]
		if not self.current_mapping:
			# first
			return mappings[0].mapping
		for i, d in enumerate(mappings):
			if i == len(mappings) - 1:
				# last
				return None
			if d.mapping == self.current_mapping:
				return mappings[i+1].mapping

		raise dataent.ValidationError('Mapping Broken')
	def before_insert(self):
		if dataent.db.count("Translated Message", {"source": self.source, "language":self.language}):
			raise dataent.ValidationError("Translated Message for this source message already exists")
Beispiel #6
0
    def validate_data(self):
        def _get_msg(row_num, msg):
            return _("Row # {0}: ").format(row_num + 1) + msg

        self.validation_messages = []
        item_warehouse_combinations = []

        default_currency = dataent.db.get_default("currency")

        for row_num, row in enumerate(self.items):
            # find duplicates
            if [row.item_code, row.warehouse] in item_warehouse_combinations:
                self.validation_messages.append(
                    _get_msg(row_num, _("Duplicate entry")))
            else:
                item_warehouse_combinations.append(
                    [row.item_code, row.warehouse])

            self.validate_item(row.item_code, row_num + 1)

            # validate warehouse
            if not dataent.db.get_value("Warehouse", row.warehouse):
                self.validation_messages.append(
                    _get_msg(row_num, _("Warehouse not found in the system")))

            # if both not specified
            if row.qty in ["", None] and row.valuation_rate in ["", None]:
                self.validation_messages.append(
                    _get_msg(
                        row_num,
                        _("Please specify either Quantity or Valuation Rate or both"
                          )))

            # do not allow negative quantity
            if flt(row.qty) < 0:
                self.validation_messages.append(
                    _get_msg(row_num, _("Negative Quantity is not allowed")))

            # do not allow negative valuation
            if flt(row.valuation_rate) < 0:
                self.validation_messages.append(
                    _get_msg(row_num,
                             _("Negative Valuation Rate is not allowed")))

            if row.qty and row.valuation_rate in ["", None]:
                row.valuation_rate = get_stock_balance(
                    row.item_code,
                    row.warehouse,
                    self.posting_date,
                    self.posting_time,
                    with_valuation_rate=True)[1]
                if not row.valuation_rate:
                    # try if there is a buying price list in default currency
                    buying_rate = dataent.db.get_value(
                        "Item Price", {
                            "item_code": row.item_code,
                            "buying": 1,
                            "currency": default_currency
                        }, "price_list_rate")
                    if buying_rate:
                        row.valuation_rate = buying_rate

                    else:
                        # get valuation rate from Item
                        row.valuation_rate = dataent.get_value(
                            'Item', row.item_code, 'valuation_rate')

        # throw all validation messages
        if self.validation_messages:
            for msg in self.validation_messages:
                msgprint(msg)

            raise dataent.ValidationError(self.validation_messages)
Beispiel #7
0
def _msgprint(msg, verbose):
    if verbose:
        msgprint(msg, raise_exception=True)
    else:
        raise dataent.ValidationError(msg)