Beispiel #1
0
    def create_child_policy(self, policy_number, customer_number, rate, date_expiry, premium_amount, transaction_type,
                            agency_id, insurance_company, pricing_model, master_id, subclass, vehicle_id=None, reason=None, ):
        
        if vehicle_id is None:
            vehicle_id = self.vehicle_id

        # create child policy
        new_child = ChildPolicy(
            policy_number,
            vehicle_id,
            customer_number,
            rate,
            date_expiry,
            premium_amount,
            transaction_type,
            agency_id,
            insurance_company,
            pricing_model,
            master_id,
            subclass,
            reason
        )
        new_child.save()

        return new_child.id
    def revise_child_policy(transaction_type, previous_policy, policy_details, reason=None):
        # deactivate old policy
        previous_policy.deactivate()

        # Create an update of the old policy, to accomodate the changes made
        child_controller = ChildController()
        revised_policy = child_controller.create_child_policy(
            previous_policy.cp_number,
            previous_policy.customer_number,
            previous_policy.rate,
            previous_policy.date_expiry,
            # new premium amount
            policy_details["premium_amount"],
            transaction_type,
            previous_policy.agency_id,
            previous_policy.company,
            previous_policy.pricing_model,
            previous_policy.master_policy,
            previous_policy.subclass,
            previous_policy.vehicle,
            reason,

        )
        # TODO: Look at the dates in all policies and change them appropriately
        # activate the updated policy
        updated_policy = ChildPolicy.get_child_by_id(
            revised_policy, False).activate()
        return revised_policy
Beispiel #3
0
 def cancel(child_policy_id):
     """
     To cancel a child policy, set 'is_active' to false and 'transaction_type' to 'CNC' for cancelled
     """
     # get child policy
     child_policy = ChildPolicy.get_recent_by_id(child_policy_id)
     # deactivate child policy
     data = {
         "transaction_type": 'CNC',
         "is_active": False
     }
     child_policy.update(data)
Beispiel #4
0
    def get(self, customer_number):
        """
        get the customer active child policies
        this is needed for the payments module where user's active policy payments will be tracked
        :return:
        """
        customers = ChildPolicy.get_child_policies(customer_number)
        if customers:
            return make_response(
                helper.make_rest_success_response("success", customers), 200)

        return make_response(
            helper.make_rest_fail_response("No customer policies were found"),
            404)
Beispiel #5
0
    def get_unselected_benefits_extensions(child_policy_id):
        """
        We need to exclude the benefits and extensions that the user
        has not selected from the insurance company
        :param child_policy_id:
        :return:
        """
        child_policy = ChildPolicy.get_child_by_id(child_policy_id)
        if not child_policy:
            return None

        return {
            'extensions': ChildController.get_unselected_extensions(child_policy_id),
            'benefits': ChildController.get_unselected_benefits(child_policy_id)
        }
