コード例 #1
0
ファイル: product.py プロジェクト: DataSciCode/servo
def index_incoming(request, shipment=None, date=None, status=''):
    """
    Lists purchase order items that have not arrived yet
    """
    inventory = PurchaseOrderItem.objects.filter(date_received=None)

    if request.is_ajax():
        return HttpResponse(inventory.count())

    if request.POST.getlist('id'):
        count = len(request.POST.getlist('id'))
        
        for i in request.POST.getlist('id'):
            item = PurchaseOrderItem.objects.get(pk=i)
            item.date_received = datetime.now()
            item.received_by = request.user
            item.save()
        
        messages.add_message(request, messages.INFO, 
            _(u'%d tuotetta saavutettu' % count))

        return redirect('/products/incoming/')

    if request.GET.get('i'):
        item = PurchaseOrderItem.objects.get(pk=request.GET['i'])

        if request.method == 'POST':
            item.date_received = datetime.now()
            item.received_by = request.user
            form = PurchaseOrderItemForm(request.POST, instance=item)
            if form.is_valid():
                item = form.save()
                if item.order_item: # copy SN from POI to SOI
                    item.order_item.sn = item.sn
                    item.order_item.save()
                messages.add_message(request, messages.INFO, 
                    _(u'Tuote %s saavutettu' % item.code))
        else:
            form = PurchaseOrderItemForm(instance=item)

        return render(request, 'products/receive_item.html', {
            'form': form,
            'item': item
            })

    return render(request, 'products/index_incoming.html',
        {'inventory': inventory})
コード例 #2
0
ファイル: shipments.py プロジェクト: luetgendorf/Servo
def view_incoming(request, pk):
    """
    Shows an incoming part
    """
    next = False
    item = get_object_or_404(PurchaseOrderItem, pk=pk)

    data = prep_list_view(request)

    data['next'] = ""
    data['subtitle'] = item.code

    try:
        next = item.get_next_by_created_at(received_at=None)
        data['next'] = next.pk
    except PurchaseOrderItem.DoesNotExist:
        pass  # That was the last of them...

    if request.method == "POST":

        item.received_by = request.user
        item.received_at = timezone.now()

        form = PurchaseOrderItemForm(request.POST, instance=item)

        if form.is_valid():
            try:
                item = form.save()
            except gsxws.GsxError as e:
                messages.error(request, e)
                return redirect(view_incoming, date, pk)

            messages.success(request, _(u"Product %s received") % item.code)

            if next:
                return redirect(view_incoming, next.pk)
            else:
                return redirect(list_incoming)
    else:
        form = PurchaseOrderItemForm(instance=item)

    data['form'] = form
    data['item'] = item
    data['url'] = request.path

    return render(request, "products/receive_item.html", data)
コード例 #3
0
ファイル: product.py プロジェクト: DataSciCode/servo
def edit_po(request, id, item_id=None, action='add'):
    po = PurchaseOrder.objects.get(pk=id)
    form = PurchaseOrderForm(instance=po)

    if request.method == 'POST':
        data = request.POST.copy()
        data['created_by'] = request.user.id
        form = PurchaseOrderForm(data, instance=po)

        if not form.is_valid():
            return render(request, 'products/purchase_order.html', {
                'form': form, 'order': po
                })
            
        form.save()
        messages.add_message(request, messages.INFO,
            _(u'Ostotilaus tallennettu'))

        items = request.POST.getlist('items')
        prices = request.POST.getlist('prices')
        amounts = request.POST.getlist('amounts')
        
        for k, v in enumerate(items):
            item = PurchaseOrderItem.objects.get(pk=v)
            item.amount = int(amounts[k])
            item.price = prices[k]
            d = dict(amount=amounts[k], price=prices[k], 
                product=item.product.id,
                title=item.product.title,
                code=item.product.code)
            
            f = PurchaseOrderItemForm(d, instance=item)

            if not f.is_valid():
                print f.errors
                messages.add_message(request, messages.ERROR,
                    _('Tarkista tuote %s' % item.product.code))
                break

            f.save()

        return redirect('/products/po/')

    if item_id and action == 'add':
        product = Product.objects.get(pk=item_id)
        poi = PurchaseOrderItem.objects.create(code=product.code,
            purchase_order=po,
            product_id=product.id, 
            price=product.price_purchase)

        messages.add_message(request, messages.INFO, 
            _(u'Tuote #%d lisätty' % product.id))

        return redirect(po)

    if item_id and action == 'remove':
        poi = PurchaseOrderItem.objects.get(pk=item_id)
        pid = poi.product_id
        Inventory.objects.filter(slot=poi.purchase_order_id, 
            product_id=pid).delete()
        poi.delete()

        messages.add_message(request, messages.INFO, 
            _(u'Tuote #%d poistettu' % pid))

        return redirect(po)
    
    request.session['current_po'] = po

    return render(request, 'products/purchase_order.html', {'order': po,
        'form': form})