Пример #1
0
def update_stock(request, company):
    try:
        c = Company.objects.get(url_name=company)
    except Company.DoesNotExist:
        return JsonError(_("Company does not exist."))

    if not has_permission(request.user, c, 'stock', 'edit'):
        return JsonError(_("You have no permission to edit stock"))

    data = JsonParse(request.POST.get('data'))

    try:
        stock = Stock.objects.get(id=data['stock_id'], company=c)
        stock.name = data['stock_name']
        stock.save()

        StockProduct.objects.filter(stock=stock).delete()

        for key, value in data['stock_products'].iteritems():
            product = Product.objects.get(id=value['product_id'], company=c)

            stock_product = StockProduct(stock=stock, product=product, deduction=value['deduction'], created_by=request.user)
            stock_product.save()

        history_info = {'previous_state': serializers.serialize("json", [stock])}

        if "stock_left" in data and "stock_purchase_price" in data:
            if Decimal(data["stock_left"]) > stock.stock:
                return JsonError("left_stock_bigger_than_stock")

            stock.left_stock = data["stock_left"]
            stock.purchase_price = data["stock_purchase_price"]
            stock.save()
        else:
            return JsonError("stock_left_or_stock_purchase_price_missing")

        history_info['new_state'] = serializers.serialize("json", [stock])
        history_info['reason'] = data['reason']

        stock_history = StockUpdateHistory(stock=stock, history_info=json.dumps(history_info), created_by=request.user)
        stock_history.save()

        stock_products = []
        for sp in StockProduct.objects.filter(stock=stock):
            stock_products.append({"stock_id": str(sp.stock.id), "stock_product_id": str(sp.id), "product_id": str(sp.product.id), "product_name": sp.product.name, "deduction": str(sp.deduction)})

    except Exception as e:
        print "Error updating stock"
        print e

        return JsonError('could_not_update_stock')

    """
    if 'for_product' in data:
        for fp in data['for_product']:
            product_id = fp[0]
            deduction = fp[1]

            if deduction == "":
                deduction = 1

            try:
                product = Product.objects.get(id=int(product_id))
            except Product.DoesNotExist:
                continue

            try:
                stock_product = StockProduct(stock=stock, product=product, deduction=deduction, created_by=request.user)
                stock_product.save()
            except Exception as e:
                print "Error saving stock product"
                print e

                return JsonError('could_not_save_stock_product')

    stocks = Stock.objects.filter(company=c).order_by('-datetime_created')
    page = 1
    i = 0
    """

    return JsonOk(extra={'stock_name': stock.name, 'stock_products': json.dumps(stock_products), 'stock_history': json.dumps(stock.stock_history)})
Пример #2
0
def save_stock(request, company):
    try:
        c = Company.objects.get(url_name=company)
    except Company.DoesNotExist:
        return JsonError(_("Company does not exist."))

    # permissions
    if not has_permission(request.user, c, 'document', 'edit'):
        return JsonError(_("You have no permission to edit documents"))

    if not has_permission(request.user, c, 'stock', 'edit'):
        return JsonError(_("You have no permission to edit stock"))

    data = JsonParse(request.POST.get('data'))

    document = None

    if 'document' in data and data['document'] == '' or data['document'] == 'all':
        document = None
    else:
        document = Document.objects.get(id=data['document'])

    # TODO: validate with forms

    try:
        stock = Stock(
            company=c,
            document=document,
            name=data['stock_name'],
            # quantity=data['quantity'],
            stock=data['stock'],
            left_stock=data['stock'],
            stock_type=data['type'],
            unit_type=data['unit'],
            purchase_price=data['purchase_price'],
            created_by=request.user
        )
        stock.save()

    except Exception as e:
        print "Error saving stock or saving purchase price"
        print e

        return JsonError('could_not_save_stock')

    if 'for_product' in data:
        for fp in data['for_product']:
            product_id = fp[0]
            deduction = fp[1]

            if deduction == "":
                deduction = 1

            try:
                product = Product.objects.get(id=int(product_id))
            except Product.DoesNotExist:
                continue

            try:
                stock_product = StockProduct(stock=stock, product=product, deduction=deduction, created_by=request.user)
                stock_product.save()
            except Exception as e:
                print "Error saving stock product"
                print e

                return JsonError('could_not_save_stock_product')

    stocks = Stock.objects.filter(company=c).order_by('-datetime_created')
    page = 1
    i = 0

    for s in stocks:
        if i > 0 and i%g.MISC['stocks_per_page'] == 0:
            page += 1

        if s == stock:
            break

        i += 1

    redirect_url = reverse('pos:manage_stock', kwargs={'company': c.url_name, 'page': page}) + "#" + str(stock.id)

    return JsonOk(extra={'redirect_url': redirect_url})