Exemple #1
0
def add_stock():
    if not current_user.is_authenticated:
        return redirect(url_for('login'))
    # add stock to database
    # declare the product form
    form = StockForm(request.form)
    form.product.choices = [(x.id, x.name) for x in Product.query.all()]
    form.stocktype.choices = [(x.id, x.name) for x in Stocktype.query.all()]

    msg = None
    if request.method == 'GET':
        form.process()
        return render_template('layouts/default.html',
                               content=render_template('pages/add-stock.html',
                                                       form=form,
                                                       message=msg))
    # check if both http method is POST and form is valid on submit
    if form.validate_on_submit():
        # assign form data to variables
        cost_price = request.form.get('cost_price', '', type=float)
        sell_price = request.form.get('sell_price', '', type=float)
        quantity = request.form.get('quantity', '', type=int)
        product_id = request.form.get('product', '', type=int)
        stocktype_id = request.form.get('stocktype', '', type=int)
        # see if stock entry already exists
        stock = Stock.query.filter_by(product_id=product_id,
                                      quantity=quantity,
                                      stocktype_id=stocktype_id).first()
        #
        if stock:
            flash(
                f'Error: A stock entry for {stock.quantity} {stock.product} already exists!'
            )
        else:
            stock = Stock(cost_price, sell_price, quantity, product_id,
                          stocktype_id)
            stock.save()
            flash(
                f'Stock for {stock.product.name} successfully created! Return to stock page or add another stock.'
            )
    else:
        flash('I am sorry but the details you entered cannot be saved :(')
    # print (msg)
    return render_template('layouts/default.html',
                           content=render_template('pages/add-stock.html',
                                                   message=msg,
                                                   form=form))
Exemple #2
0
    def post(self):
        '''Add stock.Admin only'''

        parser = reqparse.RequestParser()
        parser.add_argument('product_id',
                            help='The product_id field cannot be blank',
                            required=True,
                            type=int)
        parser.add_argument('quantity',
                            help='The quantity field cannot be blank',
                            required=True,
                            type=int)
        data = parser.parse_args()
        new_stock = Stock(id=None,
                          product_id=data['product_id'],
                          quantity=data['quantity'],
                          available=True)
        new_stock.save()
        return {"message": "stock added"}, 201