import { Component, OnInit } from '@angular/core';
import {
FormArray,
FormBuilder,
FormGroup,
Validators,
FormControl,
} from '@angular/forms';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { TemplateService } from '../../template.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { Option } from '../../../../model/common.model';
import * as CST from '../../../../const/const-define';
import { Utils } from 'utils/utils';
import { AddTemplateRequestRes } from '../../../../model/template/template-add.model';
import {
TemplateGetRes,
Template,
} from '../../../../model/template/template-get.model';
import { ItemsQueryRes } from '../../../../model/template/items-query.model';
import { AddTemplateItemRequestRes } from '../../../../model/template/items-add.model';
import { SearchSellerRequestRes } from '../../../../model/template/subject-query.model';
import { SearchSupplierRequestRes } from '../../../../model/template/supplier-query.model';
@Component({
selector: 'app-templateedit',
templateUrl: './templateedit.component.html',
styleUrls: ['./templateedit.component.css'],
})
export class TemplateeditComponent implements OnInit {
DataForm: FormGroup;
Form1: FormGroup;
Form2: FormGroup;
Form3: FormGroup;
Form4: FormGroup;
public invoice: string;
public templateNo: string;
public PublicShow: boolean;
templateInfo: Template = {
createOpID: '',
createTime: '',
einvoiceSupplierName: '',
einvoiceSupplierNo: '',
invoiceContent: '',
invoiceContentType: '',
lastModifyTime: '',
modifyOpID: '',
paperInvoiceSupplierName: '',
paperInvoiceSupplierNo: '',
sellerName: '',
sellerNo: '',
templateName: '',
templateNo: '',
};
templateinformationEdit = false; // 模板信息编辑显示
billEdit = false; // 开票方关联编辑显示
supplierEdit = false; // 开票供应商关联编辑显示
regularEdit = false; // 开票规则显示
a = false;
templateNameError = false;
copylist = {};
// 签约主体
public tsubjectlist = [];
// 纸质发票
public pinvoicelist = [];
// 电子发票
public einvoicelist = [];
// 开票内容
public invoicelist = [];
// 零税率类型
public taxtypelist = [new Option('', '空')];
list = [];
constructor(
private fb: FormBuilder,
private routeInfo: ActivatedRoute,
private utils: Utils,
private tservice: TemplateService,
private router: Router,
private messageService: NzMessageService,
private modalService: NzModalService
) {}
ngOnInit() {
this.templateNo = this.routeInfo.snapshot.queryParams['templateNo'];
this.initConst(); // 初始化常量
if (this.templateNo) {
this.PublicShow = false;
this.queryTemplateInfo(true);
this.editCreateForm();
this.queryItems();
} else {
this.PublicShow = true;
this.createForm();
}
this.invoice = '1';
}
// 查询单个模板信息
queryTemplateInfo(e: boolean) {
const data = {
urlParams: {
ID: this.templateNo,
},
};
this.tservice.templateget(data).subscribe((res: TemplateGetRes) => {
if (res.code && res.code === CST.CODE.SUCCESS) {
this.templateInfo = Object.assign(this.templateInfo, res.data);
if (e) {
this.initInfoForm();
}
} else {
this.messageService.error(res.message || '查询发票模板失败');
// this.router.navigate(['/template/subject']);
}
});
}
// 查询发票明细
queryItems() {
const data = {
urlParams: {
templateNo: this.templateNo,
},
};
this.tservice.templateitemssearch(data).subscribe((res: ItemsQueryRes) => {
if (
res.code &&
res.code === CST.CODE.SUCCESS &&
res.data.templateItemList
) {
this.list = res.data.templateItemList;
this.list.forEach((v) => {
v.checked = false;
v.editshow = false;
v.addchecked = false;
});
} else {
// this.messageService.error(res.message || '开票明细失败');
this.list = [];
}
});
}
// 新增
addData() {
let validateSuccess = true;
for (const i in this.DataForm.controls) {
if (this.DataForm.controls.hasOwnProperty(i)) {
this.DataForm.controls[i].markAsDirty();
this.DataForm.controls[i].updateValueAndValidity();
if (this.DataForm.controls[i].status === 'INVALID') {
validateSuccess = false;
}
}
}
if (!validateSuccess) {
return false;
}
const value = this.DataForm.value;
const data = {
templateNo: value.templateNo,
templateInfo: {
einvoiceSupplierNo: value.einvoiceSupplierNo,
invoiceContent: value.invoiceContent,
invoiceContentType: value.invoiceContentType,
paperInvoiceSupplierNo: value.paperInvoiceSupplierNo,
sellerNo: value.sellerNo,
templateName: value.templateName,
},
};
this.tservice.templatesadd(data).subscribe((res: AddTemplateRequestRes) => {
if (res.code && res.code === CST.CODE.SUCCESS) {
this.messageService.success('添加成功');
this.router.navigate(['/template/invoicetemplate']);
} else {
this.messageService.error(res.message || '添加失败');
}
});
}
// 模板信息修改
templateInformationEdit() {
let validateSuccess = true;
for (const i in this.Form1.controls) {
if (this.Form1.controls.hasOwnProperty(i)) {
this.Form1.controls[i].markAsDirty();
this.Form1.controls[i].updateValueAndValidity();
if (this.Form1.controls[i].status === 'INVALID') {
validateSuccess = false;
}
}
}
if (!validateSuccess) {
return false;
}
const data = this.Form1.value;
this.update(data, '1');
}
// 开票方关联修改
saveBill() {
this.update(this.Form2.value, '2');
}
// 开票供应商关联修改
saveSupplier() {
this.update(this.Form3.value, '3');
}
// 开票规则修改
saveRegular() {
let validateSuccess = true;
for (const i in this.Form4.controls) {
if (this.Form4.controls.hasOwnProperty(i)) {
this.Form4.controls[i].markAsDirty();
this.Form4.controls[i].updateValueAndValidity();
if (this.Form4.controls[i].status === 'INVALID') {
validateSuccess = false;
}
}
}
if (!validateSuccess) {
return false;
}
this.update(this.Form4.value, '4');
}
// 修改服务
update(datas: any, num) {
const data = {
urlParams: {
ID: this.templateNo,
},
templateInfo: Object.assign({}, datas),
};
this.tservice
.templateupdate(data)
.subscribe((res: AddTemplateRequestRes) => {
if (res.code && res.code === CST.CODE.SUCCESS) {
this.messageService.success('修改成功');
this.queryTemplateInfo(false);
setTimeout(() => {
this.editForm(num);
}, 100);
} else {
this.messageService.error(res.message || '修改失败');
}
});
}
editForm(num) {
switch (num) {
case '1':
this.Form1.setValue({
templateName: this.templateInfo.templateName,
});
this.templateinformationEdit = false;
break;
case '2':
this.Form2.setValue({
sellerNo: this.templateInfo.sellerNo,
});
this.billEdit = false;
break;
case '3':
this.Form3.setValue({
paperInvoiceSupplierNo: this.templateInfo.paperInvoiceSupplierNo,
einvoiceSupplierNo: this.templateInfo.einvoiceSupplierNo,
});
this.supplierEdit = false;
break;
case '4':
this.Form4.setValue({
invoiceContentType: this.templateInfo.invoiceContentType,
invoiceContent: this.templateInfo.invoiceContent,
});
this.regularEdit = false;
break;
default:
break;
}
}
// 批量删除开票明细管理
ticketDetailDel() {
if (this.list.every((v) => v.checked === false)) {
this.messageService.warning('请选择开票明细');
return false;
}
const delList = [];
this.list.forEach((v1) => {
if (v1.templateItemID && v1.checked === true) {
delList.push(v1.templateItemID);
}
});
const data = {
templateItemIDList: delList,
};
this.modalService.info({
nzContent: '删除信息已提交,请点击确定查看处理结果',
nzOnOk: () => {
this.tservice
.templateitemsdelete(data)
.subscribe((res: AddTemplateRequestRes) => {
if (res.code && res.code === CST.CODE.SUCCESS) {
this.queryItems();
this.messageService.success('删除成功');
} else {
this.messageService.error(res.message || '删除失败');
}
});
},
});
}
// 添加开票明细管理
ticketDetailAdd() {
const ticketAddList = {
createOpID: null,
createTime: null,
itemName: null,
lastModifyTime: null,
modifyOpID: null,
taxCategoryCode: null,
taxRate: null,
templateItemID: null,
templateItemNo: null,
templateNo: null,
unit: null,
zeroTaxRateType: null,
checked: false,
editshow: true,
addchecked: true,
};
this.list.push(ticketAddList);
}
// 新增不需要时删除
ticketDetaiAddDel(i) {
this.list = this.list.filter((k, v) => v !== i);
}
// 保存开票明细
ticketDetaiSave(i) {
const valadate = this.itemVadate(i);
if (!valadate) {
return false;
}
const data = {
templateNo: this.templateNo,
templateItemInfo: {
itemName: this.list[i].itemName,
taxCategoryCode: this.list[i].taxCategoryCode,
taxRate: this.list[i].taxRate,
templateItemNo: this.list[i].templateItemNo,
zeroTaxRateType: this.list[i].zeroTaxRateType,
unit: this.list[i].unit,
},
};
this.tservice
.templateitemsadd(data)
.subscribe((res: AddTemplateItemRequestRes) => {
if (res.code && res.code === CST.CODE.SUCCESS) {
this.queryItems();
this.messageService.success('添加成功');
} else {
this.messageService.error(res.message || '添加失败');
}
});
}
// 修改开票明细
ticketDetaiUpdate(i) {
const valadate = this.itemVadate(i);
if (!valadate) {
return false;
}
const data = {
urlParams: {
ID: this.list[i].templateItemID,
},
templateItemInfo: {
itemName: this.list[i].itemName,
taxCategoryCode: this.list[i].taxCategoryCode,
taxRate: this.list[i].taxRate,
templateItemNo: this.list[i].templateItemNo,
zeroTaxRateType: this.list[i].zeroTaxRateType,
unit: this.list[i].unit,
},
};
this.tservice
.templateitemsupdate(data)
.subscribe((res: AddTemplateItemRequestRes) => {
if (res.code && res.code === CST.CODE.SUCCESS) {
this.queryItems();
this.messageService.success('修改成功');
} else {
this.messageService.error(res.message || '修改失败');
}
});
}
// 开票明细管理验证
itemVadate(i) {
const a = /^\d+(\.\d+)?$/;
let itemError = true;
if (this.list[i].taxRate !== null && !a.test(this.list[i].taxRate)) {
this.showError(this.list[i], 'taxRateShow');
itemError = false;
}
if (
Number.parseFloat(this.list[i].taxRate) === 0 &&
(this.list[i].zeroTaxRateType === '' ||
this.list[i].zeroTaxRateType == null)
) {
this.showError(this.list[i], 'zeroTaxRateTypeShows');
itemError = false;
}
return itemError;
}
// 开票明细编辑按钮
ticketDetaiEdit(i: number) {
this.copylist[i] = Object.assign(
Object.create(Object.getPrototypeOf(this.list[i])),
this.list[i]
);
// this.copylist[i] = JSON.parse(JSON.stringify(this.list[i]));
this.list[i].editshow = true;
}
// 开票明细取消按钮
ticketDetaiCancel(i: number) {
Object.assign(this.list[i], this.copylist[i]);
this.list[i].editshow = false;
delete this.copylist[i];
}
// 新增时初始化
createForm() {
this.DataForm = this.fb.group({
templateName: ['', [Validators.required, this.utils.isEmptySpace]],
templateNo: ['', [Validators.required, this.utils.isEmptySpace]],
einvoiceSupplierNo: [''],
invoiceContentType: [''],
paperInvoiceSupplierNo: [''],
sellerNo: ['', [Validators.required]],
invoiceContent: ['', [Validators.required, this.utils.isEmptySpace]],
checkbox: [''],
});
}
// 编辑初始化
editCreateForm() {
this.Form1 = this.fb.group({
templateName: ['', [Validators.required, this.utils.isEmptySpace]],
});
this.Form2 = this.fb.group({
sellerNo: [''],
});
this.Form3 = this.fb.group({
paperInvoiceSupplierNo: [''],
einvoiceSupplierNo: [''],
});
this.Form4 = this.fb.group({
invoiceContentType: [''],
invoiceContent: ['', [Validators.required, this.utils.isEmptySpace]],
});
}
// 请求消息后赋值
initInfoForm() {
this.Form1.setValue({
templateName: this.templateInfo.templateName,
});
this.Form2.setValue({
sellerNo: this.templateInfo.sellerNo,
});
this.Form3.setValue({
paperInvoiceSupplierNo: this.templateInfo.paperInvoiceSupplierNo,
einvoiceSupplierNo: this.templateInfo.einvoiceSupplierNo,
});
this.Form4.setValue({
invoiceContentType: this.templateInfo.invoiceContentType,
invoiceContent: this.templateInfo.invoiceContent,
});
}
// 查询签约主体列表
querySubjectList() {
const data = {
urlParams: {
pageSize: 100,
pageIndex: 1,
},
sellerName: null,
sellerNo: null,
};
this.tservice
.sellerssearch(data)
.subscribe((res: SearchSellerRequestRes) => {
if (res && res.code === CST.CODE.SUCCESS && res.data.sellerList) {
res.data.sellerList.forEach((v) => {
this.tsubjectlist.push(new Option(v.sellerNo, v.sellerName));
});
} else {
this.tsubjectlist = [];
}
});
}
// 查询纸质发票
pquerySupplierList() {
const data = {
urlParams: {
pageSize: 100,
pageIndex: 1,
},
invoiceMethod: '1',
};
this.tservice
.supplierssearch(data)
.subscribe((res: SearchSupplierRequestRes) => {
if (
res.code &&
res.code === CST.CODE.SUCCESS &&
res.data.supplierList
) {
res.data.supplierList.forEach((v) => {
this.pinvoicelist.push(new Option(v.supplierNo, v.supplierName));
});
} else {
this.pinvoicelist = [];
}
});
}
// 查询电子发票列表
equerySupplierList() {
const data = {
urlParams: {
pageSize: 100,
pageIndex: 1,
},
invoiceMethod: '2',
};
this.tservice
.supplierssearch(data)
.subscribe((res: SearchSupplierRequestRes) => {
if (
res.code &&
res.code === CST.CODE.SUCCESS &&
res.data.supplierList
) {
res.data.supplierList.forEach((v) => {
this.einvoicelist.push(new Option(v.supplierNo, v.supplierName));
});
} else {
this.einvoicelist = [];
}
});
}
initConst() {
this.tservice.queryConst({}).subscribe((res) => {
if (res && res.code === CST.CODE.SUCCESS) {
// 开票内容
Object.keys(res.data.INVOICE_CONTENT_TYPE).forEach((key, index) => {
this.invoicelist.push(
new Option(key, res.data.INVOICE_CONTENT_TYPE[key])
);
});
Object.keys(res.data.ZERO_TAX_RATE_TYPE).forEach((key, index) => {
this.taxtypelist.push(
new Option(key, res.data.ZERO_TAX_RATE_TYPE[key])
);
});
}
});
this.querySubjectList(); // 查询签约主体列表
this.pquerySupplierList(); // 查询纸质发票,电子发票
this.equerySupplierList(); // 查询纸质发票,电子发票
}
// 边框变红
showError(list, name) {
list[name] = true;
setTimeout(() => {
list[name] = false;
}, 3000);
}
clickTemplateInformation() {
this.templateinformationEdit = true;
}
cancelTemplateInformation() {
this.templateinformationEdit = false;
this.editForm('1');
}
clickBill() {
this.billEdit = true;
}
cancelBill() {
this.billEdit = false;
this.editForm('2');
}
clickSupplier() {
this.supplierEdit = true;
}
cancelSupplier() {
this.supplierEdit = false;
this.editForm('3');
}
clickRegular() {
this.regularEdit = true;
}
cancelRegular() {
this.regularEdit = false;
this.editForm('4');
}
}、
模板信息
模板名称*
模板名称不能为空
模板编号
*
模板编号不能为空
开票方关联
签约主体*
请选择签约主体
开票供应商关联
纸质发票
电子发票
开票规则
开票内容*
项目名称*
项目名称不能为空
新增模板
模板信息
修改
取消
保存
{{templateInfo.templateName}}
模板名称不能为空
{{templateNo}}
开票方关联
修改
取消
保存
{{templateInfo.sellerName}}
开票供应商关联
修改
取消
保存
{{templateInfo.paperInvoiceSupplierName}}
{{templateInfo.einvoiceSupplierName}}
开票规则
修改
取消
保存
{{templateInfo.invoiceContentType === '1' ? '项目明细' : '商品明细'}}
{{templateInfo.invoiceContent}}
项目名称不能为空
开票明细管理
添加
删除
操作
编号
发票内容名称
单位
税收分类编码
税率
零税率类型
编辑
保存
保存
|
删除
取消
{{option.templateItemNo}}
{{option.itemName}}
{{option.unit}}
{{option.taxCategoryCode}}
{{option.taxRate}}
{{option.zeroTaxRateTypeShow}}