Beispiel #6
0
    def get(self, child_id):
        """
        Process request to get all unselected benefits and extensions
        of a policy holder, and the rest of the policy details
        :param policy_id:
        :return:
        """
        child = ChildPolicy.get_child_by_id(child_id).serialize()
        result = ChildController.get_unselected_benefits_extensions(child_id)
        if child:
            if result:
                child.update({"unselected_benefits": result})

            return make_response(
                helper.make_rest_success_response("Success", child), 200)

        else:
            return make_response(
                helper.make_rest_fail_response("Policy does not exist"), 404)
    def post(self):
        # get the current agency details
        uid = get_jwt_identity()

        # get user role so that you can use it to get the agency_id,
        claims = get_jwt_claims()
        role = claims['role']

        # get company_id
        company_id = self.get_agency_id(role, uid)
        policy_details = underwriting_parser.parse_args()
        # get the transaction type i.e could be a new policy, renewal or endorsement or extension
        if policy_details:
            transaction_type = policy_details['transaction_type']
            # if it's a new transaction (customer had not been enrolled into this kind of policy before)
            if transaction_type == 'NET':
                # Create master policy, first generate master policy number
                index = MasterPolicy.query.all()
                new_policy = PolicyNoGenerator('MS', len(index))
                # Get class details after receiving the class id from the front end e.g if name is Motor Private
                class_details = InsuranceClass.get_class_by_id(
                    policy_details['class_id'])
                # generate master policy number using the acronym from the class details
                new_policy.set_mpi(class_details.acronym)
                ms_policy_number = new_policy.generate_policy_no()

                master_policy = MasterController.create_master_policy(
                    ms_policy_number,
                    policy_details['customer_number'],
                    policy_details['date_expiry'],
                    policy_details['insurance_company']
                )
                # create child policy number
                child_policy_no = self.create_child_policy_number(
                    policy_details)
                # Create child policy with details
                child_policy_id = self.create_child(
                    policy_details, child_policy_no, company_id, master_policy)
                # Send response if successfully onboarded with the onboarded data
                # data = self.get_cover_data(child_policy_id)
                response = helper.make_rest_success_response(
                    "Congratulations! The customer was enrolled successfully. Cover will be"
                    " activated after payment is made", child_policy_id)
                return make_response(response, 200)

            # if it's an endorsement i.e the customer wants to add an item under the master policy
            elif transaction_type == 'END':
                # In this case, you could be endorsing the child policy or master policy
                # get the master policy by master policy id
                if policy_details["master_policy_id"]:
                    endorsed_policy = MasterPolicy.get_policy_by_id(
                        policy_details["master_policy_id"])

                    # check whether the policy has expired.
                    time_difference = (
                        endorsed_policy.date_expiry - datetime.now()).days
                    if time_difference > 1:
                        # create child policy number
                        child_policy_no = self.create_child_policy_number(
                            policy_details)
                        # Then endorse it with the child policy details:
                        child_policy_id = self.create_child(
                            policy_details, child_policy_no, company_id, endorsed_policy)
                        # endorsement successful
                        response = helper.make_rest_success_response(
                            "Endorsement successful", child_policy_id)
                        return make_response(response, 200)

                    else:
                        response = helper.make_rest_fail_response(
                            "Failed! Policy expired. Kindly renew it then endorse.")
                        return make_response(response, 500)

                if policy_details["child_policy_id"]:

                    # In this case we add a benefit to the child policy
                    endorsed_policy = ChildPolicy.get_child_by_id(
                        policy_details["child_policy_id"])

                    # Check whether the policy has expired.
                    time_difference = (
                        endorsed_policy.date_expiry - datetime.now()).days
                    if time_difference > 1:
                        # revise the child policy with the a new premium amount
                        revised_policy = self.revise_child_policy(
                            "END", endorsed_policy, policy_details, "benefit")

                        # append the new benefit to revised policy
                        child_controller = ChildController()
                        child_controller.add_benefits(
                            policy_details['benefits'], revised_policy)

                        # endorsement successful
                        response = helper.make_rest_success_response(
                            "Endorsement successful", revised_policy)
                        return make_response(response, 200)

                    else:
                        response = helper.make_rest_fail_response(
                            "Failed! Policy expired. Kindly renew it then endorse.")
                        return make_response(response, 500)

            elif transaction_type == 'RET':
                #   renew the transaction
                if MasterController.renew_transaction(policy_details['master_policy_id'],
                                                      policy_details['expiry_date'],
                                                      transaction_type):
                    return make_response(helper.make_rest_success_response("Policy renewed successfully"),
                                         200)
                else:
                    return make_response(helper.make_rest_success_response("Failed to renew Policy"))
                # if the customer want's to extend the cover to cater for extra losses
            elif transaction_type == 'EXT':
                # get child policy
                extended_policy = ChildPolicy.get_child_by_id(
                    policy_details['child_policy_id'])
                # check whether policy is less than one year old (365) days
                time_difference = (
                    datetime.now() - extended_policy.date_activated).days
                if time_difference < 365:
                    # revise the child policy with the a new premium amount
                    revised_policy = self.revise_child_policy(
                        "EXT", extended_policy, policy_details)

                    # append the new extension to revised policy
                    child_controller = ChildController()
                    child_controller.add_extensions(policy_details["extensions"],
                                                    revised_policy)

                    return make_response(helper.make_rest_success_response("Policy extended successfully", revised_policy),
                                         200)
                else:
                    return make_response(helper.make_rest_success_response("Failed to extend policy"))

            elif transaction_type == 'REF':
                refund_type = policy_details["refund_type"]
                # fetch the child policy to be refunded or updated
                child_policy = ChildPolicy.get_child_by_id(
                    policy_details['child_policy_id'], True)

                # get premium amount
                premium_amount = child_policy.premium_amount
                refund_amount = None

                # TODO: create model that stores transactions, the amount paid below is dummy data
                amount_paid = 800
                if refund_type == "sold":
                    # deactivate the previous child policy so that we can create a new one
                    child_policy.deactivate()

                    # initialize child controller
                    child_controller = ChildController()
                    child_controller.create_child_policy(
                        child_policy.cp_number,
                        child_policy.customer_number,
                        child_policy.rate,
                        child_policy.date_expiry,
                        # Zero premium amount
                        0,
                        "REF",
                        child_policy.agency_id,
                        child_policy.company,
                        child_policy.pricing_model,
                        child_policy.master_policy,
                        child_policy.subclass,
                        child_policy.vehicle,
                        "sold"
                    )

                    # if the policy has lasted for less than thirty days refund full amount paid
                    period_lasted = (datetime.now() -
                                     child_policy.date_activated).days
                    if period_lasted <= 30:
                        refund_amount = amount_paid

                    # if more than 30 days, deduct the cost incured for the period the cover has run
                    else:
                        cost_incurred = (premium_amount / 365) * period_lasted
                        if amount_paid >= cost_incurred:
                            refund_amount = amount_paid - cost_incurred
                        # TODO: Create a clause for handling amount paid

                elif refund_type == "benefit":
                    # deactivate previous policy
                    child_policy.deactivate()

                    # create new transaction with the same child policy number and vehicle details
                    revised_policy = self.revise_child_policy(
                        "REF", child_policy, policy_details, "benefit_removed")

                    # Append any remaining benefits
                    # first get the id of the cancelled benefit
                    cancelled_benefit = policy_details["benefit_id"]
                    # get remaining
                    remaining_benefits = [
                        i.serialize() for i in child_policy.benefits if i.id != cancelled_benefit]

                    # append them to the revised policy
                    ChildController.add_benefits(
                        remaining_benefits, revised_policy)

                    # get refund amount based on the new recalculated premium amount
                    new_premium_amount = policy_details["premium_amount"]
                    previous_premium = child_policy.premium_amount

                    # difference
                    premium_change = previous_premium - new_premium_amount

                    # If the policy has run for 30 days, refund full difference
                    period_lasted = (datetime.now() -
                                     child_policy.date_activated).days
                    amount_to_revise = (period_lasted / 365) * premium_change
                    refund_amount = amount_paid

                    # TODO: Revise transaction table and update date due using the amount to revise

                elif refund_type == "sum_insured":
                    # create new child policy based on new details after downward revision of sum insured
                    # deactivate previous policy
                    child_policy.deactivate()

                    # create transaction with new details
                    new_child = self.create_child(
                        policy_details, child_policy.cp_number, company_id, child_policy.master_policy)

                    # TODO: update with new dates
                    new_child.activate()

                    # get refund amount based on the new recalculated premium amount
                    new_premium_amount = policy_details["premium_amount"]
                    previous_premium = child_policy.premium_amount

                    # difference
                    premium_change = previous_premium - new_premium_amount

                    # If the policy has run for 30 days, refund full difference
                    period_lasted = (datetime.now() -
                                     child_policy.date_activated).days
                    amount_to_revise = (period_lasted / 365) * premium_change
                    refund_amount = amount_to_revise

                    # TODO: Revise transaction table and update date due using the amount to revise

                return make_response(helper.make_rest_success_response("Refund successful", refund_amount),
                                     200)

            elif transaction_type == 'CNC':
                # To cancel a transaction
                child_controller = ChildController()
                child_controller.cancel(policy_details['child_policy_id'])
                return make_response(helper.make_rest_success_response("Policy cancelled successfully"),
                                     200)

        else:
            response = helper.make_rest_fail_response("Failed")
            return make_response(response, 500)