invoiceReceivables.php 17 KB

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