You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

506 lines
18 KiB

  1. <?php
  2. namespace backend\modules\hotel\models;
  3. use backend\modules\hotel\models\Ali\TopLogger;
  4. use backend\modules\zzcs\models\BaseUser;
  5. use Yii;
  6. /**
  7. * This is the model class for table "order_invoice_info".
  8. *
  9. * @property integer $ID
  10. * @property integer $CreateUseId
  11. * @property string $CreateTime
  12. * @property integer $ChannelId
  13. * @property integer $UpdateUserId
  14. * @property string $UpdateTime
  15. * @property integer $NeedInvoice
  16. * @property integer $EarllyPrepare
  17. * @property integer $PostType
  18. * @property integer $InvoiceType
  19. * @property string $Comment
  20. * @property string $InvoiceTitle
  21. * @property string $InvoiceMoney
  22. * @property string $CompanyTel
  23. * @property string $CompanyTax
  24. * @property string $ReceiverAddress
  25. * @property string $ReceiverName
  26. * @property string $ReceiverMobile
  27. * @property string $RegisterAddress
  28. * @property string $Bank
  29. * @property string $BankAccount
  30. * @property integer $OrderID
  31. * @property integer $Status
  32. * @property string $OpenTicketTime
  33. * @property integer $OpenTicketUserId
  34. * @property string $InvoiceNum
  35. * @property string $CourierNum
  36. * @property string $PostTime
  37. * @property integer $PostUserId
  38. * @property string $PostCompany
  39. * @property string $RejectReason
  40. * @property string $ApplyUser
  41. * @property string $Remark
  42. */
  43. class OrderInvoiceInfo extends \yii\db\ActiveRecord
  44. {
  45. CONST ORDER_WAITTING_APPLY = 1;
  46. CONST ORDER_APPLYYING = 2;
  47. CONST ORDER_TICKET_OPENED = 3;
  48. CONST ORDER_POSTED = 4;
  49. CONST ORDER_REFUSE = 5;
  50. CONST ORDER_STATUS_DESC = [
  51. 1 => '待申请',
  52. 2 => '申请中',
  53. 3 => '已开票',
  54. 4 => '已邮寄',
  55. 5 => '已驳回',
  56. ];
  57. // 快递方式
  58. CONST IN_SEND_TYPE = [
  59. 1 => '顺丰到付',
  60. 2 => '顺丰寄付',
  61. 3 => '圆通寄付',
  62. ];
  63. const NEEDINVOICE = array(0 => '不需要', 1 => '需要');
  64. const EARLLYPREPARE = array(0 => '不需要', 1 => '需要');
  65. const POSTTYPE = array(1 => '前台自取', 2 => '邮寄');
  66. const INVOICETYPE = array(1 => '增值税普通发票', 2 => '增值税专用发票');
  67. /**
  68. * @inheritdoc
  69. */
  70. public static function tableName()
  71. {
  72. return 'order_invoice_info';
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function rules()
  78. {
  79. return [
  80. [['OrderID', 'InvoiceType', 'ChannelId', 'NeedInvoice', 'PostType', 'InvoiceMoney'], 'required'],
  81. [['CreateUseId', 'ChannelId', 'UpdateUserId', 'NeedInvoice', 'EarllyPrepare', 'PostType', 'InvoiceType', 'OrderID', 'Status', 'OpenTicketUserId', 'ApplyUser', 'PostUserId'], 'integer'],
  82. [['UpdateTime'], 'safe'],
  83. [['CreateTime', 'InvoiceMoney', 'ReceiverName', 'BankAccount', 'OpenTicketTime', 'CourierNum', 'PostTime'], 'string', 'max' => 50],
  84. [['Comment', 'InvoiceTitle', 'RegisterAddress'], 'string', 'max' => 255],
  85. [['CompanyTel', 'CompanyTax', 'ReceiverMobile', 'Bank', 'PostCompany'], 'string', 'max' => 100],
  86. [['ReceiverAddress', 'RejectReason'], 'string', 'max' => 300],
  87. [['Remark'], 'string', 'max' => 500],
  88. [['InvoiceNum'], 'string', 'max' => 20],
  89. [['OrderID'], 'unique'],
  90. [['Comment', 'ReceiverName', 'ReceiverMobile', 'ReceiverAddress', 'CompanyTax'], 'required', 'on' => ['addInvoice']],
  91. [['Comment', 'ApplyUser', 'ReceiverName', 'ReceiverMobile', 'ReceiverAddress', 'PostCompany', 'CompanyTax'], 'required', 'on' => ['showInvoice']],
  92. [['Comment', 'ReceiverName', 'ReceiverMobile', 'ReceiverAddress', 'CompanyTax'], 'required', 'on' => ['exclusive']],
  93. [['CompanyTel'], 'required','on'=>'exclusive','message'=>'公司电话不能为空'],
  94. [['RegisterAddress'], 'required','on'=>'exclusive','message'=>'公司地址不能为空'],
  95. [['Bank'], 'required','on'=>'exclusive','message'=>'开户行不能为空'],
  96. [['BankAccount'], 'required','on'=>'exclusive','message'=>'银行账号不能为空'],
  97. ];
  98. }
  99. public function scenarios()
  100. {
  101. return array_merge(parent::scenarios(), [
  102. 'showInvoice' => ['InvoiceTitle', 'OrderID', 'InvoiceType', 'ChannelId', 'NeedInvoice', 'PostType', 'InvoiceMoney', 'CompanyTax', 'Comment', 'ApplyUser', 'ReceiverName', 'ReceiverMobile', 'ReceiverAddress', 'PostCompany'],
  103. 'addInvoice' => ['InvoiceTitle', 'OrderID', 'InvoiceType', 'ChannelId', 'NeedInvoice', 'PostType', 'InvoiceMoney', 'CompanyTax', 'Comment', 'ReceiverName', 'ReceiverMobile', 'ReceiverAddress', 'PostCompany'],
  104. 'exclusive' => ['InvoiceTitle', 'OrderID', 'InvoiceType', 'ChannelId', 'NeedInvoice', 'PostType', 'InvoiceMoney', 'CompanyTax', 'CompanyTel', 'RegisterAddress', 'Bank', 'BankAccount', 'Comment', 'ReceiverName', 'ReceiverMobile', 'ReceiverAddress', 'PostCompany'],
  105. ]);
  106. }
  107. /**
  108. * @inheritdoc
  109. */
  110. public function attributeLabels()
  111. {
  112. return [
  113. 'ID' => Yii::t('app', 'ID'),
  114. 'CreateUseId' => Yii::t('app', 'Create Use ID'),
  115. 'CreateTime' => Yii::t('app', '申请日期'),
  116. 'ChannelId' => Yii::t('app', '渠道'),
  117. 'UpdateUserId' => Yii::t('app', 'Update User ID'),
  118. 'UpdateTime' => Yii::t('app', 'Update Time'),
  119. 'NeedInvoice' => Yii::t('app', '发票需求'),//是否要开发票 0不需要,1需要
  120. 'EarllyPrepare' => Yii::t('app', '是否要提前开发票 0不需要,1需要'),
  121. 'PostType' => Yii::t('app', '领取方式'),// 1:前台自取 2:邮寄
  122. 'InvoiceType' => Yii::t('app', '发票类型'),// 1:增值税普通发票;2:增值税专用发票
  123. 'Comment' => Yii::t('app', '开票项目'),//(例如:代订房费、住宿费、会议费、旅游费等)
  124. 'InvoiceTitle' => Yii::t('app', '发票抬头'),
  125. 'InvoiceMoney' => Yii::t('app', '开票金额'),
  126. 'CompanyTel' => Yii::t('app', '公司电话'),
  127. 'CompanyTax' => Yii::t('app', '公司税号'),
  128. 'ReceiverAddress' => Yii::t('app', '邮寄地址'),
  129. 'ReceiverName' => Yii::t('app', '收件人'),
  130. 'ReceiverMobile' => Yii::t('app', '联系电话'),
  131. 'RegisterAddress' => Yii::t('app', '公司注册地址 (增值税专用发票使用)'),
  132. 'Bank' => Yii::t('app', '开户行'),
  133. 'BankAccount' => Yii::t('app', '银行账号'),
  134. 'OrderID' => Yii::t('app', '关联订单'),
  135. 'Status' => Yii::t('app', '0 待开 1 已开'),
  136. 'OpenTicketTime' => Yii::t('app', '开票时间'),
  137. 'OpenTicketUserId' => Yii::t('app', '发票开具处理人'),
  138. 'InvoiceNum' => Yii::t('app', '发票编号'),
  139. 'CourierNum' => Yii::t('app', '快递单号'),
  140. 'PostTime' => Yii::t('app', '邮寄时间'),
  141. 'PostCompany' => Yii::t('app', '快递公司'),
  142. 'ApplyUser' => Yii::t('app', '申请人'),
  143. 'Remark' => Yii::t('app', '备注'),
  144. ];
  145. }
  146. public function getBaseSupplier()
  147. {
  148. return $this->hasOne(BaseSupplier::className(), ['ID' => 'ChannelId']);
  149. }
  150. /**
  151. * @Author wanglg
  152. * @Desc 获取发票申请人相关信息
  153. * @return \yii\db\ActiveQuery
  154. */
  155. public function getBaseUser()
  156. {
  157. return $this->hasOne(BaseUser::className(), ['ID' => 'ApplyUser']);
  158. }
  159. /**
  160. * @Author wanglg
  161. * @Desc 关联订单表
  162. * @return
  163. */
  164. public function getOrderMain()
  165. {
  166. return $this->hasOne(OrderMain::className(), ['ORDER_ID' => 'OrderID'])
  167. ->andOnCondition(['order_main.PARENT_ORDER_ID' => 0, 'order_main.CANCEL_FLAG' => 0, 'order_main.ORDER_PROD_TYPE' => 25]);
  168. // ->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]);
  169. }
  170. /**
  171. * @Author wanglg
  172. * @Desc 获取订单的发票信息
  173. * @param $order_id
  174. * @return array|\yii\db\ActiveRecord[]
  175. */
  176. public function getInvoiceInfo($order_id)
  177. {
  178. $invoice = OrderInvoiceInfo::find()
  179. ->select(['a.OrderID', 'a.CreateTime', 'a.NeedInvoice', 'a.EarllyPrepare', 'a.PostType',
  180. 'a.InvoiceType', 'a.Status', 'a.Comment', 'a.InvoiceTitle', 'a.CompanyTel', 'a.CompanyTax', 'a.ReceiverAddress',
  181. 'a.ReceiverName', 'a.ReceiverMobile', 'a.RegisterAddress', 'a.Bank', 'a.BankAccount', 'b.TRUE_NAME'])
  182. ->leftJoin('base_user b', 'b.ID=a.CreateUseId')
  183. ->from('order_invoice_info as a')
  184. ->where(['a.OrderID' => $order_id])
  185. ->asArray()->all();
  186. return $invoice;
  187. }
  188. public function beforeSave($insert)
  189. {
  190. if ($this->isNewRecord) {
  191. $this-> CreateTime = date('Y-m-d H:i:s', time());
  192. } else {
  193. $this->UpdateUserId = Yii::$app->user->id;
  194. $this->UpdateTime = date('Y-m-d H:i:s', time());;
  195. }
  196. return parent::beforeSave($insert);
  197. }
  198. /**
  199. * Author:Steven
  200. * Desc:确认开票
  201. * @return bool
  202. * @throws \yii\db\Exception
  203. */
  204. public function makeInvoice()
  205. {
  206. if (Yii::$app->db->transaction)
  207. $commitFlag = false;
  208. else {
  209. $commitFlag = true;
  210. $tran = Yii::$app->db->beginTransaction();
  211. }
  212. try {
  213. //保存日志
  214. $this->saveLog('makeInvoice');
  215. $invoice = new InvoiceLog();
  216. $invoice->CreateTime = date('Y-m-d H:i:s');
  217. $invoice->CreateUseId = Yii::$app->user->id;
  218. $invoice->OrderId = $this->OrderID;
  219. $invoice->LogDesc = '已开票,发票编号:' . $this->InvoiceNum;
  220. $invoice->save();
  221. if ($this->save()) {
  222. if ($commitFlag && isset($tran)) {
  223. $tran->commit();
  224. }
  225. return true;
  226. } else {
  227. if ($commitFlag && isset($tran)) {
  228. $tran->rollBack();
  229. }
  230. return false;
  231. }
  232. } catch (\Exception $exception) {
  233. if ($commitFlag && isset($tran)) {
  234. $tran->rollBack();
  235. }
  236. return false;
  237. }
  238. }
  239. /**
  240. * Author:Steven
  241. * Desc:邮寄发票
  242. * @return bool
  243. * @throws \yii\db\Exception
  244. */
  245. public function postInvoice()
  246. {
  247. $logger = new TopLogger();
  248. if (Yii::$app->db->transaction)
  249. $commitFlag = false;
  250. else {
  251. $commitFlag = true;
  252. $tran = Yii::$app->db->beginTransaction();
  253. }
  254. try {
  255. //保存日志
  256. $hotel_log = $this->saveLog('postInvoice');
  257. $invoice = new InvoiceLog();
  258. $invoice->CreateTime = date('Y-m-d H:i:s');
  259. $invoice->CreateUseId = Yii::$app->user->id;
  260. $invoice->OrderId = $this->OrderID;
  261. $invoice->LogDesc = '发票已邮寄,快递公司:' . self::IN_SEND_TYPE[$this->PostCompany] . ',快递单号:' . $this->CourierNum;
  262. $log_res = $invoice->save();
  263. $logger -> log([date("Y-m-d H:i:s"), '发票日志保存结果:' .$log_res . ',事务开启:' . $commitFlag . ',酒店日志:' . $hotel_log ."\n" . PHP_EOL], 'ali/error');
  264. if ($this->save()) {
  265. if ($commitFlag && isset($tran)) {
  266. $tran->commit();
  267. }
  268. return true;
  269. } else {
  270. if ($commitFlag && isset($tran)) {
  271. $tran->rollBack();
  272. }
  273. return false;
  274. }
  275. } catch (\Exception $exception) {
  276. $logger -> log([date("Y-m-d H:i:s"), '错误异常:' . $exception ."\n" . PHP_EOL], 'ali/error');
  277. if ($commitFlag && isset($tran)) {
  278. $tran->rollBack();
  279. }
  280. return false;
  281. }
  282. }
  283. /**
  284. * Author:Steven
  285. * Desc:发票驳回
  286. * @return bool
  287. * @throws \yii\db\Exception
  288. */
  289. public function refuse()
  290. {
  291. if (Yii::$app->db->transaction)
  292. $commitFlag = false;
  293. else {
  294. $commitFlag = true;
  295. $tran = Yii::$app->db->beginTransaction();
  296. }
  297. try {
  298. //保存日志
  299. $this->saveLog('refuse');
  300. $invoice = new InvoiceLog();
  301. $invoice->CreateTime = date('Y-m-d H:i:s');
  302. $invoice->CreateUseId = Yii::$app->user->id;
  303. $invoice->OrderId = $this->OrderID;
  304. $invoice->LogDesc = '发票已驳回,驳回原因:' . $this->RejectReason;
  305. $invoice->save();
  306. if ($this->save()) {
  307. if ($commitFlag && isset($tran)) {
  308. $tran->commit();
  309. }
  310. return true;
  311. } else {
  312. if ($commitFlag && isset($tran)) {
  313. $tran->rollBack();
  314. }
  315. return false;
  316. }
  317. } catch (\Exception $exception) {
  318. if ($commitFlag && isset($tran)) {
  319. $tran->rollBack();
  320. }
  321. return false;
  322. }
  323. }
  324. /**
  325. * @Author wanglg
  326. * @Desc 添加发票信息
  327. * @return bool
  328. */
  329. public function addInvoice($msg)
  330. {
  331. if (Yii::$app->db->transaction)
  332. $commitFlag = false;
  333. else {
  334. $commitFlag = true;
  335. $tran = Yii::$app->db->beginTransaction();
  336. }
  337. try {
  338. //保存日志
  339. $this->saveLog('addInvoice', $msg);
  340. $invoice = new InvoiceLog();
  341. $invoice->CreateTime = date('Y-m-d H:i:s');
  342. $invoice->CreateUseId = Yii::$app->user->id;
  343. $invoice->OrderId = $this->OrderID;
  344. $invoice->LogDesc = $msg;
  345. $invoice->save();
  346. if ($this->save()) {
  347. if ($commitFlag && isset($tran)) {
  348. $tran->commit();
  349. }
  350. return true;
  351. } else {
  352. if ($commitFlag && isset($tran)) {
  353. $tran->rollBack();
  354. }
  355. return false;
  356. }
  357. } catch (\Exception $exception) {
  358. if ($commitFlag && isset($tran)) {
  359. $tran->rollBack();
  360. }
  361. return false;
  362. }
  363. }
  364. /**
  365. * @Author wanglg
  366. * @Desc 申请开票
  367. * @return bool
  368. */
  369. public function applyInvoice()
  370. {
  371. if (Yii::$app->db->transaction)
  372. $commitFlag = false;
  373. else {
  374. $commitFlag = true;
  375. $tran = Yii::$app->db->beginTransaction();
  376. }
  377. try {
  378. //保存日志
  379. $this->saveLog('applyInvoice');
  380. $invoice = new InvoiceLog();
  381. $invoice->CreateTime = date('Y-m-d H:i:s');
  382. $invoice->CreateUseId = Yii::$app->user->id;
  383. $invoice->OrderId = $this->OrderID;
  384. $invoice->LogDesc = '申请开票,开票订单:'.$this->OrderID;
  385. $invoice->save();
  386. if ($this->save()) {
  387. if ($commitFlag && isset($tran)) {
  388. $tran->commit();
  389. }
  390. return true;
  391. } else {
  392. if ($commitFlag && isset($tran)) {
  393. $tran->rollBack();
  394. }
  395. return false;
  396. }
  397. } catch (\Exception $exception) {
  398. if ($commitFlag && isset($tran)) {
  399. $tran->rollBack();
  400. }
  401. return false;
  402. }
  403. }
  404. /**
  405. * Author:Steven
  406. * Desc:保存日志
  407. * @param $type
  408. * @param string $msg
  409. */
  410. public function saveLog($type, $msg = '')
  411. {
  412. //写订单日志、状态日志
  413. //订单表没有基础房型id,需要查一下
  414. $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')
  415. ->select(['ORDER_ID', 'PROD_ID', 'PARENT_PROD_ID', 'opera_hotel_room.PARENT_ROOM_TYPE'])->where([
  416. 'ORDER_ID' => $this->OrderID,
  417. 'ORDER_PROD_TYPE' => OrderMain::ORDER_PROD_TYPE_MAIN,
  418. 'PARENT_ORDER_ID' => 0,
  419. 'order_main.CANCEL_FLAG' => 0
  420. ])->limit(1)->asArray()->one();
  421. // $baseRoom = OperaHotelRoom::findOne(['CANCEL_FLAG' => 0, 'HOTEL_ID' => $this->PARENT_PROD_ID, 'ROOM_TYPE' => $this->PROD_ID]);
  422. $orderLog = new OperaHotelLog();
  423. $orderLog->CREATE_USER_ID = Yii::$app->user->id;
  424. $orderLog->HOTEL_ID = $res_order['PARENT_PROD_ID'];
  425. $orderLog->LOG_TYPE = OperaHotelLog::LOG_TYPE_ORDER;
  426. $orderLog->ROOM_TYPE = $res_order['PROD_ID'];
  427. $orderLog->PARENT_ROOM_TYPE = $res_order['PARENT_ROOM_TYPE'];
  428. switch ($type) {
  429. case 'makeInvoice': //开票
  430. $orderLog->LOG_DESC = "已开票,发票单号:" . $this->InvoiceNum;
  431. break;
  432. case 'postInvoice': //邮寄发票
  433. $orderLog->LOG_DESC = '发票已邮寄,快递公司:' . self::IN_SEND_TYPE[$this->PostCompany] . ',快递单号:' . $this->CourierNum;
  434. break;
  435. case 'refuse': //驳回发票
  436. $orderLog->LOG_DESC = '发票已驳回,驳回原因:' . $this->RejectReason;
  437. break;
  438. case 'applyInvoice':
  439. $orderLog->LOG_DESC = '申请开票,发票申请中';
  440. break;
  441. case 'addInvoice':
  442. $orderLog->LOG_DESC = $msg . ',关联订单号:'.$this->OrderID;
  443. break;
  444. default:
  445. $orderLog->LOG_DESC = '';
  446. }
  447. $orderLog->ORDER_ID = $this->OrderID;
  448. $orderLog->save();
  449. }
  450. public function startWord()
  451. {
  452. ob_start();
  453. echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  454. xmlns:w="urn:schemas-microsoft-com:office:word"
  455. xmlns="http://www.w3.org/TR/REC-html40">';
  456. }
  457. public function saveWord($path)
  458. {
  459. echo "</html>";
  460. $data = ob_get_contents();
  461. $this->wirtefile ($path,$data);
  462. }
  463. public function wirteFile ($fn,$data)
  464. {
  465. $fp=@fopen($fn,"wb",0777);
  466. @fwrite($fp,$data,0777);
  467. @fclose($fp);
  468. }
  469. }