StoreOrderInvoice.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\order;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\order\StoreOrderCartInfoServices;
  14. use app\services\order\StoreOrderInvoiceServices;
  15. use app\services\order\StoreOrderServices;
  16. use app\services\product\product\StoreProductServices;
  17. use app\services\serve\ServeServices;
  18. use app\services\system\store\SystemStoreServices;
  19. use app\services\user\UserServices;
  20. use think\facade\App;
  21. /**
  22. * 发票管理
  23. * Class StoreOrderInvoice
  24. * @package app\adminapi\controller\v1\order
  25. */
  26. class StoreOrderInvoice extends AuthController
  27. {
  28. /**
  29. * StoreOrderInvoice constructor.
  30. * @param App $app
  31. * @param StoreOrderInvoiceServices $services
  32. */
  33. public function __construct(App $app, StoreOrderInvoiceServices $services)
  34. {
  35. parent::__construct($app);
  36. $this->services = $services;
  37. }
  38. /**
  39. * 获取订单类型数量
  40. * @return mixed
  41. */
  42. public function chart()
  43. {
  44. $where = $this->request->getMore([
  45. ['data', '', '', 'time'],
  46. ['real_name', ''],
  47. ['field_key', ''],
  48. [['type', 'd'], 0],
  49. ]);
  50. $data = $this->services->chart($where);
  51. return app('json')->success($data);
  52. }
  53. /**
  54. * 查询发票列表
  55. * @return mixed
  56. */
  57. public function list()
  58. {
  59. $where = $this->request->getMore([
  60. ['status', 0],
  61. ['real_name', ''],
  62. ['header_type', ''],
  63. ['type', ''],
  64. ['data', '', '', 'time'],
  65. ['field_key', ''],
  66. ]);
  67. return app('json')->success($this->services->getList($where));
  68. }
  69. /**
  70. * 设置发票状态
  71. * @param string $id
  72. * @return mixed
  73. */
  74. public function set_invoice($id = '')
  75. {
  76. if ($id == '') return app('json')->fail('参数错误');
  77. $data = $this->request->postMore([
  78. ['is_invoice', 0],
  79. ['invoice_number', 0],
  80. ['remark', '']
  81. ]);
  82. if ($data['is_invoice'] == 1 && !$data['invoice_number']) {
  83. return app('json')->fail('请填写开票号');
  84. }
  85. $this->services->setInvoice((int)$id, $data);
  86. return app('json')->success('设置成功');
  87. }
  88. /**
  89. * 订单详情
  90. * @param $id 订单id
  91. * @return mixed
  92. */
  93. public function orderInfo(StoreProductServices $productServices, StoreOrderServices $orderServices, $id)
  94. {
  95. if (!$id || !($orderInfo = $orderServices->get($id))) {
  96. return app('json')->fail('订单不存在');
  97. }
  98. /** @var UserServices $services */
  99. $services = app()->make(UserServices::class);
  100. $userInfo = $services->get($orderInfo['uid']);
  101. if (!$userInfo) {
  102. return app('json')->fail('用户信息不存在');
  103. }
  104. $userInfo = $userInfo->hidden(['pwd', 'add_ip', 'last_ip', 'login_type']);
  105. $userInfo['spread_name'] = '';
  106. if ($userInfo['spread_uid'])
  107. $userInfo['spread_name'] = $services->value(['uid' => $userInfo['spread_uid']], 'nickname');
  108. $orderInfo = $orderServices->tidyOrder($orderInfo->toArray(), true, true);
  109. //核算优惠金额
  110. $vipTruePrice = array_column($orderInfo['cartInfo'], 'vip_sum_truePrice');
  111. $vipTruePrice = array_sum($vipTruePrice);
  112. $orderInfo['vip_true_price'] = $vipTruePrice ?: 0;
  113. $orderInfo['add_time'] = $orderInfo['_add_time'] ?? '';
  114. $productId = array_column($orderInfo['cartInfo'], 'product_id');
  115. $cateData = $productServices->productIdByProductCateName($productId);
  116. foreach ($orderInfo['cartInfo'] as &$item) {
  117. $item['class_name'] = $cateData[$item['product_id']] ?? '';
  118. }
  119. if ($orderInfo['store_id'] && $orderInfo['shipping_type'] == 2) {
  120. /** @var $storeServices */
  121. $storeServices = app()->make(SystemStoreServices::class);
  122. $orderInfo['_store_name'] = $storeServices->value(['id' => $orderInfo['store_id']], 'name');
  123. } else {
  124. $orderInfo['_store_name'] = '';
  125. }
  126. $userInfo = $userInfo->toArray();
  127. $invoice = $this->services->getOne(['order_id' => $id]);
  128. return app('json')->success(compact('orderInfo', 'userInfo', 'invoice'));
  129. }
  130. /**
  131. * 获取电子发票配置信息
  132. * @return \think\Response
  133. * @author wuhaotian
  134. * @email 442384644@qq.com
  135. * @date 2024/5/16
  136. */
  137. public function elecInvoiceConfig()
  138. {
  139. $data = [
  140. 'elec_invoice' => (int)sys_config('elec_invoice'),
  141. 'auto_invoice' => (int)sys_config('auto_invoice'),
  142. 'elec_invoice_cate' => (int)sys_config('elec_invoice_cate'),
  143. 'elec_invoice_cate_name' => sys_config('elec_invoice_cate_name'),
  144. 'elec_invoice_tax_rate' => (int)sys_config('elec_invoice_tax_rate')
  145. ];
  146. return app('json')->success($data);
  147. }
  148. /**
  149. * 获取发票开具页面iframe地址
  150. * @param $id
  151. * @return \think\Response
  152. * @throws \ReflectionException
  153. * @author wuhaotian
  154. * @email 442384644@qq.com
  155. * @date 2024/5/13
  156. */
  157. public function invoiceIssuanceUrl($id)
  158. {
  159. if (sys_config('elec_invoice', 1) != 1) {
  160. return app('json')->fail('电子发票功能未开启,请在一号通中开启并且在商城后台一号通配置中开启');
  161. }
  162. $info = $this->services->getOne(['id' => $id]);
  163. $unique = app()->make(StoreOrderServices::class)->value(['id' => $info['order_id']], 'order_id');
  164. $cartInfo = app()->make(StoreOrderCartInfoServices::class)->getOrderCartInfo($info['order_id']);
  165. $goods = [];
  166. foreach ($cartInfo as $item) {
  167. $goods[] = [
  168. 'store_name' => $item['cart_info']['productInfo']['store_name'],
  169. 'unit_price' => bcadd($item['cart_info']['truePrice'], $item['cart_info']['postage_price'], 2),
  170. 'num' => $item['cart_info']['cart_num']
  171. ];
  172. }
  173. $data = [];
  174. $data['unique'] = $unique;
  175. $data['goods'] = $goods;
  176. $data['account_name'] = $info['name'];
  177. $data['email'] = $info['email'];
  178. if ($info['header_type'] == 1) {
  179. $data['invoice_type'] = 82;
  180. $data['is_enterprise'] = 0;
  181. } else {
  182. $data['invoice_type'] = $info['type'] == 1 ? 82 : 81;
  183. $data['tax_id'] = $info['duty_number'];
  184. $data['is_enterprise'] = 1;
  185. }
  186. $invoice = app()->make(ServeServices::class)->invoice();
  187. return app('json')->success($invoice->invoiceIssuanceUrl($data));
  188. }
  189. /**
  190. * 保存发票信息
  191. * @param $id
  192. * @return \think\Response
  193. * @author wuhaotian
  194. * @email 442384644@qq.com
  195. * @date 2024/5/14
  196. */
  197. public function saveInvoiceInfo($id)
  198. {
  199. $data = $this->request->postMore([
  200. ['invoice_num', ''],
  201. ['invoice_type', ''],
  202. ['invoice_serial_number', ''],
  203. ]);
  204. $info = $this->services->getOne(['id' => $id]);
  205. $data['unique_num'] = app()->make(StoreOrderServices::class)->value(['id' => $info['order_id']], 'order_id');
  206. $data['is_invoice'] = 1;
  207. $data['invoice_time'] = time();
  208. $this->services->update($id, $data);
  209. return app('json')->success('保存成功');
  210. }
  211. /**
  212. * 查看发票详情
  213. * @param $id
  214. * @return \think\Response
  215. * @author wuhaotian
  216. * @email 442384644@qq.com
  217. * @date 2024/5/14
  218. */
  219. public function invoiceInfo($id)
  220. {
  221. $info = $this->services->getOne(['id' => $id]);
  222. $invoice = app()->make(ServeServices::class)->invoice();
  223. return app('json')->success($invoice->invoiceInfo($info['invoice_num']));
  224. }
  225. /**
  226. * 下载发票
  227. * @param $id
  228. * @return \think\Response
  229. * @author wuhaotian
  230. * @email 442384644@qq.com
  231. * @date 2024/5/14
  232. */
  233. public function downInvoice($id)
  234. {
  235. $info = $this->services->getOne(['id' => $id]);
  236. $invoice = app()->make(ServeServices::class)->invoice();
  237. return app('json')->success($invoice->downloadInvoice($info['invoice_num']));
  238. }
  239. /**
  240. * 电子发票分类
  241. * @return \think\Response
  242. * @author wuhaotian
  243. * @email 442384644@qq.com
  244. * @date 2024/5/15
  245. */
  246. public function invoiceCategory()
  247. {
  248. $where = $this->request->getMore([
  249. ['name', ''],
  250. ['page', 1],
  251. ['limit', 100],
  252. ]);
  253. $invoice = app()->make(ServeServices::class)->invoice();
  254. return app('json')->success($invoice->category($where));
  255. }
  256. /**
  257. * 开具发票
  258. * @param $id
  259. * @return \think\Response
  260. * @throws \ReflectionException
  261. * @throws \think\db\exception\DataNotFoundException
  262. * @throws \think\db\exception\DbException
  263. * @throws \think\db\exception\ModelNotFoundException
  264. * @author wuhaotian
  265. * @email 442384644@qq.com
  266. * @date 2024/5/15
  267. */
  268. public function invoiceIssuance($id)
  269. {
  270. $this->services->invoiceIssuance($id);
  271. return app('json')->success('开票成功');
  272. }
  273. /**
  274. * 负数发票开具
  275. * @param $id
  276. * @return \think\Response
  277. * @throws \think\db\exception\DataNotFoundException
  278. * @throws \think\db\exception\DbException
  279. * @throws \think\db\exception\ModelNotFoundException
  280. * @author wuhaotian
  281. * @email 442384644@qq.com
  282. * @date 2024/5/16
  283. */
  284. public function redInvoiceIssuance($id)
  285. {
  286. $this->services->redInvoiceIssuance($id);
  287. return app('json')->success('开具负数发票成功');
  288. }
  289. }