Untitled

 avatar
unknown
plain_text
a year ago
7.0 kB
8
Indexable
  async exportExcel(res: Response, query: exportExcel, id_customer?: number) {
    // Validate dateFrom, dateTo
    const dateFrom = dayjs(query.dateFrom);
    const dateTo = dayjs(query.dateTo);
    const days = dateTo.diff(dateFrom, 'day');
    if (days >= 45)
      throw new BadRequestException('Không thể xuất file quá 45 ngày');

    const { type } = query;
    const workbook = new excel.Workbook();
    const worksheet = workbook.addWorksheet('export-order-' + type);
    query.filter = query.filter || [`id_customer=${id_customer}`];
    query.not = query.not || [];
    let customer;
    switch (type) {
      case TYPE_EXPORT_EXCEL.CHODUYET:
        query.filter.push(`id_status=${ENUM_STATUS.STATUS1}`);
        break;
      case TYPE_EXPORT_EXCEL.CHOLAYHANG:
        query.filter.push(`id_status=${ENUM_STATUS.STATUS2}`);
        break;
      case TYPE_EXPORT_EXCEL.DONCONLAI:
        query.not.push(
          `id_status!=${ENUM_STATUS.STATUS0}`,
          `id_status!=${ENUM_STATUS.STATUS16}`,
          `id_status!=${ENUM_STATUS.STATUS2}`,
          `id_status!=${ENUM_STATUS.STATUS1}`,
        );
        break;
      default:
        if (id_customer && !query.tab) {
          query.not.push(`id_status!=${ENUM_STATUS.STATUS16}`);
        }
        break;
    }
    if (id_customer) {
      customer = await this.customerService.findOne({
        where: {
          id: id_customer,
        },
      });
    }
    if (query.tab) {
      switch (query.tab) {
        case 2:
          query.filter.push('id_status=1', 'id_status=2');
          break;
        case 3:
          query.filter.push('id_status=3', 'id_status=4', 'id_status=5');
          break;
        case 4:
          query.filter.push('id_status=6');
          break;
        case 5:
          query.filter.push('id_status=7', 'id_status=8');
          break;
        case 6:
          query.filter.push('id_status=9');
          break;
        case 7:
          query.filter.push('id_status=10');
          break;
        case 8:
          query.filter.push(
            'id_status=11',
            'id_status=12',
            'id_status=13',
            'id_status=17',
            'id_status=18',
          );
          break;
        case 9:
          query.filter.push('id_status=14', 'id_status=15');
          break;
        case 10:
          query.filter.push('id_status=0');
          break;
        default:
          break;
      }
    }
    const orders = await this.repo.getManyNoPaginateV2(query);

    // Init width for each row
    worksheet.columns = [
      { width: 5 },
      { width: 15 },
      { width: 20 },
      { width: 15 },
      { width: 20 },
      { width: 15 },
      { width: 30 },
      { width: 15 },
      { width: 30 },
      { width: 15 },
      { width: 15 },
      { width: 15 },
      { width: 25 },
      { width: 20 },
      { width: 15 },
      { width: 25 },
      { width: 15 },
      { width: 15 },
      { width: 15 },
      { width: 25 },
      { width: 15 },
      { width: 15 },
      { width: 15 },
      { width: 15 },
      { width: 15 },
      { width: 25 },
      { width: 15 },
      { width: 30 },
      { width: 20 },
      { width: 30 },
      { width: 30 },
      { width: 30 },
      { width: 30 },
    ];

    // Define header and title row
    worksheet.addRows([
      id_customer ? [`Danh sách đơn hàng của ${customer.name}`] : null,
      [
        'STT',
        'Mã Đơn Hàng',
        'Mã Đối Tác',
        'Tên cửa hàng',
        'Trạng thái',
        'Người gửi',
        'SĐT gửi',
        'Địa chỉ gửi',
        'Người nhận',
        'SĐT nhận',
        'Địa chỉ nhận',
        'Phí vận chuyển',
        'Phí thu hộ ban đầu',
        'Phí vượt khối lượng',
        'Phí bảo hiểm',
        'Phí thu hộ tiền hàng',
        'Phí hoàn hàng',
        'Phụ phí',
        'Tiền COD',
        'Tùy chọn thanh toán',
        'Giá trị khai giá',
        'Khối lượng',
        'Dài',
        'Rộng',
        'Cao',
        'Mã đơn khách hàng',
        'Tên hàng hóa',
        'Ghi chú thêm',
        'Ngày tạo đơn',
        'Ngày giao hàng thành công',
        'Ngày hoàn hàng thành công',
        'Ngày đối soát',
        'Ghi chú công khai',
      ],
    ]);

    // Merge first row
    worksheet.mergeCells('A1', 'AD1');
    // CSS for header and title row
    const titleRow = worksheet.getRow(1);
    titleRow.eachCell((cell) => {
      cell.style = {
        font: { color: { argb: '2775CD' }, size: 15 },
      };
    });
    const headerRow = worksheet.getRow(2);
    headerRow.eachCell((cell) => {
      cell.style = {
        font: { bold: true, color: { argb: 'FFFFFF' } },
        fill: {
          type: 'pattern',
          pattern: 'solid',
          fgColor: { argb: '00467F' },
        },
        alignment: { horizontal: 'center' },
      };
    });

    // Add Data for Excel
    worksheet.addRows(
      await Promise.all(
        orders.map(async (order, index) => {
          const ware_house_create = await this.wareHouseService.findOne({
            where: {
              id: order?.id_ware_house_create,
            },
            withDeleted: true,
          });

          return [
            index + 1,
            order?.code,
            order?.partner_order_code,
            order?.customer.name,
            STATUS_ORDER[order?.id_status],
            ware_house_create?.name,
            ware_house_create?.phone,
            ware_house_create?.address,
            order?.to_infomation?.name,
            order?.to_infomation?.phone,
            order?.to_infomation?.address,
            order?.fee_delivery,
            order?.old_cod,
            order?.fee_extra,
            order?.fee_insurance,
            order?.fee_cod,
            order?.fee_return,
            order?.edit_fee,
            order?.cod,
            PAYER_ORDER[order?.payer],
            order?.insurance_value,
            order?.weight_real,
            order?.size?.length,
            order?.size?.width,
            order?.size?.height,
            order?.client_order_code,
            order?.item_name,
            order?.note_delivery,
            order?.createdAt,
            order?.delivery_at,
            order?.return_at,
            order?.partner_debits_at,
            order?.note_public,
          ];
        }),
      ),
    );

    res.setHeader(
      'Content-Type',
      'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    );
    res.setHeader(
      'Content-Disposition',
      'attachment; filename=' + 'export-order-' + type + '.xlsx',
    );
    return workbook.xlsx.write(res).then(function () {
      res.status(200).end();
    });
  }
Editor is loading...
Leave a Comment