invoice.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. /**
  165. *
  166. * @var date $date 提交时间
  167. */
  168. public $date;
  169. public $isDelete;
  170. /**
  171. *
  172. * @var date $updateTime 更新时间
  173. */
  174. public $updateTime;
  175. /**
  176. *
  177. * @var date $printTime 打印时间
  178. */
  179. public $printTime;
  180. public $postTime;
  181. public $_table = 'CLD_invoice';
  182. public $_primarykey = 'iid';
  183. public $_fields = array (
  184. 'iid',
  185. 'invoiceManage',
  186. 'pendingApprovals',
  187. 'processApprovals',
  188. 'invoiceSerial',
  189. 'status',
  190. 'printStatus',
  191. 'postStatus',
  192. 'untreadStatus',
  193. 'invoicePrice',
  194. 'moldManage',
  195. 'cid',
  196. 'categoryName',
  197. 'remark',
  198. 'invoiceElement',
  199. 'invoiceType',
  200. 'invoiceTitle',
  201. 'invoiceCompany',
  202. 'invoiceNo',
  203. 'TIN',
  204. 'address',
  205. 'phone',
  206. 'bank',
  207. 'bankAccount',
  208. 'doPost',
  209. 'recipients',
  210. 'recipientsPhone',
  211. 'recipientsAddress',
  212. 'mailItems',
  213. 'expressCompany',
  214. 'expressNumber',
  215. 'actualItems',
  216. 'untreadReason',
  217. 'untreadPost',
  218. 'untreadCompany',
  219. 'untreadNumber',
  220. 'untreadItems',
  221. 'poster',
  222. 'sid',
  223. 'userName',
  224. 'date',
  225. 'isDelete',
  226. 'updateTime',
  227. 'printTime',
  228. 'postTime'
  229. );
  230. /**
  231. * 根据发票ID获取一条发票数据
  232. * @param number $iid 发票ID
  233. * @param number $select 需要获取的字段,为空获取全部数据
  234. * @return array|array() 返回发票数据
  235. */
  236. public function getInvoiceByIid($iid = 0, $select = "") {
  237. $condition = array (
  238. 'where' => "iid=" . $iid,
  239. 'asArray' => TRUE
  240. );
  241. if (! empty ( $select ))
  242. $condition += array (
  243. 'select' => $select
  244. );
  245. $Detail = array ();
  246. if (is_numeric ( $iid ) && ! empty ( $iid ))
  247. $Detail = $this->getOne ( $condition );
  248. $Detail ['expressCompany'] = '';
  249. $Detail ['expressCom'] = '';
  250. if (isset ( $Detail ['expressCompany'] ) && ! empty ( $Detail ['expressCompany'] )) {
  251. $express = explode ( ":", $Detail ['expressCompany'] );
  252. $Detail ['expressCompany'] = $express [0];
  253. $Detail ['expressCom'] = $express [1];
  254. }
  255. $Detail ['iidKey'] = $this->authcode ( $Detail ['iid'], '' );
  256. Doo::loadModel ( 'L_category' );
  257. $lCategory = new L_category ();
  258. $Detail ['category'] = $lCategory->getCategory ();
  259. return $Detail;
  260. }
  261. /**
  262. * 获取单个可以打印的发票数据
  263. * @param number $iid 发票id
  264. * @param number $select 需要获取的字段,为空获取全部数据
  265. * @return array|array() 返回发票数据
  266. */
  267. public function getInvoiceByPrint($iid = 0, $select = "") {
  268. $condition = array (
  269. 'where' => "iid= '" . $iid . "' and status=2",
  270. 'asArray' => TRUE
  271. );
  272. if (! empty ( $select ))
  273. $condition += array (
  274. 'select' => $select
  275. );
  276. $detail = array ();
  277. if (! empty ( $iid ) && is_numeric ( $iid ))
  278. $detail = $this->getOne ( $condition );
  279. return $detail;
  280. }
  281. /**
  282. * 添加一个发票并进入审批状态
  283. * @param array $item 发票相关数据
  284. * @return number 返回发票ID
  285. */
  286. public function addInvoice($item = array()) {
  287. $lid = 0;
  288. if (is_array ( $item ) && ! empty ( $item )) {
  289. foreach ( $item as $key => $value ) {
  290. $this->$key = $value;
  291. }
  292. $lid = $this->insert ();
  293. }
  294. return $lid;
  295. }
  296. /**
  297. * 根据参数字段更新相应字段(主键ID必须传)
  298. * @param array $item 相关需要更新的字段信息
  299. * @return number 返回发票ID
  300. */
  301. public function setInvoiceByCondition($item = array()) {
  302. $lid = 0;
  303. if (is_array ( $item ) && ! empty ( $item )) {
  304. foreach ( $item as $key => $value ) {
  305. $this->$key = $value;
  306. }
  307. $lid = $this->update ();
  308. }
  309. return $lid;
  310. }
  311. /**
  312. * 根据管理组和发票当前状态获得打印发票数据;其中当iid为空时获取所有发票数据,反之获取1条数据
  313. * @param number $sid 管理组人员ID
  314. * @param number $status 状态 为0时获取可以打印数据,为1时获取已经完成打印数据
  315. * @param number $iid 发票ID
  316. * @return array|array() 返回所有发票数据,当iid有值时返回一条数据
  317. */
  318. public function getPrintInvoiceByManage($sid = 0, $status = 0, $iid = 0) {
  319. $list = array ();
  320. if (! empty ( $sid ) && empty ( $iid ))
  321. $list = $this->find ( array (
  322. 'where' => "status=2 and printStatus=" . $status . " and moldManage like '%[\"" . $sid . "\",%'",
  323. 'desc' => 'iid',
  324. 'asArray' => TRUE
  325. ) );
  326. elseif (! empty ( $sid ) && ! empty ( $iid ))
  327. $list = $this->getOne ( array (
  328. 'where' => "status=2 and printStatus=" . $status . " and moldManage like '%[\"" . $sid . "\",%' and iid=" . $iid,
  329. 'asArray' => TRUE
  330. ) );
  331. return $list;
  332. }
  333. /**
  334. * 获得与我相关的发票数据,其中包含 处理中,待处理,最旧入账等数据;当iid有值时获取一条关于sid的发票
  335. * @param number $sid 用户ID
  336. * @param number $iid 发票ID
  337. * @return array|array()
  338. */
  339. public function getMyInvoice($sid = 0, $iid = 0) {
  340. Doo::loadModel ( 'invoiceOperationLog' );
  341. $invoiceOperationLog = new invoiceOperationLog ();
  342. Doo::loadClass ( 'XDeode' );
  343. $XDeode = new XDeode ( 5 );
  344. $list = array ();
  345. if (! empty ( $iid )) {
  346. $list = $this->getOne ( array (
  347. 'where' => " sid=" . $sid . " and iid=" . $iid,
  348. 'asArray' => TRUE
  349. ) );
  350. } elseif (! empty ( $sid ) && empty ( $iid )) {
  351. $list ['pendingInvoice'] = array ();
  352. $list ['handleInvoice'] = array ();
  353. $list ['pendingInvoice'] = $this->find ( array (
  354. 'where' => "((status=1) or (status=2 and printStatus=0) or ( status=2 and printStatus=1 and untreadStatus=1) ) and sid=" . $sid,
  355. 'desc' => 'iid',
  356. 'asArray' => TRUE
  357. ) );
  358. foreach ( $list ['pendingInvoice'] as $key => $value ) {
  359. $list ['pendingInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  360. }
  361. $list ['handleInvoice'] = $this->find ( array (
  362. '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,
  363. 'desc' => 'iid',
  364. 'asArray' => TRUE
  365. ) );
  366. foreach ( $list ['handleInvoice'] as $key => $value ) {
  367. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  368. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  369. $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  370. }
  371. }
  372. return $list;
  373. }
  374. /**
  375. * 获得与我有关可收款的发票,已经出票的发票
  376. * @param number $sid 开票人ID
  377. */
  378. public function getInvoiceByReceivables($sid = 0) {
  379. Doo::loadClass ( 'XDeode' );
  380. $XDeode = new XDeode ( 5 );
  381. $list ['handleInvoice'] = $this->find ( array (
  382. 'where' => "(status=2 and printStatus=1 and untreadStatus=0 ) and isDelete=0 and sid=" . $sid,
  383. 'desc' => 'iid',
  384. 'asArray' => TRUE
  385. ) );
  386. foreach ( $list ['handleInvoice'] as $key => $value ) {
  387. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  388. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  389. // $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  390. }
  391. return $list['handleInvoice'];
  392. }
  393. /**
  394. * 获取需要邮寄的发票
  395. * @param integer $postStatus 0为获取需要邮寄的发票,1为获取已经邮寄的发票
  396. * @return array|array() 返回相关数据
  397. */
  398. public function getPostByInvoice($postStatus = 0) {
  399. $list = $this->find ( array (
  400. 'where' => "postStatus=" . $postStatus . " and doPost=1 and status=2",
  401. 'asArray' => TRUE
  402. ) );
  403. foreach ( $list as $key => $value ) {
  404. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  405. $list [$key] ['expressCompany'] = '';
  406. $list [$key] ['expressCom'] = '';
  407. if (! empty ( $value ['expressCompany'] )) {
  408. $express = explode ( ":", $value ['expressCompany'] );
  409. $list [$key] ['expressCompany'] = $express [0];
  410. $list [$key] ['expressCom'] = $express [1];
  411. }
  412. }
  413. return $list;
  414. }
  415. /**
  416. * 根据退票状态获得相关数据,iid有值时只获得一条数据
  417. * @param number $untreadStatus 退票状态
  418. * @param number $iid 发票ID
  419. */
  420. public function getInvoiceByUntreadStatus($untreadStatus = 1, $iid = 0) {
  421. if (empty ( $iid )) {
  422. $list = $this->find ( array (
  423. 'where' => " printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2 ",
  424. 'asArray' => TRUE
  425. ) );
  426. if ($untreadStatus == 2) {
  427. Doo::loadModel ( 'invoiceOperationLog' );
  428. $invoiceOperationLog = new invoiceOperationLog ();
  429. }
  430. foreach ( $list as $key => $value ) {
  431. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  432. $list [$key] ['untreadCompany'] = '';
  433. $list [$key] ['untreadCom'] = '';
  434. if (! empty ( $value ['untreadCompany'] )) {
  435. $express = explode ( ":", $value ['untreadCompany'] );
  436. $list [$key] ['untreadCompany'] = $express [0];
  437. $list [$key] ['untreadCom'] = $express [1];
  438. }
  439. if ($untreadStatus == 2) {
  440. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  441. }
  442. }
  443. } else {
  444. $list = $this->getOne ( array (
  445. 'where' => "iid=" . $iid . " and printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2",
  446. 'asArray' => TRUE
  447. ) );
  448. }
  449. return $list;
  450. }
  451. /**
  452. * 加密或解密指定字符串
  453. *
  454. * @param string $string 要加密或解密的字符串
  455. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  456. * @param string $key 加解密的key
  457. * @param $expiry 超时值
  458. *
  459. */
  460. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  461. $ckey_length = 4;
  462. if (! $key) {
  463. $key = $this->INVOICEKEY;
  464. }
  465. $key = md5 ( $key );
  466. $keya = md5 ( substr ( $key, 0, 16 ) );
  467. $keyb = md5 ( substr ( $key, 16, 16 ) );
  468. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  469. $cryptkey = $keya . md5 ( $keya . $keyc );
  470. $key_length = strlen ( $cryptkey );
  471. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  472. $string_length = strlen ( $string );
  473. $result = '';
  474. $box = range ( 0, 255 );
  475. $rndkey = array ();
  476. for($i = 0; $i <= 255; $i ++) {
  477. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  478. }
  479. for($j = $i = 0; $i < 256; $i ++) {
  480. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  481. $tmp = $box [$i];
  482. $box [$i] = $box [$j];
  483. $box [$j] = $tmp;
  484. }
  485. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  486. $a = ($a + 1) % 256;
  487. $j = ($j + $box [$a]) % 256;
  488. $tmp = $box [$a];
  489. $box [$a] = $box [$j];
  490. $box [$j] = $tmp;
  491. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  492. }
  493. if ($operation == 'DECODE') {
  494. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  495. return substr ( $result, 26 );
  496. } else {
  497. return '';
  498. }
  499. } else {
  500. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  501. }
  502. }
  503. }
  504. ?>