// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\store\model; use app\common\enum\order\OrderStatus as OrderStatusEnum; use app\common\model\Receipt as ReceiptModel; /** * 商家记录表模型 * Class Store * @package app\store\model */ class Receipt extends ReceiptModel { public function getList($conditions){ $model = $this->getQuery($conditions); return $model->alias('receipt') ->leftJoin('order','receipt.order_id=order.order_id') ->leftJoin("member_order",'receipt.order_id=member_order.id') ->field('receipt.*,order.pay_status,order.order_status,order.finished_refund') ->order('receipt_id','desc')->paginate(15) ->each(function($v) { // 是否可开票 $v['can_invoice'] = $this->checkCanInvoice($v,$v['ftype']); if ($v['order_status'] != OrderStatusEnum::CLOSE) { // 未关闭的订单 $v['finished_refund'] = 0; } //会员卡不可以退款 if($v['ftype']==2){ $v['finished_refund'] = 0; } }); } /** * 验证是否可开票 会员卡付款后可开票 * ftype=1 商品 * @param $order * @return bool */ private function checkCanInvoice($order,$ftype=0) { //会员卡付款后可开票,也即是 if($ftype==2){ return 1; } $orderGoods = OrderGoods::with(['refund'])->where('order_id', $order['order_id'])->select()->toArray(); $refundClose = false; // 是否 订单所有商品售后入口关闭; 订单商品表 refund_time 中最大的时间 < 当前时间 $oneNotRefund = false; // 订单中至少有一款商品未退款成功 订单表 order_status 不等于40-已关闭 $refundTimeMax = 0; if(count($orderGoods)){ $refundTimeMax = max(array_column($orderGoods, 'refund_time')); } //$refundTimeMax = max(array_column($orderGoods, 'refund_time')); if ($refundTimeMax > 0 && time() > $refundTimeMax) { $refundClose = true; } if ($order['order_status'] != OrderStatusEnum::CLOSE) { $oneNotRefund = true; } if ( $refundClose // 订单所有商品售后入口关闭 && OrderRefund::where('status', 0)->where('order_id', $order['order_id'])->count() == 0 // 不存在 进行中的售后单 && $oneNotRefund // 订单中至少有一款商品未退款成功。(所有商品都退款完成订单会关闭,所以不建议开票) ) { return 1; } return 0; } private function getQuery($conditions) { $model = new self(); $from = 1633017600 ;$to = time(); if ($conditions and count($conditions)){ foreach ($conditions as $key=>$c){ if ($key == "headup" && trim($c,' ')){ $model = $model->whereLike('receipt.'.$key,'%'.$c.'%'); } if ($key == 'tax_no' && trim($c,' ')){ $model = $model->whereLike('receipt.'.$key,'%'.$c.'%'); } if ($key == 'status' && $c != '-1'){ $model = $model->where('receipt.'.$key,$c); } if ($key =='betweenTime'){ if (isset($c[0]) && $c[0] && isset($c[1]) && $c[1]){ $times = between_time($c); $from = $times['start_time']; $to = $times['end_time'] + 86400; $model = $model->whereBetweenTime('receipt.create_time',$from,$to); } } if ($key == 'receipt_ids' && !empty($c)) { $model = $model->whereIn('receipt.receipt_id', $c); } if ($key == 'order_status'){ if ($c == 40){ $model = $model->where('order.order_status',$c); }else{ $model = $model->where('order.order_status','<>',40); } } if ($key == 'order_no' && trim($c,' ')){ $model = $model->whereLike('receipt.'.$key,'%'.$c.'%'); } // 是否已全部退款成功 if ($key == 'finished_refund' && $c != '-1'){ if ($c == 1) { $model = $model->where('order.'.$key,$c)->where('order.order_status', '=', OrderStatusEnum::CLOSE); } else { $model = $model->where('order.order_status', '<>', OrderStatusEnum::CLOSE) ->whereOr('order.order_status', '=', null); } } //邮箱搜索 if ($key == "email" && trim($c,' ')){ $model = $model->whereLike('receipt.'.$key,'%'.$c.'%'); } } } return $model; } public static function updateStatus($id,$receipt_no){ self::where('receipt_id',$id)->update(['status'=>20,'receipt_no'=>$receipt_no]); return true; } public static function getDetail($id){ return self::find($id); } /** * 获取待开发票数量 * @return int */ public function getWaitReceiptTotal() { $orders = $this->where('status', 10)->select(); $total = 0; foreach ($orders as $order){ $can_invoice = $this->checkCanInvoice($order,$order['ftype']); if($can_invoice) $total += 1; } return $total; } /** * 导出发票申请记录 */ public function export(array $param) { $data['header'] = ['订单编号', '发票抬头', '税号', '发票金额','邮箱地址', '申请时间','是否已退款', '是否可开票', '开票状态'];// '发票类型', $data['filename'] = '发票申请导出'; $data['data'] = []; $model = $this->getQuery($param); $list = $model->alias('receipt') ->leftJoin('order','receipt.order_id=order.order_id') ->field('receipt.*,order.pay_status,order.order_status,order.finished_refund') ->order('receipt_id','desc') ->select(); foreach ($list as $arr){ $new_list['order_no'] = (string)$arr['order_no']; $new_list['headup'] = $arr['headup']; $new_list['tax_no'] = $arr['tax_no']; $new_list['pay_money'] = $arr['pay_money']; //$new_list['receipt_type'] = $arr['receipt_type']==10?'增值税电子普通发票':'其它'; $new_list['email'] = $arr['email']; $new_list['create_time'] = $arr['create_time']; $new_list['finished_refund'] = $arr['finished_refund']?'是' : '否'; $new_list['can_invoice'] = $this->checkCanInvoice($arr) ? '是' : '否'; $new_list['status'] = str_replace([10,20],['待开票', '已开票'], $arr['status']); $data['data'][] = $new_list; } return $data; } }