invoiceTraining.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 invoiceTraining extends DooModel {
  11. public $itid;
  12. public $bindReceipt;
  13. public $status;
  14. public $submitStatus;
  15. public $trainName;
  16. public $cid;
  17. public $categoryName;
  18. public $creator;
  19. public $trainDate;
  20. public $invoiceTotal;
  21. public $invoiceTotalAmount;
  22. public $invoiceArriveAmount;
  23. public $arriveSchedule;
  24. public $creatorDate;
  25. public $settlementStatus;
  26. public $_table = 'CLD_invoiceTraining';
  27. public $_primarykey = 'itid';
  28. public $_fields = array (
  29. 'itid',
  30. 'bindReceipt',
  31. 'status',
  32. 'submitStatus',
  33. 'trainName',
  34. 'cid',
  35. 'categoryName',
  36. 'creator',
  37. 'trainDate',
  38. 'invoiceTotal',
  39. 'invoiceTotalAmount',
  40. 'invoiceArriveAmount',
  41. 'arriveSchedule',
  42. 'settlementStatus',
  43. 'creatorDate'
  44. );
  45. /**
  46. * 添加一个培训班
  47. * @param array $item 培训班相关数据
  48. * @return number 返回培训班ID
  49. */
  50. public function addInvoiceTraining($item = array()) {
  51. $itid = 0;
  52. if (is_array ( $item ) && ! empty ( $item )) {
  53. foreach ( $item as $key => $value ) {
  54. $this->$key = $value;
  55. }
  56. $itid = $this->insert ();
  57. }
  58. return $itid;
  59. }
  60. function getInvoiceTrainStatistics($itid = 0) {
  61. $itidCondition = ' and trainId=NULL ';
  62. if (! empty ( $itid ))
  63. $itidCondition = ' and trainId in (' . $itid . ') ';
  64. $sql = 'select sum(invoicePrice) as invoicePrice ,trainId
  65. from CLD_invoice
  66. where status=2 and printStatus=1 and (untreadStatus =0 or untreadStatus=3) ' . $itidCondition . '
  67. GROUP BY status';
  68. $query = Doo::db ()->query ( $sql );
  69. $result = $query->fetchAll ();
  70. if (! empty ( $result ))
  71. $detail = array (
  72. 'invoicePrice' => $result [0] ['invoicePrice']
  73. );
  74. else
  75. $detail = array (
  76. 'invoicePrice' => 0
  77. );
  78. // print_r($result);
  79. $itidCondition = ' and a.trainId=NULL ';
  80. if (! empty ( $itid ))
  81. $itidCondition = ' and a.trainId in (' . $itid . ') ';
  82. $sql = 'select sum(b.receivablesPrice) as receivablesPrice ,a.trainId,count(*) as count
  83. from CLD_invoice as a left join CLD_invoiceReceivables as b on find_in_set(b.irid,a.irid)
  84. where a.status=2 and a.printStatus=1 and (a.untreadStatus =0 or a.untreadStatus=3) and a.irid!="" ' . $itidCondition . '
  85. GROUP BY a.status';
  86. $query = Doo::db ()->query ( $sql );
  87. $result = $query->fetchAll ();
  88. if (! empty ( $result )) {
  89. $detail += array (
  90. 'receivablesPrice' => $result [0] ['receivablesPrice']
  91. );
  92. $detail += array (
  93. 'rPrice' => $detail ['invoicePrice'] - $result [0] ['receivablesPrice']
  94. );
  95. } else {
  96. $detail += array (
  97. 'receivablesPrice' => 0
  98. );
  99. $detail += array (
  100. 'rPrice' => $detail ['invoicePrice']
  101. );
  102. }
  103. return $detail;
  104. }
  105. /**
  106. * 根据办事处获得结算完成培训班
  107. * @param number $SStatus
  108. * @return string
  109. */
  110. function getInvoiceTrainBySettlementStatus($SStatus = 0, $cid = 0,$bindReceipt) {
  111. $condition = array (
  112. 'where' => " settlementStatus=" . $SStatus . ' and cid=' . $cid. ' and bindReceipt=' . $bindReceipt,
  113. 'asArray' => TRUE
  114. );
  115. $list = $this->find ( $condition );
  116. Doo::loadClass ( 'XDeode' );
  117. $XDeode = new XDeode ( 5 );
  118. foreach ( $list as $key => $value ) {
  119. $list [$key] ['itidKey'] = $XDeode->encode ( $value ['itid'] );
  120. }
  121. return $list;
  122. }
  123. function findAll($condition=array()){
  124. $list = $this->find ( $condition );
  125. Doo::loadClass ( 'XDeode' );
  126. $XDeode = new XDeode ( 5 );
  127. foreach ( $list as $key => $value ) {
  128. $list [$key] ['itidKey'] = $XDeode->encode ( $value ['itid'] );
  129. }
  130. return $list;
  131. }
  132. /**
  133. * 获得办事处下的培训班
  134. */
  135. function getInvoiceTrainingByCid($cid = 0) {
  136. $condition = array (
  137. 'where' => " cid=" . $cid,
  138. 'desc'=>'itid',
  139. 'asArray' => TRUE
  140. );
  141. $list = $this->find ( $condition );
  142. Doo::loadClass ( 'XDeode' );
  143. $XDeode = new XDeode ( 5 );
  144. foreach ( $list as $key => $value ) {
  145. $list [$key] ['itidKey'] = $XDeode->encode ( $value ['itid'] );
  146. }
  147. return $list;
  148. }
  149. function getInvoiceTrainingByTodo($select = "", $cid = 0, $sid = 0) {
  150. Doo::loadModel ( 'staff' );
  151. $staff = new staff ();
  152. Doo::loadModel ( 'L_category' );
  153. $lCategory = new L_category ();
  154. $condition = array (
  155. 'where' => "(status= 0 or status=1) and ( cid in (" . $cid . ") or creator=" . $sid . " )",
  156. // 'limit' => 8,
  157. 'asArray' => TRUE
  158. );
  159. if (! empty ( $select ))
  160. $condition += array (
  161. 'select' => $select
  162. );
  163. $list = $this->find ( $condition );
  164. Doo::loadClass ( 'XDeode' );
  165. $XDeode = new XDeode ( 5 );
  166. $itid = array ();
  167. foreach ( $list as $key => $value ) {
  168. $list [$key] ['trainingKey'] = $XDeode->encode ( $value ['itid'] );
  169. $detail = $staff->getStaffBySid ( $value ['creator'] );
  170. $list [$key] ['staff'] = $detail;
  171. $detail = $lCategory->getCategoryById ( $value ['cid'] );
  172. $list [$key] ['category'] = $detail;
  173. array_push ( $itid, $value ['itid'] );
  174. }
  175. // 获得开票金额合计 入账金额合计 入账完成度
  176. Doo::loadModel ( 'invoice' );
  177. $invoice = new invoice ();
  178. $invoiceList = $invoiceRecelvablesList = array ();
  179. $itidSum=array();
  180. if (! empty ( $itid )) {
  181. $itidSql = implode ( ',', $itid );
  182. $invoiceList = $invoice->sumOfinvoiceTrain ( $itidSql );
  183. // 1.所有培训班发票 收款合计
  184. $invoiceRecelvablesList = $invoice->InvoiceTrainIncomesPrice($itid);
  185. //print_r($invoiceRecelvablesList);
  186. //$invoiceRecelvablesList = $invoice->sumOfInvoiceRecelvablesTrain ( $itidSql );
  187. // 收款有多个发票绑定,导致收款金额没有统计--TODO
  188. // 获得收款扩展部分的金额
  189. // id
  190. $iidList=$invoice->getInvoiceByTrain($itidSql);
  191. foreach ($iidList as $key=>$value){
  192. $idData=explode(",", $value['iid']);
  193. $sum=0;
  194. foreach ($idData as $item){
  195. $sum+=$invoice->getTrainRP($item);
  196. }
  197. //
  198. $itidSum[$value['trainId']]=$sum;
  199. }
  200. }
  201. //print_r($iidList);
  202. // print_r($invoiceRecelvablesList);
  203. // print_r($itidSum);
  204. foreach ( $list as $key => $value ) {
  205. $countInvoice = $invoice->getInvoiceByTrainingCount ( $value ['itid'] );
  206. $value ['invoiceTotal'] = $countInvoice;
  207. $list [$key] ['invoiceTotal'] = $countInvoice;
  208. foreach ( $invoiceList as $k => $v ) {
  209. if ($value ['itid'] == $v ['trainId']) {
  210. $list [$key] ['invoiceTotalAmount'] = $v ['invoicePrice'];
  211. break;
  212. }
  213. }
  214. foreach ( $invoiceRecelvablesList as $i => $o ) {
  215. if ($value ['itid'] == $o ['trainId']) {
  216. $list [$key] ['invoiceArriveAmount'] = $o ['receivablesPrice'];
  217. $list [$key] ['arriveSchedule'] = "0";
  218. if ($value ['invoiceTotal'] != 0) {
  219. //$list [$key] ['invoiceArriveAmount']+=$itidSum[$value ['itid']];
  220. $list [$key] ['arriveSchedule'] = round ( $list [$key] ['invoiceArriveAmount'] / $list [$key] ['invoiceTotalAmount'] * 100, 2 );
  221. // $list [$key] ['arriveSchedule'] = round ( $o ['count'] / $value ['invoiceTotal'] * 100, 2 );
  222. }
  223. break;
  224. }
  225. }
  226. }
  227. return $list;
  228. }
  229. /**
  230. * 根据参数字段更新相应字段(主键ID必须传)
  231. * @param array $item 相关需要更新的字段信息
  232. * @return number 返回发票ID
  233. */
  234. public function setInvoiceTrainByCondition($item = array(), $itid = "") {
  235. $lid = 0;
  236. if (is_array ( $item ) && ! empty ( $item )) {
  237. foreach ( $item as $key => $value ) {
  238. $this->$key = $value;
  239. }
  240. if (! empty ( $itid )) {
  241. $condition = array (
  242. 'where' => "itid in (" . $itid . ")",
  243. 'asArray' => TRUE
  244. );
  245. $this->update ( $condition );
  246. } else
  247. $lid = $this->update ();
  248. }
  249. return $lid;
  250. }
  251. /**
  252. * 根据状态获取培训班
  253. * @param number $status
  254. */
  255. function getInvoiceTrainingByStatus($status = 0, $select = "", $cid = 0, $sid = 0) {
  256. Doo::loadModel ( 'staff' );
  257. $staff = new staff ();
  258. Doo::loadModel ( 'L_category' );
  259. $lCategory = new L_category ();
  260. $condition = array (
  261. 'where' => "status= '" . $status . "' and (cid in(" . $cid . ") or creator=" . $sid . " )",
  262. // 'limit' => 8,
  263. 'asArray' => TRUE
  264. );
  265. if (! empty ( $select ))
  266. $condition += array (
  267. 'select' => $select
  268. );
  269. $list = $this->find ( $condition );
  270. Doo::loadClass ( 'XDeode' );
  271. $XDeode = new XDeode ( 5 );
  272. $itid = array ();
  273. foreach ( $list as $key => $value ) {
  274. $list [$key] ['trainingKey'] = $XDeode->encode ( $value ['itid'] );
  275. $detail = $staff->getStaffBySid ( $value ['creator'] );
  276. $list [$key] ['staff'] = $detail;
  277. $detail = $lCategory->getCategoryById ( $value ['cid'] );
  278. $list [$key] ['category'] = $detail;
  279. array_push ( $itid, $value ['itid'] );
  280. }
  281. // 获得开票金额合计 入账金额合计 入账完成度
  282. Doo::loadModel ( 'invoice' );
  283. $invoice = new invoice ();
  284. $invoiceList = $invoiceRecelvablesList = array ();
  285. if (! empty ( $itid )) {
  286. $itidSql = implode ( ',', $itid );
  287. $invoiceList = $invoice->sumOfinvoiceTrain ( $itidSql );
  288. $invoiceRecelvablesList =$invoice->InvoiceTrainIncomesPrice($itid);
  289. //$invoiceRecelvablesList = $invoice->sumOfInvoiceRecelvablesTrain ( $itidSql );
  290. }
  291. // print_r($invoiceRecelvablesList);
  292. //print_r($itidSum);
  293. foreach ( $list as $key => $value ) {
  294. $countInvoice = $invoice->getInvoiceByTrainingCount ( $value ['itid'] );
  295. $value ['invoiceTotal'] = $countInvoice;
  296. $list [$key] ['invoiceTotal'] = $countInvoice;
  297. foreach ( $invoiceList as $k => $v ) {
  298. if ($value ['itid'] == $v ['trainId']) {
  299. $list [$key] ['invoiceTotalAmount'] = $v ['invoicePrice'];
  300. break;
  301. }
  302. }
  303. foreach ( $invoiceRecelvablesList as $i => $o ) {
  304. if ($value ['itid'] == $o ['trainId']) {
  305. $list [$key] ['invoiceArriveAmount'] = $o ['receivablesPrice'];
  306. $list [$key] ['arriveSchedule'] = "0";
  307. if ($value ['invoiceTotal'] != 0) {
  308. $list [$key] ['arriveSchedule'] = round ( ($list [$key] ['invoiceArriveAmount'] )/ $list [$key] ['invoiceTotalAmount'] * 100, 2 );
  309. // $list [$key] ['arriveSchedule'] = round ( $o ['count'] / $value ['invoiceTotal'] * 100, 2 );
  310. }
  311. break;
  312. }
  313. }
  314. }
  315. return $list;
  316. }
  317. // function Get($itid = 0){
  318. // Doo::loadClass ( 'XDeode' );
  319. // $XDeode = new XDeode ( 5 );
  320. // if (! is_numeric ( $itid )){
  321. // $itid = $XDeode->decode ( $itid );
  322. // }
  323. // $detail = array ();
  324. // if (! empty ( $itid ) && is_numeric ( $itid )) {
  325. // $detail = $this->getOne ( array (
  326. // 'where' => " itid=" . $itid,
  327. // 'asArray' => TRUE
  328. // ) );
  329. // }
  330. // return $detail;
  331. // }
  332. /**
  333. * 根据ID获得培训班
  334. * @param number $itid
  335. */
  336. function getInvoiceTrainingByItid($itid = 0) {
  337. Doo::loadClass ( 'XDeode' );
  338. $XDeode = new XDeode ( 5 );
  339. if (! is_numeric ( $itid ))
  340. $itid = $XDeode->decode ( $itid );
  341. $detail = array ();
  342. if (! empty ( $itid ) && is_numeric ( $itid )) {
  343. $detail = $this->getOne ( array (
  344. 'where' => " itid=" . $itid,
  345. 'asArray' => TRUE
  346. ) );
  347. }
  348. if (! empty ( $detail )) {
  349. $detail ['trainingKey'] = '';
  350. $detail ['invoiceArriveAmount']=$detail ['invoiceCompanyAmount']=$detail ['RIAmount']=$detail ['invoiceArriveAmount']=$detail ['invoiceTotalAmount']=0;
  351. Doo::loadModel ( 'L_category' );
  352. $lCategory = new L_category ();
  353. Doo::loadModel ( 'staff' );
  354. $staff = new staff ();
  355. $cateList = $lCategory->getCategoryById ( $detail ['cid'] );
  356. $itid = $detail ['itid'];
  357. $detail ['trainingKey'] = $XDeode->encode ( $detail ['itid'] );
  358. $staffdetail = $staff->getStaffBySid ( $detail ['creator'] );
  359. $detail ['staff'] = $staffdetail;
  360. $catedetail = $lCategory->getCategoryById ( $detail ['cid'] );
  361. $detail ['category'] = $catedetail;
  362. // 获得开票金额合计 入账金额合计 入账完成度
  363. Doo::loadModel ( 'invoice' );
  364. $invoice = new invoice ();
  365. //echo $itid;
  366. $invoiceList=$companyList=$RIList = $invoiceRecelvablesList = array ();
  367. if (! empty ( $itid )) {
  368. $itidSql = $itid;
  369. //培训班收款
  370. $invoiceList = $invoice->sumOfinvoiceTrain ( $itidSql );
  371. $invoiceRecelvablesList = $invoice->sumOfInvoiceRecelvablesTrain ( $itidSql );
  372. //公司收款--新的入账--fix
  373. $companyList = $invoice->sumOfInvoiceRecelvablesCompany ( $itidSql );
  374. //获得办事处已收款
  375. $RIList=$invoice->sumOfInvoiceByItid($itid);
  376. //print_r($RIList);
  377. }
  378. //print_r($invoiceRecelvablesList);
  379. //入账金额-培训班
  380. Doo::loadModel ( 'bill' );
  381. $bill = new bill ();
  382. $billPrice=$bill->find(array (
  383. 'select'=>'sum(price) as price',
  384. 'where' => 'trainId='. $itid ,
  385. 'orderby' => 'trainId',
  386. 'asArray' => true
  387. ));
  388. $detail ['billPrice']=0;
  389. if (!empty($billPrice)){
  390. if (!empty($billPrice[0]['price'])){
  391. $detail ['billPrice']=$billPrice[0]['price'];
  392. }
  393. }
  394. $countInvoice = $invoice->getInvoiceByTrainingCount ( $detail ['itid'] );
  395. $detail ['invoiceTotal'] = $countInvoice;
  396. foreach ( $invoiceList as $k => $v ) {
  397. if ($detail ['itid'] == $v ['trainId']) {
  398. $detail ['invoiceTotalAmount'] = $v ['invoicePrice'];
  399. break;
  400. }
  401. }
  402. //入账金额
  403. foreach ( $invoiceRecelvablesList as $i => $o ) {
  404. if ($detail['itid'] == $o ['trainId']) {
  405. $detail ['invoiceArriveAmount'] = $o ['receivablesPrice'];
  406. $detail ['arriveSchedule'] = 0;
  407. if ($detail ['invoiceTotal'] != 0&&$detail ['invoiceArriveAmount']!=0) {
  408. $detail ['arriveSchedule'] = round ( $detail ['invoiceArriveAmount'] / $detail ['invoiceTotalAmount'] * 100, 2 );
  409. }
  410. break;
  411. }
  412. }
  413. //办事处已收款
  414. foreach ($RIList as $key=>$value){
  415. if ($detail ['itid'] == $value ['trainId']) {
  416. $detail ['RIAmount'] = $value ['invoicePrice'];
  417. break;
  418. }
  419. }
  420. //公司已收款
  421. $detail ['invoiceCompanyAmount']=$detail ['invoiceArriveAmount']-$detail ['RIAmount'];
  422. // foreach ($companyList as $key=>$value){
  423. // if ($detail ['itid'] == $value ['trainId']) {
  424. // $detail ['invoiceCompanyAmount'] = $value ['receivablesPrice'];
  425. // break;
  426. // }
  427. // }
  428. $detail ['invoiceCompanyAmountM']=sprintf ( "%.2f", $detail ['invoiceCompanyAmount'] );
  429. $detail ['invoiceArriveAmountM']=sprintf ( "%.2f", $detail ['invoiceArriveAmount'] );
  430. }
  431. //echo var_dump($detail ['invoiceTotalAmount'] ) ;
  432. //print_r($detail);
  433. return $detail;
  434. }
  435. /**
  436. * 根据ID删除数据
  437. * @param number $itid
  438. */
  439. function delInvoiceTrainingByItid($itid = 0) {
  440. Doo::loadClass ( 'XDeode' );
  441. $XDeode = new XDeode ( 5 );
  442. $itid = $XDeode->decode ( $itid );
  443. if (! empty ( $itid ) && is_numeric ( $itid ))
  444. $this->delete ( array (
  445. 'where' => 'itid=' . $itid
  446. ) );
  447. // 删除培训班下的发票
  448. Doo::loadModel ( 'invoiceStore' );
  449. $invoiceStore = new invoiceStore ();
  450. $invoiceStore->delete ( array (
  451. 'where' => 'trainId=' . $itid
  452. ) );
  453. }
  454. /**
  455. * 获得某办事处未完成的培训班
  456. * @return string
  457. */
  458. function getInvoiceTrainingByUnfinished($cid = 0) {
  459. $condition = array (
  460. 'where' => "(status= 0 or status=1) and cid=" . $cid,
  461. 'asArray' => true
  462. );
  463. $list = $this->find ( $condition );
  464. Doo::loadClass ( 'XDeode' );
  465. $XDeode = new XDeode ( 5 );
  466. foreach ( $list as $key => $value ) {
  467. $list [$key] ['itidKey'] = $XDeode->encode ( $value ['itid'] );
  468. }
  469. return $list;
  470. }
  471. /**
  472. * 获得所有培训班
  473. */
  474. function getInvoiceTrainingBySubmitStatus() {
  475. $condition = array (
  476. 'where' => "submitStatus=1",
  477. 'asArray' => TRUE
  478. );
  479. $list = $this->find ( $condition );
  480. Doo::loadClass ( 'XDeode' );
  481. $XDeode = new XDeode ( 5 );
  482. foreach ( $list as $key => $value ) {
  483. $list [$key] ['itidKey'] = $XDeode->encode ( $value ['itid'] );
  484. }
  485. return $list;
  486. }
  487. }
  488. ?>