index.vue 16 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
<template>
  <div class="p-4">
    <BasicTable @register="registerTable" :pagination="{ pageSize: 20 }">
      <template #toolbar>
        <a-button
          type="primary"
          @click="handleCheckSum"
          :style="{ borderRadius: '5px 5px 5px 5px' }"
          >应付款汇总</a-button
        >
        <FinanceEdit @register="registerFinanceEdit" @success="handleSuccess" />
        <TrackEdit @register="registerTrackEdit" @success="handleSuccess" />
        <CheckSum @register="registerCheckSum" />
        <InvoiceUpload @register="registerInvoiceUpload" @success="handleSuccess" />
        <CheckDetail @register="registerInvoiceDetail" />
        <DeductShow @register="registerDeductShow" />
        <InvoiceShow @register="registerInvoiceShow" />
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '财务编辑',
                onClick: handleFinanceEdit.bind(null, record),
              },
              {
                label: '跟单编辑',
                onClick: handleTrackEdit.bind(null, record),
              },
              {
                label: '发票上传',
                onClick: handleInvoiceUpload.bind(null, record),
              },
            ]"
            :dropDownActions="[
              {
                label: '提交审核',
                popConfirm: {
                  title: '是否确认提交审核',
                  placement: 'left',
                  confirm: handleCommit.bind(null, record),
                },
                // onClick: handleCommit.bind(null, record),
              },
              {
                label: '订单信息',
                onClick: handleDetail.bind(null, record),
              },
              {
                label: '删除',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
                // onClick: handleDelete.bind(null, record),
              },
              {
                label: '生产科发票',
                onClick: handleInvoiceShow.bind(null, record),
              },
              {
                label: '扣款单',
                onClick: handleDeductShow.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { BasicTable, useTable, BasicColumn, TableAction } from '/@/components/Table';
  import { searchFormSchema, columns } from './pay.data';
  import TrackEdit from './TrackEdit.vue';
  import FinanceEdit from './FinanceEdit.vue';
  import InvoiceUpload from './InvoiceUpload.vue';
  import CheckDetail from './CheckDetail.vue';
  import CheckSum from './CheckSum.vue';
  import DeductShow from './DeductShow.vue';
  import InvoiceShow from './InvoiceShow.vue';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { getCheck, checkDelete, checkCommit, checkDetail } from '@/api/project/invoice';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getOrderList } from '/@/api/project/order';

  const [registerCheckSum, { openModal: openCheckSum }] = useModal();
  const [registerFinanceEdit, { openDrawer: openFinanceEdit }] = useDrawer();
  const [registerTrackEdit, { openDrawer: openTrackEdit }] = useDrawer();
  const [registerInvoiceUpload, { openModal: openInvoiceUpload }] = useModal();
  const [registerDeductShow, { openModal: openDeductShow }] = useModal();
  const [registerInvoiceShow, { openModal: openInvoiceShow }] = useModal();
  const [registerInvoiceDetail, { openDrawer: openCheckDetail }] = useDrawer();
  const checkedKeys = ref<Array<string | number>>([]);
  // 添加分页状态
  const pagination = ref({
    current: 1, // 当前页
    pageSize: 10, // 每页条数
    total: 0, // 数据总数
  });
  const [registerTable, { reload }] = useTable({
    title: '',
    api: getCheck,
    columns: columns,
    bordered: true,
    clickToRowSelect: false,
    rowKey: 'id',
    pagination: {
      pageSize: 20,
    },
    rowSelection: {
      type: 'checkbox',
      selectedRowKeys: checkedKeys,
      onSelect,
      onSelectAll,
    },
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
    },
    useSearchForm: true,
    showTableSetting: true,
    showIndexColumn: false,
    tableSetting: {
      setting: false,
    },
    actionColumn: {
      width: 260,
      title: 'Action',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
    },
  });
  //选择
  // function onSelect(record, selected) {
  //   if (selected) {
  //     checkedKeys.value = [...checkedKeys.value, record.id];
  //   } else {
  //     checkedKeys.value = checkedKeys.value.filter((id) => id !== record.id);
  //   }
  // }
  // function onSelectAll(selected, selectedRows, changeRows) {
  //   const changeIds = changeRows.map((item) => item.id);
  //   if (selected) {
  //     checkedKeys.value = [...checkedKeys.value, ...changeIds];
  //   } else {
  //     checkedKeys.value = checkedKeys.value.filter((id) => {
  //       return !changeIds.includes(id);
  //     });
  //   }
  // }

  // type CustomerCodeEntry = [string, number]; // 定义二维数组类型 [customerCode, count]

  // const selectedCustomCodes = ref<CustomerCodeEntry[]>([]); // 创建一个二维数组的 ref

  // // 选择函数
  // async function onSelect(record: any, selected: boolean) {
  //   if (selected) {
  //     const res = await checkDetail({ checkNo: record.checkNo });
  //     const customerCode = res[0].customerCode;

  //     const index = selectedCustomCodes.value.findIndex(([code]) => code === customerCode);

  //     if (index !== -1) {
  //       // 如果存在,增加计数
  //       selectedCustomCodes.value[index][1] += 1;
  //     } else {
  //       // 如果不存在,添加新项 [customerCode, 1]
  //       selectedCustomCodes.value.push([customerCode, 1]);
  //     }

  //     checkedKeys.value = [...checkedKeys.value, record.id];
  //   } else {
  //     const res = await checkDetail({ checkNo: record.checkNo });
  //     const customerCode = res[0].customerCode;

  //     const index = selectedCustomCodes.value.findIndex(([code]) => code === customerCode);

  //     if (index !== -1) {
  //       if (selectedCustomCodes.value[index][1] > 1) {
  //         // 如果计数大于 1,减少计数
  //         selectedCustomCodes.value[index][1] -= 1;
  //       } else {
  //         // 如果计数为 1,移除该项
  //         selectedCustomCodes.value.splice(index, 1);
  //       }
  //     }

  //     checkedKeys.value = checkedKeys.value.filter((id) => id !== record.id);
  //   }
  //   console.log(selectedCustomCodes.value, 56561);
  //   console.log(checkedKeys.value, 56562);
  // }

  // async function onSelectAll(selected: boolean, selectedRows: any[], changeRows: any[]) {
  //   const changeIds = changeRows.map((item) => item.id);
  //   const changeCustomerCodes = await Promise.all(
  //     changeRows.map(async (item) => {
  //       const res = await checkDetail({ checkNo: item.checkNo });
  //       return res[0].customerCode;
  //     }),
  //   );

  //   if (selected) {
  //     // 添加到 checkedKeys
  //     checkedKeys.value = [...checkedKeys.value, ...changeIds];

  //     // 处理每个 customerCode
  //     changeCustomerCodes.forEach((code) => {
  //       const index = selectedCustomCodes.value.findIndex(
  //         ([customerCode]) => customerCode === code,
  //       );
  //       if (index !== -1) {
  //         // 如果存在,增加计数
  //         selectedCustomCodes.value[index][1] += 1;
  //       } else {
  //         // 如果不存在,添加新项 [customerCode, 1]
  //         selectedCustomCodes.value.push([code, 1]);
  //       }
  //     });
  //   } else {
  //     // 从 checkedKeys 中移除
  //     checkedKeys.value = checkedKeys.value.filter((id) => !changeIds.includes(id));

  //     // 处理每个 customerCode
  //     changeCustomerCodes.forEach((code) => {
  //       const index = selectedCustomCodes.value.findIndex(
  //         ([customerCode]) => customerCode === code,
  //       );
  //       if (index !== -1) {
  //         if (selectedCustomCodes.value[index][1] > 1) {
  //           // 如果计数大于 1,减少计数
  //           selectedCustomCodes.value[index][1] -= 1;
  //         } else {
  //           // 如果计数为 1,移除该项
  //           selectedCustomCodes.value.splice(index, 1);
  //         }
  //       }
  //     });
  //   }
  //   console.log(selectedCustomCodes.value, 56561);
  //   console.log(checkedKeys.value, 56562);
  // }
  //选择算法:创建二维数组,使客户编码和生产科相同
  type CustomerCodeEntry = [string, number]; // 定义二维数组类型 [customerCode, count]
  type ProductionDepartmentEntry = [string, number]; // 定义二维数组类型 [productionDepartment, count]

  const selectedCustomCodes = ref<CustomerCodeEntry[]>([]); // 创建一个二维数组的 ref
  const selectedProductionDepartment = ref<ProductionDepartmentEntry[]>([]); // 创建一个二维数组的 ref
  // const checkedKeys = ref<string[]>([]); // 选中的键值列表

  // 单选函数
  async function onSelect(record, selected: boolean) {
    const res = await checkDetail({ checkNo: record.checkNo });
    const customerCode = res[0].customerCode;
    const productionDepartment = res[0].productionDepartment;

    const customerCodeIndex = selectedCustomCodes.value.findIndex(
      ([code]) => code === customerCode,
    );

    const productionDepartmentIndex = selectedProductionDepartment.value.findIndex(
      ([department]) => department === productionDepartment,
    );

    if (selected) {
      checkedKeys.value = [...checkedKeys.value, record.id];

      // 更新 selectedCustomCodes
      if (customerCodeIndex !== -1) {
        selectedCustomCodes.value[customerCodeIndex][1] += 1;
      } else {
        selectedCustomCodes.value.push([customerCode, 1]);
      }

      // 更新 selectedProductionDepartment
      if (productionDepartmentIndex !== -1) {
        selectedProductionDepartment.value[productionDepartmentIndex][1] += 1;
      } else {
        selectedProductionDepartment.value.push([productionDepartment, 1]);
      }
    } else {
      checkedKeys.value = checkedKeys.value.filter((id) => id !== record.id);

      // 更新 selectedCustomCodes
      if (customerCodeIndex !== -1) {
        if (selectedCustomCodes.value[customerCodeIndex][1] > 1) {
          selectedCustomCodes.value[customerCodeIndex][1] -= 1;
        } else {
          selectedCustomCodes.value.splice(customerCodeIndex, 1);
        }
      }

      // 更新 selectedProductionDepartment
      if (productionDepartmentIndex !== -1) {
        if (selectedProductionDepartment.value[productionDepartmentIndex][1] > 1) {
          selectedProductionDepartment.value[productionDepartmentIndex][1] -= 1;
        } else {
          selectedProductionDepartment.value.splice(productionDepartmentIndex, 1);
        }
      }
    }
    // console.log(checkedKeys.value, 565666666); // 输出当前的 selectedCustomCodes 值
    // console.log(selectedCustomCodes.value, 565666666); // 输出当前的 selectedCustomCodes 值
    // console.log(selectedProductionDepartment.value, 565666666); // 输出当前的 selectedCustomCodes 值
  }

  // 全选函数
  async function onSelectAll(selected: boolean, selectedRows: any[], changeRows: any[]) {
    const changeIds = changeRows.map((item) => item.id);
    const changeCustomerCodes = await Promise.all(
      changeRows.map(async (item) => {
        const res = await checkDetail({ checkNo: item.checkNo });
        return res[0].customerCode;
      }),
    );

    const changeProductionDepartments = await Promise.all(
      changeRows.map(async (item) => {
        const res = await checkDetail({ checkNo: item.checkNo });
        return res[0].productionDepartment;
      }),
    );

    if (selected) {
      checkedKeys.value = [...checkedKeys.value, ...changeIds];

      // 更新 selectedCustomCodes
      changeCustomerCodes.forEach((code) => {
        const index = selectedCustomCodes.value.findIndex(
          ([customerCode]) => customerCode === code,
        );
        if (index !== -1) {
          selectedCustomCodes.value[index][1] += 1;
        } else {
          selectedCustomCodes.value.push([code, 1]);
        }
      });

      // 更新 selectedProductionDepartment
      changeProductionDepartments.forEach((department) => {
        const index = selectedProductionDepartment.value.findIndex(
          ([prodDepartment]) => prodDepartment === department,
        );
        if (index !== -1) {
          selectedProductionDepartment.value[index][1] += 1;
        } else {
          selectedProductionDepartment.value.push([department, 1]);
        }
      });
    } else {
      checkedKeys.value = checkedKeys.value.filter((id) => !changeIds.includes(id));

      // 更新 selectedCustomCodes
      changeCustomerCodes.forEach((code) => {
        const index = selectedCustomCodes.value.findIndex(
          ([customerCode]) => customerCode === code,
        );
        if (index !== -1) {
          if (selectedCustomCodes.value[index][1] > 1) {
            selectedCustomCodes.value[index][1] -= 1;
          } else {
            selectedCustomCodes.value.splice(index, 1);
          }
        }
      });

      // 更新 selectedProductionDepartment
      changeProductionDepartments.forEach((department) => {
        const index = selectedProductionDepartment.value.findIndex(
          ([prodDepartment]) => prodDepartment === department,
        );
        if (index !== -1) {
          if (selectedProductionDepartment.value[index][1] > 1) {
            selectedProductionDepartment.value[index][1] -= 1;
          } else {
            selectedProductionDepartment.value.splice(index, 1);
          }
        }
      });
    }

    // console.log(checkedKeys.value, 565666666); // 输出当前的 selectedCustomCodes 值
    // console.log(selectedCustomCodes.value, 565666666); // 输出当前的 selectedCustomCodes 值
    // console.log(selectedProductionDepartment.value, 565666666); // 输出当前的 selectedCustomCodes 值
  }

  function handleFinanceEdit(record) {
    openFinanceEdit(true, {
      data: record,
    });
  }
  function handleTrackEdit(record) {
    openTrackEdit(true, {
      data: record,
    });
  }
  function handleInvoiceUpload(record) {
    openInvoiceUpload(true, {
      data: record,
    });
  }
  function handleDelete(record) {
    const id: string[] = Array.isArray(record.id) ? record.id : [record.id];
    checkDelete({ ids: id });
    setTimeout(() => {
      reload();
    }, 50);
  }
  function handleInvoiceShow(record) {
    openInvoiceShow(true, {
      data: record,
    });
  }
  function handleDeductShow(record) {
    openDeductShow(true, {
      data: record,
    });
  }
  function handleDetail(record) {
    openCheckDetail(true, {
      data: record,
    });
  }
  function handleCommit(record) {
    const currentDate: string = (() => {
      const date = new Date();
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    })();
    checkCommit({ id: record.id, actualPayedDate: currentDate });
    setTimeout(() => {
      reload();
    }, 50);
  }

  const { createMessage } = useMessage();
  const { error } = createMessage;
  function handleCheckSum(record) {
    if (checkedKeys.value.length == 0) {
      error('请选择订单');
      return;
    }
    if (selectedProductionDepartment.value.length > 1) {
      error('勾选订单的生产科需一致');
      return;
    }
    openCheckSum(true, {
      data: checkedKeys.value,
    });
  }
  function handleSuccess() {
    setTimeout(() => {
      reload();
    }, 150);
  }
</script>