diff options
author | HombreLaser <sebastian-440@live.com> | 2023-04-16 11:29:32 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2023-04-16 11:29:32 -0600 |
commit | 3c76bc939f44614850898bcf1e5086f997d94dcf (patch) | |
tree | a37db998dded91dc6daa96e705b812f4d59895f1 /app/services/payment_services | |
parent | 9607fcb5a9cdc7595633336881cc0dd8aa9fb8ca (diff) |
Refactoriza PaymentService
Diffstat (limited to 'app/services/payment_services')
-rw-r--r-- | app/services/payment_services/payment_service.rb | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/app/services/payment_services/payment_service.rb b/app/services/payment_services/payment_service.rb index 82267d5..32f6d94 100644 --- a/app/services/payment_services/payment_service.rb +++ b/app/services/payment_services/payment_service.rb @@ -23,24 +23,30 @@ module PaymentServices return end + update_products_available_quantity + update_order + end + + private + + def update_products_available_quantity + @order.products.joins(:product_orders).select('products.*, product_orders.quantity AS bought_quantity').distinct + .find_each do |product| + product.update(available_quantity: product.available_quantity - product.bought_quantity) + end + end + + def update_order @order.update(payment_id: @payment.id) @user_account.cart.product_carts.destroy_all @order end - private - def total total = 0 - @order.product_orders.joins(:product).select( - 'product_orders.*, products.bulk_price AS bulk_price, products.unitary_price AS unitary_price' - ).each do |product_order| - total += if product_order.quantity < 5 - product_order.unitary_price * product_order.quantity - else - product_order.bulk_price * product_order.quantity - end + @order.product_orders.includes(:product).find_each do |product_order| + total += product_order.total end total end |