invoicePaper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 invoicePaper extends DooModel {
  11. /**
  12. *
  13. * @var integer $iid 发票ID
  14. */
  15. public $ipid;
  16. public $invoiceType;
  17. public $invoiceCode;
  18. public $invoiceNo;
  19. public $iid;
  20. public $date;
  21. public $_table = 'CLD_invoicePaper';
  22. public $_primarykey = 'ipid';
  23. public $_fields = array (
  24. 'ipid',
  25. 'invoiceType',
  26. 'invoiceCode',
  27. 'invoiceNo',
  28. 'iid',
  29. 'date'
  30. );
  31. function getInvoicePaper($limit = 0, $con = "", $desc = 'desc') {
  32. if (empty ( $limit ) || empty ( $con ))
  33. return array ();
  34. $field = 'a.invoiceType,a.invoiceCode,a.invoiceNo,a.iid,a.date as paperDate,a.ipid
  35. ,b.invoiceSerial,b.invoiceTitle,b.invoiceCompany,b.invoicePrice,b.invoiceElement,b.date as invoiceDate,b.userName,b.categoryName,b.printTime';
  36. $sql = "select " . $field . " from " . $this->_table . " as a left join CLD_invoice as b on (a.iid=b.iid) " . $con . " ORDER BY a.invoiceNo " . $desc . ' limit ' . $limit;
  37. $query = Doo::db ()->query ( $sql );
  38. $list = $query->fetchAll ();
  39. Doo::loadClass ( 'XDeode' );
  40. $XDeode = new XDeode ( 5 );
  41. foreach ( $list as $key => $value ) {
  42. $list [$key] ['iidKey'] = $XDeode->encode ( $value ['iid'] );
  43. $list [$key] ['ipidKey'] = $XDeode->encode ( $value ['ipid'] );
  44. }
  45. return $list;
  46. }
  47. function getInvoicePaperCount($invoiceForm=0) {
  48. $listCount = $this->count ( array (
  49. 'where'=>'invoiceForm='.$invoiceForm,
  50. 'asArray' => TRUE
  51. ) );
  52. return $listCount;
  53. }
  54. /**
  55. * 根据发票类型获得未打印的发票号
  56. * @param number $invoiceType
  57. * @return unknown
  58. */
  59. function getInvoicePaperByInvoiceType( $invoiceType = 0,$invoiceForm=0) {
  60. $list = $this->find ( array (
  61. 'where' => 'iid=0 and invoiceType='.$invoiceType.' and invoiceForm='.$invoiceForm,
  62. 'asArray' => TRUE
  63. ) );
  64. Doo::loadClass ( 'XDeode' );
  65. $XDeode = new XDeode ( 5 );
  66. foreach ( $list as $key => $value ) {
  67. $list [$key] ['ipidKey'] = $XDeode->encode ( $value ['ipid'] );
  68. }
  69. return $list;
  70. }
  71. /**
  72. * 根据ID获得纸票号
  73. * @param number $ipid
  74. */
  75. function getInvoicePaperByIpid($ipid=0){
  76. $detail = $this->getOne ( array (
  77. 'where' => 'iid=0 and ipid='.$ipid,
  78. 'asArray' => TRUE
  79. ) );
  80. Doo::loadClass ( 'XDeode' );
  81. $XDeode = new XDeode ( 5 );
  82. if (empty ( $detail ) )
  83. return array ();
  84. $detail ['ipidKey'] = $XDeode->encode ( $detail ['ipid'] );
  85. return $detail;
  86. }
  87. /**
  88. * 根据参数字段更新相应字段(主键ID必须传)
  89. * @param array $item 相关需要更新的字段信息
  90. * @return number 返回发票ID
  91. */
  92. public function setInvoicePaperByCondition($item = array()) {
  93. $lid = 0;
  94. if (is_array ( $item ) && ! empty ( $item )) {
  95. foreach ( $item as $key => $value ) {
  96. $this->$key = $value;
  97. }
  98. $lid = $this->update ();
  99. }
  100. return $lid;
  101. }
  102. /**
  103. * 加密或解密指定字符串
  104. *
  105. * @param string $string 要加密或解密的字符串
  106. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  107. * @param string $key 加解密的key
  108. * @param $expiry 超时值
  109. *
  110. */
  111. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  112. $ckey_length = 4;
  113. if (! $key) {
  114. $key = $this->INVOICEKEY;
  115. }
  116. $key = md5 ( $key );
  117. $keya = md5 ( substr ( $key, 0, 16 ) );
  118. $keyb = md5 ( substr ( $key, 16, 16 ) );
  119. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  120. $cryptkey = $keya . md5 ( $keya . $keyc );
  121. $key_length = strlen ( $cryptkey );
  122. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  123. $string_length = strlen ( $string );
  124. $result = '';
  125. $box = range ( 0, 255 );
  126. $rndkey = array ();
  127. for($i = 0; $i <= 255; $i ++) {
  128. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  129. }
  130. for($j = $i = 0; $i < 256; $i ++) {
  131. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  132. $tmp = $box [$i];
  133. $box [$i] = $box [$j];
  134. $box [$j] = $tmp;
  135. }
  136. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  137. $a = ($a + 1) % 256;
  138. $j = ($j + $box [$a]) % 256;
  139. $tmp = $box [$a];
  140. $box [$a] = $box [$j];
  141. $box [$j] = $tmp;
  142. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  143. }
  144. if ($operation == 'DECODE') {
  145. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  146. return substr ( $result, 26 );
  147. } else {
  148. return '';
  149. }
  150. } else {
  151. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  152. }
  153. }
  154. }
  155. ?>