from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4,mm
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
import datetime

pdfmetrics.registerFont(TTFont("Dejavu", "DejaVuSans.ttf"))
pdfmetrics.registerFont(TTFont("Dejavu-Bold", "DejaVuSans-Bold.ttf"))

FONT_REGULAR = "Dejavu"
FONT_BOLD = "Dejavu-Bold"

date_time = datetime.datetime.now()

can = canvas.Canvas(f"чек_{date_time.strftime('%Y%m%d%H%M%S')}.pdf", pagesize = A4)

x_weidth, y_heigth = A4
print(x_weidth)
print(y_heigth)
y = y_heigth - 50

check = "Кассовый чек".upper()
shop_name = "ООО 'Демонстрационный экзамен 2026'".upper()
address = "Челябинская обл., г. Копейск, ул. Ленина. д. 40"
cashier = "Шайнуров Владимир Дмитриевич"
inn = "7430917363"

date = f"Дата: {date_time.strftime('%d.%m.%Y')}"
time = f"Время: {date_time.strftime('%H%M%S')}"

cashier = "Шайнуров Владимир Дмитриевич"
inn = "7430917363"

receipt = {
    "receipt1":
    {"name": "Системный блок", "price": 280000.00, "quantity": 1},
    "receipt2":
    {"name": "Монитор", "price": 11900.00, "quantity": 2},
    "receipt3":
    {"name": "Клавиатура", "price": 4990.00, "quantity": 1},
    "receipt4":
    {"name": "Манипулятор типа 'Мышь'", "price": 1400.00, "quantity": 1},
    "receipt5":
    {"name": "Кабель HDMI", "price": 600.00, "quantity": 2},
    "receipt6":
    {"name": "Операционная система Ubuntu 24.04", "price": 0.00, "quantity": 1},
    "receipt7":
    {"name": "Язык программирования Python", "price": 0.00, "quantity": 1},
    "receipt8":
    {"name": "Редактор кода VS Code", "price": 0.00, "quantity": 1},
    "receipt9":
    {"name": "Библиотека reportlab", "price": 0.00, "quantity": 1},
}

can.setFont(FONT_BOLD, 14)
can.drawCentredString(x_weidth / 2, y, check)
y = y - 30

can.setFont(FONT_BOLD, 16)
can.drawCentredString(x_weidth / 2, y, shop_name)
y = y - 13

can.setFont(FONT_REGULAR, 10)
can.drawCentredString(x_weidth / 2, y, address)
y = y - 30

can.setFont(FONT_REGULAR, 10)
can.drawString(50, y, date)

can.setFont(FONT_REGULAR, 10)
can.drawString(457, y, time)
y  = y - 20

can.setFont(FONT_REGULAR, 12)
can.drawString(50, y, f'Кассир: {cashier}')

can.setFont(FONT_REGULAR, 12)
can.drawString(430, y, f'ИНН:{inn}')
y = y - 10

can.line(50, y, 540, y)
y = y - 18

can.setFont(FONT_BOLD, 10)
can.drawString(50, y, "Товар")
can.drawString(250, y, "Цена")
can.drawString(350, y, "Кол.")
can.drawString(450, y, "Сумма")
y = y - 20

can.setFont(FONT_REGULAR, 10)
can.drawString(50, y, receipt['receipt1']['name'])
can.drawString(250, y, f"{receipt['receipt1']['price']:.2f}")
can.drawString(350, y, f"{receipt['receipt1']['quantity']}")
can.drawString(450, y, f"{(receipt['receipt1']['price'] * receipt['receipt1']['quantity']):.2f}")
y = y - 15

can.setFont(FONT_REGULAR, 10)
can.drawString(50, y, receipt['receipt2']['name'])
can.drawString(250, y, f"{receipt['receipt2']['price']:.2f}")
can.drawString(350, y, f"{receipt['receipt2']['quantity']}")
can.drawString(450, y, f"{(receipt['receipt2']['price'] * receipt['receipt2']['quantity']):.2f}")
y = y - 15

can.setFont(FONT_REGULAR, 10)
can.drawString(50, y, receipt['receipt3']['name'])
can.drawString(250, y, f"{receipt['receipt3']['price']:.2f}")
can.drawString(350, y, f"{receipt['receipt3']['quantity']}")
can.drawString(450, y, f"{(receipt['receipt3']['price'] * receipt['receipt3']['quantity']):.2f}")
y = y - 15

