invoice.php 14 KB

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