Created
July 2, 2015 06:55
-
-
Save klippx/cf6f5ce155edd9e09ade to your computer and use it in GitHub Desktop.
Final result: FromAttachment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PouchOrder::FromAttachment | |
include DatetimeChange | |
def initialize(attachment: attachment) | |
@attachment = attachment | |
@pouch_order = PouchOrder.new( | |
number: @attachment.header.order_number, | |
phone_number: @attachment.contact_telephone, | |
issued_at: @attachment.issued_at, | |
delivery_at: @attachment.delivery_at, | |
) | |
end | |
def parse | |
parse_patient && parse_ward && parse_items | |
@pouch_order | |
end | |
private | |
def parse_patient | |
patient = Patient.where(ssn: @attachment.patient_ssn).first_or_initialize | |
patient.first_name = @attachment.patient_first_name | |
patient.last_name = @attachment.patient_last_name | |
@pouch_order.patient = patient | |
end | |
def parse_ward | |
@pouch_order.ward = Ward.find_by_consignee_gln!(gln: @attachment.delivery_customer_gln) | |
end | |
def parse_items | |
items_grouped_by_date.each do |date, items| | |
intakes_grouped_by_time(items: items).each do |time_string, intakes| | |
time = Time.zone.parse(time_string) | |
admin_time = datetime_change(datetime: date, time: time) | |
intakes.each do |intake| | |
build_line(admin_time: admin_time, intake: intake) | |
end | |
end | |
end | |
end | |
def items_grouped_by_date | |
@attachment.items.group_by(&:periodic_start) | |
end | |
def intakes_grouped_by_time(items: items) | |
items.flat_map(&:intakes).group_by(&:time) | |
end | |
def build_line(admin_time: admin_time, intake: intake) | |
@pouch_order.lines.build( | |
item: Item.find_by!(npl_id: intake.item.npl_id), | |
prescriber_name: intake.item.prescriber_name, | |
admin_time: admin_time, | |
quantity: intake.quantity, | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment