· 6 years ago · Nov 20, 2019, 09:58 AM
1class Reservation::DoctorReservationFormLogV210
2 include ActiveModel::Model
3
4 MALE = 'male'.freeze
5 FEMALE = 'female'.freeze
6 BPJS = 'bpjs'.freeze
7 EMAIL_REGEX = /\A[\w\.-]+@([\w-]+\.)+[\w-]+\z/
8
9 attr_accessor :id,
10 :user_id,
11 :key,
12 :doctor_id,
13 :hospital_id,
14 :funnel_name,
15 :price,
16 :doctor_name,
17 :doctor_speciality,
18 :hospital_name,
19 :reservation_date,
20 :reservation_time,
21 :user_name,
22 :gender,
23 :user_phone,
24 :user_email,
25 :user_birthday,
26 :follow,
27 :source,
28 :schedule_id,
29 :feedback_status,
30 :confirmation_status,
31 :bitrix_status,
32 :last_bitrix_update,
33 :bitrix_id,
34 :booking_id,
35 :is_new_patient,
36 :id_number,
37 :address,
38 :payment_method,
39 :occupation,
40 :religion,
41 :fit_to_fly_letter,
42 :blood_type,
43 :legal_guardian_name,
44 :confirmation_url,
45 :feedback_url,
46 :confirmation_pdf_url,
47 :reservation,
48 :bitrix_schedule_date,
49 :booking_version,
50 :is_dashboard,
51 :is_cancel_acknowledged,
52 :queue_number,
53 :form_2_rs_api_status,
54 :update_booking_rs_api_status
55
56 attr_reader :doctor_reservation
57 # Validations
58 validate :user_must_valid
59
60 def valid?
61 @doctor_reservation = build_doctor_reservation
62 super
63 end
64
65 def save
66 puts "==============ada di doctor reservation save==============="
67 if valid?
68 puts "==============lolos validasi==============="
69 @doctor_reservation = build_doctor_reservation
70 @doctor_reservation.set_created_at
71 begin
72 @doctor_reservation.save!
73 true
74 rescue => e
75 puts e.inspect
76 puts e.backtrace
77 errors.add(:base, "Input data failed")
78 @doctor_reservation.errors
79 .full_messages
80 .each{|error_message| errors.add(:base, error_message)}
81 false
82 end
83 else
84 @doctor_reservation.errors
85 .full_messages
86 .each{|error_message| errors.add(:base, error_message)}
87 false
88 end
89 end
90
91 def update
92 begin
93 @doctor_reservation = Reservation::DoctorReservationLog.find_by(booking_id: self.booking_id)
94 @doctor_reservation.update_attributes!(
95 last_bitrix_update: self.last_bitrix_update || @doctor_reservation.last_bitrix_update,
96 confirmation_pdf_url: self.confirmation_pdf_url || @doctor_reservation.confirmation_pdf_url,
97 feedback_status: self.feedback_status || @doctor_reservation.feedback_status,
98 bitrix_status: self.bitrix_status || @doctor_reservation.bitrix_status,
99 queue_number: self.queue_number || @doctor_reservation.queue_number,
100 confirmation_status: self.confirmation_status || @doctor_reservation.confirmation_status)
101 true
102 rescue
103 false
104 end
105 end
106
107 def valid_reservation_additional?
108 @doctor_reservation = Reservation::DoctorReservationLog.find_by(booking_id: self.booking_id)#, bitrix_status: 'pending')
109 if @doctor_reservation.blank?
110 errors.add(:base, "Booking ID harus valid")
111 return false
112 end
113 if @doctor_reservation.bitrix_id.present? && @doctor_reservation.bitrix_status != 'pending'
114 puts "Booking Sudah disimpan"
115 errors.add(:base, "Booking Sudah disimpan")
116 puts "end Booking Sudah disimpan"
117 return false
118 end
119
120 puts "=============inilah isinya rs api status di valid additional: "
121 puts self.form_2_rs_api_status
122 @doctor_reservation[:form_2_rs_api_status] = self.form_2_rs_api_status
123
124 prefilling_book_doctors = Reservation::PrefillingBookDoctor.where(show: true)
125 prefilling_book_doctors.each do|prefilling_book_doctor|
126 field_value = self.try(prefilling_book_doctor.field_name)
127 @doctor_reservation[prefilling_book_doctor.field_name] = field_value
128 if field_value.blank? && prefilling_book_doctor.mandatory
129 errors.add(:base, "#{prefilling_book_doctor.field_placeholder} harus diisi")
130 end
131 end
132 if errors.full_messages.length > 1
133 puts "errors"
134 puts errors.inspect
135
136 errors.clear
137 errors.add(:base, 'Mohon Lengkapi Data Pasien')
138 end
139 errors.full_messages.empty? && @doctor_reservation.valid?
140 end
141
142
143 private
144
145 def booking_id_must_valid
146 if self.booking_id.blank?
147 errors.add(:base, "Booking ID harus valid")
148 end
149 end
150
151 def doctor_id_must_not_empty
152 if self.doctor_id.blank?
153 errors.add(:base, "Doctor ID harus valid")
154 end
155 end
156
157 def schedule_id_must_not_empty
158 if self.schedule_id.blank?
159 errors.add(:base, "Jadwal Konsultasi harus diisi")
160 end
161 end
162
163 def reservation_date_must_valid
164 if reservation_date.present?
165 begin
166 DateTime.strptime(reservation_date, '%d %b %Y')
167 return
168 rescue
169 begin
170 DateTime.strptime(reservation_date, '%-d %b %Y')
171 return
172 rescue
173 begin
174 DateTime.strptime(reservation_date, '%d/%m/%Y')
175 return
176 rescue
177 begin
178 DateTime.strptime(reservation_date, '%-d/%-m/%Y')
179 return
180 rescue
181 begin
182 DateTime.strptime(reservation_date, '%Y-%m-%d')
183 return
184 rescue
185 begin
186 DateTime.strptime(reservation_date, '%Y-%-m-%-d')
187 return
188 rescue
189 errors.add(:base, 'Tanggal reservasi harus valid')
190 end
191 end
192 end
193 end
194 end
195 end
196 else
197 errors.add(:base, 'Tanggal reservasi harus valid')
198 end
199 end
200
201 def user_birthday_must_valid
202 if user_birthday.present?
203 begin
204 DateTime.strptime(user_birthday, '%d %b %Y')
205 return
206 rescue
207 begin
208 DateTime.strptime(user_birthday, '%-d %b %Y')
209 return
210 rescue
211 begin
212 DateTime.strptime(user_birthday, '%d/%m/%Y')
213 return
214 rescue
215 begin
216 DateTime.strptime(user_birthday, '%-d/%-m/%Y')
217 return
218 rescue
219 begin
220 DateTime.strptime(user_birthday, '%Y-%m-%d')
221 return
222 rescue
223 begin
224 DateTime.strptime(user_birthday, '%Y-%-m-%-d')
225 return
226 rescue
227 errors.add(:base, 'Tanggal lahir harus valid')
228 end
229 end
230 end
231 end
232 end
233 end
234 else
235 errors.add(:base, 'Tanggal lahir harus valid')
236 end
237 end
238
239 def email_must_valid
240 if user_email.blank?
241 errors.add(:base, 'Email harus diisi')
242 elsif !(user_email =~ EMAIL_REGEX)
243 errors.add(:base, 'Email harus valid')
244 end
245 end
246
247 def phone_must_valid
248 if user_phone.blank?
249 errors.add(:base, 'Nomor Telepon harus diisi')
250 elsif !(user_phone =~/^[0-9]{4,15}$/)
251 errors.add(:base, 'Nomor Telepon harus valid')
252 end
253 end
254
255 def user_must_valid
256 errors.add(:base, 'Nama harus diisi') if self.user_name.blank?
257 end
258
259 def build_doctor_reservation
260 Reservation::DoctorReservationLog.new(
261 doctor_id: self.doctor_id,
262 key:self.key,
263 initial_doctor_id: self.doctor_id,
264 doctor_name: self.doctor_name,
265 initial_doctor_name: self.doctor_name,
266 doctor_speciality: self.doctor_speciality,
267 initial_doctor_speciality: self.doctor_speciality,
268 hospital_id: self.hospital_id,
269 funnel_name: self.funnel_name,
270 patient_name: self.user_name,
271 price: self.price,
272 hospital_name: self.hospital_name,
273 schedule_date: self.reservation_date,
274 initial_schedule_date: self.reservation_date,
275 initial_schedule_time: self.reservation_time,
276 schedule_time: self.reservation_time,
277 gender: self.gender,
278 phone: self.user_phone,
279 email: self.user_email,
280 birthday: self.user_birthday,
281 follow: self.follow,
282 schedule_id: self.schedule_id,
283 source: self.source,
284 booking_version: self.booking_version,
285 booking_id: self.booking_id,
286 is_new_patient: self.is_new_patient,
287 id_number: self.id_number,
288 address: self.address,
289 payment_method: self.payment_method,
290 occupation: self.occupation,
291 religion: self.religion,
292 fit_to_fly_letter: self.fit_to_fly_letter,
293 blood_type: self.blood_type,
294 last_bitrix_update: self.last_bitrix_update,
295 confirmation_url: self.confirmation_url,
296 confirmation_pdf_url: self.confirmation_pdf_url,
297 feedback_url: self.feedback_url,
298 legal_guardian_name: self.legal_guardian_name,
299 is_dashboard: self.is_dashboard,
300 bitrix_status: self.bitrix_status,
301 is_cancel_acknowledged: self.is_cancel_acknowledged,
302 queue_number: self.queue_number,
303 form_2_rs_api_status: self.form_2_rs_api_status,
304 fullname_patient: fullname_patient,
305 insurance_id: insurance_id,
306 insurance_name: insurance_name,
307 update_booking_rs_api_status: self.update_booking_rs_api_status
308 )
309 end
310
311end