fix: patient appointment
auto invoicing: record payment if payment mode and amount available
test: fixes
diff --git a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py
index cc3492a..fcd87d7 100755
--- a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py
+++ b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py
@@ -123,22 +123,24 @@
 		sales_invoice.customer = frappe.get_value('Patient', appointment_doc.patient, 'customer')
 		sales_invoice.appointment = appointment_doc.name
 		sales_invoice.due_date = getdate()
-		sales_invoice.is_pos = 1
 		sales_invoice.company = appointment_doc.company
 		sales_invoice.debit_to = get_receivable_account(appointment_doc.company)
 
 		item = sales_invoice.append('items', {})
 		item = get_appointment_item(appointment_doc, item)
 
-		payment = sales_invoice.append('payments', {})
-		payment.mode_of_payment = appointment_doc.mode_of_payment
-		payment.amount = appointment_doc.paid_amount
+		# Add payments if payment details are supplied else proceed to create invoice as Unpaid
+		if appointment_doc.mode_of_payment and appointment_doc.paid_amount:
+			sales_invoice.is_pos = 1
+			payment = sales_invoice.append('payments', {})
+			payment.mode_of_payment = appointment_doc.mode_of_payment
+			payment.amount = appointment_doc.paid_amount
 
 		sales_invoice.set_missing_values(for_validate=True)
 		sales_invoice.flags.ignore_mandatory = True
 		sales_invoice.save(ignore_permissions=True)
 		sales_invoice.submit()
-		frappe.msgprint(_('Sales Invoice {0} created as paid'.format(sales_invoice.name)), alert=True)
+		frappe.msgprint(_('Sales Invoice {0} created'.format(sales_invoice.name)), alert=True)
 		frappe.db.set_value('Patient Appointment', appointment_doc.name, 'invoiced', 1)
 		frappe.db.set_value('Patient Appointment', appointment_doc.name, 'ref_sales_invoice', sales_invoice.name)
 
diff --git a/erpnext/healthcare/doctype/patient_appointment/test_patient_appointment.py b/erpnext/healthcare/doctype/patient_appointment/test_patient_appointment.py
index 7cdb28e..eeed157 100644
--- a/erpnext/healthcare/doctype/patient_appointment/test_patient_appointment.py
+++ b/erpnext/healthcare/doctype/patient_appointment/test_patient_appointment.py
@@ -11,7 +11,8 @@
 class TestPatientAppointment(unittest.TestCase):
 	def setUp(self):
 		frappe.db.sql("""delete from `tabPatient Appointment`""")
-		frappe.db.sql("""delete from `tabFee Validity""")
+		frappe.db.sql("""delete from `tabFee Validity`""")
+		frappe.db.sql("""delete from `tabPatient Encounter`""")
 
 	def test_status(self):
 		patient, medical_department, practitioner = create_healthcare_docs()
@@ -25,11 +26,16 @@
 
 	def test_start_encounter(self):
 		patient, medical_department, practitioner = create_healthcare_docs()
-		appointment = create_appointment(patient, practitioner, add_days(nowdate(), 3))
-		encounter = create_encounter(appointment)
-		self.assertEquals(frappe.db.get_value('Patient Encounter', encounter.name, 'company'), appointment.company)
-		self.assertEquals(frappe.db.get_value('Patient Encounter', encounter.name, 'practitioner'), appointment.practitioner)
-		self.assertEquals(frappe.db.get_value('Patient Encounter', encounter.name, 'patient'), appointment.patient)
+		frappe.db.set_value('Healthcare Settings', None, 'automate_appointment_invoicing', 1)
+		appointment = create_appointment(patient, practitioner, add_days(nowdate(), 4), invoice = 1)
+		self.assertEqual(frappe.db.get_value('Patient Appointment', appointment.name, 'invoiced'), 1)
+		encounter = make_encounter(appointment.name)
+		self.assertTrue(encounter)
+		self.assertEqual(encounter.company, appointment.company)
+		self.assertEqual(encounter.practitioner, appointment.practitioner)
+		self.assertEqual(encounter.patient, appointment.patient)
+		# invoiced flag mapped from appointment
+		self.assertEqual(encounter.invoiced, frappe.db.get_value('Patient Appointment', appointment.name, 'invoiced'))
 
 	def test_invoicing(self):
 		patient, medical_department, practitioner = create_healthcare_docs()
@@ -104,7 +110,13 @@
 
 def create_encounter(appointment):
 	if appointment:
-		encounter = make_encounter(appointment.name)
+		encounter = frappe.new_doc('Patient Encounter')
+		encounter.appointment = appointment.name
+		encounter.patient = appointment.patient
+		encounter.practitioner = appointment.practitioner
+		encounter.encounter_date = appointment.appointment_date
+		encounter.encounter_time = appointment.appointment_time
+		encounter.company = appointment.company
 		encounter.save()
 		encounter.submit()
 		return encounter