Пример #1
0
def add_product():
    form = AddProductForm()
    brands = Brand.read_all_bandss_filter_by("brand_id", "name")
    if request.method == "GET":
        form.restaurant.choices = Resturant.read_all_restaurants_filter_by(
            "id", "business_name")
        form.brand.choices = brands
        form.sub_cat.choices = SubCategory.read_all_subcategories_filter_by(
            "sub_category_id", "name")
        form.price.data = 0
        form.buying_price.data = 0
        form.selling_price.data = 0
        form.served_with.data = "none"
        form.commission_fee.data = 0.0
        form.headsup.data = "clickEat"

    if form.validate_on_submit():
        try:
            product_picture = save_picture(form.product_picture.data, None,
                                           "static/product_images", 500, 500)
            price = form.price.data
            rest_id = form.restaurant.data
            brand_name = [
                brand for brand in brands if brand[0] == form.brand.data
            ][0][1]
            if (Products()(name=form.name.data,
                           product_picture=product_picture,
                           description=form.description.data,
                           price=form.price.data,
                           resturant_id=form.restaurant.data,
                           brand=brand_name,
                           sub_category_id=form.sub_cat.data,
                           buying_price=form.buying_price.data,
                           selling_price=form.selling_price.data,
                           served_with=form.served_with.data,
                           commission_fee=form.commission_fee.data,
                           headsup=form.headsup.data)):
                flash("Product added successfully", "success")
                return redirect(url_for("customer_care.add_product"))

        except Exception as e:
            print("Error while trying to save product: ", e)
            session.rollback()
            flash("Error while trying to save product", "danger")

    return render_template('restaurants/restaurant/product/add_product.html',
                           form=form)
Пример #2
0
    def loginView(self):
        form = LoginForm()
        if current_user.is_authenticated and getattr(current_user, "account",
                                                     None) == "administrator":
            return redirect(self.get_url("admin.index"))

        if form.validate_on_submit():
            user = db.StaffAccounts.read_user(username=form.username.data)
            if user and user.verify_password(form.password.data):
                login_user(user)
                _session["account_type"] = "administrator"
                return redirect(self.get_url("admin.index"))
            else:
                flash("Username or Password is incorrect", "danger")
                return redirect(self.get_url(".loginView"))

        return self.render("admin/login.html", form=form)
Пример #3
0
def approve_product(product_id):
    suspend_form = SuspendProductForm()
    if suspend_form.validate_on_submit():
        try:
            product = Products.read_product(product_id)
            product.approved = True
            session.commit()
            flash("Product approved successfully", "success")
            return redirect(
                url_for("customer_care.rest_product_detail",
                        product_id=product_id))
        except Exception as e:
            session.rollback()
            print("Error while retriving data: ", e)
            flash("Internal server error failed to update product.", "danger")

    return redirect(
        url_for("customer_care.rest_product_detail", product_id=product_id))
Пример #4
0
def customer_care_login():
    form = LoginForm()

    if form.validate_on_submit():
        user = StaffAccounts.read_user(username=form.username.data)

        if not user:
            user = StaffAccounts.read_user(email=form.username.data)

        if user and user.verify_password(form.password.data):
            login_user(user)
            _session["account_type"] = "Employee"
            return redirect(url_for(".dashboard"))
        else:
            flash("User name or Password is incorrect", "danger")
            return redirect(url_for(".customer_care_login"))

    return render_template("customer_care_login.html", form=form)
Пример #5
0
def account_settings():
    account_settings_form = AccountSettingsForm()
    password_change_form = ChangePasswordForm()
    user = StaffAccounts.read_user(id=current_user.id)
    current_tab = request.args.get("tab", "personal")
    if request.method == "GET":
        account_settings_form = AccountSettingsForm(obj=current_user)

    if account_settings_form.validate_on_submit():
        user.update_employee_details(
            email=account_settings_form.email.data.strip(),
            name=account_settings_form.name.data.strip(),
            contact=account_settings_form.contact.data.strip(),
            address=account_settings_form.address.data.strip())
        flash("Updated your details successfully", "success")
        return redirect(url_for(".account_settings"))

    elif password_change_form.validate_on_submit():
        if user.verify_password(password_change_form.current_password.data):
            user.hash_password(password_change_form.new_password.data)
            session.commit()
            flash(
                "Password changed successfully, please try to log in with new password",
                "success")
            return redirect(
                url_for(".account_settings", current_tab="password"))
        else:
            flash("Current password is incorrect", "danger")
            return redirect(
                url_for(".account_settings", current_tab="password"))

    context = dict(account_settings_form=account_settings_form,
                   password_change_form=password_change_form,
                   current_tab=current_tab)
    return render_template("customer_care_account_settings.html", **context)
