define({
name: "invoice",
template: `
按商品赋码
按规格赋码
搜索
赋码
{{scope.row[specItem.attr]}}
赋码
发票赋码
搜索
`,
style: `
.tox-tinymce {
min-height:600px;
}
.code-color {
color:#29BA9C;
font-weight: bold;
}
`,
props: {
form: {
default() {
return {};
},
},
formKey: {
type: String,
},
},
data() {
return {
search:{
coding_type: 0 ,
code:'',
goods_name:'',
use_discount:1,
name:'',
// commodity_code:'',
tax_rate:'',
tax_type:'0',
tax_flag:1,
discount:''
},
dialogVisible: false,
// 记录点击的小标
columnIndex:'',
use_discountType: [],
taxRate: [],
dutyFree:[],
keyword:'',
dataList: [],
defaultProps: {
children: 'children',
label: 'mc'
},
tableColumnList: {
goodsSpecs: [],
table_data: [], // 表格中的数据
},
save_num:0,
};
},
mounted() {
console.log(this.form,'invoice');
for(let key in this.form.taxRate){
this.taxRate.push({
value:key,
label:this.form.taxRate[key]+'%'
})
}
for(let key in this.form.discount){
this.use_discountType.push({
value:this.form.discount[key],
label:this.form.discount[key]
})
}
for(let key in this.form.exemptionType){
this.dutyFree.push({
value:key,
label:this.form.exemptionType[key]
})
}
this.editHandleData(this.form); // test_data测试数据
this.dataList = this.form.treeCode;
this.search.coding_type = this.form.invoice.coding_type;
// this.form.invoice.options = this.form.invoice.options.reverse()
if(this.form.invoice.coding_type == 1){
this.tableColumnList.table_data.forEach((item,index) => {
item.coding_type = this.form.invoice.coding_type;
item.code = this.form.invoice.options[index].code ;
item.goods_name = this.form.invoice.options[index].goods_name ;
item.use_discount = this.form.invoice.options[index].use_discount;
item.name = this.form.invoice.options[index].name;
item.tax_rate = this.form.invoice.options[index].tax_rate;
item.tax_type = this.form.invoice.options[index].tax_type ;
item.tax_flag = this.form.invoice.options[index].tax_flag ;
item.discount = this.form.invoice.options[index].discount
})
}else{
this.search.code = this.form.invoice.code ;
this.search.goods_name = this.form.invoice.goods_name ;
this.search.use_discount = this.form.invoice.use_discount;
this.search.name = this.form.invoice.name;
this.search.tax_rate = this.form.invoice.tax_rate === 0 ? '0' : this.form.invoice.tax_rate;
this.search.tax_type = this.form.invoice.tax_type ;
this.search.tax_flag = this.form.invoice.tax_flag ;
this.search.discount = this.form.invoice.discount;
this.tableColumnList.table_data.forEach((item,index) => {
item.coding_type = 1;
item.code = '' ;
item.goods_name = '' ;
item.use_discount = 1;
item.name = '';
item.tax_rate = '0';
item.tax_type = '0';
item.tax_flag = 1;
item.discount = ''
})
}
},
watch:{
'search.coding_type':{
handler(newVal, oldVal){
this.search.code = '';
this.search.goods_name = '';
this.search.use_discount = 1;
this.search.name = '';
this.search.discount = '';
this.search.tax_rate = '0';
this.search.tax_type = '0';
this.search.tax_flag = 1;
},
}
},
methods: {
editHandleData(data) {
// 处理回显的数据
for (let i = 0; i < data.specs.length; i++) {
this.tableColumnList.goodsSpecs.push({
id: data.specs[i].id,
attr: data.specs[i].title,
valueList: data.specs[i].spec_item
})
// 循环旧数据 规格项都对应的数据应该赋给新数据
this.changeOption(data.option)
}
},
changeOption(data) {
let oldData = [];
// 先保存旧的表格数据
let old_table_data = JSON.parse(JSON.stringify(data));
let old_data_length = old_table_data.length
for (let i = 0; i < old_data_length; i++) {
let title = "";
this.tableColumnList.goodsSpecs.forEach((item)=> {
title = title + old_table_data[i][item.attr] + '+'
})
oldData.push({
title: title.slice(0,title.length-1),
index: i
})
}
// 重新生成处理表头数据和表格数据
this.generateTableColumn();
this.traverseSku();
let data_length = this.tableColumnList.table_data.length
for (let i = 0; i < data_length; i++) {
// 循环新的表格数据进行替换
let title = "";
this.tableColumnList.goodsSpecs.forEach((item)=> {
title = title + this.tableColumnList.table_data[i][item.attr] + '+'
})
oldData.forEach((item)=> {
if(item.title == title.slice(0,title.length-1)) {
this.tableColumnList.table_data[i].id = old_table_data[item.index].id;
}
})
}
},
// 遍历 `goodsSpecs` 生成表格数据
traverseSku() {
let ready_map = this.generateBaseData(this.tableColumnList.goodsSpecs)
this.tableColumnList.table_data = this.mergeTableData(ready_map)
},
// 重新实现笛卡尔积 入参是: this.tableColumnList.goodsSpecs 传入的数组 '为空', '长度为1', '长度大于1' 三种情况 分别处理
generateBaseData(arr) {
if (arr.length === 0) return []
if (arr.length === 1) {
let [item_spec] = arr
return item_spec.valueList.map(x => {
return {
[item_spec.attr]: x.title
}
})
}
if (arr.length >= 1) {
return arr.reduce((accumulator, spec_item) => {
// accumulator判断是之前整合的规格数组还是单个规格项
let acc_value_list = Array.isArray(accumulator.valueList) ? accumulator.valueList : accumulator
let item_value_list = spec_item.valueList
let result = []
for (let i in acc_value_list) {
for (let j in item_value_list) {
let temp_data = {}
if (!acc_value_list[i].title) {
// accumulator不是Array的情况
temp_data = {
...acc_value_list[i],
[spec_item.attr]: item_value_list[j].title,
// id:item_value_list[j].id
}
// 否则如果是单个规格项
} else {
temp_data[accumulator.attr] = acc_value_list[i].title
temp_data[spec_item.attr] = item_value_list[j].title
// temp_data['id'] = item_value_list[j].id
}
result.push(temp_data)
}
}
return result
})
}
},
generateTableColumn() {
// this.tableHeaderList = this.tableColumnList.goodsSpecs.map(x => x.attr).concat(['stock', 'withhold_stock', 'tax_flaget_price', 'product_price', 'cost_price', 'goods_sn', 'product_sn', 'weight', 'volume', 'thumb', ])
this.tableHeaderList = this.tableColumnList.goodsSpecs.map(x => x.attr).concat([])
},
// 合并 goodsSpecs 与 '现价', '库存', '市场价格' , 返回整个表格数据数组
mergeTableData(arr) {
return arr.map((item) => {
this.sp_id++;
// return { ...item, id: 'SP'+ this.sp_id, 'stock': '', 'withhold_stock': '', 'tax_flaget_price': '', 'product_price': '', 'cost_price': '', 'goods_sn': '', 'product_sn': '', 'weight': '', 'volume': '', 'thumb': '', }
return { ...item,id:item.id}
})
},
// 合并单元格
mergeRowComputed({ row, column, rowIndex, columnIndex }) {
if (columnIndex == 0) {
let key_0 = column.label
let first_idx = this.tableColumnList.table_data.findIndex(x => x[key_0] == row[key_0])
const calcSameLength = () => this.tableColumnList.table_data.filter(x => x[key_0] == row[key_0]).length
first_column_rule = rowIndex == first_idx ? [calcSameLength(), 1] : [0, 0]
return first_column_rule
}else {
// 表格数据的每一项,
const callBacks = (table_item, start_idx = 0) => {
if (columnIndex < start_idx) return true
let curr_key = this.tableHeaderList[start_idx]
return table_item[curr_key] === row[curr_key] && callBacks(table_item, ++start_idx)
}
let first_idx = this.tableColumnList.table_data.findIndex(x => callBacks(x))
const calcSameLength = () => this.tableColumnList.table_data.filter(x => callBacks(x)).length
return rowIndex == first_idx ? [calcSameLength(), 1] : [0, 0]
}
},
// 弹窗
codingDialog(index){
if(!this.form.has_option){ return }
this.dialogVisible = true;
this.columnIndex = index;
if(index === ''){
this.search.code = '' ;
this.search.goods_name = '' ;
this.search.use_discount = 1;
this.search.name = '';
this.search.tax_rate = '0';
this.search.tax_type = '0';
this.search.tax_flag = 1;
this.search.discount = '';
}else{
this.search.coding_type = this.tableColumnList.table_data[this.columnIndex].coding_type;
this.search.code = this.tableColumnList.table_data[this.columnIndex].code ;
this.search.goods_name = this.tableColumnList.table_data[this.columnIndex].goods_name ;
this.search.use_discount = this.tableColumnList.table_data[this.columnIndex].use_discount ;
this.search.name = this.tableColumnList.table_data[this.columnIndex].name ;
this.search.tax_rate = this.tableColumnList.table_data[this.columnIndex].tax_rate;
this.search.tax_type = this.tableColumnList.table_data[this.columnIndex].tax_type ;
this.search.tax_flag = this.tableColumnList.table_data[this.columnIndex].tax_flag ;
this.search.discount = this.tableColumnList.table_data[this.columnIndex].discount ;
console.log(this.tableColumnList.table_data ,this.columnIndex,'this.search.discount ');
// }
}
},
filterNode(value, data) {
if (!value) return true;
return data.mc.indexOf(value) !== -1;
},
// 搜索
serachContent(){
this.$refs.tree.filter(this.keyword);
},
// 获取点击节点数据
handleNodeClick(data){
if(data.children.length === 0){
this.search.code = data.bm;
this.search.goods_name = '' ;
this.search.name = data.spbmjc;
// this.search.commodity_code = data.mc;
}
},
validate(){
if(this.search.tax_type === ''){
this.$message.success('免税类型选项不能为空')
return false
}
if(this.search.tax_rate === ''){
this.$message.success('税率选项不能为空')
return false
}
return this.save()
},
// 保存
save(){
this.save_num += 1;
if(this.columnIndex === ''){
for(let item of this.tableColumnList.table_data){
item.coding_type = this.search.coding_type;
item.code = this.search.code;
item.goods_name = this.search.goods_name;
item.use_discount = this.search.use_discount;
item.name = this.search.name;
// item.commodity_code = this.search.commodity_code;
item.tax_rate = this.search.tax_rate;
item.tax_type = this.search.tax_type;
item.tax_flag = this.search.tax_flag;
item.discount = this.search.discount;
}
}else{
this.tableColumnList.table_data[this.columnIndex].coding_type = this.search.coding_type;
this.tableColumnList.table_data[this.columnIndex].code = this.search.code;
this.tableColumnList.table_data[this.columnIndex].goods_name = this.search.goods_name;
this.tableColumnList.table_data[this.columnIndex].use_discount = this.search.use_discount;
this.tableColumnList.table_data[this.columnIndex].name = this.search.name;
this.tableColumnList.table_data[this.columnIndex].tax_rate = this.search.tax_rate;
this.tableColumnList.table_data[this.columnIndex].tax_type = this.search.tax_type;
this.tableColumnList.table_data[this.columnIndex].tax_flag = this.search.tax_flag;
this.tableColumnList.table_data[this.columnIndex].discount = this.search.discount;
}
this.dialogVisible = false;
let options = []
for(let item of this.tableColumnList.table_data){
options.push({
// coding_type : this.search.coding_type,
code : item.code,
name : item.name,
use_discount : item.use_discount,
discount : item.discount,
tax_type : item.tax_type,
tax_rate : item.tax_rate,
goods_name : item.goods_name,
// goods_code : item.commodity_code,
tax_flag : item.tax_flag,
goods_option_id : item.id
})
}
let invoice = {
coding_type : this.search.coding_type,
code : this.search.coding_type ? '' : this.search.code,
name : this.search.coding_type ? '' : this.search.name,
use_discount : this.search.coding_type ? 1 : this.search.use_discount,
discount : this.search.coding_type ? '' : this.search.discount,
tax_type : this.search.coding_type ? '' : this.search.tax_type,
tax_rate : this.search.coding_type ? '' : this.search.tax_rate,
goods_name : this.search.coding_type ? '' : this.search.goods_name,
// goods_code : this.search.commodity_code,
tax_flag : this.search.coding_type ? 1 : this.search.tax_flag,
options : this.search.coding_type && this.form.has_option == 1 ? options : []
}
console.log(invoice,'options',this.tableColumnList.table_data,this.search.goods_name);
return invoice
}
},
});