invoice.php 46 KB

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