<?php

$cM = new modelManager();
if (isset($_POST['insertCustomerContract'])) {
    if ($c->checkRole('create') == false) {
        return false;
    }
    $contract_id = $c->_model->_changeDauNhay($_POST['contract_id']);
    settype($contract_id, 'int');
    $customer_id = $c->_model->_changeDauNhay($_POST['customer_id']);
    settype($contract_id, 'int');
    if ($contract_id == 0 || $customer_id == 0) {
        $arr = array('error' => 1, 'message' => 'Input error');
        echo json_encode($arr);
        return false;
    }
    $data = $c->_model->_viewDetail('mn_customer', $customer_id);
    if (count($data) == 0) {
        $arr = array('error' => 1, 'message' => 'Không tìm thấy khách hàng này trong cơ sở dữ liệu');
        echo json_encode($arr);
        return false;
    }
    $arr = array('select' => '`id`', 'from' => '`mn_contract_customer`', 'where' => "`contract_id`='{$contract_id}' AND `customer_id`='{$customer_id}'");
    $data = $c->_model->_select($arr);
    if (count($data) != 0) {
        $arr = array('error' => 1, 'message' => 'Khách hàng này đã có trong hợp đồng');
        echo json_encode($arr);
        return false;
    }
    $fields = array('contract_id', 'customer_id');
    $values = array($contract_id, $customer_id);
 $rowTrainer = $data[0];
 //gui mail thong bao
 $link = '';
 if ($result >= 50) {
     $formID = 21;
     $country = $_SESSION['adminCountry'];
     $arr = array('select' => '`id`', 'from' => '`mn_license`', 'where' => "`code` LIKE '{$country}%' ");
     $data = $c->_model->_select($arr);
     $number = count($data) + 1;
     $certCode = sprintf($_SESSION['adminCountry'] . "%'.06s", $number);
     $check = $c->_model->_checkLicense($rowTrainer['course'], $rowCustomer['id']);
     if (count($check) == 0) {
         $c->_model->_insertLicense($certCode, $rowTrainer['class'], $rowTrainer['course'], $rowCustomer['id'], $rowTrainer['id']);
     }
 } else {
     $cM = new modelManager();
     $check = $c->_model->_checkEntryTestUser($table, $table_id, $rowEntry['menu_id']);
     if (count($check) <= 2) {
         $formID = 22;
         $key = time();
         $cM->_insertEntryTestUser($table, $table_id, $rowEntry['menu_id'], $key);
         $link = CONS_BASE_URL . '/ajax/?entrytest=' . $key;
         $link = '<a href="' . $link . '">' . $link . '</a>';
     } else {
         $formID = 23;
     }
 }
 $arr = array("select" => "*", "from" => "`web_event_form`", "where" => "`id`='{$formID}' AND `status`=1");
 $data = $c->_model->_select($arr);
 if (count($data) > 0) {
     $rowForm = $data[0];
function readXML($file, $table, $table_id)
{
    if (!file_exists($file)) {
        $arr = array('result' => false, 'message' => 'File does not exist');
        echo json_encode($arr);
        return false;
    }
    $model = new modelManager();
    $i = 0;
    $j = 0;
    $str = '';
    $xml = simplexml_load_file($file);
    foreach ($xml->Worksheet->Table->Row as $row) {
        $name = $model->_changeDauNhay($row->Cell[0]->Data);
        $email = $model->_checkEmail($row->Cell[1]->Data);
        $phone = '';
        if (isset($row->Cell[2]->Data)) {
            $phone = $model->_changeDauNhay($row->Cell[2]->Data);
        }
        $address = '';
        if (isset($row->Cell[3]->Data)) {
            $address = $model->_changeDauNhay($row->Cell[3]->Data);
        }
        $city = '';
        if (isset($row->Cell[4]->Data)) {
            $city = $model->_changeDauNhay($row->Cell[4]->Data);
        }
        $country = '';
        if (isset($row->Cell[5]->Data)) {
            $country = $model->_changeDauNhay($row->Cell[5]->Data);
        }
        $birthday = '';
        if (isset($row->Cell[6]->Data)) {
            $birthday = $model->_changeDauNhay($row->Cell[6]->Data);
        }
        $company = '';
        if (isset($row->Cell[7]->Data)) {
            $company = $model->_changeDauNhay($row->Cell[7]->Data);
        }
        if ($birthday != '') {
            $birthday = strtotime($birthday);
        }
        $arr = array('select' => '`id`', 'from' => '`mn_customer`', 'where' => "`email`='{$email}'", 'limit' => 1);
        $data = $model->_select($arr);
        $total = count($data);
        if ($name != '' && $email != false && $total == 0) {
            $i++;
            $customer_id = $model->_insertCustomer($name, $phone, $email, $address, $city, $country, $birthday, $company);
            if ($table == 'mn_contract') {
                $model->_insertContractCustomer($table_id, $customer_id);
            } else {
                if ($table == 'mn_class') {
                    $model->_insertClassCustomer($table_id, $customer_id);
                }
            }
        } else {
            if ($name != '' && $email != false && $total == 1) {
                $row = $data[0];
                $customer_id = $row['id'];
                if ($table == 'mn_contract') {
                    $arr = array('select' => '`id`', 'from' => '`mn_contract_customer`', 'where' => "`contract_id`='{$table_id}' AND `customer_id`='{$customer_id}'", 'limit' => 1);
                    $data = $model->_select($arr);
                    if (count($data) == 0) {
                        $i++;
                        $model->_insertContractCustomer($table_id, $customer_id);
                    }
                } else {
                    if ($table == 'mn_class') {
                        $arr = array('select' => '`id`', 'from' => '`mn_class_info`', 'where' => "`class_id`='{$table_id}' AND `_table`='mn_customer' AND `table_id`='{$customer_id}'", 'limit' => 1);
                        $data = $model->_select($arr);
                        if (count($data) == 0) {
                            $i++;
                            $model->_insertClassCustomer($table_id, $customer_id);
                        }
                    }
                }
            } else {
                if ($name == '' || $email == false) {
                    $j++;
                    $str .= "{$j}.{$name} - {$email} - {$phone} <span class='error'>kiểm tra data</span><br />";
                }
            }
        }
    }
    //end for
}
<?php

$cM = new modelManager();
if (isset($_POST['insertCustomerContract'])) {
    if ($c->checkRole('create') == false) {
        return false;
    }
    $contract_id = $c->_model->_changeDauNhay($_POST['contract_id']);
    settype($contract_id, 'int');
    $customer_id = $c->_model->_changeDauNhay($_POST['customer_id']);
    settype($contract_id, 'int');
    if ($contract_id == 0 || $customer_id == 0) {
        $arr = array('error' => 1, 'message' => 'Input error');
        echo json_encode($arr);
        return false;
    }
    $data = $c->_model->_viewDetail('mn_customer', $customer_id);
    if (count($data) == 0) {
        $arr = array('error' => 1, 'message' => 'Không tìm thấy khách hàng này trong cơ sở dữ liệu');
        echo json_encode($arr);
        return false;
    }
    $arr = array('select' => '`id`', 'from' => '`mn_contract_customer`', 'where' => "`contract_id`='{$contract_id}' AND `customer_id`='{$customer_id}'");
    $data = $c->_model->_select($arr);
    if (count($data) != 0) {
        $arr = array('error' => 1, 'message' => 'Khách hàng này đã có trong hợp đồng');
        echo json_encode($arr);
        return false;
    }
    $fields = array('contract_id', 'customer_id');
    $values = array($contract_id, $customer_id);