コード例 #1
0
def tpl_list(products):

    cart_line = u"<code>"
    cart_line += st_col(2, u"№", True) + "|"
    cart_line += st_col(6, u"цена") + "|"
    cart_line += st_col(5, u"кол.") + "|"
    cart_line += st_col(7, u"сумма") + "\n\r"

    cart_line += ('-' * 23) + u"\n\r"

    total = 0
    for product in products:
        sym = Decimal(product["price"]) * Decimal(product["quantity"])
        total += sym

        cart_line += st_col(2, empty_nl(str(product["product_id"])),
                            True) + u"|"
        cart_line += st_col(6, empty_nl(str(product["price"]))) + u"|"
        cart_line += st_col(5, empty_nl(str(product["quantity"]))) + u"|"
        cart_line += st_col(7, empty_nl(str(sym)))
        cart_line += u"\n\r"

    cart_line += u"</code>\n\rИтого:  <b>" + empty_nl(str(total)) + u"</b>\n\r"

    return cart_line
コード例 #2
0
def tpl_edit_msg(price, quant, total, isPrice, product_id):

    res = u"<code>"

    if product_id is not None:
        res += u"Товар №" + str(product_id) + u"\n\r"

    if isPrice:
        res += st_col(10, u">>Цена: ") + " " + st_col(
            15, empty_nl_temp(str(price)), True) + u"\n\r"
        res += st_col(10, u"Кол-во: ") + " " + st_col(
            15, empty_nl_temp(str(quant)), True) + u"\n\r"
    else:
        res += st_col(10, u"Цена: ") + " " + st_col(
            15, empty_nl_temp(str(price)), True) + u"\n\r"
        res += st_col(10, u">>Кол-во: ") + " " + st_col(
            15, empty_nl_temp(str(quant)), True) + u"\n\r"

    res += st_col(10, u"Сумма: ") + " " + st_col(
        15, empty_nl(str(Decimal(price) * Decimal(quant))), True) + u"\n\r"
    res += u"</code>"

    res += u"Общая сумма: <b>" + empty_nl(str(total)) + u"</b>"

    return res
コード例 #3
0
def tpl_delProduct(id, st_amount):

    resMsg = ""

    if st_amount is not None:
        resMsg = u"<code>Товар №" + empty_nl(str(id)) + u" удалён.</code>\n\r"
        resMsg += u"Общая сумма: <b>" + st_amount + u"</b>"
    else:
        resMsg = u"<code>Не удалось удалить товар №" + empty_nl(
            str(id)) + u"!</code>"

    return resMsg
コード例 #4
0
def tpl_editProduct(editProduct):

    resMsg = u"<code>Товар отредактирован:\n\r"

    resMsg += st_col(2, u"№", True) + "|"
    resMsg += st_col(6, u"цена") + "|"
    resMsg += st_col(5, u"кол.") + "|"
    resMsg += st_col(7, u"сумма") + "\n\r"

    if editProduct["product_id"] == 1:
        resMsg += ('-' * 23) + "\n\r"
    else:
        resMsg += '-----------//-----------\n\r'

    resMsg += st_col(2, empty_nl(str(
        editProduct["product_id"])), True) + "|" + st_col(
            6, empty_nl(str(editProduct["price"]))) + "|" + st_col(
                5, empty_nl(str(editProduct["quantity"]))) + "|" + st_col(
                    7, empty_nl(str(editProduct["prod_amount"])))

    resMsg += u"</code>\n\r\n\rОбщая сумма: <b>" + empty_nl(
        str(editProduct["amount"])) + "</b>"

    return resMsg
