invoice.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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. $isFinance=false;
  311. foreach ($result as $key=>$value){
  312. $result [$key] ['irDetail'] =$invoiceReceivables->getInvoiceReceivablesByIridList($value ['irid']);
  313. foreach ($result [$key] ['irDetail'] as $k=>$v){
  314. if($v['receivablesBank']=='财务录入'){
  315. $isFinance=true;
  316. break;
  317. }
  318. }
  319. $result [$key] ['isFinance'] =$isFinance;
  320. $result [$key] ['iidKey'] = $XDeode->encode ( $value ['iid'] );
  321. }
  322. return $result;
  323. // $condition = array (
  324. // 'where' => "trainId = " . $trainId . " and status=2 and printStatus=1 and (untreadStatus=0 or untreadStatus=3)",
  325. // 'asArray' => TRUE
  326. // );
  327. // $detail = $this->find ( $condition );
  328. // return $detail;
  329. }
  330. //办事处收款 有问题
  331. function sumOfInvoiceByItid($itidSql=0){
  332. $sql = 'select sum(a.invoicePrice) as invoicePrice ,a.trainId as trainId,a.iid
  333. from CLD_RIExtend as b left join ' . $this->_table . ' as a on (a.iid=b.iid)
  334. where (a.trainId in (' . $itidSql . ') and a.status=2 and a.printStatus=1 and a.untreadStatus=3) or b.RIstatus=1
  335. GROUP BY a.trainId';
  336. //echo $sql;
  337. $query = Doo::db ()->query ( $sql );
  338. return $result = $query->fetchAll ();
  339. }
  340. /**
  341. * 根据多个iid获得多个发票
  342. * @param number $iid
  343. */
  344. function getInvoiceInIid($iid = 0, $select = '') {
  345. if (empty ( $iid ))
  346. die ( 'illegal request' );
  347. $condition = array (
  348. 'where' => "iid in ( " . $iid . ")",
  349. 'asArray' => TRUE
  350. );
  351. if (! empty ( $select ))
  352. $condition += array (
  353. 'select' => $select
  354. );
  355. $list = $this->find ( $condition );
  356. Doo::loadClass ( 'XDeode' );
  357. $XDeode = new XDeode ( 5 );
  358. foreach ( $list as $key => $value ) {
  359. $list [$key] ['iidKey'] = $XDeode->encode ( $value ['iid'] );
  360. }
  361. return $list;
  362. }
  363. function setInvoiceLoss($iid = "") {
  364. if (empty ( $iid ))
  365. return 0;
  366. $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`)
  367. 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;
  368. $query = Doo::db ()->query ( $sql );
  369. return Doo::db ()->lastInsertId ();
  370. }
  371. function getInvoiceByInvoiceNo($invoiceNo = '') {
  372. if (empty ( $invoiceNo ))
  373. return array ();
  374. $detail = $this->getOne ( array (
  375. 'where' => "invoiceNo= '" . $invoiceNo . "'",
  376. 'asArray' => TRUE
  377. ) );
  378. return $detail;
  379. }
  380. /**
  381. * 根据标题或者开票单位获得相关信息 - 准确查询
  382. * @param string $title 查询条件
  383. * @param string $query 默认模糊查询
  384. */
  385. public function getInvoiceByTitle($title = "", $query = "FUZZY", $cond = '', $fkq = false) {
  386. $detail = array ();
  387. if (! empty ( $title )) {
  388. $condition = array (
  389. 'asArray' => TRUE
  390. );
  391. if ($query == $this->FUZZY)
  392. $condition += array (
  393. 'where' => " (invoiceTitle like '%" . $title . "%' or invoiceCompany like '%" . $title . "%') " . $cond
  394. );
  395. else
  396. $condition += array (
  397. 'where' => "(invoiceTitle = '" . $title . "' or invoiceCompany= '" . $title . "') " . $cond
  398. );
  399. Doo::loadClass ( 'XDeode' );
  400. $XDeode = new XDeode ( 5 );
  401. if ($fkq) {
  402. $detail = $this->find ( $condition );
  403. foreach ( $detail as $key => $value ) {
  404. $detail [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  405. $detail [$key] ['iidKeyXD'] = $XDeode->encode ( $value ['iid'] );
  406. }
  407. } else {
  408. $detail = $this->getOne ( $condition );
  409. if (! empty ( $detail )) {
  410. $detail ['iidKey'] = $this->authcode ( $detail ['iid'], '' );
  411. $detail ['iidKeyXD'] = $XDeode->encode ( $detail ['iid'] );
  412. }
  413. }
  414. }
  415. return $detail;
  416. }
  417. public function getInvoiceByIsid($iid = "", $sid = 0) {
  418. if (empty ( $iid ) || empty ( $sid ))
  419. return array ();
  420. Doo::loadClass ( 'XDeode' );
  421. $XDeode = new XDeode ( 5 );
  422. $iid = $XDeode->decode ( $iid );
  423. if (! is_numeric ( $iid ))
  424. return array ();
  425. $detail = $this->getOne ( array (
  426. 'where' => "iid= '" . $iid . "'", // and sid=".$sid,
  427. 'asArray' => TRUE
  428. ) );
  429. $detail ['expressCompany'] = '';
  430. $detail ['expressCom'] = '';
  431. $detail ['invoiceTraining'] = array ();
  432. if (! empty ( $detail ['expressCompany'] )) {
  433. $express = explode ( ":", $detail ['expressCompany'] );
  434. $detail ['expressCompany'] = $express [0];
  435. $detail ['expressCom'] = $express [1];
  436. }
  437. // 培训班
  438. if ($detail ['settlementType'] == 1) {
  439. Doo::loadModel ( 'invoiceTraining' );
  440. $invoiceTraining = new invoiceTraining ();
  441. $detail ['invoiceTraining'] = $invoiceTraining->getInvoiceTrainingByItid ( $detail ['trainId'] );
  442. }
  443. $detail ['iidKeyXD'] = $this->authcode ( $detail ['iid'], '' );
  444. return $detail;
  445. }
  446. /**
  447. * 统计培训班开票
  448. */
  449. function sumOfinvoiceTrain($itidSql) {
  450. $sql = 'select sum(b.invoicePrice) as invoicePrice ,b.trainId as trainId
  451. from CLD_invoiceStore as a left join ' . $this->_table . ' as b on (a.iid=b.iid)
  452. 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
  453. GROUP BY b.trainId';
  454. $query = Doo::db ()->query ( $sql );
  455. return $result = $query->fetchAll ();
  456. }
  457. /**
  458. * 公司收款汇总
  459. * @param unknown $itidSql
  460. */
  461. function sumOfInvoiceRecelvablesCompany($itidSql) {
  462. $sql = 'select sum(b.receivablesPrice) as receivablesPrice ,a.trainId,count(*) as count
  463. from ' . $this->_table . ' as a left join CLD_invoiceReceivables as b on (a.iid=b.iid)
  464. where a.trainId in (' . $itidSql . ') and b.receivablesBank!="财务录入" and a.status=2 and a.printStatus=1 and ( a.untreadStatus=3) and a.irid!=""
  465. GROUP BY a.trainId';
  466. $query = Doo::db ()->query ( $sql );
  467. return $result = $query->fetchAll ();
  468. }
  469. //
  470. function sumOfInvoiceRecelvablesTrain($itidSql) {
  471. // $sql = 'select sum(b.receivablesPrice) as receivablesPrice ,a.trainId,count(*) as count
  472. // from ' . $this->_table . ' as a left join CLD_invoiceReceivables as b on find_in_set(a.irid,b.irid)
  473. // where a.trainId in (' . $itidSql . ') and a.status=2 and a.printStatus=1 and (a.untreadStatus =0 or a.untreadStatus=3) and a.irid!=""
  474. // GROUP BY a.trainId';
  475. $sql = 'select sum(b.receivablesPrice) as receivablesPrice ,a.trainId,count(*) as count
  476. from ' . $this->_table . ' as a left join CLD_invoiceReceivables as b on (a.iid=b.iid)
  477. where a.trainId in (' . $itidSql . ') and a.status=2 and a.printStatus=1 and (a.untreadStatus =0 or a.untreadStatus=3) and a.irid!=""
  478. GROUP BY a.trainId';
  479. $query = Doo::db ()->query ( $sql );
  480. return $result = $query->fetchAll ();
  481. }
  482. // 获得培训班发票
  483. function getInvoiceByTrainingCount($itid = 0) {
  484. if (empty ( $itid ))
  485. return 0;
  486. $sql = 'select count(*) as count from CLD_invoiceStore as a left join ' . $this->_table . ' as b on (a.iid=b.iid)
  487. 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 )';
  488. //echo $sql;
  489. //echo $sql;
  490. $query = Doo::db ()->query ( $sql );
  491. $result = $query->fetchAll ();
  492. return $result [0] ['count'];
  493. // $detail = $this->count ( array (
  494. // 'where' => "trainId= '" . $itid . "' and untreadStatus!=2 and isDelete=0", // and sid=".$sid,
  495. // 'asArray' => TRUE
  496. // ) );
  497. // return $detail;
  498. }
  499. /**
  500. * 根据仓库ID获得发票
  501. * @param string $Istoreid
  502. * @return unknown
  503. */
  504. public function getInvoiceByIstoreid($Istoreid = "", $select = "") {
  505. if (empty ( $Istoreid ))
  506. return array ();
  507. $condition = array (
  508. 'where' => "isid in ( " . $Istoreid . ")",
  509. 'asArray' => TRUE
  510. );
  511. if (! empty ( $select ))
  512. $condition += array (
  513. 'select' => $select
  514. );
  515. $detail = $this->find ( $condition );
  516. return $detail;
  517. }
  518. /**
  519. * 获取单个可以打印的发票数据
  520. * @param number $iid 发票id
  521. * @param number $select 需要获取的字段,为空获取全部数据
  522. * @return array|array() 返回发票数据
  523. */
  524. public function getInvoiceByPrint($iid = 0, $select = "") {
  525. $condition = array (
  526. 'where' => "iid= '" . $iid . "' and status=2 and untreadStatus=0 ",
  527. 'asArray' => TRUE
  528. );
  529. if (! empty ( $select ))
  530. $condition += array (
  531. 'select' => $select
  532. );
  533. $detail = array ();
  534. if (! empty ( $iid ) && is_numeric ( $iid ))
  535. $detail = $this->getOne ( $condition );
  536. return $detail;
  537. }
  538. /**
  539. * 添加一个发票并进入审批状态
  540. * @param array $item 发票相关数据
  541. * @return number 返回发票ID
  542. */
  543. public function addInvoice($item = array()) {
  544. $lid = 0;
  545. if (is_array ( $item ) && ! empty ( $item )) {
  546. foreach ( $item as $key => $value ) {
  547. $this->$key = $value;
  548. }
  549. $lid = $this->insert ();
  550. }
  551. return $lid;
  552. }
  553. /**
  554. * 根据参数字段更新相应字段(主键ID必须传)
  555. * @param array $item 相关需要更新的字段信息
  556. * @return number 返回发票ID
  557. */
  558. public function setInvoiceByCondition($item = array()) {
  559. $lid = 0;
  560. if (is_array ( $item ) && ! empty ( $item )) {
  561. foreach ( $item as $key => $value ) {
  562. $this->$key = $value;
  563. }
  564. $lid = $this->update ();
  565. }
  566. return $lid;
  567. }
  568. /**
  569. * 根据参数字段批量更新相应字段(主键ID必须传)
  570. * @param array $item 相关需要更新的字段信息
  571. * @return number 返回发票ID
  572. */
  573. public function BatchInvoiceByCondition($item = array(), $iid = "") {
  574. $lid = 0;
  575. if (is_array ( $item ) && ! empty ( $item )) {
  576. foreach ( $item as $key => $value ) {
  577. $this->$key = $value;
  578. }
  579. if (! empty ( $iid )) {
  580. $condition = array (
  581. 'where' => "iid in (" . $iid . ")",
  582. 'asArray' => TRUE
  583. );
  584. $this->update ( $condition );
  585. } else
  586. $lid = $this->update ();
  587. }
  588. return $lid;
  589. }
  590. /**
  591. * 批量更新
  592. * @param string $isid
  593. * @return number
  594. */
  595. function UpdateBatchInvoice($sql = "") {
  596. if (empty ( $sql ))
  597. return 0;
  598. $query = Doo::db ()->query ( $sql );
  599. }
  600. /**
  601. * 根据管理组和发票当前状态获得打印发票数据;其中当iid为空时获取所有发票数据,反之获取1条数据
  602. * @param number $sid 管理组人员ID
  603. * @param number $status 状态 为0时获取可以打印数据,为1时获取已经完成打印数据
  604. * @param number $iid 发票ID
  605. * @return array|array() 返回所有发票数据,当iid有值时返回一条数据
  606. */
  607. public function getPrintInvoiceByManage($sid = 0, $status = 0, $iid = 0) {
  608. $list = array ();
  609. if (! empty ( $sid ) && empty ( $iid ))
  610. $list = $this->find ( array (
  611. 'where' => "status=2 and printStatus=" . $status . " and untreadStatus=0 and moldManage like '%[\"" . $sid . "\",%'",
  612. 'desc' => 'approvalTime',
  613. 'limit' => 10,
  614. 'asArray' => TRUE
  615. ) );
  616. elseif (! empty ( $sid ) && ! empty ( $iid ))
  617. $list = $this->getOne ( array (
  618. 'where' => "status=2 and printStatus=" . $status . " and untreadStatus=0 and moldManage like '%[\"" . $sid . "\",%' and iid=" . $iid,
  619. 'asArray' => TRUE
  620. ) );
  621. return $list;
  622. }
  623. /**
  624. * 获得与我相关的发票数据,其中包含 处理中,待处理,最旧入账等数据;当iid有值时获取一条关于sid的发票
  625. * @param number $sid 用户ID
  626. * @param number $iid 发票ID
  627. * @return array|array()
  628. */
  629. public function getMyInvoice($sid = 0, $iid = 0) {
  630. Doo::loadModel ( 'invoiceOperationLog' );
  631. $invoiceOperationLog = new invoiceOperationLog ();
  632. Doo::loadClass ( 'XDeode' );
  633. $XDeode = new XDeode ( 5 );
  634. $list = array ();
  635. if (! empty ( $iid )) {
  636. $list = $this->getOne ( array (
  637. 'where' => " sid=" . $sid . " and iid=" . $iid,
  638. 'asArray' => TRUE
  639. ) );
  640. } elseif (! empty ( $sid ) && empty ( $iid )) {
  641. $list ['pendingInvoice'] = array ();
  642. $list ['handleInvoice'] = array ();
  643. $list ['pendingInvoice'] = $this->find ( array ( // 加入邮件中也为 处理中 已退票
  644. 'where' => "((status=1 and untreadStatus=0) or (status=2 and printStatus=0 and untreadStatus=0) or
  645. ( status=2 and printStatus=1 and untreadStatus=1 and untreadPost=0)
  646. or (postStatus=0 and doPost=1 and status=2 and untreadStatus=0) ) and isDelete=0 and sid=" . $sid,
  647. 'desc' => 'iid',
  648. 'asArray' => TRUE
  649. ) );
  650. foreach ( $list ['pendingInvoice'] as $key => $value ) {
  651. $list ['pendingInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  652. $list ['pendingInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  653. }
  654. $list ['handleInvoice'] = $this->find ( array (
  655. // 需求变更 -去除出票状态的数据 or (status=2 and printStatus=1 and untreadStatus=0 ) or (status=2 and printStatus=1 and untreadStatus=2 )
  656. // 新加入退票中 需要邮寄的发票
  657. 'where' => "(status=3 or status=4 or (status=2 and untreadStatus=2 ) or ( status=2 and untreadStatus=1 ) ) and isDelete=0 and sid=" . $sid,
  658. 'desc' => 'iid',
  659. 'asArray' => TRUE
  660. ) );
  661. foreach ( $list ['handleInvoice'] as $key => $value ) {
  662. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  663. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  664. $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  665. }
  666. }
  667. return $list;
  668. }
  669. /**
  670. * 获得所有未入账发票
  671. * @param number $invoiceTC 发票title
  672. */
  673. public function getInvoiceByUnAccount($invoiceTC = '', $iridExist = false) {
  674. Doo::loadClass ( 'XDeode' );
  675. $XDeode = new XDeode ( 5 );
  676. $title = "";
  677. if (! empty ( $invoiceTC ))
  678. $title = ' and (invoiceTitle like "%' . $invoiceTC . '%" or invoiceCompany like "%' . $invoiceTC . '%" )';
  679. $iridString = ' and irid="" ';
  680. if ($iridExist)
  681. $iridString = ' and irid!="" ';
  682. $list = $this->find ( array ( //
  683. 'where' => "status=2 and printStatus=1 " . $iridString . " and untreadStatus=0 and ( (doPost=1 and postStatus=1) or doPost=0 ) and isDelete=0 " . $title,
  684. 'desc' => 'iid',
  685. 'limit' => 5,
  686. 'asArray' => TRUE
  687. ) );
  688. foreach ( $list as $key => $value ) {
  689. $list [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  690. }
  691. return $list;
  692. }
  693. /**
  694. * 获得与我有关可收款的发票,已经出票的发票
  695. * @param number $sid 开票人ID
  696. */
  697. public function getInvoiceByReceivables($sid = 0) {
  698. Doo::loadClass ( 'XDeode' );
  699. $XDeode = new XDeode ( 5 );
  700. $list ['handleInvoice'] = $this->find ( array (
  701. 'where' => "(status=2 and printStatus=1 and untreadStatus=0 ) and isDelete=0 and sid=" . $sid,
  702. 'desc' => 'iid',
  703. 'asArray' => TRUE
  704. ) );
  705. foreach ( $list ['handleInvoice'] as $key => $value ) {
  706. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  707. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  708. // $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  709. }
  710. return $list ['handleInvoice'];
  711. }
  712. /**
  713. * 获取需要邮寄的发票
  714. * @param integer $postStatus 0为获取需要邮寄的发票,1为获取已经邮寄的发票
  715. * @return array|array() 返回相关数据
  716. */
  717. public function getPostByInvoice($postStatus = 0, $desc = "desc") {
  718. Doo::loadClass ( 'XDeode' );
  719. $XDeode = new XDeode ( 5 );
  720. $list = $this->find ( array ( // and untreadStatus=0
  721. 'where' => "postStatus=" . $postStatus . " and printStatus=1 and doPost=1 and status=2 and isDelete=0",
  722. 'limit' => 7,
  723. $desc => 'iid',
  724. 'asArray' => TRUE
  725. ) );
  726. foreach ( $list as $key => $value ) {
  727. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  728. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  729. $list [$key] ['expressCompany'] = '';
  730. $list [$key] ['expressCom'] = '';
  731. if (! empty ( $value ['expressCompany'] )) {
  732. $express = explode ( ":", $value ['expressCompany'] );
  733. $list [$key] ['expressCompany'] = $express [0];
  734. $list [$key] ['expressCom'] = $express [1];
  735. }
  736. }
  737. return $list;
  738. }
  739. /**
  740. * 根据退票状态获得相关数据,iid有值时只获得一条数据
  741. * @param number $untreadStatus 退票状态
  742. * @param number $iid 发票ID
  743. */
  744. public function getMyInvoiceByUntreadStatus($untreadStatus = 1, $iid = 0,$limit=10,$sid=0) {
  745. Doo::loadClass ( 'XDeode' );
  746. $XDeode = new XDeode ( 5 );
  747. if (empty ( $iid )) {
  748. $list = $this->find ( array ( // printStatus=1 and
  749. 'where' => " untreadStatus=" . $untreadStatus . " and status=2 and sid=".$sid,
  750. 'limit' => $limit,
  751. 'asArray' => TRUE
  752. ) );
  753. if ($untreadStatus == 2) {
  754. Doo::loadModel ( 'invoiceOperationLog' );
  755. $invoiceOperationLog = new invoiceOperationLog ();
  756. }
  757. foreach ( $list as $key => $value ) {
  758. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  759. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  760. $list [$key] ['untreadCompany'] = '';
  761. $list [$key] ['untreadCom'] = '';
  762. if (! empty ( $value ['untreadCompany'] )) {
  763. $express = explode ( ":", $value ['untreadCompany'] );
  764. $list [$key] ['untreadCompany'] = $express [0];
  765. $list [$key] ['untreadCom'] = $express [1];
  766. }
  767. if ($untreadStatus == 2) {
  768. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  769. }
  770. $list [$key] ['lossDetail'] = array ();
  771. if ($value ['lossIid'] != 0) {
  772. $detail = $this->getOne ( array (
  773. 'where' => "iid=" . $value ['lossIid'],
  774. 'asArray' => TRUE
  775. ) );
  776. $detail ['iidKeyK'] = $XDeode->encode ( $detail ['iid'] );
  777. $list [$key] ['lossDetail'] = $detail;
  778. }
  779. }
  780. } else {
  781. $list = $this->getOne ( array (
  782. 'where' => "iid=" . $iid . " and printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2",
  783. 'asArray' => TRUE
  784. ) );
  785. }
  786. return $list;
  787. }
  788. /**
  789. * 根据退票状态获得相关数据,iid有值时只获得一条数据
  790. * @param number $untreadStatus 退票状态
  791. * @param number $iid 发票ID
  792. */
  793. public function getInvoiceByUntreadStatus($untreadStatus = 1, $iid = 0,$limit=10) {
  794. Doo::loadClass ( 'XDeode' );
  795. $XDeode = new XDeode ( 5 );
  796. if (empty ( $iid )) {
  797. $list = $this->find ( array ( // printStatus=1 and
  798. 'where' => " untreadStatus=" . $untreadStatus . " and status=2 ",
  799. 'limit' => $limit,
  800. 'asArray' => TRUE
  801. ) );
  802. if ($untreadStatus == 2) {
  803. Doo::loadModel ( 'invoiceOperationLog' );
  804. $invoiceOperationLog = new invoiceOperationLog ();
  805. }
  806. foreach ( $list as $key => $value ) {
  807. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  808. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  809. $list [$key] ['untreadCompany'] = '';
  810. $list [$key] ['untreadCom'] = '';
  811. if (! empty ( $value ['untreadCompany'] )) {
  812. $express = explode ( ":", $value ['untreadCompany'] );
  813. $list [$key] ['untreadCompany'] = $express [0];
  814. $list [$key] ['untreadCom'] = $express [1];
  815. }
  816. if ($untreadStatus == 2) {
  817. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  818. }
  819. $list [$key] ['lossDetail'] = array ();
  820. if ($value ['lossIid'] != 0) {
  821. $detail = $this->getOne ( array (
  822. 'where' => "iid=" . $value ['lossIid'],
  823. 'asArray' => TRUE
  824. ) );
  825. $detail ['iidKeyK'] = $XDeode->encode ( $detail ['iid'] );
  826. $list [$key] ['lossDetail'] = $detail;
  827. }
  828. }
  829. } else {
  830. $list = $this->getOne ( array (
  831. 'where' => "iid=" . $iid . " and printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2",
  832. 'asArray' => TRUE
  833. ) );
  834. }
  835. return $list;
  836. }
  837. /**
  838. * 审批中获取相关数据 包括最近的一条操作记录
  839. * @param number $status.
  840. * @param number $limit.
  841. */
  842. function getInvoiceByProcessStatus($limit = 10, $desc = 'desc') {
  843. Doo::loadClass ( 'XDeode' );
  844. $XDeode = new XDeode ( 5 );
  845. $list = $this->find ( array (
  846. 'where' => " (status=1 or status=2 or status=3 or status=4) and processApprovals!=''",
  847. 'limit' => $limit,
  848. $desc => 'iid',
  849. 'asArray' => TRUE
  850. ) );
  851. Doo::loadModel ( 'invoiceOperationLog' );
  852. $invoiceOperationLog = new invoiceOperationLog ();
  853. foreach ( $list as $key => $value ) {
  854. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  855. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], $value ['status'] );
  856. }
  857. return $list;
  858. }
  859. function getInvoiceByUntreadStatusPage($limit = 0, $con = "", $desc = 'desc', $olStatus = "desc",$descField="bindTime") {
  860. if (empty ( $limit ) || empty ( $con ))
  861. return array ();
  862. $listCount = $this->count ( array (
  863. 'where' => $con,
  864. 'asArray' => TRUE
  865. ) );
  866. $list = $this->find ( array (
  867. 'where' => $con,
  868. 'limit' => $limit,
  869. $desc => $descField,
  870. 'asArray' => TRUE
  871. ) ); // echo $con;
  872. Doo::loadClass ( 'XDeode' );
  873. $XDeode = new XDeode ( 5 );
  874. Doo::loadModel ( 'invoiceReceivables' );
  875. $invoiceReceivables = new invoiceReceivables ();
  876. Doo::loadModel ( 'invoiceOperationLog' );
  877. $invoiceOperationLog = new invoiceOperationLog ();
  878. foreach ( $list as $key => $value ) {
  879. $list [$key] ['count'] = $listCount;
  880. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  881. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  882. $list [$key] ['trainIdKey'] = $XDeode->encode ( $value ['trainId'] );
  883. $list [$key] ['untreadCompany'] = '';
  884. $list [$key] ['untreadCom'] = '';
  885. if (! empty ( $value ['untreadCompany'] )) {
  886. $express = explode ( ":", $value ['untreadCompany'] );
  887. $list [$key] ['untreadCompany'] = $express [0];
  888. $list [$key] ['untreadCom'] = $express [1];
  889. }
  890. $list [$key] ['irList'] = $invoiceReceivables->getInvoiceReceivablesInIridString ( $value ['irid'] );
  891. if ($olStatus == 'desc')
  892. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  893. else
  894. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], $olStatus );
  895. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  896. $list [$key] ['OL'] = $invoiceOperationLog->getInvoiceOperationInStatus ( $value ['iid'], '2,3,4' );
  897. // print_r($list [$key] ['OperationLog']);
  898. $list [$key] ['sumPrice'] = 0;
  899. if (! empty ( $list [$key] ['irList'] ))
  900. $list [$key] ['sumPrice'] = $list [$key] ['irList'] [0] ['sumPrice'];
  901. $list [$key] ['diffPrice'] = $list [$key] ['invoicePrice'];
  902. if (! empty ( $list [$key] ['irList'] [0] ['sumPrice'] ))
  903. $list [$key] ['diffPrice'] = $list [$key] ['invoicePrice'] - $list [$key] ['irList'] [0] ['sumPrice'];
  904. // $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  905. $list [$key] ['lossDetail'] = array ();
  906. if ($value ['lossIid'] != 0) {
  907. $detail = $this->getOne ( array (
  908. 'where' => "iid=" . $value ['lossIid'],
  909. 'asArray' => TRUE
  910. ) );
  911. $detail ['iidKeyK'] = $XDeode->encode ( $detail ['iid'] );
  912. $list [$key] ['lossDetail'] = $detail;
  913. }
  914. $lossDate = strtotime ( "+1 month", strtotime ( $list [$key] ['OperationLog'] ['date'] ) );
  915. $nowDate = strtotime ( 'now' );
  916. $list [$key] ['lossShow'] = false;
  917. if ($lossDate < $nowDate)
  918. $list [$key] ['lossShow'] = true;
  919. }
  920. return $list;
  921. }
  922. /**
  923. * 公司应收款汇总
  924. * @param unknown $con
  925. */
  926. function getInvoiceCompanyStatistics($con = '') {
  927. if (empty ( $con ))
  928. return array ();
  929. $sql = 'select invoicePrice,iid
  930. from ' . $this->_table . '
  931. where ' . $con . '
  932. ';
  933. $query = Doo::db ()->query ( $sql );
  934. $result = $query->fetchAll ();
  935. $iidlist = array ();
  936. $invoicePrice = 0;
  937. foreach ( $result as $value ) {
  938. array_push ( $iidlist, $value ['iid'] );
  939. $invoicePrice += $value ['invoicePrice'];
  940. }
  941. $iidString = implode ( ',', $iidlist );
  942. // 获得已收款发票
  943. if (! empty ( $iidString )) {
  944. $sql = 'select sum(receivablesPrice) as receivablesPrice
  945. from CLD_invoiceReceivables
  946. where iid in (' . $iidString . ') and bindStatus=1
  947. GROUP BY bindStatus';
  948. $query = Doo::db ()->query ( $sql );
  949. $result = $query->fetch ();
  950. } else {
  951. $result ['receivablesPrice'] = 0;
  952. }
  953. return array (
  954. 'invoicePrice' => $invoicePrice,
  955. 'receivablesPrice' => $result ['receivablesPrice'],
  956. 'diffPrice' => $invoicePrice - $result ['receivablesPrice']
  957. );
  958. }
  959. /**
  960. * 获得当前月份办事处入账完成度
  961. * @param number $cid
  962. */
  963. function getInvoiceScheduleByCid($cid = 0, $month = true) {
  964. if (empty ( $cid ))
  965. return 0;
  966. if ($month === false)
  967. $monthSql = "";
  968. else {
  969. $month = date ( "m" );
  970. $monthSql = " and Month(date)=" . $month;
  971. }
  972. // 完成打印
  973. $recordCount = $this->count ( array (
  974. 'where' => " status=2 and printStatus=1 and untreadStatus=3 " . $monthSql . " and cid =" . $cid,
  975. 'asArray' => TRUE
  976. ) );
  977. // 当月创建的发票
  978. $allCount = $this->count ( array (
  979. 'where' => " isDelete=0 " . $monthSql . " and cid =" . $cid,
  980. 'asArray' => TRUE
  981. ) );
  982. if ($recordCount == 0)
  983. return 0;
  984. return $recordCount / $allCount;
  985. }
  986. /**
  987. * 或者办事处成员的完成度
  988. */
  989. function getInvoiceScheduleByCidGroupSid($cid = 0) {
  990. if (empty ( $cid ))
  991. return array ();
  992. // 完成发票
  993. $recordCount = $this->find ( array (
  994. 'select' => 'count(*) as count,sid',
  995. 'where' => " status=2 and printStatus=1 and untreadStatus=3 and cid =" . $cid,
  996. 'groupby' => 'sid',
  997. 'asArray' => TRUE
  998. ) );
  999. // 当月创建的发票
  1000. $allCount = $this->find ( array (
  1001. 'select' => 'count(*) as count,sid',
  1002. 'where' => " isDelete=0 and cid =" . $cid,
  1003. 'groupby' => 'sid',
  1004. 'asArray' => TRUE
  1005. ) );
  1006. $list = array ();
  1007. foreach ( $recordCount as $key => $value ) {
  1008. foreach ( $allCount as $k => $v ) {
  1009. if ($value ['sid'] == $v ['sid']) {
  1010. if ($value ['count'] == 0)
  1011. $list [$value ['sid']] = 0;
  1012. else
  1013. $list [$value ['sid']] = ( int ) (($value ['count'] / $v ['count']) * 100);
  1014. }
  1015. }
  1016. }
  1017. return $list;
  1018. }
  1019. /**
  1020. * 获得个人最新发票信息
  1021. */
  1022. function getInvoiceByNew($sid = 0) {
  1023. if (empty ( $sid ))
  1024. return array ();
  1025. $Detail = $this->getOne ( array (
  1026. 'where' => "sid=" . $sid,
  1027. 'desc' => 'iid',
  1028. 'asArray' => TRUE
  1029. ) );
  1030. return $Detail;
  1031. }
  1032. /**
  1033. * 加密或解密指定字符串
  1034. *
  1035. * @param string $string 要加密或解密的字符串
  1036. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  1037. * @param string $key 加解密的key
  1038. * @param $expiry 超时值
  1039. *
  1040. */
  1041. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  1042. $ckey_length = 4;
  1043. if (! $key) {
  1044. $key = $this->INVOICEKEY;
  1045. }
  1046. $key = md5 ( $key );
  1047. $keya = md5 ( substr ( $key, 0, 16 ) );
  1048. $keyb = md5 ( substr ( $key, 16, 16 ) );
  1049. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  1050. $cryptkey = $keya . md5 ( $keya . $keyc );
  1051. $key_length = strlen ( $cryptkey );
  1052. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  1053. $string_length = strlen ( $string );
  1054. $result = '';
  1055. $box = range ( 0, 255 );
  1056. $rndkey = array ();
  1057. for($i = 0; $i <= 255; $i ++) {
  1058. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  1059. }
  1060. for($j = $i = 0; $i < 256; $i ++) {
  1061. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  1062. $tmp = $box [$i];
  1063. $box [$i] = $box [$j];
  1064. $box [$j] = $tmp;
  1065. }
  1066. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  1067. $a = ($a + 1) % 256;
  1068. $j = ($j + $box [$a]) % 256;
  1069. $tmp = $box [$a];
  1070. $box [$a] = $box [$j];
  1071. $box [$j] = $tmp;
  1072. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  1073. }
  1074. if ($operation == 'DECODE') {
  1075. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  1076. return substr ( $result, 26 );
  1077. } else {
  1078. return '';
  1079. }
  1080. } else {
  1081. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  1082. }
  1083. }
  1084. }
  1085. ?>