invoice.php 37 KB

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