|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834 |
- <?php
- /**
- *
- * ============================================================================
- * * 版权所有 蜘蛛出行 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 温依莅
- * PhpStorm GetTouristStock.php 组合产品成本,库存操作类
- * Create By 2017/6/28 9:44 $
- */
- namespace backend\modules\api\logic;
-
- use backend\modules\api\models\OperaProductRun;
- use backend\modules\api\models\OperaTouristCommon;
- use common\models\CheckData;
- use backend\modules\api\logic\GetStockData;
-
- use common\models\OperaTouristRun;
- use common\util\HotelUtil;
- use yii\db\Query;
- use yii\base\Exception;
- use yii\db\Expression;
-
- use Yii;
-
- /**
- * 组合产品成本库存操作
- */
- class GetTouristStock extends Query
- {
- /**
- * Function Description:获取格式化的组合产品库存成本
- * Function Name: getTouristStock
- * @param int $tourist_id
- * @param string $run_date 出发日期
- * @param int $org_id 组合产品售卖渠道id
- * @param int $org_id 渠道id
- * @param int $user_id 用户id
- * @param int $main_corp_id 运营主体id
- * @return array
- *
- * @author 温依莅
- */
- public function getFormatTouristStock($tourist_id, $run_date, $org_id, $user_id, $main_corp_id = -1)
- {
- $res = $this->getTouristStock($tourist_id, $run_date, $org_id, $user_id, $main_corp_id = -1);
- $result = array();
- $result['code'] = '0';
- $result['info'] = '获取成功';
- //在此组合产品存在子资源不可用的情况时,这里不返回具体错误信息,而是返回各库存为0
- if ($res['code'] != '0') {
- $final_stock_arr = array(
- 'tourist_id' => $tourist_id,
- 'limit_total_num' => 0, //渠道限制销售总库存
- 'limit_adult_num' => 0, //渠道限制销售成人总库存
- 'max_total_num' => 0, //总体剩余库存
- 'max_adult_num' => 0, //总体成人剩余库存
- 'adult_cost' => 0, //成人成本
- 'child_cost' => 0, //儿童成本
- 'diff_cost' => 0 //单人差成本
- );
- $result['list'] = $final_stock_arr;
- } else {
- $result['list'] = $res['list'];
- }
- //若成功,则返回实际库存成本数据
-
- return $result;
- }
-
- /**
- * Function Description:获取自由行产品实际需要支付金额
- * Function Name: getTouristPayTotal
- * @param int $tourist_id 自由行产品id
- * @param string $run_date 出发时间
- * @param int $org_id 自由行产品渠道id
- * @param array $prod_arr 成人儿童订票信息
- * @param int $user_id 用户id
- *
- * @return array
- *
- * @author 温依莅
- */
- public function getTouristPayTotal($tourist_id, $run_date, $org_id = 164, $user_id = 2, $prod_arr)
- {
- $result = array();
- //1,处理参数 (微信所传格式为TR-10001;成人票:TR-1001-1,儿童票:TR-1001-2)
- #1.1 处理tourist_id
- $res = $this->getTouristPara($tourist_id);
- $tourist_id = $res['real_id'];
- #1.2处理prod_arr :[['prod_id'=>'TR-1000-1','prod_name'=>'成人票','prod_num'=>'4'],['prod_id'=>'TR-1000-2','prod_name'=>'儿童票','prod_num'=>'3']]
- if (!$prod_arr) {
- $result['code'] = '1';
- $result['info'] = '无票种信息';
- return $result;
- }
- $adult_num = 0;
- $child_num = 0;
- foreach ($prod_arr as $k => $v) {
- if (!isset($v['prod_id']) || !isset($v['prod_num'])) {
- $result['code'] = '1';
- $result['info'] = '票种信息错误';
- return $result;
- }
- $res = $this->getTouristPara($v['prod_id']);
- if ($res['list'][2] == 1) {
- $adult_num += $v['prod_num'];
- } else {
- $child_num += $v['prod_num'];
- }
-
- }
- if ($adult_num == 0 && $child_num == 0) {
- $result['code'] = '1';
- $result['info'] = '订票数量不可为0';
- return $result;
- }
- //2,判断该组合产品是否整体可售(opera_tourist_common 表的is_onsale字段 和是否在可售时间限制内)
- #2.1主产品是否可售
- $res = $this->getTouristIsOnSale($tourist_id);
- if ($res['code'] != '0') {
- $result['code'] = '1';
- $result['info'] = '所选产品不可售';
- return $result;
- }
- #2.2是否在可售时间范围内
- $res = OperaTouristCommon::judgeTouristTimeAllow($tourist_id, $run_date);
- if (!$res) {
- $result['code'] = '1';
- $result['info'] = '不在允许售卖日期内';
- return $result;
- }
- //3,调用库存接口判断获取自由行库存数据
- $res = $this->getTouristStock($tourist_id, $run_date, $org_id, $user_id);
- if ($res['code'] != '0') {
- $result['code'] = '1';
- $result['info'] = '所选产品无库存';
- return $result;
- }
- //4,判断是自由行产品库存是否满足
- $limit_total_num = $res['list']['limit_total_num'];//渠道限定总人数
- $limit_adult_num = $res['list']['limit_adult_num'];//渠道限定成人人数
- if (($limit_total_num < $adult_num + $child_num) || ($limit_adult_num < $adult_num)) {
- $result['code'] = '1';
- $result['info'] = '该选择库存不足';
- return $result;
- }
- //5,根据巴士酒店门票各自的库存成本信息,结合自由行产品的销售规则
- $stock_list = $res['list']['extra_info'];
- #5.1 根据成人,儿童人数获取 自由行产品实际总成本
- $bus_total_cost = 0;
- $hotel_total_cost = 0;
- $ticket_total_cost_adult = 0;
- $ticket_total_cost_child = 0;
- foreach ($stock_list as $sk => $sv) {
- if ($sk == 'bus_extra_info' && !empty($sv)) { //巴士
- $bus_total_cost = ($adult_num + $child_num) * $sv['list']['adult_cost'];
- //$tourist_total_cost = ($adult_num + $child_num) * $sv['list']['adult_cost'];
- } else if ($sk == 'hotel_extra_info' && !empty($sv)) { //酒店
- #-【酒店】获取酒店住店人数
- $hotel_people_num = $adult_num;
- if ($sv['crowd_type'] == '11') {
- $hotel_people_num = $adult_num + $child_num;
- }
- #-【酒店】循环根据酒店入住人数和单房间最大人数获取实际房间数,和相应成本
- $hotel_list = $sv['list'];
- if (!empty($hotel_list)) {
- foreach ($hotel_list as $hk => $hv) {
- foreach ($hv as $dk => $dv) {
- $add_para = $dv['cost'] * ceil($hotel_people_num / $dv['room_max_people']);
- //$tourist_total_cost += $add_para;
- $hotel_total_cost += $add_para;
- }
- }
- }
-
- } else if ($sk == 'ticket_extra_info' && isset($sv['crowd_type'])) { //门票
- $ticket_total_cost_adult += $adult_num * $sv['list']['adult_cost'];
- //$tourist_total_cost += $adult_num * $sv['list']['adult_cost'];
- if ($sv['crowd_type'] != '10') {
- $ticket_total_cost_child += $child_num * $sv['list']['child_cost'];
- //$tourist_total_cost += $child_num * $sv['list']['child_cost'];
- }
- }
- }
- #5.2 根据自由行产品在该日的加价方式获取最终售价
- $opera_tourist_run=new OperaTouristRun();
- $sale_res=$opera_tourist_run->getTouristAddInfo($tourist_id, $run_date, $org_id);
- if ($sale_res['code'] != '0') {
- $result['code'] = '1';
- $result['info'] = '所选产品该日期已下架';
- return $result;
- }
- $tourist_total_cost_adult = round($bus_total_cost*($adult_num/($adult_num+$child_num)),2) + round(0.5*$hotel_total_cost,2) + $ticket_total_cost_adult;
- $tourist_total_cost_child = ($bus_total_cost-round($bus_total_cost*($adult_num/($adult_num+$child_num)),2)) + ($hotel_total_cost-round(0.5*$hotel_total_cost,2)) + $ticket_total_cost_child;
- $final_total_money = 0;
- $sale_type_adult = $sale_res['list']['sale_type_adult'];
- $sale_type_child = $sale_res['list']['sale_type_child'];
- $diff_arr_adult = $sale_res['list']['diff_para_adult'];
- $diff_arr_child = $sale_res['list']['diff_para_child'];
- if ($sale_type_adult == 1) {
- $final_total_money_adult =$tourist_total_cost_adult+ $adult_num * $diff_arr_adult;//固定加价
- } else {
- $final_total_money_adult = $tourist_total_cost_adult * (1+0.01 * $diff_arr_adult);//百分比加价
- }
- if($sale_type_child ==1){
- $final_total_money_child=$tourist_total_cost_child+$child_num * $diff_arr_child;//固定加价
- }else{
- $final_total_money_child = $tourist_total_cost_child * (1+0.01 * $diff_arr_child);//百分比加价
- }
- $final_total_money = floor($final_total_money_adult+$final_total_money_child);//最终价格向下取整
- #5.3 判断最终自由行产品总售价,如果非正则报相应错误信息
- if ($final_total_money <= 0) {
- $result['code'] = '1';
- $result['info'] = '售价不能低于0';
- return $result;
- }
- $tourist_total_cost=$tourist_total_cost_child+$tourist_total_cost_adult;
- $extra_info = array(
- 'tourist_id' => $tourist_id,
- 'run_date' => $run_date,
- 'org_id' => $org_id,
- 'total_cost' => $tourist_total_cost,
- 'bus_cost' => $bus_total_cost,
- 'hotel_cost' => $hotel_total_cost,
- 'ticket_cost' => $ticket_total_cost_adult+$ticket_total_cost_child,
- 'adult_num' => $adult_num,
- 'child_num' => $child_num
- );
- $result['code'] = '0';
- $result['info'] = '获取自由行产品应付金额成功';
- $result['list'] = array('total_money' => $final_total_money, 'extra_info' => $extra_info);
- return $result;
- }
-
- /**
- * Function Description:判断自由行产品是否可售
- * Function Name: getTouristIsOnSale
- * @param int $tourist_id 自由行产品id
- * @return array
- *
- * @author 温依莅
- */
- public function getTouristIsOnSale($tourist_id)
- {
- $result = array();
- $res = (new Query())->select('a.tourist_id,a.is_onsale,a.pre_days,a.pre_time')
- ->from('opera_tourist_common as a')
- ->where('a.tourist_id=' . $tourist_id)
- ->limit(1)->one();
- if (!$res || $res['is_onsale'] == 0) {
- $result['code'] = '1';
- $result['info'] = '该自由行产品不可售';
- return $result;
- }
- $result['code'] = '0';
- $result['info'] = '该自由行产品可售';
- return $result;
-
- }
-
- /**
- * Function Description:参数转换:将TR-1101格式转为1101
- * Function Name: getTouristPara
- * @param int $tourist_id
- *
- * @return mixed
- *
- * @author 温依莅
- */
- public function getTouristPara($tourist_id)
- {
- if (strpos($tourist_id, 'TR-') === false) {
- return array('real_id' => $tourist_id, 'list' => array());
- } else {
- $arr = explode('-', $tourist_id);
- return array('real_id' => $arr[1], 'list' => $arr);
- }
- }
-
- /**
- * Function Description:获取自由行产品价格日历加价方式
- * Function Name: getTouristAddInfo
- * @param int $tourist_id 自由行产品id
- * @param string $run_date 出发时间
- * @param int $org_id 自由行销售渠道id
- *
- * @return array
- *
- * @author 温依莅
- */
- public function getTouristAddInfo($tourist_id, $run_date, $org_id)
- {
- $result = array();
- $res = (new Query())->select('tourist_id,sale_type_adult,diff_para_adult,sale_type_child,diff_para_child,is_onsale')->from('opera_tourist_run')->where(['tourist_id' => $tourist_id, 'run_date' => $run_date, 'to_org_id' => $org_id])->limit(1)->one();
- if (!$res) { //没有在opera_product_run设置,取默认设置
- $sale_type_adult = 2;
- $sale_type_child = 2;
- $diff_para_adult = Yii::$app->params['tourist_profit'] ? Yii::$app->params['tourist_profit'] : 10;
- $diff_para_child = Yii::$app->params['tourist_profit'] ? Yii::$app->params['tourist_profit'] : 10;
- } else if (!$res['is_onsale']) { //该渠道日期下架不可售
- $result['code'] = '1';
- $result['info'] = '该日期不可售';
- return $result;
- } else {
- $sale_type_adult = $res['sale_type_adult'];
- $sale_type_child = $res['sale_type_child'];
- $diff_para_adult = $res['diff_para_adult'];
- $diff_para_child = $res['diff_para_child'];
- }
- $result['code'] = '0';
- $result['info'] = '获取加价信息成功';
- $result['list'] = array('sale_type_adult' => $sale_type_adult, 'diff_para_adult' => $diff_para_adult,'sale_type_child'=>$sale_type_child,'diff_para_child'=>$diff_para_child);
- return $result;
- }
-
- /**
- * Function Description:获取组合产品的库存和成本
- * Function Name: getTouristStock
- * @param int $tourist_id
- * @param string $run_date 出发日期
- * @param int $org_id 组合产品售卖渠道id
- * @param int $org_id 组合产品销售渠道id
- * @param int $user_id 用户id
- * @param int $main_corp_id 运营主体id
- * @return array
- *
- * @author 温依莅
- */
- public function getTouristStock($tourist_id, $run_date, $org_id, $user_id, $main_corp_id = -1)
- {
- $stock_obj = new GetStockData();
- //1,校验参数,获取部分数据
- $result = array();
- $param = array('tourist_id' => $tourist_id, 'run_date' => $run_date, 'org_id' => $org_id, 'user_id' => $user_id, 'main_corp_id' => $main_corp_id);
- $check_obj = new CheckData();
- if (!$check_obj->checkArrayData($param, [[['tourist_id', 'user_id'], 2], [['main_corp_id'], 3], [['run_date'], 6]])) {
- $result['code'] = '1';
- $result['info'] = '参数格式不正确(tourist_id,run_date)';
- return $result;
- }
- #1.1 获取运营主体id
- $corp_res = $this->getTouristMainCorpId($user_id, $main_corp_id);
- if ($corp_res['code'] != '0') {
- $result['code'] = '1';
- $result['info'] = $corp_res['info'];
- return $result;
- }
- $main_corp_id = $corp_res['list']['main_corp_id'];//获取运营主体id
- #1.2获取运营主体内部采购渠道id
- // $purchase_res = $this->getPurchaseOrgId($main_corp_id);
- // if ($purchase_res['code'] != '0') {
- // $result['code'] = '1';
- // $result['info'] = $purchase_res['info'];
- // return $result;
- // }
- // $purchase_org_id = $purchase_res['list']['purchase_org_id'];
- //2,根据组合产品tourist_id获取组合产品子资源信息
- #2.1 初步获取组合产品
- $res = $this->getTouristResource($tourist_id);
- if ($res['code'] != '0') {
- $result['code'] = '1';
- $result['info'] = $res['info'];
- return $result;
- }
- $res_info = $res['list']['res_info'];
- #2.2,按日期,资源类型重排组合资源数组
- $tourist_arr = array();
- foreach ($res_info as $k => $v) {
- $the_date = date('Y-m-d', strtotime('+' . $v['day_from_start'] . ' day', strtotime($run_date)));//获取组合产品各子资源生效日期
- $tourist_arr[$the_date][$v['res_type']][$v['id']] = $v;
- }
- //3,处理组合产品子资源获取库存和成本
- $stock_info = array();//库存数组
- #按日期循环
- foreach ($tourist_arr as $tk => $tv) {
-
- #-按资源类型循环($rk:1巴士 2酒店 3门票)
- foreach ($tv as $rk => $rv) {
- #--巴士资源
- if ($rk == 1) {
- #--【巴士】获取巴士人群类型(成人10,儿童01,成人儿童11)
- if (!isset($stock_info[$tk]['bus']['crowd_type'])) {
- $tmp_arr = current($rv);
- $bus_crowd_type = $tmp_arr['crowd_type'];
- $stock_info[$tk]['bus']['crowd_type'] = $bus_crowd_type;
- }
- #--【巴士】循环该日期该资源类型下的具体巴士子资源
- foreach ($rv as $sk => $sv) {
- #取巴士库存时所用的渠道商(传0时取蜘蛛国旅)
- $bus_org_id=$org_id==0?1:$org_id;
- #---【巴士】获取班次run_id
- $run_res = $this->getBusRunInfo($sv['sub_res_id'], $tk, $sv['extra_res_info']);
- if ($run_res['code'] != '0') {
- $result['code'] = '1';
- $result['info'] = $run_res['info'];
- return $result;
- }
- #---【巴士】获取巴士该班次票种的成本
- $bus_cost = $stock_obj->getBusBasicCost($sv['sub_res_id'], $bus_org_id, $user_id, $main_corp_id);
- if ($bus_cost['code'] != '0') {
- $result['code'] = '1';
- $result['info'] = $bus_cost['info'];
- return $result;
- }
-
- if (!isset($stock_info[$tk]['bus']['cost'])) {
- $stock_info[$tk]['bus']['cost'] = 0;//$bus_cost['list']['sale_price'];
- }
- $stock_info[$tk]['bus']['cost'] += $bus_cost['list']['sale_price'];//获取巴士当日成本
- #---【巴士】获取巴士该班次票种的库存
- $bus_stock = $stock_obj->getBusBasicStock($run_res['list']['run_id'], $run_res['list']['prod_id']);
- if ($bus_stock['code'] != '0') {
- $result['code'] = '1';
- $result['info'] = $bus_stock['info'];
- return $result;
- }
- #---【巴士】比较获取该日期该班次票种的单次可售最大数
-
- if (!isset($stock_info[$tk]['bus']['stock'])) {
- $stock_info[$tk]['bus']['stock'] = $bus_stock['list']['single_max_stock'];
- }
- $stock_info[$tk]['bus']['stock'] = min($stock_info[$tk]['bus']['stock'], $bus_stock['list']['single_max_stock']);
- #---【巴士】参数集合
- $stock_info[$tk]['bus']['detail_list'][$sk]['cost'] = $bus_cost['list']['sale_price'];
- $stock_info[$tk]['bus']['detail_list'][$sk]['stock'] = $bus_stock['list']['single_max_stock'];
- $stock_info[$tk]['bus']['detail_list'][$sk]['param'] = array('run_id' => $run_res['list']['run_id'], 'prod_id' => $sv['sub_res_id'], 'org_id' => $bus_org_id, 'user_id' => $user_id, 'main_corp_id' => $main_corp_id);
-
- }
- #--酒店资源
- } else if ($rk == 2) {
- #酒店获取价格默认渠道时为微信渠道#
- $hotel_org_id=$org_id==0?164:$org_id;
- #--【酒店】获取酒店人群类型(成人10,儿童01,成人儿童11)
- if (!isset($stock_info[$tk]['hotel']['crowd_type'])) {
- $tmp_arr = current($rv);
- $hotel_crowd_type = $tmp_arr['crowd_type'];
- $stock_info[$tk]['hotel']['crowd_type'] = $hotel_crowd_type;
- }
- #--【酒店】循环该日期该资源类型下的酒店子资源
- foreach ($rv as $sk => $sv) {
- #---【酒店】获取酒店该日期该子房型该渠道下的库存和成本
- $hotel_stock = HotelUtil::getHotelStockAndPrice($sv['extra_res_info'], $sv['top_res_id'], $sv['sub_res_id'], $tk, $hotel_org_id);
- if ($hotel_stock['code'] != '0') {
- $result['code'] = '1';
- $result['info'] = $hotel_stock['info'];
- return $result;
- }
- if (!isset($stock_info[$tk]['hotel']['cost'])) {
- $stock_info[$tk]['hotel']['cost'] = 0;//$hotel_stock['list']['price'];
- $stock_info[$tk]['hotel']['diff_cost'] = 0;//单人差成本
- }
- #---【酒店】判断酒店房间最大容纳人数,若小于1,返回错误信息
- if ($sv['max_num'] < 1) {
- $result['code'] = '1';
- $result['info'] = 'hotel_id:' . $sv['extra_res_info'] . '房型最大人数无效';
- return $result;
- }
- $stock_info[$tk]['hotel']['cost'] += round($hotel_stock['list']['price'] / $sv['max_num'], 2);//获取酒店当日成本
- $single_room_diff = ($sv['max_num'] == 1) ? 0 : round($hotel_stock['list']['price'] / $sv['max_num'], 2);//酒店单人差
- $stock_info[$tk]['hotel']['diff_cost'] += $single_room_diff;
-
- #---【酒店】比较获取该日期该酒店子房型的单次可售最大数
- if (!isset($stock_info[$tk]['hotel']['channel_stock'])) {
- $stock_info[$tk]['hotel']['channel_stock'] = $hotel_stock['list']['limit_total_num'] * $sv['max_num'];//酒店的渠道限制库存【人数】(对该运营主体的内部采购渠道)
- }
- if (!isset($stock_info[$tk]['hotel']['total_stock'])) {
- $stock_info[$tk]['hotel']['total_stock'] = $hotel_stock['list']['max_total_num'] * $sv['max_num'];//酒店的总体库存【人数】(对该运营主体的内部采购渠道)
- }
- $stock_info[$tk]['hotel']['channel_stock'] = min($stock_info[$tk]['hotel']['channel_stock'], $hotel_stock['list']['limit_total_num'] * $sv['max_num']);
- $stock_info[$tk]['hotel']['total_stock'] = min($stock_info[$tk]['hotel']['total_stock'], $hotel_stock['list']['max_total_num'] * $sv['max_num']);
- #---【酒店】参数集合
- $stock_info[$tk]['hotel']['detail_list'][$sk]['channel_stock'] = $hotel_stock['list']['limit_total_num'] * $sv['max_num'];
- $stock_info[$tk]['hotel']['detail_list'][$sk]['total_stock'] = $hotel_stock['list']['max_total_num'] * $sv['max_num'];
- $stock_info[$tk]['hotel']['detail_list'][$sk]['cost'] = $hotel_stock['list']['price'];
- $stock_info[$tk]['hotel']['detail_list'][$sk]['diff_cost'] = $single_room_diff;
- $stock_info[$tk]['hotel']['detail_list'][$sk]['room_max_people'] = $sv['max_num'];//单间子房间最大人数
- $stock_info[$tk]['hotel']['detail_list'][$sk]['param'] = array('hotel_id' => $sv['extra_res_info'], 'base_room_type' => $sv['top_res_id'], 'room_type' => $sv['sub_res_id'], 'run_date' => $tk, 'org_id' => $hotel_org_id);
- }
- #--门票资源
- } else if ($rk == 3) {
- $ticket_has_adult = 0;//门票是否有成人子资源
- $ticket_has_child = 0;//门票是否有儿童子资源
- #--【门票】获取门票人群类型(成人10,儿童01,成人儿童11)
- if (!isset($stock_info[$tk]['ticket']['crowd_type'])) {
- $tmp_arr = current($rv);
- $ticket_crowd_type = $tmp_arr['crowd_type'];
- $stock_info[$tk]['ticket']['crowd_type'] = $ticket_crowd_type;
- }
- if ($ticket_crowd_type == '11') { //成人+儿童
- $ticket_has_adult = 1;
- $ticket_has_child = 1;
- } else if ($ticket_crowd_type == '10') { //成人
- $ticket_has_adult = 1;
- $ticket_has_child = 0;
- } else if ($ticket_crowd_type == '01') { //儿童
- $ticket_has_adult = 0;
- $ticket_has_child = 1;
- }
- #--【门票】循环该日期该资源类型下的门票子资源
- foreach ($rv as $sk => $sv) {
-
- #---【门票】获取门票该日期的库存和成本【门票这里产品要区分成人和儿童】
- #---(1),【门票成人】
- if ($ticket_has_adult) {
- $ticket_stock_adult = $stock_obj->getTicketBasicStock($sv['top_res_id'], $sv['sub_res_id'], $tk,$org_id);
- if ($ticket_stock_adult['code'] != '0') {
- $result['code'] = '1';
- $result['info'] = $ticket_stock_adult['info'];
- return $result;
- }
- if (!isset($stock_info[$tk]['ticket']['adult_cost'])) {
- $stock_info[$tk]['ticket']['adult_cost'] = 0;//$ticket_stock_adult['list']['base_price'];
- }
- $stock_info[$tk]['ticket']['adult_cost'] += $ticket_stock_adult['list']['base_price'];//获取门票当日成人成本
- #---【门票】比较获取该日期该门票子票种(成人类型)的单次可售最大数
- if (!isset($stock_info[$tk]['ticket']['adult_stock'])) {
- $stock_info[$tk]['ticket']['adult_stock'] = $ticket_stock_adult['list']['total_stock'];//门票[成人]的库存
- }
-
- $stock_info[$tk]['ticket']['adult_stock'] = min($stock_info[$tk]['ticket']['adult_stock'], $ticket_stock_adult['list']['total_stock']);
- }
- #---(2),【门票儿童】
- if ($ticket_has_child) {
- $ticket_stock_child = $stock_obj->getTicketBasicStock($sv['top_res_id'], $sv['sub_res_id1'], $tk,$org_id);
- if ($ticket_stock_child['code'] != '0') {
- $result['code'] = '1';
- $result['info'] = $ticket_stock_child['info'];
- return $result;
- }
- if (!isset($stock_info[$tk]['ticket']['child_cost'])) {
- $stock_info[$tk]['ticket']['child_cost'] = 0;//$ticket_stock_child['list']['base_price'];
- }
- $stock_info[$tk]['ticket']['child_cost'] += $ticket_stock_child['list']['base_price'];//获取门票当日儿童成本
- #---【门票】比较获取该日期该门票子票种(儿童类型)的单次可售最大数
- if (!isset($stock_info[$tk]['ticket']['child_stock'])) {
- $stock_info[$tk]['ticket']['child_stock'] = $ticket_stock_child['list']['total_stock'];//门票[成人]的库存
- }
- $stock_info[$tk]['ticket']['child_stock'] = min($stock_info[$tk]['ticket']['child_stock'], $ticket_stock_child['list']['total_stock']);
- }
- #---【门票】参数集合
- if ($ticket_has_adult) {
- $stock_info[$tk]['ticket']['detail_list'][$sk]['adult_stock'] = $ticket_stock_adult['list']['total_stock'];
- $stock_info[$tk]['ticket']['detail_list'][$sk]['adult_cost'] = $ticket_stock_adult['list']['base_price'];
- }
- if ($ticket_has_child) {
- $stock_info[$tk]['ticket']['detail_list'][$sk]['child_stock'] = $ticket_stock_child['list']['total_stock'];
- $stock_info[$tk]['ticket']['detail_list'][$sk]['child_cost'] = $ticket_stock_child['list']['base_price'];
- }
- $stock_info[$tk]['ticket']['detail_list'][$sk]['param'] = array('main_prod_id' => $sv['top_res_id'], 'sub_prod_id' => $sv['sub_res_id'], 'run_date' => $tk);
- }
- }
- }
-
- }
-
- //4,整合处理组合产品库存信息
- $hotel_extra_info = array();//酒店成本信息
- $bus_extra_info = array('list' => array('adult_cost' => 0, 'child_cost' => 0));//巴士成本信息
- $ticket_extra_info = array('list' => array('adult_cost' => 0, 'child_cost' => 0));//门票成本信息
- #4.1循环得到每日的库存成本数据
- foreach ($stock_info as $date_k => $date_v) {
- #初始化该日期的数组
- $date_stock_arr[$date_k] = array(
- 'limit_total_num' => 1000000, //渠道限制销售总库存
- 'limit_adult_num' => 1000000, //渠道限制销售成人总库存
- 'max_total_num' => 1000000, //总体剩余库存
- 'max_adult_num' => 1000000, //总体成人剩余库存
- 'adult_cost' => 0, //成人成本
- 'child_cost' => 0, //儿童成本
- 'diff_cost' => 0 //单人差成本
- );
- foreach ($date_v as $res_k => $res_v) {
- if ($res_k == 'bus') { //【巴士】成人+儿童
- #处理得到巴士成本信息
- $bus_extra_info['crowd_type'] = $res_v['crowd_type'];
- $bus_extra_info['list']['adult_cost'] += $res_v['cost'];
- $bus_extra_info['list']['child_cost'] += $res_v['cost'];
-
- $date_stock_arr[$date_k]['limit_total_num'] = min($date_stock_arr[$date_k]['limit_total_num'], $res_v['stock']);
- $date_stock_arr[$date_k]['limit_adult_num'] = min($date_stock_arr[$date_k]['limit_adult_num'], $res_v['stock']);
- $date_stock_arr[$date_k]['max_total_num'] = min($date_stock_arr[$date_k]['max_total_num'], $res_v['stock']);
- $date_stock_arr[$date_k]['max_adult_num'] = min($date_stock_arr[$date_k]['max_adult_num'], $res_v['stock']);
- $date_stock_arr[$date_k]['adult_cost'] += $res_v['cost'];
- $date_stock_arr[$date_k]['child_cost'] += $res_v['cost'];
- //$date_stock_arr[$date_k]['diff_cost'] += 0;
-
- } else if ($res_k == 'hotel') { //【酒店】
- #处理得到酒店成本信息
- $hotel_extra_info['crowd_type'] = $res_v['crowd_type'];
- $hotel_extra_info['list'][$date_k] = $res_v['detail_list'];
-
- if ($res_v['crowd_type'] == '10') { #【酒店】人群:仅成人
-
- //$date_stock_arr[$date_k]['limit_total_num'] = min($date_stock_arr[$date_k]['limit_total_num'], $res_v['channel_stock']);//仅成人情况,此处不再取最小值
- $date_stock_arr[$date_k]['limit_adult_num'] = min($date_stock_arr[$date_k]['limit_adult_num'], $res_v['channel_stock']);
- //$date_stock_arr[$date_k]['max_total_num'] = min($date_stock_arr[$date_k]['max_total_num'], $res_v['total_stock']);//仅成人情况,此处不再取最小值
- $date_stock_arr[$date_k]['max_adult_num'] = min($date_stock_arr[$date_k]['max_adult_num'], $res_v['total_stock']);
- $date_stock_arr[$date_k]['adult_cost'] += $res_v['cost'];
- //$date_stock_arr[$date_k]['child_cost'] += $res_v['cost'];
- $date_stock_arr[$date_k]['diff_cost'] += $res_v['diff_cost'];
-
- } else { #【酒店】人群:成人+儿童
-
- $date_stock_arr[$date_k]['limit_total_num'] = min($date_stock_arr[$date_k]['limit_total_num'], $res_v['channel_stock']);
- $date_stock_arr[$date_k]['limit_adult_num'] = min($date_stock_arr[$date_k]['limit_adult_num'], $res_v['channel_stock']);
- $date_stock_arr[$date_k]['max_total_num'] = min($date_stock_arr[$date_k]['max_total_num'], $res_v['total_stock']);
- $date_stock_arr[$date_k]['max_adult_num'] = min($date_stock_arr[$date_k]['max_adult_num'], $res_v['total_stock']);
- $date_stock_arr[$date_k]['adult_cost'] += $res_v['cost'];
- $date_stock_arr[$date_k]['child_cost'] += $res_v['cost'];
- $date_stock_arr[$date_k]['diff_cost'] += $res_v['diff_cost'];
- }
-
-
- } else if ($res_k == 'ticket') { //【门票】
- #处理得到门票成本信息
- $ticket_extra_info['crowd_type'] = $res_v['crowd_type'];
- $ticket_extra_info['list']['adult_cost'] += $res_v['adult_cost'];
- if ($res_v['crowd_type'] == '10') {
- $ticket_extra_info['list']['child_cost'] = 0;
- } else {
- $ticket_extra_info['list']['child_cost'] += $res_v['child_cost'];
- }
-
- if ($res_v['crowd_type'] == '10') { #【门票】人群:仅成人
-
- $date_stock_arr[$date_k]['limit_total_num'] = min($date_stock_arr[$date_k]['limit_total_num'], $res_v['adult_stock']);
- $date_stock_arr[$date_k]['limit_adult_num'] = min($date_stock_arr[$date_k]['limit_adult_num'], $res_v['adult_stock']);
- $date_stock_arr[$date_k]['max_total_num'] = min($date_stock_arr[$date_k]['max_total_num'], $res_v['adult_stock']);
- $date_stock_arr[$date_k]['max_adult_num'] = min($date_stock_arr[$date_k]['max_adult_num'], $res_v['adult_stock']);
- $date_stock_arr[$date_k]['adult_cost'] += $res_v['adult_cost'];
- //$date_stock_arr[$date_k]['child_cost'] += $res_v['child_cost'];
- //$date_stock_arr[$date_k]['diff_cost'] += $res_v['diff_cost'];
-
- } else { #【门票】人群:成人+儿童
- $date_stock_arr[$date_k]['limit_total_num'] = min($date_stock_arr[$date_k]['limit_total_num'], $res_v['adult_stock'] + $res_v['child_stock']);
- $date_stock_arr[$date_k]['limit_adult_num'] = min($date_stock_arr[$date_k]['limit_adult_num'], $res_v['adult_stock']);
- $date_stock_arr[$date_k]['max_total_num'] = min($date_stock_arr[$date_k]['max_total_num'], $res_v['adult_stock'] + $res_v['child_stock']);
- $date_stock_arr[$date_k]['max_adult_num'] = min($date_stock_arr[$date_k]['max_adult_num'], $res_v['adult_stock']);
- $date_stock_arr[$date_k]['adult_cost'] += $res_v['adult_cost'];
- $date_stock_arr[$date_k]['child_cost'] += $res_v['child_cost'];
- //$date_stock_arr[$date_k]['diff_cost'] += $res_v['diff_cost'];
- }
- }
- }
-
- }
- #4.2整合得到最终库存成本数据
- $final_stock_arr = array(
- 'tourist_id' => $tourist_id,
- 'limit_total_num' => 1000000, //渠道限制销售总库存
- 'limit_adult_num' => 1000000, //渠道限制销售成人总库存
- 'max_total_num' => 1000000, //总体剩余库存
- 'max_adult_num' => 1000000, //总体成人剩余库存
- 'adult_cost' => 0, //成人成本
- 'child_cost' => 0, //儿童成本
- 'diff_cost' => 0, //单人差成本
- 'extra_info' => array('bus_extra_info' => $bus_extra_info, 'hotel_extra_info' => $hotel_extra_info, 'ticket_extra_info' => $ticket_extra_info)//附属数据供计算价格使用
- );
-
- foreach ($date_stock_arr as $dk => $dv) {
- $final_stock_arr['limit_total_num'] = min($final_stock_arr['limit_total_num'], $dv['limit_total_num']);
- $final_stock_arr['limit_adult_num'] = min($final_stock_arr['limit_adult_num'], $dv['limit_adult_num']);
- $final_stock_arr['max_total_num'] = min($final_stock_arr['max_total_num'], $dv['max_total_num']);
- $final_stock_arr['max_adult_num'] = min($final_stock_arr['max_adult_num'], $dv['max_adult_num']);
- $final_stock_arr['adult_cost'] += $dv['adult_cost'];
- $final_stock_arr['child_cost'] += $dv['child_cost'];
- $final_stock_arr['diff_cost'] += $dv['diff_cost'];
- }
- #4.3对库存做非负处理
- $final_stock_arr['ori_stock'] = array('limit_total_num' => $final_stock_arr['limit_total_num'], 'limit_adult_num' => $final_stock_arr['limit_adult_num'], 'max_total_num' => $final_stock_arr['max_total_num'], 'max_adult_num' => $final_stock_arr['max_adult_num']);
- $final_stock_arr['limit_total_num'] < 0 && $final_stock_arr['limit_total_num'] = 0;
- $final_stock_arr['limit_adult_num'] < 0 && $final_stock_arr['limit_adult_num'] = 0;
- $final_stock_arr['max_total_num'] < 0 && $final_stock_arr['max_total_num'] = 0;
- $final_stock_arr['max_adult_num'] < 0 && $final_stock_arr['max_adult_num'] = 0;
-
-
- $result['code'] = '0';
- $result['info'] = '获取组合产品库存成本信息成功';
- $result['list'] = $final_stock_arr;
- return $result;
- }
-
- /**
- * Function Description:获取组合产品子资源
- * Function Name: getTouristResource
- * @param $tourist_id
- *
- * @return array
- * @author 温依莅
- */
- public function getTouristResource($tourist_id)
- {
- $res = (new Query())->select([
- 'id',
- 'tourist_id',
- 'create_user_id',
- 'create_time',
- 'update_user_id',
- 'update_time',
- 'cancel_flag',
- 'res_type',
- 'top_res_id',
- 'sub_res_id',
- 'sub_res_id1',
- 'select_res_ids',
- 'select_res_ids1',
- 'extra_res_info',
- 'crowd_type',
- 'max_num',
- 'day_from_start',
- ])->from('opera_tourist_detail')
- ->where([
- 'tourist_id' => $tourist_id,
- 'cancel_flag' => 0
- ])->all();
- if (!$res) {
- $result['code'] = '1';
- $result['info'] = '无符合条件组合产品';
- return $result;
- }
- $result['code'] = '0';
- $result['info'] = '获取组合产品子资源成功';
- $result['list'] = array('tourist_id' => $tourist_id, 'res_info' => $res);
- return $result;
- }
-
- /**
- * Function Description:获取运营主体内部采购渠道id
- * Function Name: getPurchaseOrgId
- * @param $main_corp_id
- *
- * @return array
- * @author 温依莅
- */
- public function getPurchaseOrgId($main_corp_id)
- {
- $result = array();
- $res = (new Query())->select(['id', 'purchase_org_id'])->from('base_main_corporation')->where(['id' => $main_corp_id])->one();
- if (!$res) {
- $result['code'] = '1';
- $result['info'] = '获取运营主体内部采购渠道出错';
- return $result;
- }
- $result['code'] = '0';
- $result['info'] = '获取运营主体内部采购渠道成功';
- $result['list'] = array('purchase_org_id' => $res['purchase_org_id'], 'main_corp_id' => $main_corp_id);
- return $result;
- }
-
- /**
- * Function Description:获取运营主体id
- * Function Name: getTouristMainCorpId
- * @param int $user_id 用户id
- * @param int $main_corp_id 运营主体id,若非-1,则直接取该值,否则从user_id判断main_corp_id值
- *
- * @return mixed
- *
- * @author 温依莅
- */
- public function getTouristMainCorpId($user_id, $main_corp_id = -1)
- {
- $res = (new Query())->select('id,main_corp_id,user_name')->from('base_user')->where(['id' => $user_id, 'cancel_flag' => 0])->one();
- if (!$res) {
- $result['code'] = '1';
- $result['info'] = '无效用户';
- return $result;
- }
- $real_main_corp_id = ($main_corp_id == -1 ? $res['main_corp_id'] : $main_corp_id);
- if ($real_main_corp_id == 0) { //如果main_corp_id为0,下单时视为蜘蛛国旅(运营主体为1)处理
- $real_main_corp_id = 1;
- }
- $result['code'] = '0';
- $result['info'] = '获取运营主体成功';
- $result['list'] = array('user_id' => $user_id, 'main_corp_id' => $real_main_corp_id);
- return $result;
- }
-
- /**
- * Function Description:巴士根据票种,和票种出发时间判断班次run_id
- * Function Name: getBusRunInfo
- * @param int $prod_id 巴士票种id
- * @param string $run_date 出发日期
- * @param string $start_time 票种的出发时间
- *
- * @return array
- *
- * @author 温依莅
- */
- public function getBusRunInfo($prod_id, $run_date, $start_time)
- {
- $result = array();
- //1,判断日期格式是否正确
- if (!preg_match('/^(0[0-9]|1[0-9]|2[0-3])\:(0[0-9]|[1-5][0-9])$/', $start_time)) {
- $result['code'] = '1';
- $result['info'] = '巴士产品出发时间格式不正确';
- return $result;
- }
- //2,获取班次信息
- $res = (new Query())->select([
- 'rm.run_id',
- 'rm.run_date',
- 'rp.parent_prod_id as line_id',
- 'rp.prod_id',
- 'rs.station_res_id',
- 'rs.start_time',
- ])->from('run_prod as rp')
- ->leftJoin('run_main as rm', 'rm.run_id=rp.run_id')
- ->leftJoin('run_station rs', 'rp.start_station_res_id=rs.station_res_id')
- ->leftJoin('run_bus rb', 'rb.RUN_ID = rp.RUN_ID')
- ->where([
- 'rp.prod_id' => $prod_id,
- 'rm.run_date' => $run_date,
- 'rs.start_time' => $start_time,
- 'rp.cancel_flag' => 0,
- 'rb.run_bus_status' => 138,
- ])->limit(1)->one();
- if (!$res) {
- $reason = '票种' . $prod_id . '在' . $run_date . ' ' . $start_time . '无符合条件班次';
- $result['code'] = '1';
- $result['info'] = $reason;
- return $result;
- }
-
- $result['code'] = '0';
- $result['info'] = '获取巴士班次成功';
- $result['list'] = $res;
- return $result;
- }
- }
|