invoice.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. $Detail ['expressCompany'] = '';
  255. $Detail ['expressCom'] = '';
  256. if (isset ( $Detail ['expressCompany'] ) && ! empty ( $Detail ['expressCompany'] )) {
  257. $express = explode ( ":", $Detail ['expressCompany'] );
  258. $Detail ['expressCompany'] = $express [0];
  259. $Detail ['expressCom'] = $express [1];
  260. }
  261. $Detail ['iidKey'] = $this->authcode ( $Detail ['iid'], '' );
  262. Doo::loadModel ( 'L_category' );
  263. $lCategory = new L_category ();
  264. $Detail ['category'] = $lCategory->getCategory ();
  265. return $Detail;
  266. }
  267. public function getInvoiceByIsid($iid = "", $sid = 0) {
  268. if (empty ( $iid ) || empty ( $sid ))
  269. return array ();
  270. Doo::loadClass ( 'XDeode' );
  271. $XDeode = new XDeode ( 5 );
  272. $iid = $XDeode->decode ( $iid );
  273. if (! is_numeric ( $iid ))
  274. return array ();
  275. $detail = $this->getOne ( array (
  276. 'where' => "iid= '" . $iid . "'", // and sid=".$sid,
  277. 'asArray' => TRUE
  278. ) );
  279. return $detail;
  280. }
  281. /**
  282. * 获取单个可以打印的发票数据
  283. * @param number $iid 发票id
  284. * @param number $select 需要获取的字段,为空获取全部数据
  285. * @return array|array() 返回发票数据
  286. */
  287. public function getInvoiceByPrint($iid = 0, $select = "") {
  288. $condition = array (
  289. 'where' => "iid= '" . $iid . "' and status=2",
  290. 'asArray' => TRUE
  291. );
  292. if (! empty ( $select ))
  293. $condition += array (
  294. 'select' => $select
  295. );
  296. $detail = array ();
  297. if (! empty ( $iid ) && is_numeric ( $iid ))
  298. $detail = $this->getOne ( $condition );
  299. return $detail;
  300. }
  301. /**
  302. * 添加一个发票并进入审批状态
  303. * @param array $item 发票相关数据
  304. * @return number 返回发票ID
  305. */
  306. public function addInvoice($item = array()) {
  307. $lid = 0;
  308. if (is_array ( $item ) && ! empty ( $item )) {
  309. foreach ( $item as $key => $value ) {
  310. $this->$key = $value;
  311. }
  312. $lid = $this->insert ();
  313. }
  314. return $lid;
  315. }
  316. /**
  317. * 根据参数字段更新相应字段(主键ID必须传)
  318. * @param array $item 相关需要更新的字段信息
  319. * @return number 返回发票ID
  320. */
  321. public function setInvoiceByCondition($item = array()) {
  322. $lid = 0;
  323. if (is_array ( $item ) && ! empty ( $item )) {
  324. foreach ( $item as $key => $value ) {
  325. $this->$key = $value;
  326. }
  327. $lid = $this->update ();
  328. }
  329. return $lid;
  330. }
  331. /**
  332. * 根据管理组和发票当前状态获得打印发票数据;其中当iid为空时获取所有发票数据,反之获取1条数据
  333. * @param number $sid 管理组人员ID
  334. * @param number $status 状态 为0时获取可以打印数据,为1时获取已经完成打印数据
  335. * @param number $iid 发票ID
  336. * @return array|array() 返回所有发票数据,当iid有值时返回一条数据
  337. */
  338. public function getPrintInvoiceByManage($sid = 0, $status = 0, $iid = 0) {
  339. $list = array ();
  340. if (! empty ( $sid ) && empty ( $iid ))
  341. $list = $this->find ( array (
  342. 'where' => "status=2 and printStatus=" . $status . " and moldManage like '%[\"" . $sid . "\",%'",
  343. 'desc' => 'iid',
  344. 'asArray' => TRUE
  345. ) );
  346. elseif (! empty ( $sid ) && ! empty ( $iid ))
  347. $list = $this->getOne ( array (
  348. 'where' => "status=2 and printStatus=" . $status . " and moldManage like '%[\"" . $sid . "\",%' and iid=" . $iid,
  349. 'asArray' => TRUE
  350. ) );
  351. return $list;
  352. }
  353. /**
  354. * 获得与我相关的发票数据,其中包含 处理中,待处理,最旧入账等数据;当iid有值时获取一条关于sid的发票
  355. * @param number $sid 用户ID
  356. * @param number $iid 发票ID
  357. * @return array|array()
  358. */
  359. public function getMyInvoice($sid = 0, $iid = 0) {
  360. Doo::loadModel ( 'invoiceOperationLog' );
  361. $invoiceOperationLog = new invoiceOperationLog ();
  362. Doo::loadClass ( 'XDeode' );
  363. $XDeode = new XDeode ( 5 );
  364. $list = array ();
  365. if (! empty ( $iid )) {
  366. $list = $this->getOne ( array (
  367. 'where' => " sid=" . $sid . " and iid=" . $iid,
  368. 'asArray' => TRUE
  369. ) );
  370. } elseif (! empty ( $sid ) && empty ( $iid )) {
  371. $list ['pendingInvoice'] = array ();
  372. $list ['handleInvoice'] = array ();
  373. $list ['pendingInvoice'] = $this->find ( array (
  374. 'where' => "((status=1) or (status=2 and printStatus=0) or ( status=2 and printStatus=1 and untreadStatus=1) ) and sid=" . $sid,
  375. 'desc' => 'iid',
  376. 'asArray' => TRUE
  377. ) );
  378. foreach ( $list ['pendingInvoice'] as $key => $value ) {
  379. $list ['pendingInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  380. $list ['pendingInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  381. }
  382. $list ['handleInvoice'] = $this->find ( array (
  383. '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,
  384. 'desc' => 'iid',
  385. 'asArray' => TRUE
  386. ) );
  387. foreach ( $list ['handleInvoice'] as $key => $value ) {
  388. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  389. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  390. $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  391. }
  392. }
  393. return $list;
  394. }
  395. /**
  396. * 获得与我有关可收款的发票,已经出票的发票
  397. * @param number $sid 开票人ID
  398. */
  399. public function getInvoiceByReceivables($sid = 0) {
  400. Doo::loadClass ( 'XDeode' );
  401. $XDeode = new XDeode ( 5 );
  402. $list ['handleInvoice'] = $this->find ( array (
  403. 'where' => "(status=2 and printStatus=1 and untreadStatus=0 ) and isDelete=0 and sid=" . $sid,
  404. 'desc' => 'iid',
  405. 'asArray' => TRUE
  406. ) );
  407. foreach ( $list ['handleInvoice'] as $key => $value ) {
  408. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  409. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  410. // $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  411. }
  412. return $list ['handleInvoice'];
  413. }
  414. /**
  415. * 获取需要邮寄的发票
  416. * @param integer $postStatus 0为获取需要邮寄的发票,1为获取已经邮寄的发票
  417. * @return array|array() 返回相关数据
  418. */
  419. public function getPostByInvoice($postStatus = 0) {
  420. Doo::loadClass ( 'XDeode' );
  421. $XDeode = new XDeode ( 5 );
  422. $list = $this->find ( array (
  423. 'where' => "postStatus=" . $postStatus . " and doPost=1 and status=2",
  424. 'asArray' => TRUE
  425. ) );
  426. foreach ( $list as $key => $value ) {
  427. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  428. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  429. $list [$key] ['expressCompany'] = '';
  430. $list [$key] ['expressCom'] = '';
  431. if (! empty ( $value ['expressCompany'] )) {
  432. $express = explode ( ":", $value ['expressCompany'] );
  433. $list [$key] ['expressCompany'] = $express [0];
  434. $list [$key] ['expressCom'] = $express [1];
  435. }
  436. }
  437. return $list;
  438. }
  439. /**
  440. * 根据退票状态获得相关数据,iid有值时只获得一条数据
  441. * @param number $untreadStatus 退票状态
  442. * @param number $iid 发票ID
  443. */
  444. public function getInvoiceByUntreadStatus($untreadStatus = 1, $iid = 0) {
  445. Doo::loadClass ( 'XDeode' );
  446. $XDeode = new XDeode ( 5 );
  447. if (empty ( $iid )) {
  448. $list = $this->find ( array (
  449. 'where' => " printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2 ",
  450. 'asArray' => TRUE
  451. ) );
  452. if ($untreadStatus == 2) {
  453. Doo::loadModel ( 'invoiceOperationLog' );
  454. $invoiceOperationLog = new invoiceOperationLog ();
  455. }
  456. foreach ( $list as $key => $value ) {
  457. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  458. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  459. $list [$key] ['untreadCompany'] = '';
  460. $list [$key] ['untreadCom'] = '';
  461. if (! empty ( $value ['untreadCompany'] )) {
  462. $express = explode ( ":", $value ['untreadCompany'] );
  463. $list [$key] ['untreadCompany'] = $express [0];
  464. $list [$key] ['untreadCom'] = $express [1];
  465. }
  466. if ($untreadStatus == 2) {
  467. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  468. }
  469. }
  470. } else {
  471. $list = $this->getOne ( array (
  472. 'where' => "iid=" . $iid . " and printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2",
  473. 'asArray' => TRUE
  474. ) );
  475. }
  476. return $list;
  477. }
  478. /**
  479. * 审批中获取相关数据 包括最近的一条操作记录
  480. * @param number $status.
  481. * @param number $limit.
  482. */
  483. function getInvoiceByProcessStatus($limit = 4, $desc = 'desc') {
  484. Doo::loadClass ( 'XDeode' );
  485. $XDeode = new XDeode ( 5 );
  486. $list = $this->find ( array (
  487. 'where' => " (status=1 or status=3 or status=4) and processApprovals!=''",
  488. 'limit' => $limit,
  489. $desc => 'iid',
  490. 'asArray' => TRUE
  491. ) );
  492. Doo::loadModel ( 'invoiceOperationLog' );
  493. $invoiceOperationLog = new invoiceOperationLog ();
  494. foreach ( $list as $key => $value ) {
  495. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  496. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  497. }
  498. return $list;
  499. }
  500. function getInvoiceByUntreadStatusPage($limit = 0, $sid = 0, $con = "", $desc = 'desc') {
  501. if (empty ( $limit ) || empty ( $sid ))
  502. return array ();
  503. $list = $this->find ( array (
  504. 'where' => " status=2 and untreadStatus=3 and sid=" . $sid.$con,
  505. 'limit' => $limit,
  506. $desc => 'iid',
  507. 'asArray' => TRUE
  508. ) );
  509. Doo::loadClass ( 'XDeode' );
  510. $XDeode = new XDeode ( 5 );
  511. Doo::loadModel ( 'invoiceReceivables' );
  512. $invoiceReceivables = new invoiceReceivables ();
  513. foreach ( $list as $key => $value ) {
  514. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  515. $list [$key] ['irList'] = $invoiceReceivables->getInvoiceReceivablesInIridString ( $value ['irid'] );
  516. $list [$key] ['sumPrice'] = 0;
  517. if (! empty ( $list [$key] ['irList'] ))
  518. $list [$key] ['sumPrice'] = $list [$key] ['irList'] [0] ['sumPrice'];
  519. // $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  520. }
  521. return $list;
  522. }
  523. /**
  524. * 加密或解密指定字符串
  525. *
  526. * @param string $string 要加密或解密的字符串
  527. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  528. * @param string $key 加解密的key
  529. * @param $expiry 超时值
  530. *
  531. */
  532. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  533. $ckey_length = 4;
  534. if (! $key) {
  535. $key = $this->INVOICEKEY;
  536. }
  537. $key = md5 ( $key );
  538. $keya = md5 ( substr ( $key, 0, 16 ) );
  539. $keyb = md5 ( substr ( $key, 16, 16 ) );
  540. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  541. $cryptkey = $keya . md5 ( $keya . $keyc );
  542. $key_length = strlen ( $cryptkey );
  543. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  544. $string_length = strlen ( $string );
  545. $result = '';
  546. $box = range ( 0, 255 );
  547. $rndkey = array ();
  548. for($i = 0; $i <= 255; $i ++) {
  549. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  550. }
  551. for($j = $i = 0; $i < 256; $i ++) {
  552. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  553. $tmp = $box [$i];
  554. $box [$i] = $box [$j];
  555. $box [$j] = $tmp;
  556. }
  557. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  558. $a = ($a + 1) % 256;
  559. $j = ($j + $box [$a]) % 256;
  560. $tmp = $box [$a];
  561. $box [$a] = $box [$j];
  562. $box [$j] = $tmp;
  563. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  564. }
  565. if ($operation == 'DECODE') {
  566. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  567. return substr ( $result, 26 );
  568. } else {
  569. return '';
  570. }
  571. } else {
  572. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  573. }
  574. }
  575. }
  576. ?>