invoice.php 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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(invoicePrice) as invoicePrice ,trainId
  406. from ' . $this->_table . '
  407. where trainId in (' . $itidSql . ') and status=2 and printStatus=1 and (untreadStatus =0 or untreadStatus=3)
  408. GROUP BY 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. $query = Doo::db ()->query ( $sql );
  418. return $result = $query->fetchAll ();
  419. }
  420. function getInvoiceByTrainingCount($itid = 0) {
  421. if (empty ( $itid ))
  422. return 0;
  423. $detail = $this->count ( array (
  424. 'where' => "trainId= '" . $itid . "' and untreadStatus!=2", // and sid=".$sid,
  425. 'asArray' => TRUE
  426. ) );
  427. return $detail;
  428. }
  429. /**
  430. * 根据仓库ID获得发票
  431. * @param string $Istoreid
  432. * @return unknown
  433. */
  434. public function getInvoiceByIstoreid($Istoreid = "", $select = "") {
  435. if (empty ( $Istoreid ))
  436. return array ();
  437. $condition = array (
  438. 'where' => "isid in ( " . $Istoreid . ")",
  439. 'asArray' => TRUE
  440. );
  441. if (! empty ( $select ))
  442. $condition += array (
  443. 'select' => $select
  444. );
  445. $detail = $this->find ( $condition );
  446. return $detail;
  447. }
  448. /**
  449. * 获取单个可以打印的发票数据
  450. * @param number $iid 发票id
  451. * @param number $select 需要获取的字段,为空获取全部数据
  452. * @return array|array() 返回发票数据
  453. */
  454. public function getInvoiceByPrint($iid = 0, $select = "") {
  455. $condition = array (
  456. 'where' => "iid= '" . $iid . "' and status=2 and untreadStatus=0 ",
  457. 'asArray' => TRUE
  458. );
  459. if (! empty ( $select ))
  460. $condition += array (
  461. 'select' => $select
  462. );
  463. $detail = array ();
  464. if (! empty ( $iid ) && is_numeric ( $iid ))
  465. $detail = $this->getOne ( $condition );
  466. return $detail;
  467. }
  468. /**
  469. * 添加一个发票并进入审批状态
  470. * @param array $item 发票相关数据
  471. * @return number 返回发票ID
  472. */
  473. public function addInvoice($item = array()) {
  474. $lid = 0;
  475. if (is_array ( $item ) && ! empty ( $item )) {
  476. foreach ( $item as $key => $value ) {
  477. $this->$key = $value;
  478. }
  479. $lid = $this->insert ();
  480. }
  481. return $lid;
  482. }
  483. /**
  484. * 根据参数字段更新相应字段(主键ID必须传)
  485. * @param array $item 相关需要更新的字段信息
  486. * @return number 返回发票ID
  487. */
  488. public function setInvoiceByCondition($item = array()) {
  489. $lid = 0;
  490. if (is_array ( $item ) && ! empty ( $item )) {
  491. foreach ( $item as $key => $value ) {
  492. $this->$key = $value;
  493. }
  494. $lid = $this->update ();
  495. }
  496. return $lid;
  497. }
  498. /**
  499. * 根据参数字段批量更新相应字段(主键ID必须传)
  500. * @param array $item 相关需要更新的字段信息
  501. * @return number 返回发票ID
  502. */
  503. public function BatchInvoiceByCondition($item = array(), $iid = "") {
  504. $lid = 0;
  505. if (is_array ( $item ) && ! empty ( $item )) {
  506. foreach ( $item as $key => $value ) {
  507. $this->$key = $value;
  508. }
  509. if (! empty ( $iid )) {
  510. $condition = array (
  511. 'where' => "iid in (" . $iid . ")",
  512. 'asArray' => TRUE
  513. );
  514. $this->update ( $condition );
  515. } else
  516. $lid = $this->update ();
  517. }
  518. return $lid;
  519. }
  520. /**
  521. * 根据管理组和发票当前状态获得打印发票数据;其中当iid为空时获取所有发票数据,反之获取1条数据
  522. * @param number $sid 管理组人员ID
  523. * @param number $status 状态 为0时获取可以打印数据,为1时获取已经完成打印数据
  524. * @param number $iid 发票ID
  525. * @return array|array() 返回所有发票数据,当iid有值时返回一条数据
  526. */
  527. public function getPrintInvoiceByManage($sid = 0, $status = 0, $iid = 0) {
  528. $list = array ();
  529. if (! empty ( $sid ) && empty ( $iid ))
  530. $list = $this->find ( array (
  531. 'where' => "status=2 and printStatus=" . $status . " and untreadStatus=0 and moldManage like '%[\"" . $sid . "\",%'",
  532. 'desc' => 'approvalTime',
  533. 'limit' => 10,
  534. 'asArray' => TRUE
  535. ) );
  536. elseif (! empty ( $sid ) && ! empty ( $iid ))
  537. $list = $this->getOne ( array (
  538. 'where' => "status=2 and printStatus=" . $status . " and untreadStatus=0 and moldManage like '%[\"" . $sid . "\",%' and iid=" . $iid,
  539. 'asArray' => TRUE
  540. ) );
  541. return $list;
  542. }
  543. /**
  544. * 获得与我相关的发票数据,其中包含 处理中,待处理,最旧入账等数据;当iid有值时获取一条关于sid的发票
  545. * @param number $sid 用户ID
  546. * @param number $iid 发票ID
  547. * @return array|array()
  548. */
  549. public function getMyInvoice($sid = 0, $iid = 0) {
  550. Doo::loadModel ( 'invoiceOperationLog' );
  551. $invoiceOperationLog = new invoiceOperationLog ();
  552. Doo::loadClass ( 'XDeode' );
  553. $XDeode = new XDeode ( 5 );
  554. $list = array ();
  555. if (! empty ( $iid )) {
  556. $list = $this->getOne ( array (
  557. 'where' => " sid=" . $sid . " and iid=" . $iid,
  558. 'asArray' => TRUE
  559. ) );
  560. } elseif (! empty ( $sid ) && empty ( $iid )) {
  561. $list ['pendingInvoice'] = array ();
  562. $list ['handleInvoice'] = array ();
  563. $list ['pendingInvoice'] = $this->find ( array ( // 加入邮件中也为 处理中 已退票
  564. 'where' => "((status=1 and untreadStatus=0) or (status=2 and printStatus=0 and untreadStatus=0) or
  565. ( status=2 and printStatus=1 and untreadStatus=1 and untreadPost=0)
  566. or (postStatus=0 and doPost=1 and status=2 and untreadStatus=0) ) and isDelete=0 and sid=" . $sid,
  567. 'desc' => 'iid',
  568. 'asArray' => TRUE
  569. ) );
  570. foreach ( $list ['pendingInvoice'] as $key => $value ) {
  571. $list ['pendingInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  572. $list ['pendingInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  573. }
  574. $list ['handleInvoice'] = $this->find ( array (
  575. // 需求变更 -去除出票状态的数据 or (status=2 and printStatus=1 and untreadStatus=0 ) or (status=2 and printStatus=1 and untreadStatus=2 )
  576. // 新加入退票中 需要邮寄的发票
  577. 'where' => "(status=3 or status=4 or (status=2 and untreadStatus=2 ) or ( status=2 and untreadStatus=1 ) ) and isDelete=0 and sid=" . $sid,
  578. 'desc' => 'iid',
  579. 'asArray' => TRUE
  580. ) );
  581. foreach ( $list ['handleInvoice'] as $key => $value ) {
  582. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  583. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  584. $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  585. }
  586. }
  587. return $list;
  588. }
  589. /**
  590. * 获得所有未入账发票
  591. * @param number $invoiceTC 发票title
  592. */
  593. public function getInvoiceByUnAccount($invoiceTC = '', $iridExist = false) {
  594. Doo::loadClass ( 'XDeode' );
  595. $XDeode = new XDeode ( 5 );
  596. $title = "";
  597. if (! empty ( $invoiceTC ))
  598. $title = ' and (invoiceTitle like "%' . $invoiceTC . '%" or invoiceCompany like "%' . $invoiceTC . '%" )';
  599. $iridString = ' and irid="" ';
  600. if ($iridExist)
  601. $iridString = ' and irid!="" ';
  602. $list = $this->find ( array ( //
  603. 'where' => "status=2 and printStatus=1 " . $iridString . " and untreadStatus=0 and ( (doPost=1 and postStatus=1) or doPost=0 ) and isDelete=0 " . $title,
  604. 'desc' => 'iid',
  605. 'limit' => 5,
  606. 'asArray' => TRUE
  607. ) );
  608. foreach ( $list as $key => $value ) {
  609. $list [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  610. }
  611. return $list;
  612. }
  613. /**
  614. * 获得与我有关可收款的发票,已经出票的发票
  615. * @param number $sid 开票人ID
  616. */
  617. public function getInvoiceByReceivables($sid = 0) {
  618. Doo::loadClass ( 'XDeode' );
  619. $XDeode = new XDeode ( 5 );
  620. $list ['handleInvoice'] = $this->find ( array (
  621. 'where' => "(status=2 and printStatus=1 and untreadStatus=0 ) and isDelete=0 and sid=" . $sid,
  622. 'desc' => 'iid',
  623. 'asArray' => TRUE
  624. ) );
  625. foreach ( $list ['handleInvoice'] as $key => $value ) {
  626. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  627. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  628. // $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  629. }
  630. return $list ['handleInvoice'];
  631. }
  632. /**
  633. * 获取需要邮寄的发票
  634. * @param integer $postStatus 0为获取需要邮寄的发票,1为获取已经邮寄的发票
  635. * @return array|array() 返回相关数据
  636. */
  637. public function getPostByInvoice($postStatus = 0, $desc = "desc") {
  638. Doo::loadClass ( 'XDeode' );
  639. $XDeode = new XDeode ( 5 );
  640. $list = $this->find ( array ( // and untreadStatus=0
  641. 'where' => "postStatus=" . $postStatus . " and printStatus=1 and doPost=1 and status=2 and isDelete=0",
  642. 'limit' => 7,
  643. $desc => 'iid',
  644. 'asArray' => TRUE
  645. ) );
  646. foreach ( $list as $key => $value ) {
  647. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  648. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  649. $list [$key] ['expressCompany'] = '';
  650. $list [$key] ['expressCom'] = '';
  651. if (! empty ( $value ['expressCompany'] )) {
  652. $express = explode ( ":", $value ['expressCompany'] );
  653. $list [$key] ['expressCompany'] = $express [0];
  654. $list [$key] ['expressCom'] = $express [1];
  655. }
  656. }
  657. return $list;
  658. }
  659. /**
  660. * 根据退票状态获得相关数据,iid有值时只获得一条数据
  661. * @param number $untreadStatus 退票状态
  662. * @param number $iid 发票ID
  663. */
  664. public function getInvoiceByUntreadStatus($untreadStatus = 1, $iid = 0) {
  665. Doo::loadClass ( 'XDeode' );
  666. $XDeode = new XDeode ( 5 );
  667. if (empty ( $iid )) {
  668. $list = $this->find ( array ( // printStatus=1 and
  669. 'where' => " untreadStatus=" . $untreadStatus . " and status=2 ",
  670. 'limit' => 10,
  671. 'asArray' => TRUE
  672. ) );
  673. if ($untreadStatus == 2) {
  674. Doo::loadModel ( 'invoiceOperationLog' );
  675. $invoiceOperationLog = new invoiceOperationLog ();
  676. }
  677. foreach ( $list as $key => $value ) {
  678. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  679. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  680. $list [$key] ['untreadCompany'] = '';
  681. $list [$key] ['untreadCom'] = '';
  682. if (! empty ( $value ['untreadCompany'] )) {
  683. $express = explode ( ":", $value ['untreadCompany'] );
  684. $list [$key] ['untreadCompany'] = $express [0];
  685. $list [$key] ['untreadCom'] = $express [1];
  686. }
  687. if ($untreadStatus == 2) {
  688. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  689. }
  690. $list [$key] ['lossDetail'] = array ();
  691. if ($value ['lossIid'] != 0) {
  692. $detail = $this->getOne ( array (
  693. 'where' => "iid=" . $value ['lossIid'],
  694. 'asArray' => TRUE
  695. ) );
  696. $detail ['iidKeyK'] = $XDeode->encode ( $detail ['iid'] );
  697. $list [$key] ['lossDetail'] = $detail;
  698. }
  699. }
  700. } else {
  701. $list = $this->getOne ( array (
  702. 'where' => "iid=" . $iid . " and printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2",
  703. 'asArray' => TRUE
  704. ) );
  705. }
  706. return $list;
  707. }
  708. /**
  709. * 审批中获取相关数据 包括最近的一条操作记录
  710. * @param number $status.
  711. * @param number $limit.
  712. */
  713. function getInvoiceByProcessStatus($limit = 10, $desc = 'desc') {
  714. Doo::loadClass ( 'XDeode' );
  715. $XDeode = new XDeode ( 5 );
  716. $list = $this->find ( array (
  717. 'where' => " (status=1 or status=2 or status=3 or status=4) and processApprovals!=''",
  718. 'limit' => $limit,
  719. $desc => 'iid',
  720. 'asArray' => TRUE
  721. ) );
  722. Doo::loadModel ( 'invoiceOperationLog' );
  723. $invoiceOperationLog = new invoiceOperationLog ();
  724. foreach ( $list as $key => $value ) {
  725. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  726. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], $value ['status'] );
  727. }
  728. return $list;
  729. }
  730. function getInvoiceByUntreadStatusPage($limit = 0, $con = "", $desc = 'desc', $olStatus = "desc") {
  731. if (empty ( $limit ) || empty ( $con ))
  732. return array ();
  733. $listCount = $this->count ( array (
  734. 'where' => $con,
  735. 'asArray' => TRUE
  736. ) );
  737. $list = $this->find ( array (
  738. 'where' => $con,
  739. 'limit' => $limit,
  740. $desc => 'approvalTime',
  741. 'asArray' => TRUE
  742. ) ); // echo $con;
  743. Doo::loadClass ( 'XDeode' );
  744. $XDeode = new XDeode ( 5 );
  745. Doo::loadModel ( 'invoiceReceivables' );
  746. $invoiceReceivables = new invoiceReceivables ();
  747. Doo::loadModel ( 'invoiceOperationLog' );
  748. $invoiceOperationLog = new invoiceOperationLog ();
  749. foreach ( $list as $key => $value ) {
  750. $list [$key] ['count'] = $listCount;
  751. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  752. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  753. $list [$key] ['untreadCompany'] = '';
  754. $list [$key] ['untreadCom'] = '';
  755. if (! empty ( $value ['untreadCompany'] )) {
  756. $express = explode ( ":", $value ['untreadCompany'] );
  757. $list [$key] ['untreadCompany'] = $express [0];
  758. $list [$key] ['untreadCom'] = $express [1];
  759. }
  760. $list [$key] ['irList'] = $invoiceReceivables->getInvoiceReceivablesInIridString ( $value ['irid'] );
  761. if ($olStatus == 'desc')
  762. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  763. else
  764. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], $olStatus );
  765. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  766. $list [$key] ['OL'] = $invoiceOperationLog->getInvoiceOperationInStatus ( $value ['iid'], '2,3,4' );
  767. // print_r($list [$key] ['OperationLog']);
  768. $list [$key] ['sumPrice'] = 0;
  769. if (! empty ( $list [$key] ['irList'] ))
  770. $list [$key] ['sumPrice'] = $list [$key] ['irList'] [0] ['sumPrice'];
  771. $list [$key] ['diffPrice'] = $list [$key] ['invoicePrice'];
  772. if (! empty ( $list [$key] ['irList'] [0] ['sumPrice'] ))
  773. $list [$key] ['diffPrice'] = $list [$key] ['invoicePrice'] - $list [$key] ['irList'] [0] ['sumPrice'];
  774. // $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  775. $list [$key] ['lossDetail'] = array ();
  776. if ($value ['lossIid'] != 0) {
  777. $detail = $this->getOne ( array (
  778. 'where' => "iid=" . $value ['lossIid'],
  779. 'asArray' => TRUE
  780. ) );
  781. $detail ['iidKeyK'] = $XDeode->encode ( $detail ['iid'] );
  782. $list [$key] ['lossDetail'] = $detail;
  783. }
  784. $lossDate = strtotime ( "+1 month", strtotime ( $list [$key] ['OperationLog'] ['date'] ) );
  785. $nowDate = strtotime ( 'now' );
  786. $list [$key] ['lossShow'] = false;
  787. if ($lossDate < $nowDate)
  788. $list [$key] ['lossShow'] = true;
  789. }
  790. return $list;
  791. }
  792. /**
  793. * 公司应收款汇总
  794. * @param unknown $con
  795. */
  796. function getInvoiceCompanyStatistics($con = '') {
  797. if (empty ( $con ))
  798. return array ();
  799. $sql = 'select invoicePrice,iid
  800. from ' . $this->_table . '
  801. where ' . $con . '
  802. ';
  803. $query = Doo::db ()->query ( $sql );
  804. $result = $query->fetchAll ();
  805. $iidlist = array ();
  806. $invoicePrice = 0;
  807. foreach ( $result as $value ) {
  808. array_push ( $iidlist, $value ['iid'] );
  809. $invoicePrice += $value ['invoicePrice'];
  810. }
  811. $iidString = implode ( ',', $iidlist );
  812. // 获得已收款发票
  813. if (! empty ( $iidString )) {
  814. $sql = 'select sum(receivablesPrice) as receivablesPrice
  815. from CLD_invoiceReceivables
  816. where iid in (' . $iidString . ') and bindStatus=1
  817. GROUP BY bindStatus';
  818. $query = Doo::db ()->query ( $sql );
  819. $result = $query->fetch ();
  820. } else {
  821. $result ['receivablesPrice'] = 0;
  822. }
  823. return array (
  824. 'invoicePrice' => $invoicePrice,
  825. 'receivablesPrice' => $result ['receivablesPrice'],
  826. 'diffPrice' => $invoicePrice - $result ['receivablesPrice']
  827. );
  828. }
  829. /**
  830. * 获得当前月份办事处入账完成度
  831. * @param number $cid
  832. */
  833. function getInvoiceScheduleByCid($cid = 0, $month = true) {
  834. if (empty ( $cid ))
  835. return 0;
  836. if ($month === false)
  837. $monthSql = "";
  838. else {
  839. $month = date ( "m" );
  840. $monthSql = " and Month(date)=" . $month;
  841. }
  842. // 完成打印
  843. $recordCount = $this->count ( array (
  844. 'where' => " status=2 and printStatus=1 and untreadStatus=3 " . $monthSql . " and cid =" . $cid,
  845. 'asArray' => TRUE
  846. ) );
  847. // 当月创建的发票
  848. $allCount = $this->count ( array (
  849. 'where' => " isDelete=0 " . $monthSql . " and cid =" . $cid,
  850. 'asArray' => TRUE
  851. ) );
  852. if ($recordCount == 0)
  853. return 0;
  854. return $recordCount / $allCount;
  855. }
  856. /**
  857. * 或者办事处成员的完成度
  858. */
  859. function getInvoiceScheduleByCidGroupSid($cid = 0) {
  860. if (empty ( $cid ))
  861. return array ();
  862. // 完成发票
  863. $recordCount = $this->find ( array (
  864. 'select' => 'count(*) as count,sid',
  865. 'where' => " status=2 and printStatus=1 and untreadStatus=3 and cid =" . $cid,
  866. 'groupby' => 'sid',
  867. 'asArray' => TRUE
  868. ) );
  869. // 当月创建的发票
  870. $allCount = $this->find ( array (
  871. 'select' => 'count(*) as count,sid',
  872. 'where' => " isDelete=0 and cid =" . $cid,
  873. 'groupby' => 'sid',
  874. 'asArray' => TRUE
  875. ) );
  876. $list = array ();
  877. foreach ( $recordCount as $key => $value ) {
  878. foreach ( $allCount as $k => $v ) {
  879. if ($value ['sid'] == $v ['sid']) {
  880. if ($value ['count'] == 0)
  881. $list [$value ['sid']] = 0;
  882. else
  883. $list [$value ['sid']] = ( int ) (($value ['count'] / $v ['count']) * 100);
  884. }
  885. }
  886. }
  887. return $list;
  888. }
  889. /**
  890. * 获得个人最新发票信息
  891. */
  892. function getInvoiceByNew($sid = 0) {
  893. if (empty ( $sid ))
  894. return array ();
  895. $Detail = $this->getOne ( array (
  896. 'where' => "sid=" . $sid,
  897. 'desc' => 'iid',
  898. 'asArray' => TRUE
  899. ) );
  900. return $Detail;
  901. }
  902. /**
  903. * 加密或解密指定字符串
  904. *
  905. * @param string $string 要加密或解密的字符串
  906. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  907. * @param string $key 加解密的key
  908. * @param $expiry 超时值
  909. *
  910. */
  911. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  912. $ckey_length = 4;
  913. if (! $key) {
  914. $key = $this->INVOICEKEY;
  915. }
  916. $key = md5 ( $key );
  917. $keya = md5 ( substr ( $key, 0, 16 ) );
  918. $keyb = md5 ( substr ( $key, 16, 16 ) );
  919. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  920. $cryptkey = $keya . md5 ( $keya . $keyc );
  921. $key_length = strlen ( $cryptkey );
  922. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  923. $string_length = strlen ( $string );
  924. $result = '';
  925. $box = range ( 0, 255 );
  926. $rndkey = array ();
  927. for($i = 0; $i <= 255; $i ++) {
  928. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  929. }
  930. for($j = $i = 0; $i < 256; $i ++) {
  931. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  932. $tmp = $box [$i];
  933. $box [$i] = $box [$j];
  934. $box [$j] = $tmp;
  935. }
  936. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  937. $a = ($a + 1) % 256;
  938. $j = ($j + $box [$a]) % 256;
  939. $tmp = $box [$a];
  940. $box [$a] = $box [$j];
  941. $box [$j] = $tmp;
  942. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  943. }
  944. if ($operation == 'DECODE') {
  945. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  946. return substr ( $result, 26 );
  947. } else {
  948. return '';
  949. }
  950. } else {
  951. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  952. }
  953. }
  954. }
  955. ?>