can.setFont(FONT_REGULAR, 10)
can.drawString(50, y, receipt['receipt4']['name'])
can.drawString(250, y, f"{receipt['receipt4']['price']:.2f}")
can.drawString(350, y, f"{receipt['receipt4']['quantity']}")
can.drawString(450, y, f"{(receipt['receipt4']['price'] * receipt['receipt4']['quantity']):.2f}")
y = y - 15

can.setFont(FONT_REGULAR, 10)
can.drawString(50, y, receipt['receipt5']['name'])
can.drawString(250, y, f"{receipt['receipt5']['price']:.2f}")
can.drawString(350, y, f"{receipt['receipt5']['quantity']}")
can.drawString(450, y, f"{(receipt['receipt5']['price'] * receipt['receipt5']['quantity']):.2f}")
y = y - 15

can.setFont(FONT_REGULAR, 10)
can.drawString(50, y, receipt['receipt6']['name'])
can.drawString(250, y, f"{receipt['receipt6']['price']:.2f}")
can.drawString(350, y, f"{receipt['receipt6']['quantity']}")
can.drawString(450, y, f"{(receipt['receipt6']['price'] * receipt['receipt6']['quantity']):.2f}")
y = y - 15

can.setFont(FONT_REGULAR, 10)
can.drawString(50, y, receipt['receipt7']['name'])
can.drawString(250, y, f"{receipt['receipt7']['price']:.2f}")
can.drawString(350, y, f"{receipt['receipt7']['quantity']}")
can.drawString(450, y, f"{(receipt['receipt7']['price'] * receipt['receipt7']['quantity']):.2f}")
y = y - 15

can.setFont(FONT_REGULAR, 10)
can.drawString(50, y, receipt['receipt8']['name'])
can.drawString(250, y, f"{receipt['receipt8']['price']:.2f}")
can.drawString(350, y, f"{receipt['receipt8']['quantity']}")
can.drawString(450, y, f"{(receipt['receipt8']['price'] * receipt['receipt8']['quantity']):.2f}")
y = y - 15

can.setFont(FONT_REGULAR, 10)
can.drawString(50, y, receipt['receipt9']['name'])
can.drawString(250, y, f"{receipt['receipt9']['price']:.2f}")
can.drawString(350, y, f"{receipt['receipt9']['quantity']}")
can.drawString(450, y, f"{(receipt['receipt9']['price'] * receipt['receipt9']['quantity']):.2f}")
y = y - 11

can.line(50, y, 540, y)
y = y - 18

itog = 0
for k,v in receipt.items():
    itog += v['price'] * v['quantity']

can.setFont(FONT_BOLD, 11)
can.drawString(250, y, "ИТОГО")
can.drawString(450, y, f"{itog:.2f} руб")
y = y - 18

can.setFont(FONT_REGULAR, 9)
can.drawString(250, y, "В т.ч. НДС 22%:")
can.drawString(450, y, f"{itog * 0.22:.2f} руб")
y = y - 35

can.setFont(FONT_BOLD, 10)
can.drawCentredString(x_weidth / 2, y, "ЧЕК ОНЛАЙН".upper())
y = y - 12

can.setFont(FONT_REGULAR, 7)
can.drawCentredString(x_weidth / 2, y, f"ID: {date_time.strftime('%Y%m%d%H%M%S')}")
y = y - 22

can.setFont(FONT_REGULAR, 9)
can.drawCentredString(x_weidth / 2, y, "спасибо за покупку".upper())
y = y - 18

can.setFont(FONT_REGULAR, 9)
can.drawCentredString(x_weidth / 2, y, "Возрат - 14 дней при наличии чека")
y = y - 12

can.setFont(FONT_REGULAR, 9)
can.drawCentredString(x_weidth / 2, y, "Тел. поддержки: 8-351-39-3-63-75")
y = y - 12

can.setFont(FONT_REGULAR, 9)
can.drawCentredString(x_weidth / 2, y, f"Создано: {date_time.strftime('%d.%m.%Y:%H.%M.%S')}")
y = y - 12

can.save()