|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- <?php
- /**
- *
- * ============================================================================
- * * 版权所有 蜘蛛出行 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 温依莅
- * PhpStorm GetStockData.php 获取巴士,酒店,门票的库存和成本价信息
- * Create By 2017/6/23 9:44 $
- */
-
- namespace backend\modules\api\logic;
-
-
- use backend\modules\api\models\OperaProduct;
- use backend\modules\api\models\OperaProductRun;
- use backend\modules\api\models\OperaTickets;
- use backend\modules\api\models\RunMain;
- use backend\modules\api\models\RunProd;
- use backend\modules\api\models\OperaLine;
- use backend\modules\api\models\RunStation;
- use backend\modules\api\logic\SubmitBusAgentOrder;
-
- use yii\db\Query;
- use yii\base\Exception;
- use yii\db\Expression;
-
- use Yii;
-
- /**
- * 产品成本库存数据
- */
- class GetStockData extends Query
- {
- /**
- * Function Description:根据 班次run_id,票种prod_id得到 车资源库存
- * Function Name: getBusBasicStock
- * @param int $run_id 班次
- * @param int $prod_id 车次
- *
- * @return array
- *
- * @author 温依莅
- */
- public function getBusBasicStock($run_id, $prod_id)
- {
- //1,获取班次票种相关信息
- $res = RunProd::find()
- ->select([
- 'run_id' => 'rm.run_id',//班次id
- 'run_date' => 'rm.run_date',//出发日期
- 'run_time' => 'rm.run_time',//出发时间
- 'run_minutes' => 'rm.run_minutes',//出发分钟数
- 'prod_id' => 'rp.prod_id',//票种id
- 'prod_name' => 't.ticket_name',//票种名称
- 'seat_type' => 't.seat_type',//座位类型
- 'human_type' => 't.human_type',//人群属性
- 'line_id' => 't.line_id',//线路id
- 'line_name' => 'l.line_name',//线路名称
- 'start_res_id' => 't.start_station_res_id',//上车站id
- 'start_area_id' => 't.start_station_area_id',//出发地id
- 'start_seq_id' => 's1.station_order_id',//开始站顺序号
- 'start_time' => 's1.start_time',//开始时间
- 'start_minutes' => 's1.start_minutes',//开始分钟数
- 'end_res_id' => 't.end_station_res_id',//下车站id
- 'end_area_id' => 't.end_station_area_id',//目的地id
- 'end_seq_id' => 's2.station_order_id',//结束站顺序号
- 'end_time' => 's2.start_time',//结束时间
- 'end_minutes' => 's2.start_minutes',//结束分钟数
- ])
- ->from(RunProd::tableName() . ' as rp')
- ->leftJoin(RunMain::tableName() . ' as rm', 'rp.run_id = rm.run_id')
- ->leftJoin(OperaTickets::tableName() . ' as t', 'rp.prod_id = t.ticket_id')
- ->leftJoin(OperaLine::tableName() . ' as l', 't.line_id = l.line_id')
- ->leftJoin(RunStation::tableName() . ' as s1', 'rm.run_id = s1.run_id and l.line_id = s1.prod_id and t.start_station_res_id = s1.station_res_id')
- ->leftJoin(RunStation::tableName() . ' as s2', 'rm.run_id = s2.run_id and l.line_id = s2.prod_id and t.end_station_res_id = s2.station_res_id')
- ->where([
- 'and',
- ['=', 'rp.run_id', $run_id],
- ['=', 'rp.prod_id', $prod_id],
- ['=', 'rp.cancel_flag', 0],
- ['=', 'l.cancel_flag', 0],
- ['=', 'l.is_onsale', 1],
- ['=', 't.cancel_flag', 0],
- ['=', 't.is_onsale', 1],
- ]);
- $res_sql = $res->createCommand()->getRawSql();
- $result = array();
- $prod_info = $res->asArray()->one();
- if (!$prod_info) {
- $result['code'] = '2';
- $result['info'] = "班次-$run_id 票种-$prod_id 不可售";
- return $result;
- }
- $run_x_name = 'run_' . date('Ym', strtotime($prod_info['run_date']));
- //2,初步获取run_x表相关座位信息
- $res2 = (new Query())
- ->select('group_concat(id) as run_x_id,run_id,bus_order_id,seat_x,seat_y,seat_type,seat_seq_id,seat_name,count(id) as seat_count')
- ->from($run_x_name)
- ->where([
- 'and',
- ['=', 'run_id', $prod_info['run_id']],
- ['>=', 'order_id', $prod_info['start_seq_id']],
- ['<', 'order_id', $prod_info['end_seq_id']],
- ['=', 'seat_type', $prod_info['seat_type']],
- ['=', 'cancel_flag', 0],
- ['=', 'seat_status', 0],
- ])->groupBy(['run_id', 'bus_order_id', 'seat_type', 'seat_seq_id'])
- ->orderBy('seat_seq_id ASC')->all();
- if (!$res2) {
- $bus_arr = (new Query())->select('bus_order_id')->from('run_bus')->where(['run_id' => $run_id, 'cancel_flag' => 0])->all();//获取车次信息
- $stock_list = array('run_id' => $run_id, 'prod_id' => $prod_id, 'total_stock' => 0, 'single_max_stock' => 0);
- foreach ($bus_arr as $k => $v) {
- $stock_list['sub_list'][$v['bus_order_id']]['bus_order_id'] = $v['bus_order_id'];
- $stock_list['sub_list'][$v['bus_order_id']]['sub_stock'] = 0;
- }
- $result['code'] = '0';
- $result['info'] = '获取库存成功';
- $result['list'] = $stock_list;
-
- return $result;
-
- }
- //3,进一步筛选符合条件座位数
- $seat_arr = array();
- foreach ($res2 as $k => $v) {
- if ($v['seat_count'] == ($prod_info['end_seq_id'] - $prod_info['start_seq_id'])) {
- $seat_arr[$v['bus_order_id']][$v['seat_seq_id']] = $v;
- }
- }
- //4,处理得到库存数组
- $stock_list = array('run_id' => $run_id, 'prod_id' => $prod_id, 'total_stock' => 0, 'single_max_stock' => 0);
- foreach ($seat_arr as $ak => $av) {
- $add = count($av);
- $stock_list['single_max_stock'] = max($add, $stock_list['single_max_stock']); //单次最大购买数量
- $stock_list['total_stock'] += $add; //可购买总数量
- $stock_list['sub_list'][$ak]['bus_order_id'] = $ak;
- $stock_list['sub_list'][$ak]['sub_stock'] = $add;
- }
-
- $result['code'] = '0';
- $result['info'] = '获取巴士库存成功';
- $result['list'] = $stock_list;
- return $result;
- }
-
- /**
- * Function Description:得到巴士票种 在特定渠道和运营主体的 成本
- * Function Name: getBusBasicCost
- * @param int $prod_id 票种id
- * @param int $org_id 渠道id
- * @param int $user_id 下单用户id
- * @param int $main_corp_id 下单运营主体id
- *
- * @return array
- *
- * @author 温依莅
- */
- public function getBusBasicCost($prod_id, $org_id, $user_id, $main_corp_id = -1)
- {
- $agentObj = new SubmitBusAgentOrder();
- $result = array();
- //1,获取巴士票种在该渠道的成本(包含代售情形)
- $prod_info = $agentObj->getTicketAgentInfo($prod_id, $org_id, $user_id, $main_corp_id);
- if (!isset($prod_info['code']) || $prod_info['code'] != '0') {
- $result['code'] = '1';
- $result['info'] = $prod_info['info'];
- return json_encode($result);
- }
- $detail = $prod_info['list']['ticket_agent_list'][1];
- //2,获取巴士票种在该渠道的售价(包含代售情形)
- #1.1,判断是否是代售
-
- $is_agent = $prod_info['list']['is_agent'];
- $main_corp_id = $detail['main_corp_id']; //运营主体
- $opera_ticket_name = $is_agent ? 'opera_tickets_agent' : 'opera_tickets';
- $agent_condition = $is_agent ? ['and', ['=', 'ot.authority_status', 1], ['=', 'ot.to_main_corp_id', $main_corp_id]] : '';
- #1.2,获取巴士票种渠道数据
-
- $ticket_info = (new Query())
- ->select([
- 'ot.ticket_id',//票种id
- 'ot.cus_price',//零售价
- 'ot.prod_price',//分销价
- 'pub_sale_type' => 'bs.sale_type',//公有销售方式-->177 分销,277 代理,312 直营
- 'pri_sale_type' => 'otd.sale_type',//销售方式-->177 分销,277 代理,312 直营
- 'ticket_price' => 'otd.prod_price',//销售价格-->分销价格?
- //'otd.authority_status' //该渠道该票种是否授权
- ])
- ->from($opera_ticket_name . ' as ot')
- ->leftJoin('opera_tickets_distrib as otd', 'ot.ticket_id = otd.ticket_id and otd.cancel_flag = 0 and otd.supplier_id = ' . $org_id)
- ->leftJoin('base_supplier_sale as bs', 'bs.supplier_id = ' . $org_id . ' and bs.parent_type = 310')
- ->where([
- 'and',
- ['=', 'ot.ticket_id', $prod_id],
- ['=', 'ot.cancel_flag', 0],
- ['=', 'ot.is_onsale', 1],
- ['=', 'ot.ticket_type', 1],
- $agent_condition,
- ['=', 'bs.cancel_flag', 0],
- ['or', 'otd.authority_status=1', 'otd.authority_status is null'],
- ])->one();
-
- #1.3 根据规则获取巴士产品在该渠道的售价
- $sale_price = $this->getTicketPrice($ticket_info);
-
- //2,获取巴士票种在该渠道的成本(包含代售情形)
-
- $list['prod_id'] = $prod_id;
- $list['org_id'] = $detail['outside_sale_org_id'];//渠道id
- $list['main_corp_id'] = $detail['main_corp_id'];//下单运营主体id
- $list['source_main_corp_id'] = $detail['source_main_corp_id'] + 0;//票种所属运营主体id
- $list['base_price'] = $detail['base_price'];//票种成本
- $list['sale_price'] = $sale_price;//票种在该渠道的售价
-
-
- $result['code'] = '0';
- $result['info'] = '获取巴士成本成功';
- $result['list'] = $list;
-
- return $result;
- }
-
- /**
- * Function Description:获取票种销售价格
- * Function Name: getTicketPrice
- * @param mixed $ticket_info 票种信息
- *
- * @return mixed
- *
- * @author 张帅
- */
- public function getTicketPrice($ticket_info)
- {
- $sale_type = empty($ticket_info['pri_sale_type']) ? $ticket_info['pub_sale_type'] : $ticket_info['pri_sale_type'];
- #region 获取票种价格 判断销售规则是否设定价格
- if (empty($ticket_info['ticket_price'])) {
- #region 判断销售类型
- if ($sale_type == 177) {
- $result = $ticket_info['prod_price'];
- } else {
- $result = $ticket_info['cus_price'];
- }
- #endregion
- } else {
- $result = $ticket_info['ticket_price'];
- }
- #endregion
- return $result;
- }
-
- /**
- * Function Description:获取门票的成本和库存
- * Function Name: getTicketBasicStock
- * @param int $main_prod_id 门票父票种id
- * @param int $sub_prod_id 门票子票种id
- * @param string $run_date
- *
- * @return array
- *
- * @author 温依莅
- */
- public function getTicketBasicStock($main_prod_id, $sub_prod_id, $run_date, $to_org_id)
- {
-
- $model = new OperaProductRun();
- $res = $model->getProdInfo($main_prod_id, $sub_prod_id, $run_date, $to_org_id);
- // $res = (new Query())->select([
- // 'b.parent_id as main_prod_id',
- // 'a.prod_id as sub_prod_id',
- // 'a.run_date',
- // 'a.prod_price',
- // 'a.cus_price as base_price',
- // 'a.total_count',
- // 'a.saled_count',
- // '(a.total_count-a.saled_count) as stock',
- // 'a.is_onsale',
- // ])
- // ->from('opera_product_run a')
- // ->leftJoin('opera_product b', 'a.prod_id=b.prod_id')
- // ->where([
- // 'a.prod_id' => $sub_prod_id,
- // 'b.parent_id' => $main_prod_id,
- // 'a.run_date' => $run_date,
- // 'a.is_onsale' => 1,
- // 'a.cancel_flag' => 0,
- // 'b.is_onsale' => 1,
- // 'b.cancel_flag' => 0,
- // ])->one();
- $result = array();
- if (!$res) {
- $result['code'] = '1';
- $result['info'] = "($main_prod_id-$sub_prod_id-$run_date)该门票票种不可售";
- return $result;
- }
-
- //票种日期参数
- $list['main_prod_id'] = $res['main_prod_id'];
- $list['sub_prod_id'] = $res['sub_prod_id'];
- $list['run_date'] = $res['run_date'];
- $list['base_price'] = $res['base_price'];
- $list['total_stock'] = $res['stock'];
-
- $result['code'] = '0';
- $result['info'] = '获取门票成本库存成功';
- $result['list'] = $list;
- return $result;
- }
-
- /**
- * Function Description:获取酒店库存和成本
- * Function Name: getHotelProInfo
- * @param int $hotel_id 酒店id
- * @param int $base_room_type 基础房型
- * @param int $room_type 房型类型
- * @param string $run_date 入住日期
- * @param int $org_id 渠道id
- *
- * @return array
- *
- * @author 温依莅
- */
- public function getHotelBasicStock($hotel_id, $base_room_type, $room_type, $run_date, $org_id)
- {
- //1,获取每日房间属性
- $hotel_info = (new Query())
- ->select([
- 'rhd.run_date',//入住日期
- 'remaining_count' => new Expression("case rhd.run_status when 326 then rhd.remaining_count when 329 then -1 else 0 end"),//库存剩余数量
- 'rhd.oversell_flag',//允许超卖标志 1:允许超卖 0:不允许超卖
- 'rhd.consume_stock_type',//下单时消耗的库存类型顺序
- 'price' => new Expression("case when bss.sale_type = 177 then rhd.prod_price else rhd.cus_price end"),//价格
- 'bss.sale_type',//销售类型
- 'bss.commision_flag',
- 'bss.commision_type',
- 'bss.back_commision_type',
- 'bss.back_commision_method',
- 'bss.back_percent',
- 'bss.back_value',
- ])
- ->from('run_hotel_distrib as rhd')
- ->leftJoin('base_supplier_sale as bss', 'rhd.distrib_id = bss.supplier_id')
- ->leftJoin('opera_hotel as oh', 'oh.hotel_id=rhd.hotel_id')
- ->leftJoin('opera_hotel_base_room as ohbr', 'ohbr.hotel_id=rhd.hotel_id and ohbr.base_room_type = rhd.base_room_type')
- ->leftJoin('opera_hotel_room as ohr', 'ohr.hotel_id=rhd.hotel_id and ohr.parent_room_type = rhd.base_room_type and ohr.room_type = rhd.room_type')
- ->leftJoin('run_hotel_sub_room as rhsr', 'rhsr.hotel_id = rhd.hotel_id and rhsr.base_room_type = rhd.base_room_type and rhsr.room_type=rhd.room_type')
- ->where([
- 'and',
- ['=', 'rhd.distrib_id', $org_id],
- ['=', 'rhd.hotel_id', $hotel_id],
- ['=', 'rhd.room_type', $room_type],
- ['=', 'rhd.base_room_type', $base_room_type],
- ['=', 'rhd.run_date', $run_date],
- ['=', 'bss.parent_type', 25],
- ['=', 'bss.cancel_flag', 0],
- ['=', 'rhd.authority_status', 1],//run_hotel_distrib 授权
- ['=', 'rhd.run_status', 326],
- ['=', 'oh.hotel_status', 1],
- ['=', 'ohbr.cancel_flag', 0],
- ['=', 'ohr.is_onsale', 1],
- ['=', 'rhsr.run_status', 1],
- ])->one();
- if (!$hotel_info) {
- $result['code'] = '1';
- $result['info'] = "($hotel_id-$base_room_type-$room_type-$run_date-$org_id)该酒店房型不可售";
- return $result;
- }
-
- //2, 获取不同库存类型
- $stock_type_arr = explode(',', $hotel_info['consume_stock_type']);//下单时消耗的库存类型顺序
- $stock_type_arr = array_unique($stock_type_arr);
- $hotel_info['stock_type_arr'] = $stock_type_arr;//下单时消耗的库存类型顺序
-
-
- //3, 获取不同库存类型的库存及成本
-
- $stock_list = (new Query())
- ->select([
- 'rh.run_date',//入住日期
- 'rh.stock_type',//库存类型
- 'rh.remaining_count',//库存剩余数量
- 'cost_price' => new Expression("case rh.stock_type when 228 then rhs.base_price_buyout when 230 then rhs.base_price_reserve when 229 then rhs.base_price_inquiry else 0 end"),//价格
- ])
- ->from('run_hotel as rh')
- ->leftJoin('run_hotel_sub_room as rhs', 'rh.hotel_id = rhs.hotel_id and rh.base_room_type = rhs.base_room_type and rh.run_date = rhs.run_date')
- ->where([
- 'and',
- ['=', 'rh.hotel_id', $hotel_id],
- ['=', 'rhs.room_type', $room_type],
- ['=', 'rh.base_room_type', $base_room_type],
- ['=', 'rh.run_date', $hotel_info['run_date']],
- ['in', 'rh.stock_type', $hotel_info['stock_type_arr']],
- ['=', 'rhs.run_status', 1],
- ['=', 'rh.is_onsale', 1],//run_hotel上线
- ['=', 'rhs.is_onsale', 1],//run_hotel_sub_room 上线
- ])->all();
-
- if (!$stock_list) {
- $result['code'] = '1';
- $result['info'] = '该酒店房型不可售';
- return $result;
- }
-
- $stock_list_arr = [];
- foreach ($stock_list as $key => $vel) {
- $stock_list_arr[$vel['run_date'] . '-' . $vel['stock_type']] = $vel;
- }
-
- $hotel_info['limit_total_num'] = 0;//库存
- //4,获取总库存:max((买断228+保留230),现询229) ---$hotel_info['max_total_num']
- $sure_reserve = (int)$stock_list_arr[$hotel_info['run_date'] . '-' . 228]['remaining_count'] + (int)$stock_list_arr[$hotel_info['run_date'] . '-' . 230]['remaining_count'];
- $inquiry = (int)$stock_list_arr[$hotel_info['run_date'] . '-' . 229]['remaining_count'];
- #$max_total_num = max($sure_reserve, $inquiry); modify by nizf 现改为 获取总库存:(228+229+230)
- $max_total_num = $sure_reserve + $inquiry;
- $hotel_info['max_total_num'] = $max_total_num;
- //5,根据渠道超卖判断渠道最大可售库存 ---$hotel_info['limit_total_num']
- if ($hotel_info['oversell_flag'] == 1) {
- $hotel_info['limit_total_num'] = $max_total_num;
- } else {
- $hotel_info['limit_total_num'] = min($hotel_info['remaining_count'], $max_total_num) + 0;
- }
- //6,各类型子房型(买断228,保留230,现询229)详细信息(成本,库存)
- foreach ($hotel_info['stock_type_arr'] as $stock_key => $stock_vel) {
- if (isset($stock_list_arr[$hotel_info['run_date'] . '-' . $stock_vel])) {
- $hotel_info['stock_type_arr'][$stock_key] = $stock_list_arr[$hotel_info['run_date'] . '-' . $stock_vel];
- } else {
- unset($hotel_info['stock_type_arr'][$stock_key]);
- }
- }
-
- $result['code'] = '0';
- $result['info'] = '获取酒店房型成本库存成功';
- $result['list'] = $hotel_info;
- return $result;
- }
-
- /**
- * Function Description:获取特定日期产品价格和库存
- * Function Name: getProdPrice
- * @param $run_date
- * @param $to_org_id
- * @param $sub_prod_id
- *
- * @return array
- *
- * @author 冒炎
- */
- public function getProdPrice($run_date, $to_org_id, $sub_prod_id)
- {
- $prod = new OperaProduct();
- $product = $prod->getProdInfo($run_date, $to_org_id, $sub_prod_id, true);
- if (!$product) {
- return [
- 'code' => '2',
- 'info' => '产品不存在!'
- ];
- } else {
- //如果产品下架,返回库存为0
- if ($product['prod_is_onsale'] == 0 || $product['run_is_onsale'] == 0) {
- $data['cus_price'] = $product['cus_price'];
- $data['prod_price'] = $product['prod_price'];
- $data['total_count'] = '0';
- return [
- 'code' => '0',
- 'info' => '获取产品数据成功!',
- 'list' => $data
- ];
-
- } else {//如果没有价格,返回通用渠道的价格和库存
- if (!$product['cus_price']) {
- $ac_product = $prod->getProdInfo($run_date, $to_org_id, $sub_prod_id, false);
- //如果通用渠道的产品也没有价格,返回000
- if (!$ac_product['cus_price']) {
- $data['cus_price'] = '0';
- $data['prod_price'] = '0';
- $data['total_count'] = '0';
- return [
- 'code' => '0',
- 'info' => '获取产品数据成功!',
- 'list' => $data
- ];
- } else {
- return [
- 'code' => '0',
- 'info' => '获取产品数据成功!',
- 'list' => $ac_product
- ];
- }
- } else {
- return [
- 'code' => '0',
- 'info' => '获取产品数据成功!',
- 'list' => $product
- ];
- }
-
- }
- }
- }
-
- }
|