invoice.php 32 KB

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