Class: Admin::TransporterRulesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/admin/transporter_rules_controller.rb

Instance Method Summary collapse

Instance Method Details

#activateObject



165
166
167
# File 'app/controllers/admin/transporter_rules_controller.rb', line 165

def activate
  render :text => @transporter.activate
end

#categoriesObject



19
20
21
22
23
24
25
26
# File 'app/controllers/admin/transporter_rules_controller.rb', line 19

def categories
  respond_to do |format|
    format.json do
      # list categories like a tree
      render :json => GeoZone.find_all_by_parent_id(nil, :order => 'printable_name').collect{ |g| g.to_jstree(:transporter_rules)}.to_json
    end
  end
end

#createObject



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
# File 'app/controllers/admin/transporter_rules_controller.rb', line 39

def create
  result = true

  @shipping_methods = params[:shipping_method]

  @shipping_methods.each do |shipping_method|
    new_shipping_method = defined?(@parent_id) ? TransporterRule.new : TransporterRule.new(params[:transporter_rule])

    rule_condition = build_conditions(shipping_method)

    new_shipping_method.conditions = "[#{rule_condition.join(', ')}]"
    new_shipping_method.variables = shipping_method[1][:price][0]

    result = new_shipping_method.save

    unless defined?(@parent_id)
      @parent_id = new_shipping_method.id
    else
      new_shipping_method.update_attribute(:parent_id, @parent_id)
    end
  end

  if result
    flash[:notice] = I18n.t('transporter.create.success').capitalize
    render :action => :edit
  else
    flash[:error] = I18n.t('transporter.create.failed').capitalize
    render :action => :new
  end
end

#destroyObject



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/controllers/admin/transporter_rules_controller.rb', line 151

def destroy
  if @transporter.destroy
    flash[:notice] = I18n.t('transporter.destroy.success').capitalize
  else
    flash[:error] = I18n.t('transporter.destroy.failed').capitalize
  end
  respond_to do |wants|
    wants.html do
      redirect_to([forgeos_commerce, :admin, :transporters])
    end
    wants.js
  end
end

#duplicateObject



34
35
36
37
# File 'app/controllers/admin/transporter_rules_controller.rb', line 34

def duplicate
  @transporter = @transporter.clone
  render :action => 'new'
end

#editObject



70
71
# File 'app/controllers/admin/transporter_rules_controller.rb', line 70

def edit
end

#indexObject



9
10
11
12
13
14
15
16
17
# File 'app/controllers/admin/transporter_rules_controller.rb', line 9

def index
  respond_to do |format|
    format.html
    format.json do
      sort
      render :layout => false
    end
  end
end

#newObject



31
32
# File 'app/controllers/admin/transporter_rules_controller.rb', line 31

def new
end

#showObject



28
29
# File 'app/controllers/admin/transporter_rules_controller.rb', line 28

def show
end

#updateObject

TODO Fix bug when deleting the two (or more) first rules, the first isn’t destroy, but the second (& others) yes | Check order of index in the hash params



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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/admin/transporter_rules_controller.rb', line 74

def update

  result = true
  parent_transporter_deleted = false
  @parent_id = @transporter.id
  @transporter.update_attributes(params[:transporter_rule])
  # Get rules to delete
  @transporters_to_delete = params[:shipping_methods_to_delete]
  unless @transporters_to_delete.nil?
    @transporters_to_delete.each do |item_id|
        transporter = TransporterRule.find_by_id(item_id)

        if transporter == @transporter
          parent_transporter_deleted = true
          @transporter_name = transporter.name
          @transporter_description = transporter.description
        else
          parent_transporter_deleted = false
          transporter.destroy
        end
    end
  end

  # Get new & existing rules, create or update
  @shipping_methods = params[:shipping_method]

  @shipping_methods.each do |shipping_method|

    rule_condition = build_conditions shipping_method
    transporter_id = shipping_method[0]

    # If rule exists
    if new_shipping_method = TransporterRule.find_by_id(transporter_id)
      result = new_shipping_method.update_attribute(:conditions, "[#{rule_condition.join(', ')}]")
      result = new_shipping_method.update_attribute(:variables, shipping_method[1][:price][0])
      # Get the new parent_id & update name/description to this one
      check_parent_transporter parent_transporter_deleted, shipping_method, new_shipping_method

      result = new_shipping_method.update_attribute(:parent_id, @parent_id) if new_shipping_method.id != @parent_id

    else

      # If delivery type change
      if @delivery_type != params[:delivery_type] && shipping_method == @shipping_methods.first
        new_shipping_method = TransporterRule.find_by_id(@parent_id)
        result = new_shipping_method.update_attribute(:conditions, "[#{rule_condition.join(', ')}]")
        result = new_shipping_method.update_attribute(:variables, shipping_method[1][:price][0])
        result = new_shipping_method.update_attribute(:parent_id, @parent_id)

      else
        new_shipping_method = TransporterRule.new

        new_shipping_method.conditions = "[#{rule_condition.join(', ')}]"
        new_shipping_method.variables = shipping_method[1][:price][0]

        result = new_shipping_method.save

        # Get the new parent_id & update name/description to this one
        check_parent_transporter parent_transporter_deleted, shipping_method, new_shipping_method
        result = new_shipping_method.update_attribute(:parent_id, @parent_id)

      end
    end
  end

  # Destroy the parent transporter at the end
  @transporter.destroy if parent_transporter_deleted

  if result
    flash[:notice] = I18n.t('transporter.update.success').capitalize
    redirect_to :action => :edit
  else
    flash[:error] = I18n.t('transporter.update.failed').capitalize
    render :action => :edit
  end
end