invoiceTraining.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. //$invoiceRecelvablesList = $invoice->sumOfInvoiceRecelvablesTrain ( $itidSql );
  186. // 收款有多个发票绑定,导致收款金额没有统计--TODO
  187. // 获得收款扩展部分的金额
  188. // id
  189. $iidList=$invoice->getInvoiceByTrain($itidSql);
  190. foreach ($iidList as $key=>$value){
  191. $idData=explode(",", $value['iid']);
  192. $sum=0;
  193. foreach ($idData as $item){
  194. $sum+=$invoice->getTrainRP($item);
  195. }
  196. //
  197. $itidSum[$value['trainId']]=$sum;
  198. }
  199. }
  200. //print_r($iidList);
  201. // print_r($invoiceRecelvablesList);
  202. // print_r($itidSum);
  203. foreach ( $list as $key => $value ) {
  204. $countInvoice = $invoice->getInvoiceByTrainingCount ( $value ['itid'] );
  205. $value ['invoiceTotal'] = $countInvoice;
  206. $list [$key] ['invoiceTotal'] = $countInvoice;
  207. foreach ( $invoiceList as $k => $v ) {
  208. if ($value ['itid'] == $v ['trainId']) {
  209. $list [$key] ['invoiceTotalAmount'] = $v ['invoicePrice'];
  210. break;
  211. }
  212. }
  213. foreach ( $invoiceRecelvablesList as $i => $o ) {
  214. if ($value ['itid'] == $o ['trainId']) {
  215. $list [$key] ['invoiceArriveAmount'] = $o ['receivablesPrice'];
  216. $list [$key] ['arriveSchedule'] = "0";
  217. if ($value ['invoiceTotal'] != 0) {
  218. $list [$key] ['invoiceArriveAmount']+=$itidSum[$value ['itid']];
  219. $list [$key] ['arriveSchedule'] = round ( $list [$key] ['invoiceArriveAmount'] / $list [$key] ['invoiceTotalAmount'] * 100, 2 );
  220. // $list [$key] ['arriveSchedule'] = round ( $o ['count'] / $value ['invoiceTotal'] * 100, 2 );
  221. }
  222. break;
  223. }
  224. }
  225. }
  226. return $list;
  227. }
  228. /**
  229. * 根据参数字段更新相应字段(主键ID必须传)
  230. * @param array $item 相关需要更新的字段信息
  231. * @return number 返回发票ID
  232. */
  233. public function setInvoiceTrainByCondition($item = array(), $itid = "") {
  234. $lid = 0;
  235. if (is_array ( $item ) && ! empty ( $item )) {
  236. foreach ( $item as $key => $value ) {
  237. $this->$key = $value;
  238. }
  239. if (! empty ( $itid )) {
  240. $condition = array (
  241. 'where' => "itid in (" . $itid . ")",
  242. 'asArray' => TRUE
  243. );
  244. $this->update ( $condition );
  245. } else
  246. $lid = $this->update ();
  247. }
  248. return $lid;
  249. }
  250. /**
  251. * 根据状态获取培训班
  252. * @param number $status
  253. */
  254. function getInvoiceTrainingByStatus($status = 0, $select = "", $cid = 0, $sid = 0) {
  255. Doo::loadModel ( 'staff' );
  256. $staff = new staff ();
  257. Doo::loadModel ( 'L_category' );
  258. $lCategory = new L_category ();
  259. $condition = array (
  260. 'where' => "status= '" . $status . "' and (cid in(" . $cid . ") or creator=" . $sid . " )",
  261. // 'limit' => 8,
  262. 'asArray' => TRUE
  263. );
  264. if (! empty ( $select ))
  265. $condition += array (
  266. 'select' => $select
  267. );
  268. $list = $this->find ( $condition );
  269. Doo::loadClass ( 'XDeode' );
  270. $XDeode = new XDeode ( 5 );
  271. $itid = array ();
  272. foreach ( $list as $key => $value ) {
  273. $list [$key] ['trainingKey'] = $XDeode->encode ( $value ['itid'] );
  274. $detail = $staff->getStaffBySid ( $value ['creator'] );
  275. $list [$key] ['staff'] = $detail;
  276. $detail = $lCategory->getCategoryById ( $value ['cid'] );
  277. $list [$key] ['category'] = $detail;
  278. array_push ( $itid, $value ['itid'] );
  279. }
  280. // 获得开票金额合计 入账金额合计 入账完成度
  281. Doo::loadModel ( 'invoice' );
  282. $invoice = new invoice ();
  283. $invoiceList = $invoiceRecelvablesList = array ();
  284. if (! empty ( $itid )) {
  285. $itidSql = implode ( ',', $itid );
  286. $invoiceList = $invoice->sumOfinvoiceTrain ( $itidSql );
  287. $invoiceRecelvablesList =$invoice->InvoiceTrainIncomesPrice($itid);
  288. //$invoiceRecelvablesList = $invoice->sumOfInvoiceRecelvablesTrain ( $itidSql );
  289. }
  290. // print_r($invoiceRecelvablesList);
  291. //print_r($itidSum);
  292. foreach ( $list as $key => $value ) {
  293. $countInvoice = $invoice->getInvoiceByTrainingCount ( $value ['itid'] );
  294. $value ['invoiceTotal'] = $countInvoice;
  295. $list [$key] ['invoiceTotal'] = $countInvoice;
  296. foreach ( $invoiceList as $k => $v ) {
  297. if ($value ['itid'] == $v ['trainId']) {
  298. $list [$key] ['invoiceTotalAmount'] = $v ['invoicePrice'];
  299. break;
  300. }
  301. }
  302. foreach ( $invoiceRecelvablesList as $i => $o ) {
  303. if ($value ['itid'] == $o ['trainId']) {
  304. $list [$key] ['invoiceArriveAmount'] = $o ['receivablesPrice'];
  305. $list [$key] ['arriveSchedule'] = "0";
  306. if ($value ['invoiceTotal'] != 0) {
  307. $list [$key] ['arriveSchedule'] = round ( ($list [$key] ['invoiceArriveAmount'] )/ $list [$key] ['invoiceTotalAmount'] * 100, 2 );
  308. // $list [$key] ['arriveSchedule'] = round ( $o ['count'] / $value ['invoiceTotal'] * 100, 2 );
  309. }
  310. break;
  311. }
  312. }
  313. }
  314. return $list;
  315. }
  316. /**
  317. * 根据ID获得培训班
  318. * @param number $itid
  319. */
  320. function getInvoiceTrainingByItid($itid = 0) {
  321. Doo::loadClass ( 'XDeode' );
  322. $XDeode = new XDeode ( 5 );
  323. if (! is_numeric ( $itid ))
  324. $itid = $XDeode->decode ( $itid );
  325. $detail = array ();
  326. if (! empty ( $itid ) && is_numeric ( $itid )) {
  327. $detail = $this->getOne ( array (
  328. 'where' => " itid=" . $itid,
  329. 'asArray' => TRUE
  330. ) );
  331. }
  332. if (! empty ( $detail )) {
  333. $detail ['trainingKey'] = '';
  334. $detail ['invoiceArriveAmount']=$detail ['invoiceCompanyAmount']=$detail ['RIAmount']=$detail ['invoiceArriveAmount']=$detail ['invoiceTotalAmount']=0;
  335. Doo::loadModel ( 'L_category' );
  336. $lCategory = new L_category ();
  337. Doo::loadModel ( 'staff' );
  338. $staff = new staff ();
  339. $cateList = $lCategory->getCategoryById ( $detail ['cid'] );
  340. $itid = $detail ['itid'];
  341. $detail ['trainingKey'] = $XDeode->encode ( $detail ['itid'] );
  342. $staffdetail = $staff->getStaffBySid ( $detail ['creator'] );
  343. $detail ['staff'] = $staffdetail;
  344. $catedetail = $lCategory->getCategoryById ( $detail ['cid'] );
  345. $detail ['category'] = $catedetail;
  346. // 获得开票金额合计 入账金额合计 入账完成度
  347. Doo::loadModel ( 'invoice' );
  348. $invoice = new invoice ();
  349. //echo $itid;
  350. $invoiceList=$companyList=$RIList = $invoiceRecelvablesList = array ();
  351. if (! empty ( $itid )) {
  352. $itidSql = $itid;
  353. //培训班收款
  354. $invoiceList = $invoice->sumOfinvoiceTrain ( $itidSql );
  355. $invoiceRecelvablesList = $invoice->sumOfInvoiceRecelvablesTrain ( $itidSql );
  356. //公司收款--新的入账--fix
  357. $companyList = $invoice->sumOfInvoiceRecelvablesCompany ( $itidSql );
  358. //获得办事处已收款
  359. $RIList=$invoice->sumOfInvoiceByItid($itid);
  360. //print_r($RIList);
  361. }
  362. //print_r($invoiceRecelvablesList);
  363. //入账金额-培训班
  364. Doo::loadModel ( 'bill' );
  365. $bill = new bill ();
  366. $billPrice=$bill->find(array (
  367. 'select'=>'sum(price) as price',
  368. 'where' => 'trainId='. $itid ,
  369. 'orderby' => 'trainId',
  370. 'asArray' => true
  371. ));
  372. $detail ['billPrice']=0;
  373. if (!empty($billPrice)){
  374. if (!empty($billPrice[0]['price'])){
  375. $detail ['billPrice']=$billPrice[0]['price'];
  376. }
  377. }
  378. $countInvoice = $invoice->getInvoiceByTrainingCount ( $detail ['itid'] );
  379. $detail ['invoiceTotal'] = $countInvoice;
  380. foreach ( $invoiceList as $k => $v ) {
  381. if ($detail ['itid'] == $v ['trainId']) {
  382. $detail ['invoiceTotalAmount'] = $v ['invoicePrice'];
  383. break;
  384. }
  385. }
  386. //入账金额
  387. foreach ( $invoiceRecelvablesList as $i => $o ) {
  388. if ($detail['itid'] == $o ['trainId']) {
  389. $detail ['invoiceArriveAmount'] = $o ['receivablesPrice'];
  390. $detail ['arriveSchedule'] = 0;
  391. if ($detail ['invoiceTotal'] != 0&&$detail ['invoiceArriveAmount']!=0) {
  392. $detail ['arriveSchedule'] = round ( $detail ['invoiceArriveAmount'] / $detail ['invoiceTotalAmount'] * 100, 2 );
  393. }
  394. break;
  395. }
  396. }
  397. //办事处已收款
  398. foreach ($RIList as $key=>$value){
  399. if ($detail ['itid'] == $value ['trainId']) {
  400. $detail ['RIAmount'] = $value ['invoicePrice'];
  401. break;
  402. }
  403. }
  404. //公司已收款
  405. $detail ['invoiceCompanyAmount']=0;
  406. foreach ($companyList as $key=>$value){
  407. if ($detail ['itid'] == $value ['trainId']) {
  408. $detail ['invoiceCompanyAmount'] = $value ['receivablesPrice'];
  409. break;
  410. }
  411. }
  412. $detail ['invoiceCompanyAmountM']=sprintf ( "%.2f", $detail ['invoiceCompanyAmount'] );
  413. $detail ['invoiceArriveAmountM']=sprintf ( "%.2f", $detail ['invoiceArriveAmount'] );
  414. }
  415. //echo var_dump($detail ['invoiceTotalAmount'] ) ;
  416. //print_r($detail);
  417. return $detail;
  418. }
  419. /**
  420. * 根据ID删除数据
  421. * @param number $itid
  422. */
  423. function delInvoiceTrainingByItid($itid = 0) {
  424. Doo::loadClass ( 'XDeode' );
  425. $XDeode = new XDeode ( 5 );
  426. $itid = $XDeode->decode ( $itid );
  427. if (! empty ( $itid ) && is_numeric ( $itid ))
  428. $this->delete ( array (
  429. 'where' => 'itid=' . $itid
  430. ) );
  431. // 删除培训班下的发票
  432. Doo::loadModel ( 'invoiceStore' );
  433. $invoiceStore = new invoiceStore ();
  434. $invoiceStore->delete ( array (
  435. 'where' => 'trainId=' . $itid
  436. ) );
  437. }
  438. /**
  439. * 获得某办事处未完成的培训班
  440. * @return string
  441. */
  442. function getInvoiceTrainingByUnfinished($cid = 0) {
  443. $condition = array (
  444. 'where' => "(status= 0 or status=1) and cid=" . $cid,
  445. 'asArray' => true
  446. );
  447. $list = $this->find ( $condition );
  448. Doo::loadClass ( 'XDeode' );
  449. $XDeode = new XDeode ( 5 );
  450. foreach ( $list as $key => $value ) {
  451. $list [$key] ['itidKey'] = $XDeode->encode ( $value ['itid'] );
  452. }
  453. return $list;
  454. }
  455. /**
  456. * 获得所有培训班
  457. */
  458. function getInvoiceTrainingBySubmitStatus() {
  459. $condition = array (
  460. 'where' => "submitStatus=1",
  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. ?>