# -*- coding: utf-8 -*-
"""Kiem tra cuoi: tong T9 trong DB vs Excel moi; nhan cot cuoi trong HTML; copy HTML sang Tuan 41."""
import os, re, shutil, sqlite3

c = sqlite3.connect(r'C:\c\deepseek-harness\claw2-db\btchem.db')
print('DB sum mua_hang ky T9:', c.execute("select sum(tong_kg) from mua_hang where ky='T9'").fetchone()[0])
print('DB tong_quan T9:', c.execute("select * from mua_hang_tong_quan where thang='T9'").fetchone())
print('DB tong_quan T10:', c.execute("select * from mua_hang_tong_quan where thang='T10'").fetchone())
print('DB sum mua_hang ky T10:', c.execute("select sum(tong_kg) from mua_hang where ky='T10'").fetchone()[0])
print('DB so KH co kg > 0 trong T10:', c.execute("select count(*) from mua_hang where ky='T10' and tong_kg>0").fetchone()[0])
c.close()

h = open(r'C:\Claw 2\Bao cao - Hanh chinh\bao cao mua hang\Bao_cao\Baocaomuahang_thang10_ky01-03-10-2026.html', encoding='utf-8', errors='replace').read()
m = re.findall(r'<th[^>]*>(.*?)</th>', h, re.S)
print('nhan cot bang:', [re.sub(r'<[^>]+>', ' ', x).strip()[:28] for x in m][:14])

dest = r'C:\Claw 2\Bao cao - Hanh chinh\BAOCAOTONGHOP\HTML\Tuan 41'
os.makedirs(dest, exist_ok=True)
src_html = r'C:\Claw 2\Bao cao - Hanh chinh\bao cao mua hang\Bao_cao\Baocaomuahang_thang10_ky01-03-10-2026.html'
src_docx = r'C:\Claw 2\Bao cao - Hanh chinh\bao cao mua hang\Bao_cao\BAO_CAO_SO_SANH_MUA_HANG_T10_ky03.docx'
shutil.copy2(src_html, os.path.join(dest, os.path.basename(src_html)))
shutil.copy2(src_docx, os.path.join(dest, os.path.basename(src_docx)))
print('da luu vao Tuan 41:', os.listdir(dest))
