invoice.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. <?php
  2. Doo::loadCore ( 'db/DooModel' );
  3. /**
  4. * 发票审批相关信息及其操作业务逻辑
  5. * @author CP.
  6. * @version 1.0
  7. * @namespace invoice
  8. * @package invoiceModel
  9. */
  10. class invoice extends DooModel {
  11. private $INVOICEKEY = "APPROVAL";
  12. private $FUZZY='FUZZY';
  13. private $EXACTLY="EXACTLY";
  14. /**
  15. *
  16. * @var integer $iid 发票ID
  17. */
  18. public $iid;
  19. /**
  20. *
  21. * @var integer $status 审批状态
  22. */
  23. public $status;
  24. /**
  25. *
  26. * @var string $invoiceManage 当前审批组人员
  27. */
  28. public $printStatus;
  29. public $postStatus;
  30. public $invoiceManage;
  31. /**
  32. *
  33. * @var string $pendingApprovals 当前需要审批的人
  34. */
  35. public $pendingApprovals;
  36. /**
  37. *
  38. * @var string $processApprovals 已经审批过的人员
  39. */
  40. public $processApprovals;
  41. /**
  42. *
  43. * @var string $invoiceSerial 发票单号
  44. */
  45. public $invoiceSerial;
  46. /**
  47. *
  48. * @var integer $invoicePrice 发票金额
  49. */
  50. public $invoicePrice;
  51. /**
  52. * 管理组人员:发票打印,
  53. * @var unknown
  54. */
  55. public $moldManage;
  56. /**
  57. *
  58. * @var integer $cid 办事处ID
  59. */
  60. public $cid;
  61. /**
  62. *
  63. * @var string $categoryName 办事处名称
  64. */
  65. public $categoryName;
  66. /**
  67. *
  68. * @var integer $sid 提交发票人ID
  69. */
  70. public $sid;
  71. /**
  72. *
  73. * @var string $userName 提交人名字
  74. */
  75. public $printer;
  76. public $userName;
  77. /**
  78. *
  79. * @var string $remark 备注
  80. */
  81. public $remark;
  82. /**
  83. *
  84. * @var string $invoiceElement 发票内容
  85. */
  86. public $invoiceElement;
  87. /**
  88. *
  89. * @var string $invoiceType 发票类型
  90. */
  91. public $invoiceType;
  92. /**
  93. *
  94. * @var string $invoiceTitle 发票抬头
  95. */
  96. public $invoiceTitle;
  97. /**
  98. *
  99. * @var string $invoiceCompany 开票公司
  100. */
  101. public $invoiceCompany;
  102. /**
  103. *
  104. * @var string $invoiceNo 发票号
  105. */
  106. public $invoiceNo;
  107. /**
  108. *
  109. * @var string $TIN 纳税人识别码
  110. */
  111. public $TIN;
  112. /**
  113. *
  114. * @var string $address 注册地址
  115. */
  116. public $address;
  117. /**
  118. *
  119. * @var string $phone 电话
  120. */
  121. public $phone;
  122. /**
  123. *
  124. * @var string $bank 开户银行
  125. */
  126. public $bank;
  127. /**
  128. *
  129. * @var string $bankAccount 银行账户
  130. */
  131. public $bankAccount;
  132. /**
  133. *
  134. * @var integer $doPost 邮寄
  135. */
  136. public $doPost;
  137. /**
  138. *
  139. * @var string $recipients 收件人
  140. */
  141. public $recipients;
  142. /**
  143. *
  144. * @var string $recipientsPhone 收件人电话
  145. */
  146. public $recipientsPhone;
  147. /**
  148. *
  149. * @var string $recipientsAddress 收件地址
  150. */
  151. public $recipientsAddress;
  152. /**
  153. *
  154. * @var string $mailItems 邮寄物品
  155. */
  156. public $mailItems;
  157. public $mailItemsJson;
  158. public $expressCompany;
  159. public $expressNumber;
  160. public $actualItems;
  161. public $poster;
  162. public $untreadReason;
  163. public $untreadPost;
  164. public $untreadCompany;
  165. public $untreadNumber;
  166. public $untreadItems;
  167. public $untreadStatus;
  168. public $irid;
  169. public $bindTime;
  170. /**
  171. *
  172. * @var date $date 提交时间
  173. */
  174. public $date;
  175. public $isDelete;
  176. /**
  177. *
  178. * @var date $updateTime 更新时间
  179. */
  180. public $updateTime;
  181. /**
  182. *
  183. * @var date $printTime 打印时间
  184. */
  185. public $printTime;
  186. public $postTime;
  187. public $_table = 'CLD_invoice';
  188. public $_primarykey = 'iid';
  189. public $_fields = array (
  190. 'iid',
  191. 'invoiceManage',
  192. 'pendingApprovals',
  193. 'processApprovals',
  194. 'invoiceSerial',
  195. 'status',
  196. 'printStatus',
  197. 'postStatus',
  198. 'untreadStatus',
  199. 'invoicePrice',
  200. 'moldManage',
  201. 'cid',
  202. 'categoryName',
  203. 'irid',
  204. 'remark',
  205. 'invoiceElement',
  206. 'invoiceType',
  207. 'invoiceTitle',
  208. 'invoiceCompany',
  209. 'invoiceNo',
  210. 'TIN',
  211. 'address',
  212. 'phone',
  213. 'bank',
  214. 'bankAccount',
  215. 'doPost',
  216. 'recipients',
  217. 'recipientsPhone',
  218. 'recipientsAddress',
  219. 'mailItems',
  220. 'mailItemsJson',
  221. 'expressCompany',
  222. 'expressNumber',
  223. 'actualItems',
  224. 'untreadReason',
  225. 'untreadPost',
  226. 'untreadCompany',
  227. 'untreadNumber',
  228. 'untreadItems',
  229. 'poster',
  230. 'sid',
  231. 'userName',
  232. 'printer',
  233. 'date',
  234. 'isDelete',
  235. 'updateTime',
  236. 'printTime',
  237. 'postTime',
  238. 'bindTime'
  239. );
  240. /**
  241. * 根据发票ID获取一条发票数据
  242. * @param number $iid 发票ID
  243. * @param number $select 需要获取的字段,为空获取全部数据
  244. * @return array|array() 返回发票数据
  245. */
  246. public function getInvoiceByIid($iid = 0, $select = "") {
  247. if (! is_numeric ( $iid ))
  248. $iid = $this->authcode ( $iid );
  249. $condition = array (
  250. 'where' => "iid=" . $iid,
  251. 'asArray' => TRUE
  252. );
  253. if (! empty ( $select ))
  254. $condition += array (
  255. 'select' => $select
  256. );
  257. $Detail = array ();
  258. if (is_numeric ( $iid ) && ! empty ( $iid ))
  259. $Detail = $this->getOne ( $condition );
  260. if (isset ( $Detail ['expressCompany'] ) && ! empty ( $Detail ['expressCompany'] )) {
  261. $express = explode ( ":", $Detail ['expressCompany'] );
  262. $Detail ['expressCompany'] = $express [0];
  263. $Detail ['expressCom'] = $express [1];
  264. } else {
  265. $Detail ['expressCompany'] = '';
  266. $Detail ['expressCom'] = '';
  267. }
  268. $Detail ['iidKey'] = $this->authcode ( $Detail ['iid'], '' );
  269. Doo::loadModel ( 'L_category' );
  270. $lCategory = new L_category ();
  271. $Detail ['category'] = $lCategory->getCategory ();
  272. return $Detail;
  273. }
  274. /**
  275. * 根据标题或者开票单位获得相关信息 - 准确查询
  276. * @param string $title 查询条件
  277. * @param string $query 默认模糊查询
  278. */
  279. public function getInvoiceByTitle($title="",$query="FUZZY",$cond=''){
  280. $detail=array();
  281. if (!empty($title)){
  282. $condition = array (
  283. 'asArray' => TRUE
  284. );
  285. if ($query==$this->FUZZY)
  286. $condition += array (
  287. 'where' => " (invoiceTitle like '%" . $title . "%' or invoiceCompany '%" . $title . "%') ".$cond,
  288. );
  289. else
  290. $condition += array (
  291. 'where' => "(invoiceTitle = '" . $title . "' or invoiceCompany= '" . $title . "') " .$cond,
  292. );
  293. $detail = $this->getOne ( $condition );
  294. if (!empty($detail)){
  295. Doo::loadClass ( 'XDeode' );
  296. $XDeode = new XDeode ( 5 );
  297. $detail ['iidKey'] = $this->authcode ( $detail ['iid'], '' );
  298. $detail ['iidKeyXD'] = $XDeode->encode ( $detail ['iid'] );
  299. }
  300. }
  301. return $detail;
  302. }
  303. public function getInvoiceByIsid($iid = "", $sid = 0) {
  304. if (empty ( $iid ) || empty ( $sid ))
  305. return array ();
  306. Doo::loadClass ( 'XDeode' );
  307. $XDeode = new XDeode ( 5 );
  308. $iid = $XDeode->decode ( $iid );
  309. if (! is_numeric ( $iid ))
  310. return array ();
  311. $detail = $this->getOne ( array (
  312. 'where' => "iid= '" . $iid . "'", // and sid=".$sid,
  313. 'asArray' => TRUE
  314. ) );
  315. return $detail;
  316. }
  317. /**
  318. * 获取单个可以打印的发票数据
  319. * @param number $iid 发票id
  320. * @param number $select 需要获取的字段,为空获取全部数据
  321. * @return array|array() 返回发票数据
  322. */
  323. public function getInvoiceByPrint($iid = 0, $select = "") {
  324. $condition = array (
  325. 'where' => "iid= '" . $iid . "' and status=2",
  326. 'asArray' => TRUE
  327. );
  328. if (! empty ( $select ))
  329. $condition += array (
  330. 'select' => $select
  331. );
  332. $detail = array ();
  333. if (! empty ( $iid ) && is_numeric ( $iid ))
  334. $detail = $this->getOne ( $condition );
  335. return $detail;
  336. }
  337. /**
  338. * 添加一个发票并进入审批状态
  339. * @param array $item 发票相关数据
  340. * @return number 返回发票ID
  341. */
  342. public function addInvoice($item = array()) {
  343. $lid = 0;
  344. if (is_array ( $item ) && ! empty ( $item )) {
  345. foreach ( $item as $key => $value ) {
  346. $this->$key = $value;
  347. }
  348. $lid = $this->insert ();
  349. }
  350. return $lid;
  351. }
  352. /**
  353. * 根据参数字段更新相应字段(主键ID必须传)
  354. * @param array $item 相关需要更新的字段信息
  355. * @return number 返回发票ID
  356. */
  357. public function setInvoiceByCondition($item = array()) {
  358. $lid = 0;
  359. if (is_array ( $item ) && ! empty ( $item )) {
  360. foreach ( $item as $key => $value ) {
  361. $this->$key = $value;
  362. }
  363. $lid = $this->update ();
  364. }
  365. return $lid;
  366. }
  367. /**
  368. * 根据管理组和发票当前状态获得打印发票数据;其中当iid为空时获取所有发票数据,反之获取1条数据
  369. * @param number $sid 管理组人员ID
  370. * @param number $status 状态 为0时获取可以打印数据,为1时获取已经完成打印数据
  371. * @param number $iid 发票ID
  372. * @return array|array() 返回所有发票数据,当iid有值时返回一条数据
  373. */
  374. public function getPrintInvoiceByManage($sid = 0, $status = 0, $iid = 0) {
  375. $list = array ();
  376. if (! empty ( $sid ) && empty ( $iid ))
  377. $list = $this->find ( array (
  378. 'where' => "status=2 and printStatus=" . $status . " and moldManage like '%[\"" . $sid . "\",%'",
  379. 'desc' => 'iid',
  380. 'asArray' => TRUE
  381. ) );
  382. elseif (! empty ( $sid ) && ! empty ( $iid ))
  383. $list = $this->getOne ( array (
  384. 'where' => "status=2 and printStatus=" . $status . " and moldManage like '%[\"" . $sid . "\",%' and iid=" . $iid,
  385. 'asArray' => TRUE
  386. ) );
  387. return $list;
  388. }
  389. /**
  390. * 获得与我相关的发票数据,其中包含 处理中,待处理,最旧入账等数据;当iid有值时获取一条关于sid的发票
  391. * @param number $sid 用户ID
  392. * @param number $iid 发票ID
  393. * @return array|array()
  394. */
  395. public function getMyInvoice($sid = 0, $iid = 0) {
  396. Doo::loadModel ( 'invoiceOperationLog' );
  397. $invoiceOperationLog = new invoiceOperationLog ();
  398. Doo::loadClass ( 'XDeode' );
  399. $XDeode = new XDeode ( 5 );
  400. $list = array ();
  401. if (! empty ( $iid )) {
  402. $list = $this->getOne ( array (
  403. 'where' => " sid=" . $sid . " and iid=" . $iid,
  404. 'asArray' => TRUE
  405. ) );
  406. } elseif (! empty ( $sid ) && empty ( $iid )) {
  407. $list ['pendingInvoice'] = array ();
  408. $list ['handleInvoice'] = array ();
  409. $list ['pendingInvoice'] = $this->find ( array (
  410. 'where' => "((status=1) or (status=2 and printStatus=0) or ( status=2 and printStatus=1 and untreadStatus=1) ) and sid=" . $sid,
  411. 'desc' => 'iid',
  412. 'asArray' => TRUE
  413. ) );
  414. foreach ( $list ['pendingInvoice'] as $key => $value ) {
  415. $list ['pendingInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  416. $list ['pendingInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  417. }
  418. $list ['handleInvoice'] = $this->find ( array (
  419. // 需求变更 -去除出票状态的数据 or (status=2 and printStatus=1 and untreadStatus=0 ) or (status=2 and printStatus=1 and untreadStatus=2 )
  420. 'where' => "(status=3 or status=4 ) and isDelete=0 and sid=" . $sid,
  421. 'desc' => 'iid',
  422. 'asArray' => TRUE
  423. ) );
  424. foreach ( $list ['handleInvoice'] as $key => $value ) {
  425. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  426. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  427. $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  428. }
  429. }
  430. return $list;
  431. }
  432. /**
  433. * 获得所有未入账发票
  434. * @param number $invoiceTC 发票title
  435. */
  436. public function getInvoiceByUnAccount($invoiceTC='') {
  437. Doo::loadClass ( 'XDeode' );
  438. $XDeode = new XDeode ( 5 );
  439. $title="";
  440. if (!empty($invoiceTC))
  441. $title=' and (invoiceTitle like "%'.$invoiceTC.'%" or invoiceCompany like "%'.$invoiceTC.'%" )';
  442. $list = $this->find ( array (//
  443. 'where' => "status=2 and printStatus=1 and untreadStatus=0 and irid='' and isDelete=0 ".$title,
  444. 'desc' => 'iid',
  445. 'asArray' => TRUE
  446. ));
  447. foreach ( $list as $key => $value ) {
  448. $list [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  449. }
  450. return $list;
  451. }
  452. /**
  453. * 获得与我有关可收款的发票,已经出票的发票
  454. * @param number $sid 开票人ID
  455. */
  456. public function getInvoiceByReceivables($sid = 0) {
  457. Doo::loadClass ( 'XDeode' );
  458. $XDeode = new XDeode ( 5 );
  459. $list ['handleInvoice'] = $this->find ( array (
  460. 'where' => "(status=2 and printStatus=1 and untreadStatus=0 ) and isDelete=0 and sid=" . $sid,
  461. 'desc' => 'iid',
  462. 'asArray' => TRUE
  463. ) );
  464. foreach ( $list ['handleInvoice'] as $key => $value ) {
  465. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  466. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  467. // $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  468. }
  469. return $list ['handleInvoice'];
  470. }
  471. /**
  472. * 获取需要邮寄的发票
  473. * @param integer $postStatus 0为获取需要邮寄的发票,1为获取已经邮寄的发票
  474. * @return array|array() 返回相关数据
  475. */
  476. public function getPostByInvoice($postStatus = 0) {
  477. Doo::loadClass ( 'XDeode' );
  478. $XDeode = new XDeode ( 5 );
  479. $list = $this->find ( array (
  480. 'where' => "postStatus=" . $postStatus . " and doPost=1 and status=2 and isDelete=0",
  481. 'asArray' => TRUE
  482. ) );
  483. foreach ( $list as $key => $value ) {
  484. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  485. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  486. $list [$key] ['expressCompany'] = '';
  487. $list [$key] ['expressCom'] = '';
  488. if (! empty ( $value ['expressCompany'] )) {
  489. $express = explode ( ":", $value ['expressCompany'] );
  490. $list [$key] ['expressCompany'] = $express [0];
  491. $list [$key] ['expressCom'] = $express [1];
  492. }
  493. }
  494. return $list;
  495. }
  496. /**
  497. * 根据退票状态获得相关数据,iid有值时只获得一条数据
  498. * @param number $untreadStatus 退票状态
  499. * @param number $iid 发票ID
  500. */
  501. public function getInvoiceByUntreadStatus($untreadStatus = 1, $iid = 0) {
  502. Doo::loadClass ( 'XDeode' );
  503. $XDeode = new XDeode ( 5 );
  504. if (empty ( $iid )) {
  505. $list = $this->find ( array (
  506. 'where' => " printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2 ",
  507. 'asArray' => TRUE
  508. ) );
  509. if ($untreadStatus == 2) {
  510. Doo::loadModel ( 'invoiceOperationLog' );
  511. $invoiceOperationLog = new invoiceOperationLog ();
  512. }
  513. foreach ( $list as $key => $value ) {
  514. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  515. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  516. $list [$key] ['untreadCompany'] = '';
  517. $list [$key] ['untreadCom'] = '';
  518. if (! empty ( $value ['untreadCompany'] )) {
  519. $express = explode ( ":", $value ['untreadCompany'] );
  520. $list [$key] ['untreadCompany'] = $express [0];
  521. $list [$key] ['untreadCom'] = $express [1];
  522. }
  523. if ($untreadStatus == 2) {
  524. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  525. }
  526. }
  527. } else {
  528. $list = $this->getOne ( array (
  529. 'where' => "iid=" . $iid . " and printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2",
  530. 'asArray' => TRUE
  531. ) );
  532. }
  533. return $list;
  534. }
  535. /**
  536. * 审批中获取相关数据 包括最近的一条操作记录
  537. * @param number $status.
  538. * @param number $limit.
  539. */
  540. function getInvoiceByProcessStatus($limit = 4, $desc = 'desc') {
  541. Doo::loadClass ( 'XDeode' );
  542. $XDeode = new XDeode ( 5 );
  543. $list = $this->find ( array (
  544. 'where' => " (status=1 or status=3 or status=4) and processApprovals!=''",
  545. 'limit' => $limit,
  546. $desc => 'iid',
  547. 'asArray' => TRUE
  548. ) );
  549. Doo::loadModel ( 'invoiceOperationLog' );
  550. $invoiceOperationLog = new invoiceOperationLog ();
  551. foreach ( $list as $key => $value ) {
  552. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  553. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  554. }
  555. return $list;
  556. }
  557. function getInvoiceByUntreadStatusPage($limit = 0, $con = "", $desc = 'desc') {
  558. if (empty ( $limit ) || empty ( $con ))
  559. return array ();
  560. $list = $this->find ( array (
  561. 'where' => $con,
  562. 'limit' => $limit,
  563. $desc => 'iid',
  564. 'asArray' => TRUE
  565. ) );
  566. Doo::loadClass ( 'XDeode' );
  567. $XDeode = new XDeode ( 5 );
  568. Doo::loadModel ( 'invoiceReceivables' );
  569. $invoiceReceivables = new invoiceReceivables ();
  570. Doo::loadModel ( 'invoiceOperationLog' );
  571. $invoiceOperationLog = new invoiceOperationLog ();
  572. foreach ( $list as $key => $value ) {
  573. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  574. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  575. $list [$key] ['untreadCompany'] = '';
  576. $list [$key] ['untreadCom'] = '';
  577. if (! empty ( $value ['untreadCompany'] )) {
  578. $express = explode ( ":", $value ['untreadCompany'] );
  579. $list [$key] ['untreadCompany'] = $express [0];
  580. $list [$key] ['untreadCom'] = $express [1];
  581. }
  582. $list [$key] ['irList'] = $invoiceReceivables->getInvoiceReceivablesInIridString ( $value ['irid'] );
  583. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  584. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  585. $list [$key] ['sumPrice'] = 0;
  586. if (! empty ( $list [$key] ['irList'] ))
  587. $list [$key] ['sumPrice'] = $list [$key] ['irList'] [0] ['sumPrice'];
  588. // $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  589. }
  590. return $list;
  591. }
  592. /**
  593. * 加密或解密指定字符串
  594. *
  595. * @param string $string 要加密或解密的字符串
  596. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  597. * @param string $key 加解密的key
  598. * @param $expiry 超时值
  599. *
  600. */
  601. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  602. $ckey_length = 4;
  603. if (! $key) {
  604. $key = $this->INVOICEKEY;
  605. }
  606. $key = md5 ( $key );
  607. $keya = md5 ( substr ( $key, 0, 16 ) );
  608. $keyb = md5 ( substr ( $key, 16, 16 ) );
  609. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  610. $cryptkey = $keya . md5 ( $keya . $keyc );
  611. $key_length = strlen ( $cryptkey );
  612. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  613. $string_length = strlen ( $string );
  614. $result = '';
  615. $box = range ( 0, 255 );
  616. $rndkey = array ();
  617. for($i = 0; $i <= 255; $i ++) {
  618. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  619. }
  620. for($j = $i = 0; $i < 256; $i ++) {
  621. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  622. $tmp = $box [$i];
  623. $box [$i] = $box [$j];
  624. $box [$j] = $tmp;
  625. }
  626. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  627. $a = ($a + 1) % 256;
  628. $j = ($j + $box [$a]) % 256;
  629. $tmp = $box [$a];
  630. $box [$a] = $box [$j];
  631. $box [$j] = $tmp;
  632. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  633. }
  634. if ($operation == 'DECODE') {
  635. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  636. return substr ( $result, 26 );
  637. } else {
  638. return '';
  639. }
  640. } else {
  641. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  642. }
  643. }
  644. }
  645. ?>