<!DOCTYPE html>
<html lang="vi">

<head>
    <meta charset="UTF-8">
    <title>A230 - KẾ HOẠCH KIỂM TOÁN</title>

    <style>
        .export-button {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 10px 24px;
            background-color: #22c55e;
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: background-color 0.2s;
        }

        .export-button:hover {
            background-color: #16a34a;
        }

        .print-button {
            position: fixed;
            top: 20px;
            right: 160px;
            padding: 10px 24px;
            background-color: #14b8a6;
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: background-color 0.2s;
        }

        .print-button:hover {
            background-color: #0d9488;
        }

        @media print {

            .print-button,
            .export-button {
                display: none;
            }
        }
    </style>
</head>

<body
    style="font-family: 'Times New Roman', serif; max-width: 800px; margin: 0 auto; padding: 20px; line-height: 1.4; font-size: 13pt;">

    <button onclick="printDocument()" class="action-button print-button">In nhanh</button>
    <button onclick="exportToWord()" class="export-button">Xuất ra Word</button>

    <!-- Title -->
    <table style="width: 100%; border-collapse: collapse;">
        <tr>
            <td colspan="2" style="text-align: left;">
                <p style="margin: 0;"><i>Số: <span data-field="soKhoach"></span></i></p>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: right;">
                <p style="margin: 0;"><i>Hà Nội, <span data-field="ngayKhoach"></span></i></p>
            </td>
        </tr>
    </table>

    <div style="text-align: center; margin: 20px 0;">
        <h4 style="margin: 0;">KẾ HOẠCH KIỂM TOÁN BCTC <br>
            CHO NĂM TÀI CHÍNH KẾT THÚC NGÀY <span data-field="NamKiT"></span>
        </h4>

    </div>
    <table style="width: 100%; border-collapse: collapse;">

        <tr>
            <td colspan="2" style="text-align: left;">
                <p style="margin: 0;"><i>Kính gửi: <span data-field="NguoiDaiDienKH"></span></i></p>
            </td>
            <td colspan="3" style="text-align: left;">
                <p style="margin: 0;"><i>Chức vụ: <span data-field="ChucVuKH"></span></i></p>
            </td>
        </tr>
    </table>

    <div style="text-align: center; margin: 20px 0;">
        <h5 style="margin: 0;"><span data-field="TenKH"></span><br>
            <span data-field="DiaChiKH"></span>
        </h5>

    </div>
    <!-- Legal basis -->

    <p style="margin: 20px 0;">Thưa Quý vị, </p>
    <p style="margin: 20px 0; text-align: justify;">Chúng tôi rất hân hạnh được Quý vị tin tưởng bổ nhiệm là kiểm toán
        viên để kiểm toán Báo
        cáo tài chính cho năm tài chính kết thúc ngày <span data-field="NamKiT"></span> của Quý Công ty. Để công việc
        kiểm toán được tiến hành
        thuận lợi, chúng tôi xin gửi đến Quý vị kế hoạch kiểm toán như sau:</p>

    <!-- Party A -->
    <div style="margin-top: 25px;">
        <p style="margin: 0; font-weight: bold;">1. Nhóm kiểm toán</span></p>
        <table style="width: 100%; border-collapse: collapse; margin: 20px 0;">
            <tr style="text-align: left;">
                <th>Họ và tên</th>
                <th>Chức danh</th>
                <th>Vị trí/ Nhiệm vụ</th>
            </tr>
            <tr>
                <td>Trần Văn B</td>
                <td>Tổng giám đốc</td>
                <td>Thành viên Ban giám đốc, Phụ trách chung cuộc kiểm toán</td>
            </tr>
            <tr>
                <td>Trần Văn B</td>
                <td>Kiểm toán viên</td>
                <td>Kiểm toán viên phụ trách cuộc kiểm toán/ Trực tiếp thực hiện</td>
            </tr>
            <tr>
                <td>Nguyễn Văn C</td>
                <td>Trợ lý kiểm toán</td>
                <td>Trực tiếp thực hiện</td>
            </tr>
        </table>
        <p style="margin: 0; font-weight: bold; ">2. Phạm vi công việc</span></p>
        <p style="margin: 20px 0; ">Theo thỏa thuận, chúng tôi sẽ tiến hành kiểm toán Báo cáo tài
            chính cho năm tài chính kết thúc ngày <span data-field="NamKiT"></span> nhằm đưa ra ý kiến liệu báo cáo tài
            chính có phản ánh
            trung thực, hợp lý tình hình tài chính, kết quả kinh doanh và lưu chuyển tiền tệ của Công ty, phù hợp với
            Chuẩn mực kế toán Việt Nam, Chế độ kế toán doanh nghiệp Việt Nam và các quy định pháp lý có liên quan đến
            việc lập và trình bày báo cáo tài chính hay không. </p>
        <b>Các công việc cụ thể mà chúng tôi sẽ thực hiện như sau:</b>
        <p style="margin-left: 20px; text-align: justify;">
            <span>● Tham gia chứng kiến kiểm kê tiền mặt, hàng tồn kho và tài sản cố định
                của Công ty tại thời điểm cuối năm tài chính;</span><br>
            <span>● Tìm hiểu Công ty và môi trường hoạt động bao gồm cả tìm hiểu kiểm soát
                nội bộ và hệ thống kế toán;</span><br>
            <span>● Thu thập các thông tin pháp lý, các quy định nội bộ và các tài liệu
                quan trọng như: Điều lệ Công ty, Biên bản họp Hội đồng quản trị, Đại hội đồng cổ đông, Ban Giám đốc làm
                cơ sở để kiểm toán các thông tin liên quan trên Báo cáo tài chính;</span><br>
            <span>● Đánh giá các chính sách kế toán được áp dụng, các ước tính và xét đoán
                quan trọng của Ban Giám đốc cũng như đánh giá việc trình bày tổng thể Báo cáo tài chính;</span><br>
            <span>● Thực hiện kiểm tra các kiểm soát nội bộ và kiểm tra cơ bản đối với Báo cáo tài chính và các khoản
                mục trình bày trên Báo cáo tài chính;</span><br>
            <span>● <b>Trao đổi những vấn đề phát sinh cần xem xét, các bút toán cần điều chỉnh;</b></span><br>
            <span>● <b>Tổng hợp và lập Báo cáo kiểm toán dự thảo trình bày ý kiến của Kiểm toán viên về tính trung thực,
                    hợp lý của Báo cáo tài chính được kiểm toán. Phát hành Báo cáo kiểm toán chính thức sau khi nhận
                    được ý kiến đồng ý của Ban Giám đốc.</b></span><br>

        </p>
        <p style="margin: 0; font-weight: bold; ">3. Thời gian dự kiến thực hiện kiểm toán</span></p>
        <p style="margin-left: 20px; text-align: justify;">
            <span>Chúng tôi dự kiến sẽ tham gia chứng kiến kiểm kê tại Công ty từ ngày <span
                    data-field="TuNgayKiemToan"></span> đến
                <span data-field="DenNgayKiemToan"></span>.</span><br>
            <span>Cuộc kiểm toán sẽ được thực hiện từ ngày <span data-field="NgayKiemToan"></span> tại văn phòng Quý
                Công ty và dự kiến kết thúc
                trong vòng sau (03) ngày làm việc. </span><br>
            <span>Thời gian dự kiến phát hành báo cáo kiểm toán dự thảo là: <span
                    data-field="NgayBaoCaoKiemToan"></span>.</span><br>
        </p>
        <p style="margin: 0; font-weight: bold; ">4. Yêu cầu phối hợp làm việc</span></p>
        <p style="margin-left: 20px; text-align: justify;">
            <span>Trong thời gian thực hiện kiểm toán, đề nghị Công ty tạo điều kiện thuận lợi cho các nhân viên
                của chúng tôi trong quá trình làm việc.</span><br>
        </p>
        <p style="margin: 0; font-weight: bold; ">5. Yêu cầu cung cấp tài liệu</span></p>
        <p style="margin-left: 20px; text-align: justify;">
            <span>Chúng tôi cũng gửi kèm theo kế hoạch này Danh mục tài liệu đề nghị Quý Công ty cung cấp. Các tài liệu
                trong phần A</span><br>
            <span> - Tổng quát, cần được gửi cho chúng tôi trước ngày <span
                    data-field="NgayGuiYeuCauCungCapTaiLieu"></span>, các tài liệu còn lại đề nghị gửi
                trước hoặc được cung cấp ngay khi chúng tôi bắt đầu thực hiện kiểm toán.</span><br>
            <span> Nếu Quý vị có câu hỏi gì hoặc gợi ý gì khác về kế hoạch kiểm toán này, xin vui lòng liên hệ lại với
                chúng tôi. </span><br>
            <span>Nếu Quý vị đồng ý với kế hoạch kiểm toán này, xin vui lòng ký xác nhận và fax lại cho chúng tôi để
                chúng tôi có thể tiến hành kịp thời gian yêu cầu.</span> <br>
            <span>Thay mặt và đại diện cho Công ty,</span>

        </p>

        <table>
            <tr>

                <td style=" padding: 5px 0; font-weight: bold;">
                    <hr>
                </td>


            </tr>
            <tr>
                <td style=" padding: 5px 0; font-weight: bold;">
                    <b>Họ và Tên: Ông Nguyễn Huy Hoàng</b>
                </td>
            </tr>
            <tr>
                <td style=" padding: 5px 0; ">
                    Tổng Giám Đốc
                </td>
            </tr>
            <tr>
                <td style=" padding: 5px 0; font-weight: bold;">
                    Công ty TNHH Kiểm toán IVC
                </td>
            </tr>
        </table>
        <hr>
        <p style="margin: 0; font-weight: bold; ">Xác nhận của <span data-field="TenKH"></span></span></p>
        <p style="margin-left: 20px; text-align: justify;">
            <span>Tôi đồng ý với nội dung của Kế hoạch kiểm toán trên đây do Công ty TNHH Kiểm toán IVC cung
                cấp.</span><br>
        <table>
            <tr>

                <td style=" padding: 5px 0; font-weight: bold;">
                    <hr>
                </td>


            </tr>
            <tr>
                <td style=" padding: 5px 0; font-weight: bold;">
                    <b>Họ và Tên: <span data-field="NguoiDaiDienKH"></span></b>
                </td>
            </tr>
            <tr>
                <td style=" padding: 5px 0; ">
                    Chức danh: <span data-field="ChucVuKH"></span>
                </td>
            </tr>
            <tr>
                <td style=" padding: 5px 0; font-weight: bold;">
                    <span data-field="TenKH"></span><br>
                    <span data-field="TinhKh"></span>, <span data-field="ngayKhoach"></span>
                </td>
            </tr>
        </table>
    </div>


    <script src="https://cdnjs.cloudflare.com/ajax/libs/docx/7.8.2/docx.js"></script>
    <script>

        document.addEventListener('contextmenu', function (e) {
            e.preventDefault();
        }, false);

        document.onkeydown = function (e) {
            if (e.keyCode == 123) {
                return false;
            }
            if (e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)) {
                return false;
            }
            if (e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)) {
                return false;
            }
            if (e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)) {
                return false;
            }
        };
        const appId = '45324891-da90-4c7e-89cb-255d02772e29';
        const accessKey = 'V2-ERBuT-OvzzD-vNJDC-ODMfm-BuZyK-7yfLe-XEsob-vFasR';
        const region = 'www';

        // Add this new function for printing
        function printDocument() {
            const printButton = document.querySelector('.print-button');
            const exportButton = document.querySelector('.export-button');

            // Hide both buttons
            if (printButton) printButton.style.display = 'none';
            if (exportButton) exportButton.style.display = 'none';

            // Print the document
            window.print();

            // Show buttons again after printing
            if (printButton) printButton.style.display = 'block';
            if (exportButton) exportButton.style.display = 'block';
        }

        // Add the export function
        function exportToWord() {
            // Lấy nội dung cần xuất (bỏ qua button xuất và scripts)
            const content = document.body.cloneNode(true);
            const exportButton = content.querySelector('.export-button');
            if (exportButton) {
                exportButton.remove();
            }

            const printButton = content.querySelector('.print-button');
            if (printButton) {
                printButton.remove();
            }

            // Tạo style inline để giữ format
            const css = `
        body { font-family: 'Times New Roman', serif; }
        h3 { margin: 0; }
        .export-button { display: none; }
    `;

            // Tạo HTML tối giản
            const html = `
        <html xmlns:o='urn:schemas-microsoft-com:office:office' 
              xmlns:w='urn:schemas-microsoft-com:office:word'>
        <head>
            <meta charset='utf-8'>
            <title>A230 - KẾ HOẠCH KIỂM TOÁN</title>
            <style>${css}</style>
        </head>
        <body>
            ${content.innerHTML}
        </body>
        </html>
    `;

            // Tạo blob với type phù hợp cho MS Word
            const blob = new Blob([html], {
                type: 'application/msword;charset=utf-8'
            });

            // Download file
            const link = document.createElement('a');
            link.href = URL.createObjectURL(blob);
            link.download = 'thanh-li-hop-dong.doc';
            link.click();
            URL.revokeObjectURL(link.href);
        }

        function formatVietnameseDate(dateString) {
            const date = new Date(dateString);
            return `ngày ${String(date.getDate()).padStart(2, '0')} tháng ${String(date.getMonth() + 1).padStart(2, '0')} năm ${date.getFullYear()}`;
        }

        function formatVietnameseDateshort(dateString) {
            const date = new Date(dateString);
            return `${String(date.getDate()).padStart(2, '0')}/${String(date.getMonth() + 1).padStart(2, '0')}/${date.getFullYear()}`;
        }

        function getContractIdFromUrl() {
            const urlParams = new URLSearchParams(window.location.search);
            return urlParams.get('ID_KHKT');
        }

        async function fetchContractData() {
            try {
                const contractId = getContractIdFromUrl();
                if (!contractId) {
                    throw new Error('Không tìm thấy số hợp đồng trong URL');
                }

                const response = await fetch(`https://${region}.appsheet.com/api/v2/apps/${appId}/tables/Plan/Action`, {
                    method: 'POST',
                    headers: {
                        'ApplicationAccessKey': accessKey,
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        Action: 'Find',
                        Properties: {},
                        Selector: `Filter(Plan, ID_KHKT = '${contractId}')`
                    })
                });

                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }

                const data = await response.json();
                const contract = data.find(c => c.ID_KHKT === contractId);

                if (!contract) {
                    throw new Error(`Không tìm thấy hợp đồng với số: ${contractId}`);
                }

                document.querySelectorAll('[data-field]').forEach(element => {
                    const fieldName = element.getAttribute('data-field');
                    if (contract[fieldName]) {

                        if (fieldName === 'ngayKhoach') {
                            element.textContent = formatVietnameseDate(contract[fieldName]);
                        } else if (fieldName === 'TuNgayKiemToan' || fieldName === 'DenNgayKiemToan' || fieldName === 'NgayKiemToan' || fieldName === 'NgayBaoCaoKiemToan' || fieldName === 'NgayGuiYeuCauCungCapTaiLieu') {
                            element.textContent = formatVietnameseDateshort(contract[fieldName]);
                        } else if (fieldName === 'TenKH' && contract['MaKH']) {
                            element.textContent = contract['TenKH'].toUpperCase();
                        } else if (fieldName === 'VAT') {
                            element.textContent = (contract[fieldName]) * 100 + '%';
                        }
                        else if (fieldName === 'GiaTriHopDongChuaVAT' || fieldName === 'TongGiaTriHopDong' || fieldName === 'GiaTriDaThucHien' || fieldName === 'GiaTriDaTamUng' || fieldName === 'GiaTriConLai') {
                            element.textContent = formatVietnameseCurrency(contract[fieldName]);
                        } else {
                            element.textContent = contract[fieldName];
                        }
                    }
                });

            } catch (error) {
                console.error('Failed to fetch data:', error);
                document.body.innerHTML = `<div style="color: red; text-align: center; padding: 20px;">
                    Lỗi khi tải dữ liệu: ${error.message}
                </div>`;
            }
        }

        window.addEventListener('load', fetchContractData);
    </script>
</body>

</html>