invoice.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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. public $isid;
  20. public $trainId;
  21. public $lossIid;
  22. public $ipSource;
  23. /**
  24. *
  25. * @var integer $status 审批状态
  26. */
  27. public $status;
  28. /**
  29. *
  30. * @var string $invoiceManage 当前审批组人员
  31. */
  32. public $printStatus;
  33. public $postStatus;
  34. public $invoiceManage;
  35. /**
  36. *
  37. * @var string $pendingApprovals 当前需要审批的人
  38. */
  39. public $pendingApprovals;
  40. /**
  41. *
  42. * @var string $processApprovals 已经审批过的人员
  43. */
  44. public $processApprovals;
  45. /**
  46. *
  47. * @var string $invoiceSerial 发票单号
  48. */
  49. public $invoiceSerial;
  50. /**
  51. *
  52. * @var integer $invoicePrice 发票金额
  53. */
  54. public $invoicePrice;
  55. public $invoiceQuantity;
  56. public $invoiceUnitPrice;
  57. /**
  58. * 管理组人员:发票打印,
  59. * @var unknown
  60. */
  61. public $moldManage;
  62. /**
  63. *
  64. * @var integer $cid 办事处ID
  65. */
  66. public $cid;
  67. /**
  68. *
  69. * @var string $categoryName 办事处名称
  70. */
  71. public $categoryName;
  72. /**
  73. *
  74. * @var integer $sid 提交发票人ID
  75. */
  76. public $sid;
  77. /**
  78. *
  79. * @var string $userName 提交人名字
  80. */
  81. public $printer;
  82. public $userName;
  83. /**
  84. *
  85. * @var string $remark 备注
  86. */
  87. public $remark;
  88. /**
  89. *
  90. * @var string $invoiceElement 发票内容
  91. */
  92. public $invoiceElement;
  93. /**
  94. *
  95. * @var string $invoiceType 发票类型
  96. */
  97. public $invoiceType;
  98. /**
  99. *
  100. * @var string $invoiceTitle 发票抬头
  101. */
  102. public $invoiceTitle;
  103. /**
  104. *
  105. * @var string $invoiceCompany 开票公司
  106. */
  107. public $invoiceCompany;
  108. /**
  109. *
  110. * @var string $invoiceNo 发票号
  111. */
  112. public $invoiceNo;
  113. /**
  114. *
  115. * @var string $TIN 纳税人识别码
  116. */
  117. public $TIN;
  118. /**
  119. *
  120. * @var string $address 注册地址
  121. */
  122. public $address;
  123. /**
  124. *
  125. * @var string $phone 电话
  126. */
  127. public $phone;
  128. /**
  129. *
  130. * @var string $bank 开户银行
  131. */
  132. public $bank;
  133. /**
  134. *
  135. * @var string $bankAccount 银行账户
  136. */
  137. public $bankAccount;
  138. /**
  139. *
  140. * @var integer $doPost 邮寄
  141. */
  142. public $doPost;
  143. /**
  144. *
  145. * @var string $recipients 收件人
  146. */
  147. public $recipients;
  148. /**
  149. *
  150. * @var string $recipientsPhone 收件人电话
  151. */
  152. public $recipientsPhone;
  153. /**
  154. *
  155. * @var string $recipientsAddress 收件地址
  156. */
  157. public $recipientsAddress;
  158. /**
  159. *
  160. * @var string $mailItems 邮寄物品
  161. */
  162. public $mailItems;
  163. public $mailItemsJson;
  164. public $expressCompany;
  165. public $expressNumber;
  166. public $actualItems;
  167. public $poster;
  168. public $untreadReason;
  169. public $untreadPost;
  170. public $untreadCompany;
  171. public $untreadNumber;
  172. public $untreadItems;
  173. public $untreadStatus;
  174. public $irid;
  175. public $bindTime;
  176. /**
  177. *
  178. * @var date $date 提交时间
  179. */
  180. public $date;
  181. public $isDelete;
  182. public $approvalTime;
  183. /**
  184. *
  185. * @var date $updateTime 更新时间
  186. */
  187. public $updateTime;
  188. /**
  189. *
  190. * @var date $printTime 打印时间
  191. */
  192. public $printTime;
  193. public $postTime;
  194. public $settlementType;
  195. public $accountingTime;
  196. public $_table = 'CLD_invoice';
  197. public $_primarykey = 'iid';
  198. public $_fields = array (
  199. 'iid',
  200. 'isid',
  201. 'trainId',
  202. 'ipSource',
  203. 'lossIid',
  204. 'invoiceManage',
  205. 'pendingApprovals',
  206. 'processApprovals',
  207. 'invoiceSerial',
  208. 'status',
  209. 'printStatus',
  210. 'postStatus',
  211. 'untreadStatus',
  212. 'invoiceQuantity',
  213. 'invoiceUnitPrice',
  214. 'invoicePrice',
  215. 'moldManage',
  216. 'cid',
  217. 'categoryName',
  218. 'irid',
  219. 'remark',
  220. 'invoiceElement',
  221. 'invoiceType',
  222. 'invoiceTitle',
  223. 'invoiceCompany',
  224. 'invoiceNo',
  225. 'TIN',
  226. 'address',
  227. 'phone',
  228. 'bank',
  229. 'bankAccount',
  230. 'doPost',
  231. 'recipients',
  232. 'recipientsPhone',
  233. 'recipientsAddress',
  234. 'mailItems',
  235. 'mailItemsJson',
  236. 'expressCompany',
  237. 'expressNumber',
  238. 'actualItems',
  239. 'untreadReason',
  240. 'untreadPost',
  241. 'untreadCompany',
  242. 'untreadNumber',
  243. 'untreadItems',
  244. 'poster',
  245. 'sid',
  246. 'userName',
  247. 'printer',
  248. 'date',
  249. 'isDelete',
  250. 'approvalTime',
  251. 'updateTime',
  252. 'printTime',
  253. 'postTime',
  254. 'bindTime',
  255. 'settlementType',
  256. 'accountingTime'
  257. );
  258. /**
  259. * 根据发票ID获取一条发票数据
  260. * @param number $iid 发票ID
  261. * @param number $select 需要获取的字段,为空获取全部数据
  262. * @return array|array() 返回发票数据
  263. */
  264. public function getInvoiceByIid($iid = 0, $select = "") {
  265. if (! is_numeric ( $iid ))
  266. $iid = $this->authcode ( $iid );
  267. $condition = array (
  268. 'where' => "iid=" . $iid,
  269. 'asArray' => TRUE
  270. );
  271. if (! empty ( $select ))
  272. $condition += array (
  273. 'select' => $select
  274. );
  275. $Detail = array ();
  276. if (is_numeric ( $iid ) && ! empty ( $iid ))
  277. $Detail = $this->getOne ( $condition );
  278. if (empty ( $Detail ))
  279. return $Detail;
  280. if (isset ( $Detail ['expressCompany'] ) && ! empty ( $Detail ['expressCompany'] )) {
  281. $express = explode ( ":", $Detail ['expressCompany'] );
  282. $Detail ['expressCompany'] = $express [0];
  283. $Detail ['expressCom'] = $express [1];
  284. } else {
  285. $Detail ['expressCompany'] = '';
  286. $Detail ['expressCom'] = '';
  287. }
  288. Doo::loadClass ( 'XDeode' );
  289. $XDeode = new XDeode ( 5 );
  290. $Detail ['iidKeyUrl'] = $XDeode->encode ( $Detail ['iid'] );
  291. $Detail ['iidKey'] = $this->authcode ( $Detail ['iid'], '' );
  292. Doo::loadModel ( 'L_category' );
  293. $lCategory = new L_category ();
  294. $Detail ['category'] = $lCategory->getCategory ();
  295. return $Detail;
  296. }
  297. /**
  298. * 根据培训班ID获得发票
  299. */
  300. function getInvoicePrintedByItid($trainId = 0) {
  301. $sql = 'select a.*,b.RIstatus as RIstatus
  302. from ' . $this->_table . ' as a left join CLD_RIExtend as b on (a.iid=b.iid)
  303. where a.trainId = ' . $trainId . ' and a.status=2 and a.printStatus=1 and (a.untreadStatus=0 or a.untreadStatus=3) ORDER BY a.iid ASC';
  304. $query = Doo::db ()->query ( $sql );
  305. $result = $query->fetchAll ();
  306. Doo::loadClass ( 'XDeode' );
  307. $XDeode = new XDeode ( 5 );
  308. Doo::loadModel ( 'invoiceReceivables' );
  309. $invoiceReceivables = new invoiceReceivables ();
  310. foreach ($result as $key=>$value){
  311. $result [$key] ['irDetail'] =$invoiceReceivables->getInvoiceReceivablesByIridList($value ['irid']);
  312. $result [$key] ['iidKey'] = $XDeode->encode ( $value ['iid'] );
  313. }
  314. return $result;
  315. // $condition = array (
  316. // 'where' => "trainId = " . $trainId . " and status=2 and printStatus=1 and (untreadStatus=0 or untreadStatus=3)",
  317. // 'asArray' => TRUE
  318. // );
  319. // $detail = $this->find ( $condition );
  320. // return $detail;
  321. }
  322. //办事处收款 有问题
  323. function sumOfInvoiceByItid($itidSql=0){
  324. $sql = 'select sum(a.invoicePrice) as invoicePrice ,a.trainId as trainId,a.iid
  325. from CLD_RIExtend as b left join ' . $this->_table . ' as a on (a.iid=b.iid)
  326. where (a.trainId in (' . $itidSql . ') and a.status=2 and a.printStatus=1 and a.untreadStatus=3) or b.RIstatus=1
  327. GROUP BY a.trainId';
  328. //echo $sql;
  329. $query = Doo::db ()->query ( $sql );
  330. return $result = $query->fetchAll ();
  331. }
  332. /**
  333. * 根据多个iid获得多个发票
  334. * @param number $iid
  335. */
  336. function getInvoiceInIid($iid = 0, $select = '') {
  337. if (empty ( $iid ))
  338. die ( 'illegal request' );
  339. $condition = array (
  340. 'where' => "iid in ( " . $iid . ")",
  341. 'asArray' => TRUE
  342. );
  343. if (! empty ( $select ))
  344. $condition += array (
  345. 'select' => $select
  346. );
  347. $list = $this->find ( $condition );
  348. Doo::loadClass ( 'XDeode' );
  349. $XDeode = new XDeode ( 5 );
  350. foreach ( $list as $key => $value ) {
  351. $list [$key] ['iidKey'] = $XDeode->encode ( $value ['iid'] );
  352. }
  353. return $list;
  354. }
  355. function setInvoiceLoss($iid = "") {
  356. if (empty ( $iid ))
  357. return 0;
  358. $sql = 'insert into ' . $this->_table . '(`status`, `printStatus`, `postStatus`, `untreadStatus`, `invoiceManage`, `pendingApprovals`, `processApprovals`, `invoiceSerial`,`invoiceQuantity`, `invoiceUnitPrice`, `invoicePrice`, `moldManage`, `cid`, `categoryName`, `sid`, `userName`, `printer`, `irid`, `remark`, `invoiceElement`, `invoiceType`, `invoiceTitle`, `invoiceCompany`, `invoiceNo`, `TIN`, `address`, `phone`, `bank`, `bankAccount`, `doPost`, `recipients`, `recipientsPhone`, `recipientsAddress`, `mailItems`, `mailItemsJson`, `expressCompany`, `expressNumber`, `actualItems`, `untreadReason`, `untreadPost`, `untreadCompany`, `untreadNumber`, `untreadItems`, `poster`, `date`, `isDelete`, `updateTime`, `printTime`, `postTime`, `bindTime`)
  359. select `status`, `printStatus`, `postStatus`, `untreadStatus`, `invoiceManage`, `pendingApprovals`, `processApprovals`, `invoiceSerial`,`invoiceQuantity`, `invoiceUnitPrice`, `invoicePrice`, `moldManage`, `cid`, `categoryName`, `sid`, `userName`, `printer`, `irid`, `remark`, `invoiceElement`, `invoiceType`, `invoiceTitle`, `invoiceCompany`, `invoiceNo`, `TIN`, `address`, `phone`, `bank`, `bankAccount`, `doPost`, `recipients`, `recipientsPhone`, `recipientsAddress`, `mailItems`, `mailItemsJson`, `expressCompany`, `expressNumber`, `actualItems`, `untreadReason`, `untreadPost`, `untreadCompany`, `untreadNumber`, `untreadItems`, `poster`, `date`, `isDelete`, `updateTime`, `printTime`, `postTime`, `bindTime` from ' . $this->_table . ' where iid=' . $iid;
  360. $query = Doo::db ()->query ( $sql );
  361. return Doo::db ()->lastInsertId ();
  362. }
  363. function getInvoiceByInvoiceNo($invoiceNo = '') {
  364. if (empty ( $invoiceNo ))
  365. return array ();
  366. $detail = $this->getOne ( array (
  367. 'where' => "invoiceNo= '" . $invoiceNo . "'",
  368. 'asArray' => TRUE
  369. ) );
  370. return $detail;
  371. }
  372. /**
  373. * 根据标题或者开票单位获得相关信息 - 准确查询
  374. * @param string $title 查询条件
  375. * @param string $query 默认模糊查询
  376. */
  377. public function getInvoiceByTitle($title = "", $query = "FUZZY", $cond = '', $fkq = false) {
  378. $detail = array ();
  379. if (! empty ( $title )) {
  380. $condition = array (
  381. 'asArray' => TRUE
  382. );
  383. if ($query == $this->FUZZY)
  384. $condition += array (
  385. 'where' => " (invoiceTitle like '%" . $title . "%' or invoiceCompany like '%" . $title . "%') " . $cond
  386. );
  387. else
  388. $condition += array (
  389. 'where' => "(invoiceTitle = '" . $title . "' or invoiceCompany= '" . $title . "') " . $cond
  390. );
  391. Doo::loadClass ( 'XDeode' );
  392. $XDeode = new XDeode ( 5 );
  393. if ($fkq) {
  394. $detail = $this->find ( $condition );
  395. foreach ( $detail as $key => $value ) {
  396. $detail [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  397. $detail [$key] ['iidKeyXD'] = $XDeode->encode ( $value ['iid'] );
  398. }
  399. } else {
  400. $detail = $this->getOne ( $condition );
  401. if (! empty ( $detail )) {
  402. $detail ['iidKey'] = $this->authcode ( $detail ['iid'], '' );
  403. $detail ['iidKeyXD'] = $XDeode->encode ( $detail ['iid'] );
  404. }
  405. }
  406. }
  407. return $detail;
  408. }
  409. public function getInvoiceByIsid($iid = "", $sid = 0) {
  410. if (empty ( $iid ) || empty ( $sid ))
  411. return array ();
  412. Doo::loadClass ( 'XDeode' );
  413. $XDeode = new XDeode ( 5 );
  414. $iid = $XDeode->decode ( $iid );
  415. if (! is_numeric ( $iid ))
  416. return array ();
  417. $detail = $this->getOne ( array (
  418. 'where' => "iid= '" . $iid . "'", // and sid=".$sid,
  419. 'asArray' => TRUE
  420. ) );
  421. $detail ['expressCompany'] = '';
  422. $detail ['expressCom'] = '';
  423. $detail ['invoiceTraining'] = array ();
  424. if (! empty ( $detail ['expressCompany'] )) {
  425. $express = explode ( ":", $detail ['expressCompany'] );
  426. $detail ['expressCompany'] = $express [0];
  427. $detail ['expressCom'] = $express [1];
  428. }
  429. // 培训班
  430. if ($detail ['settlementType'] == 1) {
  431. Doo::loadModel ( 'invoiceTraining' );
  432. $invoiceTraining = new invoiceTraining ();
  433. $detail ['invoiceTraining'] = $invoiceTraining->getInvoiceTrainingByItid ( $detail ['trainId'] );
  434. }
  435. $detail ['iidKeyXD'] = $this->authcode ( $detail ['iid'], '' );
  436. return $detail;
  437. }
  438. /**
  439. * 统计培训班开票
  440. */
  441. function sumOfinvoiceTrain($itidSql) {
  442. $sql = 'select sum(b.invoicePrice) as invoicePrice ,b.trainId as trainId
  443. from CLD_invoiceStore as a left join ' . $this->_table . ' as b on (a.iid=b.iid)
  444. where b.trainId in (' . $itidSql . ') and b.status=2 and b.printStatus=1 and (b.untreadStatus =0 or b.untreadStatus=3) and b.isDelete=0
  445. GROUP BY b.trainId';
  446. $query = Doo::db ()->query ( $sql );
  447. return $result = $query->fetchAll ();
  448. }
  449. /**
  450. * 公司收款汇总
  451. * @param unknown $itidSql
  452. */
  453. function sumOfInvoiceRecelvablesCompany($itidSql) {
  454. $sql = 'select sum(b.receivablesPrice) as receivablesPrice ,a.trainId,count(*) as count
  455. from ' . $this->_table . ' as a left join CLD_invoiceReceivables as b on (a.iid=b.iid)
  456. where a.trainId in (' . $itidSql . ') and b.receivablesBank!="财务录入" and a.status=2 and a.printStatus=1 and ( a.untreadStatus=3) and a.irid!=""
  457. GROUP BY a.trainId';
  458. $query = Doo::db ()->query ( $sql );
  459. return $result = $query->fetchAll ();
  460. }
  461. //
  462. function sumOfInvoiceRecelvablesTrain($itidSql) {
  463. // $sql = 'select sum(b.receivablesPrice) as receivablesPrice ,a.trainId,count(*) as count
  464. // from ' . $this->_table . ' as a left join CLD_invoiceReceivables as b on find_in_set(a.irid,b.irid)
  465. // where a.trainId in (' . $itidSql . ') and a.status=2 and a.printStatus=1 and (a.untreadStatus =0 or a.untreadStatus=3) and a.irid!=""
  466. // GROUP BY a.trainId';
  467. $sql = 'select sum(b.receivablesPrice) as receivablesPrice ,a.trainId,count(*) as count
  468. from ' . $this->_table . ' as a left join CLD_invoiceReceivables as b on (a.iid=b.iid)
  469. where a.trainId in (' . $itidSql . ') and a.status=2 and a.printStatus=1 and (a.untreadStatus =0 or a.untreadStatus=3) and a.irid!=""
  470. GROUP BY a.trainId';
  471. $query = Doo::db ()->query ( $sql );
  472. return $result = $query->fetchAll ();
  473. }
  474. // 获得培训班发票
  475. function getInvoiceByTrainingCount($itid = 0) {
  476. if (empty ( $itid ))
  477. return 0;
  478. $sql = 'select count(*) as count from CLD_invoiceStore as a left join ' . $this->_table . ' as b on (a.iid=b.iid)
  479. where a.trainId= ' . $itid . ' and b.status!=4 and (b.untreadStatus is null or b.untreadStatus!=2) and (b.isDelete is null or b.isDelete=0 )';
  480. //echo $sql;
  481. //echo $sql;
  482. $query = Doo::db ()->query ( $sql );
  483. $result = $query->fetchAll ();
  484. return $result [0] ['count'];
  485. // $detail = $this->count ( array (
  486. // 'where' => "trainId= '" . $itid . "' and untreadStatus!=2 and isDelete=0", // and sid=".$sid,
  487. // 'asArray' => TRUE
  488. // ) );
  489. // return $detail;
  490. }
  491. /**
  492. * 根据仓库ID获得发票
  493. * @param string $Istoreid
  494. * @return unknown
  495. */
  496. public function getInvoiceByIstoreid($Istoreid = "", $select = "") {
  497. if (empty ( $Istoreid ))
  498. return array ();
  499. $condition = array (
  500. 'where' => "isid in ( " . $Istoreid . ")",
  501. 'asArray' => TRUE
  502. );
  503. if (! empty ( $select ))
  504. $condition += array (
  505. 'select' => $select
  506. );
  507. $detail = $this->find ( $condition );
  508. return $detail;
  509. }
  510. /**
  511. * 获取单个可以打印的发票数据
  512. * @param number $iid 发票id
  513. * @param number $select 需要获取的字段,为空获取全部数据
  514. * @return array|array() 返回发票数据
  515. */
  516. public function getInvoiceByPrint($iid = 0, $select = "") {
  517. $condition = array (
  518. 'where' => "iid= '" . $iid . "' and status=2 and untreadStatus=0 ",
  519. 'asArray' => TRUE
  520. );
  521. if (! empty ( $select ))
  522. $condition += array (
  523. 'select' => $select
  524. );
  525. $detail = array ();
  526. if (! empty ( $iid ) && is_numeric ( $iid ))
  527. $detail = $this->getOne ( $condition );
  528. return $detail;
  529. }
  530. /**
  531. * 添加一个发票并进入审批状态
  532. * @param array $item 发票相关数据
  533. * @return number 返回发票ID
  534. */
  535. public function addInvoice($item = array()) {
  536. $lid = 0;
  537. if (is_array ( $item ) && ! empty ( $item )) {
  538. foreach ( $item as $key => $value ) {
  539. $this->$key = $value;
  540. }
  541. $lid = $this->insert ();
  542. }
  543. return $lid;
  544. }
  545. /**
  546. * 根据参数字段更新相应字段(主键ID必须传)
  547. * @param array $item 相关需要更新的字段信息
  548. * @return number 返回发票ID
  549. */
  550. public function setInvoiceByCondition($item = array()) {
  551. $lid = 0;
  552. if (is_array ( $item ) && ! empty ( $item )) {
  553. foreach ( $item as $key => $value ) {
  554. $this->$key = $value;
  555. }
  556. $lid = $this->update ();
  557. }
  558. return $lid;
  559. }
  560. /**
  561. * 根据参数字段批量更新相应字段(主键ID必须传)
  562. * @param array $item 相关需要更新的字段信息
  563. * @return number 返回发票ID
  564. */
  565. public function BatchInvoiceByCondition($item = array(), $iid = "") {
  566. $lid = 0;
  567. if (is_array ( $item ) && ! empty ( $item )) {
  568. foreach ( $item as $key => $value ) {
  569. $this->$key = $value;
  570. }
  571. if (! empty ( $iid )) {
  572. $condition = array (
  573. 'where' => "iid in (" . $iid . ")",
  574. 'asArray' => TRUE
  575. );
  576. $this->update ( $condition );
  577. } else
  578. $lid = $this->update ();
  579. }
  580. return $lid;
  581. }
  582. /**
  583. * 批量更新
  584. * @param string $isid
  585. * @return number
  586. */
  587. function UpdateBatchInvoice($sql = "") {
  588. if (empty ( $sql ))
  589. return 0;
  590. $query = Doo::db ()->query ( $sql );
  591. }
  592. /**
  593. * 根据管理组和发票当前状态获得打印发票数据;其中当iid为空时获取所有发票数据,反之获取1条数据
  594. * @param number $sid 管理组人员ID
  595. * @param number $status 状态 为0时获取可以打印数据,为1时获取已经完成打印数据
  596. * @param number $iid 发票ID
  597. * @return array|array() 返回所有发票数据,当iid有值时返回一条数据
  598. */
  599. public function getPrintInvoiceByManage($sid = 0, $status = 0, $iid = 0) {
  600. $list = array ();
  601. if (! empty ( $sid ) && empty ( $iid ))
  602. $list = $this->find ( array (
  603. 'where' => "status=2 and printStatus=" . $status . " and untreadStatus=0 and moldManage like '%[\"" . $sid . "\",%'",
  604. 'desc' => 'approvalTime',
  605. 'limit' => 10,
  606. 'asArray' => TRUE
  607. ) );
  608. elseif (! empty ( $sid ) && ! empty ( $iid ))
  609. $list = $this->getOne ( array (
  610. 'where' => "status=2 and printStatus=" . $status . " and untreadStatus=0 and moldManage like '%[\"" . $sid . "\",%' and iid=" . $iid,
  611. 'asArray' => TRUE
  612. ) );
  613. return $list;
  614. }
  615. /**
  616. * 获得与我相关的发票数据,其中包含 处理中,待处理,最旧入账等数据;当iid有值时获取一条关于sid的发票
  617. * @param number $sid 用户ID
  618. * @param number $iid 发票ID
  619. * @return array|array()
  620. */
  621. public function getMyInvoice($sid = 0, $iid = 0) {
  622. Doo::loadModel ( 'invoiceOperationLog' );
  623. $invoiceOperationLog = new invoiceOperationLog ();
  624. Doo::loadClass ( 'XDeode' );
  625. $XDeode = new XDeode ( 5 );
  626. $list = array ();
  627. if (! empty ( $iid )) {
  628. $list = $this->getOne ( array (
  629. 'where' => " sid=" . $sid . " and iid=" . $iid,
  630. 'asArray' => TRUE
  631. ) );
  632. } elseif (! empty ( $sid ) && empty ( $iid )) {
  633. $list ['pendingInvoice'] = array ();
  634. $list ['handleInvoice'] = array ();
  635. $list ['pendingInvoice'] = $this->find ( array ( // 加入邮件中也为 处理中 已退票
  636. 'where' => "((status=1 and untreadStatus=0) or (status=2 and printStatus=0 and untreadStatus=0) or
  637. ( status=2 and printStatus=1 and untreadStatus=1 and untreadPost=0)
  638. or (postStatus=0 and doPost=1 and status=2 and untreadStatus=0) ) and isDelete=0 and sid=" . $sid,
  639. 'desc' => 'iid',
  640. 'asArray' => TRUE
  641. ) );
  642. foreach ( $list ['pendingInvoice'] as $key => $value ) {
  643. $list ['pendingInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  644. $list ['pendingInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  645. }
  646. $list ['handleInvoice'] = $this->find ( array (
  647. // 需求变更 -去除出票状态的数据 or (status=2 and printStatus=1 and untreadStatus=0 ) or (status=2 and printStatus=1 and untreadStatus=2 )
  648. // 新加入退票中 需要邮寄的发票
  649. 'where' => "(status=3 or status=4 or (status=2 and untreadStatus=2 ) or ( status=2 and untreadStatus=1 ) ) and isDelete=0 and sid=" . $sid,
  650. 'desc' => 'iid',
  651. 'asArray' => TRUE
  652. ) );
  653. foreach ( $list ['handleInvoice'] as $key => $value ) {
  654. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  655. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  656. $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  657. }
  658. }
  659. return $list;
  660. }
  661. /**
  662. * 获得所有未入账发票
  663. * @param number $invoiceTC 发票title
  664. */
  665. public function getInvoiceByUnAccount($invoiceTC = '', $iridExist = false) {
  666. Doo::loadClass ( 'XDeode' );
  667. $XDeode = new XDeode ( 5 );
  668. $title = "";
  669. if (! empty ( $invoiceTC ))
  670. $title = ' and (invoiceTitle like "%' . $invoiceTC . '%" or invoiceCompany like "%' . $invoiceTC . '%" )';
  671. $iridString = ' and irid="" ';
  672. if ($iridExist)
  673. $iridString = ' and irid!="" ';
  674. $list = $this->find ( array ( //
  675. 'where' => "status=2 and printStatus=1 " . $iridString . " and untreadStatus=0 and ( (doPost=1 and postStatus=1) or doPost=0 ) and isDelete=0 " . $title,
  676. 'desc' => 'iid',
  677. 'limit' => 5,
  678. 'asArray' => TRUE
  679. ) );
  680. foreach ( $list as $key => $value ) {
  681. $list [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  682. }
  683. return $list;
  684. }
  685. /**
  686. * 获得与我有关可收款的发票,已经出票的发票
  687. * @param number $sid 开票人ID
  688. */
  689. public function getInvoiceByReceivables($sid = 0) {
  690. Doo::loadClass ( 'XDeode' );
  691. $XDeode = new XDeode ( 5 );
  692. $list ['handleInvoice'] = $this->find ( array (
  693. 'where' => "(status=2 and printStatus=1 and untreadStatus=0 ) and isDelete=0 and sid=" . $sid,
  694. 'desc' => 'iid',
  695. 'asArray' => TRUE
  696. ) );
  697. foreach ( $list ['handleInvoice'] as $key => $value ) {
  698. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  699. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  700. // $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  701. }
  702. return $list ['handleInvoice'];
  703. }
  704. /**
  705. * 获取需要邮寄的发票
  706. * @param integer $postStatus 0为获取需要邮寄的发票,1为获取已经邮寄的发票
  707. * @return array|array() 返回相关数据
  708. */
  709. public function getPostByInvoice($postStatus = 0, $desc = "desc") {
  710. Doo::loadClass ( 'XDeode' );
  711. $XDeode = new XDeode ( 5 );
  712. $list = $this->find ( array ( // and untreadStatus=0
  713. 'where' => "postStatus=" . $postStatus . " and printStatus=1 and doPost=1 and status=2 and isDelete=0",
  714. 'limit' => 7,
  715. $desc => 'iid',
  716. 'asArray' => TRUE
  717. ) );
  718. foreach ( $list as $key => $value ) {
  719. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  720. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  721. $list [$key] ['expressCompany'] = '';
  722. $list [$key] ['expressCom'] = '';
  723. if (! empty ( $value ['expressCompany'] )) {
  724. $express = explode ( ":", $value ['expressCompany'] );
  725. $list [$key] ['expressCompany'] = $express [0];
  726. $list [$key] ['expressCom'] = $express [1];
  727. }
  728. }
  729. return $list;
  730. }
  731. /**
  732. * 根据退票状态获得相关数据,iid有值时只获得一条数据
  733. * @param number $untreadStatus 退票状态
  734. * @param number $iid 发票ID
  735. */
  736. public function getMyInvoiceByUntreadStatus($untreadStatus = 1, $iid = 0,$limit=10,$sid=0) {
  737. Doo::loadClass ( 'XDeode' );
  738. $XDeode = new XDeode ( 5 );
  739. if (empty ( $iid )) {
  740. $list = $this->find ( array ( // printStatus=1 and
  741. 'where' => " untreadStatus=" . $untreadStatus . " and status=2 and sid=".$sid,
  742. 'limit' => $limit,
  743. 'asArray' => TRUE
  744. ) );
  745. if ($untreadStatus == 2) {
  746. Doo::loadModel ( 'invoiceOperationLog' );
  747. $invoiceOperationLog = new invoiceOperationLog ();
  748. }
  749. foreach ( $list as $key => $value ) {
  750. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  751. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  752. $list [$key] ['untreadCompany'] = '';
  753. $list [$key] ['untreadCom'] = '';
  754. if (! empty ( $value ['untreadCompany'] )) {
  755. $express = explode ( ":", $value ['untreadCompany'] );
  756. $list [$key] ['untreadCompany'] = $express [0];
  757. $list [$key] ['untreadCom'] = $express [1];
  758. }
  759. if ($untreadStatus == 2) {
  760. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  761. }
  762. $list [$key] ['lossDetail'] = array ();
  763. if ($value ['lossIid'] != 0) {
  764. $detail = $this->getOne ( array (
  765. 'where' => "iid=" . $value ['lossIid'],
  766. 'asArray' => TRUE
  767. ) );
  768. $detail ['iidKeyK'] = $XDeode->encode ( $detail ['iid'] );
  769. $list [$key] ['lossDetail'] = $detail;
  770. }
  771. }
  772. } else {
  773. $list = $this->getOne ( array (
  774. 'where' => "iid=" . $iid . " and printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2",
  775. 'asArray' => TRUE
  776. ) );
  777. }
  778. return $list;
  779. }
  780. /**
  781. * 根据退票状态获得相关数据,iid有值时只获得一条数据
  782. * @param number $untreadStatus 退票状态
  783. * @param number $iid 发票ID
  784. */
  785. public function getInvoiceByUntreadStatus($untreadStatus = 1, $iid = 0,$limit=10) {
  786. Doo::loadClass ( 'XDeode' );
  787. $XDeode = new XDeode ( 5 );
  788. if (empty ( $iid )) {
  789. $list = $this->find ( array ( // printStatus=1 and
  790. 'where' => " untreadStatus=" . $untreadStatus . " and status=2 ",
  791. 'limit' => $limit,
  792. 'asArray' => TRUE
  793. ) );
  794. if ($untreadStatus == 2) {
  795. Doo::loadModel ( 'invoiceOperationLog' );
  796. $invoiceOperationLog = new invoiceOperationLog ();
  797. }
  798. foreach ( $list as $key => $value ) {
  799. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  800. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  801. $list [$key] ['untreadCompany'] = '';
  802. $list [$key] ['untreadCom'] = '';
  803. if (! empty ( $value ['untreadCompany'] )) {
  804. $express = explode ( ":", $value ['untreadCompany'] );
  805. $list [$key] ['untreadCompany'] = $express [0];
  806. $list [$key] ['untreadCom'] = $express [1];
  807. }
  808. if ($untreadStatus == 2) {
  809. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  810. }
  811. $list [$key] ['lossDetail'] = array ();
  812. if ($value ['lossIid'] != 0) {
  813. $detail = $this->getOne ( array (
  814. 'where' => "iid=" . $value ['lossIid'],
  815. 'asArray' => TRUE
  816. ) );
  817. $detail ['iidKeyK'] = $XDeode->encode ( $detail ['iid'] );
  818. $list [$key] ['lossDetail'] = $detail;
  819. }
  820. }
  821. } else {
  822. $list = $this->getOne ( array (
  823. 'where' => "iid=" . $iid . " and printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2",
  824. 'asArray' => TRUE
  825. ) );
  826. }
  827. return $list;
  828. }
  829. /**
  830. * 审批中获取相关数据 包括最近的一条操作记录
  831. * @param number $status.
  832. * @param number $limit.
  833. */
  834. function getInvoiceByProcessStatus($limit = 10, $desc = 'desc') {
  835. Doo::loadClass ( 'XDeode' );
  836. $XDeode = new XDeode ( 5 );
  837. $list = $this->find ( array (
  838. 'where' => " (status=1 or status=2 or status=3 or status=4) and processApprovals!=''",
  839. 'limit' => $limit,
  840. $desc => 'iid',
  841. 'asArray' => TRUE
  842. ) );
  843. Doo::loadModel ( 'invoiceOperationLog' );
  844. $invoiceOperationLog = new invoiceOperationLog ();
  845. foreach ( $list as $key => $value ) {
  846. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  847. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], $value ['status'] );
  848. }
  849. return $list;
  850. }
  851. function getInvoiceByUntreadStatusPage($limit = 0, $con = "", $desc = 'desc', $olStatus = "desc",$descField="bindTime") {
  852. if (empty ( $limit ) || empty ( $con ))
  853. return array ();
  854. $listCount = $this->count ( array (
  855. 'where' => $con,
  856. 'asArray' => TRUE
  857. ) );
  858. $list = $this->find ( array (
  859. 'where' => $con,
  860. 'limit' => $limit,
  861. $desc => $descField,
  862. 'asArray' => TRUE
  863. ) ); // echo $con;
  864. Doo::loadClass ( 'XDeode' );
  865. $XDeode = new XDeode ( 5 );
  866. Doo::loadModel ( 'invoiceReceivables' );
  867. $invoiceReceivables = new invoiceReceivables ();
  868. Doo::loadModel ( 'invoiceOperationLog' );
  869. $invoiceOperationLog = new invoiceOperationLog ();
  870. foreach ( $list as $key => $value ) {
  871. $list [$key] ['count'] = $listCount;
  872. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  873. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  874. $list [$key] ['trainIdKey'] = $XDeode->encode ( $value ['trainId'] );
  875. $list [$key] ['untreadCompany'] = '';
  876. $list [$key] ['untreadCom'] = '';
  877. if (! empty ( $value ['untreadCompany'] )) {
  878. $express = explode ( ":", $value ['untreadCompany'] );
  879. $list [$key] ['untreadCompany'] = $express [0];
  880. $list [$key] ['untreadCom'] = $express [1];
  881. }
  882. $list [$key] ['irList'] = $invoiceReceivables->getInvoiceReceivablesInIridString ( $value ['irid'] );
  883. if ($olStatus == 'desc')
  884. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  885. else
  886. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], $olStatus );
  887. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  888. $list [$key] ['OL'] = $invoiceOperationLog->getInvoiceOperationInStatus ( $value ['iid'], '2,3,4' );
  889. // print_r($list [$key] ['OperationLog']);
  890. $list [$key] ['sumPrice'] = 0;
  891. if (! empty ( $list [$key] ['irList'] ))
  892. $list [$key] ['sumPrice'] = $list [$key] ['irList'] [0] ['sumPrice'];
  893. $list [$key] ['diffPrice'] = $list [$key] ['invoicePrice'];
  894. if (! empty ( $list [$key] ['irList'] [0] ['sumPrice'] ))
  895. $list [$key] ['diffPrice'] = $list [$key] ['invoicePrice'] - $list [$key] ['irList'] [0] ['sumPrice'];
  896. // $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  897. $list [$key] ['lossDetail'] = array ();
  898. if ($value ['lossIid'] != 0) {
  899. $detail = $this->getOne ( array (
  900. 'where' => "iid=" . $value ['lossIid'],
  901. 'asArray' => TRUE
  902. ) );
  903. $detail ['iidKeyK'] = $XDeode->encode ( $detail ['iid'] );
  904. $list [$key] ['lossDetail'] = $detail;
  905. }
  906. $lossDate = strtotime ( "+1 month", strtotime ( $list [$key] ['OperationLog'] ['date'] ) );
  907. $nowDate = strtotime ( 'now' );
  908. $list [$key] ['lossShow'] = false;
  909. if ($lossDate < $nowDate)
  910. $list [$key] ['lossShow'] = true;
  911. }
  912. return $list;
  913. }
  914. /**
  915. * 公司应收款汇总
  916. * @param unknown $con
  917. */
  918. function getInvoiceCompanyStatistics($con = '') {
  919. if (empty ( $con ))
  920. return array ();
  921. $sql = 'select invoicePrice,iid
  922. from ' . $this->_table . '
  923. where ' . $con . '
  924. ';
  925. $query = Doo::db ()->query ( $sql );
  926. $result = $query->fetchAll ();
  927. $iidlist = array ();
  928. $invoicePrice = 0;
  929. foreach ( $result as $value ) {
  930. array_push ( $iidlist, $value ['iid'] );
  931. $invoicePrice += $value ['invoicePrice'];
  932. }
  933. $iidString = implode ( ',', $iidlist );
  934. // 获得已收款发票
  935. if (! empty ( $iidString )) {
  936. $sql = 'select sum(receivablesPrice) as receivablesPrice
  937. from CLD_invoiceReceivables
  938. where iid in (' . $iidString . ') and bindStatus=1
  939. GROUP BY bindStatus';
  940. $query = Doo::db ()->query ( $sql );
  941. $result = $query->fetch ();
  942. } else {
  943. $result ['receivablesPrice'] = 0;
  944. }
  945. return array (
  946. 'invoicePrice' => $invoicePrice,
  947. 'receivablesPrice' => $result ['receivablesPrice'],
  948. 'diffPrice' => $invoicePrice - $result ['receivablesPrice']
  949. );
  950. }
  951. /**
  952. * 获得当前月份办事处入账完成度
  953. * @param number $cid
  954. */
  955. function getInvoiceScheduleByCid($cid = 0, $month = true) {
  956. if (empty ( $cid ))
  957. return 0;
  958. if ($month === false)
  959. $monthSql = "";
  960. else {
  961. $month = date ( "m" );
  962. $monthSql = " and Month(date)=" . $month;
  963. }
  964. // 完成打印
  965. $recordCount = $this->count ( array (
  966. 'where' => " status=2 and printStatus=1 and untreadStatus=3 " . $monthSql . " and cid =" . $cid,
  967. 'asArray' => TRUE
  968. ) );
  969. // 当月创建的发票
  970. $allCount = $this->count ( array (
  971. 'where' => " isDelete=0 " . $monthSql . " and cid =" . $cid,
  972. 'asArray' => TRUE
  973. ) );
  974. if ($recordCount == 0)
  975. return 0;
  976. return $recordCount / $allCount;
  977. }
  978. /**
  979. * 或者办事处成员的完成度
  980. */
  981. function getInvoiceScheduleByCidGroupSid($cid = 0) {
  982. if (empty ( $cid ))
  983. return array ();
  984. // 完成发票
  985. $recordCount = $this->find ( array (
  986. 'select' => 'count(*) as count,sid',
  987. 'where' => " status=2 and printStatus=1 and untreadStatus=3 and cid =" . $cid,
  988. 'groupby' => 'sid',
  989. 'asArray' => TRUE
  990. ) );
  991. // 当月创建的发票
  992. $allCount = $this->find ( array (
  993. 'select' => 'count(*) as count,sid',
  994. 'where' => " isDelete=0 and cid =" . $cid,
  995. 'groupby' => 'sid',
  996. 'asArray' => TRUE
  997. ) );
  998. $list = array ();
  999. foreach ( $recordCount as $key => $value ) {
  1000. foreach ( $allCount as $k => $v ) {
  1001. if ($value ['sid'] == $v ['sid']) {
  1002. if ($value ['count'] == 0)
  1003. $list [$value ['sid']] = 0;
  1004. else
  1005. $list [$value ['sid']] = ( int ) (($value ['count'] / $v ['count']) * 100);
  1006. }
  1007. }
  1008. }
  1009. return $list;
  1010. }
  1011. /**
  1012. * 获得个人最新发票信息
  1013. */
  1014. function getInvoiceByNew($sid = 0) {
  1015. if (empty ( $sid ))
  1016. return array ();
  1017. $Detail = $this->getOne ( array (
  1018. 'where' => "sid=" . $sid,
  1019. 'desc' => 'iid',
  1020. 'asArray' => TRUE
  1021. ) );
  1022. return $Detail;
  1023. }
  1024. /**
  1025. * 加密或解密指定字符串
  1026. *
  1027. * @param string $string 要加密或解密的字符串
  1028. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  1029. * @param string $key 加解密的key
  1030. * @param $expiry 超时值
  1031. *
  1032. */
  1033. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  1034. $ckey_length = 4;
  1035. if (! $key) {
  1036. $key = $this->INVOICEKEY;
  1037. }
  1038. $key = md5 ( $key );
  1039. $keya = md5 ( substr ( $key, 0, 16 ) );
  1040. $keyb = md5 ( substr ( $key, 16, 16 ) );
  1041. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  1042. $cryptkey = $keya . md5 ( $keya . $keyc );
  1043. $key_length = strlen ( $cryptkey );
  1044. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  1045. $string_length = strlen ( $string );
  1046. $result = '';
  1047. $box = range ( 0, 255 );
  1048. $rndkey = array ();
  1049. for($i = 0; $i <= 255; $i ++) {
  1050. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  1051. }
  1052. for($j = $i = 0; $i < 256; $i ++) {
  1053. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  1054. $tmp = $box [$i];
  1055. $box [$i] = $box [$j];
  1056. $box [$j] = $tmp;
  1057. }
  1058. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  1059. $a = ($a + 1) % 256;
  1060. $j = ($j + $box [$a]) % 256;
  1061. $tmp = $box [$a];
  1062. $box [$a] = $box [$j];
  1063. $box [$j] = $tmp;
  1064. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  1065. }
  1066. if ($operation == 'DECODE') {
  1067. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  1068. return substr ( $result, 26 );
  1069. } else {
  1070. return '';
  1071. }
  1072. } else {
  1073. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  1074. }
  1075. }
  1076. }
  1077. ?>