booking订单导入

<?php

// ini_set('display_errors', "1");

// ini_set('display_startup_errors', "1");

// ini_set('html_errors', "1");

// ini_set('memory_limit', "512M");

// error_reporting(E_ALL);

global $_W,$_GPC;

$weid = $_W['uniacid'];

$op = empty($_GPC['op'])? 'list':$_GPC['op'];

if($op=='list'){

$pindex = max(1, intval($_GPC['page']));

$psize = 10;

$categorys = pdo_fetchall("SELECT * FROM ".tablename($this->category_table)." ORDER BY createtime DESC  LIMIT ".($pindex – 1) * $psize.",{$psize}");

$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($this->category_table) );

$pager = pagination($total, $pindex, $psize);

}

elseif($op=='post'){

load()->func('tpl');

$id = intval($_GPC['id']);

$category = pdo_fetch("SELECT * FROM ".tablename($this->category_table)." WHERE weid=:weid AND id=:id",array(':weid'=>$weid,':id'=>$id));

if(empty($category)){

$category = array(

'isshow'=>1,

);

}

//数据提交的内容

if (checksubmit('submit')) {

if(!empty($id)){

function read_csv($file,$dbhost,$dbuser,$dbpass,$database) {

$keys = array();

$newArray = array();

$array = array_map("str_getcsv", file($file));

$count = count($array) – 1;

$labels = array_shift($array);

$labels[0] = "add_id"; //订单编号

$labels[1] = "booked"; //住房预定时间

$labels[2] = "arrival"; //开始入住时间

$labels[3] = "departure"; //结束入住时间

$labels[4] = "length_of_stay"; //住房时间天数

$labels[5] = "booking_window"; //订购票的窗口多少号

$labels[6] = "status"; //订单状态

$labels[7] = "comission_eur"; //人民币欧元

$labels[8] = "progress_bar"; //进度

$labels[9] = "fee"; //费用

$labels[10] = "fee_currency";  //费用货币

$labels[11] = "exchange_rate"; //汇率

$labels[12] = "ttv"; //不知道该字段

$labels[13] = "slip_nr";        //不知道该字段

$labels[14] = "affiliate_id"; //会员身份ID

$labels[15] = "hotel_name"; //酒店的名称

$labels[16] = "accomodation_type"; //住宿的类型

$labels[17] = "hotel_country"; //酒店的国家

$labels[18] = "hotel_city"; //酒店的城市

$labels[19] = "hotel_ufi"; //不知道该字段

$labels[20] = "booker_country"; //不知道该字段

$labels[21] = "booker_language"; //不知道该字段

$labels[22] = "label"; //酒店的标签

$labels[23] = "user_device"; //用户的设备订单  例如:电脑 手机

$labels[24] = "travel_purpose"; //用户旅行的目的

foreach ($labels as $label) {

$keys[] = $label;

}

$keys[] = "";

for ($i = 0; $i < $count; $i++) {

$array[$i][] = $i;

}

$newArray = array();

for ($j = 0; $j < $count; $j++) {

$d = array_combine($keys, $array[$j]);

$newArray[$j] = $d;

}

$conn = mysqli_connect($dbhost, $dbuser, $dbpass);

if(! $conn )

{

die('连接失败: ' . mysqli_error($conn));

}

mysqli_query($conn, "set names utf8");

mysqli_select_db( $conn, $database );

mysqli_query($conn, "SET AUTOCOMMIT=0"); 

$reslte = true;

mysqli_begin_transaction($conn);      

try{

//默认成功类型

$status['type'] = "1";

foreach ($newArray as $keys => $open) {

$lable_explode = explode('-',$open['label']);

$label_1 = (int)@$lable_explode[0];

$label_2 = (int)@$lable_explode[1];

$label_1 = $label_1 == 0 ? 1 : $label_1;

$label_2 = $label_2 == 0 ? 1 : $label_2;

if(strlen($open['label']) == 5){

$label_2 = 0;

}

$open['label_1'] = $label_1;

$open['label_2'] = $label_2;

$fetch_reslte = pdo_fetch("SELECT * FROM ims_meepo_book WHERE add_id=:add_id", array(":add_id"=>$open['add_id']));

$configs = pdo_fetch("SELECT * FROM ims_meepo_online_list_config WHERE id=1 ", array(''=>''));   

//千位字符去除逗号 ,

$open['commision_eur'] = str_replace(",","",@$open['commision_eur']);

$open['fee'] = str_replace(",","",$open['fee']);

//判斷下單類型

$type_info = get_type($label_1,$label_2);

$open['user_id'] = $type_info['user_id'];

$open['ascription_name'] = $type_info['ascription_name'];

$baifen = (float)$open['progress_bar']/100;

$ratio = $configs['ao_default']+@$configs['incentive_ratio'];

$open['ren_money'] = round(((($open['fee']/$baifen)*$configs['exchange_rate'])*$ratio)/100,2);

// $open['t_money'] = 0;

// if($type_info['ascription_name'] == '推荐'){

// $open['t_money'] = (int)round(((($open['fee']/$baifen)*$configs['exchange_rate'])*$configs['rr_default'])/100,2);

// }

$open['dd_zong'] = round((($open['fee']/$baifen)*$configs['exchange_rate']),2);

$open['daoru_time'] = time();

if($open['status'] !== '已完成' && $open['status'] !== '未完成' && $open['status'] !== '已取消'){ //操作订单状态

//错误订单统计

$reslte = false;

$status['type'] = "2";

$status['error'][$keys] = $open['add_id'];

break;

}                                       

          

//字符转义

$open['hotel_name'] = addslashes($open['hotel_name']);

$open['label'] = addslashes($open['label']);

$open['accomodation_type'] = addslashes($open['accomodation_type']);

$open['hotel_city'] = addslashes($open['hotel_city']);

$rizhi_data = array();

$rizhi_data['label'] = $open['label'];

$rizhi_data['type'] = @$open['type'];

$rizhi_data['dd_zong'] = $open['dd_zong'];

$rizhi_data['status'] = $open['status'];

$rizhi_data['money'] = @$open['money'];

$rizhi_data['departure'] = $open['departure'];

$rizhi_data['booked'] = $open['booked'];

$rizhi_data['hotel_country'] = $open['hotel_country'];

$rizhi_data['hotel_name'] = $open['hotel_name'];

$rizhi_data['user_id'] = $open['user_id'];

$rizhi_data['add_id'] = $open['add_id'];

$rizhi_data['time '] = time();

if(!empty($fetch_reslte)){ //含有该数据

if(($open['status'] == '已完成' || $open['status'] == '已取消') && $fetch_reslte['status'] == '未完成' && $fetch_reslte['time_statics'] == 0 && $fetch_reslte['order_static'] == 0){ 

$non = mysqli_query($conn,"UPDATE ims_meepo_book SET 

add_id='{$open['add_id']}',

booked='{$open['booked']}',

arrival='{$open['arrival']}',

departure='{$open['departure']}',

length_of_stay='{$open['length_of_stay']}',

booking_window='{$open['booking_window']}',

status='{$open['status']}',

comission_eur='{$open['comission_eur']}',

progress_bar='{$open['progress_bar']}',

fee='{$open['fee']}',

fee_currency='{$open['fee_currency']}',

exchange_rate='{$open['exchange_rate']}',

ttv='{$open['ttv']}',

slip_nr='{$open['slip_nr']}',

affiliate_id='{$open['affiliate_id']}',

hotel_name='{$open['hotel_name']}',

accomodation_type='{$open['accomodation_type']}',

hotel_country='{$open['hotel_country']}',

hotel_city='{$open['hotel_city']}',

hotel_ufi='{$open['hotel_ufi']}',

booker_country='{$open['booker_country']}',

booker_language='{$open['booker_language']}',

label='{$open['label']}',

label_1='{$label_1}',

label_2='{$label_2}',

user_device='{$open['user_device']}',

travel_purpose='{$open['travel_purpose']}',

user_id='{$open['user_id']}',

ascription_name='{$open['ascription_name']}',

ren_money='{$open['ren_money']}',

dd_zong='{$open['dd_zong']}',

daoru_time='{$open['daoru_time']}'

WHERE add_id = '{$open['add_id']}'");

$status['update'][$keys] = $open['add_id'];

//insert log record

$rizhi_data['rizhi_type'] = '导入订单更新';

add_rizhi_record($rizhi_data);

if($open['status'] == '未完成'){

process($open);

}else if($open['status'] == '已完成'){

complete($open);

}

}else{

$status['repeat'][$keys] = $open['add_id']; 

}

}else{  //未含有该数据

$sql = "INSERT INTO ims_meepo_book (

add_id,

booked,

arrival,

departure,

length_of_stay,

booking_window,

status,

comission_eur,

progress_bar,

fee,

fee_currency,

exchange_rate,

ttv,slip_nr,

affiliate_id,

hotel_name,

accomodation_type,

hotel_country,

hotel_city,

hotel_ufi,

booker_country,

booker_language,

label,

label_1,

label_2,

user_device,

travel_purpose,

user_id,

ascription_name,

ren_money,

dd_zong,

daoru_time

) VALUES (

'{$open['add_id']}',

'{$open['booked']}',

'{$open['arrival']}',

'{$open['departure']}',

'{$open['length_of_stay']}',

'{$open['booking_window']}',

'{$open['status']}',

'{$open['comission_eur']}',

'{$open['progress_bar']}',

'{$open['fee']}',

'{$open['fee_currency']}',

'{$open['exchange_rate']}',

'{$open['ttv']}',

'{$open['slip_nr']}',

'{$open['affiliate_id']}',

'{$open['hotel_name']}',

'{$open['accomodation_type']}',

'{$open['hotel_country']}',

'{$open['hotel_city']}',

'{$open['hotel_ufi']}',

'{$open['booker_country']}',

'{$open['booker_language']}',

'{$open['label']}',

'{$label_1}',

'{$label_2}',

'{$open['user_device']}',

'{$open['travel_purpose']}',

'{$open['user_id']}',

'{$open['ascription_name']}',

'{$open['ren_money']}',

'{$open['dd_zong']}',

'{$open['daoru_time']}'

)";

mysqli_query($sql);

//insert log record

$rizhi_data['rizhi_type'] = '导入订单新增';

add_rizhi_record($rizhi_data);

if($open['status'] == '未完成'){

process($open);

}else if($open['status'] == '已完成'){

complete($open);

}

$non = mysqli_query($conn,$sql);

$status['insert'][$keys] = $open['add_id'];

}

}

if($reslte == false){

mysqli_rollback($conn);

return $status;

          

}else{

mysqli_commit($conn);               

return $status;

          

}

}

catch (Exception $e){

$status['type'] = "3";

$status['exception'][$keys] = $e->getMessage();

mysqli_rollback($conn);

return $status;

      }

finally{

mysqli_rollback($conn);

return $status;

        

}

}

$data_img = $_GPC['no_img'];

$file_jd = dirname(dirname(dirname(dirname(dirname(__FILE__)))));

$link = $file_jd.'/attachment/'.$data_img;

$dbhost = $_W['config']['db']['master']['host'];

$dbuser = $_W['config']['db']['master']['username'];

$dbpass = $_W['config']['db']['master']['password'];

$database  = $_W['config']['db']['master']['database'];

$aData = read_csv($link,$dbhost,$dbuser,$dbpass,$database);

//检测语句

// echo "<pre>";print_r(read_csv($link,$dbhost,$dbuser,$dbpass,$database));exit();

$mix_data = array();

$mix_data['createtime'] = time();

$mix_data['no_img'] = $_POST['no_img'];

pdo_update($this->category_table,$mix_data,array('id'=>$id,'weid'=>$weid));

if($aData['type'] == '1'){

batch_calculate_rebate_ratio();

$ins = count(@$aData['insert']);

$upd = count($aData['update']);

$rep = count(@$aData['repeat']);

message('订单文件导入成功,其中新订单'.$ins.'笔,更新订单'.$upd.'笔,重复订单'.$rep.'笔',$this->createWebUrl('category_manage_list',array('op'=>'post','id'=>'5')),'success');

}else if($aData['type'] == '2'){

$arr_to_str = implode('<br/>',$aData['error']);

message('订单文件导入失败,其中错误订单号'.$arr_to_str,$this->createWebUrl('category_manage_list',array('op'=>'post','id'=>'5')),'error');

}else{

$err = count($aData['exception']);

$arr_to_str = implode('<br/>',$aData['exception']);

message('系统异常请联系管理员'.$arr_to_str,$this->createWebUrl('category_manage_list',array('op'=>'post','id'=>'5')),'error');

}

}else{

message('请勿串改URL',$this->createWebUrl('category_manage_list'),'error');

}

}

}

