Sha256: bc5484e7dadd51c2052c762628ac771c848b782afbdbd075573d4e1c05bff99c

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module Skr

    class Handlers::Sales < Lanes::API::ControllerBase

        def create

            customer = if data['customer']
                           Skr::Customer.find_by_code(data['customer'])
                       else
                           Skr::Customer.joins(:billing_address)
                               .merge( Skr::Address.where( data['billing_address'] ) ).first
                       end
            unless customer
                customer = Skr::Customer.create!(
                    name: data['billing_address']['name'],
                    billing_address_attributes: data['billing_address']
                )
            end

            invoice = Skr::Invoice.new(
                customer: customer,
                billing_address_attributes: data['billing_address']
            )
            invoice.location = data['location'] ? Skr::Location.find_by_code(data['location']) :
                                   Skr::Location.default
            %w{form options}.each do | attr |
                invoice[attr] = data[attr] if data[attr]
            end

            (data['skus'] || []).each do | l |
                sku_loc = Skr::SkuLoc
                            .where({ location: invoice.location, sku_id: l['sku_id'] })
                            .first
                invoice.lines << Skr::InvLine.new({
                    sku_loc: sku_loc, qty: l['qty']
                })
            end

            std_api_reply(:create, invoice, methods: 'total', success: invoice.save)
        end

    end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stockor-0.5.0 lib/skr/handlers/sales.rb
stockor-0.4.0 lib/skr/handlers/sales.rb