Пример #6
0
def set_promotional_price(product_id):
    promotional_price_form = SetPromotionForm()
    if promotional_price_form.validate_on_submit():
        try:
            product = Products.read_product(product_id)
            product.promotional_price_set = True
            ProductDiscounts()(product_id=product_id,
                               price=promotional_price_form.price.data,
                               from_date=promotional_price_form.from_date.data,
                               to_date=promotional_price_form.to_date.data,
                               is_scheduled=True)
            flash("Product promotional price set successfully", "success")
            return redirect(
                url_for("customer_care.rest_product_detail",
                        product_id=product_id))
        except Exception as e:
            session.rollback()
            print("Error while retriving data: ", e)
            flash("Internal server error failed to update product.", "danger")

    return redirect(
        url_for("customer_care.rest_product_detail", product_id=product_id))
Пример #7
0
def terminate_order(order_id):
    form = ReasonForm()
    order = Order.read_order(id=order_id)
    if order.is_terminated and order.termination_reason:
        flash("Order already terminated", "danger")
        return redirect(
            url_for(".customer_care_order_detail", order_id=order_id))

    if order.is_paid:
        flash(
            "Cannot terminate already paid order, items have to be returned and customer compensated",
            "danger")
        return redirect(
            url_for(".customer_care_order_detail", order_id=order_id))

    if form.validate_on_submit() and order:
        if order.customer_care_terminate_order(form.reason.data):
            flash("Order terminated successfully", "success")
        else:
            flash("Failed to terminate order", "danger")
    else:
        flash("Failed to terminate order", "danger")

    return redirect(url_for(".customer_care_order_detail", order_id=order_id))
Пример #8
0
def deleteFromTopSelling(product_id):
    suspend_form = SuspendProductForm()
    if suspend_form.validate_on_submit():
        try:
            if (TopSellingProducts.delete_pdt_from_top_selling(product_id)):
                flash("Product was removed successfully!!", "success")
                return redirect(
                    url_for("customer_care.rest_product_detail",
                            product_id=product_id))
            else:
                flash("Error while trying to delete product from top selling",
                      "danger")
                return redirect(
                    url_for("customer_care.rest_product_detail",
                            product_id=product_id))
        except Exception as e:
            session.rollback()
            print("Error while retriving data: ", e)
            flash("Internal server error failed to update product.", "danger")
Пример #9
0
def addToTopSelling(product_id):
    suspend_form = SuspendProductForm()
    if suspend_form.validate_on_submit():
        try:
            if TopSellingProducts()(product_id=product_id):
                flash(
                    "Product added to top most selling products successfully!!",
                    "success")
                return redirect(
                    url_for("customer_care.rest_product_detail",
                            product_id=product_id))
            else:
                flash("Product already added to Top most selling", "danger")
                return redirect(
                    url_for("customer_care.rest_product_detail",
                            product_id=product_id))
        except Exception as e:
            session.rollback()
            print("Error while retriving data: ", e)
            flash("Internal server error failed to update product.", "danger")
Пример #10
0
def remove_promotion_price(product_id):
    suspend_form = SuspendProductForm()
    if suspend_form.validate_on_submit():
        try:
            if ProductDiscounts.remove_promotion_price(product_id):
                flash("Product promotional price removed successfully",
                      "success")
                return redirect(
                    url_for("customer_care.rest_product_detail",
                            product_id=product_id))
            else:
                flash("Product promotional price was not removed successfully",
                      "danger")
                return redirect(
                    url_for("customer_care.rest_product_detail",
                            product_id=product_id))
        except Exception as e:
            session.rollback()
            print("Error while retriving data: ", e)
            flash("Internal server error failed to update product.", "danger")

    return redirect(
        url_for("customer_care.rest_product_detail", product_id=product_id))