// 推广激勵

function batch_calculate_rebate_ratio(){

$configs = pdo_fetch("SELECT * FROM ims_meepo_online_list_config WHERE id=1 ", array(''=>''));

$rr_default = $configs['rr_default'];

$rr_10 = $configs['rr_10'];

$rr_20 = $configs['rr_20'];

$ro_2 = $configs['ro_2'];

$ro_10 = $configs['ro_10'];

$ao_default = $configs['ao_default'];

$ao_5 = $configs['ao_5'];

$ao_20 = $configs['ao_20'];

$gong_id = $configs['gong_id'];

//get all users

$result = pdo_fetchall("SELECT * FROM ims_meepo_online_user WHERE id != :gong_id ", array(":gong_id"=>$gong_id));

foreach($result as $row){

$ratio = 0;

$user_id = $row['id'];

//計算推薦總人數

$rr_count = pdo_fetchcolumn("SELECT count(*) FROM ims_meepo_online_user WHERE one=:user_id ", array(":user_id"=>$user_id));

if($rr_count >= 10 && $rr_count < 20){

$ratio+=$rr_10;

}else if($rr_count >= 20){

$ratio+=$rr_20;

}

//計算推薦下單數

$ro_count = pdo_fetchcolumn("SELECT count(*) FROM ims_meepo_book WHERE label_2=:user_id and label_2!=label_1 ", array(":user_id"=>$user_id));

if($ro_count >= 2 && $ro_count < 10){

$ratio+=$ro_2;

}else if($ro_count >= 10){

$ratio+=$ro_10;

}

//計算自主下單數

$ao_count = pdo_fetchcolumn("SELECT count(*) FROM ims_meepo_book WHERE label_2=$user_id and label_2=label_1 ");

if($ao_count >= 5 && $ao_count < 20){

$ratio+=$ao_5;

}else if($ao_count >= 20){

$ratio+=$ao_20;

}

//update user info

$sql = "UPDATE ims_meepo_online_user SET incentive_ratio = {$ratio} WHERE id={$user_id}";

pdo_query($sql);

}

}

