Module: ODDB::State::Admin::PackageMethods

Included in:
Package
Defined in:
src/state/admin/package.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) ajax_create_part



15
16
17
18
19
20
21
22
23
24
# File 'src/state/admin/package.rb', line 15

def ajax_create_part
  check_model
  parts = @model.parts.dup
  if(!error?)
    part = Persistence::CreateItem.new(@model.pointer + :part)
    part.carry(:registration, @model.registration)
    parts.push part
  end
  AjaxParts.new @session, parts
end

- (Object) ajax_delete_part



25
26
27
28
29
30
31
32
33
34
# File 'src/state/admin/package.rb', line 25

def ajax_delete_part
  check_model
  keys = [:pointer, :part]
  input = user_input(keys, keys)
  if(!error? \
     && (part = @model.parts[input[:part].to_i]))
    @session.app.delete part.pointer
  end
  AjaxParts.new(@session, @model.parts)
end

- (Object) check_model



35
36
37
38
39
40
41
# File 'src/state/admin/package.rb', line 35

def check_model
  if(@model.pointer != @session.user_input(:pointer))
    @errors.store :pointer, create_error(:e_state_expired, :pointer, nil)
  elsif !allowed?
    @errors.store :pointer, create_error(:e_not_allowed, :pointer, nil)
  end
end

- (Object) delete



42
43
44
45
46
47
48
# File 'src/state/admin/package.rb', line 42

def delete
  sequence = @model.parent(@session.app) 
  if(klass = resolve_state(sequence.pointer))
    @session.app.delete(@model.pointer)
    klass.new(@session, sequence)
  end
end

- (Object) update



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
# File 'src/state/admin/package.rb', line 49

def update
  ikscode = @session.user_input(:ikscd)
  error = if(ikscode.is_a? RuntimeError)
    ikscode
  elsif(ikscode.empty?)
    create_error(:e_missing_ikscd, :ikscd, ikscode)
  elsif((dup = @model.parent(@session.app).package(ikscode)) \
        && dup != @model)
    create_error(:e_duplicate_ikscd, :ikscd, ikscode)
  end
  if error
    @errors.store(:ikscd, error)
    if(@model.is_a? Persistence::CreateItem)
      @model.carry(:price_exfactory, 
        ODDB::Package.price_internal(@session.user_input(:price_exfactory), 
                                    :exfactory))
      @model.carry(:price_public, 
        ODDB::Package.price_internal(@session.user_input(:price_public),
                                    :public))
    end
    return self
  end
  if(@model.is_a? Persistence::CreateItem)
    @model.append(ikscode)
    @model = @session.app.create(@model.pointer)
  end
  keys = [
    :ddd_dose,
    :deductible,
    :descr,
    :disable,
    :disable_ddd_price,
    :ikscat,
    :market_date,
    :pharmacode,
    :pretty_dose,
    :preview_with_market_date,
    :price_exfactory,
    :price_public,
    :photo_link,
    :refdata_override,
    :lppv,
  ]
  input = user_input(keys)
  part_keys = [
    :multi,
    :count,
    :measure,
    :commercial_form,
    :composition,
  ]
  parts = user_input(part_keys)
  time = Time.now
  [:price_exfactory, :price_public].each { |key|
    if(input.include?(key))
      price = ODDB::Package.price_internal(input[key], key)
      price.origin = unique_email
      price.authority = :user
      price.valid_from = time
      input.store(key, price)
    end
  }
  unless(error?)
    if(ikscode != @model.ikscd)
      @model.ikscd = ikscode
    end
    @model = @session.app.update(@model.pointer, input, unique_email)
    update_parts(parts)
  end
  self
end

- (Object) update_parts(input)



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 'src/state/admin/package.rb', line 120

def update_parts(input)
  if(counts = input[:count])
    comforms = input[:commercial_form] || {}
    counts.each { |idx, count|
      part = @model.parts.at(idx.to_i)
      ptr = part ? part.pointer : (@model.pointer + :part).creator
      current = { :package => @model.pointer }
      [:multi, :count, :measure, :composition].each { |key|
        values = (input[key] ||= {})
        current.store(key, values[idx])
      }
      if(cidx = current[:composition])
        comp = @model.registration.compositions[cidx.to_i]
        current[:composition] = comp ? comp.pointer : nil
      end
      if(name = comforms[idx])
        if(name.empty?)
          current.store(:commercial_form, nil)
        elsif(comform = ODDB::CommercialForm.find_by_name(name))
          current.store(:commercial_form, comform.pointer)
        else
          @errors.store(:commercial_form,
                        create_error(:e_unknown_comform,
                                     :commercial_form, name))
        end
      end
      @session.app.update(ptr, current, unique_email)
    }
  end
end