invoice = D('RetailInvoice'); $this->invoiceApply = D('RetailInvoiceApply'); $this->billOrder = D('RetailBillOrder'); } public function create() { $post = I('post.'); $post['merchant_id'] = self::$merchant_id; $post['store_id'] = self::$store_id; $post['uid'] = self::$uid; $result = $this->invoice->createOrder($post); if($result) { $this->invoice->add(); $this->ajaxReturn([ 'status' => 1, 'msg' => '添加成功', 'data' => $this->invoice->getLastInsID() ]); } else { $this->ajaxReturn([ 'status' => 0, 'msg' => $this->invoice->getError() ]); } } public function detail() { $id = I('post.id', 0, 'intval'); $map = [ 'merchant_id' => self::$merchant_id, 'store_id' => self::$store_id, 'uid' => self::$uid, 'id' => $id ]; $result = $this->invoice->getFind($map); if (!$result) { $this->ajaxReturn([ 'status' => 0, 'msg' => '发票不存在' ]); } $this->ajaxReturn([ 'status' => 1, 'msg' => '发票信息', 'data' => $result ]); } public function update() { $data = I('post.'); $map = [ 'merchant_id' => self::$merchant_id, 'store_id' => self::$store_id, 'uid' => self::$uid, 'id' => is_numeric($data['id']) ? $data['id'] : 0 ]; unset($data['id']); $result = $this->invoice->updateOrder($map, $data); if($result !== false) { $this->ajaxReturn([ 'status' => 1, 'msg' => '修改成功' ]); } else { $this->ajaxReturn([ 'status' => 0, 'msg' => '修改失败' ]); } } public function delete() { $id = I('post.id/d', 0); $map = [ 'merchant_id' => self::$merchant_id, 'store_id' => self::$store_id, 'uid' => self::$uid, 'id' => $id ]; $result = $this->invoice->deleteOrder($map); if($result) { $this->ajaxReturn([ 'status' => 1, 'msg' => '删除成功' ]); } else { $this->ajaxReturn([ 'status' => 0, 'msg' => '删除失败' ]); } } public function lists() { $map = [ 'merchant_id' => self::$merchant_id, 'store_id' => self::$store_id, 'uid' => self::$uid ]; $result = $this->invoice->getSelect($map, '*', 'is_default desc, id desc', I('post.page/d', 1)); $this->ajaxReturn([ 'status' => 1, 'msg' => '获取成功', 'data' => $result, ]); } public function setDefault() { $id = I('post.id/d', 0); $map = [ 'merchant_id' => self::$merchant_id, 'store_id' => self::$store_id, 'uid' => self::$uid, ]; $result = $this->invoice->getFind(array_merge([ $map, 'id' => $id, 'is_default' => 0 ])); if($result) { $this->invoice->updateOrder($map, ['is_default' => 0]); $this->invoice->updateOrder(['id' => $id], ['is_default' => 1]); $this->ajaxReturn([ 'status' => 1, 'msg' => '设置成功' ]); } $this->ajaxReturn([ 'status' => 0, 'msg' => '设置失败' ]); } public function order() { $map = [ 'merchant_id' => self::$merchant_id, 'store_id' => self::$store_id, 'uid' => self::$uid, ]; $id = I('post.id/d', null); $applys = $this->invoiceApply->field('order_ids')->where(is_numeric($id) ? array_merge($map, [ 'id' => $id ]) : $map)->select(); $order_ids = []; foreach($applys as $v) { $values = explode(',', $v['order_ids']); foreach($values as $value) { if (!in_array($value, $order_ids)) { $order_ids[] = $value; } } } if (count($order_ids)) { $map['id'] = is_numeric($id) ? ['in', $order_ids] : ['not in', $order_ids]; } $result = $this->billOrder->getSelect(array_merge($map, [ 'pay_state' => 1 ]), 'id, store_id, all_money, add_time', 'id desc', I('post.page/d', 1)); $this->ajaxReturn([ 'status' => 1, 'msg' => '可开票订单', 'data' => $this->getGoodsInfo($result) ]); } public function apply() { $post = I('post.'); $post['merchant_id'] = self::$merchant_id; $post['store_id'] = self::$store_id; $post['uid'] = self::$uid; $apply = $this->invoiceApply->where([ 'merchant_id' => self::$merchant_id, 'store_id' => self::$store_id, 'uid' => self::$uid ])->where(array_map(function ($id) { return [ 'FIND_IN_SET("' . $id . '", order_ids)', '_logic' => 'OR', ]; }, explode(',', $post['order_ids'])))->find(); if ($apply) { $this->ajaxReturn([ 'status' => 0, 'msg' => '请勿重复申请开具发票' ]); } $result = $this->invoiceApply->createOrder(array_merge($post, [ 'status' => 1 ])); if($result) { $this->invoiceApply->add(); $this->ajaxReturn([ 'status' => 1, 'msg' => '申请成功', 'data' => [ 'id' => $this->invoiceApply->getLastInsID() ] ]); } else { $this->ajaxReturn([ 'status' => 0, 'msg' => $this->invoiceApply->getError() ]); } } public function applyDetail() { $result = $this->invoiceApply->getFind([ 'merchant_id' => self::$merchant_id, 'store_id' => self::$store_id, 'uid' => self::$uid, 'id' => I('post.id/d', 0) ]); if ($result) { $result['apply_time'] = date('Y.m.d H:i', $result['apply_time']); $result['invoice'] = $this->invoice->getFind(['id' => $result['invoice_id']]); $this->ajaxReturn([ 'status' => 1, 'msg' => '获取成功', 'data' => $result ]); } else { $this->ajaxReturn([ 'status' => 0, 'msg' => '获取失败' ]); } } public function applyRecord() { $result = $this->invoiceApply->getSelect([ 'merchant_id' => self::$merchant_id, 'store_id' => self::$store_id, 'uid' => self::$uid ], '*', 'id desc', I('post.page/d', 1)); $order_ids = []; foreach($result as $v) { $values = explode(',', $v['order_ids']); foreach($values as $value) { if (!in_array($value, $order_ids)) { $order_ids[] = $value; } } } if (count($order_ids)) { $billOrder = $this->billOrder->getSelect([ 'id' => ['in', $order_ids] ], 'id, store_id, all_money, add_time', 'id desc', I('post.page/d', 1)); if ($billOrder) { $billOrder = $this->getGoodsInfo($billOrder); } } foreach ($result as &$v) { $ids = explode(',', $v['order_ids']); foreach ($billOrder as $bill) { if (in_array($bill['id'], $ids)) { foreach($bill['goods_info'] as $item) { $v['goods_info'][] = $item; } } } $v['apply_time'] = date('Y-m-d H:i', $v['apply_time']); $v['invoice'] = $this->invoice->getFind(['id' => $v['invoice_id']]); $v['status_text'] = $v['status'] == 1 ? '开票中' : ($v['status'] == 2 ? '已开票' : '申请拒绝'); $v['count'] = count($v['goods_info']); } $this->ajaxReturn([ 'status' => 1, 'msg' => '获取成功', 'data' => $result, ]); } private function getGoodsInfo($result) { $store = D('RetailStore'); $stores = []; $sell = D('RetailSell'); $image = new \Common\Business\Image(); foreach($result as &$v) { if ($stores[$v['store_id']]) { $v['store_name'] = $stores[$v['store_id']]['store_name']; } else { $store = $store->getFind(array('id' => $v['store_id']), 'storetag as store_name'); $v['store_name'] = $store['store_name']; $stores[$v['store_id']] = $store; } $v['date'] = date('Y-m-d H:i', $v['add_time']); $goods_list = $sell->getSelect(['bill_id' => $v['id']], [ 'goods_id', 'goods_img_id', 'goods_name', 'price', 'sell_number', 'all_money' ]); foreach ($goods_list as &$v2) { $v2['goods_img'] = $image->getGoodsImageUrl(['img_id' => $v['goods_img_id'], 'store_id' => $v['store_id']]); } $v['goods_info'] = $goods_list; } return $result; } }