Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ofelix03/1eee75812b0ef2793dac99cea934ef79 to your computer and use it in GitHub Desktop.

Select an option

Save ofelix03/1eee75812b0ef2793dac99cea934ef79 to your computer and use it in GitHub Desktop.
refactor-function-with-high-code-cognitive-complexity
# this method was extract from a cheque class
def create(self, vals):
if "amount" not in vals:
raise ValidationError(_("Cheque amount must be provided"))
if "amount" in vals and vals["amount"] <= 0:
raise ValidationError(_("Cheque amount must be greater than zero"))
if "special_clearing" in vals and vals["special_clearing"]:
vals["special_note"] = "Special Clearing"
if "internal_hold" in vals and vals["internal_hold"]:
vals["state"] = "internally_held"
elif "internal_hold" in vals and not vals["internal_hold"]:
vals["state"] = "draft"
else:
vals["state"] = "draft"
if "acc_no" in vals and vals["acc_no"]:
if "branch_code" in vals:
cheques = self.search(
[
("reference", "=", vals["reference"]),
("acc_no", "=", vals["acc_no"]),
("branch_code", "=", vals["branch_code"]),
("state", "!=", "cancelled"),
]
)
if cheques:
raise ValidationError(
_("This cheque/Slip has already been recorded")
)
partner_account = self._model_partner_cheque_account().search(
[
("acc_no", "=", vals["acc_no"]),
("branch_id", "=", vals["branch"]),
]
)
if not partner_account:
partner_account.create(
{
"partner": vals["partner"],
"branch_id": vals["branch"],
"acc_no": vals["acc_no"],
"currency_id": vals["currency_id"],
}
)
else:
cheques = self.search(
[
("reference", "=", vals["reference"]),
("acc_no", "=", vals["acc_no"]),
("state", "!=", "cancelled"),
]
)
if cheques:
raise ValidationError(
_("This cheque/Slip has already been recorded")
)
res = super(QuantumCheque, self).create(vals)
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment