invoiceReceivables.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 invoiceReceivables extends DooModel {
  11. private $INVOICEKEY = "APPROVAL";
  12. public $irid;
  13. public $bindStatus;
  14. public $untreadStatus;
  15. public $untreadStaff;
  16. public $iid;
  17. public $iidExtend;
  18. public $bindPrice;
  19. public $receivablesPrice;
  20. public $receivablesSerial;
  21. public $receivablesMessage;
  22. public $receivablesBank;
  23. public $receivablesDate;
  24. public $receivablesCategory;
  25. public $receivablesStaff;
  26. public $recepitTitleMark;
  27. public $inputStaff;
  28. public $inputType;
  29. public $source;
  30. public $date;
  31. public $confirmTime;
  32. public $bindDate;
  33. public $unbundlingTime;
  34. public $accountClerk;
  35. public $sourcePrice;
  36. public $_table = 'CLD_invoiceReceivables';
  37. public $_primarykey = 'irid';
  38. public $_fields = array (
  39. 'irid',
  40. 'bindStatus',
  41. 'untreadStatus',
  42. 'untreadStaff',
  43. 'iid',
  44. 'iidExtend',
  45. 'bindPrice',
  46. 'receivablesPrice',
  47. 'receivablesSerial',
  48. 'receivablesMessage',
  49. 'receivablesBank',
  50. 'receivablesDate',
  51. 'receivablesCategory',
  52. 'receivablesStaff',
  53. 'recepitTitleMark',
  54. 'inputStaff',
  55. 'inputType',
  56. 'source',
  57. 'date',
  58. 'confirmTime',
  59. 'bindDate',
  60. 'unbundlingTime',
  61. 'accountClerk',
  62. 'sourcePrice'
  63. );
  64. public function getSumOfReceivablesPrice($iridString = "") {
  65. if (empty ( $iridString ))
  66. return 0;
  67. $list = $this->getOne ( array (
  68. 'select' => ' sum(receivablesPrice) as receivablesPrice ',
  69. 'where' => " irid IN ( " . $iridString . " )",
  70. 'groupby' => 'iid',
  71. 'asArray' => TRUE
  72. ) );
  73. // echo " irid in ( " . $iridString . " )";
  74. // echo $list ['receivablesPrice'].'sdsdf';
  75. if (empty ( $list ))
  76. return 0;
  77. return $list ['receivablesPrice'];
  78. }
  79. /**
  80. * 获取最新几条收款包括最新的一条操作日志
  81. * @param number $limit
  82. * @return unknown
  83. */
  84. public function getInvoiceReceivablesByNew($limit = 4, $con = "") {
  85. if (empty ( $con ))
  86. return array ();
  87. $list = $this->find ( array (
  88. 'where' => $con,
  89. 'desc' => 'irid',
  90. 'limit' => $limit,
  91. 'asArray' => TRUE
  92. ) );
  93. Doo::loadModel ( 'invoiceROLog' );
  94. $invoiceROLog = new invoiceROLog ();
  95. Doo::loadModel ( 'invoice' );
  96. $invoice = new invoice ();
  97. foreach ( $list as $key => $value ) {
  98. $list [$key] ['invoiceROLog'] = $invoiceROLog->getInvoiceROLogByIrid ( $value ['irid'], 'desc' );
  99. $list [$key] ['invoiceDetail'] = $invoice->getInvoiceByIid ( $value ['iid'] );
  100. }
  101. return $list;
  102. }
  103. /**
  104. * 获得最近录入发票,不包含已经绑定的
  105. */
  106. public function getInvoiceReceivablesByInput() {
  107. $list = $this->find ( array (
  108. 'where' => "bindStatus=0",
  109. 'desc' => 'irid',
  110. 'asArray' => TRUE
  111. ) );
  112. foreach ( $list as $key => $value ) {
  113. $list [$key] ['receivablesCategory'] = explode ( ':', $value ['receivablesCategory'] );
  114. if ($value ['receivablesStaff'] != 'PUBLIC')
  115. $list [$key] ['receivablesStaff'] = explode ( '-', $value ['receivablesStaff'] );
  116. $list [$key] ['iridKey'] = $this->authcode ( $value ['irid'], '' );
  117. }
  118. return $list;
  119. }
  120. /**
  121. * 获得部分入账的收款
  122. */
  123. function getInvoiceReceivablesByHalf() {
  124. $sql = 'select group_concat(b.invoiceSerial) as invoiceSerial ,group_concat(b.iid) as invoiceId ,a.*
  125. from ' . $this->_table . ' as a left join CLD_invoice as b on
  126. find_in_set(b.iid,a.iidExtend) or a.iid=b.iid
  127. where a.bindStatus=0 and a.iid!=0 GROUP BY a.irid
  128. ';
  129. $query = Doo::db ()->query ( $sql );
  130. $list = $query->fetchAll ();
  131. if ($list [0] ['irid'] == NULL) {
  132. return array ();
  133. }
  134. Doo::loadClass ( 'XDeode' );
  135. $XDeode = new XDeode ( 5 );
  136. foreach ( $list as $key => $value ) {
  137. $list [$key] ['uPrice'] = $value ['receivablesPrice'] - $value ['bindPrice'];
  138. $invoiceSerial = explode ( ',', $value ['invoiceSerial'] );
  139. $invoiceId = explode ( ",", $value ['invoiceId'] );
  140. foreach ( $invoiceId as $k => $v ) {
  141. $invoiceId [$k] = $XDeode->encode ( $v );
  142. }
  143. $list [$key] ['invoiceList'] = array_combine ( $invoiceId, $invoiceSerial );
  144. $list [$key] ['iridAuthKey'] = $this->authcode ( $value ['irid'], '' );
  145. }
  146. return $list;
  147. }
  148. public function getInvoiceReceivablesByClaim($cid = 0, $sid = 0) {
  149. if (! empty ( $cid )) {
  150. $list ['csClaim'] = $this->find ( array (
  151. 'where' => "receivablesCategory='public' and receivablesStaff='public' and bindStatus=0",
  152. 'desc' => 'irid',
  153. 'asArray' => TRUE
  154. ) );
  155. foreach ( $list ['csClaim'] as $key => $value ) {
  156. $list ['csClaim'] [$key] ['iidKey'] = $this->authcode ( $value ['irid'], '' );
  157. // $list ['csClaim'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  158. }
  159. $list ['sClaim'] = $this->find ( array (
  160. 'where' => "receivablesCategory!='public' and receivablesStaff='public' and receivablesCategory like '" . $cid . ":%' and bindStatus=0",
  161. 'desc' => 'irid',
  162. 'asArray' => TRUE
  163. ) );
  164. foreach ( $list ['sClaim'] as $key => $value ) {
  165. $list ['sClaim'] [$key] ['iidKey'] = $this->authcode ( $value ['irid'], '' );
  166. }
  167. }
  168. if (! empty ( $cid ) && ! empty ( $sid )) {
  169. $list ['arClaim'] = $this->find ( array ( // and receivablesCategory like '" . $cid . ":%'
  170. 'where' => "receivablesCategory!='public' and receivablesStaff!='public' and receivablesStaff like '" . $sid . "-%' and bindStatus=0",
  171. 'desc' => 'irid',
  172. 'asArray' => TRUE
  173. ) );
  174. foreach ( $list ['arClaim'] as $key => $value ) {
  175. $staff = explode ( "-", $value ['receivablesStaff'] );
  176. $list ['arClaim'] [$key] ['sid'] = $staff [0];
  177. $list ['arClaim'] [$key] ['username'] = $staff [1];
  178. }
  179. $list ['myClaim'] = $this->find ( array (
  180. 'where' => " receivablesStaff like '" . $sid . "-%' and receivablesCategory like '" . $cid . ":%' and bindStatus=0",
  181. 'desc' => 'irid',
  182. 'asArray' => TRUE
  183. ) );
  184. foreach ( $list ['myClaim'] as $key => $value ) {
  185. $list ['myClaim'] [$key] ['iidKey'] = $this->authcode ( $value ['irid'], '' );
  186. $staff = explode ( "-", $value ['receivablesStaff'] );
  187. $list ['myClaim'] [$key] ['sid'] = $staff [0];
  188. $list ['myClaim'] [$key] ['username'] = $staff [1];
  189. }
  190. $list ['bindClaim'] = $this->find ( array (
  191. 'where' => " bindStatus=1 and receivablesStaff like '" . $sid . "-%' ",
  192. 'desc' => 'irid',
  193. 'limit' => 3,
  194. 'asArray' => TRUE
  195. ) );
  196. Doo::loadModel ( 'invoice' );
  197. $invoice = new invoice ();
  198. Doo::loadClass ( 'XDeode' );
  199. $XDeode = new XDeode ( 5 );
  200. // print_r($list ['bindClaim']);
  201. foreach ( $list ['bindClaim'] as $key => $value ) {
  202. $id = $invoice->getInvoiceByIid ( $value ['iid'] );
  203. $id ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  204. $list ['bindClaim'] [$key] ['invoice'] = $id;
  205. $list ['bindClaim'] [$key] ['iridKeyUrl'] = $XDeode->encode ( $value ['irid'] );
  206. }
  207. }
  208. return $list;
  209. }
  210. /**
  211. * 根据发票ID和绑定状态获得收款数据,绑定状态为ALL获取所有状态数据
  212. * @param string $iid 发票ID
  213. * @param string $bindStatus
  214. * @return mixed
  215. */
  216. public function getInvoiceReceivablesByIid($iid = "", $bindStatus = "All") {
  217. Doo::loadClass ( 'XDeode' );
  218. $XDeode = new XDeode ( 5 );
  219. $iid = $this->authcode ( $iid );
  220. $list = array ();
  221. if (! empty ( $iid ) && is_numeric ( $iid )) {
  222. $sql = '';
  223. if ($bindStatus == 1)
  224. $sql = " and bindStatus=1";
  225. elseif ($bindStatus === 0)
  226. $sql = " and bindStatus=0";
  227. $list = $this->find ( array (
  228. 'where' => " iid=" . $iid . $sql,
  229. 'asArray' => TRUE
  230. ) );
  231. }
  232. foreach ( $list as $key => $value ) {
  233. $list [$key] ['iidKey'] = $this->authcode ( $value ['irid'], '' );
  234. $staff = explode ( "-", $value ['receivablesStaff'] );
  235. $list [$key] ['sid'] = $staff [0];
  236. $list [$key] ['username'] = $staff [1];
  237. $list [$key] ['iridKey'] = $XDeode->encode ( $value ['irid'] );
  238. }
  239. return $list;
  240. }
  241. public function getInvoiceReceivablesInIridString($iridString = "") {
  242. $list = array ();
  243. if (! empty ( $iridString ))
  244. $list = $this->find ( array (
  245. 'where' => " irid in ( " . $iridString . " )",
  246. 'asArray' => TRUE
  247. ) );
  248. $price = 0;
  249. $serial = 0;
  250. Doo::loadClass ( 'XDeode' );
  251. $XDeode = new XDeode ( 5 );
  252. foreach ( $list as $key => $value ) {
  253. $price += $value ['receivablesPrice'];
  254. $list [$key] ['iridKeyK'] = $XDeode->encode ( $value ['irid'] );
  255. }
  256. if (! empty ( $list ))
  257. $list [0] ['sumPrice'] = $price;
  258. return $list;
  259. }
  260. /**
  261. * 根据收款ID获取一条相关数据
  262. * @param string $irid
  263. * @param string $con 附加获取收款条件 可以为空
  264. * @return unknown[]|mixed[]|string[]
  265. */
  266. public function getInvoiceReceivablesByIrid($irid = "", $con = "") {
  267. Doo::loadClass ( 'XDeode' );
  268. $XDeode = new XDeode ( 5 );
  269. if (! is_numeric ( $irid ))
  270. $irid = $this->authcode ( $irid );
  271. $detail = array ();
  272. if (! empty ( $irid ) && is_numeric ( $irid ))
  273. $detail = $this->getOne ( array (
  274. 'where' => " irid=" . $irid . $con,
  275. 'asArray' => TRUE
  276. ) );
  277. if (empty ( $detail ))
  278. return $detail;
  279. $detail ['cid'] = '';
  280. $detail ['title'] = '';
  281. if ($detail ['receivablesCategory'] != "PUBLIC") {
  282. $category = explode ( ":", $detail ['receivablesCategory'] );
  283. $detail ['cid'] = $category [0];
  284. $detail ['title'] = $category [1];
  285. }
  286. $detail ['sid'] = '';
  287. $detail ['username'] = '';
  288. if ($detail ['receivablesStaff'] != "PUBLIC") {
  289. $staff = explode ( "-", $detail ['receivablesStaff'] );
  290. $detail ['sid'] = $staff [0];
  291. $detail ['username'] = $staff [1];
  292. }
  293. $detail ['iridKey'] = $XDeode->encode ( $detail ['irid'] );
  294. $detail ['iridEn'] = $this->authcode ( $detail ['irid'], '' );
  295. return $detail;
  296. }
  297. public function getInvoiceReceivablesByIrsid($irid = "", $sid = "") {
  298. Doo::loadClass ( 'XDeode' );
  299. $XDeode = new XDeode ( 5 );
  300. $irid = $XDeode->decode ( $irid );
  301. $detail = array ();
  302. if (! empty ( $irid ) && is_numeric ( $irid )) {
  303. if (! empty ( $sid ))
  304. $detail = $this->getOne ( array (
  305. 'where' => " irid=" . $irid . " and receivablesStaff like '" . $sid . "_%'",
  306. 'asArray' => TRUE
  307. ) );
  308. else
  309. $detail = $this->getOne ( array (
  310. 'where' => " irid=" . $irid,
  311. 'asArray' => TRUE
  312. ) );
  313. $detail ['sid'] = '';
  314. $detail ['username'] = '';
  315. if (! empty ( $detail ['receivablesStaff'] )) {
  316. $staff = explode ( "-", $detail ['receivablesStaff'] );
  317. $detail ['sid'] = $staff [0];
  318. $detail ['username'] = $staff [1];
  319. }
  320. $detail ['iridKey'] = $XDeode->encode ( $detail ['irid'] );
  321. $detail ['iridEn'] = $this->authcode ( $detail ['irid'], '' );
  322. }
  323. return $detail;
  324. }
  325. /**
  326. * 添加一个收款
  327. * @param array $item 收款相关数据
  328. * @return number 返回收款ID
  329. */
  330. public function addInvoiceReceivables($item = array()) {
  331. $lid = 0;
  332. if (is_array ( $item ) && ! empty ( $item )) {
  333. foreach ( $item as $key => $value ) {
  334. $this->$key = $value;
  335. }
  336. $lid = $this->insert ();
  337. }
  338. return $lid;
  339. }
  340. /**
  341. * 根据参数字段更新相应字段(主键ID必须传)
  342. * @param array $item 相关需要更新的字段信息
  343. * @return number 返回发票ID
  344. */
  345. public function setInvoiceReceivablesByCondition($item = array()) {
  346. $lid = 0;
  347. if (is_array ( $item ) && ! empty ( $item )) {
  348. foreach ( $item as $key => $value ) {
  349. $this->$key = $value;
  350. }
  351. $lid = $this->update ();
  352. }
  353. return $lid;
  354. }
  355. /**
  356. * 根据irid 获取一组收款数据
  357. * @param string $irid
  358. * @return unknown
  359. */
  360. public function getInvoiceReceivablesInIrid($irid = '') {
  361. if (empty ( $irid ))
  362. return array ();
  363. $list = $this->find ( array (
  364. 'where' => " irid in(" . $irid . ")",
  365. 'asArray' => TRUE
  366. ) );
  367. foreach ( $list as $key => $value ) {
  368. }
  369. return $list;
  370. }
  371. function getInvoiceReceivablesByConditionPage($limit = 0, $con = "", $desc = 'desc', $descField = 'irid') {
  372. if (empty ( $limit ))
  373. return array ();
  374. $sql = 'select group_concat(b.invoiceSerial) as invoiceSerial ,group_concat(b.iid) as invoiceId ,a.*,b.invoicePrice
  375. from ' . $this->_table . ' as a left join CLD_invoice as b on
  376. find_in_set(b.iid,a.iidExtend) or a.iid=b.iid
  377. where ' . $con . ' group by a.irid ORDER BY ' . $descField . ' '.$desc.' limit ' . $limit . '
  378. ';
  379. $query = Doo::db ()->query ( $sql );
  380. $list = $query->fetchAll ();
  381. Doo::loadClass ( 'XDeode' );
  382. $XDeode = new XDeode ( 5 );
  383. Doo::loadModel ( 'invoiceROLog' );
  384. $invoiceROLog = new invoiceROLog ();
  385. foreach ( $list as $key => $value ) {
  386. $invoiceSerial = explode ( ',', $value ['invoiceSerial'] );
  387. $invoiceId = explode ( ",", $value ['invoiceId'] );
  388. if (empty($invoiceSerial[0]))
  389. $list [$key] ['invoiceList'] =array();
  390. else{
  391. foreach ( $invoiceId as $k => $v ) {
  392. if (!empty($v))
  393. $invoiceId [$k] = $XDeode->encode ( $v );
  394. }
  395. $list [$key] ['invoiceList'] = array_combine ( $invoiceId, $invoiceSerial );
  396. }
  397. $list [$key] ['iridKeyK'] = $XDeode->encode ( $value ['irid'] );
  398. $list [$key] ['iridKey'] = $this->authcode ( $value ['irid'], '' );
  399. $list [$key] ['irolg'] = $invoiceROLog->getInvoiceROLogByIrid ( $value ['irid'], 'desc' );
  400. }
  401. //print_r($list);
  402. return $list;
  403. }
  404. function getReceivablesByUntreadStatusPage($limit = 0, $con = "", $desc = 'desc', $descField = 'irid') {
  405. if (empty ( $limit ))
  406. return array ();
  407. $list = $this->find ( array (
  408. 'where' => $con, // " bindStatus=1 and receivablesStaff like '" . $sid . "_%' ".$con,
  409. 'limit' => $limit,
  410. $desc => $descField,
  411. 'asArray' => TRUE
  412. ) );
  413. Doo::loadClass ( 'XDeode' );
  414. $XDeode = new XDeode ( 5 );
  415. Doo::loadModel ( 'invoice' );
  416. $invoice = new invoice ();
  417. foreach ( $list as $key => $value ) {
  418. $list [$key] ['iridKeyK'] = $XDeode->encode ( $value ['irid'] );
  419. $list [$key] ['iridKey'] = $this->authcode ( $value ['irid'], '' );
  420. if (isset ( $value ['iid'] ) && ! empty ( $value ['iid'] )) {
  421. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  422. $list [$key] ['irList'] = $invoice->getInvoiceByIid ( $value ['iid'] );
  423. } else {
  424. $list [$key] ['iidKeyK'] = '';
  425. $list [$key] ['irList'] = array (
  426. 'invoiceSerial' => ''
  427. );
  428. }
  429. $list [$key] ['irolg'] = $invoiceROLog->getInvoiceROLogByIrid ( $value ['irid'], 'desc' );
  430. // $list [$key] ['sumPrice'] = 0;
  431. // if (! empty ( $list [$key] ['irList'] ))
  432. // $list [$key] ['sumPrice'] = $list [$key] ['irList'] [0] ['sumPrice'];
  433. // $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  434. }
  435. return $list;
  436. }
  437. function delInvoiceReceivablesByIrid($irid = 0) {
  438. if (! empty ( $irid ) && is_numeric ( $irid ))
  439. $this->delete ( array (
  440. 'where' => 'irid=' . $irid
  441. ) );
  442. }
  443. /**
  444. * 加密或解密指定字符串
  445. *
  446. * @param string $string 要加密或解密的字符串
  447. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  448. * @param string $key 加解密的key
  449. * @param $expiry 超时值
  450. *
  451. */
  452. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  453. $ckey_length = 4;
  454. if (! $key) {
  455. $key = $this->INVOICEKEY;
  456. }
  457. $key = md5 ( $key );
  458. $keya = md5 ( substr ( $key, 0, 16 ) );
  459. $keyb = md5 ( substr ( $key, 16, 16 ) );
  460. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  461. $cryptkey = $keya . md5 ( $keya . $keyc );
  462. $key_length = strlen ( $cryptkey );
  463. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  464. $string_length = strlen ( $string );
  465. $result = '';
  466. $box = range ( 0, 255 );
  467. $rndkey = array ();
  468. for($i = 0; $i <= 255; $i ++) {
  469. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  470. }
  471. for($j = $i = 0; $i < 256; $i ++) {
  472. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  473. $tmp = $box [$i];
  474. $box [$i] = $box [$j];
  475. $box [$j] = $tmp;
  476. }
  477. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  478. $a = ($a + 1) % 256;
  479. $j = ($j + $box [$a]) % 256;
  480. $tmp = $box [$a];
  481. $box [$a] = $box [$j];
  482. $box [$j] = $tmp;
  483. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  484. }
  485. if ($operation == 'DECODE') {
  486. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  487. return substr ( $result, 26 );
  488. } else {
  489. return '';
  490. }
  491. } else {
  492. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  493. }
  494. }
  495. }
  496. ?>