Example #1
0
def makerStock():
    products = None
    if g.user.maker:
        products = Product.query.filter_by(active_flg=True)\
            .filter_by(maker_id=g.user.maker_id)\
            .all()

    if products:
        for p in products:
            if not p.price_unit:
                p.price_unit = 0
            urls = getImgUrls(p.id)
            p.img_urls = []
            p.img_thumb_urls = []
            if urls:
                for u in urls:
                    u = u.split('app')[1]
                    p.img_urls.append(u)
                    p.img_thumb_urls.append(getThumbUrls(u, height=80, width=80).split('app')[1])
            else:
                p.img_urls.append(NO_PHOTO_URL.split('app')[1])
                p.img_thumb_urls.append(NO_PHOTO_THUMB_URL.split('app')[1])

    form = SimpleSubmitForm()

    if form.is_submitted():
        string = request.form.getlist('product_data')
        if string:
            data_to_update = []
            for s in string:
                raw = s.split(',')
                id = int(raw[0].split(':')[1])
                code = raw[1].split(':')[1]
                str_qty = raw[2].split(':')[1]
                qty = 0
                if str_qty:
                    try:
                        qty = int(str_qty)
                    except ValueError:
                        qty = 0;
                data_to_update.append({'id': id, 'code': code, 'qty': qty})
            if data_to_update:
                for d in data_to_update:
                    p = Product.query.get(d['id'])
                    p.maker_code = d['code']
                    p.maker_qty_stock = d['qty']
                    db.session.add(p)
                db.session.commit()


    return render_template('maker/makerStock.html',
                           title=gettext("My Stock"),
                           products=products,
                           form=form)
Example #2
0
def shop(page=1):
    products = Product.query.filter_by(active_flg=True)

    curr_search = None
    if session['search_string']:
        curr_search = session['search_string']
        products = products.filter(or_(Product.code.ilike('%' + session['search_string'] + '%'),
                                       (Product.desc_CS.ilike('%' + session['search_string'] + '%')),
                                       (Product.desc_JP.ilike('%' + session['search_string'] + '%'))))

    curr_catalog_terms = None
    if session['catalog_ids']:
        cps = CatalogedProducts.query.filter(CatalogedProducts.catalog_id.in_(session['catalog_ids'])).all()
        cataloged_product_ids = []
        curr_catalog_terms = []
        for cp in cps:
            cataloged_product_ids.append(cp.product_id)
        for id in session['catalog_ids']:
            curr_catalog_terms.append(Catalog.query.get(id).name_JP)
        products = products.filter(Product.id.in_(cataloged_product_ids))

    if g.category_id:
            products = products.filter_by(category_id=int(g.category_id))

    if session['available_only'] is True:
        products = products.filter(Product.available_qty > 0)

    products = products.order_by(Product.maker_id, Product.code)
    products = products.paginate(page, current_user.products_per_page, False)

    discount = 0
    if current_user.role == USER_ROLES['ROLE_CUSTOMER'] and current_user.customer:
        discount = current_user.customer.base_discount
    for p in products.items:

        if not p.price_retail:
            p.price_retail = 0
        unrounded_price = p.price_retail * (1.0 - discount)
        p.customer_price = int(5 * round(float(unrounded_price)/5))

        urls = getImgUrls(p.id)
        p.img_urls = []
        p.img_thumb_urls = []
        if urls:
            for u in urls:
                u = u.split('app')[1]
                p.img_urls.append(u)
                p.img_thumb_urls.append(getThumbUrls(u).split('app')[1])
        else:
            p.img_urls.append(NO_PHOTO_URL.split('app')[1])
            p.img_thumb_urls.append(NO_PHOTO_THUMB_URL.split('app')[1])
    form = ShopHeaderForm()
    categories = [(a.id, a.name_JP) for a in Category.query.all()]
    categories = [(0, gettext('All'))] + categories
    form.category.choices = categories
    catalog = prepare_catalog()
    return render_template('/shop/shop.html',
                           title=gettext('AxiStore shop'),
                           products=products,
                           curr_category=g.category_id,
                           axm_product_url=AXM_PRODUCT_URL_JA,
                           curr_search=curr_search,
                           curr_catalog_terms=curr_catalog_terms,
                           catalog=catalog,
                           form=form)
Example #3
0
def placeorder():

    cart_items = Cart.query.filter_by(user_id=current_user.id).all()

    form = ShopOrderConfirmForm()
    if form.validate_on_submit():
        new_request = Request()
        new_request.user_id = current_user.id
        if not current_user.customer:
            flash(gettext("We apologize but your customer data is insufficient. Please, contact our customer support."))
            return redirect(url_for("shop"))

        customer_note = form.note.data
        if customer_note:
            new_request.note = customer_note

        new_request.customer_id = current_user.customer.id
        new_request.active_flg = True
        db.session.add(new_request)
        db.session.commit()

        for item in cart_items:
            new_product = db.session.query(Product).filter_by(id=item.product_id).first()
            if not new_product:
                flash(gettext("Product not found."))
                return redirect(url_for("shop"))
            rp = RequestedProducts(quantity=item.quantity)
            rp.product = new_product
            rp.request_id = new_request.id
            new_request.products.append(rp)
            db.session.delete(item)

        db.session.commit()

        # send confirmation email
        mailer.order_confirmation(current_user, new_request)

        flash(gettext("Order created successfully."))
        return redirect(url_for("orderconfirm", req_id=new_request.id))

    discount = 0
    if current_user.role == USER_ROLES['ROLE_CUSTOMER'] and current_user.customer:
        discount = current_user.customer.base_discount
    total = 0
    pieces = 0
    for item in cart_items:
        if not item.product.price_retail:
            item.product.price_retail = 0
        unrounded_price = item.product.price_retail * (1.0 - discount)
        item.customer_price = int(5 * round(float(unrounded_price)/5))
        total += item.customer_price * item.quantity
        pieces += item.quantity

        urls = getImgUrls(item.product.id)
        if urls:
            item.img_url = urls[0].split('app')[1]
        else:
            item.img_url = NO_PHOTO_URL.split('app')[1]

    return render_template('/shop/placeorder.html',
                           title=gettext('Place order'),
                           cart_items=cart_items,
                           total=total,
                           pieces=pieces,
                           form=form)