function get_type($label_1, $label_2){

$configs = pdo_fetch("SELECT * FROM ims_meepo_online_list_config WHERE id=1 ", array(''=>''));

$gong_user_id = $configs['gong_id'];   

$user_id = $gong_user_id;

$ascription_name = '平台';

$label_explode = explode("-",$user_label);

$user_info = pdo_fetch("SELECT * FROM ims_meepo_online_user WHERE id=:id ", array(":id"=>$label_1));

if(!empty($user_info) && $user_info['id'] != $gong_user_id){

$user_id = $label_explode[0];

if($label_1 == $label_2){

$ascription_name = '自主';   

}else if( $label_1 != 0 && $label_2 == 0){

$ascription_name = '分享'; 

}else if($label_1 != $label_2 && $label_2 != 0){

$ascription_name = '推荐';

}

}

return array('user_id'=>$user_id, 'ascription_name'=>$ascription_name);

}

function process($data){

$time = time();

$baifen = (float)$data['progress_bar']/100;

$configs = pdo_fetch("SELECT * FROM ims_meepo_online_list_config WHERE id=1 ", array(''=>''));   

$label_1 = $data['label_1'];

$label_2 = $data['label_2'];

//a-b 推荐 b推荐a

//a-a 自主 a

//a 分享 

$dingdan_data = array();

$dingdan_data['time'] = $time;

$dingdan_data['add_id'] = $data['add_id'];

$yongjin_data = array();

$yongjin_data['add_id'] = $data['add_id'];

$yongjin_data['departure'] = $data['departure'];

if( !empty($label_1) && (int)$label_1 != 0 && !empty($label_2) && (int)$label_2 != 0 && $label_1 != $label_2){

//a-b

//a

//insert dingdan

$ratio = $configs['ao_default']+get_user_incentive_ratio($label_1);

$money = round(((($data['fee']/$baifen)*$configs['exchange_rate'])*$ratio)/100,2);

$dingdan_data['money'] = $money;

$dingdan_data['user_id'] = $label_1;

$dingdan_data['type'] = '自主';

$dingdan_data['nickname'] = get_user_nickname($label_1);

add_dingdan_record($dingdan_data);

//add_yongjin_record

$yongjin_data['user_id'] = $label_1;

$yongjin_data['money'] = $money;

$yongjin_data['ratio'] = $ratio;

$yongjin_data['type'] = '自主';

add_yongjin_record($yongjin_data);

//b

$ratio = $configs['rr_default'];

$money = round(((($data['fee']/$baifen)*$configs['exchange_rate'])*$ratio)/100,2);

$dingdan_data['money'] = $money;

$dingdan_data['user_id'] = $label_2;

$dingdan_data['type'] = '推荐';

$dingdan_data['nickname'] = get_user_nickname($label_2);

add_dingdan_record($dingdan_data);

//add_yongjin_record

$yongjin_data['user_id'] = $label_2;

$yongjin_data['money'] = $money;

$yongjin_data['ratio'] = $ratio;

$yongjin_data['type'] = '推荐';

add_yongjin_record($yongjin_data);

}else if( !empty($label_1) && (int)$label_1 != 0 && (int)$label_2 == 0 ){

//a

$ratio = $configs['rr_default'];

$money = round(((($data['fee']/$baifen)*$configs['exchange_rate'])*$ratio)/100,2);

$dingdan_data['money'] = $money;

$dingdan_data['user_id'] = $label_1;

$dingdan_data['type'] = '推荐';

$dingdan_data['nickname'] = get_user_nickname($label_1);

add_dingdan_record($dingdan_data);

//add_yongjin_record

$yongjin_data['user_id'] = $label_1;

$yongjin_data['money'] = $money;

$yongjin_data['ratio'] = $ratio;

$yongjin_data['type'] = '推荐';

add_yongjin_record($yongjin_data);

}else if( !empty($label_1) && (int)$label_1 != 0 && (int)$label_1 == (int)$label_2 ){

//a-a

$ratio = $configs['ao_default']+get_user_incentive_ratio($label_1);

$money = round(((($data['fee']/$baifen)*$configs['exchange_rate'])*$ratio)/100,2);

$dingdan_data['money'] = $money;

$dingdan_data['user_id'] = $label_1;

$dingdan_data['type'] = '自主';

$dingdan_data['nickname'] = get_user_nickname($label_1);

add_dingdan_record($dingdan_data);

//add_yongjin_record

$yongjin_data['user_id'] = $label_1;

$yongjin_data['money'] = $money;

$yongjin_data['ratio'] = $ratio;

$yongjin_data['type'] = '自主';

add_yongjin_record($yongjin_data);

}

}

