|
- <?php
-
- namespace backend\modules\hotel\models;
-
- use backend\modules\hotel\models\Ali\TopLogger;
- use backend\modules\zzcs\models\BaseUser;
- use Yii;
-
- /**
- * This is the model class for table "order_invoice_info".
- *
- * @property integer $ID
- * @property integer $CreateUseId
- * @property string $CreateTime
- * @property integer $ChannelId
- * @property integer $UpdateUserId
- * @property string $UpdateTime
- * @property integer $NeedInvoice
- * @property integer $EarllyPrepare
- * @property integer $PostType
- * @property integer $InvoiceType
- * @property string $Comment
- * @property string $InvoiceTitle
- * @property string $InvoiceMoney
- * @property string $CompanyTel
- * @property string $CompanyTax
- * @property string $ReceiverAddress
- * @property string $ReceiverName
- * @property string $ReceiverMobile
- * @property string $RegisterAddress
- * @property string $Bank
- * @property string $BankAccount
- * @property integer $OrderID
- * @property integer $Status
- * @property string $OpenTicketTime
- * @property integer $OpenTicketUserId
- * @property string $InvoiceNum
- * @property string $CourierNum
- * @property string $PostTime
- * @property integer $PostUserId
- * @property string $PostCompany
- * @property string $RejectReason
- * @property string $ApplyUser
- * @property string $Remark
- */
- class OrderInvoiceInfo extends \yii\db\ActiveRecord
- {
- CONST ORDER_WAITTING_APPLY = 1;
- CONST ORDER_APPLYYING = 2;
- CONST ORDER_TICKET_OPENED = 3;
- CONST ORDER_POSTED = 4;
- CONST ORDER_REFUSE = 5;
-
- CONST ORDER_STATUS_DESC = [
- 1 => '待申请',
- 2 => '申请中',
- 3 => '已开票',
- 4 => '已邮寄',
- 5 => '已驳回',
- ];
- // 快递方式
- CONST IN_SEND_TYPE = [
- 1 => '顺丰到付',
- 2 => '顺丰寄付',
- 3 => '圆通寄付',
- ];
-
- const NEEDINVOICE = array(0 => '不需要', 1 => '需要');
- const EARLLYPREPARE = array(0 => '不需要', 1 => '需要');
- const POSTTYPE = array(1 => '前台自取', 2 => '邮寄');
- const INVOICETYPE = array(1 => '增值税普通发票', 2 => '增值税专用发票');
-
-
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'order_invoice_info';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['OrderID', 'InvoiceType', 'ChannelId', 'NeedInvoice', 'PostType', 'InvoiceMoney'], 'required'],
- [['CreateUseId', 'ChannelId', 'UpdateUserId', 'NeedInvoice', 'EarllyPrepare', 'PostType', 'InvoiceType', 'OrderID', 'Status', 'OpenTicketUserId', 'ApplyUser', 'PostUserId'], 'integer'],
- [['UpdateTime'], 'safe'],
- [['CreateTime', 'InvoiceMoney', 'ReceiverName', 'BankAccount', 'OpenTicketTime', 'CourierNum', 'PostTime'], 'string', 'max' => 50],
- [['Comment', 'InvoiceTitle', 'RegisterAddress'], 'string', 'max' => 255],
- [['CompanyTel', 'CompanyTax', 'ReceiverMobile', 'Bank', 'PostCompany'], 'string', 'max' => 100],
- [['ReceiverAddress', 'RejectReason'], 'string', 'max' => 300],
- [['Remark'], 'string', 'max' => 500],
- [['InvoiceNum'], 'string', 'max' => 20],
- [['OrderID'], 'unique'],
- [['Comment', 'ReceiverName', 'ReceiverMobile', 'ReceiverAddress', 'CompanyTax'], 'required', 'on' => ['addInvoice']],
- [['Comment', 'ApplyUser', 'ReceiverName', 'ReceiverMobile', 'ReceiverAddress', 'PostCompany', 'CompanyTax'], 'required', 'on' => ['showInvoice']],
- [['Comment', 'ReceiverName', 'ReceiverMobile', 'ReceiverAddress', 'CompanyTax'], 'required', 'on' => ['exclusive']],
- [['CompanyTel'], 'required','on'=>'exclusive','message'=>'公司电话不能为空'],
- [['RegisterAddress'], 'required','on'=>'exclusive','message'=>'公司地址不能为空'],
- [['Bank'], 'required','on'=>'exclusive','message'=>'开户行不能为空'],
- [['BankAccount'], 'required','on'=>'exclusive','message'=>'银行账号不能为空'],
- ];
- }
-
- public function scenarios()
- {
-
- return array_merge(parent::scenarios(), [
- 'showInvoice' => ['InvoiceTitle', 'OrderID', 'InvoiceType', 'ChannelId', 'NeedInvoice', 'PostType', 'InvoiceMoney', 'CompanyTax', 'Comment', 'ApplyUser', 'ReceiverName', 'ReceiverMobile', 'ReceiverAddress', 'PostCompany'],
- 'addInvoice' => ['InvoiceTitle', 'OrderID', 'InvoiceType', 'ChannelId', 'NeedInvoice', 'PostType', 'InvoiceMoney', 'CompanyTax', 'Comment', 'ReceiverName', 'ReceiverMobile', 'ReceiverAddress', 'PostCompany'],
- 'exclusive' => ['InvoiceTitle', 'OrderID', 'InvoiceType', 'ChannelId', 'NeedInvoice', 'PostType', 'InvoiceMoney', 'CompanyTax', 'CompanyTel', 'RegisterAddress', 'Bank', 'BankAccount', 'Comment', 'ReceiverName', 'ReceiverMobile', 'ReceiverAddress', 'PostCompany'],
- ]);
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'ID' => Yii::t('app', 'ID'),
- 'CreateUseId' => Yii::t('app', 'Create Use ID'),
- 'CreateTime' => Yii::t('app', '申请日期'),
- 'ChannelId' => Yii::t('app', '渠道'),
- 'UpdateUserId' => Yii::t('app', 'Update User ID'),
- 'UpdateTime' => Yii::t('app', 'Update Time'),
- 'NeedInvoice' => Yii::t('app', '发票需求'),//是否要开发票 0不需要,1需要
- 'EarllyPrepare' => Yii::t('app', '是否要提前开发票 0不需要,1需要'),
- 'PostType' => Yii::t('app', '领取方式'),// 1:前台自取 2:邮寄
- 'InvoiceType' => Yii::t('app', '发票类型'),// 1:增值税普通发票;2:增值税专用发票
- 'Comment' => Yii::t('app', '开票项目'),//(例如:代订房费、住宿费、会议费、旅游费等)
- 'InvoiceTitle' => Yii::t('app', '发票抬头'),
- 'InvoiceMoney' => Yii::t('app', '开票金额'),
- 'CompanyTel' => Yii::t('app', '公司电话'),
- 'CompanyTax' => Yii::t('app', '公司税号'),
- 'ReceiverAddress' => Yii::t('app', '邮寄地址'),
- 'ReceiverName' => Yii::t('app', '收件人'),
- 'ReceiverMobile' => Yii::t('app', '联系电话'),
- 'RegisterAddress' => Yii::t('app', '公司注册地址 (增值税专用发票使用)'),
- 'Bank' => Yii::t('app', '开户行'),
- 'BankAccount' => Yii::t('app', '银行账号'),
- 'OrderID' => Yii::t('app', '关联订单'),
- 'Status' => Yii::t('app', '0 待开 1 已开'),
- 'OpenTicketTime' => Yii::t('app', '开票时间'),
- 'OpenTicketUserId' => Yii::t('app', '发票开具处理人'),
- 'InvoiceNum' => Yii::t('app', '发票编号'),
- 'CourierNum' => Yii::t('app', '快递单号'),
- 'PostTime' => Yii::t('app', '邮寄时间'),
- 'PostCompany' => Yii::t('app', '快递公司'),
- 'ApplyUser' => Yii::t('app', '申请人'),
- 'Remark' => Yii::t('app', '备注'),
- ];
- }
-
- public function getBaseSupplier()
- {
- return $this->hasOne(BaseSupplier::className(), ['ID' => 'ChannelId']);
- }
-
-
- /**
- * @Author wanglg
- * @Desc 获取发票申请人相关信息
- * @return \yii\db\ActiveQuery
- */
- public function getBaseUser()
- {
- return $this->hasOne(BaseUser::className(), ['ID' => 'ApplyUser']);
- }
-
- /**
- * @Author wanglg
- * @Desc 关联订单表
- * @return
- */
- public function getOrderMain()
- {
- return $this->hasOne(OrderMain::className(), ['ORDER_ID' => 'OrderID'])
- ->andOnCondition(['order_main.PARENT_ORDER_ID' => 0, 'order_main.CANCEL_FLAG' => 0, 'order_main.ORDER_PROD_TYPE' => 25]);
- // ->andOnCondition(['order_main.PARENT_ORDER_ID' => 0, 'order_main.ORDER_VALID_STATUS' => 1, 'order_main.ORDER_STATUS' => [313, 198, 314, 382, 147], 'order_main.ORDER_LEVEL' => [0, 1], 'order_main.CANCEL_FLAG' => 0, 'order_main.ORDER_PROD_TYPE' => 25]);
- }
-
- /**
- * @Author wanglg
- * @Desc 获取订单的发票信息
- * @param $order_id
- * @return array|\yii\db\ActiveRecord[]
- */
- public function getInvoiceInfo($order_id)
- {
- $invoice = OrderInvoiceInfo::find()
- ->select(['a.OrderID', 'a.CreateTime', 'a.NeedInvoice', 'a.EarllyPrepare', 'a.PostType',
- 'a.InvoiceType', 'a.Status', 'a.Comment', 'a.InvoiceTitle', 'a.CompanyTel', 'a.CompanyTax', 'a.ReceiverAddress',
- 'a.ReceiverName', 'a.ReceiverMobile', 'a.RegisterAddress', 'a.Bank', 'a.BankAccount', 'b.TRUE_NAME'])
- ->leftJoin('base_user b', 'b.ID=a.CreateUseId')
- ->from('order_invoice_info as a')
- ->where(['a.OrderID' => $order_id])
- ->asArray()->all();
- return $invoice;
- }
-
- public function beforeSave($insert)
- {
- if ($this->isNewRecord) {
- $this-> CreateTime = date('Y-m-d H:i:s', time());
- } else {
- $this->UpdateUserId = Yii::$app->user->id;
- $this->UpdateTime = date('Y-m-d H:i:s', time());;
- }
-
- return parent::beforeSave($insert);
- }
-
-
- /**
- * Author:Steven
- * Desc:确认开票
- * @return bool
- * @throws \yii\db\Exception
- */
- public function makeInvoice()
- {
- if (Yii::$app->db->transaction)
- $commitFlag = false;
- else {
- $commitFlag = true;
- $tran = Yii::$app->db->beginTransaction();
- }
-
- try {
- //保存日志
- $this->saveLog('makeInvoice');
- $invoice = new InvoiceLog();
- $invoice->CreateTime = date('Y-m-d H:i:s');
- $invoice->CreateUseId = Yii::$app->user->id;
- $invoice->OrderId = $this->OrderID;
- $invoice->LogDesc = '已开票,发票编号:' . $this->InvoiceNum;
- $invoice->save();
- if ($this->save()) {
- if ($commitFlag && isset($tran)) {
- $tran->commit();
- }
- return true;
- } else {
- if ($commitFlag && isset($tran)) {
- $tran->rollBack();
- }
- return false;
- }
- } catch (\Exception $exception) {
- if ($commitFlag && isset($tran)) {
- $tran->rollBack();
- }
- return false;
- }
- }
-
-
- /**
- * Author:Steven
- * Desc:邮寄发票
- * @return bool
- * @throws \yii\db\Exception
- */
- public function postInvoice()
- {
- $logger = new TopLogger();
- if (Yii::$app->db->transaction)
- $commitFlag = false;
- else {
- $commitFlag = true;
- $tran = Yii::$app->db->beginTransaction();
- }
-
- try {
- //保存日志
- $hotel_log = $this->saveLog('postInvoice');
- $invoice = new InvoiceLog();
- $invoice->CreateTime = date('Y-m-d H:i:s');
- $invoice->CreateUseId = Yii::$app->user->id;
- $invoice->OrderId = $this->OrderID;
- $invoice->LogDesc = '发票已邮寄,快递公司:' . self::IN_SEND_TYPE[$this->PostCompany] . ',快递单号:' . $this->CourierNum;
- $log_res = $invoice->save();
- $logger -> log([date("Y-m-d H:i:s"), '发票日志保存结果:' .$log_res . ',事务开启:' . $commitFlag . ',酒店日志:' . $hotel_log ."\n" . PHP_EOL], 'ali/error');
- if ($this->save()) {
- if ($commitFlag && isset($tran)) {
- $tran->commit();
- }
- return true;
- } else {
- if ($commitFlag && isset($tran)) {
- $tran->rollBack();
- }
- return false;
- }
- } catch (\Exception $exception) {
- $logger -> log([date("Y-m-d H:i:s"), '错误异常:' . $exception ."\n" . PHP_EOL], 'ali/error');
- if ($commitFlag && isset($tran)) {
- $tran->rollBack();
- }
- return false;
- }
- }
-
- /**
- * Author:Steven
- * Desc:发票驳回
- * @return bool
- * @throws \yii\db\Exception
- */
- public function refuse()
- {
- if (Yii::$app->db->transaction)
- $commitFlag = false;
- else {
- $commitFlag = true;
- $tran = Yii::$app->db->beginTransaction();
- }
-
- try {
- //保存日志
- $this->saveLog('refuse');
- $invoice = new InvoiceLog();
- $invoice->CreateTime = date('Y-m-d H:i:s');
- $invoice->CreateUseId = Yii::$app->user->id;
- $invoice->OrderId = $this->OrderID;
- $invoice->LogDesc = '发票已驳回,驳回原因:' . $this->RejectReason;
- $invoice->save();
- if ($this->save()) {
- if ($commitFlag && isset($tran)) {
- $tran->commit();
- }
- return true;
- } else {
- if ($commitFlag && isset($tran)) {
- $tran->rollBack();
- }
- return false;
- }
- } catch (\Exception $exception) {
- if ($commitFlag && isset($tran)) {
- $tran->rollBack();
- }
- return false;
- }
- }
-
- /**
- * @Author wanglg
- * @Desc 添加发票信息
- * @return bool
- */
- public function addInvoice($msg)
- {
- if (Yii::$app->db->transaction)
- $commitFlag = false;
- else {
- $commitFlag = true;
- $tran = Yii::$app->db->beginTransaction();
- }
-
- try {
- //保存日志
- $this->saveLog('addInvoice', $msg);
- $invoice = new InvoiceLog();
- $invoice->CreateTime = date('Y-m-d H:i:s');
- $invoice->CreateUseId = Yii::$app->user->id;
- $invoice->OrderId = $this->OrderID;
- $invoice->LogDesc = $msg;
- $invoice->save();
- if ($this->save()) {
- if ($commitFlag && isset($tran)) {
- $tran->commit();
- }
- return true;
- } else {
- if ($commitFlag && isset($tran)) {
- $tran->rollBack();
- }
- return false;
- }
- } catch (\Exception $exception) {
- if ($commitFlag && isset($tran)) {
- $tran->rollBack();
- }
- return false;
- }
- }
-
- /**
- * @Author wanglg
- * @Desc 申请开票
- * @return bool
- */
- public function applyInvoice()
-
- {
- if (Yii::$app->db->transaction)
- $commitFlag = false;
- else {
- $commitFlag = true;
- $tran = Yii::$app->db->beginTransaction();
- }
-
- try {
- //保存日志
- $this->saveLog('applyInvoice');
- $invoice = new InvoiceLog();
- $invoice->CreateTime = date('Y-m-d H:i:s');
- $invoice->CreateUseId = Yii::$app->user->id;
- $invoice->OrderId = $this->OrderID;
- $invoice->LogDesc = '申请开票,开票订单:'.$this->OrderID;
- $invoice->save();
- if ($this->save()) {
- if ($commitFlag && isset($tran)) {
- $tran->commit();
- }
- return true;
- } else {
- if ($commitFlag && isset($tran)) {
- $tran->rollBack();
- }
- return false;
- }
- } catch (\Exception $exception) {
- if ($commitFlag && isset($tran)) {
- $tran->rollBack();
- }
- return false;
- }
- }
-
- /**
- * Author:Steven
- * Desc:保存日志
- * @param $type
- * @param string $msg
- */
- public function saveLog($type, $msg = '')
- {
- //写订单日志、状态日志
- //订单表没有基础房型id,需要查一下
- $res_order = OrderMain::find()->leftJoin('opera_hotel_room', 'order_main.PARENT_PROD_ID=opera_hotel_room.HOTEL_ID and order_main.PROD_ID=opera_hotel_room.ROOM_TYPE and opera_hotel_room.CANCEL_FLAG=0')
- ->select(['ORDER_ID', 'PROD_ID', 'PARENT_PROD_ID', 'opera_hotel_room.PARENT_ROOM_TYPE'])->where([
- 'ORDER_ID' => $this->OrderID,
- 'ORDER_PROD_TYPE' => OrderMain::ORDER_PROD_TYPE_MAIN,
- 'PARENT_ORDER_ID' => 0,
- 'order_main.CANCEL_FLAG' => 0
- ])->limit(1)->asArray()->one();
-
- // $baseRoom = OperaHotelRoom::findOne(['CANCEL_FLAG' => 0, 'HOTEL_ID' => $this->PARENT_PROD_ID, 'ROOM_TYPE' => $this->PROD_ID]);
- $orderLog = new OperaHotelLog();
- $orderLog->CREATE_USER_ID = Yii::$app->user->id;
- $orderLog->HOTEL_ID = $res_order['PARENT_PROD_ID'];
- $orderLog->LOG_TYPE = OperaHotelLog::LOG_TYPE_ORDER;
- $orderLog->ROOM_TYPE = $res_order['PROD_ID'];
- $orderLog->PARENT_ROOM_TYPE = $res_order['PARENT_ROOM_TYPE'];
- switch ($type) {
- case 'makeInvoice': //开票
- $orderLog->LOG_DESC = "已开票,发票单号:" . $this->InvoiceNum;
- break;
- case 'postInvoice': //邮寄发票
- $orderLog->LOG_DESC = '发票已邮寄,快递公司:' . self::IN_SEND_TYPE[$this->PostCompany] . ',快递单号:' . $this->CourierNum;
- break;
- case 'refuse': //驳回发票
- $orderLog->LOG_DESC = '发票已驳回,驳回原因:' . $this->RejectReason;
- break;
- case 'applyInvoice':
- $orderLog->LOG_DESC = '申请开票,发票申请中';
- break;
- case 'addInvoice':
- $orderLog->LOG_DESC = $msg . ',关联订单号:'.$this->OrderID;
- break;
- default:
- $orderLog->LOG_DESC = '';
- }
- $orderLog->ORDER_ID = $this->OrderID;
- $orderLog->save();
- }
-
- public function startWord()
- {
- ob_start();
- echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"
- xmlns:w="urn:schemas-microsoft-com:office:word"
- xmlns="http://www.w3.org/TR/REC-html40">';
- }
-
- public function saveWord($path)
- {
- echo "</html>";
- $data = ob_get_contents();
- $this->wirtefile ($path,$data);
- }
-
- public function wirteFile ($fn,$data)
- {
- $fp=@fopen($fn,"wb",0777);
- @fwrite($fp,$data,0777);
- @fclose($fp);
- }
- }
|