InvoiceController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <?php
  2. /**
  3. * 开票功能控制器
  4. * @author CP
  5. * @version 1.0
  6. * @namespace invoice
  7. * @package invoiceController
  8. */
  9. class InvoiceController extends DooController {
  10. public $staff;
  11. public $verifyId;
  12. public $executeId;
  13. public static $NEW = 0;
  14. public static $NEW2 = 0;
  15. private $INVOICEKEY = "APPROVAL";
  16. function __construct() {
  17. if (isset ( $_COOKIE ["staff"] )) {
  18. if (! empty ( $_COOKIE ["staff"] )) {
  19. Doo::loadModel ( 'staff' );
  20. Doo::loadModel ( 'verify' );
  21. $verify = new verify ();
  22. $staff = new staff ();
  23. Doo::loadModel ( "execute" );
  24. $execute = new execute ();
  25. $verifyList = $verify->find ( array (
  26. 'select' => 'staff',
  27. 'asArray' => true
  28. ) );
  29. $list = array ();
  30. // 判断角色的审批权限
  31. foreach ( $verifyList as $key => $value ) {
  32. $ver = json_decode ( $value ['staff'] );
  33. foreach ( $ver as $k => $v ) {
  34. if ($v [1] == 'ROLE') {
  35. $roleList = json_decode ( $v [3] );
  36. foreach ( $roleList as $t => $g ) {
  37. $gList = explode ( "_", $g );
  38. array_push ( $list, $gList [0] );
  39. // print_r($list);
  40. }
  41. } else
  42. array_push ( $list, $v [0] );
  43. }
  44. }
  45. // 判断执行人的审批权限
  46. $executeList = $execute->find ( array (
  47. 'select' => 'staff',
  48. 'asArray' => true
  49. ) );
  50. $list2 = array ();
  51. foreach ( $executeList as $key => $value ) {
  52. $ver = json_decode ( $value ['staff'] );
  53. foreach ( $ver as $k => $v ) {
  54. array_push ( $list2, $v [0] );
  55. }
  56. }
  57. $eidList = file_get_contents ( "protected/config/execute/execute.ini" );
  58. $eidList = array_filter ( explode ( ",", $eidList ) );
  59. $this->executeId = array_merge ( $list2, $eidList );
  60. $this->verifyId = $list;
  61. $this->staff = $staff->getUserByIdList ( $_COOKIE ["staff"] );
  62. ReceiptController::$NEW = $this->getReceiptCount ();
  63. ReceiptController::$NEW2 = $this->getExeCount ();
  64. return "/";
  65. }
  66. }
  67. Doo::loadCore ( 'uri/DooUriRouter' );
  68. $router = new DooUriRouter ();
  69. $routeRs = $router->execute ( Doo::app ()->route, Doo::conf ()->SUBFOLDER );
  70. if ($routeRs ['1'] != "login") {
  71. header ( 'Content-Type:text/html;charset=utf-8' );
  72. @header ( "Location: /login" );
  73. }
  74. }
  75. function invoice() {
  76. $data ['memu'] = "invoice";
  77. $data ['staff'] = $this->staff;
  78. $data ['receiptMemu'] = 'invoice';
  79. $data ['verifyId'] = $this->verifyId;
  80. $data ['executeId'] = $this->executeId;
  81. $this->render ( "/admin/invoice", $data );
  82. }
  83. function invoiceAdd() {
  84. Doo::loadModel ( 'L_category' );
  85. $lCategory = new L_category ();
  86. $category = $lCategory->find ( array (
  87. 'asArray' => true
  88. ) );
  89. $data ['category'] = $category;
  90. $data ['memu'] = "invoice";
  91. $data ['staff'] = $this->staff;
  92. $data ['receiptMemu'] = 'invoice';
  93. $data ['verifyId'] = $this->verifyId;
  94. $data ['executeId'] = $this->executeId;
  95. $this->render ( "/admin/invoiceAdd", $data );
  96. }
  97. /**
  98. * 提交一份开票申请,并记录下操作日志
  99. *
  100. * @since 1.0.0
  101. *
  102. * @var integer cid 办事处ID
  103. * @var integer invoiceType 发票类型
  104. * @var integer doPost 是否邮寄
  105. * @var integer invoicePrice 开票金额
  106. * @var string invoiceElement 开票内容
  107. * @var string invoiceTitle 发票抬头
  108. * @var string invoiceCompany 单位名称
  109. * @var string TIN 纳税人识别码
  110. * @var string address 注册地址
  111. * @var string phone 注册电话
  112. * @var string bank 开户银行
  113. * @var string bankAccount 银行账户
  114. * @var string recipients 收件人
  115. * @var string recipientsPhone 收件人手机/电话
  116. * @var string recipientsAddress 收件地址
  117. * @var string mailItems 邮寄物品
  118. * @var string remark 备注
  119. * @return string 返回跳转开票主页路径
  120. */
  121. function invoiceAddDo() {
  122. $cid = $this->get_args ( 'cid' ) && is_numeric ( $this->get_args ( 'cid' ) ) ? $this->get_args ( 'cid' ) : 0;
  123. $invoiceType = $this->get_args ( 'invoiceType' ) && is_numeric ( $this->get_args ( 'invoiceType' ) ) ? $this->get_args ( 'invoiceType' ) : 0;
  124. $doPost = $this->get_args ( 'doPost' ) && is_numeric ( $this->get_args ( 'doPost' ) ) ? $this->get_args ( 'doPost' ) : 0;
  125. $invoicePrice = $this->get_args ( 'invoicePrice' ) ? $this->get_args ( 'invoicePrice' ) : "";
  126. $invoiceElement = $this->get_args ( 'invoiceElement' ) ? $this->get_args ( 'invoiceElement' ) : "";
  127. $invoiceTitle = $this->get_args ( 'invoiceTitle' ) ? $this->get_args ( 'invoiceTitle' ) : "";
  128. $invoiceCompany = $this->get_args ( 'invoiceCompany' ) ? $this->get_args ( 'invoiceCompany' ) : "";
  129. $TIN = $this->get_args ( 'TIN' ) ? $this->get_args ( 'TIN' ) : "";
  130. $address = $this->get_args ( 'address' ) ? $this->get_args ( 'address' ) : "";
  131. $phone = $this->get_args ( 'phone' ) ? $this->get_args ( 'phone' ) : "";
  132. $bank = $this->get_args ( 'bank' ) ? $this->get_args ( 'bank' ) : "";
  133. $bankAccount = $this->get_args ( 'bankAccount' ) ? $this->get_args ( 'bankAccount' ) : "";
  134. $recipients = $this->get_args ( 'recipients' ) ? $this->get_args ( 'recipients' ) : "";
  135. $recipientsPhone = $this->get_args ( 'recipientsPhone' ) ? $this->get_args ( 'recipientsPhone' ) : "";
  136. $recipientsAddress = $this->get_args ( 'recipientsAddress' ) ? $this->get_args ( 'recipientsAddress' ) : "";
  137. $mailItems = $this->get_args ( 'mailItems' ) ? $this->get_args ( 'mailItems' ) : "";
  138. $remark = $this->get_args ( 'remark' ) ? $this->get_args ( 'remark' ) : "";
  139. if (! empty ( $cid ) && ! empty ( $invoicePrice ) && ! empty ( $invoiceElement )) {
  140. Doo::loadModel ( 'invoice' );
  141. $invoice = new invoice ();
  142. Doo::loadModel ( 'L_category' );
  143. $lCategory = new L_category ();
  144. Doo::loadModel ( 'invoiceManage' );
  145. $invoiceManage = new invoiceManage ();
  146. Doo::loadModel ( 'invoiceOperationLog' );
  147. $invoiceOperationLog = new invoiceOperationLog ();
  148. $categoryDetil = $lCategory->getOne ( array (
  149. 'SELECT' => 'title',
  150. 'where' => 'cid=' . $cid,
  151. 'asArray' => true
  152. ) );
  153. $invoiceManageDetail = $invoiceManage->getOne ( array (
  154. 'SELECT' => 'title',
  155. 'where' => 'mold="发票审批"',
  156. 'asArray' => true
  157. ) );
  158. $invoice->cid = $cid;
  159. $invoice->categoryName = $categoryDetil ['title'];
  160. $invoice->invoiceElement = $invoiceElement;
  161. $invoice->invoicePrice = $invoicePrice;
  162. $invoice->invoiceType = $invoiceType;
  163. if ($invoiceType == 0)
  164. $invoice->invoiceTitle = $invoiceTitle;
  165. elseif ($invoiceType == 1) {
  166. $invoice->invoiceCompany = $invoiceCompany;
  167. $invoice->TIN = $TIN;
  168. $invoice->address = $address;
  169. $invoice->phone = $phone;
  170. $invoice->bank = $bank;
  171. $invoice->bankAccount = $bankAccount;
  172. }
  173. $invoice->doPost = $doPost;
  174. if ($doPost == 1) {
  175. $invoice->recipients = $recipients;
  176. $invoice->recipientsPhone = $recipientsPhone;
  177. $invoice->recipientsAddress = $recipientsAddress;
  178. $invoice->mailItems = $mailItems;
  179. }
  180. $invoice->status = 1;
  181. $invoice->invoiceSerial = "#F" . date ( "Ymd" ) . mt_rand ( 1000, 9999 );
  182. $invoice->date = date ( "Y-m-d H:i:s" );
  183. $invoice->updateTime = date ( "Y-m-d H:i:s" );
  184. $invoice->sid = $this->staff [0] ['sid'];
  185. $invoice->userName = $this->staff [0] ['username'];
  186. $invoice->invoiceManage = $invoiceManageDetail ['staff'];
  187. $staffId = $a = json_decode ( $invoiceManageDetail ['staff'] );
  188. $invoice->pendingApprovals = $staffId [0] [0];
  189. $invoice->remark = $remark;
  190. $iid = $invoice->insert ();
  191. $item = array (
  192. 'date' => date ( "Y-m-d H:i:s" ),
  193. 'operation' => "提交审批",
  194. 'status' => 1,
  195. 'img' => $this->staff [0] ['avatar'],
  196. 'username' => $this->staff [0] ['username'],
  197. 'uid' => $this->staff [0] ['sid'],
  198. 'iid' => $iid
  199. );
  200. $invoiceOperationLog->setInvoiceOperationLog ( $item );
  201. }
  202. return "/invoice";
  203. }
  204. function invoiceApproval() {
  205. Doo::loadModel ( 'invoice' );
  206. $invoice = new invoice ();
  207. Doo::loadModel ( 'invoiceManage' );
  208. $invoiceManage = new invoiceManage ();
  209. Doo::loadClass ( 'XDeode' );
  210. $XDeode = new XDeode ( 5 );
  211. Doo::loadModel ( 'staff' );
  212. $staff = new staff ();
  213. $invoiceList = $invoice->find ( array (
  214. 'where' => 'status=1 and pendingApprovals=' . $this->staff [0] ['sid'],
  215. 'desc' => 'iid',
  216. 'asArray' => true
  217. ) );
  218. foreach ( $invoiceList as $key => $value ) {
  219. $invoiceList [$key] ['iidKey'] = $XDeode->encode ( $value ['iid'] );
  220. $lastApprover = json_decode ( $value ['processApprovals'], true );
  221. if (! empty ( $lastApprover )) {
  222. $lastApproverKey = array_keys ( $lastApprover );
  223. $lastApprover = end ( $lastApprover );
  224. $staffDetail = $staff->getOne ( array (
  225. 'where' => 'sid=' . end ( $lastApproverKey ),
  226. 'asArray' => true
  227. ) );
  228. $lastApprover ['username'] = $staffDetail ['username'];
  229. }
  230. $invoiceList [$key] ['lastApprover'] = $lastApprover;
  231. }
  232. $data ['invoiceList'] = $invoiceList;
  233. $data ['memu'] = "invoice";
  234. $data ['staff'] = $this->staff;
  235. $data ['receiptMemu'] = 'invoiceApproval';
  236. $data ['verifyId'] = $this->verifyId;
  237. $data ['executeId'] = $this->executeId;
  238. $this->render ( "/admin/invoiceApproval", $data );
  239. }
  240. function invoiceDetail() {
  241. Doo::loadClass ( 'XDeode' );
  242. $XDeode = new XDeode ( 5 );
  243. $iid = isset ( $this->params ['iid'] ) ? $this->params ['iid'] : "";
  244. $iid = $XDeode->decode ( $iid );
  245. if (! is_numeric ( $iid ))
  246. die ( 'illegal request' );
  247. Doo::loadModel ( 'invoice' );
  248. $invoice = new invoice ();
  249. // Doo::loadModel('invoiceManage');
  250. // $invoiceManage=new invoiceManage();
  251. $invoiceDetail = $invoice->getOne ( array (
  252. 'where' => 'status=1 and pendingApprovals=' . $this->staff [0] ['sid'] . ' and iid=' . $iid,
  253. 'asArray' => true
  254. ) );
  255. $data ['invoiceDetail'] = $invoiceDetail;
  256. $data ['INVOICEKEY'] = $this->authcode ( $invoiceDetail ['iid'], '' );
  257. $data ['memu'] = "invoice";
  258. $data ['staff'] = $this->staff;
  259. $data ['receiptMemu'] = 'invoiceApproval';
  260. $data ['verifyId'] = $this->verifyId;
  261. $data ['executeId'] = $this->executeId;
  262. $this->render ( "/admin/invoiceApprovalDetail", $data );
  263. }
  264. /**
  265. * 对发票进行审批,其中操作有终止,退回,同意动作。操作成功并记录下操作日志
  266. *
  267. * @since 1.0.0
  268. *
  269. * @var integer iid 开票ID 已加密
  270. * @var integer status 发票审批状态
  271. * @var integer opintion 审批发票的意见
  272. * @return string 如操作成功返回审批首页
  273. */
  274. function invoiceApprovalDo() {
  275. $iid = $this->get_args ( 'invoiceKey' ) ? $this->get_args ( 'invoiceKey' ) : "";
  276. $status = $this->get_args ( 'status' ) && is_numeric ( $this->get_args ( 'status' ) ) ? $this->get_args ( 'status' ) : 0;
  277. $opinion = $this->get_args ( 'opinion' ) ? $this->get_args ( 'opinion' ) : "";
  278. $iid = $this->authcode ( $iid );
  279. if (! is_numeric ( $iid ))
  280. die ( 'illegal request' );
  281. if (! empty ( $iid ) && ! empty ( $status ) && ! empty ( $opinion )) {
  282. if (! ($status == 2 || $status == 3 || $status == 4))
  283. die ( 'illegal request' );
  284. Doo::loadModel ( 'invoice' );
  285. $invoice = new invoice ();
  286. Doo::loadModel ( 'invoiceOperationLog' );
  287. $invoiceOperationLog = new invoiceOperationLog ();
  288. $invoiceDetail = $invoice->getOne ( array (
  289. 'where' => 'status=1 and iid=' . $iid . ' and pendingApprovals=' . $this->staff [0] ['sid'],
  290. 'asArray' => true
  291. ) );
  292. if (empty ( $invoiceDetail ))
  293. die ( 'illegal request' );
  294. $processApprovals = json_decode ( $invoiceDetail ['processApprovals'], true );
  295. $invoiceManage = json_decode ( $invoiceDetail ['invoiceManage'], true );
  296. if ($status == 3) {
  297. $invoice->status = $status;
  298. $item = array (
  299. 'operation' => "退回"
  300. );
  301. } else {
  302. if (empty ( $processApprovals )) {
  303. $processApprovals = array (
  304. $this->staff [0] ['sid'] => array (
  305. 'date' => date ( "Y-m-d H:i:s" ),
  306. 'opinion' => $opinion,
  307. 'status' => $status
  308. )
  309. );
  310. if ($status != 4 && isset ( $invoiceManage [1] ))
  311. $invoice->pendingApprovals = $invoiceManage [1] [0];
  312. $invoice->processApprovals = json_encode ( $processApprovals );
  313. } else {
  314. $processApprovals [$this->staff [0] ['sid']] = array (
  315. 'date' => date ( "Y-m-d H:i:s" ),
  316. 'opinion' => $opinion,
  317. 'status' => $status
  318. );
  319. $pendingApprovals = 0;
  320. foreach ( $invoiceManage as $key => $value ) {
  321. if ($value [0] == $this->staff [0] ['sid']) {
  322. if (isset ( $invoiceManage [$key + 1] ))
  323. $pendingApprovals = $invoiceManage [$key + 1] [0];
  324. break;
  325. }
  326. }
  327. if ($status != 4 && ! empty ( $pendingApprovals ))
  328. $invoice->pendingApprovals = $pendingApprovals;
  329. $invoice->processApprovals = json_encode ( $processApprovals );
  330. }
  331. $item = array (
  332. 'operation' => "同意"
  333. );
  334. if (count ( $processApprovals ) == count ( $invoiceManage ) && $status != 4) {
  335. $invoice->status = $status;
  336. $invoice->pendingApprovals = 0;
  337. } elseif ($status == 4) {
  338. $invoice->status = $status;
  339. $item = array (
  340. 'operation' => "终止"
  341. );
  342. }
  343. }
  344. $invoice->updateTime = date ( "Y-m-d H:i:s" );
  345. $invoice->update ( array (
  346. 'where' => 'iid=' . $iid
  347. ) );
  348. $item += array (
  349. 'date' => date ( "Y-m-d H:i:s" ),
  350. 'img' => $this->staff [0] ['avatar'],
  351. 'username' => $this->staff [0] ['username'],
  352. 'uid' => $this->staff [0] ['sid'],
  353. 'status' => $status,
  354. 'iid' => $iid
  355. );
  356. $invoiceOperationLog->setInvoiceOperationLog ( $item );
  357. return '/invoiceApproval';
  358. }
  359. die ( 'illegal request' );
  360. }
  361. private function getExeCount() {
  362. Doo::loadModel ( 'receipt' );
  363. $receipt = new receipt ();
  364. $receiptList = $receipt->find ( array (
  365. 'where' => '(executeCopy like \'%["' . $this->staff [0] ['sid'] . '%\' and executeStaff NOT LIKE \'%' . $this->staff [0] ['sid'] . '%\' ) and (status=1 or status=6)',
  366. 'desc' => 'rid',
  367. 'asArray' => true
  368. ) );
  369. return count ( $receiptList );
  370. }
  371. private function getReceiptCount() {
  372. $status = 2;
  373. $year = date ( 'Y' );
  374. Doo::loadModel ( 'receipt' );
  375. $receipt = new receipt ();
  376. Doo::loadModel ( 'verify' );
  377. $dateCondition = " and Year(date) =" . $year;
  378. $approvalCondition = ' and nowStaff like "%' . $this->staff [0] ['sid'] . '%" ';
  379. $receiptList = $receipt->find ( array (
  380. 'where' => ' status=' . $status . $dateCondition . $approvalCondition,
  381. 'desc' => 'rid',
  382. 'asArray' => true
  383. ) );
  384. return count ( $receiptList );
  385. }
  386. function _GetFileEXT($filename) {
  387. $pics = explode ( '.', $filename );
  388. $num = count ( $pics );
  389. return $pics [$num - 1];
  390. }
  391. /**
  392. * 获取get或者POST值
  393. *
  394. * @param string $name
  395. * 属性名称
  396. * @return fixed 值
  397. */
  398. function get_args($name) {
  399. if (isset ( $_GET [$name] )) {
  400. if (is_array ( $_GET [$name] ))
  401. return $_GET [$name];
  402. else
  403. return addslashes ( $_GET [$name] );
  404. } elseif (isset ( $_POST [$name] )) {
  405. if (is_array ( $_POST [$name] ))
  406. return $_POST [$name];
  407. else
  408. return addslashes ( $_POST [$name] );
  409. } else
  410. return false;
  411. }
  412. /**
  413. * 加密或解密指定字符串
  414. *
  415. * @param string $string
  416. * 要加密或解密的字符串
  417. * @param string $operation
  418. * 当取值为'DECODE'时表示解密,否则为加密
  419. * @param string $key
  420. * 加解密的key
  421. * @param $expiry 超时值
  422. *
  423. */
  424. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  425. $ckey_length = 4;
  426. if (! $key) {
  427. $key = $this->INVOICEKEY;
  428. }
  429. $key = md5 ( $key );
  430. $keya = md5 ( substr ( $key, 0, 16 ) );
  431. $keyb = md5 ( substr ( $key, 16, 16 ) );
  432. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  433. $cryptkey = $keya . md5 ( $keya . $keyc );
  434. $key_length = strlen ( $cryptkey );
  435. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  436. $string_length = strlen ( $string );
  437. $result = '';
  438. $box = range ( 0, 255 );
  439. $rndkey = array ();
  440. for($i = 0; $i <= 255; $i ++) {
  441. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  442. }
  443. for($j = $i = 0; $i < 256; $i ++) {
  444. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  445. $tmp = $box [$i];
  446. $box [$i] = $box [$j];
  447. $box [$j] = $tmp;
  448. }
  449. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  450. $a = ($a + 1) % 256;
  451. $j = ($j + $box [$a]) % 256;
  452. $tmp = $box [$a];
  453. $box [$a] = $box [$j];
  454. $box [$j] = $tmp;
  455. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  456. }
  457. if ($operation == 'DECODE') {
  458. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  459. return substr ( $result, 26 );
  460. } else {
  461. return '';
  462. }
  463. } else {
  464. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  465. }
  466. }
  467. }
  468. class ReceiptController {
  469. public static $NEW = 0;
  470. public static $NEW2 = 0;
  471. }
  472. ?>