def test_policy_change_billing_schedule(self): self.policy.billing_schedule = "Quarterly" pa = PolicyAccounting(self.policy.id) invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .order_by(Invoice.bill_date).all() self.payments.append(pa.make_payment(contact_id=self.policy.named_insured, date_cursor=invoices[0].bill_date, amount=300)) self.assertEquals(len(invoices), 4) self.assertEquals(pa.return_account_balance(invoices[0].due_date), 0) self.assertEquals(pa.return_account_balance(invoices[-1].due_date), 900) # Evaluate that there are not deleted invoices in this policy yet self.assertEquals(len([invoice for invoice in invoices if invoice.deleted]), 0) # Create new invoices with a monthly schedule on the same policy pa.policy.billing_schedule = 'Monthly' pa.make_invoices() invoices = Invoice.query.filter_by(policy_id=self.policy.id) \ .order_by(Invoice.bill_date).all() # The number of invoices should now be 16 (12 + 4) self.assertEquals(len(invoices), 16) # There should be 4 "deleted" invoices in the policy self.assertEquals(len([invoice for invoice in invoices if invoice.deleted]), 4) # The pending balance should still be 900 even when new invoices were created. self.assertEquals(pa.return_account_balance(invoices[-1].due_date), 900)
def test_monthly_payments_and_balance(self): self.policy.billing_schedule = "Monthly" pa = PolicyAccounting(self.policy.id) invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .order_by(Invoice.bill_date).all() for invoice in invoices: self.assertEquals(pa.return_account_balance(date_cursor=invoice.bill_date), invoice.amount_due) self.payments.append(pa.make_payment(contact_id=self.policy.named_insured, date_cursor=invoice.bill_date, amount=invoice.amount_due)) self.assertEquals(pa.return_account_balance(date_cursor=invoice.bill_date), 0)
def test_quarterly_on_last_installment_bill_date(self): self.policy.billing_schedule = "Quarterly" pa = PolicyAccounting(self.policy.id) invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .order_by(Invoice.bill_date).all() import logging logging.info("Invoice.bill_date") logging.info( pa.return_account_balance(date_cursor=invoices[3].bill_date)) self.assertEquals( pa.return_account_balance(date_cursor=invoices[3].bill_date), 1200)
def get_result(policy, supplied_date): errors = {} try: pa = PolicyAccounting(policy) except orm.exc.NoResultFound: error_text = "No Policy found with Policy id: " + policy errors['error'] = error_text logger.error(error_text) return render_template("error.html", context=errors) invoices = pa.get_invoices(supplied_date) balance = pa.return_account_balance(supplied_date) main_dic = {} invoices_list = [] for invoice in invoices: logger.debug( "invoice: bill_date: %s - due_date: %s - cancel_date: %s - amount_due: %s", invoice.bill_date, invoice.due_date, invoice.cancel_date, invoice.amount_due) invoices_list.append({ 'bill_date': invoice.bill_date.strftime('%Y-%m-%d'), 'due_date': invoice.due_date.strftime('%Y-%m-%d'), 'cancel_date': invoice.cancel_date.strftime('%Y-%m-%d'), 'amount_due': invoice.amount_due }) main_dic['invoices'] = invoices_list main_dic['balance'] = balance main_dic['policy_id'] = policy return render_template('table.html', context=main_dic)
def test_quarterly_on_eff_date(self): print "Testing account balance on quarterly effective date...\n" self.policy.billing_schedule = "Quarterly" pa = PolicyAccounting(self.policy.id) self.assertEquals( pa.return_account_balance(date_cursor=self.policy.effective_date), 300)
def test_annual_on_eff_date(self): print "Testing account balance on annual effective date...\n" self.policy.billing_schedule = "Annual" pa = PolicyAccounting(self.policy.id) self.assertEquals( pa.return_account_balance(date_cursor=self.policy.effective_date), 1200)
def test_monthly_on_eff_date(self): self.policy.billing_schedule = "Monthly" pa = PolicyAccounting(self.policy.id) self.assertEquals( pa.return_account_balance(date_cursor=self.policy.effective_date), 100)
def main_page(): if request.method == 'GET': # send the user the form return render_template('invoices.html') elif request.method == 'POST': # read form data policy_number = request.form['policy_number'] effective_date = request.form['effective_date'] # Use Policy Number to get Policy Id policy = Policy.query.filter_by(policy_number=policy_number)\ .first() # Pass Policy ID to the filter mechnism if policy is not None: invoices = Invoice.query.filter_by(policy_id=policy.id)\ .order_by(Invoice.bill_date)\ .all() pa = PolicyAccounting(policy.id) balance = pa.return_account_balance(effective_date) else: invoices = [] balance = 0 return render_template("invoices.html", invoices=invoices, balance=balance)
def test_quarterly_on_last_installment_bill_date(self): print "Testing account balance on last installment of quarterly bill date...\n" self.policy.billing_schedule = "Quarterly" pa = PolicyAccounting(self.policy.id) invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .order_by(Invoice.bill_date).all() self.assertEquals( pa.return_account_balance(date_cursor=invoices[3].bill_date), 1200)
def test_monthly_on_last_installment_bill_date(self): self.policy.billing_schedule = "Monthly" pa = PolicyAccounting(self.policy.id) invoices = Invoice.query.filter_by(policy_id=self.policy.id) \ .order_by(Invoice.bill_date).all() self.assertEquals( pa.return_account_balance(date_cursor=invoices[11].bill_date), 1200)
def test_quarterly_on_second_installment_bill_date_with_full_payment(self): self.policy.billing_schedule = "Quarterly" pa = PolicyAccounting(self.policy.id) invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .order_by(Invoice.bill_date).all() self.payments.append(pa.make_payment(contact_id=self.policy.named_insured, date_cursor=invoices[1].bill_date, amount=600)) self.assertEquals(pa.return_account_balance(date_cursor=invoices[1].bill_date), 0)
def test_monthly_account_balance(self): self.policy.billing_schedule = "Monthly" pa = PolicyAccounting(self.policy.id) invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .order_by(Invoice.bill_date).all() # An index is used instead of iterate directly on the elements to help with the balance calculation for curr_invoice in range(len(invoices)): self.assertEquals(pa.return_account_balance(date_cursor=invoices[curr_invoice].bill_date), (curr_invoice + 1) * invoices[curr_invoice].amount_due)
def test_two_pay_on_last_installment_bill_date(self): self.policy.billing_schedule = "Two-Pay" pa = PolicyAccounting(self.policy) invoices = Invoice.query.filter_by(policy_id=self.policy.id) \ .order_by(Invoice.bill_date).all() for i in range(1, 3): self.payments.append(pa.make_payment(contact_id=self.policy.named_insured, date_cursor=invoices[0].bill_date, amount=600)) self.assertEquals(pa.return_account_balance(date_cursor=invoices[1].bill_date), 0)
def test_monthly_on_sixth_installment_bill_date(self): self.policy.billing_schedule = "Monthly" pa = PolicyAccounting(self.policy) invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .order_by(Invoice.bill_date).all() for i in range(0, 6): self.payments.append(pa.make_payment(contact_id=self.policy.named_insured, date_cursor=invoices[i].bill_date, amount=100)) self.assertEquals(pa.return_account_balance(date_cursor=invoices[11].bill_date), 600)
def test_quarterly_on_eff_date_after_change(self): """ Same as above, but after a change in billing schedule. """ self.policy.billing_schedule = "Monthly" pa = PolicyAccounting(self.policy.id) pa.change_billing_schedule("Quarterly") self.assertEquals( pa.return_account_balance(date_cursor=self.policy.effective_date), 300)
def test_quarterly_on_last_installment_bill_date_after_change(self): """ Same as above, buf after change in billing schedule. """ self.policy.billing_schedule = "Monthly" pa = PolicyAccounting(self.policy.id) pa.change_billing_schedule("Quarterly") invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .filter(Invoice.deleted == False)\ .order_by(Invoice.bill_date).all() self.assertEquals( pa.return_account_balance(date_cursor=invoices[3].bill_date), 1200)
def get_invoice(): if request.json or request.post: data = dict() policy_id = int(request.json['policy_id']) query_date = datetime.datetime.strptime(request.json['query_date'], '%d/%m/%Y').date() pa = PolicyAccounting(policy_id) data['policy_number'] = pa.get_policy_number() data['account_balance'] = pa.return_account_balance(query_date) data['invoices'] = pa.get_invoices(query_date) return jsonify(data) else: return make_response(jsonify({'error': 'Bad request'}), 400)
def consult_policy(): response = {} list_invoices = [] list_payments = [] data = request.json try: curr_date = datetime.strptime(data['date'], "%Y-%m-%d") policy_id = int(data['policy_id']['id']) pa = PolicyAccounting(policy_id) except: abort(400) # Get current balance to date response["total_balance"] = pa.return_account_balance( date_cursor=curr_date) # Get invoices invoices = Invoice.query.filter_by(policy_id=policy_id) \ .filter(Invoice.bill_date <= curr_date ) \ .filter(Invoice.deleted == False) \ .order_by(Invoice.bill_date) \ .all() for invoice in invoices: dict_invoice = { 'bill_date': invoice.bill_date.strftime("%m/%d/%Y"), 'due_date': invoice.due_date.strftime("%m/%d/%Y"), 'cancel_date': invoice.cancel_date.strftime("%m/%d/%Y"), 'amount_due': invoice.amount_due } list_invoices.append(dict_invoice) response['invoices'] = list_invoices # Get payments payments = Payment.query.filter_by(policy_id=policy_id)\ .filter(Payment.transaction_date <= curr_date)\ .all() print len(payments) for payment in payments: dict_payment = { 'transaction_date': payment.transaction_date.strftime("%m/%d/%Y"), 'amount_paid': payment.amount_paid } list_payments.append(dict_payment) response['payments'] = list_payments # Add policy status response['policy_status'] = pa.policy.status return json.dumps(response)
def test_quarterly_on_second_installment_bill_date_with_full_payment_after_change( self): """ Same as above, buf after change in billing schedule. """ self.policy.billing_schedule = "Monthly" pa = PolicyAccounting(self.policy.id) pa.change_billing_schedule("Quarterly") invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .filter(Invoice.deleted == False)\ .order_by(Invoice.bill_date).all() self.payments.append( pa.make_payment(contact_id=self.policy.named_insured, date_cursor=invoices[1].bill_date, amount=600)) self.assertEquals( pa.return_account_balance(date_cursor=invoices[1].bill_date), 0)
def get_policy(policy_id): date_cursor = datetime.strptime(request.values.get("dateCursor"), "%Y-%m-%d") policy_object = Policy.query.filter_by(id=policy_id).one() pa = PolicyAccounting(policy_object.id) account_balance = pa.return_account_balance(date_cursor=date_cursor) invoices_queryset = Invoice.query.filter_by(policy_id=policy_id).all() payments_queryset = Payment.query.filter_by(policy_id=policy_id).all() payments, invoices = [], [] policy = policy_serializer(policy_object, account_balance) for payment in payments_queryset: payments.append(payment_serializer(payment)) for invoice in invoices_queryset: invoices.append(invoice_serializer(invoice)) return jsonify({ "policy": policy, "payments": payments, "invoices": invoices })
def policy(): """ Returns policy detail """ policy_id = request.args.get('policy_id') date = request.args.get('date') if policy_id is None or policy_id == '': return Response(status=400) # Default date if not date: date = datetime.now().date() else: date = datetime.strptime(date, '%Y-%m-%d').date() # Return error page if policy not found try: policy_account = PolicyAccounting(policy_id) except NoResultFound as ex: return render_template('error.html', status=404, resource_type='Policy') # Retrieve relevant information about policy for view policy_details = { 'account_balance': policy_account.return_account_balance(date), 'agent': policy_account.agent, 'annual_premium': policy_account.policy.annual_premium, 'named_insured': policy_account.named_insured, 'invoices': policy_account.policy.invoices, 'payments': policy_account.policy.payments, 'policy_number': policy_account.policy.policy_number, 'date': date, } return render_template('policy_detail.html', policy_details=policy_details)
class TestChangeBillingSchedule(unittest.TestCase): @classmethod def setUpClass(cls): cls.test_agent = Contact("Test Agent", "Agent") cls.test_insured = Contact("Test Insured", "Named Insured") db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.commit() @classmethod def tearDownClass(cls): db.session.delete(cls.test_insured) db.session.delete(cls.test_agent) db.session.commit() def setUp(self): self.policy = Policy("Test Policy", date(2015, 1, 1), 1200) self.policy.named_insured = self.test_insured.id self.policy.agent = self.test_agent.id self.policy.billing_schedule = "Quarterly" db.session.add(self.policy) db.session.commit() self.pa = PolicyAccounting(self.policy.id) invoice = self.policy.invoices[0] self.payment = self.pa.make_payment( contact_id=self.policy.named_insured, date_cursor=invoice.bill_date, amount=invoice.amount_due, ) def tearDown(self): for invoice in self.policy.invoices: db.session.delete(invoice) db.session.delete(self.payment) db.session.delete(self.policy) db.session.commit() def test_valid_billing_schedule(self): self.assertEquals(self.pa.return_account_balance(), 900) self.assertEquals(len(self.policy.invoices), 4) old_invoices = self.policy.invoices self.pa.change_billing_schedule("Monthly") self.assertEquals(self.pa.return_account_balance(), 900) self.assertEquals(len(self.policy.invoices), 16) self.assertEquals(self.policy.billing_schedule, "Monthly") for invoice in self.policy.invoices: if invoice not in old_invoices: self.assertEquals(invoice.amount_due, 100) self.assertEquals(invoice.policy_id, self.policy.id) self.assertEquals(invoice.deleted, False) for invoice in old_invoices: self.assertEquals(invoice.deleted, True) def test_invalid_billing_schedule(self): self.assertEquals(self.pa.return_account_balance(), 900) self.assertEquals(len(self.policy.invoices), 4) old_invoices = self.policy.invoices self.pa.change_billing_schedule("Invalid") self.assertEquals(self.pa.return_account_balance(), 900) self._old_data_remains_unchanged(old_invoices) def test_empty_billing_schedule(self): self.assertEquals(self.pa.return_account_balance(), 900) old_invoices = self.policy.invoices self.assertEquals(len(self.policy.invoices), 4) self.pa.change_billing_schedule() self.assertEquals(self.pa.return_account_balance(), 900) self._old_data_remains_unchanged(old_invoices) def test_same_billing_schedule(self): self.assertEquals(self.pa.return_account_balance(), 900) self.assertEquals(len(self.policy.invoices), 4) old_invoices = self.policy.invoices self.pa.change_billing_schedule("Quarterly") self.assertEquals(self.pa.return_account_balance(), 900) self._old_data_remains_unchanged(old_invoices) def _old_data_remains_unchanged(self, old_invoices): self.assertEquals(len(self.policy.invoices), 4) self.assertEquals(self.policy.billing_schedule, "Quarterly") self.assertEquals(old_invoices, self.policy.invoices)
def test_two_pay_on_eff_date(self): self.policy.billing_schedule = "Two-Pay" pa = PolicyAccounting(self.policy.id) self.assertEquals(pa.return_account_balance(date_cursor=self.policy.effective_date), 600)