dumadi
what am I doing wrong here? Decimal doesn’t’ seem to be working!
schema "purchase_order" do
embeds_many(:purchase_order_item, PurchaseOrderItem, on_replace: :delete)
field(:po_no, :integer, read_after_writes: true)
field(:tax, :float, read_after_writes: true)
field(:total, :decimal, read_after_writes: true)
field(:delivery_charges, :decimal, read_after_writes: true)
field(:approved, :boolean, read_after_writes: true)
field(:date_approved, :utc_datetime, read_after_writes: true)
belongs_to(:distribution_centre, DistributionCentre)
belongs_to(:supplier, Supplier)
timestamps(type: :utc_datetime, read_after_writes: true)
end
@doc false
def changeset(purchase_order, attrs) do
purchase_order
|> cast(attrs, [
:distribution_centre_id,
:supplier_id,
:approved,
:delivery_charges,
:po_no,
:tax,
:total,
:date_approved
])
|> cast_embed(:purchase_order_item, required: true, with: &PurchaseOrderItem.changeset/2)
|> set_total
|> validate_inclusion(:po_no, generate_po_code())
|> validate_required([:distribution_centre_id, :approved])
|> unique_constraint(:po_no, name: :purchase_order_po_no_index)
|> foreign_key_constraint(:distribution_centre_id)
|> foreign_key_constraint(:supplier_id, name: :purchase_order_supplier_id_fkey)
end
defp set_total(changeset) do
purchase_order_items = get_field(changeset, :purchase_order_item)
total =
Enum.reduce(purchase_order_items, Decimal.new(0), fn items, acc ->
taxes = get_field(changeset, :tax) |> Decimal.from_float()
delivery_charges = get_field(changeset, :delivery_charges)
tax_and_delivery = Decimal.add(taxes, delivery_charges)
Decimal.add(tax_and_delivery, Decimal.add(acc, items.total))
end)
changeset
|> put_change(:total, total)
end
defp generate_po_code(length \\ 10) do
code =
10
|> :math.pow(length)
|> round()
|> :rand.uniform()
|> Integer.to_string()
|> String.pad_leading(length, "0")
{:ok, code}
end
And this is the embedded schema.
embedded_schema do
field(:item_name, :string, read_after_writes: true)
field(:description, :string, read_after_writes: true)
field(:quantity, :integer, read_after_writes: true)
field(:unit_price, :decimal, read_after_writes: true)
field(:total, :decimal, read_after_writes: true)
field(:delete, :boolean, virtual: true)
timestamps(type: :utc_datetime, read_after_writes: true)
end
@doc false
def changeset(%PurchaseOrderItem{} = purchase_order_item, attrs) do
purchase_order_item
|> cast(attrs, [
:item_name,
:description,
:total,
:quantity,
:unit_price,
:delete
])
|> set_delete
|> set_total
|> validate_required([:item_name, :description, :quantity, :unit_price])
end
defp set_total(changeset) do
quantity = get_field(changeset, :quantity) |> Decimal.new()
unit_price = get_field(changeset, :unit_price)
quantity_and_price = Decimal.mult(quantity, unit_price)
changeset
|> put_change(:total, quantity_and_price)
end
defp set_delete(changeset) do
if get_change(changeset, :delete) do
%{changeset | action: :delete}
else
changeset
end
end
Trending in Questions
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #elixirconf-us
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
axelson
What is the incorrect behavior that you are seeing?
dumadi
I am still trying to wrap my hands around Ecto as newbie Elixir Programmer. Thanks Axelson for your response I got it to work
axelson
I’m glad that you got it to work!