Пример #11
0
def rest_product_detail(product_id):
    product = Products.read_product(product_id)
    top_selling_status = TopSellingProducts.read_top_most_selling_product(
        product_id)
    comments = Comments.product_comments(product_id)
    product_rating = Rate.read_product_rate(product_id)
    restuarant = Resturant.read_restaurant(id=product.resturant_id)
    form = ProductsVerificationForm()
    promotional_price_form = SetPromotionForm()
    suspend_form = SuspendProductForm()
    suspend_form.product_id.data = product_id

    if request.method == "GET":
        form = ProductsVerificationForm(obj=product)
        form.sub_category.choices = SubCategory.read_all_subcategories_filter_by(
            "sub_category_id",
            "name",
            category_id=product.sub_category.category_id)

    # form.category.choices = Category.read_all_categories_by_attr("category_id","name")

    if form.validate_on_submit():
        if form.product_picture.data:
            try:
                product_pic = save_picture(form.product_picture.data,
                                           product.product_picture,
                                           "static/product_images", 500, 500)
                # product.sub_category_id = form.sub_category.data
                product.name = form.name.data
                product.product_picture = product_pic
                product.price = form.price.data
                product.description = form.description.data
                product.product = form.price.data
                product.buying_price = form.buying_price.data
                product.selling_price = form.selling_price.data
                product.served_with = form.served_with.data
                product.commission_fee = form.commission_fee.data
                product.headsup = form.headsup.data
                product.free_delivery = form.free_delivery.data
                session.commit()
                flash("Product updated successfully", "success")
                return redirect(
                    url_for("customer_care.rest_product_detail",
                            product_id=product_id))
            except Exception as e:
                print("Customer care updating product error:", e)
                session.rollback()
                flash("Internal server error failed to update product.",
                      "danger")
        else:
            try:
                # product.sub_category_id = form.sub_category.data
                product.name = form.name.data
                product.price = form.price.data
                product.description = form.description.data
                product.product = form.price.data
                product.buying_price = form.buying_price.data
                product.selling_price = form.selling_price.data
                product.served_with = form.served_with.data
                product.commission_fee = form.commission_fee.data
                product.headsup = form.headsup.data
                product.free_delivery = form.free_delivery.data
                session.commit()
                flash("Product updated successfully", "success")
            except Exception as e:
                print("Customer care updating product error:", e)
                session.rollback()
                flash("Internal server error failed to update product.",
                      "danger")
        return redirect(
            url_for("customer_care.rest_product_detail",
                    product_id=product_id))

    return render_template(
        'restaurants/restaurant/product/product_details.html',
        shop=restuarant,
        product=product,
        product_rating=product_rating,
        comments=comments,
        form=form,
        promo_form=promotional_price_form,
        suspend_form=suspend_form,
        top_selling_status=top_selling_status)
