invoice.php 33 KB

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