function complete($data){

//check dingdan & yongjin record has been insert

$add_id = $data['add_id'];

$sql = "select count(*) from ims_meepo_online_new_dingdan where add_id = $add_id";

$dingdan_cnt = pdo_fetchcolumn($sql);

$sql = "select count(*) from ims_meepo_yongjin where add_id = $add_id";

$yongjin_cnt = pdo_fetchcolumn($sql);

if($dingdan_cnt > 0 && $yongjin_cnt > 0){

$sql = "select * from ims_meepo_yongjin where add_id = $add_id";

$result = pdo_fetchall($sql);

foreach($result as $row){

$user_id = $row['user_id'];

$money = $row['money'];

update_balance($user_id, $money+get_user_balance($user_id));

}

}else{

process($data);

complete($data);

}

}

function add_rizhi_record($rizhi_data){

$label = $rizhi_data['label'];

$type = $rizhi_data['type'];

$dd_zong = $rizhi_data['dd_zong'];

$status = $rizhi_data['status'];

$money = $rizhi_data['money'];

$departure = $rizhi_data['departure'];

$booked = $rizhi_data['booked'];

$hotel_country = $rizhi_data['hotel_country'];

$hotel_name = $rizhi_data['hotel_name'];

$user_id = $rizhi_data['user_id'];

$rizhi_type = $rizhi_data['rizhi_type'];

$add_id = $rizhi_data['add_id'];

$time = @$rizhi_data['time'];

$sql = "INSERT INTO ims_meepo_online_rizi (label,dingdan_texts,dingdan_money,dingdan_static,fanyong_money,tuifang_time,yuding_time,mingcheng,did,static,add_id,`time`) 

VALUES ('{$label}','{$type}','{$dd_zong}','{$status}','{$money}','{$departure}','{$booked}','({$hotel_country}){$hotel_name}','{$user_id}','{$rizhi_type}','{$add_id}','{$time}')";

// echo $sql;

pdo_query($sql);

}

