Invoice.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace User\Lib;
  3. use Dever;
  4. class Invoice
  5. {
  6. # 获取发票
  7. public function get($source_id, $source_table, $uid, $address_id)
  8. {
  9. $info = $this->getData($source_id, $source_table, 1);
  10. if (!$info) {
  11. $info = $this->getInfo($uid, $address_id);
  12. }
  13. return $info;
  14. }
  15. # 获取默认发票
  16. public function getData($source_id, $source_table = 'user/info', $type = 2)
  17. {
  18. $where['source_table'] = $source_table;
  19. $where['source_id'] = $source_id;
  20. $where['type'] = $type;
  21. $data = Dever::db('user/invoice')->one($where);
  22. if ($data) {
  23. $data = $this->info($data);
  24. }
  25. return $data;
  26. }
  27. # 获取某个发票
  28. public function getInfo($source_id, $id, $source_table = 'user/info')
  29. {
  30. $where['source_table'] = $source_table;
  31. $where['source_id'] = $source_id;
  32. $where['id'] = $id;
  33. $data = Dever::db('user/invoice')->one($where);
  34. if ($data) {
  35. $data = $this->info($data);
  36. }
  37. return $data;
  38. }
  39. # 获取发票列表
  40. public function getList($source_id, $source_table = 'user/info')
  41. {
  42. $where['source_table'] = $source_table;
  43. $where['source_id'] = $source_id;
  44. $data = Dever::db('user/invoice')->getList($where);
  45. if ($data) {
  46. foreach ($data as $k => $v) {
  47. $data[$k] = $this->info($v);
  48. }
  49. }
  50. return $data;
  51. }
  52. public function info($data)
  53. {
  54. $data['title_type_name'] = Dever::db('user/invoice')->config['title_type'][$data['title_type']];
  55. return $data;
  56. }
  57. # 更新
  58. public function update($id, $source_id, $source_table, $type = 2, $title_type = 1, $title = '', $email = '', $mobile = '', $number = '', $phone = '', $address = '', $bank = '', $bank_number = '')
  59. {
  60. $update['title_type'] = $title_type;
  61. if ($title_type == 1) {
  62. if (!$number) {
  63. Dever::alert('公司税号不能为空');
  64. }
  65. $update['number'] = $number;
  66. }
  67. $update['type'] = $type;
  68. $update['title'] = $title;
  69. if (!$title) {
  70. Dever::alert('发票抬头不能为空');
  71. }
  72. if ($email) {
  73. $update['email'] = $email;
  74. }
  75. if ($mobile) {
  76. $update['mobile'] = $mobile;
  77. }
  78. if ($phone) {
  79. $update['phone'] = $phone;
  80. }
  81. if ($address) {
  82. $update['address'] = $address;
  83. }
  84. if ($bank) {
  85. $update['bank'] = $bank;
  86. }
  87. if ($bank_number) {
  88. $update['bank_number'] = $bank_number;
  89. }
  90. if ($type == 2) {
  91. Dever::db('user/invoice')->updateType(array('where_type' => 2, 'set_type' => 1));
  92. }
  93. if ($id) {
  94. $update['where_id'] = $id;
  95. Dever::db('user/invoice')->update($update);
  96. } else {
  97. $update['source_id'] = $source_id;
  98. $update['source_table'] = $source_table;
  99. $id = Dever::db('user/invoice')->insert($update);
  100. }
  101. return $id;
  102. }
  103. # 删除和恢复
  104. public function delete($source_id, $id, $state = 2, $source_table = 'user/info')
  105. {
  106. $info = $this->getInfo($source_id, $id, $source_table);
  107. if ($info) {
  108. $update['where_id'] = $info['id'];
  109. $update['state'] = $state;
  110. Dever::db('user/invoice')->update($update);
  111. } else {
  112. Dever::alert('更新失败');
  113. }
  114. return 'ok';
  115. }
  116. public function getSource($source_table, $source_id)
  117. {
  118. $info = Dever::db($source_table)->one($source_id);
  119. return $info;
  120. }
  121. public function getManageUrl($source_table, $source_id, $type)
  122. {
  123. $info = Dever::db('user/invoice')->one(array('source_table' => $source_table, 'source_id' => $source_id));
  124. $url = Dever::url('project/database/update?project=user&table=invoice&search_option_source_table=' . $source_table . '&search_option_source_id=' . $source_id, 'manage');
  125. if ($info) {
  126. $url .= '&where_id=' . $info['id'];
  127. }
  128. return $url;
  129. }
  130. }