invoiceTraining.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. /**
  318. * 根据ID获得培训班
  319. * @param number $itid
  320. */
  321. function getInvoiceTrainingByItid($itid = 0) {
  322. Doo::loadClass ( 'XDeode' );
  323. $XDeode = new XDeode ( 5 );
  324. if (! is_numeric ( $itid ))
  325. $itid = $XDeode->decode ( $itid );
  326. $detail = array ();
  327. if (! empty ( $itid ) && is_numeric ( $itid )) {
  328. $detail = $this->getOne ( array (
  329. 'where' => " itid=" . $itid,
  330. 'asArray' => TRUE
  331. ) );
  332. }
  333. if (! empty ( $detail )) {
  334. $detail ['trainingKey'] = '';
  335. $detail ['invoiceArriveAmount']=$detail ['invoiceCompanyAmount']=$detail ['RIAmount']=$detail ['invoiceArriveAmount']=$detail ['invoiceTotalAmount']=0;
  336. Doo::loadModel ( 'L_category' );
  337. $lCategory = new L_category ();
  338. Doo::loadModel ( 'staff' );
  339. $staff = new staff ();
  340. $cateList = $lCategory->getCategoryById ( $detail ['cid'] );
  341. $itid = $detail ['itid'];
  342. $detail ['trainingKey'] = $XDeode->encode ( $detail ['itid'] );
  343. $staffdetail = $staff->getStaffBySid ( $detail ['creator'] );
  344. $detail ['staff'] = $staffdetail;
  345. $catedetail = $lCategory->getCategoryById ( $detail ['cid'] );
  346. $detail ['category'] = $catedetail;
  347. // 获得开票金额合计 入账金额合计 入账完成度
  348. Doo::loadModel ( 'invoice' );
  349. $invoice = new invoice ();
  350. //echo $itid;
  351. $invoiceList=$companyList=$RIList = $invoiceRecelvablesList = array ();
  352. if (! empty ( $itid )) {
  353. $itidSql = $itid;
  354. //培训班收款
  355. $invoiceList = $invoice->sumOfinvoiceTrain ( $itidSql );
  356. $invoiceRecelvablesList = $invoice->sumOfInvoiceRecelvablesTrain ( $itidSql );
  357. //公司收款--新的入账--fix
  358. $companyList = $invoice->sumOfInvoiceRecelvablesCompany ( $itidSql );
  359. //获得办事处已收款
  360. $RIList=$invoice->sumOfInvoiceByItid($itid);
  361. //print_r($RIList);
  362. }
  363. //print_r($invoiceRecelvablesList);
  364. //入账金额-培训班
  365. Doo::loadModel ( 'bill' );
  366. $bill = new bill ();
  367. $billPrice=$bill->find(array (
  368. 'select'=>'sum(price) as price',
  369. 'where' => 'trainId='. $itid ,
  370. 'orderby' => 'trainId',
  371. 'asArray' => true
  372. ));
  373. $detail ['billPrice']=0;
  374. if (!empty($billPrice)){
  375. if (!empty($billPrice[0]['price'])){
  376. $detail ['billPrice']=$billPrice[0]['price'];
  377. }
  378. }
  379. $countInvoice = $invoice->getInvoiceByTrainingCount ( $detail ['itid'] );
  380. $detail ['invoiceTotal'] = $countInvoice;
  381. foreach ( $invoiceList as $k => $v ) {
  382. if ($detail ['itid'] == $v ['trainId']) {
  383. $detail ['invoiceTotalAmount'] = $v ['invoicePrice'];
  384. break;
  385. }
  386. }
  387. //入账金额
  388. foreach ( $invoiceRecelvablesList as $i => $o ) {
  389. if ($detail['itid'] == $o ['trainId']) {
  390. $detail ['invoiceArriveAmount'] = $o ['receivablesPrice'];
  391. $detail ['arriveSchedule'] = 0;
  392. if ($detail ['invoiceTotal'] != 0&&$detail ['invoiceArriveAmount']!=0) {
  393. $detail ['arriveSchedule'] = round ( $detail ['invoiceArriveAmount'] / $detail ['invoiceTotalAmount'] * 100, 2 );
  394. }
  395. break;
  396. }
  397. }
  398. //办事处已收款
  399. foreach ($RIList as $key=>$value){
  400. if ($detail ['itid'] == $value ['trainId']) {
  401. $detail ['RIAmount'] = $value ['invoicePrice'];
  402. break;
  403. }
  404. }
  405. //公司已收款
  406. $detail ['invoiceCompanyAmount']=0;
  407. foreach ($companyList as $key=>$value){
  408. if ($detail ['itid'] == $value ['trainId']) {
  409. $detail ['invoiceCompanyAmount'] = $value ['receivablesPrice'];
  410. break;
  411. }
  412. }
  413. $detail ['invoiceCompanyAmountM']=sprintf ( "%.2f", $detail ['invoiceCompanyAmount'] );
  414. $detail ['invoiceArriveAmountM']=sprintf ( "%.2f", $detail ['invoiceArriveAmount'] );
  415. }
  416. //echo var_dump($detail ['invoiceTotalAmount'] ) ;
  417. //print_r($detail);
  418. return $detail;
  419. }
  420. /**
  421. * 根据ID删除数据
  422. * @param number $itid
  423. */
  424. function delInvoiceTrainingByItid($itid = 0) {
  425. Doo::loadClass ( 'XDeode' );
  426. $XDeode = new XDeode ( 5 );
  427. $itid = $XDeode->decode ( $itid );
  428. if (! empty ( $itid ) && is_numeric ( $itid ))
  429. $this->delete ( array (
  430. 'where' => 'itid=' . $itid
  431. ) );
  432. // 删除培训班下的发票
  433. Doo::loadModel ( 'invoiceStore' );
  434. $invoiceStore = new invoiceStore ();
  435. $invoiceStore->delete ( array (
  436. 'where' => 'trainId=' . $itid
  437. ) );
  438. }
  439. /**
  440. * 获得某办事处未完成的培训班
  441. * @return string
  442. */
  443. function getInvoiceTrainingByUnfinished($cid = 0) {
  444. $condition = array (
  445. 'where' => "(status= 0 or status=1) and cid=" . $cid,
  446. 'asArray' => true
  447. );
  448. $list = $this->find ( $condition );
  449. Doo::loadClass ( 'XDeode' );
  450. $XDeode = new XDeode ( 5 );
  451. foreach ( $list as $key => $value ) {
  452. $list [$key] ['itidKey'] = $XDeode->encode ( $value ['itid'] );
  453. }
  454. return $list;
  455. }
  456. /**
  457. * 获得所有培训班
  458. */
  459. function getInvoiceTrainingBySubmitStatus() {
  460. $condition = array (
  461. 'where' => "submitStatus=1",
  462. 'asArray' => TRUE
  463. );
  464. $list = $this->find ( $condition );
  465. Doo::loadClass ( 'XDeode' );
  466. $XDeode = new XDeode ( 5 );
  467. foreach ( $list as $key => $value ) {
  468. $list [$key] ['itidKey'] = $XDeode->encode ( $value ['itid'] );
  469. }
  470. return $list;
  471. }
  472. }
  473. ?>