invoice.php 40 KB

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