invoice.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. /**
  13. *
  14. * @var integer $iid 发票ID
  15. */
  16. public $iid;
  17. /**
  18. *
  19. * @var integer $status 审批状态
  20. */
  21. public $status;
  22. /**
  23. *
  24. * @var string $invoiceManage 当前审批组人员
  25. */
  26. public $printStatus;
  27. public $postStatus;
  28. public $invoiceManage;
  29. /**
  30. *
  31. * @var string $pendingApprovals 当前需要审批的人
  32. */
  33. public $pendingApprovals;
  34. /**
  35. *
  36. * @var string $processApprovals 已经审批过的人员
  37. */
  38. public $processApprovals;
  39. /**
  40. *
  41. * @var string $invoiceSerial 发票单号
  42. */
  43. public $invoiceSerial;
  44. /**
  45. *
  46. * @var integer $invoicePrice 发票金额
  47. */
  48. public $invoicePrice;
  49. /**
  50. * 管理组人员:发票打印,
  51. * @var unknown
  52. */
  53. public $moldManage;
  54. /**
  55. *
  56. * @var integer $cid 办事处ID
  57. */
  58. public $cid;
  59. /**
  60. *
  61. * @var string $categoryName 办事处名称
  62. */
  63. public $categoryName;
  64. /**
  65. *
  66. * @var integer $sid 提交发票人ID
  67. */
  68. public $sid;
  69. /**
  70. *
  71. * @var string $userName 提交人名字
  72. */
  73. public $userName;
  74. /**
  75. *
  76. * @var string $remark 备注
  77. */
  78. public $remark;
  79. /**
  80. *
  81. * @var string $invoiceElement 发票内容
  82. */
  83. public $invoiceElement;
  84. /**
  85. *
  86. * @var string $invoiceType 发票类型
  87. */
  88. public $invoiceType;
  89. /**
  90. *
  91. * @var string $invoiceTitle 发票抬头
  92. */
  93. public $invoiceTitle;
  94. /**
  95. *
  96. * @var string $invoiceCompany 开票公司
  97. */
  98. public $invoiceCompany;
  99. /**
  100. *
  101. * @var string $invoiceNo 发票号
  102. */
  103. public $invoiceNo;
  104. /**
  105. *
  106. * @var string $TIN 纳税人识别码
  107. */
  108. public $TIN;
  109. /**
  110. *
  111. * @var string $address 注册地址
  112. */
  113. public $address;
  114. /**
  115. *
  116. * @var string $phone 电话
  117. */
  118. public $phone;
  119. /**
  120. *
  121. * @var string $bank 开户银行
  122. */
  123. public $bank;
  124. /**
  125. *
  126. * @var string $bankAccount 银行账户
  127. */
  128. public $bankAccount;
  129. /**
  130. *
  131. * @var integer $doPost 邮寄
  132. */
  133. public $doPost;
  134. /**
  135. *
  136. * @var string $recipients 收件人
  137. */
  138. public $recipients;
  139. /**
  140. *
  141. * @var string $recipientsPhone 收件人电话
  142. */
  143. public $recipientsPhone;
  144. /**
  145. *
  146. * @var string $recipientsAddress 收件地址
  147. */
  148. public $recipientsAddress;
  149. /**
  150. *
  151. * @var string $mailItems 邮寄物品
  152. */
  153. public $mailItems;
  154. public $expressCompany;
  155. public $expressNumber;
  156. public $actualItems;
  157. public $poster;
  158. public $untreadReason;
  159. public $untreadPost;
  160. public $untreadCompany;
  161. public $untreadNumber;
  162. public $untreadItems;
  163. public $untreadStatus;
  164. public $irid;
  165. public $bindTime;
  166. /**
  167. *
  168. * @var date $date 提交时间
  169. */
  170. public $date;
  171. public $isDelete;
  172. /**
  173. *
  174. * @var date $updateTime 更新时间
  175. */
  176. public $updateTime;
  177. /**
  178. *
  179. * @var date $printTime 打印时间
  180. */
  181. public $printTime;
  182. public $postTime;
  183. public $_table = 'CLD_invoice';
  184. public $_primarykey = 'iid';
  185. public $_fields = array (
  186. 'iid',
  187. 'invoiceManage',
  188. 'pendingApprovals',
  189. 'processApprovals',
  190. 'invoiceSerial',
  191. 'status',
  192. 'printStatus',
  193. 'postStatus',
  194. 'untreadStatus',
  195. 'invoicePrice',
  196. 'moldManage',
  197. 'cid',
  198. 'categoryName',
  199. 'irid',
  200. 'remark',
  201. 'invoiceElement',
  202. 'invoiceType',
  203. 'invoiceTitle',
  204. 'invoiceCompany',
  205. 'invoiceNo',
  206. 'TIN',
  207. 'address',
  208. 'phone',
  209. 'bank',
  210. 'bankAccount',
  211. 'doPost',
  212. 'recipients',
  213. 'recipientsPhone',
  214. 'recipientsAddress',
  215. 'mailItems',
  216. 'expressCompany',
  217. 'expressNumber',
  218. 'actualItems',
  219. 'untreadReason',
  220. 'untreadPost',
  221. 'untreadCompany',
  222. 'untreadNumber',
  223. 'untreadItems',
  224. 'poster',
  225. 'sid',
  226. 'userName',
  227. 'date',
  228. 'isDelete',
  229. 'updateTime',
  230. 'printTime',
  231. 'postTime',
  232. 'bindTime'
  233. );
  234. /**
  235. * 根据发票ID获取一条发票数据
  236. * @param number $iid 发票ID
  237. * @param number $select 需要获取的字段,为空获取全部数据
  238. * @return array|array() 返回发票数据
  239. */
  240. public function getInvoiceByIid($iid = 0, $select = "") {
  241. if (! is_numeric ( $iid ))
  242. $iid = $this->authcode ( $iid );
  243. $condition = array (
  244. 'where' => "iid=" . $iid,
  245. 'asArray' => TRUE
  246. );
  247. if (! empty ( $select ))
  248. $condition += array (
  249. 'select' => $select
  250. );
  251. $Detail = array ();
  252. if (is_numeric ( $iid ) && ! empty ( $iid ))
  253. $Detail = $this->getOne ( $condition );
  254. if (isset ( $Detail ['expressCompany'] ) && ! empty ( $Detail ['expressCompany'] )) {
  255. $express = explode ( ":", $Detail ['expressCompany'] );
  256. $Detail ['expressCompany'] = $express [0];
  257. $Detail ['expressCom'] = $express [1];
  258. }else{
  259. $Detail ['expressCompany'] = '';
  260. $Detail ['expressCom'] = '';
  261. }
  262. $Detail ['iidKey'] = $this->authcode ( $Detail ['iid'], '' );
  263. Doo::loadModel ( 'L_category' );
  264. $lCategory = new L_category ();
  265. $Detail ['category'] = $lCategory->getCategory ();
  266. return $Detail;
  267. }
  268. public function getInvoiceByIsid($iid = "", $sid = 0) {
  269. if (empty ( $iid ) || empty ( $sid ))
  270. return array ();
  271. Doo::loadClass ( 'XDeode' );
  272. $XDeode = new XDeode ( 5 );
  273. $iid = $XDeode->decode ( $iid );
  274. if (! is_numeric ( $iid ))
  275. return array ();
  276. $detail = $this->getOne ( array (
  277. 'where' => "iid= '" . $iid . "'", // and sid=".$sid,
  278. 'asArray' => TRUE
  279. ) );
  280. return $detail;
  281. }
  282. /**
  283. * 获取单个可以打印的发票数据
  284. * @param number $iid 发票id
  285. * @param number $select 需要获取的字段,为空获取全部数据
  286. * @return array|array() 返回发票数据
  287. */
  288. public function getInvoiceByPrint($iid = 0, $select = "") {
  289. $condition = array (
  290. 'where' => "iid= '" . $iid . "' and status=2",
  291. 'asArray' => TRUE
  292. );
  293. if (! empty ( $select ))
  294. $condition += array (
  295. 'select' => $select
  296. );
  297. $detail = array ();
  298. if (! empty ( $iid ) && is_numeric ( $iid ))
  299. $detail = $this->getOne ( $condition );
  300. return $detail;
  301. }
  302. /**
  303. * 添加一个发票并进入审批状态
  304. * @param array $item 发票相关数据
  305. * @return number 返回发票ID
  306. */
  307. public function addInvoice($item = array()) {
  308. $lid = 0;
  309. if (is_array ( $item ) && ! empty ( $item )) {
  310. foreach ( $item as $key => $value ) {
  311. $this->$key = $value;
  312. }
  313. $lid = $this->insert ();
  314. }
  315. return $lid;
  316. }
  317. /**
  318. * 根据参数字段更新相应字段(主键ID必须传)
  319. * @param array $item 相关需要更新的字段信息
  320. * @return number 返回发票ID
  321. */
  322. public function setInvoiceByCondition($item = array()) {
  323. $lid = 0;
  324. if (is_array ( $item ) && ! empty ( $item )) {
  325. foreach ( $item as $key => $value ) {
  326. $this->$key = $value;
  327. }
  328. $lid = $this->update ();
  329. }
  330. return $lid;
  331. }
  332. /**
  333. * 根据管理组和发票当前状态获得打印发票数据;其中当iid为空时获取所有发票数据,反之获取1条数据
  334. * @param number $sid 管理组人员ID
  335. * @param number $status 状态 为0时获取可以打印数据,为1时获取已经完成打印数据
  336. * @param number $iid 发票ID
  337. * @return array|array() 返回所有发票数据,当iid有值时返回一条数据
  338. */
  339. public function getPrintInvoiceByManage($sid = 0, $status = 0, $iid = 0) {
  340. $list = array ();
  341. if (! empty ( $sid ) && empty ( $iid ))
  342. $list = $this->find ( array (
  343. 'where' => "status=2 and printStatus=" . $status . " and moldManage like '%[\"" . $sid . "\",%'",
  344. 'desc' => 'iid',
  345. 'asArray' => TRUE
  346. ) );
  347. elseif (! empty ( $sid ) && ! empty ( $iid ))
  348. $list = $this->getOne ( array (
  349. 'where' => "status=2 and printStatus=" . $status . " and moldManage like '%[\"" . $sid . "\",%' and iid=" . $iid,
  350. 'asArray' => TRUE
  351. ) );
  352. return $list;
  353. }
  354. /**
  355. * 获得与我相关的发票数据,其中包含 处理中,待处理,最旧入账等数据;当iid有值时获取一条关于sid的发票
  356. * @param number $sid 用户ID
  357. * @param number $iid 发票ID
  358. * @return array|array()
  359. */
  360. public function getMyInvoice($sid = 0, $iid = 0) {
  361. Doo::loadModel ( 'invoiceOperationLog' );
  362. $invoiceOperationLog = new invoiceOperationLog ();
  363. Doo::loadClass ( 'XDeode' );
  364. $XDeode = new XDeode ( 5 );
  365. $list = array ();
  366. if (! empty ( $iid )) {
  367. $list = $this->getOne ( array (
  368. 'where' => " sid=" . $sid . " and iid=" . $iid,
  369. 'asArray' => TRUE
  370. ) );
  371. } elseif (! empty ( $sid ) && empty ( $iid )) {
  372. $list ['pendingInvoice'] = array ();
  373. $list ['handleInvoice'] = array ();
  374. $list ['pendingInvoice'] = $this->find ( array (
  375. 'where' => "((status=1) or (status=2 and printStatus=0) or ( status=2 and printStatus=1 and untreadStatus=1) ) and sid=" . $sid,
  376. 'desc' => 'iid',
  377. 'asArray' => TRUE
  378. ) );
  379. foreach ( $list ['pendingInvoice'] as $key => $value ) {
  380. $list ['pendingInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  381. $list ['pendingInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  382. }
  383. $list ['handleInvoice'] = $this->find ( array (
  384. 'where' => "(status=3 or status=4 or (status=2 and printStatus=1 and untreadStatus=0 ) or (status=2 and printStatus=1 and untreadStatus=2 ) ) and isDelete=0 and sid=" . $sid,
  385. 'desc' => 'iid',
  386. 'asArray' => TRUE
  387. ) );
  388. foreach ( $list ['handleInvoice'] as $key => $value ) {
  389. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  390. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  391. $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  392. }
  393. }
  394. return $list;
  395. }
  396. /**
  397. * 获得与我有关可收款的发票,已经出票的发票
  398. * @param number $sid 开票人ID
  399. */
  400. public function getInvoiceByReceivables($sid = 0) {
  401. Doo::loadClass ( 'XDeode' );
  402. $XDeode = new XDeode ( 5 );
  403. $list ['handleInvoice'] = $this->find ( array (
  404. 'where' => "(status=2 and printStatus=1 and untreadStatus=0 ) and isDelete=0 and sid=" . $sid,
  405. 'desc' => 'iid',
  406. 'asArray' => TRUE
  407. ) );
  408. foreach ( $list ['handleInvoice'] as $key => $value ) {
  409. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  410. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  411. // $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  412. }
  413. return $list ['handleInvoice'];
  414. }
  415. /**
  416. * 获取需要邮寄的发票
  417. * @param integer $postStatus 0为获取需要邮寄的发票,1为获取已经邮寄的发票
  418. * @return array|array() 返回相关数据
  419. */
  420. public function getPostByInvoice($postStatus = 0) {
  421. Doo::loadClass ( 'XDeode' );
  422. $XDeode = new XDeode ( 5 );
  423. $list = $this->find ( array (
  424. 'where' => "postStatus=" . $postStatus . " and doPost=1 and status=2 and isDelete=0",
  425. 'asArray' => TRUE
  426. ) );
  427. foreach ( $list as $key => $value ) {
  428. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  429. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  430. $list [$key] ['expressCompany'] = '';
  431. $list [$key] ['expressCom'] = '';
  432. if (! empty ( $value ['expressCompany'] )) {
  433. $express = explode ( ":", $value ['expressCompany'] );
  434. $list [$key] ['expressCompany'] = $express [0];
  435. $list [$key] ['expressCom'] = $express [1];
  436. }
  437. }
  438. return $list;
  439. }
  440. /**
  441. * 根据退票状态获得相关数据,iid有值时只获得一条数据
  442. * @param number $untreadStatus 退票状态
  443. * @param number $iid 发票ID
  444. */
  445. public function getInvoiceByUntreadStatus($untreadStatus = 1, $iid = 0) {
  446. Doo::loadClass ( 'XDeode' );
  447. $XDeode = new XDeode ( 5 );
  448. if (empty ( $iid )) {
  449. $list = $this->find ( array (
  450. 'where' => " printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2 ",
  451. 'asArray' => TRUE
  452. ) );
  453. if ($untreadStatus == 2) {
  454. Doo::loadModel ( 'invoiceOperationLog' );
  455. $invoiceOperationLog = new invoiceOperationLog ();
  456. }
  457. foreach ( $list as $key => $value ) {
  458. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  459. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  460. $list [$key] ['untreadCompany'] = '';
  461. $list [$key] ['untreadCom'] = '';
  462. if (! empty ( $value ['untreadCompany'] )) {
  463. $express = explode ( ":", $value ['untreadCompany'] );
  464. $list [$key] ['untreadCompany'] = $express [0];
  465. $list [$key] ['untreadCom'] = $express [1];
  466. }
  467. if ($untreadStatus == 2) {
  468. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  469. }
  470. }
  471. } else {
  472. $list = $this->getOne ( array (
  473. 'where' => "iid=" . $iid . " and printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2",
  474. 'asArray' => TRUE
  475. ) );
  476. }
  477. return $list;
  478. }
  479. /**
  480. * 审批中获取相关数据 包括最近的一条操作记录
  481. * @param number $status.
  482. * @param number $limit.
  483. */
  484. function getInvoiceByProcessStatus($limit = 4, $desc = 'desc') {
  485. Doo::loadClass ( 'XDeode' );
  486. $XDeode = new XDeode ( 5 );
  487. $list = $this->find ( array (
  488. 'where' => " (status=1 or status=3 or status=4) and processApprovals!=''",
  489. 'limit' => $limit,
  490. $desc => 'iid',
  491. 'asArray' => TRUE
  492. ) );
  493. Doo::loadModel ( 'invoiceOperationLog' );
  494. $invoiceOperationLog = new invoiceOperationLog ();
  495. foreach ( $list as $key => $value ) {
  496. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  497. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  498. }
  499. return $list;
  500. }
  501. function getInvoiceByUntreadStatusPage($limit = 0, $con = "", $desc = 'desc') {
  502. if (empty ( $limit ) || empty ( $con ))
  503. return array ();
  504. $list = $this->find ( array (
  505. 'where' => $con,
  506. 'limit' => $limit,
  507. $desc => 'iid',
  508. 'asArray' => TRUE
  509. ) );
  510. Doo::loadClass ( 'XDeode' );
  511. $XDeode = new XDeode ( 5 );
  512. Doo::loadModel ( 'invoiceReceivables' );
  513. $invoiceReceivables = new invoiceReceivables ();
  514. Doo::loadModel ( 'invoiceOperationLog' );
  515. $invoiceOperationLog = new invoiceOperationLog ();
  516. foreach ( $list as $key => $value ) {
  517. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  518. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  519. $list [$key] ['untreadCompany'] = '';
  520. $list [$key] ['untreadCom'] = '';
  521. if (! empty ( $value ['untreadCompany'] )) {
  522. $express = explode ( ":", $value ['untreadCompany'] );
  523. $list [$key] ['untreadCompany'] = $express [0];
  524. $list [$key] ['untreadCom'] = $express [1];
  525. }
  526. $list [$key] ['irList'] = $invoiceReceivables->getInvoiceReceivablesInIridString ( $value ['irid'] );
  527. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid( $value ['iid'], 'desc' );
  528. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  529. $list [$key] ['sumPrice'] = 0;
  530. if (! empty ( $list [$key] ['irList'] ))
  531. $list [$key] ['sumPrice'] = $list [$key] ['irList'] [0] ['sumPrice'];
  532. // $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  533. }
  534. return $list;
  535. }
  536. /**
  537. * 加密或解密指定字符串
  538. *
  539. * @param string $string 要加密或解密的字符串
  540. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  541. * @param string $key 加解密的key
  542. * @param $expiry 超时值
  543. *
  544. */
  545. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  546. $ckey_length = 4;
  547. if (! $key) {
  548. $key = $this->INVOICEKEY;
  549. }
  550. $key = md5 ( $key );
  551. $keya = md5 ( substr ( $key, 0, 16 ) );
  552. $keyb = md5 ( substr ( $key, 16, 16 ) );
  553. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  554. $cryptkey = $keya . md5 ( $keya . $keyc );
  555. $key_length = strlen ( $cryptkey );
  556. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  557. $string_length = strlen ( $string );
  558. $result = '';
  559. $box = range ( 0, 255 );
  560. $rndkey = array ();
  561. for($i = 0; $i <= 255; $i ++) {
  562. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  563. }
  564. for($j = $i = 0; $i < 256; $i ++) {
  565. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  566. $tmp = $box [$i];
  567. $box [$i] = $box [$j];
  568. $box [$j] = $tmp;
  569. }
  570. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  571. $a = ($a + 1) % 256;
  572. $j = ($j + $box [$a]) % 256;
  573. $tmp = $box [$a];
  574. $box [$a] = $box [$j];
  575. $box [$j] = $tmp;
  576. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  577. }
  578. if ($operation == 'DECODE') {
  579. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  580. return substr ( $result, 26 );
  581. } else {
  582. return '';
  583. }
  584. } else {
  585. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  586. }
  587. }
  588. }
  589. ?>