Beispiel #1
0
def mobile_list_taxes(request, company_id):
    try:
        c = Company.objects.get(id=company_id)
    except Company.DoesNotExist:
        return JsonError(_("Company does not exist"))

    taxes = get_all_taxes(request.user, c)
    return JsonOk(extra=taxes)
Beispiel #2
0
def products(request, company):
    c = get_object_or_404(Company, url_name=company)
    
    # needs to be at least guest to view products
    if not has_permission(request.user, c, 'product', 'view'):
        return no_permission_view(request, c, _("You have no permission to view products."))

    # if there are no taxes defined, don't show anything
    if Tax.objects.filter(company=c).count() == 0:
        return error(request, c, _("There are no taxes defined. Please go to tax management and define them."))

    # if there are no categories defined, throw an error
    if Category.objects.filter(company=c).count() == 0:
        return error(request, c, _("There are no categories defined, please go to category management to define them."))

    # fields that need to be limited in length:
    lengths = {
        'code': max_field_length(Product, 'code'),
        'price': g.DECIMAL['currency_digits'] + 1,
        'purchase_price': g.DECIMAL['currency_digits'] + 1,
        'shortcut': max_field_length(Product, 'shortcut'),
        'stock': g.DECIMAL['quantity_digits'],
        'name': max_field_length(Product, 'name'),
        'tax': g.DECIMAL['percentage_decimal_places'] + 4,  # up to '100.' + 'decimal_digits'
    }
    
    context = {
        'company': c,
        'title': _("Products"),
        'site_title': g.MISC['site_title'],
        # lists
        'taxes': JsonStringify(get_all_taxes(request.user, c)),
        'categories': JsonStringify(get_all_categories(c, json=True)),
        'units': JsonStringify(g.UNITS),
        'discounts': JsonStringify(get_all_discounts(request.user, c)),
        # urls for ajax calls
        'add_url': reverse('pos:create_product', args=[c.url_name]),
        # config variables
        'can_edit': has_permission(request.user, c, 'product', 'edit'),
        'currency': get_company_value(request.user, c, 'pos_currency'),
        # images
        'image_dimensions': g.IMAGE_DIMENSIONS['product'],
        'image_upload_formats': g.MISC['image_upload_formats'], # what can be uploaded
        'max_upload_size': round(g.MISC['max_upload_image_size']/2**20, 2), # show in megabytes
        'max_upload_size_bytes': g.MISC['max_upload_image_size'], # bytes for javascript
        # html fields
        'field_lengths': lengths,
        'separator': get_company_value(request.user, c, 'pos_decimal_separator'),
        # numbers etc
        'default_tax_id': get_default_tax(request.user, c)['id'],
        'decimal_places': get_company_value(request.user, c, 'pos_decimal_places')*2,  # ACHTUNG: rounding comes at the end
    }
    return render(request, 'pos/manage/products.html', context)
Beispiel #3
0
def mobile_sync_db(request, company_id):

    try:
        company = Company.objects.get(id=company_id)
    except Company.DoesNotExist:
        return JsonError(_("Company does not exist"))

    data = JsonParse(request.POST['data'])

    seq = data['database_version']
    device_id = data['device_id']

    sync_objects = Sync.objects.only('seq')\
        .filter(company=company).order_by('-seq')

    last_key = 0

    if len(sync_objects) > 0:
        last_key = sync_objects[0].seq

    ret = {'version': last_key}

    if seq == 0 or last_key - 5000 > seq or seq > last_key:

        # if first sync or sync outdated, why not load whole DB

        ret['updated'] = False
        ret['drop'] = True

        # get taxes
        ret['taxes'] = get_all_taxes(request.user, company)

        # get contacts
        contacts = Contact.objects.filter(company=company)
        cs = []
        for contact in contacts:
            cs.append(contact_to_dict(request.user, company, contact))

        ret['contacts'] = cs

        # get categories
        categories = Category.objects.filter(company=company)
        ct = []

        for category in categories:
            ct.append(category_to_dict(category, android=True))

        ret['categories'] = ct

        # get discounts
        discounts = Discount.objects.filter(company=company)
        ds = []

        for discount in discounts:
            ds.append(discount_to_dict(request.user, company, discount, android=True))

        ret['discounts'] = ds

        # get products
        products = Product.objects.filter(company=company)
        pr = []
        for product in products:
            pr.append(product_to_dict(request.user, company, product, android=True))

        ret['products'] = pr

        try:
            register = Register.objects.get(company=company, device_id=device_id)
            ret['register'] = register_to_dict(request.user, company, register)
        except Register.DoesNotExist:
            ret['register'] = None

        ret['company'] = company_to_dict(request.user, company, android=True, with_config=True)

    elif seq < last_key:

        ret['updated'] = False
        ret['drop'] = False
        seq_list = Sync.objects.filter(company=company, seq__lte=last_key).order_by('seq')

        items = []

        for seq_item in seq_list:
            item_ret = {'action': seq_item.action,
                        'model':  seq_item.model,
                        'object_id': seq_item.object_id}

            if seq_item.action == 'save':
                if seq_item.model == 'Category':
                    c = Category.objects.get(id=seq_item.object_id)
                    item_ret['item'] = category_to_dict(c, android=True)

                elif seq_item.model == 'Product':
                    p = Product.objects.get(id=seq_item.object_id)
                    item_ret['item'] = product_to_dict(request.user, company, p, android=True)

                elif seq_item.model == 'Tax':
                    t = Tax.objects.get(id=seq_item.object_id)
                    item_ret['item'] = tax_to_dict(request.user, company, t)

                elif seq_item.model == 'Discount':
                    d = Discount.objects.get(id=seq_item.object_id)
                    item_ret['item'] = discount_to_dict(request.user, company, d, android=True)

                elif seq_item.model == 'Contact':
                    con = Contact.objects.get(id=seq_item.object_id)
                    item_ret['item'] = contact_to_dict(request.user, company, con)

                elif seq_item.model == 'Register':
                    r = Register.objects.get(id=seq_item.object_id)
                    item_ret['item'] = register_to_dict(request.user, company, r)

                elif seq_item.model == 'Company':
                    item_ret['item'] = company_to_dict(request.user, company, android=True, with_config=True)

            items.append(item_ret)

        ret['items'] = items

    else:
        ret['updated'] = True

    return JsonOk(extra=ret, safe=False)