You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
information-system/webht/third_party/trippestOrderSync/controllers/order_finance.php

465 lines
22 KiB
PHTML

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Order_finance extends CI_Controller {
7 years ago
/*!
* 基本流程
* * 获取拼团类别: PVT?
* * 获取拼团总人数, 价格人等
* * 分别计算每次拼团成本
* * * 写入report_tour
* * 写入核算管理CK_GroupInfo
7 years ago
* * 所有产品总成本
* * * 写入report_order
* END
*/
/*!
* GCI_groupType
* 1 - PVT
* 2 - combine
*/
public function __construct(){
parent::__construct();
$this->load->helper('array');
$this->load->model('OrderFinance_model');
mb_regex_encoding("UTF-8");
bcscale(4);
7 years ago
}
public function index()
{
}
public function single_order_report($coli_sn=0)
{
$order_info = $this->OrderFinance_model->get_order_info($coli_sn);
if (empty($order_info)) {
return;
}
$report_order_exists = $this->OrderFinance_model->get_report_order($order_info->ordernumber);
if ( ! empty($report_order_exists)) {
return $this->output->set_content_type('application/json')->set_output(json_encode($report_order_exists));
}
return $this->generate_report($order_info);
}
/*!
* 单团财务表计算
* @date 2018-07-18
* @param integer $coli_sn 订单主表key
* 基本流程:
* * 获取拼团类别: PVT, 拼团
* * 获取拼团总人数, 价格人等
* * 分别计算每次拼团成本
* * * 写入report_tour
* * 所有产品总成本
* * * 写入核算管理CK_GroupInfo
* * * 写入单团财务表report_order
* END
*/
public function generate_report($order_info=array())
{
$coli_sn = $order_info->COLI_SN;
/** 单团财务表 */
$report_order = array();
$report_order['OrderYJLR'] = 0; //预计利润
$report_order['basemoney'] = 0; // 总成本
$report_order['money'] = 0; // 总收入, 实收金额
$report_order['profitmoney'] = 0; // 利润
$report_order['adultnumber'] = 0;
$report_order['childnumber'] = 0;
$report_order['babynumber'] = 0;
$report_order['ordernumber'] = $order_info->ordernumber;
$report_order['TuanName'] = $order_info->TuanName;
$report_order['operater'] = $order_info->operater;
$report_order['Agenter'] = $order_info->Agenter;
$report_order['ChinaName'] = $order_info->ChinaName;
$report_order['country'] = $order_info->country;
$report_order['reservedate'] = $order_info->reservedate;
$report_order['guesttype'] = $order_info->guesttype;
$report_order['RO_GRI_SN'] = $report_order['RO_CGI_SN'] = $order_info->GRI_SN;
// $report_order['basemoney'] = $order_info->basemoney; // 总成本
// $report_order['OrderPrice'] = $order_info->OrderPrice; // 总报价
/** 订单支付信息 */
$order_payment = $this->OrderFinance_model->get_order_payment($coli_sn);
$report_order['paymodenew'] = 0;
$report_order['money'] = $order_payment->SSmoney;
$report_order['RO_PayDescribe'] = $order_payment->payTypeDesc;
$report_order['paydate'] = $order_payment->patDate;
$report_order['OrderPrice'] = $report_order['money'];
/** 订单人数 */
$person_num = $this->OrderFinance_model->get_order_person_num($coli_sn);
$report_order['adultnumber'] = $person_num->adult_num;
$report_order['childnumber'] = $person_num->child_num;
$ret = new stdClass();
/** 图兰朵产品: 取拼团实际成本 */
$ret->combine_cost = array();
7 years ago
$combineNo_arr = $this->OrderFinance_model->get_order_combineNo($coli_sn);
// $ret->x = $combineNo_arr;
7 years ago
$group_type_arr = array_unique(array_map(function($ele){return $ele->GCI_groupType;}, $combineNo_arr));
// $this->output->set_content_type('application/json')->set_output(json_encode($combineNo_arr));
// $this->output->set_content_type('application/json')->set_output(json_encode($group_type_arr));
$processed_code = array(); // 已处理的产品编号
7 years ago
foreach ($combineNo_arr as $kc => $vc) {
if (empty($vc->GCI_combineNo)) {
continue;
}
7 years ago
if ($vc->GCI_groupType == 1) {
$ret->pvt = $pvt_cost = $this->pvt_basic($vc->GCI_combineNo, $coli_sn, $processed_code);
7 years ago
} else if ($vc->GCI_groupType == 2) {
// 这里不排除产品编号是否已处理, 因为重复的情况比较多
$tmp_cost = $this->combine_basic($vc->GCI_combineNo, $coli_sn);
$processed_code = array_merge($processed_code, $tmp_cost->combine_pags);
$ret->combine_cost[] = $tmp_cost;
$tmp_cost = null;
7 years ago
}
}
/** 保存图兰朵包价线路产品的成本到report_tour */
// TODO
// pvt
$report_tour_pvt = array();
if ( ! empty($ret->pvt)) {
$report_tour_pvt['ordernumber'] = $ret->pvt->coli_id;
$report_tour_pvt['tourCode'] = $ret->pvt->PAG_Code;
$report_tour_pvt['tourname'] = $ret->pvt->pag_name;
$report_tour_pvt['tourtime'] = $ret->pvt->startdate;
$report_tour_pvt['tourRSd'] = $ret->pvt->adult_num;
$report_tour_pvt['tourRSx'] = $ret->pvt->child_num;
$report_tour_pvt['tourCostRsd'] = $ret->pvt->person_cost;
$report_tour_pvt['tourCostRSx'] = $ret->pvt->person_cost;
$report_tour_pvt['tourcost'] = $ret->pvt->cost_sum;
$report_tour_pvt['tourPrice'] = $this->OrderFinance_model->convert_to_RMB($ret->pvt->totalPrice);
$report_tour_pvt['tourProfit'] = ($report_tour_pvt['tourPrice']>0) ? bcsub($report_tour_pvt['tourPrice'], $report_tour_pvt['tourcost']) : 0;
$report_tour_pvt['tourProvide'] = $ret->pvt->vendor_name;
$report_tour_pvt['tourBZ'] = $ret->pvt->comment;
$report_tour_pvt['orderstats'] = 0;
// 成本详情
$report_tour_pvt['RPT_Car'] = $ret->pvt->cost_category['touristCarOperations'];
$report_tour_pvt['RPT_Meal'] = $ret->pvt->cost_category['restraurantOperations'];
$report_tour_pvt['RPT_Ticket'] = $ret->pvt->cost_category['sceneryOperations'];
$report_tour_pvt['RPT_Service'] = $ret->pvt->cost_category['guiderOperations'];
$report_tour_pvt['RPT_MealGrants'] = $ret->pvt->cost_category['guide_meal'];
$report_tour_pvt['RPT_Water'] = $ret->pvt->cost_category['water'];
$report_tour_pvt['RPT_Other'] = $ret->pvt->cost_category['otherCosts'];
$report_tour_pvt['RPT_Total'] = $ret->pvt->cost_sum;
$report_tour_pvt['RPT_PersonGrade'] = $ret->pvt->person_grade;
$ret->report_tour[] = $report_tour_pvt;
// 计算订单总成本
$report_order['basemoney'] = bcadd($report_order['basemoney'], $report_tour_pvt['tourcost']);
}
// 拼团
if ( ! empty($ret->combine_cost)) {
foreach ($ret->combine_cost as $kcc => $cost) {
$cost_c = array();
$cost_c['ordernumber'] = $cost->coli_id;
$cost_c['tourCode'] = implode(',', array_unique(array_map(function($ele){ return $ele->real_code; }, $cost->order_cost)));
$cost_c['tourname'] = $cost->pag_name;
$cost_c['tourtime'] = $cost->startdate;
$cost_c['tourRSd'] = $cost->order_cost[0]->adult_num;
$cost_c['tourRSx'] = $cost->order_cost[0]->child_num;
$cost_c['tourCostRsd'] = $cost->person_cost;
$cost_c['tourCostRSx'] = $cost->person_cost;
$cost_c['tourcost'] = $cost->order_cost[0]->order_cost_sum;
$cost_c_price = 0;
foreach ($cost->order_cost as $koc => $coc) {
if ($coc->PAG_code === $coc->real_code) {
$cost_c_price = bcadd($cost_c_price, $coc->totalPrice);
}
}
$cost_c['tourPrice'] = $this->OrderFinance_model->convert_to_RMB($cost_c_price);
$cost_c['tourProfit'] = ($cost_c['tourPrice']>0) ? bcsub($cost_c['tourPrice'], $cost_c['tourcost']) : 0;
$cost_c['tourProvide'] = $cost->vendor_name;
$cost_c['tourBZ'] = $cost->comment;
$cost_c['orderstats'] = 0;
// 成本详情
$cost_c['RPT_Car'] = $cost->cost_category['touristCarOperations'];
$cost_c['RPT_Meal'] = $cost->cost_category['restraurantOperations'];
$cost_c['RPT_Ticket'] = $cost->cost_category['sceneryOperations'];
$cost_c['RPT_Service'] = $cost->cost_category['guiderOperations'];
$cost_c['RPT_MealGrants'] = $cost->cost_category['guide_meal'];
$cost_c['RPT_Water'] = $cost->cost_category['water'];
$cost_c['RPT_Other'] = $cost->cost_category['otherCosts'];
$cost_c['RPT_Total'] = $cost->cost_sum;
$cost_c['RPT_PersonGrade'] = $cost->person_num;
$ret->report_tour[] = $cost_c;
// 计算订单总成本
$report_order['basemoney'] = bcadd($report_order['basemoney'], $cost_c['tourcost']);
}
}
/** 非图兰朵供应商的包价线路产品 */
$other_tour = $this->OrderFinance_model->get_order_detail($coli_sn, 'D', false);
if ( ! empty($other_tour)) {
$ret->others = $this->OrderFinance_model->insert_report_tour_others($coli_sn);
foreach ($ret->others as $kro => $vro) {
// $report_order['basemoney'] = bcadd($report_order['basemoney'], $vro->tourcost);
// test
$report_order['basemoney'] = bcadd($report_order['basemoney'], $vro->COLD_TotalCost);
}
}
/** 火车票预定 */
$train_detail = $this->OrderFinance_model->get_order_detail($coli_sn, '2');
if( ! empty($train_detail)) {
$ret->train = $this->OrderFinance_model->insert_report_train($coli_sn);
foreach ($ret->train as $krt => $vrt) {
// $report_order['basemoney'] = bcadd($report_order['basemoney'], $vrt->TotalCost);
// test
$report_order['basemoney'] = bcadd($report_order['basemoney'], $vrt->TotalCost);
}
}
/** 酒店预订 */
$hotel_detail = $this->OrderFinance_model->get_order_detail($coli_sn, 'A');
if ( ! empty($hotel_detail)) {
$ret->hotel = $this->OrderFinance_model->insert_report_hotel($coli_sn);
foreach ($ret->hotel as $krh => $vrh) {
// $report_order['basemoney'] = bcadd($report_order['basemoney'], $vrh->roomcost);
// test
$report_order['basemoney'] = bcadd($report_order['basemoney'], $vrh->COLD_TotalCost);
}
}
// 订单利润
$report_order['profitmoney'] = $report_order['OrderYJLR'] = bcsub($report_order['money'], $report_order['basemoney']);
$report_order['builddate'] = date('Y-m-d H:i:s');
$report_order['xh'] = $this->OrderFinance_model->get_report_order_xh(); //序号
$ret->report_order = $this->OrderFinance_model->insert_report_order($report_order);
// 核算管理
$this->output->set_content_type('application/json')->set_output(json_encode($ret));
7 years ago
return;
}
/*!
* 拼团计算
* @example 180426360,180516007U
* @date 2018-07-18
7 years ago
* @param string $combineNo
*/
public function combine_basic($combineNo="", $coli_sn=0)
{
$ret = new stdClass();
$all_orders = $this->OrderFinance_model->get_all_combine_order($combineNo);
if (empty($all_orders)) {
return null;
}
$ret->coli_sn = $coli_sn;
$all_pags = array_map(function($ele){ return $ele->PAG_Code;}, $all_orders);
$all_pag = array_unique($all_pags);
$combine_pag_arr = array();
$pvt_code = $this->forbidden_combine();
if (count($all_pag) > 1) {
// 多个产品, 找出拼团产品号
// 先处理拆分拼团的产品, 补全产品号
$allowed_spread = $this->spread_code();
$extra_code_cold = array();
$all_orders_arr = json_decode(json_encode($all_orders), true); // 避免foreach object 改变自身
foreach ($all_orders_arr as $ka => $va) {
$extra_arr = array();
if (isset($allowed_spread[$va['PAG_Code']])) {
foreach ($allowed_spread[$va['PAG_Code']] as $ks => $vs) {
$tmp_extra = new stdClass();
$tmp_extra = (object) $va;
$tmp_extra->real_code = $tmp_extra->PAG_Code;
$tmp_extra->real_pag_sn = $tmp_extra->COLD_ServiceSN;
$tmp_extra->PAG_Code = $vs;
$tmp_extra->COLD_ServiceSN = null;
$extra_arr[] = $tmp_extra;
}
}
$extra_code_cold = array_merge($extra_code_cold, $extra_arr);
}
// 不直接merge,避免覆盖key
foreach ($extra_code_cold as $ke => $ve) {
$all_orders[] = $ve;
}
// 重新计算重复次数
$all_pags = array_filter(array_map(function($ele){ return $ele->PAG_Code;}, $all_orders));
// $ret->s = $all_pags;
// 重复的产品号; 可混拼的产品
$repeat_code = array();
$code_count = array_count_values($all_pags);
foreach ($all_pag as $kp => $vp) {
if (in_array($vp, $pvt_code)) {
continue;
}
$tmp = null;
$tmp = $this->get_allowed_combine($vp);
$this_allowed = ($tmp === null) ? null : array_diff($tmp, $all_pag); // diff((31,41), (31,41,47)) ==> ()
if (empty($this_allowed) && $tmp !== null) {
$combine_pag_arr[] = $tmp; // push (31,41)
}
// 重复的产品即为拼团
if($code_count[$vp] > 1) {
$repeat_code[] = $vp;
}
}
if ( ! empty($repeat_code)) {
$combine_pag_arr[] = $repeat_code;
}
} else {
// 单个产品
$combine_pag_arr[] = ($all_pag);
}
// 预定多产品, 但是只有部分发了计划
// 预定多产品, 拼团只有一个订单, 相当于整团PVT
if (empty($combine_pag_arr)) {
// $combine_pag_arr[] = array_diff($all_pag, $pvt_code); // 此处排除PVT租车产品
$combine_pag_arr[] = $all_pag;
}
$ret->combine_pags = my_array_unique($combine_pag_arr);
$ret->combine_pags = $ret->combine_pags[0]; // 这里用0是因为一个拼团应该只有一组或一个产品
$ret->person_num = 0;
$ret->PAG_Code = "";
$ret->order_cost = array();
$pag_sns = array();
$unique_coli = array();
foreach ($all_orders as $ko => $vo) {
if (in_array($vo->PAG_Code, $ret->combine_pags)) {
$pag_sns[] = $vo->COLD_ServiceSN;
// 整团单拼时, 避免重复计算人数
// 订单人数不重复计
if ( ! in_array($vo->PAG_Code, $pvt_code) && ! in_array($vo->COLI_SN, $unique_coli)) {
$unique_coli[] = $vo->COLI_SN;
$ret->person_num += $vo->COLD_PersonNum + $vo->COLD_ChildNum;
}
$ret->startdate = $vo->COLD_StartDate;
$tour_s = new stdClass();
if ($vo->COLI_SN == $coli_sn) {
$tour_s->COLD_SN = $vo->COLD_SN;
$tour_s->person_num = $vo->COLD_PersonNum + $vo->COLD_ChildNum;
$tour_s->adult_num = $vo->COLD_PersonNum;
$tour_s->child_num = $vo->COLD_ChildNum;
$tour_s->PAG_code = $vo->PAG_Code;
$tour_s->real_code = isset($vo->real_code) ? $vo->real_code : $vo->PAG_Code;
$tour_s->real_pag_sn = isset($vo->real_pag_sn) ? $vo->real_pag_sn : $vo->COLD_ServiceSN;
$tour_s->totalPrice = $vo->COLD_TotalPrice;
$ret->order_cost[] = $tour_s;
$ret->coli_id = $vo->coli_ID;
}
}
}
$this_order_real_pag_sns = array_map(function($ele) {return $ele->real_pag_sn;}, $ret->order_cost);
$pags_info = $this->OrderFinance_model->get_pag_info(implode(',', array_unique(array_filter($this_order_real_pag_sns)))); // $pag_sns
$ret->vendor_name = implode(",", array_values(array_unique(array_map(function($ele) {return $ele->VEI2_CompanyBN;}, $pags_info)))) ; // 50
$ret->pag_name = implode(";\r\n", array_map(function($ele) {return $ele->PAG_Title;}, $pags_info)) ; // TODO 限制200 用mb_substr('UTF-8')
$ret->PAG_Code = implode(",", $ret->combine_pags);
$ret->comment = "拼团" . $combineNo . ", 按" . $ret->person_num . "人等";
$combine_cost = $this->OrderFinance_model->get_combine_sumMoney($combineNo);
$ret->cost_category = $combine_cost->cost_category;
$ret->cost_sum = $combine_cost->cost_sum;
$ret->person_cost = bcdiv($ret->cost_sum, $ret->person_num);
foreach ($ret->order_cost as $kc => $voc) {
$ret->order_cost[$kc]->order_cost_sum = bcmul($voc->person_num, $ret->person_cost);
}
return $ret;
// return $this->output->set_content_type('application/json')->set_output(json_encode($ret, JSON_UNESCAPED_UNICODE));
}
/*!
7 years ago
* pvt计算
* @example 180515061M
* @date 2018-07-18
*/
public function pvt_basic($combineNo="", $coli_sn=0, $processed_code=array())
{
7 years ago
$ret = new stdClass();
// $ret->tour = array();
$all_orders = $this->OrderFinance_model->get_all_combine_order($combineNo, $processed_code);
7 years ago
if (empty($all_orders)) {
return null;
}
$ret->coli_sn = $coli_sn;
$ret->coli_id = $all_orders[0]->coli_ID;
// 总报价
$ret->totalPrice = 0;
foreach ($all_orders as $kal => $val) {
$ret->totalPrice = bcadd($ret->totalPrice, $val->COLD_TotalPrice);
}
7 years ago
// 预定的产品数
$ret->tour_count = count(array_unique(array_map(function($ele) {return $ele->PAG_Code;}, $all_orders)));
$person_num = $this->OrderFinance_model->get_order_person_num($coli_sn);
$ret->person_num = $person_num->person_num;
$ret->adult_num = $person_num->adult_num;
$ret->child_num = $person_num->child_num;
// $tour_s = new stdClass();
7 years ago
$combine_cost = $this->OrderFinance_model->get_combine_sumMoney($combineNo);
$ret->startdate = $all_orders[0]->COLD_StartDate;
$ret->person_grade = ($all_orders[0]->COLD_PersonNum + $all_orders[0]->COLD_ChildNum );
$ret->cost_category = $combine_cost->cost_category;
$ret->cost_sum = $combine_cost->cost_sum;
$ret->person_cost = bcdiv($ret->cost_sum, $ret->person_num);
$ret->comment = "PVT,共" . $ret->person_num . "人次";// 150
7 years ago
$pag_sns = array_values(array_unique(array_map(function($ele) {return $ele->COLD_ServiceSN;}, $all_orders)));
$pags_info = $this->OrderFinance_model->get_pag_info(implode(',', $pag_sns));
$ret->PAG_Code = implode(",", array_values(array_unique(array_map(function($ele) {return $ele->PAG_Code;}, $pags_info)))); // TODO 限制50
$ret->vendor_name = implode(",", array_values(array_unique(array_map(function($ele) {return $ele->VEI2_CompanyBN;}, $pags_info)))) ; // 50
$ret->pag_name = implode(";\r\n", array_map(function($ele) {return $ele->PAG_Title;}, $pags_info)) ; // TODO 限制200 用mb_substr('UTF-8')
// $ret->tour[0] = $tour_s;
// $ret->order_cost = array_sum(array_map(function($ele) {return $ele->cost_sum;}, $ret->tour));
return $ret;
// return $this->output->set_content_type('application/json')->set_output(json_encode($ret, JSON_UNESCAPED_UNICODE));
}
7 years ago
/*!
* 获取允许拼团的数组
* @date 2018-07-18
* @param string $tour_code 产品编号
* @return array 含允许拼团产品编号的数组
*/
public function get_allowed_combine($tour_code="")
{
$allowed = null;
$all_allowed = $this->allowed_combine();
foreach ($all_allowed as $key => $va) {
if (in_array($tour_code, $va)) {
$allowed = $va;
break;
}
}
return $allowed;
}
/** 允许混合拼团的不同编号产品 */
public function allowed_combine()
{
return array(
7 years ago
// * 不同产品但部分行程相同所以允许拼团 *
array("XASIC-15", "XASIC-41"),
7 years ago
array("BJSIC-47", "BJSIC-41"),
// * 产品编号不同实际是同一产品 *
array("SHSIC-31", "SHSIC-41"),
array("SHSIC-32", "SHSIC-42"),
array("SHSIC-33", "SHSIC-43"),
array("SHSIC-34", "SHSIC-44")
);
}
/** 允许拆分再拼团的产品编号 */
public function spread_code()
{
return array(
"XASIC-42" => array("XASIC-41"),
"BJSIC-42" => array("BJSIC-41"),
"BJSIC-43" => array("BJSIC-41","BJSIC-42"),
"SHSIC-42" => array("SHSIC-41"),
"SHSIC-43" => array("SHSIC-41","SHSIC-42")
);
}
/** 肯定是PVT */
public function forbidden_combine()
{
return array(
"BJALC-209",
"BJSIC-16",
"SHSIC-45",
"XASIC-16"
);
}
}
/* End of file order_finance.php */
/* Location: ./webht/third_party/trippestOrderSync/controllers/order_finance.php */