购物车商品服务费计算规则 算法购物车
程序员文章站
2022-07-12 20:19:09
...
购物车商品服务费计算规则: 总服务费 = order_items明细费用加和 + order服务费用 order服务费用(Bank Charge,Shipping Fee) order_items服务费(Handling Fee,Global Shipping,Frontend QC,HNMR 等) order服务费用由当前选中cart_item按kg数量转换统计重量,选取区间价格写入order_servers order_items服务费,遍历cart_item的服务费写入order_servers 需要注意的是: 1,服务费没有记录到数据库之前,实际运算为准,记录到数据库后,以数据库中数值为准。(防止算法的改变影响历史数据) 2,购物车多件数费用= (单件成本+ 单件服务费)* 件数 - (单件成本 * 件数) - 实际服务费 3,多件数的实际服务费,例如算国际运费的时候,服务费是总重量重新计算的服务费。 4, 前台购物车是,总服务费不可编辑,所以展示的时候和create的时候都要单独调init_with_default_serves去算费用(不能在前台有可编辑的地方);后台则需要可编辑费用,编辑后存储,取存储值,没编辑前取算法默认值。 路径:app/controllers/my_ichemical/orders_controller.rb action:new 订单创建时初始化: @order.migrate_from_cart_items 路径:app/models/orders/order.rb def migrate_from_cart_items(cart_item_ids: nil) cart_items = user.cart_items.checked.default_where(myself: self.myself) cart_items.each do |cart_item| self.order_items.build cart_item_id: cart_item.id, good_type: cart_item.good_type, good_id: cart_item.good_id, quantity: cart_item.quantity end init_with_default_serves end def init_with_default_serves(cart_item_ids: nil) summary = CartItem.checked_items(user_id: self.user_id, myself: self.myself, extra: self.extra) summary.promote_charges.each do |promote_charge| self.order_promotes.build(promote_charge_id: promote_charge.id, promote_id: promote_charge.promote_id, amount: promote_charge.subtotal) end # 总订单的服务费根据重量算在总单子上 summary.serve_charges.each do |serve_charge| self.order_serves.build(serve_charge_id: serve_charge.id, serve_id: serve_charge.serve_id, amount: serve_charge.subtotal) end compute_sum end 路径:app/models/cart_item.rb def self.checked_items(user_id: nil, buyer_id: nil, session_id: nil, myself: nil, extra: {}) if user_id @checked_items = CartItem.default_where(user_id: user_id, myself: myself).pending.checked buyer_id = User.find(user_id).buyer_id puts "-----> Checked User: #{user_id}" elsif buyer_id @checked_items = CartItem.default_where(buyer_id: buyer_id, myself: myself).pending.checked puts "-----> Checked Buyer: #{buyer_id}" elsif session_id @checked_items = CartItem.default_where(session_id: session_id, myself: myself).pending.checked puts "-----> Checked Session: #{session_id}" else @checked_items = CartItem.none puts "-----> Checked None!" end SummaryService.new(@checked_items, buyer_id: buyer_id, extra: extra) end 路径:app/models/concerns/summary_service.rb def compute_serve @serve_charges = [] QuantityServe.total.overall.each do |serve| charge = serve.compute_price(total_quantity, extra) @serve_charges << charge end @serve_price = checked_items.sum { |cart_item| cart_item.serve_price } @total_serve_price = serve_charges.sum { |i| i.subtotal } end 路径:app/models/serve.rb def compute_price(amount, extra_hash = {}) extra_hash.stringify_keys! if self.contain_max range = { 'min-lte': amount.to_d, 'max-gte': amount.to_d } else range = { 'min-lte': amount.to_d, 'max-gt': amount.to_d } end query = range.merge(extra_hash.slice(*extra)) charge = self.charges.default_where(query).first if charge && !(amount.to_d == 0 && self.id == 5) charge.subtotal = charge.final_price(amount) charge.default_subtotal = charge.subtotal else charge = self.charges.build charge.subtotal = 0 end charge end 路径:app/models/orders/order_item.rb # 子订单服务费在order_items创建时回调 cart_item.serve_charges.each do |serve_charge| op = self.order_serves.build(serve_charge_id: serve_charge.id, serve_id: serve_charge.serve_id, amount: serve_charge.subtotal) op.order = self.order end
上一篇: 开始使用Gradle