def check_timestamps(): time_ = time.time() canceled_orders = Orders_Info.select().where( Orders_Info.status == 'CREATE').where(time_ - Orders_Info.time > 10) not_finished_orders = Orders_Info.select().where( Orders_Info.status == 'AWAIT').where(time_ - Orders_Info.time > 10) message_info = [] for c_order in canceled_orders: items = Orders_Content.select().where( Orders_Content.order_id == c_order.id) for item in items: update_amount(item.item_id, item.amount) message_info.append([c_order.id, c_order.admin, c_order.chat_id]) for nf_order in not_finished_orders: items = Orders_Content.select().where( Orders_Content.order_id == nf_order.id) for item in items: update_amount(item.item_id, item.amount) message_info.append([nf_order.id, -1, nf_order.chat_id]) query = Orders_Info.update({ Orders_Info.status: 'DEL' }).where((Orders_Info.status == 'AWAIT') | (Orders_Info.status == 'CREATE')).where( time_ - Orders_Info.time > 10) query.execute() return message_info
def set_time_order(id_): query = Orders_Info.update({ Orders_Info.status: 'AWAIT', Orders_Info.time: time.time() }).where(Orders_Info.id == id_) query.execute() order = Orders_Info.select().where(Orders_Info.id == id_) return order[0].chat_id
def check_order(order_id): items = Orders_Content.select().where(Orders_Content.order_id == order_id) order = Orders_Info.select().where((Orders_Info.id == order_id) & (Orders_Info.status != 'DEL')) if (not items.exists()): query = Orders_Info.update({ Orders_Info.status: 'DEL' }).where(Orders_Info.id == order_id) query.execute() return -1 if (not order.exists()): return -2 return 0
def cancel_by_id(id_, chat_id): order = Orders_Info.select().where((Orders_Info.id == id_) & (Orders_Info.chat_id == chat_id) & (Orders_Info.status != 'CREATE')) if not order.exists(): raise NameError('Не ваш заказ, ая-яй-яй!') query = Orders_Info.update({ Orders_Info.status: 'DEL' }).where(Orders_Info.id == id_) query.execute() items = Orders_Content.select().where(Orders_Content.order_id == id_) for item in items: update_amount(item.item_id, item.amount) return order[0].admin, order[0].chat_id
def get_order_items(id_, chat_id): order = Orders_Info.select().where((Orders_Info.id == id_) & (Orders_Info.chat_id == chat_id)) if not order.exists(): raise NameError('Не ваш заказ, ая-яй-яй!') items = Orders_Content.select().where( Orders_Content.order_id == order[0].id) return [[get_full_name_by_id(item.item_id), item.item_id, item.amount] for item in items]
def clear_base(): query = Goods.delete().where(Goods.id != 1) query.execute() query = Goods.update({Goods.amount: 0}).where(Goods.id == 0) query.execute() query = Orders_Info.delete().where(Orders_Info.id > 0) query.execute() query = Orders_Content.delete().where(Orders_Content.id > 0) query.execute()
def finish_order(order_id): query = Orders_Info.update({ Orders_Info.time: time.time(), Orders_Info.status: 'PROCESS' }).where(Orders_Info.id == order_id) query.execute() order_content = Orders_Content.select().where( Orders_Content.order_id == order_id) order = '\n'.join([ '{}::{}::{}'.format(get_full_name_by_id(inst.item_id), inst.amount, inst.cost) for inst in order_content ]) order_info = Orders_Info.select().where(Orders_Info.id == order_id) ovrl_cost = sum([ item.cost for item in Orders_Content.select().where( Orders_Content.order_id == order_id) ]) return order_info[0].admin, order, order_id, ovrl_cost
def erase_item_order_by_ids(id_, order_id): query = Orders_Info.update({ Orders_Info.time: time.time() }).where(Orders_Info.id == order_id) query.execute() item = Orders_Content.select().where(Orders_Content.item_id == id_).where( Orders_Content.order_id == order_id) amount = item[0].amount update_amount(id_, amount) query = Orders_Content.delete().where(Orders_Content.item_id == id_).where( Orders_Content.order_id == order_id) query.execute() return amount
def buy_item(id_, amount_, order_id): item = Goods.select().where(Goods.id == id_)[0] if (item.amount < amount_): raise OverflowError(' '.join([ 'Этого товара осталось всего', str(item.amount), '\nВведите другое количество' ])) if (amount_ <= 0): raise OverflowError('\n'.join([ 'Число товаров должно быть натуральным', 'Введите другое количество' ])) price = Goods.select().where(Goods.id == id_)[0].price same_item = Orders_Content.select().where(( Orders_Content.order_id == order_id) & (Orders_Content.item_id == id_)) if same_item.exists(): query = Orders_Content.update({ Orders_Content.amount: Orders_Content.amount + amount_, Orders_Content.cost: Orders_Content.cost + amount_ * price }).where((Orders_Content.order_id == order_id) & (Orders_Content.item_id == id_)) query.execute() else: Orders_Content.create(order_id=order_id, item_id=id_, amount=amount_, cost=price * amount_) query = Orders_Info.update({ Orders_Info.time: time.time() }).where(Orders_Info.id == order_id) query.execute() update_amount(id_, -amount_)
def complete_order(id_): query = Orders_Info.update({ Orders_Info.status: 'COMPLETE' }).where(Orders_Info.id == id_) query.execute()
def add_new_order(chat_id, admin): item = Orders_Info.create(chat_id=chat_id, admin=admin, time=time.time(), status='CREATE') return item.id