invoice.php 31 KB

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