コード例 #5
0
def tpl_newProduct(newProduct):

    resMsg = u"<code>Тов. добавлен в корзину:\n\r"

    resMsg += st_col(2, u"№", True) + "|"
    resMsg += st_col(6, u"цена") + "|"
    resMsg += st_col(5, u"кол.") + "|"
    resMsg += st_col(7, u"сумма") + "\n\r"

    if newProduct["product_id"] == 1:
        resMsg += ('-' * 23) + "\n\r"
    else:
        resMsg += '-----------//-----------\n\r'

    resMsg += st_col(2, empty_nl(str(
        newProduct["product_id"])), True) + "|" + st_col(
            6, empty_nl(str(newProduct["price"]))) + "|" + st_col(
                5, empty_nl(str(newProduct["quantity"]))) + "|" + st_col(
                    7, empty_nl(str(newProduct["prod_amount"])))

    resMsg += u"</code>\n\r\n\rОбщая сумма: <b>" + empty_nl(
        str(newProduct["amount"])) + "</b>"

    return resMsg
コード例 #6
0
    def setProductTemp(self, user_id, action, name, val):

        # получаем временный продукт
        product_temp = self.getProductTemp(user_id, action)

        param = None

        price = '0'
        quant = '1'
        kb_mode = 'p'

        # если его нет создаём
        if product_temp is None:

            if name == 'product_id':

                # получаем продукт по id
                product = self.getProduct(user_id, int(val))

                param = (int(user_id), empty_nl(Decimal(product["price"])),
                         empty_nl(Decimal(product["quantity"])), 'u', int(val))

                # сохраняем товар во временной таблице
                try:
                    self.execute(
                        'INSERT INTO products_temp(user_id, price, quantity, action, product_id) VALUES(%s, %s, %s, %s, %s)',
                        param)

                    # получаем временный продукт
                    product_temp = self.getProductTemp(user_id, action)

                # Сообщаем пользователю об ошибке базы данных
                except pymysql.err.InternalError:
                    try:
                        self.bot.send_message(self.message.chat.id,
                                              self.db_error_text,
                                              parse_mode='HTML')
                    except Exception as e:
                        print(traceback.format_exc(10))

                except Exception as e:
                    print(traceback.format_exc(10))

            else:
                param = (int(user_id), 'c')

                # сохраняем товар во временной таблице
                try:
                    self.execute(
                        'INSERT INTO products_temp(user_id, action) VALUES(%s, %s)',
                        param)

                    # получаем временный продукт
                    product_temp = self.getProductTemp(user_id, action)

                # Сообщаем пользователю об ошибке базы данных
                except pymysql.err.InternalError:
                    try:
                        self.bot.send_message(self.message.chat.id,
                                              self.db_error_text,
                                              parse_mode='HTML')
                    except Exception as e:
                        print(traceback.format_exc(10))

                except Exception as e:
                    print(traceback.format_exc(10))

        else:

            # значения по умалчанию
            price = product_temp["price"]
            quant = product_temp["quantity"]
            kb_mode = product_temp["kb_mode"]

            # если есть обновляем
            try:

                if name == 'num' and product_temp["kb_mode"] == 'p':

                    if product_temp["price"] == '0' and product_temp[
                            "flag_price_use"] == 'n':
                        price = val
                    else:
                        price = product_temp["price"] + val

                    param = (Decimal(price), 'y', int(user_id), action)

                    self.execute(
                        'UPDATE products_temp SET  price = %s, flag_price_use = %s  WHERE user_id = %s AND action = %s',
                        param)

                elif name == 'num' and product_temp["kb_mode"] == 'q':

                    if product_temp["quantity"] == '1' and product_temp[
                            "flag_quantity_use"] == 'n':
                        quant = val
                    else:
                        quant = product_temp["quantity"] + val

                    param = (Decimal(quant), 'y', int(user_id), action)

                    self.execute(
                        'UPDATE products_temp SET  quantity = %s, flag_quantity_use = %s  WHERE user_id = %s AND action = %s',
                        param)

                elif name == 'mode':

                    kb_mode = 'p'
                    if product_temp["kb_mode"] == 'p':
                        kb_mode = 'q'

                    param = (kb_mode, int(user_id), action)

                    self.execute(
                        'UPDATE products_temp SET  kb_mode = %s WHERE user_id = %s AND action = %s',
                        param)

                elif name == 'null' and product_temp["kb_mode"] == 'p':

                    price = '0'
                    param = (Decimal(price), int(user_id), action)

                    self.execute(
                        'UPDATE products_temp SET  price = %s WHERE user_id = %s AND action = %s',
                        param)

                elif name == 'null' and product_temp["kb_mode"] == 'q':

                    quant = '0'
                    param = (Decimal(quant), int(user_id), action)

                    self.execute(
                        'UPDATE products_temp SET  quantity = %s WHERE user_id = %s AND action = %s',
                        param)

                elif name == 'back' and product_temp["kb_mode"] == 'p':

                    price = '0'
                    if len(product_temp["price"]) >= 2:
                        price = product_temp["price"][0:-1]

                    param = (Decimal(price), int(user_id), action)

                    self.execute(
                        'UPDATE products_temp SET  price = %s WHERE user_id = %s AND action = %s',
                        param)

                elif name == 'back' and product_temp["kb_mode"] == 'q':

                    quant = '0'
                    if len(product_temp["quantity"]) >= 2:
                        quant = product_temp["quantity"][0:-1]

                    param = (Decimal(quant), int(user_id), action)

                    self.execute(
                        'UPDATE products_temp SET quantity = %s WHERE user_id = %s AND action = %s',
                        param)

                elif name == '.' and product_temp["kb_mode"] == 'p':

                    if "." not in price:
                        price += '.'

                    param = (Decimal(price), int(user_id), action)

                    self.execute(
                        'UPDATE products_temp SET  price = %s WHERE user_id = %s AND action = %s',
                        param)

                elif name == '.' and product_temp["kb_mode"] == 'q':

                    if "." not in quant:
                        quant += '.'

                    param = (Decimal(quant), int(user_id), action)

                    self.execute(
                        'UPDATE products_temp SET  quantity = %s WHERE user_id = %s AND action = %s',
                        param)

                elif name == '+' and product_temp["kb_mode"] == 'p':

                    price = Decimal(product_temp["price"]) + 1

                    param = (price, int(user_id), action)

                    print(param)

                    self.execute(
                        'UPDATE products_temp SET  price = %s WHERE user_id = %s AND action = %s',
                        param)

                elif name == '+' and product_temp["kb_mode"] == 'q':

                    quant = Decimal(product_temp["quantity"]) + 1

                    param = (quant, int(user_id), action)

                    print(param)

                    self.execute(
                        'UPDATE products_temp SET  quantity = %s WHERE user_id = %s AND action = %s',
                        param)

                elif name == '-' and product_temp["kb_mode"] == 'p':

                    if Decimal(product_temp["price"]) >= 1:
                        price = Decimal(product_temp["price"]) - 1

                    param = (price, int(user_id), action)

                    self.execute(
                        'UPDATE products_temp SET  price = %s WHERE user_id = %s AND action = %s',
                        param)

                elif name == '-' and product_temp["kb_mode"] == 'q':

                    if Decimal(product_temp["quantity"]) >= 1:
                        quant = Decimal(product_temp["quantity"]) - 1

                    param = (quant, int(user_id), action)

                    self.execute(
                        'UPDATE products_temp SET  quantity = %s WHERE user_id = %s AND action = %s',
                        param)

            # Сообщаем пользователю об ошибке базы данных
            except pymysql.err.InternalError:
                try:
                    self.bot.send_message(self.message.chat.id,
                                          self.db_error_text,
                                          parse_mode='HTML')
                except Exception as e:
                    print(traceback.format_exc(10))

            except Exception as e:
                print(traceback.format_exc(10))

        return {
            'price': price,
            'quant': quant,
            "action": action,
            "kb_mode": kb_mode
        }