invoice.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  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
  581. from ' . $this->_table . '
  582. where trainId= ? and status=2 and printStatus=1 and untreadStatus=3 and irid!=""
  583. ';
  584. $query = Doo::db ()->query ( $sql,array($itidSql) );
  585. $resultInvoice = $query->fetchAll ();
  586. //收集收款金额
  587. $tempArray=array();
  588. foreach ($resultInvoice as $key=>$value){
  589. array_push($tempArray, $value['irid']);
  590. }
  591. $iridStr = implode(",", $tempArray);
  592. if(empty($iridStr)){
  593. return array('receivablesPrice'=>0,'trainId'=>$itidSql);
  594. }
  595. $sql = 'select receivablesPrice,irid
  596. from CLD_invoiceReceivables
  597. where irid in ( '.$iridStr.' ) '.$receivablesBank;
  598. $query = Doo::db ()->query ( $sql );
  599. $result = $query->fetchAll ();
  600. //合计金额-收款不会大于发票金额,大于则重置为发票总金额
  601. $sum=0;
  602. foreach ($result as $k=>$v){
  603. foreach ($resultInvoice as $value){
  604. if($value['irid']==$v['irid']&&$value['invoicePrice']<$v['receivablesPrice']){
  605. $result[$k]['receivablesPrice']=$value['invoicePrice'];
  606. }
  607. }
  608. $sum+=$result[$k]['receivablesPrice'];
  609. }
  610. return array('receivablesPrice'=>$sum,'trainId'=>$itidSql);
  611. //return array(0=>array('receivablesPrice'=>$sum,'trainId'=>$itidSql));
  612. }
  613. /**
  614. * 公司收款汇总
  615. * @param unknown $itidSql
  616. */
  617. function sumOfInvoiceRecelvablesCompany($itidSql) {
  618. //$this->find ( array ('where' => "clientname = ? and companyname =?" , 'param' => array($name,$company) , 'asArray' => TRUE ) );
  619. return array(0=>$this->_SumRecelvables($itidSql, ' and receivablesBank!="财务录入" '));
  620. // //1.获得发票完成列表
  621. // $sql = 'select irid,invoicePrice
  622. // from ' . $this->_table . '
  623. // where trainId= ? and status=2 and printStatus=1 and untreadStatus=3 and irid!=""
  624. // ';
  625. // $query = Doo::db ()->query ( $sql,array($itidSql) );
  626. // $resultInvoice = $query->fetchAll ();
  627. // //收集收款金额
  628. // $tempArray=array();
  629. // foreach ($resultInvoice as $key=>$value){
  630. // array_push($tempArray, $value['irid']);
  631. // }
  632. // $iridStr = implode(",", $tempArray);
  633. // $sql = 'select receivablesPrice,irid
  634. // from CLD_invoiceReceivables
  635. // where irid in ( '.$iridStr.' ) and receivablesBank!="财务录入"
  636. // ';
  637. // $query = Doo::db ()->query ( $sql );
  638. // $result = $query->fetchAll ();
  639. // //合计金额-收款不会大于发票金额,大于则重置为发票总金额
  640. // $sum=0;
  641. // foreach ($result as $k=>$v){
  642. // foreach ($resultInvoice as $value){
  643. // if($value['irid']==$v['irid']&&$value['invoicePrice']<$v['receivablesPrice']){
  644. // $result[$k]['receivablesPrice']=$value['invoicePrice'];
  645. // }
  646. // }
  647. // $sum+=$result[$k]['receivablesPrice'];
  648. // }
  649. // return array(0=>array('receivablesPrice'=>$sum,'trainId'=>$itidSql));
  650. // print_r($sum);
  651. // $sql = 'select sum(b.receivablesPrice) as receivablesPrice ,a.trainId,count(*) as count
  652. // from ' . $this->_table . ' as a left join CLD_invoiceReceivables as b on (a.iid=b.iid)
  653. // where a.trainId in (' . $itidSql . ') and b.receivablesBank!="财务录入" and a.status=2 and a.printStatus=1 and ( a.untreadStatus=3) and a.irid!=""
  654. // GROUP BY a.trainId';
  655. // //echo $sql;
  656. // $query = Doo::db ()->query ( $sql );
  657. // return $result = $query->fetchAll ();
  658. }
  659. //
  660. function sumOfInvoiceRecelvablesTrain($itidSql) {
  661. // $sql = 'select sum(b.receivablesPrice) as receivablesPrice ,a.trainId,count(*) as count
  662. // from ' . $this->_table . ' as a left join CLD_invoiceReceivables as b on find_in_set(a.irid,b.irid)
  663. // where a.trainId in (' . $itidSql . ') and a.status=2 and a.printStatus=1 and (a.untreadStatus =0 or a.untreadStatus=3) and a.irid!=""
  664. // GROUP BY a.trainId';
  665. return array(0=>$this->_SumRecelvables($itidSql));
  666. // $sql = 'select sum(b.receivablesPrice) as receivablesPrice ,a.trainId,count(*) as count
  667. // from ' . $this->_table . ' as a left join CLD_invoiceReceivables as b on (a.iid=b.iid )
  668. // where a.trainId in (' . $itidSql . ') and a.status=2 and a.printStatus=1 and (a.untreadStatus =0 or a.untreadStatus=3) and a.irid!=""
  669. // GROUP BY a.trainId';
  670. // $query = Doo::db ()->query ( $sql );
  671. // return $result = $query->fetchAll ();
  672. }
  673. //获得多个培训入账金额合计
  674. function InvoiceTrainIncomesPrice($invoiceTrainIdList){
  675. $result=array();
  676. foreach ($invoiceTrainIdList as $value){
  677. array_push($result, $this->_SumRecelvables($value));
  678. }
  679. return $result;
  680. }
  681. function getInvoiceByTrain($itidSql){
  682. $sql = 'select group_concat(a.iid) as iid,a.trainId
  683. from CLD_invoice as a
  684. where a.trainId in (' . $itidSql . ') and a.status=2 and a.printStatus=1 and (a.untreadStatus =0 or a.untreadStatus=3) and a.irid!=""
  685. GROUP BY a.trainId';
  686. $query = Doo::db ()->query ( $sql );
  687. return $result = $query->fetchAll ();
  688. }
  689. function getTrainRP($iid){
  690. $sql = 'select a.receivablesPrice
  691. from CLD_invoiceReceivables as a
  692. where a.iidExtend like "%,'.$iid.'"
  693. ';
  694. $query = Doo::db ()->query ( $sql );
  695. $result = $query->fetchAll();
  696. if (empty($result)){
  697. return 0;
  698. }
  699. return $result[0]['receivablesPrice'];
  700. }
  701. /**
  702. * 获得培训班发票
  703. *
  704. * @param integer $itid
  705. * @return void
  706. */
  707. function getInvoiceByTrainingCount($itid = 0) {
  708. if (empty ( $itid ))
  709. return 0;
  710. $sql = 'select count(*) as count from CLD_invoiceStore as a left join ' . $this->_table . ' as b on (a.iid=b.iid)
  711. 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 )';
  712. //echo $sql;
  713. //echo $sql;
  714. $query = Doo::db ()->query ( $sql );
  715. $result = $query->fetchAll ();
  716. return $result [0] ['count'];
  717. // $detail = $this->count ( array (
  718. // 'where' => "trainId= '" . $itid . "' and untreadStatus!=2 and isDelete=0", // and sid=".$sid,
  719. // 'asArray' => TRUE
  720. // ) );
  721. // return $detail;
  722. }
  723. /**
  724. * 根据仓库ID获得发票
  725. * @param string $Istoreid
  726. * @return unknown
  727. */
  728. public function getInvoiceByIstoreid($Istoreid = "", $select = "") {
  729. if (empty ( $Istoreid ))
  730. return array ();
  731. $condition = array (
  732. 'where' => "isid in ( " . $Istoreid . ")",
  733. 'asArray' => TRUE
  734. );
  735. if (! empty ( $select ))
  736. $condition += array (
  737. 'select' => $select
  738. );
  739. $detail = $this->find ( $condition );
  740. return $detail;
  741. }
  742. /**
  743. * 获取单个可以打印的发票数据
  744. * @param number $iid 发票id
  745. * @param number $select 需要获取的字段,为空获取全部数据
  746. * @return array|array() 返回发票数据
  747. */
  748. public function getInvoiceByPrint($iid = 0, $select = "") {
  749. $condition = array (
  750. 'where' => "iid= '" . $iid . "' and status=2 and (untreadStatus=0 or untreadStatus=4 )",
  751. 'asArray' => TRUE
  752. );
  753. if (! empty ( $select ))
  754. $condition += array (
  755. 'select' => $select
  756. );
  757. $detail = array ();
  758. if (! empty ( $iid ) && is_numeric ( $iid ))
  759. $detail = $this->getOne ( $condition );
  760. return $detail;
  761. }
  762. /**
  763. * 添加一个发票并进入审批状态
  764. * @param array $item 发票相关数据
  765. * @return number 返回发票ID
  766. */
  767. public function addInvoice($item = array()) {
  768. $lid = 0;
  769. if (is_array ( $item ) && ! empty ( $item )) {
  770. foreach ( $item as $key => $value ) {
  771. $this->$key = $value;
  772. }
  773. $lid = $this->insert ();
  774. }
  775. return $lid;
  776. }
  777. /**
  778. * 根据参数字段更新相应字段(主键ID必须传)
  779. * @param array $item 相关需要更新的字段信息
  780. * @return number 返回发票ID
  781. */
  782. public function setInvoiceByCondition($item = array()) {
  783. $lid = 0;
  784. if (is_array ( $item ) && ! empty ( $item )) {
  785. foreach ( $item as $key => $value ) {
  786. $this->$key = $value;
  787. }
  788. $lid = $this->update ();
  789. }
  790. return $lid;
  791. }
  792. /**
  793. * 根据参数字段批量更新相应字段(主键ID必须传)
  794. * @param array $item 相关需要更新的字段信息
  795. * @return number 返回发票ID
  796. */
  797. public function BatchInvoiceByCondition($item = array(), $iid = "") {
  798. $lid = 0;
  799. if (is_array ( $item ) && ! empty ( $item )) {
  800. foreach ( $item as $key => $value ) {
  801. $this->$key = $value;
  802. }
  803. if (! empty ( $iid )) {
  804. $condition = array (
  805. 'where' => "iid in (" . $iid . ")",
  806. 'asArray' => TRUE
  807. );
  808. $this->update ( $condition );
  809. } else
  810. $lid = $this->update ();
  811. }
  812. return $lid;
  813. }
  814. /**
  815. * 批量更新
  816. * @param string $isid
  817. * @return number
  818. */
  819. function UpdateBatchInvoice($sql = "") {
  820. if (empty ( $sql ))
  821. return 0;
  822. $query = Doo::db ()->query ( $sql );
  823. }
  824. /**
  825. * 根据管理组和发票当前状态获得打印发票数据;其中当iid为空时获取所有发票数据,反之获取1条数据
  826. * @param number $sid 管理组人员ID
  827. * @param number $status 状态 为0时获取可以打印数据,为1时获取已经完成打印数据
  828. * @param number $iid 发票ID
  829. * @return array|array() 返回所有发票数据,当iid有值时返回一条数据
  830. */
  831. public function getPrintInvoiceByManage($sid = 0, $status = 0, $iid = 0) {
  832. $list = array ();
  833. if (! empty ( $sid ) && empty ( $iid ))
  834. $list = $this->find ( array (
  835. 'where' => "status=2 and printStatus=" . $status . " and (untreadStatus=0 or untreadStatus=4) and moldManage like '%[\"" . $sid . "\",%'",
  836. 'desc' => 'approvalTime',
  837. 'limit' => 10,
  838. 'asArray' => TRUE
  839. ) );
  840. elseif (! empty ( $sid ) && ! empty ( $iid ))
  841. $list = $this->getOne ( array (
  842. 'where' => "status=2 and printStatus=" . $status . " and (untreadStatus=0 or untreadStatus=4) and moldManage like '%[\"" . $sid . "\",%' and iid=" . $iid,
  843. 'asArray' => TRUE
  844. ) );
  845. return $list;
  846. }
  847. /**
  848. * 获得与我相关的发票数据,其中包含 处理中,待处理,最旧入账等数据;当iid有值时获取一条关于sid的发票
  849. * @param number $sid 用户ID
  850. * @param number $iid 发票ID
  851. * @return array|array()
  852. */
  853. public function getMyInvoice($sid = 0, $iid = 0) {
  854. Doo::loadModel ( 'invoiceOperationLog' );
  855. $invoiceOperationLog = new invoiceOperationLog ();
  856. Doo::loadClass ( 'XDeode' );
  857. $XDeode = new XDeode ( 5 );
  858. $list = array ();
  859. if (! empty ( $iid )) {
  860. $list = $this->getOne ( array (
  861. 'where' => " sid=" . $sid . " and iid=" . $iid,
  862. 'asArray' => TRUE
  863. ) );
  864. } elseif (! empty ( $sid ) && empty ( $iid )) {
  865. $list ['pendingInvoice'] = array ();
  866. $list ['handleInvoice'] = array ();
  867. $list ['pendingInvoice'] = $this->find ( array ( // 加入邮件中也为 处理中 已退票
  868. 'where' => "((status=1 and untreadStatus=0) or (status=2 and printStatus=0 and untreadStatus=0) or
  869. ( status=2 and printStatus=1 and untreadStatus=1 and untreadPost=0)
  870. or (postStatus=0 and doPost=1 and status=2 and untreadStatus=0) ) and isDelete=0 and sid=" . $sid,
  871. 'desc' => 'iid',
  872. 'asArray' => TRUE
  873. ) );
  874. foreach ( $list ['pendingInvoice'] as $key => $value ) {
  875. $list ['pendingInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  876. $list ['pendingInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  877. }
  878. $list ['handleInvoice'] = $this->find ( array (
  879. // 需求变更 -去除出票状态的数据 or (status=2 and printStatus=1 and untreadStatus=0 ) or (status=2 and printStatus=1 and untreadStatus=2 )
  880. // 新加入退票中 需要邮寄的发票
  881. 'where' => "(status=3 or status=4 or (status=2 and untreadStatus=2 ) or ( status=2 and untreadStatus=1 ) ) and isDelete=0 and sid=" . $sid,
  882. 'desc' => 'iid',
  883. 'asArray' => TRUE
  884. ) );
  885. foreach ( $list ['handleInvoice'] as $key => $value ) {
  886. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  887. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  888. $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  889. }
  890. }
  891. return $list;
  892. }
  893. /**
  894. * 获得所有未入账发票
  895. * @param number $invoiceTC 发票title
  896. */
  897. public function getInvoiceByUnAccount($invoiceTC = '', $iridExist = false) {
  898. Doo::loadClass ( 'XDeode' );
  899. $XDeode = new XDeode ( 5 );
  900. $title = "";
  901. if (! empty ( $invoiceTC ))
  902. $title = ' and (invoiceTitle like "%' . $invoiceTC . '%" or invoiceCompany like "%' . $invoiceTC . '%" )';
  903. $iridString = ' and irid="" ';
  904. if ($iridExist)
  905. $iridString = ' and irid!="" ';
  906. $list = $this->find ( array ( //
  907. 'where' => "status=2 and printStatus=1 " . $iridString . " and untreadStatus=0 and ( (doPost=1 and postStatus=1) or doPost=0 ) and isDelete=0 " . $title,
  908. 'desc' => 'iid',
  909. 'limit' => 5,
  910. 'asArray' => TRUE
  911. ) );
  912. foreach ( $list as $key => $value ) {
  913. $list [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  914. }
  915. return $list;
  916. }
  917. /**
  918. * 获得与我有关可收款的发票,已经出票的发票
  919. * @param number $sid 开票人ID
  920. */
  921. public function getInvoiceByReceivables($sid = 0) {
  922. Doo::loadClass ( 'XDeode' );
  923. $XDeode = new XDeode ( 5 );
  924. $list ['handleInvoice'] = $this->find ( array (
  925. 'where' => "(status=2 and printStatus=1 and untreadStatus=0 ) and isDelete=0 and sid=" . $sid,
  926. 'desc' => 'iid',
  927. 'asArray' => TRUE
  928. ) );
  929. foreach ( $list ['handleInvoice'] as $key => $value ) {
  930. $list ['handleInvoice'] [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  931. $list ['handleInvoice'] [$key] ['iidKeyUrl'] = $XDeode->encode ( $value ['iid'] );
  932. // $list ['handleInvoice'] [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  933. }
  934. return $list ['handleInvoice'];
  935. }
  936. /**
  937. * 获取需要邮寄的发票
  938. * @param integer $postStatus 0为获取需要邮寄的发票,1为获取已经邮寄的发票
  939. * @return array|array() 返回相关数据
  940. */
  941. public function getPostByInvoice($postStatus = 0, $desc = "desc") {
  942. Doo::loadClass ( 'XDeode' );
  943. $XDeode = new XDeode ( 5 );
  944. $list = $this->find ( array ( // and untreadStatus=0
  945. 'where' => "postStatus=" . $postStatus . " and printStatus=1 and doPost=1 and status=2 and isDelete=0",
  946. 'limit' => 7,
  947. $desc => 'iid',
  948. 'asArray' => TRUE
  949. ) );
  950. foreach ( $list as $key => $value ) {
  951. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  952. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  953. $list [$key] ['expressCompany'] = '';
  954. $list [$key] ['expressCom'] = '';
  955. if (! empty ( $value ['expressCompany'] )) {
  956. $express = explode ( ":", $value ['expressCompany'] );
  957. $list [$key] ['expressCompany'] = $express [0];
  958. $list [$key] ['expressCom'] = $express [1];
  959. }
  960. }
  961. return $list;
  962. }
  963. /**
  964. * 根据退票状态获得相关数据,iid有值时只获得一条数据
  965. * @param number $untreadStatus 退票状态
  966. * @param number $iid 发票ID
  967. */
  968. public function getMyInvoiceByUntreadStatus($untreadStatus = 1, $iid = 0,$limit=10,$sid=0) {
  969. Doo::loadClass ( 'XDeode' );
  970. $XDeode = new XDeode ( 5 );
  971. if (empty ( $iid )) {
  972. $list = $this->find ( array ( // printStatus=1 and
  973. 'where' => " untreadStatus=" . $untreadStatus . " and status=2 and sid=".$sid,
  974. 'limit' => $limit,
  975. 'asArray' => TRUE
  976. ) );
  977. if ($untreadStatus == 2) {
  978. Doo::loadModel ( 'invoiceOperationLog' );
  979. $invoiceOperationLog = new invoiceOperationLog ();
  980. }
  981. foreach ( $list as $key => $value ) {
  982. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  983. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  984. $list [$key] ['untreadCompany'] = '';
  985. $list [$key] ['untreadCom'] = '';
  986. if (! empty ( $value ['untreadCompany'] )) {
  987. $express = explode ( ":", $value ['untreadCompany'] );
  988. $list [$key] ['untreadCompany'] = $express [0];
  989. $list [$key] ['untreadCom'] = $express [1];
  990. }
  991. if ($untreadStatus == 2) {
  992. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  993. }
  994. $list [$key] ['lossDetail'] = array ();
  995. if ($value ['lossIid'] != 0) {
  996. $detail = $this->getOne ( array (
  997. 'where' => "iid=" . $value ['lossIid'],
  998. 'asArray' => TRUE
  999. ) );
  1000. $detail ['iidKeyK'] = $XDeode->encode ( $detail ['iid'] );
  1001. $list [$key] ['lossDetail'] = $detail;
  1002. }
  1003. }
  1004. } else {
  1005. $list = $this->getOne ( array (
  1006. 'where' => "iid=" . $iid . " and printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2",
  1007. 'asArray' => TRUE
  1008. ) );
  1009. }
  1010. return $list;
  1011. }
  1012. /**
  1013. * 根据退票状态获得相关数据,iid有值时只获得一条数据
  1014. * @param number $untreadStatus 退票状态
  1015. * @param number $iid 发票ID
  1016. */
  1017. public function getInvoiceByUntreadStatus($untreadStatus = 1, $iid = 0,$limit=10) {
  1018. Doo::loadClass ( 'XDeode' );
  1019. $XDeode = new XDeode ( 5 );
  1020. if (empty ( $iid )) {
  1021. $list = $this->find ( array ( // printStatus=1 and
  1022. 'where' => " untreadStatus=" . $untreadStatus . " and status=2 ",
  1023. 'limit' => $limit,
  1024. 'asArray' => TRUE
  1025. ) );
  1026. if ($untreadStatus == 2) {
  1027. Doo::loadModel ( 'invoiceOperationLog' );
  1028. $invoiceOperationLog = new invoiceOperationLog ();
  1029. }
  1030. foreach ( $list as $key => $value ) {
  1031. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  1032. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  1033. $list [$key] ['untreadCompany'] = '';
  1034. $list [$key] ['untreadCom'] = '';
  1035. if (! empty ( $value ['untreadCompany'] )) {
  1036. $express = explode ( ":", $value ['untreadCompany'] );
  1037. $list [$key] ['untreadCompany'] = $express [0];
  1038. $list [$key] ['untreadCom'] = $express [1];
  1039. }
  1040. if ($untreadStatus == 2) {
  1041. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  1042. }
  1043. $list [$key] ['lossDetail'] = array ();
  1044. if ($value ['lossIid'] != 0) {
  1045. $detail = $this->getOne ( array (
  1046. 'where' => "iid=" . $value ['lossIid'],
  1047. 'asArray' => TRUE
  1048. ) );
  1049. $detail ['iidKeyK'] = $XDeode->encode ( $detail ['iid'] );
  1050. $list [$key] ['lossDetail'] = $detail;
  1051. }
  1052. $lossDate=0;
  1053. if(isset($list [$key] ['OperationLog'])){
  1054. $lossDate= date('m',strtotime ( $list [$key] ['OperationLog'] ['date'] ));
  1055. }
  1056. $printMonth=date('m',strtotime ( $value['printTime']));
  1057. $list [$key] ['lossShow'] = false;
  1058. if($printMonth<$lossDate){
  1059. $list [$key] ['lossShow'] = true;
  1060. }
  1061. }
  1062. } else {
  1063. $list = $this->getOne ( array (
  1064. 'where' => "iid=" . $iid . " and printStatus=1 and untreadStatus=" . $untreadStatus . " and status=2",
  1065. 'asArray' => TRUE
  1066. ) );
  1067. }
  1068. return $list;
  1069. }
  1070. /**
  1071. * 审批中获取相关数据 包括最近的一条操作记录
  1072. * @param number $status.
  1073. * @param number $limit.
  1074. */
  1075. function getInvoiceByProcessStatus($limit = 10, $desc = 'desc') {
  1076. Doo::loadClass ( 'XDeode' );
  1077. $XDeode = new XDeode ( 5 );
  1078. $list = $this->find ( array (
  1079. 'where' => " (status=1 or status=2 or status=3 or status=4) and processApprovals!=''",
  1080. 'limit' => $limit,
  1081. $desc => 'iid',
  1082. 'asArray' => TRUE
  1083. ) );
  1084. Doo::loadModel ( 'invoiceOperationLog' );
  1085. $invoiceOperationLog = new invoiceOperationLog ();
  1086. foreach ( $list as $key => $value ) {
  1087. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  1088. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], $value ['status'] );
  1089. }
  1090. return $list;
  1091. }
  1092. function getInvoiceByUntreadStatusPage($limit = "0,20", $con = "", $desc = 'desc', $olStatus = "desc",$descField="bindTime") {
  1093. if ( empty ( $con ))
  1094. return array ();
  1095. $listCount = $this->count ( array (
  1096. 'where' => $con,
  1097. 'asArray' => TRUE
  1098. ) );
  1099. $sqlArr=array (
  1100. 'where' => $con,
  1101. $desc => $descField,
  1102. 'asArray' => TRUE
  1103. );
  1104. if ($limit!==false){
  1105. $sqlArr+=array (
  1106. 'limit' => $limit
  1107. );
  1108. }
  1109. $list = $this->find ( $sqlArr ); // echo $con;
  1110. Doo::loadClass ( 'XDeode' );
  1111. $XDeode = new XDeode ( 5 );
  1112. Doo::loadModel ( 'invoiceReceivables' );
  1113. $invoiceReceivables = new invoiceReceivables ();
  1114. Doo::loadModel ( 'invoiceOperationLog' );
  1115. $invoiceOperationLog = new invoiceOperationLog ();
  1116. Doo::loadModel ( 'receipt' );
  1117. $receipt = new receipt ();
  1118. foreach ( $list as $key => $value ) {
  1119. //培训班结算是否完成
  1120. $receiptDetail=$receipt->getReceiptByinvoiceTrainId($value['trainId']);
  1121. $list[$key]['settlementStatus']=false;
  1122. if (!empty($receiptDetail)){
  1123. if ($receiptDetail['status']==8||$receiptDetail['status']==9){
  1124. $list[$key]['settlementStatus']=true;
  1125. }
  1126. }
  1127. $list [$key] ['count'] = $listCount;
  1128. $list [$key] ['iidKey'] = $this->authcode ( $value ['iid'], '' );
  1129. $list [$key] ['iidKeyK'] = $XDeode->encode ( $value ['iid'] );
  1130. $list [$key] ['trainIdKey'] = $XDeode->encode ( $value ['trainId'] );
  1131. $list [$key] ['untreadCompany'] = '';
  1132. $list [$key] ['untreadCom'] = '';
  1133. if (! empty ( $value ['untreadCompany'] )) {
  1134. $express = explode ( ":", $value ['untreadCompany'] );
  1135. $list [$key] ['untreadCompany'] = $express [0];
  1136. $list [$key] ['untreadCom'] = $express [1];
  1137. }
  1138. $paid=$value['invoicePrice']-$value['invoiceBalance'];
  1139. $list [$key] ['irList'] = $invoiceReceivables->getInvoiceReceivablesInIridString ( $value ['irid'] );// array(0=>array('sumPrice'=>$paid)); //
  1140. if(!empty($list [$key] ['irList'])){
  1141. $list [$key] ['irList'][0]['sumPrice']=$paid;
  1142. }
  1143. if ($olStatus == 'desc')
  1144. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  1145. else
  1146. $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], $olStatus );
  1147. $list [$key] ['OperationLog'] = $invoiceOperationLog->getInvoiceOperationByStatus ( $value ['iid'], 6 );
  1148. $list [$key] ['OL'] = $invoiceOperationLog->getInvoiceOperationInStatus ( $value ['iid'], '2,3,4' );
  1149. // print_r($list [$key] ['OperationLog']);
  1150. $list [$key] ['sumPrice'] = 0;
  1151. if (! empty ( $list [$key] ['irList'] )){
  1152. $list [$key] ['sumPrice'] = $list [$key] ['irList'] [0] ['sumPrice'];
  1153. if($value['invoicePrice']<=$list [$key] ['irList'] [0] ['sumPrice']){
  1154. $list [$key] ['sumPrice'] = $value['invoicePrice'];
  1155. }
  1156. }
  1157. $list [$key] ['diffPrice'] = $list [$key] ['invoicePrice'];
  1158. if (! empty ( $list [$key] ['irList'] [0] ['sumPrice'] )){
  1159. $diffPrice=$list [$key] ['invoicePrice'] - $list [$key] ['irList'] [0] ['sumPrice'];
  1160. if($diffPrice<=0){
  1161. $diffPrice=0;
  1162. }
  1163. $list [$key] ['diffPrice'] = $diffPrice;
  1164. // $list [$key] ['operationLog'] = $invoiceOperationLog->getInvoiceOperationLogByIid ( $value ['iid'], 'desc' );
  1165. }
  1166. $list [$key] ['lossDetail'] = array ();
  1167. if ($value ['lossIid'] != 0) {
  1168. $detail = $this->getOne ( array (
  1169. 'where' => "iid=" . $value ['lossIid'],
  1170. 'asArray' => TRUE
  1171. ) );
  1172. $detail ['iidKeyK'] = $XDeode->encode ( $detail ['iid'] );
  1173. $list [$key] ['lossDetail'] = $detail;
  1174. }
  1175. //$lossDate = strtotime ( "+1 month", strtotime ( $list [$key] ['OperationLog'] ['date'] ) );
  1176. //$nowDate = strtotime ( 'now' );
  1177. // $list [$key] ['lossShow'] = false;
  1178. // if ($lossDate < $nowDate){
  1179. // $list [$key] ['lossShow'] = true;
  1180. // }
  1181. $lossDate= date('m',strtotime ( $list [$key] ['OperationLog'] ['date'] ));
  1182. $printMonth=date('m',strtotime ( $value['printTime']));
  1183. $list [$key] ['lossShow'] = false;
  1184. if($printMonth<$lossDate){
  1185. $list [$key] ['lossShow'] = true;
  1186. }
  1187. }
  1188. return $list;
  1189. }
  1190. /**
  1191. * 公司应收款汇总
  1192. * @param unknown $con
  1193. */
  1194. function getInvoiceCompanyStatistics($con = '') {
  1195. if (empty ( $con ))
  1196. return array ();
  1197. $sql = 'select invoicePrice,iid
  1198. from ' . $this->_table . '
  1199. where ' . $con . '
  1200. ';
  1201. $query = Doo::db ()->query ( $sql );
  1202. $result = $query->fetchAll ();
  1203. $iidlist = array ();
  1204. $invoicePrice = 0;
  1205. foreach ( $result as $value ) {
  1206. array_push ( $iidlist, $value ['iid'] );
  1207. $invoicePrice += $value ['invoicePrice'];
  1208. }
  1209. $iidString = implode ( ',', $iidlist );
  1210. // 获得已收款发票
  1211. if (! empty ( $iidString )) {
  1212. $sql = 'select sum(receivablesPrice) as receivablesPrice
  1213. from CLD_invoiceReceivables
  1214. where iid in (' . $iidString . ') and bindStatus=1
  1215. GROUP BY bindStatus';
  1216. $query = Doo::db ()->query ( $sql );
  1217. $result = $query->fetch ();
  1218. } else {
  1219. $result ['receivablesPrice'] = 0;
  1220. }
  1221. $diffPrice=$invoicePrice - $result ['receivablesPrice'];
  1222. if($diffPrice<=0){
  1223. $diffPrice=0;
  1224. }
  1225. return array (
  1226. 'invoicePrice' => $invoicePrice,
  1227. 'receivablesPrice' => $result ['receivablesPrice'],
  1228. 'diffPrice' => $diffPrice
  1229. );
  1230. }
  1231. /**
  1232. * 获得当前月份办事处入账完成度
  1233. * @param number $cid
  1234. */
  1235. function getInvoiceScheduleByCid($cid = 0, $month = true) {
  1236. if (empty ( $cid ))
  1237. return 0;
  1238. if ($month === false)
  1239. $monthSql = "";
  1240. else {
  1241. $month = date ( "m" );
  1242. $year=date('Y');
  1243. $monthSql = " and Month(date)=" . $month." and Year(date)=".$year;
  1244. }
  1245. // 完成打印
  1246. $recordCount = $this->count ( array (
  1247. 'where' => " status=2 and printStatus=1 and untreadStatus=3 " . $monthSql . " and cid =" . $cid,
  1248. 'asArray' => TRUE
  1249. ) );
  1250. // 当月创建的发票
  1251. $allCount = $this->count ( array (
  1252. 'where' => " isDelete=0 " . $monthSql . " and cid =" . $cid,
  1253. 'asArray' => TRUE
  1254. ) );
  1255. if ($recordCount == 0)
  1256. return 0;
  1257. return $recordCount / $allCount;
  1258. }
  1259. /**
  1260. * 或者办事处成员的完成度
  1261. */
  1262. function getInvoiceScheduleByCidGroupSid($cid = 0) {
  1263. if (empty ( $cid ))
  1264. return array ();
  1265. // 完成发票
  1266. $recordCount = $this->find ( array (
  1267. 'select' => 'count(*) as count,sid',
  1268. 'where' => " status=2 and printStatus=1 and untreadStatus=3 and cid =" . $cid,
  1269. 'groupby' => 'sid',
  1270. 'asArray' => TRUE
  1271. ) );
  1272. // 当月创建的发票
  1273. $allCount = $this->find ( array (
  1274. 'select' => 'count(*) as count,sid',
  1275. 'where' => " isDelete=0 and cid =" . $cid,
  1276. 'groupby' => 'sid',
  1277. 'asArray' => TRUE
  1278. ) );
  1279. $list = array ();
  1280. foreach ( $recordCount as $key => $value ) {
  1281. foreach ( $allCount as $k => $v ) {
  1282. if ($value ['sid'] == $v ['sid']) {
  1283. if ($value ['count'] == 0)
  1284. $list [$value ['sid']] = 0;
  1285. else
  1286. $list [$value ['sid']] = ( int ) (($value ['count'] / $v ['count']) * 100);
  1287. }
  1288. }
  1289. }
  1290. return $list;
  1291. }
  1292. /**
  1293. * 获得个人最新发票信息
  1294. */
  1295. function getInvoiceByNew($sid = 0) {
  1296. if (empty ( $sid ))
  1297. return array ();
  1298. $Detail = $this->getOne ( array (
  1299. 'where' => "sid=" . $sid,
  1300. 'desc' => 'iid',
  1301. 'asArray' => TRUE
  1302. ) );
  1303. return $Detail;
  1304. }
  1305. /**
  1306. * 加密或解密指定字符串
  1307. *
  1308. * @param string $string 要加密或解密的字符串
  1309. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  1310. * @param string $key 加解密的key
  1311. * @param $expiry 超时值
  1312. *
  1313. */
  1314. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  1315. $ckey_length = 4;
  1316. if (! $key) {
  1317. $key = $this->INVOICEKEY;
  1318. }
  1319. $key = md5 ( $key );
  1320. $keya = md5 ( substr ( $key, 0, 16 ) );
  1321. $keyb = md5 ( substr ( $key, 16, 16 ) );
  1322. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  1323. $cryptkey = $keya . md5 ( $keya . $keyc );
  1324. $key_length = strlen ( $cryptkey );
  1325. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  1326. $string_length = strlen ( $string );
  1327. $result = '';
  1328. $box = range ( 0, 255 );
  1329. $rndkey = array ();
  1330. for($i = 0; $i <= 255; $i ++) {
  1331. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  1332. }
  1333. for($j = $i = 0; $i < 256; $i ++) {
  1334. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  1335. $tmp = $box [$i];
  1336. $box [$i] = $box [$j];
  1337. $box [$j] = $tmp;
  1338. }
  1339. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  1340. $a = ($a + 1) % 256;
  1341. $j = ($j + $box [$a]) % 256;
  1342. $tmp = $box [$a];
  1343. $box [$a] = $box [$j];
  1344. $box [$j] = $tmp;
  1345. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  1346. }
  1347. if ($operation == 'DECODE') {
  1348. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  1349. return substr ( $result, 26 );
  1350. } else {
  1351. return '';
  1352. }
  1353. } else {
  1354. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  1355. }
  1356. }
  1357. }
  1358. ?>