diff options
-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 |