invoice.php 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. private $FUZZY = 'FUZZY';
  13. private $EXACTLY = "EXACTLY";
  14. /**
  15. *
  16. * @var integer $iid 发票ID
  17. */
  18. public $iid;
  19. public $isid;
  20. public $trainId;
  21. public $lossIid;
  22. public $ipSource;
  23. /**
  24. *
  25. * @var integer $status 审批状态
  26. */
  27. public $status;
  28. /**
  29. *
  30. * @var string $invoiceManage 当前审批组人员
  31. */
  32. public $printStatus;
  33. public $postStatus;
  34. public $invoiceManage;
  35. /**
  36. *
  37. * @var string $pendingApprovals 当前需要审批的人
  38. */
  39. public $pendingApprovals;
  40. /**
  41. *
  42. * @var string $processApprovals 已经审批过的人员
  43. */
  44. public $processApprovals;
  45. /**
  46. *
  47. * @var string $invoiceSerial 发票单号
  48. */
  49. public $invoiceSerial;
  50. /**
  51. *
  52. * @var integer $invoicePrice 发票金额
  53. */
  54. public $invoicePrice;
  55. public $invoiceQuantity;
  56. public $invoiceUnitPrice;
  57. /**
  58. * 管理组人员:发票打印,
  59. * @var unknown
  60. */
  61. public $moldManage;
  62. /**
  63. *
  64. * @var integer $cid 办事处ID
  65. */
  66. public $cid;
  67. /**
  68. *
  69. * @var string $categoryName 办事处名称
  70. */
  71. public $categoryName;
  72. /**
  73. *
  74. * @var integer $sid 提交发票人ID
  75. */
  76. public $sid;
  77. /**
  78. *
  79. * @var string $userName 提交人名字
  80. */
  81. public $printer;
  82. public $userName;
  83. /**
  84. *
  85. * @var string $remark 备注
  86. */
  87. public $remark;
  88. /**
  89. *
  90. * @var string $invoiceElement 发票内容
  91. */
  92. public $invoiceElement;
  93. /**
  94. *
  95. * @var string $invoiceType 发票类型
  96. */
  97. public $invoiceType;
  98. /**
  99. *
  100. * @var string $invoiceTitle 发票抬头
  101. */
  102. public $invoiceTitle;
  103. /**
  104. *
  105. * @var string $invoiceCompany 开票公司
  106. */
  107. public $invoiceCompany;
  108. /**
  109. *
  110. * @var string $invoiceNo 发票号
  111. */
  112. public $invoiceNo;
  113. /**
  114. *
  115. * @var string $TIN 纳税人识别码
  116. */
  117. public $TIN;
  118. /**
  119. *
  120. * @var string $address 注册地址
  121. */
  122. public $address;
  123. /**
  124. *
  125. * @var string $phone 电话
  126. */
  127. public $phone;
  128. /**
  129. *
  130. * @var string $bank 开户银行
  131. */
  132. public $bank;
  133. /**
  134. *
  135. * @var string $bankAccount 银行账户
  136. */
  137. public $bankAccount;
  138. /**
  139. *
  140. * @var integer $doPost 邮寄
  141. */
  142. public $doPost;
  143. /**
  144. *
  145. * @var string $recipients 收件人
  146. */
  147. public $recipients;
  148. /**
  149. *
  150. * @var string $recipientsPhone 收件人电话
  151. */
  152. public $recipientsPhone;
  153. /**
  154. *
  155. * @var string $recipientsAddress 收件地址
  156. */
  157. public $recipientsAddress;
  158. /**
  159. *
  160. * @var string $mailItems 邮寄物品
  161. */
  162. public $mailItems;
  163. public $mailItemsJson;
  164. public $expressCompany;
  165. public $expressNumber;
  166. public $actualItems;
  167. public $poster;
  168. public $untreadReason;
  169. public $untreadPost;
  170. public $untreadCompany;
  171. public $untreadNumber;
  172. public $untreadItems;
  173. public $untreadStatus;
  174. public $irid;
  175. public $bindTime;
  176. /**
  177. *
  178. * @var date $date 提交时间
  179. */
  180. public $date;
  181. public $isDelete;
  182. public $approvalTime;
  183. /**
  184. *
  185. * @var date $updateTime 更新时间
  186. */
  187. public $updateTime;
  188. /**
  189. *
  190. * @var date $printTime 打印时间
  191. */
  192. public $printTime;
  193. public $postTime;
  194. public $_table = 'CLD_invoice';
  195. public $_primarykey = 'iid';
  196. public $_fields = array (
  197. 'iid',
  198. 'isid',
  199. 'trainId',
  200. 'ipSource',
  201. 'lossIid',
  202. 'invoiceManage',
  203. 'pendingApprovals',
  204. 'processApprovals',
  205. 'invoiceSerial',
  206. 'status',
  207. 'printStatus',
  208. 'postStatus',
  209. 'untreadStatus',
  210. 'invoiceQuantity',
  211. 'invoiceUnitPrice',
  212. 'invoicePrice',
  213. 'moldManage',
  214. 'cid',
  215. 'categoryName',
  216. 'irid',
  217. 'remark',
  218. 'invoiceElement',
  219. 'invoiceType',
  220. 'invoiceTitle',
  221. 'invoiceCompany',
  222. 'invoiceNo',
  223. 'TIN',
  224. 'address',
  225. 'phone',
  226. 'bank',
  227. 'bankAccount',
  228. 'doPost',
  229. 'recipients',
  230. 'recipientsPhone',
  231. 'recipientsAddress',
  232. 'mailItems',
  233. 'mailItemsJson',
  234. 'expressCompany',
  235. 'expressNumber',
  236. 'actualItems',
  237. 'untreadReason',
  238. 'untreadPost',
  239. 'untreadCompany',
  240. 'untreadNumber',
  241. 'untreadItems',
  242. 'poster',
  243. 'sid',
  244. 'userName',
  245. 'printer',
  246. 'date',
  247. 'isDelete',
  248. 'approvalTime',
  249. 'updateTime',
  250. 'printTime',
  251. 'postTime',
  252. 'bindTime'
  253. );
  254. /**
  255. * 根据发票ID获取一条发票数据
  256. * @param number $iid 发票ID
  257. * @param number $select 需要获取的字段,为空获取全部数据
  258. * @return array|array() 返回发票数据
  259. */
  260. public function getInvoiceByIid($iid = 0, $select = "") {
  261. if (! is_numeric ( $iid ))
  262. $iid = $this->authcode ( $iid );
  263. $condition = array (
  264. 'where' => "iid=" . $iid,
  265. 'asArray' => TRUE
  266. );
  267. if (! empty ( $select ))
  268. $condition += array (
  269. 'select' => $select
  270. );
  271. $Detail = array ();
  272. if (is_numeric ( $iid ) && ! empty ( $iid ))
  273. $Detail = $this->getOne ( $condition );
  274. if (empty ( $Detail ))
  275. return $Detail;
  276. if (isset ( $Detail ['expressCompany'] ) && ! empty ( $Detail ['expressCompany'] )) {
  277. $express = explode ( ":", $Detail ['expressCompany'] );
  278. $Detail ['expressCompany'] = $express [0];
  279. $Detail ['expressCom'] = $express [1];
  280. } else {
  281. $Detail ['expressCompany'] = '';
  282. $Detail ['expressCom'] = '';
  283. }
  284. Doo::loadClass ( 'XDeode' );
  285. $XDeode = new XDeode ( 5 );
  286. $Detail ['iidKeyUrl'] = $XDeode->encode ( $Detail ['iid'] );
  287. $Detail ['iidKey'] = $this->authcode ( $Detail ['iid'], '' );
  288. Doo::loadModel ( 'L_category' );
  289. $lCategory = new L_category ();
  290. $Detail ['category'] = $lCategory->getCategory ();
  291. return $Detail;
  292. }
  293. /**
  294. * 根据多个iid获得多个发票
  295. * @param number $iid
  296. */
  297. function getInvoiceInIid($iid = 0, $select = '') {
  298. if (empty ( $iid ))
  299. die ( 'illegal request' );
  300. $condition = array (
  301. 'where' => "iid in ( " . $iid . ")",
  302. 'asArray' => TRUE
  303. );
  304. if (! empty ( $select ))
  305. $condition += array (
  306. 'select' => $select
  307. );
  308. $list = $this->find ( $condition );
  309. Doo::loadClass ( 'XDeode' );
  310. $XDeode = new XDeode ( 5 );
  311. foreach ( $list as $key => $value ) {
  312. $list [$key] ['iidKey'] = $XDeode->encode ( $value ['iid'] );
  313. }
  314. return $list;
  315. }
  316. function setInvoiceLoss($iid = "") {
  317. if (empty ( $iid ))
  318. return 0;
  319. $sql = 'insert into ' . $this->_table . '(`status`, `printStatus`, `postStatus`, `untreadStatus`, `invoiceManage`, `pendingApprovals`, `processApprovals`, `invoiceSerial`,`invoiceQuantity`, `invoiceUnitPrice`, `invoicePrice`, `moldManage`, `cid`, `categoryName`, `sid`, `userName`, `printer`, `irid`, `remark`, `invoiceElement`, `invoiceType`, `invoiceTitle`, `invoiceCompany`, `invoiceNo`, `TIN`, `address`, `phone`, `bank`, `bankAccount`, `doPost`, `recipients`, `recipientsPhone`, `recipientsAddress`, `mailItems`, `mailItemsJson`, `expressCompany`, `expressNumber`, `actualItems`, `untreadReason`, `untreadPost`, `untreadCompany`, `untreadNumber`, `untreadItems`, `poster`, `date`, `isDelete`, `updateTime`, `printTime`, `postTime`, `bindTime`)
  320. select `status`, `printStatus`, `postStatus`, `untreadStatus`, `invoiceManage`, `pendingApprovals`, `processApprovals`, `invoiceSerial`,`invoiceQuantity`, `invoiceUnitPrice`, `invoicePrice`, `moldManage`, `cid`, `categoryName`, `sid`, `userName`, `printer`, `irid`, `remark`, `invoiceElement`, `invoiceType`, `invoiceTitle`, `invoiceCompany`, `invoiceNo`, `TIN`, `address`, `phone`, `bank`, `bankAccount`, `doPost`, `recipients`, `recipientsPhone`, `recipientsAddress`, `mailItems`, `mailItemsJson`, `expressCompany`, `expressNumber`, `actualItems`, `untreadReason`, `untreadPost`, `untreadCompany`, `untreadNumber`, `untreadItems`, `poster`, `date`, `isDelete`, `updateTime`, `printTime`, `postTime`, `bindTime` from ' . $this->_table . ' where iid=' . $iid;
  321. $query = Doo::db ()->query ( $sql );
  322. return Doo::db ()->lastInsertId ();
  323. }
  324. function getInvoiceByInvoiceNo($invoiceNo = '') {
  325. if (empty ( $invoiceNo ))
  326. return array ();
  327. $detail = $this->getOne ( array (
  328. 'where' => "invoiceNo= '" . $invoiceNo . "'",
  329. 'asArray' => TRUE
  330. ) );
  331. return $detail;
  332. }
  333. /**
  334. * 根据标题或者开票单位获得相关信息 - 准确查询
  335. * @param string $title 查询条件
  336. * @param string $query 默认模糊查询
  337. */
  338. public function getInvoiceByTitle($title = "", $query = "FUZZY", $cond = '', $fkq = false) {
  339. $detail = array ();
  340. if (! empty ( $title )) {
  341. $condition = array (
  342. 'asArray' => TRUE
  343. );
  344. if ($query == $this->FUZZY)
  345. $condition += array (
  346. 'where' => " (invoiceTitle like '%" . $title . "%' or invoiceCompany like '%" . $title . "%') " . $cond
  347. );
  348. else
  349. $condition += array (
  350. 'where' => "(invoiceTitle = '" . $title . "' or invoiceCompany= '" . $title . "') " . $cond
  351. );
  352. Doo::loadClass ( 'XDeode' );
  353. $XDeode = new XDeode ( 5 );
  354. if ($fkq) {
  355. $detail = $this->find ( $condition );
  356. foreach ( $detail as $key => $value ) {
  357. $detail [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  358. $detail [$key] ['iidKeyXD'] = $XDeode->encode ( $value ['iid'] );
  359. }
  360. } else {
  361. $detail = $this->getOne ( $condition );
  362. if (! empty ( $detail )) {
  363. $detail ['iidKey'] = $this->authcode ( $detail ['iid'], '' );
  364. $detail ['iidKeyXD'] = $XDeode->encode ( $detail ['iid'] );
  365. }
  366. }
  367. }
  368. return $detail;
  369. }
  370. public function getInvoiceByIsid($iid = "", $sid = 0) {
  371. if (empty ( $iid ) || empty ( $sid ))
  372. return array ();
  373. Doo::loadClass ( 'XDeode' );
  374. $XDeode = new XDeode ( 5 );
  375. $iid = $XDeode->decode ( $iid );
  376. if (! is_numeric ( $iid ))
  377. return array ();
  378. $detail = $this->getOne ( array (
  379. 'where' => "iid= '" . $iid . "'", // and sid=".$sid,
  380. 'asArray' => TRUE
  381. ) );
  382. $detail ['expressCompany'] = '';
  383. $detail ['expressCom'] = '';
  384. if (! empty ( $detail ['expressCompany'] )) {
  385. $express = explode ( ":", $detail ['expressCompany'] );
  386. $detail ['expressCompany'] = $express [0];
  387. $detail ['expressCom'] = $express [1];
  388. }
  389. $detail ['iidKeyXD'] = $this->authcode ( $detail ['iid'], '' );
  390. return $detail;
  391. }
  392. /**
  393. * 统计培训班开票
  394. */
  395. function sumOfinvoiceTrain($itidSql) {
  396. $sql = 'select sum(invoicePrice) as invoicePrice ,trainId
  397. from ' . $this->_table . '
  398. where trainId in (' . $itidSql . ') and status=2 and printStatus=1 and (untreadStatus =0 or untreadStatus=3)
  399. GROUP BY trainId';
  400. $query = Doo::db ()->query ( $sql );
  401. return $result = $query->fetchAll ();
  402. }
  403. function sumOfInvoiceRecelvablesTrain($itidSql) {
  404. $sql = 'select sum(b.receivablesPrice) as receivablesPrice ,a.trainId,count(*) as count
  405. from ' . $this->_table . ' as a left join CLD_invoiceReceivables as b on find_in_set(a.irid,b.irid)
  406. where a.trainId in (' . $itidSql . ') and a.status=2 and a.printStatus=1 and (a.untreadStatus =0 or a.untreadStatus=3) and a.irid!=""
  407. GROUP BY a.trainId';
  408. $query = Doo::db ()->query ( $sql );
  409. return $result = $query->fetchAll ();
  410. }
  411. function getInvoiceByTrainingCount($itid=0){
  412. if (empty($itid))
  413. return 0;
  414. $detail = $this->count ( array (
  415. 'where' => "trainId= '" . $itid . "' and untreadStatus!=2", // and sid=".$sid,
  416. 'asArray' => TRUE
  417. ) );
  418. return $detail;
  419. }
  420. /**
  421. * 根据仓库ID获得发票
  422. * @param string $Istoreid
  423. * @return unknown
  424. */
  425. public function getInvoiceByIstoreid($Istoreid = "", $select = "") {
  426. if (empty ( $Istoreid ))
  427. return array ();
  428. $condition = array (
  429. 'where' => "isid in ( " . $Istoreid . ")",
  430. 'asArray' => TRUE
  431. );
  432. if (! empty ( $select ))
  433. $condition += array (
  434. 'select' => $select
  435. );
  436. $detail = $this->find ( $condition );
  437. return $detail;
  438. }
  439. /**
  440. * 获取单个可以打印的发票数据
  441. * @param number $iid 发票id
  442. * @param number $select 需要获取的字段,为空获取全部数据
  443. * @return array|array() 返回发票数据
  444. */
  445. public function getInvoiceByPrint($iid = 0, $select = "") {
  446. $condition = array (
  447. 'where' => "iid= '" . $iid . "' and status=2 and untreadStatus=0 ",
  448. 'asArray' => TRUE
  449. );
  450. if (! empty ( $select ))
  451. $condition += array (
  452. 'select' => $select
  453. );
  454. $detail = array ();
  455. if (! empty ( $iid ) && is_numeric ( $iid ))
  456. $detail = $this->getOne ( $condition );
  457. return $detail;
  458. }
  459. /**
  460. * 添加一个发票并进入审批状态
  461. * @param array $item 发票相关数据
  462. * @return number 返回发票ID
  463. */
  464. public function addInvoice($item = array()) {
  465. $lid = 0;
  466. if (is_array ( $item ) && ! empty ( $item )) {
  467. foreach ( $item as $key => $value ) {
  468. $this->$key = $value;
  469. }
  470. $lid = $this->insert ();
  471. }
  472. return $lid;
  473. }
  474. /**
  475. * 根据参数字段更新相应字段(主键ID必须传)
  476. * @param array $item 相关需要更新的字段信息
  477. * @return number 返回发票ID
  478. */
  479. public function setInvoiceByCondition($item = array()) {
  480. $lid = 0;
  481. if (is_array ( $item ) && ! empty ( $item )) {
  482. foreach ( $item as $key => $value ) {
  483. $this->$key = $value;
  484. }
  485. $lid = $this->update ();
  486. }
  487. return $lid;
  488. }
  489. /**
  490. * 根据参数字段批量更新相应字段(主键ID必须传)
  491. * @param array $item 相关需要更新的字段信息
  492. * @return number 返回发票ID
  493. */
  494. public function BatchInvoiceByCondition($item = array(), $iid = "") {
  495. $lid = 0;
  496. if (is_array ( $item ) && ! empty ( $item )) {
  497. foreach ( $item as $key => $value ) {
  498. $this->$key = $value;
  499. }
  500. if (! empty ( $iid )) {
  501. $condition = array (
  502. 'where' => "iid in (" . $iid . ")",
  503. 'asArray' => TRUE
  504. );
  505. $this->update ( $condition );
  506. } else
  507. $lid = $this->update ();
  508. }
  509. return $lid;
  510. }
  511. /**
  512. * 根据管理组和发票当前状态获得打印发票数据;其中当iid为空时获取所有发票数据,反之获取1条数据
  513. * @param number $sid 管理组人员ID
  514. * @param number $status 状态 为0时获取可以打印数据,为1时获取已经完成打印数据
  515. * @param number $iid 发票ID
  516. * @return array|array() 返回所有发票数据,当iid有值时返回一条数据
  517. */
  518. public function getPrintInvoiceByManage($sid = 0, $status = 0, $iid = 0) {
  519. $list = array ();
  520. if (! empty ( $sid ) && empty ( $iid ))
  521. $list = $this->find ( array (
  522. 'where' => "status=2 and printStatus=" . $status . " and untreadStatus=0 and moldManage like '%[\"" . $sid . "\",%'",
  523. 'desc' => 'approvalTime',
  524. 'limit'=>10,
  525. 'asArray' => TRUE
  526. ) );
  527. elseif (! empty ( $sid ) && ! empty ( $iid ))
  528. $list = $this->getOne ( array (
  529. 'where' => "status=2 and printStatus=" . $status . " and untreadStatus=0 and moldManage like '%[\"" . $sid . "\",%' and iid=" . $iid,
  530. 'asArray' => TRUE
  531. ) );
  532. return $list;
  533. }
  534. /**
  535. * 获得与我相关的发票数据,其中包含 处理中,待处理,最旧入账等数据;当iid有值时获取一条关于sid的发票
  536. * @param number $sid 用户ID
  537. * @param number $iid 发票ID
  538. * @return array|array()
  539. */
  540. public function getMyInvoice($sid = 0, $iid = 0) {
  541. Doo::loadModel ( 'invoiceOperationLog' );
  542. $invoiceOperationLog = new invoiceOperationLog ();
  543. Doo::loadClass ( 'XDeode' );
  544. $XDeode = new XDeode ( 5 );
  545. $list = array ();
  546. if (! empty ( $iid )) {
  547. $list = $this->getOne ( array (
  548. 'where' => " sid=" . $sid . " and iid=" . $iid,
  549. 'asArray' => TRUE
  550. ) );
  551. } elseif (! empty ( $sid ) && empty ( $iid )) {
  552. $list ['pendingInvoice'] = array ();
  553. $list ['handleInvoice'] = array ();
  554. $list ['pendingInvoice'] = $this->find ( array ( // 加入邮件中也为 处理中 已退票
  555. 'where' => "((status=1 and untreadStatus=0) or (status=2 and printStatus=0 and untreadStatus=0) or
  556. ( status=2 and printStatus=1 and untreadStatus=1 and untreadPost=0)
  557. or (postStatus=0 and doPost=1 and status=2 and untreadStatus=0) ) and isDelete=0 and sid=" . $sid,
  558. 'desc' => 'iid',
  559. 'asArray' => TRUE
  560. ) );
  561. foreach ( $list ['pendingInvoice'] as $key => $value ) {
  562. $list ['pendingInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  563. $list ['pendingInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  564. }
  565. $list ['handleInvoice'] = $this->find ( array (
  566. // 需求变更 -去除出票状态的数据 or (status=2 and printStatus=1 and untreadStatus=0 ) or (status=2 and printStatus=1 and untreadStatus=2 )
  567. // 新加入退票中 需要邮寄的发票
  568. 'where' => "(status=3 or status=4 or (status=2 and untreadStatus=2 ) or ( status=2 and untreadStatus=1 ) ) and isDelete=0 and sid=" . $sid,
  569. 'desc' => 'iid',
  570. 'asArray' => TRUE
  571. ) );
  572. foreach ( $list ['handleInvoice'] as $key => $value ) {
  573. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  574. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  575. $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  576. }
  577. }
  578. return $list;
  579. }
  580. /**
  581. * 获得所有未入账发票
  582. * @param number $invoiceTC 发票title
  583. */
  584. public function getInvoiceByUnAccount($invoiceTC = '', $iridExist = false) {
  585. Doo::loadClass ( 'XDeode' );
  586. $XDeode = new XDeode ( 5 );
  587. $title = "";
  588. if (! empty ( $invoiceTC ))
  589. $title = ' and (invoiceTitle like "%' . $invoiceTC . '%" or invoiceCompany like "%' . $invoiceTC . '%" )';
  590. $iridString = ' and irid="" ';
  591. if ($iridExist)
  592. $iridString = ' and irid!="" ';
  593. $list = $this->find ( array ( //
  594. 'where' => "status=2 and printStatus=1 " . $iridString . " and untreadStatus=0 and ( (doPost=1 and postStatus=1) or doPost=0 ) and isDelete=0 " . $title,
  595. 'desc' => 'iid',
  596. 'limit' => 5,
  597. 'asArray' => TRUE
  598. ) );
  599. foreach ( $list as $key => $value ) {
  600. $list [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  601. }
  602. return $list;
  603. }
  604. /**
  605. * 获得与我有关可收款的发票,已经出票的发票
  606. * @param number $sid 开票人ID
  607. */
  608. public function getInvoiceByReceivables($sid = 0) {
  609. Doo::loadClass ( 'XDeode' );
  610. $XDeode = new XDeode ( 5 );
  611. $list ['handleInvoice'] = $this->find ( array (
  612. 'where' => "(status=2 and printStatus=1 and untreadStatus=0 ) and isDelete=0 and sid=" . $sid,
  613. 'desc' => 'iid',
  614. 'asArray' => TRUE
  615. ) );
  616. foreach ( $list ['handleInvoice'] as $key => $value ) {
  617. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  618. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  619. // $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  620. }
  621. return $list ['handleInvoice'];
  622. }
  623. /**
  624. * 获取需要邮寄的发票
  625. * @param integer $postStatus 0为获取需要邮寄的发票,1为获取已经邮寄的发票
  626. * @return array|array() 返回相关数据
  627. */
  628. public function getPostByInvoice($postStatus = 0,$desc="desc") {
  629. Doo::loadClass ( 'XDeode' );
  630. $XDeode = new XDeode ( 5 );
  631. $list = $this->find ( array ( // and untreadStatus=0
  632. 'where' => "postStatus=" . $postStatus . " and printStatus=1 and doPost=1 and status=2 and isDelete=0",
  633. 'limit' => 7,
  634. $desc => 'iid',
  635. 'asArray' => TRUE
  636. ) );
  637. foreach ( $list as $key => $value ) {
  638. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  639. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  640. $list [$key] ['expressCompany'] = '';
  641. $list [$key] ['expressCom'] = '';
  642. if (! empty ( $value ['expressCompany'] )) {
  643. $express = explode ( ":", $value ['expressCompany'] );
  644. $list [$key] ['expressCompany'] = $express [0];
  645. $list [$key] ['expressCom'] = $express [1];
  646. }
  647. }
  648. return $list;
  649. }
  650. /**
  651. * 根据退票状态获得相关数据,iid有值时只获得一条数据
  652. * @param number $untreadStatus 退票状态
  653. * @param number $iid 发票ID
  654. */
  655. public function getInvoiceByUntreadStatus($untreadStatus = 1, $iid = 0) {
  656. Doo::loadClass ( 'XDeode' );
  657. $XDeode = new XDeode ( 5 );
  658. if (empty ( $iid )) {
  659. $list = $this->find ( array ( // printStatus=1 and
  660. 'where' => " untreadStatus=" . $untreadStatus . " and status=2 ",
  661. 'limit'=>10,
  662. 'asArray' => TRUE
  663. ) );
  664. if ($untreadStatus == 2) {
  665. Doo::loadModel ( 'invoiceOperationLog' );
  666. $invoiceOperationLog = new invoiceOperationLog ();
  667. }
  668. foreach ( $list as $key => $value ) {
  669. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  670. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  671. $list [$key] ['untreadCompany'] = '';
  672. $list [$key] ['untreadCom'] = '';
  673. if (! empty ( $value ['untreadCompany'] )) {
  674. $express = explode ( ":", $value ['untreadCompany'] );
  675. $list [$key] ['untreadCompany'] = $express [0];
  676. $list [$key] ['untreadCom'] = $express [1];
  677. }
  678. if ($untreadStatus == 2) {
  679. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  680. }
  681. $list [$key] ['lossDetail'] = array ();
  682. if ($value ['lossIid'] != 0) {
  683. $detail = $this->getOne ( array (
  684. 'where' => "iid=" . $value ['lossIid'],
  685. 'asArray' => TRUE
  686. ) );
  687. $detail ['iidKeyK'] = $XDeode->encode ( $detail ['iid'] );
  688. $list [$key] ['lossDetail'] = $detail;
  689. }
  690. }
  691. } else {
  692. $list = $this->getOne ( array (
  693. 'where' => "iid=" . $iid . " and printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2",
  694. 'asArray' => TRUE
  695. ) );
  696. }
  697. return $list;
  698. }
  699. /**
  700. * 审批中获取相关数据 包括最近的一条操作记录
  701. * @param number $status.
  702. * @param number $limit.
  703. */
  704. function getInvoiceByProcessStatus($limit = 10, $desc = 'desc') {
  705. Doo::loadClass ( 'XDeode' );
  706. $XDeode = new XDeode ( 5 );
  707. $list = $this->find ( array (
  708. 'where' => " (status=1 or status=2 or status=3 or status=4) and processApprovals!=''",
  709. 'limit' => $limit,
  710. $desc => 'iid',
  711. 'asArray' => TRUE
  712. ) );
  713. Doo::loadModel ( 'invoiceOperationLog' );
  714. $invoiceOperationLog = new invoiceOperationLog ();
  715. foreach ( $list as $key => $value ) {
  716. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  717. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], $value ['status'] );
  718. }
  719. return $list;
  720. }
  721. function getInvoiceByUntreadStatusPage($limit = 0, $con = "", $desc = 'desc', $olStatus = "desc") {
  722. if (empty ( $limit ) || empty ( $con ))
  723. return array ();
  724. $listCount = $this->count ( array (
  725. 'where' => $con,
  726. 'asArray' => TRUE
  727. ) );
  728. $list = $this->find ( array (
  729. 'where' => $con,
  730. 'limit' => $limit,
  731. $desc => 'approvalTime',
  732. 'asArray' => TRUE
  733. ) );//echo $con;
  734. Doo::loadClass ( 'XDeode' );
  735. $XDeode = new XDeode ( 5 );
  736. Doo::loadModel ( 'invoiceReceivables' );
  737. $invoiceReceivables = new invoiceReceivables ();
  738. Doo::loadModel ( 'invoiceOperationLog' );
  739. $invoiceOperationLog = new invoiceOperationLog ();
  740. foreach ( $list as $key => $value ) {
  741. $list [$key] ['count'] = $listCount;
  742. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  743. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  744. $list [$key] ['untreadCompany'] = '';
  745. $list [$key] ['untreadCom'] = '';
  746. if (! empty ( $value ['untreadCompany'] )) {
  747. $express = explode ( ":", $value ['untreadCompany'] );
  748. $list [$key] ['untreadCompany'] = $express [0];
  749. $list [$key] ['untreadCom'] = $express [1];
  750. }
  751. $list [$key] ['irList'] = $invoiceReceivables->getInvoiceReceivablesInIridString ( $value ['irid'] );
  752. if ($olStatus == 'desc')
  753. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  754. else
  755. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], $olStatus );
  756. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  757. $list [$key] ['OL'] = $invoiceOperationLog->getInvoiceOperationInStatus ( $value ['iid'], '2,3,4' );
  758. // print_r($list [$key] ['OperationLog']);
  759. $list [$key] ['sumPrice'] = 0;
  760. if (! empty ( $list [$key] ['irList'] ))
  761. $list [$key] ['sumPrice'] = $list [$key] ['irList'] [0] ['sumPrice'];
  762. $list [$key] ['diffPrice'] = $list [$key]['invoicePrice'];
  763. if(!empty($list [$key] ['irList'] [0] ['sumPrice']))
  764. $list [$key] ['diffPrice'] = $list [$key]['invoicePrice']-$list [$key] ['irList'] [0] ['sumPrice'];
  765. // $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  766. $list [$key] ['lossDetail'] = array ();
  767. if ($value ['lossIid'] != 0) {
  768. $detail = $this->getOne ( array (
  769. 'where' => "iid=" . $value ['lossIid'],
  770. 'asArray' => TRUE
  771. ) );
  772. $detail ['iidKeyK'] = $XDeode->encode ( $detail ['iid'] );
  773. $list [$key] ['lossDetail'] = $detail;
  774. }
  775. $lossDate = strtotime ( "+1 month", strtotime ( $list [$key] ['OperationLog'] ['date'] ) );
  776. $nowDate = strtotime ( 'now' );
  777. $list [$key] ['lossShow'] = false;
  778. if ($lossDate < $nowDate)
  779. $list [$key] ['lossShow'] = true;
  780. }
  781. return $list;
  782. }
  783. /**
  784. * 公司应收款汇总
  785. * @param unknown $con
  786. */
  787. function getInvoiceCompanyStatistics($con=''){
  788. if(empty($con))
  789. return array();
  790. $sql = 'select invoicePrice,iid
  791. from ' . $this->_table . '
  792. where '.$con.'
  793. ';
  794. $query = Doo::db ()->query ( $sql );
  795. $result = $query->fetchAll ();
  796. $iidlist=array();
  797. $invoicePrice=0;
  798. foreach ($result as $value){
  799. array_push($iidlist, $value['iid']);
  800. $invoicePrice+=$value['invoicePrice'];
  801. }
  802. $iidString=implode(',', $iidlist);
  803. //获得已收款发票
  804. if (!empty($iidString)){
  805. $sql = 'select sum(receivablesPrice) as receivablesPrice
  806. from CLD_invoiceReceivables
  807. where iid in (' . $iidString . ') and bindStatus=1
  808. GROUP BY bindStatus';
  809. $query = Doo::db ()->query ( $sql );
  810. $result = $query->fetch ();
  811. }else{
  812. $result['receivablesPrice']=0;
  813. }
  814. return array('invoicePrice'=>$invoicePrice,'receivablesPrice'=>$result['receivablesPrice'],'diffPrice'=>$invoicePrice-$result['receivablesPrice']);
  815. }
  816. /**
  817. * 获得当前月份办事处入账完成度
  818. * @param number $cid
  819. */
  820. function getInvoiceScheduleByCid($cid = 0, $month = true) {
  821. if (empty ( $cid ))
  822. return 0;
  823. if ($month === false)
  824. $monthSql = "";
  825. else {
  826. $month = date ( "m" );
  827. $monthSql = " and Month(date)=" . $month;
  828. }
  829. // 完成打印
  830. $recordCount = $this->count ( array (
  831. 'where' => " status=2 and printStatus=1 and untreadStatus=3 " . $monthSql . " and cid =" . $cid,
  832. 'asArray' => TRUE
  833. ) );
  834. // 当月创建的发票
  835. $allCount = $this->count ( array (
  836. 'where' => " isDelete=0 " . $monthSql . " and cid =" . $cid,
  837. 'asArray' => TRUE
  838. ) );
  839. if ($recordCount==0)
  840. return 0;
  841. return $recordCount / $allCount;
  842. }
  843. /**
  844. * 或者办事处成员的完成度
  845. */
  846. function getInvoiceScheduleByCidGroupSid($cid = 0) {
  847. if (empty ( $cid ))
  848. return array();
  849. //完成发票
  850. $recordCount = $this->find ( array (
  851. 'select'=>'count(*) as count,sid',
  852. 'where' => " status=2 and printStatus=1 and untreadStatus=3 and cid =" . $cid,
  853. 'groupby'=>'sid',
  854. 'asArray' => TRUE
  855. ) );
  856. //当月创建的发票
  857. $allCount = $this->find ( array (
  858. 'select'=>'count(*) as count,sid',
  859. 'where' => " isDelete=0 and cid =" . $cid,
  860. 'groupby'=>'sid',
  861. 'asArray' => TRUE
  862. ) );
  863. $list=array();
  864. foreach ($recordCount as $key=>$value){
  865. foreach ($allCount as $k=>$v){
  866. if ($value['sid']==$v['sid']){
  867. if ($value['count']==0)
  868. $list[$value['sid']]=0;
  869. else
  870. $list[$value['sid']]=(int)(($value['count'] / $v['count'])*100);
  871. }
  872. }
  873. }
  874. return $list;
  875. }
  876. /**
  877. * 加密或解密指定字符串
  878. *
  879. * @param string $string 要加密或解密的字符串
  880. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  881. * @param string $key 加解密的key
  882. * @param $expiry 超时值
  883. *
  884. */
  885. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  886. $ckey_length = 4;
  887. if (! $key) {
  888. $key = $this->INVOICEKEY;
  889. }
  890. $key = md5 ( $key );
  891. $keya = md5 ( substr ( $key, 0, 16 ) );
  892. $keyb = md5 ( substr ( $key, 16, 16 ) );
  893. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  894. $cryptkey = $keya . md5 ( $keya . $keyc );
  895. $key_length = strlen ( $cryptkey );
  896. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  897. $string_length = strlen ( $string );
  898. $result = '';
  899. $box = range ( 0, 255 );
  900. $rndkey = array ();
  901. for($i = 0; $i <= 255; $i ++) {
  902. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  903. }
  904. for($j = $i = 0; $i < 256; $i ++) {
  905. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  906. $tmp = $box [$i];
  907. $box [$i] = $box [$j];
  908. $box [$j] = $tmp;
  909. }
  910. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  911. $a = ($a + 1) % 256;
  912. $j = ($j + $box [$a]) % 256;
  913. $tmp = $box [$a];
  914. $box [$a] = $box [$j];
  915. $box [$j] = $tmp;
  916. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  917. }
  918. if ($operation == 'DECODE') {
  919. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  920. return substr ( $result, 26 );
  921. } else {
  922. return '';
  923. }
  924. } else {
  925. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  926. }
  927. }
  928. }
  929. ?>