Files
BE_CPONE/application/controllers/mockup/mcu-v6/Mcu_download.php
2026-04-27 10:31:17 +07:00

241 lines
7.0 KiB
PHP

<?php
class Mcu_download extends MY_Controller
{
var $db_onedev;
public function index()
{
echo "Samplingcall API";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
$this->load->helper(array('form', 'url'));
$this->load->library('zip');
}
function runningtodo(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
if(isset($prm['agreement']) && isset($prm['suffix'])){
$delete_x = "UPDATE mcu_download SET Mcu_DownloadIsActive = 'N' WHERE Mcu_DownloadMcu_AgreementID = {$prm['agreement']['id']} AND Mcu_DownloadIsActive = 'Y'";
$this->db_onedev->query($delete_x);
$insert_x = "INSERT INTO mcu_download (
Mcu_DownloadDate,
Mcu_DownloadMcu_AgreementID,
Mcu_DownloadSuffix ,
Mcu_DownloadCreated
)
vALUES(
now(),
{$prm['agreement']['id']},
'{$prm['suffix']}',
NOW()
)";
$this->db_onedev->query($insert_x);
}
$zipname = '';
$todos = $prm['todos'];
$table_name = '';
$restToDo = 0;
$ag_number = '';
foreach ($todos as $k => $v){
$ag_number = $v['agreement']['name'];
if($v['process'] == 'Y'){
if($v['section'] == 'data_patient'){
$table_name = 'm_patient';
$query = "SELECT * FROM {$table_name}";
}
if($v['section'] == 'data_test' && ($v['rest'] == 2 || $v['rest'] == '2')){
$table_name = 't_sampletype';
$query = "SELECT * FROM {$table_name}";
}
if($v['section'] == 'data_test' && ($v['rest'] == 1 || $v['rest'] == '1')){
$table_name = 't_test';
$query = "SELECT * FROM {$table_name}";
//echo $query;
}
if($v['section'] == 'data_price' && ($v['rest'] == 3 || $v['rest'] == '3')){
$table_name = 'm_company';
$query = "SELECT *
FROM {$table_name}
JOIN mcu_agreement_customer ON Mcu_AgreementCustomerM_CompanyID = M_CompanyID AND
Mcu_AgreementCustomerMcu_AgreementID = {$v['agreement']['id']}
WHERE
M_CompanyIsActive = 'Y'
";
}
if($v['section'] == 'data_price' && ($v['rest'] == 2 || $v['rest'] == '2')){
$table_name = 'm_mou';
$query = "SELECT *
FROM {$table_name}
JOIN mcu_agreement_customer ON Mcu_AgreementCustomerM_MouID = M_MouID AND
Mcu_AgreementCustomerMcu_AgreementID = {$v['agreement']['id']}
WHERE
M_MouIsActive = 'Y'
";
}
if($v['section'] == 'data_price' && ($v['rest'] == 1 || $v['rest'] == '1')){
$table_name = 't_price';
$get_mou_def = $this->db_onedev->query("SELECT * FROM m_mou WHERE M_MouIsDefault = 'Y' AND M_MouIsActive = 'Y'")->row();
$q_moudef = "";
if($get_mou_def){
$mou_def = $get_mou_def->M_MouID;
$q_moudef = "SELECT {$table_name}.*
FROM {$table_name}
WHERE
T_PriceM_MouID = {$mou_def} AND T_PriceIsActive = 'Y'
UNION
";
}
$query = " {$q_moudef}
SELECT {$table_name}.*
FROM {$table_name}
JOIN mcu_agreement_customer ON Mcu_AgreementCustomerM_MouID = T_PriceM_MouID AND
Mcu_AgreementCustomerM_CompanyID = T_PriceM_CompanyID AND
Mcu_AgreementCustomerMcu_AgreementID = {$v['agreement']['id']}
WHERE
T_PriceIsActive = 'Y'
";
//echo $query;
}
$rows = $this->db_onedev->query($query)->result_array();
$data = json_encode($rows,JSON_PRETTY_PRINT);
$todos[$k]['progressvalue'] = ceil(intval($v['progressvalue']) + (100/intval($v['actions'])));
$rest = intval($v['rest']) - 1;
$todos[$k]['rest'] = $rest;
if($rest == 0){
$todos[$k]['process'] = 'N';
if($k != count($todos) -1 ){
$todos[$k+1]['process'] = 'Y';
}
$todos[$k]['complete'] = 'Y';
}
$restToDo = $restToDo + $rest;
$filename = $table_name.'.json';
if (file_exists("/home/one/Web/one-media/one-mcu-download/".$filename)) {
unlink("/home/one/Web/one-media/one-mcu-download/".$filename);
}
if(file_put_contents("/home/one/Web/one-media/one-mcu-download/".$filename, $data,FILE_APPEND )){
$xfilename = $filename;
if($todos[$k]['filename'] != ''){
$xfilename = ' , '.$filename;
}
$todos[$k]['filename'] = $xfilename;
$this->zip->read_file("/home/one/Web/one-media/one-mcu-download/".$table_name.".json");
}
}
else{
$restToDo = $restToDo + intval($v['rest']);
}
}
if($restToDo == 0){
if (file_exists("/home/one/Web/one-media/mcudownload_".$ag_number.".zip")) {
unlink("/home/one/Web/one-media/mcudownload_".$ag_number.".zip");
}
$this->zip->archive("/home/one/Web/one-media/mcudownload_".$ag_number.".zip");
$zipname = 'https://'.$_SERVER['SERVER_NAME']."/one-media/mcudownload_".$ag_number.".zip";
}
$prm['count'] = intval($prm['count']) - 1;
$result = array(
"total" => $prm['count'],
"records" => $todos,
"zipname" => $zipname
);
$this->sys_ok($result);
exit;
}
function search_agreement(){
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$max_rst = 12;
$tot_count =0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
// QUERY TOTAL
$sql = "SELECT count(*) as total
FROM mcu_agreement
JOIN mcu_agreement_customer ON Mcu_AgreementCustomerMcu_AgreementID = Mcu_AgreementID
JOIN m_company ON Mcu_AgreementCustomerM_CompanyID = M_CompanyID
WHERE
( M_CompanyName like '{$q['search']}' OR Mcu_AgreementNumbering like '{$q['search']}' )
AND Mcu_AgreementIsActive = 'Y'
ORDER BY Mcu_AgreementID DESC";
$query = $this->db_onedev->query($sql);
//echo $sql;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("m_city count",$this->db_onedev);
exit;
}
$sql = "
SELECT Mcu_AgreementID as id, Mcu_AgreementNumbering as name
FROM mcu_agreement
JOIN mcu_agreement_customer ON Mcu_AgreementCustomerMcu_AgreementID = Mcu_AgreementID
JOIN m_company ON Mcu_AgreementCustomerM_CompanyID = M_CompanyID
WHERE
( M_CompanyName like '{$q['search']}' OR Mcu_AgreementNumbering like '{$q['search']}' )
AND Mcu_AgreementIsActive = 'Y'
ORDER BY Mcu_AgreementID DESC
";
$query = $this->db_onedev->query($sql);
if ($query) {
$rows = $query->result_array();
//echo $this->db_onedev->last_query();
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("m_company rows",$this->db_onedev);
exit;
}
}
}