204 lines
5.7 KiB
PHP
204 lines
5.7 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace app\common\model\user\invoice;
|
||
|
|
||
|
use app\common\model\BaseModel;
|
||
|
use app\common\model\school\activity\order\Order;
|
||
|
use think\Model;
|
||
|
|
||
|
|
||
|
class Apply extends BaseModel
|
||
|
{
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
// 表名
|
||
|
protected $name = 'user_invoice_apply';
|
||
|
|
||
|
// 自动写入时间戳字段
|
||
|
protected $autoWriteTimestamp = 'integer';
|
||
|
|
||
|
// 定义时间戳字段名
|
||
|
protected $createTime = 'createtime';
|
||
|
protected $updateTime = 'updatetime';
|
||
|
protected $deleteTime = false;
|
||
|
|
||
|
// 追加属性
|
||
|
protected $append = [
|
||
|
'status_text',
|
||
|
'apply_type_text',
|
||
|
'head_type_text',
|
||
|
'invoice_type_text',
|
||
|
'invoicingtime_text'
|
||
|
];
|
||
|
|
||
|
|
||
|
|
||
|
public function getStatusList()
|
||
|
{
|
||
|
return ['1' => __('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);
|
||
|
}
|
||
|
|
||
|
|
||
|
/** 申请发票
|
||
|
* @param $order_no 申请发票的订单号
|
||
|
* @param
|
||
|
* @param $trans
|
||
|
* @return true
|
||
|
* @throws \Exception
|
||
|
*/
|
||
|
public function add($order_no,$params,$check=true,$trans=false){
|
||
|
|
||
|
if (empty($params)) {
|
||
|
throw new \Exception(__('Parameter %s can not be empty', ''));
|
||
|
}
|
||
|
|
||
|
$order = Order::getHaveInvoiceApplyOrder($order_no);
|
||
|
|
||
|
|
||
|
$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);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
//判断逻辑
|
||
|
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);
|
||
|
// }
|
||
|
|
||
|
$result = $this->allowField(true)->save($params);
|
||
|
|
||
|
if($trans){
|
||
|
self::commitTrans();
|
||
|
}
|
||
|
}catch (\Exception $e){
|
||
|
if($trans){
|
||
|
self::rollbackTrans();
|
||
|
}
|
||
|
throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
|
||
|
}
|
||
|
return $this;
|
||
|
}
|
||
|
}
|