I think this code and the one before can be rewritten if you divide the code in two different methods:
```ruby
def total
  lines.sum(:price) - discount
end

def discount
  if total_discount && !total_discount.empty?
    if self.total_discount.include?("%")
      lines.sum(:price) * total_discount.to_i)/100
    else
      total_discount.to_i
    end
  else
    0
  end
end
```