vr_bill.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. /**
  3. * 虚拟订单结算管理
  4. *
  5. *
  6. *
  7. ***/
  8. defined('InShopNC') or exit('Access Invalid!');
  9. class vr_billControl extends SystemControl{
  10. /**
  11. * 每次导出订单数量
  12. * @var int
  13. */
  14. const EXPORT_SIZE = 1000;
  15. private $links = array(
  16. array('url'=>'act=vr_bill&op=index','lang'=>'nc_manage'),
  17. );
  18. public function __construct(){
  19. parent::__construct();
  20. }
  21. /**
  22. * 所有月份销量账单
  23. *
  24. */
  25. public function indexOp(){
  26. $condition = array();
  27. if (preg_match('/^\d{4}$/',$_GET['query_year'],$match)) {
  28. $condition['os_year'] = $_GET['query_year'];
  29. }
  30. $model_bill = Model('vr_bill');
  31. $bill_list = $model_bill->getOrderStatisList($condition,'*',12,'os_month desc');
  32. Tpl::output('bill_list',$bill_list);
  33. Tpl::output('show_page',$model_bill->showpage());
  34. //输出子菜单
  35. Tpl::output('top_link',$this->sublink($this->links,'index'));
  36. Tpl::showpage('vr_bill_order_statis.index');
  37. }
  38. /**
  39. * 某月所有店铺销量账单
  40. *
  41. */
  42. public function show_statisOp(){
  43. if (!empty($_GET['os_month']) && !preg_match('/^20\d{4}$/',$_GET['os_month'],$match)) {
  44. showMessage('参数错误','','html','error');
  45. }
  46. $model_bill = Model('vr_bill');
  47. $condtion = array();
  48. if (!empty($_GET['os_month'])) {
  49. $condition['os_month'] = intval($_GET['os_month']);
  50. }
  51. if (is_numeric($_GET['bill_state'])) {
  52. $condition['ob_state'] = intval($_GET['bill_state']);
  53. }
  54. if (preg_match('/^\d{1,8}$/',$_GET['query_store'])) {
  55. $condition['ob_store_id'] = $_GET['query_store'];
  56. }elseif ($_GET['query_store'] != ''){
  57. $condition['ob_store_name'] = $_GET['query_store'];
  58. }
  59. $bill_list = $model_bill->getOrderBillList($condition,'*',30,'ob_no desc');
  60. Tpl::output('bill_list',$bill_list);
  61. Tpl::output('show_page',$model_bill->showpage());
  62. //输出子菜单
  63. Tpl::output('top_link',$this->sublink($this->links,'index'));
  64. Tpl::showpage('vr_bill_order_statis.show');
  65. }
  66. /**
  67. * 某店铺某月订单列表
  68. *
  69. */
  70. public function show_billOp(){
  71. if (!preg_match('/^20\d{5,12}$/',$_GET['ob_no'],$match)) {
  72. showMessage('参数错误','','html','error');
  73. }
  74. $model_bill = Model('vr_bill');
  75. $bill_info = $model_bill->getOrderBillInfo(array('ob_no'=>$_GET['ob_no']));
  76. if (!$bill_info){
  77. showMessage('参数错误','','html','error');
  78. }
  79. $model_order = Model('vr_order');
  80. $condition = array();
  81. $condition['store_id'] = $bill_info['ob_store_id'];
  82. if ($_GET['query_type'] == 'timeout') {
  83. //计算未使用已过期不可退兑换码列表
  84. $condition['vr_state'] = 0;
  85. $condition['vr_invalid_refund'] = 0;
  86. $condition['vr_indate'] = array('between',"{$bill_info['ob_start_date']},{$bill_info['ob_end_date']}");
  87. } else {
  88. //计算已使用兑换码列表
  89. $condition['vr_state'] = 1;
  90. $condition['vr_usetime'] = array('between',"{$bill_info['ob_start_date']},{$bill_info['ob_end_date']}");
  91. }
  92. $code_list = $model_order->getCodeList($condition, '*', 20, 'rec_id desc');
  93. //然后取订单编号
  94. $order_id_array = array();
  95. if (is_array($code_list)) {
  96. foreach ($code_list as $code_info) {
  97. $order_id_array[] = $code_info['order_id'];
  98. }
  99. }
  100. $condition = array();
  101. $condition['order_id'] = array('in',$order_id_array);
  102. $order_list = $model_order->getOrderList($condition);
  103. $order_new_list = array();
  104. if (!empty($order_list)) {
  105. foreach ($order_list as $v) {
  106. $order_new_list[$v['order_id']]['order_sn'] = $v['order_sn'];
  107. $order_new_list[$v['order_id']]['buyer_name'] = $v['buyer_name'];
  108. $order_new_list[$v['order_id']]['store_name'] = $v['store_name'];
  109. $order_new_list[$v['order_id']]['store_id'] = $v['store_id'];
  110. }
  111. }
  112. Tpl::output('order_list',$order_new_list);
  113. Tpl::output('code_list',$code_list);
  114. Tpl::output('show_page',$model_order->showpage());
  115. $sub_tpl_name = 'vr_bill.show.code_list';
  116. Tpl::output('tpl_name',$sub_tpl_name);
  117. Tpl::output('bill_info',$bill_info);
  118. Tpl::showpage('vr_bill_order_bill.show');
  119. }
  120. public function bill_checkOp(){
  121. if (!preg_match('/^20\d{5,12}$/',$_GET['ob_no'])) {
  122. showMessage('参数错误','','html','error');
  123. }
  124. $model_bill = Model('vr_bill');
  125. $condition = array();
  126. $condition['ob_no'] = $_GET['ob_no'];
  127. $condition['ob_state'] = BILL_STATE_STORE_COFIRM;
  128. $update = $model_bill->editOrderBill(array('ob_state'=>BILL_STATE_SYSTEM_CHECK),$condition);
  129. if ($update){
  130. $this->log('审核账单,账单号:'.$_GET['ob_no'],1);
  131. showMessage('审核成功,账单进入付款环节');
  132. }else{
  133. $this->log('审核账单,账单号:'.$_GET['ob_no'],0);
  134. showMessage('审核失败','','html','error');
  135. }
  136. }
  137. /**
  138. * 账单付款
  139. *
  140. */
  141. public function bill_payOp(){
  142. if (!preg_match('/^20\d{5,12}$/',$_GET['ob_no'])) {
  143. showMessage('参数错误','','html','error');
  144. }
  145. $model_bill = Model('vr_bill');
  146. $condition = array();
  147. $condition['ob_no'] = $_GET['ob_no'];
  148. $condition['ob_state'] = BILL_STATE_SYSTEM_CHECK;
  149. $bill_info = $model_bill->getOrderBillInfo($condition);
  150. if (!$bill_info){
  151. showMessage('参数错误','','html','error');
  152. }
  153. if (chksubmit()){
  154. if (!preg_match('/^20\d{2}-\d{2}-\d{2}$/',$_POST['pay_date'])) {
  155. showMessage('参数错误','','html','error');
  156. }
  157. $input = array();
  158. $input['ob_pay_content'] = $_POST['pay_content'];
  159. $input['ob_pay_date'] = strtotime($_POST['pay_date']);
  160. $input['ob_state'] = BILL_STATE_SUCCESS;
  161. $update = $model_bill->editOrderBill($input,$condition);
  162. if ($update){
  163. // 发送店铺消息
  164. $param = array();
  165. $param['code'] = 'store_bill_gathering';
  166. $param['store_id'] = $bill_info['ob_store_id'];
  167. $param['param'] = array(
  168. 'bill_no' => $bill_info['ob_no']
  169. );
  170. QueueClient::push('sendStoreMsg', $param);
  171. $this->log('账单付款,账单号:'.$_GET['ob_no'],1);
  172. showMessage('保存成功','index.php?act=vr_bill&op=show_statis&os_month='.$bill_info['os_month']);
  173. }else{
  174. $this->log('账单付款,账单号:'.$_GET['ob_no'],1);
  175. showMessage('保存失败','','html','error');
  176. }
  177. }else{
  178. Tpl::showpage('vr_bill.pay');
  179. }
  180. }
  181. /**
  182. * 打印结算单
  183. *
  184. */
  185. public function bill_printOp(){
  186. if (!preg_match('/^20\d{5,12}$/',$_GET['ob_no'])) {
  187. showMessage('参数错误','','html','error');
  188. }
  189. $model_bill = Model('vr_bill');
  190. $condition = array();
  191. $condition['ob_no'] = $_GET['ob_no'];
  192. $condition['ob_state'] = BILL_STATE_SUCCESS;
  193. $bill_info = $model_bill->getOrderBillInfo($condition);
  194. if (!$bill_info){
  195. showMessage('参数错误','','html','error');
  196. }
  197. Tpl::output('bill_info',$bill_info);
  198. Tpl::showpage('vr_bill.print','null_layout');
  199. }
  200. /**
  201. * 导出平台月出账单表
  202. *
  203. */
  204. public function export_billOp(){
  205. if (!empty($_GET['os_month']) && !preg_match('/^20\d{4}$/',$_GET['os_month'],$match)) {
  206. showMessage('参数错误','','html','error');
  207. }
  208. $model_bill = Model('vr_bill');
  209. $condition = array();
  210. if (!empty($_GET['os_month'])) {
  211. $condition['os_month'] = intval($_GET['os_month']);
  212. }
  213. if (is_numeric($_GET['bill_state'])) {
  214. $condition['ob_state'] = intval($_GET['bill_state']);
  215. }
  216. if (preg_match('/^\d{1,8}$/',$_GET['query_store'])) {
  217. $condition['ob_store_id'] = $_GET['query_store'];
  218. }elseif ($_GET['query_store'] != ''){
  219. $condition['ob_store_name'] = $_GET['query_store'];
  220. }
  221. if (!is_numeric($_GET['curpage'])){
  222. $count = $model_bill->getOrderBillCount($condition);
  223. $array = array();
  224. if ($count > self::EXPORT_SIZE ){
  225. //显示下载链接
  226. $page = ceil($count/self::EXPORT_SIZE);
  227. for ($i=1;$i<=$page;$i++){
  228. $limit1 = ($i-1)*self::EXPORT_SIZE + 1;
  229. $limit2 = $i*self::EXPORT_SIZE > $count ? $count : $i*self::EXPORT_SIZE;
  230. $array[$i] = $limit1.' ~ '.$limit2 ;
  231. }
  232. Tpl::output('list',$array);
  233. Tpl::output('murl','index.php?act=vr_bill&op=index');
  234. Tpl::showpage('export.excel');
  235. exit();
  236. }else{
  237. //如果数量小,直接下载
  238. $data = $model_bill->getOrderBillList($condition,'*','','ob_no desc',self::EXPORT_SIZE);
  239. }
  240. }else{
  241. //下载
  242. $limit1 = ($_GET['curpage']-1) * self::EXPORT_SIZE;
  243. $limit2 = self::EXPORT_SIZE;
  244. $data = $model_bill->getOrderBillList($condition,'*','','ob_no desc',"{$limit1},{$limit2}");
  245. }
  246. $export_data = array();
  247. $export_data[0] = array('账单编号','开始日期','结束日期','消费金额','佣金金额','本期应结','出账日期','账单状态','店铺','店铺ID');
  248. $ob_order_totals = 0;
  249. $ob_commis_totals = 0;
  250. $ob_result_totals = 0;
  251. foreach ($data as $k => $v) {
  252. $export_data[$k+1][] = $v['ob_no'];
  253. $export_data[$k+1][] = date('Y-m-d',$v['ob_start_date']);
  254. $export_data[$k+1][] = date('Y-m-d',$v['ob_end_date']);
  255. $ob_order_totals += $export_data[$k+1][] = $v['ob_order_totals'];
  256. $ob_commis_totals += $export_data[$k+1][] = $v['ob_commis_totals'];
  257. $ob_result_totals += $export_data[$k+1][] = $v['ob_result_totals'];
  258. $export_data[$k+1][] = date('Y-m-d',$v['ob_create_date']);
  259. $export_data[$k+1][] = billState($v['ob_state']);
  260. $export_data[$k+1][] = $v['ob_store_name'];
  261. $export_data[$k+1][] = $v['ob_store_id'];
  262. }
  263. $count = count($export_data);
  264. $export_data[$count][] = '';
  265. $export_data[$count][] = '';
  266. $export_data[$count][] = '合计';
  267. $export_data[$count][] = $ob_order_totals;
  268. $export_data[$count][] = $ob_commis_totals;
  269. $export_data[$count][] = $ob_result_totals;
  270. $csv = new Csv();
  271. $export_data = $csv->charset($export_data,CHARSET,'gbk');
  272. $csv->filename = $csv->charset('账单-',CHARSET).$_GET['os_month'];
  273. $csv->export($export_data);
  274. }
  275. /**
  276. * 导出兑换码明细CSV
  277. *
  278. */
  279. public function export_orderOp(){
  280. if (!preg_match('/^20\d{5,12}$/',$_GET['ob_no'],$match)) {
  281. showMessage('参数错误','','html','error');
  282. }
  283. $model_bill = Model('vr_bill');
  284. $bill_info = $model_bill->getOrderBillInfo(array('ob_no'=>$_GET['ob_no']));
  285. if (!$bill_info){
  286. showMessage('参数错误','','html','error');
  287. }
  288. $model_order = Model('vr_order');
  289. $condition = array();
  290. $condition['store_id'] = $bill_info['ob_store_id'];
  291. if ($_GET['query_type'] == 'timeout') {
  292. //计算未使用已过期不可退兑换码列表
  293. $condition['vr_state'] = 0;
  294. $condition['vr_invalid_refund'] = 0;
  295. $condition['vr_indate'] = array('between',"{$bill_info['ob_start_date']},{$bill_info['ob_end_date']}");
  296. } else {
  297. //计算已使用兑换码列表
  298. $condition['vr_state'] = 1;
  299. $condition['vr_usetime'] = array('between',"{$bill_info['ob_start_date']},{$bill_info['ob_end_date']}");
  300. }
  301. if (!is_numeric($_GET['curpage'])){
  302. $count = $model_order->getOrderCodeCount($condition);
  303. $array = array();
  304. if ($count > self::EXPORT_SIZE ){
  305. //显示下载链接
  306. $page = ceil($count/self::EXPORT_SIZE);
  307. for ($i=1;$i<=$page;$i++){
  308. $limit1 = ($i-1)*self::EXPORT_SIZE + 1;
  309. $limit2 = $i*self::EXPORT_SIZE > $count ? $count : $i*self::EXPORT_SIZE;
  310. $array[$i] = $limit1.' ~ '.$limit2 ;
  311. }
  312. Tpl::output('list',$array);
  313. Tpl::output('murl','index.php?act=vr_bill&op=show_bill&ob_no='.$_GET['ob_no']);
  314. Tpl::showpage('export.excel');
  315. exit();
  316. }else{
  317. //如果数量小,直接下载
  318. $data = $model_order->getCodeList($condition,'*','','rec_id desc',self::EXPORT_SIZE);
  319. }
  320. }else{
  321. //下载
  322. $limit1 = ($_GET['curpage']-1) * self::EXPORT_SIZE;
  323. $limit2 = self::EXPORT_SIZE;
  324. $data = $model_order->getCodeList($condition,'*','','rec_id desc',"{$limit1},{$limit2}");
  325. }
  326. //然后取订单编号
  327. $order_id_array = array();
  328. if (is_array($data)) {
  329. foreach ($data as $code_info) {
  330. $order_id_array[] = $code_info['order_id'];
  331. }
  332. }
  333. $condition = array();
  334. $condition['order_id'] = array('in',$order_id_array);
  335. $order_list = $model_order->getOrderList($condition);
  336. $order_new_list = array();
  337. if (!empty($order_list)) {
  338. foreach ($order_list as $v) {
  339. $order_new_list[$v['order_id']]['order_sn'] = $v['order_sn'];
  340. $order_new_list[$v['order_id']]['buyer_name'] = $v['buyer_name'];
  341. $order_new_list[$v['order_id']]['buyer_id'] = $v['buyer_id'];
  342. $order_new_list[$v['order_id']]['store_name'] = $v['store_name'];
  343. $order_new_list[$v['order_id']]['store_id'] = $v['store_id'];
  344. $order_new_list[$v['order_id']]['goods_name'] = $v['goods_name'];
  345. }
  346. }
  347. $export_data = array();
  348. $export_data[0] = array('兑换码','消费时间','订单号','消费金额','佣金金额','商家','商家编号','买家','买家编号','商品');
  349. if ($_GET['query_type'] == 'timeout') {
  350. $export_data[0][1] = '过期时间';
  351. }
  352. $pay_totals = 0;
  353. $commis_totals = 0;
  354. $k = 0;
  355. foreach ($data as $v) {
  356. //该订单算佣金
  357. $export_data[$k+1][] = $v['vr_code'];
  358. if ($_GET['query_type'] == 'timeout') {
  359. $export_data[$k+1][] = date('Y-m-d H:i:s',$v['vr_indate']);
  360. } else {
  361. $export_data[$k+1][] = date('Y-m-d H:i:s',$v['vr_usetime']);
  362. }
  363. $export_data[$k+1][] = 'NC'.$order_new_list[$v['order_id']]['order_sn'];
  364. $pay_totals += $export_data[$k+1][] = floatval($v['pay_price']);
  365. $commis_totals += $export_data[$k+1][] = floatval($v['pay_price'] * $v['commis_rate']/100);
  366. $export_data[$k+1][] = $order_new_list[$v['order_id']]['store_name'];
  367. $export_data[$k+1][] = $order_new_list[$v['order_id']]['store_id'];
  368. $export_data[$k+1][] = $order_new_list[$v['order_id']]['buyer_name'];
  369. $export_data[$k+1][] = $order_new_list[$v['order_id']]['buyer_id'];
  370. $export_data[$k+1][] = $order_new_list[$v['order_id']]['goods_name'];
  371. $k++;
  372. }
  373. $count = count($export_data);
  374. $export_data[$count][] = '合计';
  375. $export_data[$count][] = '';
  376. $export_data[$count][] = '';
  377. $export_data[$count][] = $pay_totals;
  378. $export_data[$count][] = $commis_totals;
  379. $csv = new Csv();
  380. $export_data = $csv->charset($export_data,CHARSET,'gbk');
  381. $file_name = $_GET['query_type'] == 'timeout' ? '过期兑换码列表' : '已消费兑换码列表';
  382. $csv->filename = $csv->charset($file_name.'-',CHARSET).$_GET['ob_no'];
  383. $csv->export($export_data);
  384. }
  385. }