function add_dingdan_record($dingdan_data){

$add_id = $dingdan_data['add_id'];

$time = $dingdan_data['time'];

$money = $dingdan_data['money'];

$user_id = $dingdan_data['user_id'];

$type = $dingdan_data['type'];

$nickname = $dingdan_data['nickname'];

//insert ims_meepo_online_new_dingdan table

$sql = "INSERT INTO `dyl`.`ims_meepo_online_new_dingdan` (`id`, `add_id`, `static`, `times`, `money`, `uid`, `text`, `name`, `time_srat`) 

VALUES (NULL, $add_id, NULL, '$time', $money, $user_id, '$type', '$nickname', NULL)";

pdo_query($sql);

}

function add_yongjin_record($data){

$add_id = $data['add_id'];

$departure = $data['departure'];

$user_id = $data['user_id'];

$money = $data['money'];

$ratio = $data['ratio'];

$type = $data['type'];

$sql = "INSERT INTO `ims_meepo_yongjin` (`add_id`, `departure`, `user_id`, `money`, `ratio`, `type`, `add_time`) 

VALUES ('$add_id', '$departure', $user_id, $money, $ratio, '$type', NOW())";

pdo_query($sql);

}

function update_balance($user_id, $money){

$sql = "update `ims_meepo_online_user` set balance=$money where id = $user_id";

pdo_query($sql);

}

function get_user_incentive_ratio($user_id){

$sql = "select incentive_ratio from ims_meepo_online_user where id = $user_id";

return pdo_fetchcolumn($sql);

}

function get_user_nickname($user_id){

$sql = "select nickname from ims_meepo_online_user where id = $user_id";

return pdo_fetchcolumn($sql);

}

function get_user_balance($user_id){

$sql = "select balance from ims_meepo_online_user where id = $user_id";

return pdo_fetchcolumn($sql);

}

include $this->template('category_manage_list');