invoice.php 46 KB

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