Пример #12
0
    def customer_care_register_order_sales(cls, **kwargs):
        with current_app.app_context():
            try:
                order = kwargs.get("order")
                data = kwargs.get("data")

                if "order_id" not in data:
                    abort(403)
                if int(data["order_id"]) != int(order.id):
                    abort(403)

                if "customer_received" in data:
                    if order.is_prepared == False:
                        flash("Order has to first be prepared", "danger")
                        return jsonify()
                    if order.is_paid == False:
                        flash("Order has to first be paid", "danger")
                        return jsonify()
                    if data["customer_received"] == True and order.customer_received == False:
                        order.customer_received = True
                        session.commit()
                        flash("Order status 'Customer received' has been set",
                              "success")
                    else:
                        flash(
                            "Order status 'Customer received' was already set",
                            "info")

                elif "is_prepared" in data:
                    if data["is_prepared"] == True and order.is_prepared == False:
                        order.is_prepared = True
                        session.commit()
                        flash("Order status 'Prepared' has been set",
                              "success")
                    else:
                        flash("Order status 'Prepared' was already set",
                              "info")

                elif "courier_id" in data:
                    if order.is_prepared == False:
                        flash("Order has to be first prepared", "danger")
                        return jsonify()
                    delivery_detials.DeliveryDetails.assign_courier(
                        int(data["courier_id"]), int(order.id))
                    flash("Courier has been set for this order successfully",
                          "success")

                elif "is_paid" in data:
                    if order.is_prepared == False:
                        flash("Order has to first  be prepared", "danger")
                        return jsonify()
                    if data["is_paid"] == True and order.is_paid == False:
                        order.is_paid = True
                        cash_on_delivery = pym.CashOnDelivery.read_cash_on_delivery(
                            payment_id=order.payment[0].payment_id)
                        delivery_details = order.delivery_details[0]
                        if delivery_details.courier_id == None:
                            flash(
                                "Order paid can only be set when courier has been set.",
                                "danger")
                            session.rollback()
                            return jsonify()
                        if not cash_on_delivery:
                            flash(
                                "Order paid can only be set for cash on delivery items.",
                                "danger")
                            session.rollback()
                            return jsonify()
                        else:
                            cash_on_delivery.status = "confirmed"
                        for pdt_ in order.cart:
                            product = session.query(pdts.Products).filter_by(
                                product_id=pdt_.product_id).first()
                            if product:
                                sales.Sales()(
                                    product_id=pdt_.product_id,
                                    quantity=pdt_.quantity,
                                    amount=pdt_.total,
                                    commission_amount=product.commission_amount
                                    if product.commission_amount else 0)
                        if order.customer.email:
                            items = order.cart
                            cart_items_total = []
                            items_str = ""
                            item_string = ""
                            for i in items:
                                item = i.serialize()
                                item_string = f"<li>Item: {item['product_name']} from {item['restaurant']} Quantity: {item['quantity']} <b>SubTotal: {'{:,} Ugx'.format(item['total'])}</b></li>"
                                items_str += item_string
                                cart_items_total.append(item['total'])
                            total = items[0].serialize()
                            items_total = sum(cart_items_total)
                            subject_customer = """
                                        <html>
                                            <p>Written by [email protected]<br>
                                                Office Address:<br>
                                                Afra Road,<br>
                                                Near Hindu Temple,<br>
                                                Room 08,<br>
                                                Arua City, Uganda.
                                            </p>
                                            <p>Dear <b>{user_name}</b></p>,
                                            <p>You have made a payment for the following items.</p>
                                            <p>Order Reference: <b>{order_ref}</b></p>
                                            <p>Order Date: {order_date}</p>
                                            <hr>
                                            <ul style='list-style-type: none;display: block;width: 100%;padding: 0px 10px;overflow: hidden;'>
                                                {items_str}
                                            </ul>
                                            <hr>
                                            <p>Items Total: <b>{total}</b></p>
                                            <p>Payment Method: {payment_method}</p>
                                            <p>Delivery Price: {delivery_fee}</p>
                                            <p>Total: {order_total}</p>
                                            <p>Status: <b>Paid</b></p>
                                            <hr>

                                            <p><b>Thank you for buying on ClickEat, please come gain :)</b></p>
                                            <p>You have received this email because you are a member of ClickEat.</p>
                                            <p>For any help please contact us by clicking 0785857000/0777758880</p>
                                        </html>
                            """.format(
                                user_name=order.customer.name,
                                order_ref=order.order_ref_simple_version,
                                order_date="{: %d/%m/%Y}".format(
                                    order.order_date),
                                items_str=items_str,
                                total='{:,} Ugx'.format(items_total),
                                payment_method=order.payment[0].payment_method.
                                serialize(),
                                delivery_fee='{:,} Ugx'.format(
                                    order.delivery_fee),
                                order_total='{:,} Ugx'.format(
                                    order.delivery_fee + items_total))
                            mail_ = order_receipt_email
                            mail_.recipients = [order.customer.email]
                            mail_.text = subject_customer
                            # mail_.context = dict(
                            #     items = [i.serialize() for i in order.cart],
                            #     order_ref = order.order_ref_simple_version,
                            #     order_date="{: %d/%m/%Y}".format(order.order_date),
                            #     user_name = order.customer.name,
                            #     delivery_fees = order.delivery_fee,
                            #     payment_method = order.payment[0].payment_method.serialize(),
                            #     customer_received = order.customer_received
                            # )
                            mail_.send()
                            session.commit()
                            flash("Order status has been set to paid",
                                  "success")

                        else:
                            session.commit()
                            flash("Order status has been set to paid",
                                  "success")
                    else:
                        session.rollback()
                        flash("Order status already set to paid", "info")
            except Exception as e:
                session.rollback()
                print("Updating Order statuses error: ", e)
                flash(f"Error: {e}", "danger")