invoiceReceivables.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 (empty($list))
  132. return array ();
  133. if ($list [0] ['irid'] == NULL) {
  134. return array ();
  135. }
  136. Doo::loadClass ( 'XDeode' );
  137. $XDeode = new XDeode ( 5 );
  138. foreach ( $list as $key => $value ) {
  139. $list [$key] ['uPrice'] = $value ['receivablesPrice'] - $value ['bindPrice'];
  140. $invoiceSerial = explode ( ',', $value ['invoiceSerial'] );
  141. $invoiceId = explode ( ",", $value ['invoiceId'] );
  142. foreach ( $invoiceId as $k => $v ) {
  143. $invoiceId [$k] = $XDeode->encode ( $v );
  144. }
  145. $list [$key] ['invoiceList'] = array_combine ( $invoiceId, $invoiceSerial );
  146. $list [$key] ['iridAuthKey'] = $this->authcode ( $value ['irid'], '' );
  147. }
  148. return $list;
  149. }
  150. public function getInvoiceReceivablesByClaim($cid = 0, $sid = 0) {
  151. if (! empty ( $cid )) {
  152. $list ['csClaim'] = $this->find ( array (
  153. 'where' => "receivablesCategory='public' and receivablesStaff='public' and bindStatus=0",
  154. 'desc' => 'irid',
  155. 'asArray' => TRUE
  156. ) );
  157. foreach ( $list ['csClaim'] as $key => $value ) {
  158. $list ['csClaim'] [$key] ['iidKey'] = $this->authcode ( $value ['irid'], '' );
  159. // $list ['csClaim'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  160. }
  161. $list ['sClaim'] = $this->find ( array (
  162. 'where' => "receivablesCategory!='public' and receivablesStaff='public' and receivablesCategory like '" . $cid . ":%' and bindStatus=0",
  163. 'desc' => 'irid',
  164. 'asArray' => TRUE
  165. ) );
  166. foreach ( $list ['sClaim'] as $key => $value ) {
  167. $list ['sClaim'] [$key] ['iidKey'] = $this->authcode ( $value ['irid'], '' );
  168. }
  169. }
  170. if (! empty ( $cid ) && ! empty ( $sid )) {
  171. $list ['arClaim'] = $this->find ( array ( // and receivablesCategory like '" . $cid . ":%'
  172. 'where' => "receivablesCategory!='public' and receivablesStaff!='public' and receivablesStaff like '" . $sid . "-%' and bindStatus=0",
  173. 'desc' => 'irid',
  174. 'asArray' => TRUE
  175. ) );
  176. foreach ( $list ['arClaim'] as $key => $value ) {
  177. $staff = explode ( "-", $value ['receivablesStaff'] );
  178. $list ['arClaim'] [$key] ['sid'] = $staff [0];
  179. $list ['arClaim'] [$key] ['username'] = $staff [1];
  180. }
  181. $list ['myClaim'] = $this->find ( array (
  182. 'where' => " receivablesStaff like '" . $sid . "-%' and receivablesCategory like '" . $cid . ":%' and bindStatus=0",
  183. 'desc' => 'irid',
  184. 'asArray' => TRUE
  185. ) );
  186. foreach ( $list ['myClaim'] as $key => $value ) {
  187. $list ['myClaim'] [$key] ['iidKey'] = $this->authcode ( $value ['irid'], '' );
  188. $staff = explode ( "-", $value ['receivablesStaff'] );
  189. $list ['myClaim'] [$key] ['sid'] = $staff [0];
  190. $list ['myClaim'] [$key] ['username'] = $staff [1];
  191. }
  192. $list ['bindClaim'] = $this->find ( array (
  193. 'where' => " bindStatus=1 and receivablesStaff like '" . $sid . "-%' ",
  194. 'desc' => 'irid',
  195. 'limit' => 3,
  196. 'asArray' => TRUE
  197. ) );
  198. Doo::loadModel ( 'invoice' );
  199. $invoice = new invoice ();
  200. Doo::loadClass ( 'XDeode' );
  201. $XDeode = new XDeode ( 5 );
  202. // print_r($list ['bindClaim']);
  203. foreach ( $list ['bindClaim'] as $key => $value ) {
  204. $id = $invoice->getInvoiceByIid ( $value ['iid'] );
  205. $id ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  206. $list ['bindClaim'] [$key] ['invoice'] = $id;
  207. $list ['bindClaim'] [$key] ['iridKeyUrl'] = $XDeode->encode ( $value ['irid'] );
  208. }
  209. }
  210. return $list;
  211. }
  212. /**
  213. * 根据发票ID和绑定状态获得收款数据,绑定状态为ALL获取所有状态数据
  214. * @param string $iid 发票ID
  215. * @param string $bindStatus
  216. * @return mixed
  217. */
  218. public function getInvoiceReceivablesByIid($iid = "", $bindStatus = "All") {
  219. Doo::loadClass ( 'XDeode' );
  220. $XDeode = new XDeode ( 5 );
  221. $iid = $this->authcode ( $iid );
  222. $list = array ();
  223. if (! empty ( $iid ) && is_numeric ( $iid )) {
  224. $sql = '';
  225. if ($bindStatus == 1)
  226. $sql = " and bindStatus=1";
  227. elseif ($bindStatus === 0)
  228. $sql = " and bindStatus=0";
  229. $list = $this->find ( array (
  230. 'where' => " iid=" . $iid . $sql,
  231. 'asArray' => TRUE
  232. ) );
  233. }
  234. foreach ( $list as $key => $value ) {
  235. $list [$key] ['iidKey'] = $this->authcode ( $value ['irid'], '' );
  236. $staff = explode ( "-", $value ['receivablesStaff'] );
  237. $list [$key] ['sid'] = $staff [0];
  238. $list [$key] ['username'] = $staff [1];
  239. $list [$key] ['iridKey'] = $XDeode->encode ( $value ['irid'] );
  240. }
  241. return $list;
  242. }
  243. public function getInvoiceReceivablesInIridString($iridString = "") {
  244. $list = array ();
  245. if (! empty ( $iridString ))
  246. $list = $this->find ( array (
  247. 'where' => " irid in ( " . $iridString . " )",
  248. 'asArray' => TRUE
  249. ) );
  250. $price = 0;
  251. $serial = 0;
  252. Doo::loadClass ( 'XDeode' );
  253. $XDeode = new XDeode ( 5 );
  254. foreach ( $list as $key => $value ) {
  255. $price += $value ['receivablesPrice'];
  256. $list [$key] ['iridKeyK'] = $XDeode->encode ( $value ['irid'] );
  257. }
  258. if (! empty ( $list ))
  259. $list [0] ['sumPrice'] = $price;
  260. return $list;
  261. }
  262. /**
  263. * 根据收款ID获取一条相关数据
  264. * @param string $irid
  265. * @param string $con 附加获取收款条件 可以为空
  266. * @return unknown[]|mixed[]|string[]
  267. */
  268. public function getInvoiceReceivablesByIrid($irid = "", $con = "") {
  269. Doo::loadClass ( 'XDeode' );
  270. $XDeode = new XDeode ( 5 );
  271. if (! is_numeric ( $irid ))
  272. $irid = $this->authcode ( $irid );
  273. $detail = array ();
  274. if (! empty ( $irid ) && is_numeric ( $irid ))
  275. $detail = $this->getOne ( array (
  276. 'where' => " irid=" . $irid . $con,
  277. 'asArray' => TRUE
  278. ) );
  279. if (empty ( $detail ))
  280. return $detail;
  281. $detail ['cid'] = '';
  282. $detail ['title'] = '';
  283. if ($detail ['receivablesCategory'] != "PUBLIC") {
  284. $category = explode ( ":", $detail ['receivablesCategory'] );
  285. $detail ['cid'] = $category [0];
  286. $detail ['title'] = $category [1];
  287. }
  288. $detail ['sid'] = '';
  289. $detail ['username'] = '';
  290. if ($detail ['receivablesStaff'] != "PUBLIC") {
  291. $staff = explode ( "-", $detail ['receivablesStaff'] );
  292. $detail ['sid'] = $staff [0];
  293. $detail ['username'] = $staff [1];
  294. }
  295. $detail ['iridKey'] = $XDeode->encode ( $detail ['irid'] );
  296. $detail ['iridEn'] = $this->authcode ( $detail ['irid'], '' );
  297. return $detail;
  298. }
  299. public function getInvoiceReceivablesByIrsid($irid = "", $sid = "") {
  300. Doo::loadClass ( 'XDeode' );
  301. $XDeode = new XDeode ( 5 );
  302. $irid = $XDeode->decode ( $irid );
  303. $detail = array ();
  304. if (! empty ( $irid ) && is_numeric ( $irid )) {
  305. if (! empty ( $sid ))
  306. $detail = $this->getOne ( array (
  307. 'where' => " irid=" . $irid . " and receivablesStaff like '" . $sid . "_%'",
  308. 'asArray' => TRUE
  309. ) );
  310. else
  311. $detail = $this->getOne ( array (
  312. 'where' => " irid=" . $irid,
  313. 'asArray' => TRUE
  314. ) );
  315. $detail ['sid'] = '';
  316. $detail ['username'] = '';
  317. if (! empty ( $detail ['receivablesStaff'] )) {
  318. $staff = explode ( "-", $detail ['receivablesStaff'] );
  319. $detail ['sid'] = $staff [0];
  320. $detail ['username'] = $staff [1];
  321. }
  322. $detail ['iridKey'] = $XDeode->encode ( $detail ['irid'] );
  323. $detail ['iridEn'] = $this->authcode ( $detail ['irid'], '' );
  324. }
  325. return $detail;
  326. }
  327. /**
  328. * 添加一个收款
  329. * @param array $item 收款相关数据
  330. * @return number 返回收款ID
  331. */
  332. public function addInvoiceReceivables($item = array()) {
  333. $lid = 0;
  334. if (is_array ( $item ) && ! empty ( $item )) {
  335. foreach ( $item as $key => $value ) {
  336. $this->$key = $value;
  337. }
  338. $lid = $this->insert ();
  339. }
  340. return $lid;
  341. }
  342. /**
  343. * 根据参数字段更新相应字段(主键ID必须传)
  344. * @param array $item 相关需要更新的字段信息
  345. * @return number 返回发票ID
  346. */
  347. public function setInvoiceReceivablesByCondition($item = array()) {
  348. $lid = 0;
  349. if (is_array ( $item ) && ! empty ( $item )) {
  350. foreach ( $item as $key => $value ) {
  351. $this->$key = $value;
  352. }
  353. $lid = $this->update ();
  354. }
  355. return $lid;
  356. }
  357. /**
  358. * 根据irid 获取一组收款数据
  359. * @param string $irid
  360. * @return unknown
  361. */
  362. public function getInvoiceReceivablesInIrid($irid = '') {
  363. if (empty ( $irid ))
  364. return array ();
  365. $list = $this->find ( array (
  366. 'where' => " irid in(" . $irid . ")",
  367. 'asArray' => TRUE
  368. ) );
  369. foreach ( $list as $key => $value ) {
  370. }
  371. return $list;
  372. }
  373. function getInvoiceReceivablesByConditionPage($limit = 0, $con = "", $desc = 'desc', $descField = 'irid') {
  374. if (empty ( $limit ))
  375. return array ();
  376. $sql = 'select group_concat(b.invoiceSerial) as invoiceSerial ,group_concat(b.iid) as invoiceId ,a.*,b.invoicePrice
  377. from ' . $this->_table . ' as a left join CLD_invoice as b on
  378. find_in_set(b.iid,a.iidExtend) or a.iid=b.iid
  379. where ' . $con . ' group by a.irid ORDER BY ' . $descField . ' '.$desc.' limit ' . $limit . '
  380. ';
  381. $query = Doo::db ()->query ( $sql );
  382. $list = $query->fetchAll ();
  383. Doo::loadClass ( 'XDeode' );
  384. $XDeode = new XDeode ( 5 );
  385. Doo::loadModel ( 'invoiceROLog' );
  386. $invoiceROLog = new invoiceROLog ();
  387. foreach ( $list as $key => $value ) {
  388. $invoiceSerial = explode ( ',', $value ['invoiceSerial'] );
  389. $invoiceId = explode ( ",", $value ['invoiceId'] );
  390. if (empty($invoiceSerial[0]))
  391. $list [$key] ['invoiceList'] =array();
  392. else{
  393. foreach ( $invoiceId as $k => $v ) {
  394. if (!empty($v))
  395. $invoiceId [$k] = $XDeode->encode ( $v );
  396. }
  397. $list [$key] ['invoiceList'] = array_combine ( $invoiceId, $invoiceSerial );
  398. }
  399. $list [$key] ['iridKeyK'] = $XDeode->encode ( $value ['irid'] );
  400. $list [$key] ['iridKey'] = $this->authcode ( $value ['irid'], '' );
  401. $list [$key] ['irolg'] = $invoiceROLog->getInvoiceROLogByIrid ( $value ['irid'], 'desc' );
  402. }
  403. //print_r($list);
  404. return $list;
  405. }
  406. function getReceivablesByUntreadStatusPage($limit = 0, $con = "", $desc = 'desc', $descField = 'irid') {
  407. if (empty ( $limit ))
  408. return array ();
  409. $list = $this->find ( array (
  410. 'where' => $con, // " bindStatus=1 and receivablesStaff like '" . $sid . "_%' ".$con,
  411. 'limit' => $limit,
  412. $desc => $descField,
  413. 'asArray' => TRUE
  414. ) );
  415. Doo::loadClass ( 'XDeode' );
  416. $XDeode = new XDeode ( 5 );
  417. Doo::loadModel ( 'invoice' );
  418. $invoice = new invoice ();
  419. foreach ( $list as $key => $value ) {
  420. $list [$key] ['iridKeyK'] = $XDeode->encode ( $value ['irid'] );
  421. $list [$key] ['iridKey'] = $this->authcode ( $value ['irid'], '' );
  422. if (isset ( $value ['iid'] ) && ! empty ( $value ['iid'] )) {
  423. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  424. $list [$key] ['irList'] = $invoice->getInvoiceByIid ( $value ['iid'] );
  425. } else {
  426. $list [$key] ['iidKeyK'] = '';
  427. $list [$key] ['irList'] = array (
  428. 'invoiceSerial' => ''
  429. );
  430. }
  431. $list [$key] ['irolg'] = $invoiceROLog->getInvoiceROLogByIrid ( $value ['irid'], 'desc' );
  432. // $list [$key] ['sumPrice'] = 0;
  433. // if (! empty ( $list [$key] ['irList'] ))
  434. // $list [$key] ['sumPrice'] = $list [$key] ['irList'] [0] ['sumPrice'];
  435. // $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  436. }
  437. return $list;
  438. }
  439. function delInvoiceReceivablesByIrid($irid = 0) {
  440. if (! empty ( $irid ) && is_numeric ( $irid ))
  441. $this->delete ( array (
  442. 'where' => 'irid=' . $irid
  443. ) );
  444. }
  445. /**
  446. * 加密或解密指定字符串
  447. *
  448. * @param string $string 要加密或解密的字符串
  449. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  450. * @param string $key 加解密的key
  451. * @param $expiry 超时值
  452. *
  453. */
  454. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  455. $ckey_length = 4;
  456. if (! $key) {
  457. $key = $this->INVOICEKEY;
  458. }
  459. $key = md5 ( $key );
  460. $keya = md5 ( substr ( $key, 0, 16 ) );
  461. $keyb = md5 ( substr ( $key, 16, 16 ) );
  462. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  463. $cryptkey = $keya . md5 ( $keya . $keyc );
  464. $key_length = strlen ( $cryptkey );
  465. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  466. $string_length = strlen ( $string );
  467. $result = '';
  468. $box = range ( 0, 255 );
  469. $rndkey = array ();
  470. for($i = 0; $i <= 255; $i ++) {
  471. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  472. }
  473. for($j = $i = 0; $i < 256; $i ++) {
  474. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  475. $tmp = $box [$i];
  476. $box [$i] = $box [$j];
  477. $box [$j] = $tmp;
  478. }
  479. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  480. $a = ($a + 1) % 256;
  481. $j = ($j + $box [$a]) % 256;
  482. $tmp = $box [$a];
  483. $box [$a] = $box [$j];
  484. $box [$j] = $tmp;
  485. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  486. }
  487. if ($operation == 'DECODE') {
  488. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  489. return substr ( $result, 26 );
  490. } else {
  491. return '';
  492. }
  493. } else {
  494. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  495. }
  496. }
  497. }
  498. ?>