Ejemplo n.º 1
0
def create_customer(sales_id):
    sales = User.get_user_by_id_from_cache(sales_id)

    if sales is None:
        abort(400,
              description=gettext(
                  u'Incorrect sales, cannot find sales with id %(id)'),
              id=sales_id)

    data = request.json
    customer = None
    if data.get('mobile', None):
        customer = Customer.find_by_mobile(sales.store_id, data['mobile'])
        from application.api.viewhelper import handle_no_rx_customer
        customer = handle_no_rx_customer(customer)

    if customer:
        if customer.sales_id != sales.id:
            raise NoPermissionOnCustomerException(
                customer,
                gettext(u'The mobile belongs to other sales\' customer'))
        else:
            raise DuplicatedCustomerException(gettext(
                u'The mobile is the same as customer %(name)s',
                name=customer.respect_name),
                                              existing_customer_id=customer.id)
    else:
        customer = Customer(**data)
        customer.store_id = sales.store_id
        customer.sales_id = sales.id
        customer.reset_status()
        customer.save_and_flush()

    return customer, 201, add_location_header(
        dict(), url_for('api.get_customer', uid=customer.id))
Ejemplo n.º 2
0
def populate_customer_from_request(data, store_id, user_id):
    from application.models.customer import Customer

    customer_id = extract_customer_id(data)
    customer_data = data.get('customer')
    customer = None

    if not customer_id and not customer_data:
        abort(400,
              description=gettext(
                  'either customer_id or customer has to be provided'))
    elif customer_id:
        customer = Customer.find(customer_id)
        if not customer:
            abort(400,
                  description=gettext('customer with id %(id)s is not found',
                                      id=data['customer_id']))
        elif unicode(customer.sales_id) != unicode(user_id) and data.get(
                'force', None) != 'force':
            raise NoPermissionOnCustomerException(
                customer,
                gettext(
                    u'the customer %(name)s you are operating does not belong to you. Please contact your sales manager',
                    name=customer.name))
    else:
        if customer_data.get('mobile', None):
            customer = Customer.find_by_mobile(store_id,
                                               customer_data['mobile'])

        customer = handle_no_rx_customer(customer)

        if customer and unicode(
                customer.sales_id) != unicode(user_id) and data.get(
                    'force', None) != 'force':
            raise NoPermissionOnCustomerException(
                customer,
                gettext(
                    u'the customer %(name)s you are operating does not belong to you. Please contact your sales manager',
                    name=customer.name))

        if not customer:
            customer = Customer(**customer_data)
            customer.store_id = store_id
            customer.sales_id = user_id

    data['customer'] = customer
    return data
Ejemplo n.º 3
0
def search_sales_customer(sales_id):
    sales = User.get_user_by_id_from_cache(sales_id)
    data = request.json
    mobile = data.get('mobile', None)
    if mobile:
        customer = Customer.find_by_mobile(sales.store_id, mobile)
        if customer:
            if customer.sales_id == sales.id:
                return customer
            else:
                raise NoPermissionOnCustomerException(
                    customer,
                    gettext(u'The mobile belongs to other sales\' customer'))
        else:
            abort(400,
                  description=gettext(
                      u'Customer with mobile %(mobile)s not found',
                      mobile=mobile))
    else:
        abort(400, description=gettext(u'Mobile is empty'))