invoiceReceivables.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 invoiceReceivables extends DooModel {
  11. private $INVOICEKEY = "APPROVAL";
  12. public $irid;
  13. public $bindStatus;
  14. public $untreadStatus;
  15. public $untreadStaff;
  16. public $iid;
  17. public $iidExtend;
  18. public $bindPrice;
  19. public $receivablesPrice;
  20. public $OriginalPrice;
  21. public $receivablesSerial;
  22. public $receivablesMessage;
  23. public $receivablesBank;
  24. public $receivablesDate;
  25. public $receivablesCategory;
  26. public $receivablesStaff;
  27. public $recepitTitleMark;
  28. public $inputStaff;
  29. public $inputType;
  30. public $source;
  31. public $date;
  32. public $confirmTime;
  33. public $bindDate;
  34. public $unbundlingTime;
  35. public $accountClerk;
  36. public $sourcePrice;
  37. public $bankBatchNumber;
  38. public $refundType;
  39. public $refundCompany;
  40. public $refundNumber;
  41. public $refundPrice;
  42. public $refundRemarks;
  43. public $refundLog;
  44. public $relationIrid;
  45. public $relationLog;
  46. public $_table = 'CLD_invoiceReceivables';
  47. public $_primarykey = 'irid';
  48. public $_fields = array (
  49. 'irid',
  50. 'bindStatus',
  51. 'untreadStatus',
  52. 'untreadStaff',
  53. 'iid',
  54. 'iidExtend',
  55. 'bindPrice',
  56. 'receivablesPrice',
  57. 'OriginalPrice',
  58. 'receivablesSerial',
  59. 'receivablesMessage',
  60. 'receivablesBank',
  61. 'receivablesDate',
  62. 'receivablesCategory',
  63. 'receivablesStaff',
  64. 'recepitTitleMark',
  65. 'inputStaff',
  66. 'inputType',
  67. 'source',
  68. 'date',
  69. 'confirmTime',
  70. 'bindDate',
  71. 'unbundlingTime',
  72. 'accountClerk',
  73. 'sourcePrice',
  74. 'bankBatchNumber',
  75. 'refundType',
  76. 'refundCompany',
  77. 'refundNumber',
  78. 'refundPrice',
  79. 'refundRemarks',
  80. 'refundLog',
  81. 'relationIrid',
  82. 'relationLog'
  83. );
  84. public function getSumOfReceivablesPrice($iridString = "") {
  85. if (empty ( $iridString ))
  86. return 0;
  87. $list = $this->getOne ( array (
  88. 'select' => ' sum(receivablesPrice) as receivablesPrice ',
  89. 'where' => " irid IN ( " . $iridString . " )",
  90. 'groupby' => 'iid',
  91. 'asArray' => TRUE
  92. ) );
  93. if (empty ( $list ))
  94. return 0;
  95. return $list ['receivablesPrice'];
  96. }
  97. /**
  98. * 获取最新几条收款包括最新的一条操作日志
  99. * @param number $limit
  100. * @return unknown
  101. */
  102. public function getInvoiceReceivablesByNew($limit = 4, $con = "") {
  103. if (empty ( $con ))
  104. return array ();
  105. $list = $this->find ( array (
  106. 'where' => $con,
  107. 'desc' => 'irid',
  108. 'limit' => $limit,
  109. 'asArray' => TRUE
  110. ) );
  111. Doo::loadModel ( 'invoiceROLog' );
  112. $invoiceROLog = new invoiceROLog ();
  113. Doo::loadModel ( 'invoice' );
  114. $invoice = new invoice ();
  115. foreach ( $list as $key => $value ) {
  116. $list [$key] ['invoiceROLog'] = $invoiceROLog->getInvoiceROLogByIrid ( $value ['irid'], 'desc' );
  117. $list [$key] ['invoiceDetail'] = $invoice->getInvoiceByIid ( $value ['iid'] );
  118. }
  119. return $list;
  120. }
  121. public function getInvoiceReceivablesByBBN($bankBatchNumber = "") {
  122. if (empty ( $bankBatchNumber ))
  123. return array ();
  124. $list = $this->find ( array (
  125. 'where' => "bankBatchNumber= '".$bankBatchNumber."'",
  126. 'asArray' => TRUE
  127. ) );
  128. return $list;
  129. }
  130. /**
  131. * 获得最近录入发票,不包含已经绑定的
  132. */
  133. public function getInvoiceReceivablesByInput() {
  134. Doo::loadClass ( 'XDeode' );
  135. $XDeode = new XDeode ( 5 );
  136. $list = $this->find ( array (
  137. 'where' => "bindStatus=0 and iid=0",
  138. 'desc' => 'irid',
  139. 'asArray' => TRUE
  140. ) );
  141. foreach ( $list as $key => $value ) {
  142. if ($value ['receivablesCategory'] != 'PUBLIC')
  143. $list [$key] ['receivablesCategory'] = explode ( ':', $value ['receivablesCategory'] );
  144. $list [$key]['iridKeyXD'] = $XDeode->encode ( $value ['irid'] );
  145. if ($value ['receivablesStaff'] != 'PUBLIC')
  146. $list [$key] ['receivablesStaff'] = explode ( '-', $value ['receivablesStaff'] );
  147. $list [$key] ['iridKey'] = $this->authcode ( $value ['irid'], '' );
  148. }
  149. return $list;
  150. }
  151. /**
  152. * 获得部分入账的收款
  153. */
  154. function getInvoiceReceivablesByHalf() {
  155. $sql = 'select group_concat(b.invoiceSerial) as invoiceSerial ,group_concat(b.iid) as invoiceId ,a.*
  156. from ' . $this->_table . ' as a left join CLD_invoice as b on
  157. find_in_set(b.iid,a.iidExtend) or a.iid=b.iid
  158. where a.bindStatus=0 and a.iid!=0 GROUP BY a.irid
  159. ';
  160. $query = Doo::db ()->query ( $sql );
  161. $list = $query->fetchAll ();
  162. if (empty ( $list ))
  163. return array ();
  164. if ($list [0] ['irid'] == NULL) {
  165. return array ();
  166. }
  167. Doo::loadClass ( 'XDeode' );
  168. $XDeode = new XDeode ( 5 );
  169. foreach ( $list as $key => $value ) {
  170. $list [$key] ['uPrice'] = $value ['receivablesPrice'] - $value ['bindPrice'];
  171. $invoiceSerial = explode ( ',', $value ['invoiceSerial'] );
  172. $invoiceId = explode ( ",", $value ['invoiceId'] );
  173. foreach ( $invoiceId as $k => $v ) {
  174. $invoiceId [$k] = $XDeode->encode ( $v );
  175. }
  176. $list [$key] ['invoiceList'] = array_combine ( $invoiceId, $invoiceSerial );
  177. $list [$key] ['iridAuthKey'] = $this->authcode ( $value ['irid'], '' );
  178. }
  179. return $list;
  180. }
  181. public function getInvoiceReceivablesByClaim($cid = 0, $sid = 0) {
  182. Doo::loadClass ( 'XDeode' );
  183. $XDeode = new XDeode ( 5 );
  184. if (! empty ( $cid )) {
  185. $list ['csClaim'] = $this->find ( array (
  186. 'where' => "receivablesCategory='public' and receivablesStaff='public' and bindStatus=0 and bankBatchNumber=''",
  187. 'desc' => 'irid',
  188. 'asArray' => TRUE
  189. ) );
  190. foreach ( $list ['csClaim'] as $key => $value ) {
  191. $list ['csClaim'] [$key] ['iidKey'] = $this->authcode ( $value ['irid'], '' );
  192. // $list ['csClaim'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  193. }
  194. $list ['sClaim'] = $this->find ( array (
  195. 'where' => "receivablesCategory!='public' and receivablesStaff='public' and receivablesCategory like '" . $cid . ":%' and bindStatus=0",
  196. 'desc' => 'irid',
  197. 'asArray' => TRUE
  198. ) );
  199. foreach ( $list ['sClaim'] as $key => $value ) {
  200. $list ['sClaim'] [$key] ['iidKey'] = $this->authcode ( $value ['irid'], '' );
  201. }
  202. }
  203. if (! empty ( $cid ) && ! empty ( $sid )) {
  204. $list ['arClaim'] = $this->find ( array ( // and receivablesCategory like '" . $cid . ":%'
  205. 'where' => "receivablesCategory!='public' and receivablesStaff!='public' and receivablesStaff like '" . $sid . "-%' and bindStatus=0",
  206. 'desc' => 'irid',
  207. 'asArray' => TRUE
  208. ) );
  209. foreach ( $list ['arClaim'] as $key => $value ) {
  210. $staff = explode ( "-", $value ['receivablesStaff'] );
  211. $list ['arClaim'] [$key] ['sid'] = $staff [0];
  212. $list ['arClaim'] [$key] ['username'] = $staff [1];
  213. $list ['arClaim'] [$key] ['iridKey'] = $XDeode->encode ( $value ['irid'] );
  214. }
  215. $list ['myClaim'] = $this->find ( array (
  216. 'where' => " receivablesStaff like '" . $sid . "-%' and receivablesCategory like '" . $cid . ":%' and bindStatus=0",
  217. 'desc' => 'irid',
  218. 'asArray' => TRUE
  219. ) );
  220. foreach ( $list ['myClaim'] as $key => $value ) {
  221. $list ['myClaim'] [$key] ['iidKey'] = $this->authcode ( $value ['irid'], '' );
  222. $staff = explode ( "-", $value ['receivablesStaff'] );
  223. $list ['myClaim'] [$key] ['sid'] = $staff [0];
  224. $list ['myClaim'] [$key] ['username'] = $staff [1];
  225. }
  226. $list ['bindClaim'] = $this->find ( array (
  227. 'where' => " bindStatus=1 and receivablesStaff like '" . $sid . "-%' ",
  228. 'desc' => 'irid',
  229. 'limit' => 3,
  230. 'asArray' => TRUE
  231. ) );
  232. Doo::loadModel ( 'invoice' );
  233. $invoice = new invoice ();
  234. Doo::loadClass ( 'XDeode' );
  235. $XDeode = new XDeode ( 5 );
  236. // print_r($list ['bindClaim']);
  237. foreach ( $list ['bindClaim'] as $key => $value ) {
  238. $id = $invoice->getInvoiceByIid ( $value ['iid'] );
  239. $id ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  240. $list ['bindClaim'] [$key] ['invoice'] = $id;
  241. $list ['bindClaim'] [$key] ['iridKeyUrl'] = $XDeode->encode ( $value ['irid'] );
  242. }
  243. }
  244. return $list;
  245. }
  246. /**
  247. * 根据发票ID和绑定状态获得收款数据,绑定状态为ALL获取所有状态数据
  248. * @param string $iid 发票ID
  249. * @param string $bindStatus
  250. * @return mixed
  251. */
  252. public function getInvoiceReceivablesByIid($iid = "", $bindStatus = "All") {
  253. Doo::loadClass ( 'XDeode' );
  254. $XDeode = new XDeode ( 5 );
  255. $iid = $this->authcode ( $iid );
  256. $list = array ();
  257. if (! empty ( $iid ) && is_numeric ( $iid )) {
  258. $sql = '';
  259. if ($bindStatus == 1)
  260. $sql = " and bindStatus=1";
  261. elseif ($bindStatus === 0)
  262. $sql = " and bindStatus=0";
  263. $list = $this->find ( array (
  264. 'where' => " iid=" . $iid . $sql,
  265. 'asArray' => TRUE
  266. ) );
  267. }
  268. foreach ( $list as $key => $value ) {
  269. $list [$key] ['iidKey'] = $this->authcode ( $value ['irid'], '' );
  270. $staff = explode ( "-", $value ['receivablesStaff'] );
  271. $list [$key] ['sid'] = $staff [0];
  272. $list [$key] ['username'] = $staff [1];
  273. $list [$key] ['iridKey'] = $XDeode->encode ( $value ['irid'] );
  274. }
  275. return $list;
  276. }
  277. public function getInvoiceReceivablesInIridString($iridString = "") {
  278. $list = array ();
  279. if (! empty ( $iridString ))
  280. $list = $this->find ( array (
  281. 'where' => " irid in ( " . $iridString . " )",
  282. 'asArray' => TRUE
  283. ) );
  284. $price = 0;
  285. $serial = 0;
  286. Doo::loadClass ( 'XDeode' );
  287. $XDeode = new XDeode ( 5 );
  288. foreach ( $list as $key => $value ) {
  289. $price += $value ['receivablesPrice'];
  290. $list [$key] ['iridKeyK'] = $XDeode->encode ( $value ['irid'] );
  291. }
  292. if (! empty ( $list ))
  293. $list [0] ['sumPrice'] = $price;
  294. return $list;
  295. }
  296. /**
  297. * 根据收款ID获取一条相关数据
  298. * @param string $irid
  299. * @param string $con 附加获取收款条件 可以为空
  300. * @return unknown[]|mixed[]|string[]
  301. */
  302. public function getInvoiceReceivablesByIrid($irid = "", $con = "") {
  303. Doo::loadClass ( 'XDeode' );
  304. $XDeode = new XDeode ( 5 );
  305. if (! is_numeric ( $irid ))
  306. $irid = $this->authcode ( $irid );
  307. $detail = array ();
  308. if (! empty ( $irid ) && is_numeric ( $irid ))
  309. $detail = $this->getOne ( array (
  310. 'where' => " irid=" . $irid . $con,
  311. 'asArray' => TRUE
  312. ) );
  313. if (empty ( $detail ))
  314. return $detail;
  315. $detail ['cid'] = '';
  316. $detail ['title'] = '';
  317. if ($detail ['receivablesCategory'] != "PUBLIC") {
  318. $category = explode ( ":", $detail ['receivablesCategory'] );
  319. $detail ['cid'] = $category [0];
  320. $detail ['title'] = $category [1];
  321. }
  322. $detail ['sid'] = '';
  323. $detail ['username'] = '';
  324. if ($detail ['receivablesStaff'] != "PUBLIC") {
  325. $staff = explode ( "-", $detail ['receivablesStaff'] );
  326. $detail ['sid'] = $staff [0];
  327. $detail ['username'] = $staff [1];
  328. }
  329. $detail ['iridKey'] = $XDeode->encode ( $detail ['irid'] );
  330. $detail ['iridEn'] = $this->authcode ( $detail ['irid'], '' );
  331. return $detail;
  332. }
  333. /**
  334. * 根据收款序列号获取一条相关数据
  335. * @param string $irid
  336. * @param string $con 附加获取收款条件 可以为空
  337. * @return unknown[]|mixed[]|string[]
  338. */
  339. public function getInvoiceReceivablesBySerial($receivablesSerial = "", $con = "") {
  340. Doo::loadClass ( 'XDeode' );
  341. $XDeode = new XDeode ( 5 );
  342. if (!empty($receivablesSerial))
  343. $detail = $this->getOne ( array (
  344. 'where' => " receivablesSerial like '%" . $receivablesSerial."%'" . $con,
  345. 'limit'=>1,
  346. 'asArray' => TRUE
  347. ) );
  348. if (empty ( $detail ))
  349. return $detail;
  350. $detail ['cid'] = '';
  351. $detail ['title'] = '';
  352. if ($detail ['receivablesCategory'] != "PUBLIC") {
  353. $category = explode ( ":", $detail ['receivablesCategory'] );
  354. $detail ['cid'] = $category [0];
  355. $detail ['title'] = $category [1];
  356. }
  357. $detail ['sid'] = '';
  358. $detail ['username'] = '';
  359. if ($detail ['receivablesStaff'] != "PUBLIC") {
  360. $staff = explode ( "-", $detail ['receivablesStaff'] );
  361. $detail ['sid'] = $staff [0];
  362. $detail ['username'] = $staff [1];
  363. }
  364. $detail ['iridKey'] = $XDeode->encode ( $detail ['irid'] );
  365. $detail ['iridEn'] = $this->authcode ( $detail ['irid'], '' );
  366. return $detail;
  367. }
  368. public function getInvoiceReceivablesByIrsid($irid = "", $sid = "") {
  369. Doo::loadClass ( 'XDeode' );
  370. $XDeode = new XDeode ( 5 );
  371. $irid = $XDeode->decode ( $irid );
  372. $detail = array ();
  373. if (! empty ( $irid ) && is_numeric ( $irid )) {
  374. if (! empty ( $sid ))
  375. $detail = $this->getOne ( array (
  376. 'where' => " irid=" . $irid . " and receivablesStaff like '" . $sid . "_%'",
  377. 'asArray' => TRUE
  378. ) );
  379. else
  380. $detail = $this->getOne ( array (
  381. 'where' => " irid=" . $irid,
  382. 'asArray' => TRUE
  383. ) );
  384. $detail ['sid'] = '';
  385. $detail ['username'] = '';//
  386. if (! empty ( $detail ['receivablesStaff'] )&&$detail ['receivablesStaff'] !='PUBLIC') {
  387. $staff = explode ( "-", $detail ['receivablesStaff'] );
  388. $detail ['sid'] = $staff [0];
  389. $detail ['username'] = $staff [1];
  390. }
  391. $detail ['iridKey'] = $XDeode->encode ( $detail ['irid'] );
  392. $detail ['iridEn'] = $this->authcode ( $detail ['irid'], '' );
  393. }
  394. return $detail;
  395. }
  396. /**
  397. * 添加一个收款
  398. * @param array $item 收款相关数据
  399. * @return number 返回收款ID
  400. */
  401. public function addInvoiceReceivables($item = array()) {
  402. $lid = 0;
  403. if (is_array ( $item ) && ! empty ( $item )) {
  404. foreach ( $item as $key => $value ) {
  405. $this->$key = $value;
  406. }
  407. $lid = $this->insert ();
  408. }
  409. return $lid;
  410. }
  411. /**
  412. * 根据参数字段更新相应字段(主键ID必须传)
  413. * @param array $item 相关需要更新的字段信息
  414. * @return number 返回发票ID
  415. */
  416. public function setInvoiceReceivablesByCondition($item = array()) {
  417. $lid = 0;
  418. if (is_array ( $item ) && ! empty ( $item )) {
  419. foreach ( $item as $key => $value ) {
  420. $this->$key = $value;
  421. }
  422. $lid = $this->update ();
  423. }
  424. return $lid;
  425. }
  426. /**
  427. * 根据irid 获取一组收款数据
  428. * @param string $irid
  429. * @return unknown
  430. */
  431. public function getInvoiceReceivablesInIrid($irid = '') {
  432. if (empty ( $irid ))
  433. return array ();
  434. $list = $this->find ( array (
  435. 'where' => " irid in(" . $irid . ")",
  436. 'asArray' => TRUE
  437. ) );
  438. foreach ( $list as $key => $value ) {
  439. }
  440. return $list;
  441. }
  442. function getInvoiceReceivablesByConditionPage($limit = 0, $con = "", $desc = 'desc', $descField = 'irid') {
  443. if (empty ( $limit ))
  444. return array ();
  445. $sql = 'select group_concat(b.invoiceSerial) as invoiceSerial ,group_concat(b.iid) as invoiceId ,a.*,b.invoicePrice
  446. from ' . $this->_table . ' as a left join CLD_invoice as b on
  447. find_in_set(b.iid,a.iidExtend) or a.iid=b.iid
  448. where ' . $con . ' group by a.irid ORDER BY ' . $descField . ' ' . $desc . ' limit ' . $limit . '
  449. ';
  450. $query = Doo::db ()->query ( $sql );
  451. $list = $query->fetchAll ();
  452. Doo::loadClass ( 'XDeode' );
  453. $XDeode = new XDeode ( 5 );
  454. Doo::loadModel ( 'invoiceROLog' );
  455. $invoiceROLog = new invoiceROLog ();
  456. foreach ( $list as $key => $value ) {
  457. $invoiceSerial = explode ( ',', $value ['invoiceSerial'] );
  458. $invoiceId = explode ( ",", $value ['invoiceId'] );
  459. if (empty ( $invoiceSerial [0] ))
  460. $list [$key] ['invoiceList'] = array ();
  461. else {
  462. foreach ( $invoiceId as $k => $v ) {
  463. if (! empty ( $v ))
  464. $invoiceId [$k] = $XDeode->encode ( $v );
  465. }
  466. $list [$key] ['invoiceList'] = array_combine ( $invoiceId, $invoiceSerial );
  467. }
  468. $list [$key] ['iridKeyK'] = $XDeode->encode ( $value ['irid'] );
  469. $list [$key] ['iridKey'] = $this->authcode ( $value ['irid'], '' );
  470. $list [$key] ['irolg'] = $invoiceROLog->getInvoiceROLogByIrid ( $value ['irid'], 'desc' );
  471. }
  472. // print_r($list);
  473. return $list;
  474. }
  475. function getReceivablesByUntreadStatusPage($limit = 0, $con = "", $desc = 'desc', $descField = 'irid') {
  476. if (empty ( $limit ))
  477. return array ();
  478. $list = $this->find ( array (
  479. 'where' => $con, // " bindStatus=1 and receivablesStaff like '" . $sid . "_%' ".$con,
  480. 'limit' => $limit,
  481. $desc => $descField,
  482. 'asArray' => TRUE
  483. ) );
  484. Doo::loadClass ( 'XDeode' );
  485. $XDeode = new XDeode ( 5 );
  486. Doo::loadModel ( 'invoice' );
  487. $invoice = new invoice ();
  488. Doo::loadModel ( 'invoiceROLog' );
  489. $invoiceROLog = new invoiceROLog ();
  490. foreach ( $list as $key => $value ) {
  491. $list [$key] ['iridKeyK'] = $XDeode->encode ( $value ['irid'] );
  492. $list [$key] ['iridKey'] = $this->authcode ( $value ['irid'], '' );
  493. if (isset ( $value ['iid'] ) && ! empty ( $value ['iid'] )) {
  494. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  495. $list [$key] ['irList'] = $invoice->getInvoiceByIid ( $value ['iid'] );
  496. } else {
  497. $list [$key] ['iidKeyK'] = '';
  498. $list [$key] ['irList'] = array (
  499. 'invoiceSerial' => ''
  500. );
  501. }
  502. $list [$key] ['irolg'] = $invoiceROLog->getInvoiceROLogByIrid ( $value ['irid'], 'desc' );
  503. // $list [$key] ['sumPrice'] = 0;
  504. // if (! empty ( $list [$key] ['irList'] ))
  505. // $list [$key] ['sumPrice'] = $list [$key] ['irList'] [0] ['sumPrice'];
  506. // $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  507. }
  508. return $list;
  509. }
  510. function delInvoiceReceivablesByIrid($irid = 0) {
  511. if (! empty ( $irid ) && is_numeric ( $irid ))
  512. $this->delete ( array (
  513. 'where' => 'irid=' . $irid
  514. ) );
  515. }
  516. /**
  517. * 加密或解密指定字符串
  518. *
  519. * @param string $string 要加密或解密的字符串
  520. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  521. * @param string $key 加解密的key
  522. * @param $expiry 超时值
  523. *
  524. */
  525. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  526. $ckey_length = 4;
  527. if (! $key) {
  528. $key = $this->INVOICEKEY;
  529. }
  530. $key = md5 ( $key );
  531. $keya = md5 ( substr ( $key, 0, 16 ) );
  532. $keyb = md5 ( substr ( $key, 16, 16 ) );
  533. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  534. $cryptkey = $keya . md5 ( $keya . $keyc );
  535. $key_length = strlen ( $cryptkey );
  536. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  537. $string_length = strlen ( $string );
  538. $result = '';
  539. $box = range ( 0, 255 );
  540. $rndkey = array ();
  541. for($i = 0; $i <= 255; $i ++) {
  542. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  543. }
  544. for($j = $i = 0; $i < 256; $i ++) {
  545. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  546. $tmp = $box [$i];
  547. $box [$i] = $box [$j];
  548. $box [$j] = $tmp;
  549. }
  550. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  551. $a = ($a + 1) % 256;
  552. $j = ($j + $box [$a]) % 256;
  553. $tmp = $box [$a];
  554. $box [$a] = $box [$j];
  555. $box [$j] = $tmp;
  556. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  557. }
  558. if ($operation == 'DECODE') {
  559. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  560. return substr ( $result, 26 );
  561. } else {
  562. return '';
  563. }
  564. } else {
  565. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  566. }
  567. }
  568. }
  569. ?>