__('Status 1'), '2' => __('Status 2')]; } public function getApplyTypeList() { return ['1' => __('Apply_type 1'), '2' => __('Apply_type 2')]; } public function getHeadTypeList() { return ['personal' => __('Head_type personal'), 'corporate' => __('Head_type corporate')]; } public function getInvoiceTypeList() { return ['ordinary' => __('Invoice_type ordinary'), 'special' => __('Invoice_type special')]; } public function getStatusTextAttr($value, $data) { $value = $value ? $value : (isset($data['status']) ? $data['status'] : ''); $list = $this->getStatusList(); return isset($list[$value]) ? $list[$value] : ''; } public function getApplyTypeTextAttr($value, $data) { $value = $value ? $value : (isset($data['apply_type']) ? $data['apply_type'] : ''); $list = $this->getApplyTypeList(); return isset($list[$value]) ? $list[$value] : ''; } public function getHeadTypeTextAttr($value, $data) { $value = $value ? $value : (isset($data['head_type']) ? $data['head_type'] : ''); $list = $this->getHeadTypeList(); return isset($list[$value]) ? $list[$value] : ''; } public function getInvoiceTypeTextAttr($value, $data) { $value = $value ? $value : (isset($data['invoice_type']) ? $data['invoice_type'] : ''); $list = $this->getInvoiceTypeList(); return isset($list[$value]) ? $list[$value] : ''; } public function getInvoicingtimeTextAttr($value, $data) { $value = $value ? $value : (isset($data['invoicingtime']) ? $data['invoicingtime'] : ''); return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } protected function setInvoicingtimeAttr($value) { return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value); } public function user() { return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0); } public function activityorders() { return $this->hasMany(Order::class, 'user_invoice_apply_id', 'id'); } /** 申请发票 * @param $order_no 申请发票的订单号 * @param * @param $trans * @throws \Exception */ public function add($order_no,$params,$oper_id = 0,$oper_type='user',$check=true,$trans=false){ if (empty($params)) { throw new \Exception(__('Parameter %s can not be empty', '')); } $rule = [ // 'user_id'=>'require', 'head_type'=>'require', 'invoice_type'=>'require', 'invoice_header' => 'require', 'invoice_reservation_phone' => 'require', 'invoice_reservation_email' => 'require', ]; $rule_msg = [ // "user_id.require"=>'提交用户必填', "head_type.require"=>'抬头类型必填', "invoice_type.require"=>'发票类型必填', 'invoice_header.require' => '发票抬头必填', 'invoice_reservation_phone.require' => '发票预留电话必填', 'invoice_reservation_email.require' => '发票预留邮箱必填', ]; self::check($params,$rule,$rule_msg); //个人无法开专用发票 if($params['head_type']=='personal' && $params['invoice_type']=='special'){ throw new \Exception('个人用户无法开专用发票'); } //企业需要填写纳税人识别号等信息 if($params['head_type']=='corporate'){ $rule = [ 'tax_id'=>'require', 'bank_deposit'=>'require', 'bank_number'=>'require', 'enterprise_address'=>'require', 'enterprise_phone'=>'require', ]; $rule_msg = [ 'tax_id.require' => '纳税人识别号必填', 'bank_deposit.require' => '开户银行必填', 'bank_number.require' => '银行账号必填', 'enterprise_address.require' => '企业地址必填', 'enterprise_phone.require' => '企业电话必填', ]; self::check($params,$rule,$rule_msg); } //是否存在逗号隔开 $order_nos = explode(',',$order_no); $order_ids = []; $orders = []; foreach ($order_nos as $orderno){ $order = Order::getHaveInvoiceApplyOrder($orderno); if($check){ //用户操作权限检测 Order::checkOptionAuth($order['id'],$oper_id ,$oper_type); } $order_ids[] = $order->id; $orders[] = $order; $params['user_id'] = $order->user_id; } if(empty($order_ids)){ throw new \Exception('订单不存在或已开过发票'); } //是单开还是连开 if(count($order_ids) >1){ $params['apply_type'] = '2'; $typeinfo = "(联合开票)"; }else{ $params['apply_type'] = '1'; $typeinfo = "(单独开票)"; } //是否已提交发票申请 if($order->invoiceaplpy) throw new \Exception('订单已提交发票申请,请勿重复提交!'); //计算合开金额 //用户支付金额 $params['price'] = Order::where( "id","in",$order_ids)->sum("sub_refundprice"); //平台手续费 $params['fee_price'] = Order::where( "id","in",$order_ids)->sum("fee_price"); if(!$params['price']) throw new \Exception('订单金额异常,该订单无可申请发票金额'); //判断逻辑 if($trans){ self::beginTrans(); } $res = true; try{ //是否采用模型验证 // if ($this->modelValidate) { // $name = str_replace("\\model\\", "\\validate\\", get_class($this)); // $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; // $this->validateFailException()->validate($validate); // } //添加发票申请 $self = new self; $result = $self->allowField(true)->save($params); //更新订单发票申请状态 Order::where( "id","in",$order_ids)->update([ 'invoice_status'=>'1', 'user_invoice_apply_id'=> $self->id, ]); //逐个发订单日志 foreach ($orders as $order){ OrderLog::log($order['id'],"活动订单发起了发票申请{$typeinfo},等待系统开票",$oper_id ,$oper_type); } //合并调用开发票事件 $data = [ 'orders' => Order::where( "id","in",$order_ids)->select(), 'invoice_apply' => $self, 'oper_id' => $oper_id, 'oper_type' => $oper_type, ]; \think\Hook::listen('activity_order_invoice_apply_after', $data); if($trans){ self::commitTrans(); } }catch (\Exception $e){ if($trans){ self::rollbackTrans(); } throw new \Exception($e->getMessage().$e->getFile().$e->getLine()); } return $self; } /** 发票开票(线下) * @param $id 申请的发票id * @param $desc 开票备注 * @param $trans * @throws \Exception */ public function examine($id,$desc,$oper_id = 0,$oper_type='user',$check=true,$trans=false) { $self = self::get($id); if(!$self) throw new \Exception('发票申请不存在'); //已开票则直接返回 if($self["status"] == '2'){ return $self; } if(! $desc) throw new \Exception('请填写开票备注'); // $params = [ 'status' => '2', 'desc' => $desc, 'invoicingtime' => time(), ]; if($check){ } if($trans){ self::beginTrans(); } $res = true; try{ //是否采用模型验证 // if ($this->modelValidate) { // $name = str_replace("\\model\\", "\\validate\\", get_class($this)); // $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; // $this->validateFailException()->validate($validate); // } //更新发票状态 $self->allowField(true)->save($params); $activityorders = $self->activityorders; foreach ($activityorders as $order){ //更新订单发票申请状态 $order["invoice_status"] = '2'; $order->save(); //发订单日志 OrderLog::log($order['id'],"活动订单已开票",$oper_id ,$oper_type); } //调用开发票事件 $data = [ 'orders' => $activityorders, 'invoice_apply' => $self, 'oper_id' => $oper_id, 'oper_type' => $oper_type, ]; //开票成功事件 \think\Hook::listen('activity_order_invoice_success_after', $data); if($trans){ self::commitTrans(); } }catch (\Exception $e){ if($trans){ self::rollbackTrans(); } throw new \Exception($e->getMessage().$e->getFile().$e->getLine()); } return $self; } }