invoice.php 36 KB

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