<commit_before>defmodule ExMoney.Transaction do
  use ExMoney.Web, :model

  alias ExMoney.Transaction

  import Ecto.Query

  schema "transactions" do
    field :saltedge_transaction_id, :integer
    field :mode, :string
    field :status, :string
    field :made_on, Ecto.Date
    field :amount, :decimal
    field :currency_code, :string
    field :description, :string
    field :duplicated, :boolean, default: false

    has_one :transaction_info, ExMoney.TransactionInfo, on_delete: :delete_all
    belongs_to :category, ExMoney.Category
    belongs_to :user, ExMoney.User
    belongs_to :account, ExMoney.Account
    belongs_to :saltedge_account, ExMoney.Account,
      foreign_key: :saltedge_account_id,
      references: :saltedge_account_id

    timestamps
  end

  @required_fields ~w(
    saltedge_transaction_id
    mode
    status
    made_on
    amount
    currency_code
    description
    duplicated
    saltedge_account_id
    account_id
    user_id
  )
  @optional_fields ~w(category_id)

  def changeset(model, params \\ %{}) do
    model
    |> cast(params, @required_fields, @optional_fields)
  end

  def changeset_custom(model, params \\ %{}) do
    model
    |> cast(params, ~w(amount category_id account_id made_on user_id), ~w(description ))
    |> negate_amount(params)
  end

  def update_changeset(model, params \\ %{}) do
    model
    |> cast(params, ~w(), ~w(category_id description))
  end

  def negate_amount(changeset, %{}), do: changeset
  def negate_amount(changeset, %{"type" => "income"}), do: changeset
<commit_msg>Fix creation of the cash transaction<commit_after>defmodule ExMoney.Transaction do
  use ExMoney.Web, :model

  alias ExMoney.Transaction

  import Ecto.Query

  schema "transactions" do
    field :saltedge_transaction_id, :integer
    field :mode, :string
    field :status, :string
    field :made_on, Ecto.Date
    field :amount, :decimal
    field :currency_code, :string
    field :description, :string
    field :duplicated, :boolean, default: false

    has_one :transaction_info, ExMoney.TransactionInfo, on_delete: :delete_all
    belongs_to :category, ExMoney.Category
    belongs_to :user, ExMoney.User
    belongs_to :account, ExMoney.Account
    belongs_to :saltedge_account, ExMoney.Account,
      foreign_key: :saltedge_account_id,
      references: :saltedge_account_id

    timestamps
  end

  @required_fields ~w(
    saltedge_transaction_id
    mode
    status
    made_on
    amount
    currency_code
    description
    duplicated
    saltedge_account_id
    account_id
    user_id
  )
  @optional_fields ~w(category_id)

  def changeset(model, params \\ %{}) do
    model
    |> cast(params, @required_fields, @optional_fields)
  end

  def changeset_custom(model, params \\ %{}) do
    model
    |> cast(params, ~w(amount category_id account_id made_on user_id), ~w(description ))
    |> negate_amount(params)
  end

  def update_changeset(model, params \\ %{}) do
    model
    |> cast(params, ~w(), ~w(category_id description))
  end

  def negate_amount(changeset, params) when params == %{}, do: changeset
  def negate_amount(changeset, %{"type" => "income"}), do: changeset

{'fraction_non_alphanumeric': 0.096232985121874, 'fraction_numerical': 0.0, 'mean_word_length': 3.688427299703264, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '742069', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1005, 'n_words': 248}