Module: Rawbotz::RawbotzApp::Routing::Orders

Includes:
RawgentoModels
Defined in:
lib/rawbotz/routes/orders.rb

Defined Under Namespace

Modules: Stock

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rawbotz/routes/orders.rb', line 7

def self.registered(app)
  # app.get '/orders',             &show_orders
  show_orders = lambda do
    @orders = Order.order(created_at: :desc).all
    haml "orders/index".to_sym
  end

  # app.get '/order/new',          &create_order
  create_order = lambda do
    if settings.supplier.orders.where(state: 'new').present?
      add_flash :message, "Already one Order in progress"
      @order = settings.supplier.orders.where(state: 'new').first
    else
      @order = Order.create(state: :new)
      @order.supplier     = settings.supplier
      @order.order_method = :magento

      # There is hell a lot of products missing if with remote order
      RawgentoDB::Query.understocked.each do |product_id, name, min_qty, stock|
        local_product = LocalProduct.find_by(product_id: product_id)
        if local_product.present? && local_product.supplier == settings.supplier
          @order.order_items.create(local_product: local_product,
                                    current_stock: stock,
                                    min_stock: min_qty)
        end
        # else none of our business
      end
      @order.save
      add_flash :success, "New Order created"
    end
    redirect "/order/#{@order.id}"
  end

  # app.get '/order/:id',          &show_order
  show_order = lambda do
    @order = Order.find(params[:id])
    @stock = {}
    begin
      RawgentoDB::Query.stock.each {|s| @stock[s.product_id] = s.qty}
    rescue Exception => e
      add_flash :error, "Cannot connect to MySQL database (#{e.message})"
    end
    haml 'order/view'.to_sym
  end

  # app.post '/order/:id/',         &act_on_order
  act_on_order = lambda do
    @order = Order.find(params[:id])
    params.each do |k,v|
      if k && k.start_with?("item_") && v != ""
        OrderItem.find(k[5..-1]).update(num_wished: v.to_i)
      end
    end
    @order.internal_comment = params[:internal_comment]
    @order.save
    if params['action'] == 'order'
      @order.update(state: :queued)
      add_flash :success, 'Order queued'
      redirect '/orders'
    elsif params['action'] == 'delete'
      @order.update(state: :deleted)
      add_flash :success, 'Order marked deleted'
      redirect '/orders'
    else
      @stock = {}
      begin
        RawgentoDB::Query.stock.each {|s| @stock[s.product_id] = s.qty}
      rescue Exception => e
        add_flash :error, "Cannot connect to MySQL database (#{e.message})"
      end
      haml "order/view".to_sym
    end
  end

  # app.get '/order/:id/packlist', &show_order_packlist
  show_order_packlist = lambda do
    @order = Order.find(params[:id])
    @orphans = []
    @refunds = {}
    if @order.supplier == settings.supplier && @order.remote_order_link.present?
      order_linker = Rawbotz::OrderLinker.new @order
      order_linker.link!
      @orphans = order_linker.orphans
      @refunds = order_linker.refunds
    end
    haml "order/packlist".to_sym
  end

  # app.get  '/order/:id/packlist/pdf', &show_pdf_packlist
  show_pdf_packlist = lambda do
    attachment "packlist_order.pdf"
    @order = Order.find(params[:id])
    html = haml "order/packlist.pdf".to_sym, layout: :pdf_layout
    PDFKit.configure do |config|
      config.verbose = false
      config.default_options[:quiet] = true
    end

    kit = PDFKit.new(html)

    kit.stylesheets << File.join(settings.root, "public",
                                 "pure-min.css")
    kit.stylesheets << File.join(settings.root, "public",
                                 "rawbotz.css")
    # This would need more resources (base64 encoded woff?)
    #kit.stylesheets << File.join(settings.root, "public",
    #                             "font-awesome-4.5.0/css/font-awesome.min.css")
    kit.stylesheets << File.join(settings.root, "public",
                                 "jui/jquery-ui.min.css")
    kit.to_pdf
  end

  # routes
  app.get  '/orders',             &show_orders
  app.get  '/order/new',          &create_order
  app.get  '/order/:id',          &show_order
  app.post '/order/:id',          &act_on_order
  app.get  '/order/:id/packlist', &show_order_packlist
  app.get  '/order/:id/packlist/pdf', &show_pdf_packlist
end