|
- <?php
-
- namespace backend\modules\zzcs\models;
-
- use Yii;
- use yii\db\Expression;
-
- /**
- * This is the model class for table "group_order_cost".
- *
- * @property integer $id
- * @property integer $order_title_id
- * @property integer $res_type
- * @property string $res_name
- * @property string $res_cost
- * @property string $sale_price
- * @property string $profit
- * @property string $profit_margin
- * @property string $create_time
- * @property string $update_time
- * @property integer $cancel_flag
- * @property integer $create_user_id
- */
- class GroupOrderCost extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'group_order_cost';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['order_title_id', 'res_type', 'cancel_flag', 'create_user_id','order_id'], 'integer'],
- [['res_cost', 'sale_price', 'profit'], 'number'],
- [['res_name', 'profit_margin'], 'string', 'max' => 50],
- [['create_time', 'update_time','run_date'], 'string', 'max' => 20],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'order_title_id' => 'Order Title ID',
- 'res_type' => 'Res Type',
- 'res_cost' => 'Res Cost',
- 'res_name' => 'Res Name',
- 'sale_price' => 'Sale Price',
- 'profit' => 'Profit',
- 'profit_margin' => 'Profit Margin',
- 'create_time' => 'Create Time',
- 'update_time' => 'Update Time',
- 'cancel_flag' => 'Cancel Flag',
- 'create_user_id' => 'Create User ID',
- 'run_date' => 'Run Date',
- 'order_id' => 'Order Id',
- ];
- }
-
- /**
- * Function Description:插入数据
- * Function Name: IstGroupCost
- * @param $values
- *
- * @return bool
- *
- * @author 娄梦宁
- */
- public function IstGroupCost($values){
- $transaction=Yii::$app->db->beginTransaction();
- $tmp_group=clone $this;
- $tmp_group->attributes=$values;
- $res=$tmp_group->insert();
- if(!$res){
- $transaction->rollBack();
- return false;
- }
- $transaction->commit();
- return true;
- }
- /*
- * 获取组合订单成本数据
- */
- public function getInfo($order_title_id){
- $result=self::find()->select(['res_type','order_title_id','res_name','res_cost','sale_price','profit','profit_margin','order_id'])
- ->from(self::tableName())
- ->where(['and',['=','order_title_id',$order_title_id],['=','cancel_flag',0]])
- ->orderBy('res_type,id')
- ->asArray()
- ->all();
- $count=self::find()->select(['count(1) as cnt','sum(res_cost) as cost','sum(sale_price) as sale_price'])->from(self::tableName())
- ->where(['and',['=','order_title_id',$order_title_id],['=','cancel_flag',0],['=','res_type',1]])->asArray()->one();
- return ['res_list'=>$result,
- 'bus'=>$count];
- }
- /*
- * 更新巴士资源成本信息
- */
- public function uptBus($value,$where){
- self::updateAll($value,$where);
- }
- /*
- * 获取组合订单成本数据
- */
- public function getInfoResult($order_title_id){
- $result=self::find()->select(['res_type','order_title_id','res_name','res_cost','sale_price','profit','profit_margin','order_id','run_date'])
- ->from(self::tableName())
- ->where(['and',['=','order_title_id',$order_title_id],['=','cancel_flag',0]])
- ->orderBy('res_type,id')
- ->asArray()
- ->all();
- //处理结果集
- $new_arr=[];
- foreach($result as $key=>$val){
- if($val['res_type']==1){
- $new_arr['bus_arr'][$val['order_id']]=$result[$key];
- }elseif($val['res_type']==3){
- $new_arr['ticket_arr'][$val['order_id']]=$result[$key];
- }elseif($val['res_type']==2){
- $new_arr['hotel_arr'][$val['order_id']][$val['run_date']]=$result[$key];
- }else{
- $new_arr['total']=$result[$key];
- }
- }
- return $new_arr;
- }
- /*
- * 巴士资源取消
- */
- public function CancelBus($order_id,$order_title_id){
- self::updateAll(['cancel_flag'=>1],['order_id'=>$order_id,'order_title_id'=>$order_title_id,'res_type'=>1]);
- }
- /*
- * 巴士资源本来有,被取消了,。。。然后又加入了。。所用的接口
- */
- public function Cancel0Bus($order_id,$order_title_id){
- self::updateAll(['cancel_flag'=>0],['order_id'=>$order_id,'order_title_id'=>$order_title_id,'res_type'=>1]);
- }
- /*
- * 根据组合订单号获取门票产品总销售价
- */
- public function getSumTicketSalePrice($order_title_id){
- $result=self::find()->select('sum(sale_price) as price,count(1) as cnt,sum(res_cost) as res_cost')
- ->from(self::tableName())
- ->where(['and',['=','cancel_flag',0],['=','res_type',3],['=','order_title_id',$order_title_id]])
- ->asArray()
- ->one();
- return $result;
- }
- /*
- * 判断是否有酒店子资源(并判断酒店是否有销售价--2017-12-25 娄梦宁)
- */
- public function getSumHotelSalePrice($order_title_id){
- $result=self::find()->select('sum(res_cost) as price,count(1) as cnt')
- ->from(self::tableName())
- ->where(['and',['=','cancel_flag',0],['=','res_type',2],['=','order_title_id',$order_title_id],['!=','sale_price',0]])
- ->asArray()
- ->one();
- return $result;
- }
- /*
- * 获取酒店子资源详细信息
- */
- public function getHotelSon($order_title_id){
- $result=self::find()->select('res_cost,run_date,order_id')
- ->from(self::tableName())
- ->where(['and',['=','cancel_flag',0],['=','res_type',2],['=','order_title_id',$order_title_id]])
- ->asArray()
- ->all();
- return $result;
- }
- /*
- * 获取门票产品子资源数组
- */
- public function getTicketSonArr($order_title_id){
- $result=self::find()->select('res_cost,order_id')
- ->from(self::tableName())
- ->where(['and',['=','cancel_flag',0],['=','res_type',3],['=','order_title_id',$order_title_id]])
- ->asArray()
- ->all();
- return $result;
- }
-
- /*
- * 插入新组合订单前删除组合订单的财务信息
- */
- public function cancelOrderTitle($order_title_id){
- self::updateAll(['cancel_flag'=>1],['order_title_id'=>$order_title_id]);
- }
-
- /**
- * Function Description:组合订单酒店子资源价格变化时修改酒店订单的销售价,利润,佣金,支付信息,记录日志
- * Function Name: upt_hotel_info
- * @param $order_id
- * @param $order_price
- *
- *
- * @author 娄梦宁
- */
- public function upt_hotel_info($order_id,$order_price){
- //查出用户
- $cookies = Yii::$app->request->cookies;
- $user_id = $cookies->getValue('user_id', -1);
- $order_main =new OrderMain();
- $base_supploer_sale=new BaseSupplierSale();
- //取出酒店订单相关信息
- $select=[
- 'order_price',
- 'base_price',
- 'cnt'=>new Expression('substring_index(order_description,"|",-1)'),
- 'outside_sale_org_id',
- 'parent_prod_id',
- 'prod_id'
- ];
- $order_info=$order_main::find()->select($select)
- ->from('order_main')
- ->where(['and',['=','cancel_flag',0],['=','order_id',$order_id]])
- ->asArray()
- ->one();
- $parent_upt_data=[
- 'update_user_id'=>$user_id,
- 'update_time'=>date('Y-m-d H:i:s')
- ];
- $son_upt_data=[
- 'update_user_id'=>$user_id,
- 'update_time'=>date('Y-m-d H:i:s')
- ];
- //1.计算佣金,如果佣金规则是按照售价百分比来算就修改
- $commission_rule=$base_supploer_sale::find()->select('sale_type,commision_flag,commision_type,back_commision_type,back_commision_method,back_percent,back_value')
- ->from('base_supplier_sale')
- ->where(['and',['=','parent_type',25],['=','cancel_flag',0],['=','prod_supplier_id',$order_info['outside_sale_org_id']]])
- ->asArray()
- ->one();
- $percentage=$order_price/$order_info['order_price'];
- //佣金规则
- if($commission_rule['back_commision_method']==308){//按百分比算
- $parent_commission=(int)$commission_rule['back_percent']*0.01*$order_price;
- $son_commission=round($parent_commission/$order_info['cnt'],2);
- $parent_upt_data['total_commission']=$parent_commission;
- $son_upt_data['total_commission']=$son_commission;
- }
- //2.计算价格修改数据
- $parent_upt_data['order_price']=$order_price;
- $son_upt_data['order_price']=new Expression('order_price*'.$percentage);
- //3.利润字段修改
- $parent_upt_data['profit_value']=$order_price-(int)$order_info['base_price'];
- $son_upt_data['profit_value']=new Expression('order_price-base_price');
- //4.修改主订单
- $order_main::updateAll($parent_upt_data,['order_id'=>$order_id]);
- //5.修改子订单
- $order_main::updateAll($son_upt_data,['parent_order_id'=>$order_id]);
- //6.修改pay_main 和 pay_detail
- $order_pay_main=new OrderPayMain();
- $order_pay_detail=new OrderPayDetail();
- $order_pay_main::updateAll(['pay_total'=>$order_price,'update_user_id'=>$user_id],['and',['=','cancel_flag',0],['=','order_id',$order_id]]);
- $pay_main_id=$order_pay_main::find()->select('id')->from('order_pay_main')->where(['and',['=','cancel_flag',0],['=','order_id',$order_id]])
- ->asArray()->one();
- $order_pay_detail::updateAll(['pay_money'=>$order_price,'update_user_id'=>$user_id],['and',['=','cancel_flag',0],['=','pay_main_id',$pay_main_id['id']]]);
-
- //7:插入操作日志
- $opera_hotel_log=new OperaHotelLog();
- $parent_log = [
- 'CREATE_USER_ID' => $user_id,
- 'CREATE_TIME' => date('Y-m-d H:i:s'),
- 'LOG_TYPE' => 3,
- 'HOTEL_ID' => $order_info['parent_prod_id'],
- 'PARENT_ROOM_TYPE' => 0,
- 'ROOM_TYPE' => $order_info['prod_id'],
- 'LOG_DESC' => '组合订单:修改订单号为' . $order_id . '的订单,修改价格为:' . $order_price,
- 'ORDER_ID' => $order_id,
- ];
- $opera_hotel_log->attributes=$parent_log;
- $opera_hotel_log->insert();
- }
-
- /**
- * Function Description:组合订单门票价格变动相关改动
- * Function Name: upt_ticket_info
- * @param $order_id
- * @param $order_price
- *
- *
- * @author 娄梦宁
- */
- public function upt_ticket_info($order_id,$order_price){
- //查出用户
- $cookies = Yii::$app->request->cookies;
- $user_id = $cookies->getValue('user_id', -1);
- $order_main =new OrderMain();
- //取出酒店订单相关信息
- $select=[
- 'order_price',
- 'base_price',
- 'cnt'=>new Expression('substring_index(order_description,"|",-1)'),
- 'prod_id'
- ];
- $order_info=$order_main::find()->select($select)
- ->from('order_main')
- ->where(['and',['=','cancel_flag',0],['=','order_id',$order_id]])
- ->asArray()
- ->one();
- $parent_upt_data=[
- 'order_price'=>$order_price,
- 'profit_value'=>$order_price-(int)$order_info['base_price'],
- 'update_user_id'=>$user_id,
- 'update_time'=>date('Y-m-d H:i:s')
- ];
- if($order_info['order_price']!=0){
- $percentage=$order_price/$order_info['order_price'];
- }else{
- $percentage=0;
- }
-
- $son_upt_data=[
- 'order_price'=>new Expression('order_price*'.$percentage),
- 'profit_value'=>new Expression('order_price - base_price'),
- 'update_user_id'=>$user_id,
- 'update_time'=>date('Y-m-d H:i:s')
- ];
- $order_main::updateAll($parent_upt_data,['order_id'=>$order_id]);
-
- $order_main::updateAll($son_upt_data,['parent_order_id'=>$order_id]);
- }
-
- }
|