def create_customer(self):
        customer = Customers()
        customer.partner_id = self.partner.id
        customer.company_name = self.partner.company_name
        customer.address = self.partner.address_1 + '' + self.partner.address_2
        customer.city = self.partner.city
        customer.state = self.partner.state
        customer.Pincode = self.partner.pin_code
        customer.country = self.partner.state
        customer.pan_number = ''
        customer.deleted = False
        customer.customer_vertical = None
        customer.delivery_sequence = '000'
        customer.save()
        print('Customer %s created' % customer.company_name)

        self.customer = customer
        ''' Creating customer contacts1 '''
        self.create_customer_contacts()
        ''' Creating customer contacts2 '''
        self.create_customer_contacts()
Ejemplo n.º 2
0
    def create(self, request, *args, **kwargs):
        data = request.data
        tenant_id = None
        is_valid_domain = None
        if 'logo' in request.FILES:
            logo = request.FILES['logo']
            customer_data = json.loads(data['data'])
        else:
            logo = None
            customer_data = json.loads(data['data'])

        # initial_partner_obj = InitialPartner.objects.filter(id=1)

        customer_obj = Customers()
        if logo != None:
            customer_obj.logo = logo

        if not request.user.is_superuser:
            if request.user.profile.user_type == 'P':
                customer_data['partner_id'] = request.user.partner.first(
                ).partner.id

        if 'cloud_account' in customer_data.keys():
            cloud_account = customer_data['cloud_account']
            if cloud_account['type'] == 'MS':
                domain = cloud_account['details']['domain_name']
                if domain and domain != '':
                    ms_api = MicrosoftApi()
                    domain_exist = ms_api.check_domain_exists(domain)
                    if not domain_exist:
                        return Response('Invalid_domain')
                    else:
                        customer_info = ms_api.get_customer_from_domain(domain)
                        if 'companyProfile' in customer_info:
                            tenant_id = customer_info['companyProfile'][
                                'tenantId']
                            is_valid_domain = True

        customer_obj.company_name = customer_data['company_name']
        customer_obj.address = customer_data['address']
        customer_obj.city = customer_data['city']
        customer_obj.state = customer_data['state']
        customer_obj.Pincode = customer_data['postcode']
        customer_obj.country = customer_data['country']
        customer_obj.pan_number = customer_data['pan_number']
        customer_obj.customer_vertical = customer_data['customer_vertical'][0][
            'id']
        customer_obj.delivery_sequence = customer_data['delivery_sequence']
        customer_obj.segment = customer_data['segment'][0]['id']
        if 'partner_id' in customer_data:
            partnerId = customer_data['partner_id']
            customer_obj.partner_id = partnerId
        customer_obj.save()

        try:
            # Inserting primary contact details of Customer
            self.saveContactDetails(customer_data['primary_contact'],
                                    customer_obj.id)
        except Exception:
            return Response(Exception)

        try:
            # Inserting secondary contact details of Customer
            self.saveContactDetails(customer_data['secondary_contact'],
                                    customer_obj.id)
        except Exception:
            return Response(Exception)
        """ Validating and Storing customer's cloud account requirements """
        if 'cloud_account' in customer_data.keys():
            try:
                cloud_account_data = customer_data['cloud_account']
                if tenant_id is not None:
                    cloud_account_data['details']['tenant_id'] = tenant_id
                    cloud_account_data['details']['active'] = is_valid_domain
                    if is_valid_domain:
                        cloud_account_data['active'] = True
                cloud_account_data['customer'] = customer_obj.id
                cloud_account_serializer = CloudAccountsSerializer(
                    data=cloud_account_data, context={'request': request})
                cloud_account_serializer.is_valid(raise_exception=True)
                cloud_account_serializer.save()
            except Exception:
                customer_obj.delete()
                return Response(Exception)

            if cloud_account['type'] == 'MS':
                domain = cloud_account_data['details']['domain_name']
                if tenant_id is None and domain != '':
                    data['customer'] = customer_obj.id
                    CloudAccountsViewSet.store_ms_domain(
                        data, domain, cloud_account_serializer.data['id'])

        return Response(customer_obj.id)