Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
5.6 kB
3
Indexable
Never
async listInvoice(params: any, errorCodeHandlingsService?: any) {
    try {
      const query = this.invoiceRepo.createQueryBuilder('invoice');

      query
        .select([
          'invoice.id as id',
          'invoice.order_id as orderId',
          'buyer.business_name as outletName',
          'supplier.business_name as warehouseName',
         // 'outlet.business_name as outletName',
          'invoice_number as invoiceNumber',
         // 'warehouse.business_name as warehouseName',
          'orders.total as estAmount',
          'orders.payment_status as payment_status',
          'orders.actual_amount as actual_amount',
          'orders.product_count as product_count',
          'orders.actual_subtotal as actual_subtotal',
        ]) 
        .leftJoin('invoice.buyer', 'buyer')
        .leftJoin('invoice.supplier', 'supplier')
        .leftJoin('invoice.orders', 'orders')
        .leftJoin('invoice.outlet', 'outlet')
        .leftJoin('invoice.warehouse', 'warehouse');

      // console.log(query.getQuery());
      // console.log(params);
      // console.log(params.isFlag === 'isSupplier');
      // console.log(params.isFlag === 'isBuyer');
      query.andWhere('orders.order_type = :orderType', { orderType: 'Online' });
      if (params.isFlag === 'isSupplier') {
        console.log('isSupplier');
        query.andWhere('invoice.supplier_id Ilike :supplier_id', { supplier_id: params.supplier_id });
      } else if (params.isFlag === 'isBuyer') {
        console.log('isBuyer', params.buyer_id);
        query.andWhere('invoice.buyer_id Ilike :buyer_id', { buyer_id: `%${params.buyer_id}%` });
      }
      if (params.customer_id != null) {
        query.andWhere('supplier.id  = :supplier_id', { supplier_id: params.customer_id });
      }
      if (params.ware_house_id != null) {
        query.andWhere('warehouse.id  = :ware_house_id', { ware_house_id: params.ware_house_id });
      }
      console.log(query.getQuery());
      if (params.search != (undefined || null)) {
        query.andWhere(
          new Brackets(search_where => {
            // search_where.where('invoice.order_id Ilike :search', {
            //   search: `%${params.search}%`,
            // });
            search_where.orWhere('invoice.invoice_number Ilike :search', {
              search: `%${params.search}%`,
            });
            if (params.buyer_id) {
              search_where.orWhere('invoice.invoice_number Ilike :search', {
                search: `%${params.search}%`,
              });
            }
            // if (params.buyer_id) {
            //   search_where.orWhere('buyer.first_name Ilike :search', {
            //     search: `%${params.search}%`,
            //   });
            // }
            // if (params.supplier_id) {
            //   search_where.orWhere('supplier.first_name Ilike :search', {
            //     search: `%${params.search}%`,
            //   });
            // }
            if (params.buyer_id) {
              search_where.orWhere('buyer.business_name Ilike :search', {
                search: `%${params.search}%`,
              });
            }
            if (params.supplier_id) {
              search_where.orWhere('supplier.business_name Ilike :search', {
                search: `%${params.search}%`,
              });
            }
          }),
        );
      }
      if (params.estimate_amount_key != null && params.estimate_amount_value != null) {
        if (params.estimate_amount_key == 'less') {
          query.andWhere('"orders"."total"::numeric < :estimate_amount_value', {
            estimate_amount_value: params.estimate_amount_value,
          });
        } else if (params.estimate_amount_key == 'equal') {
          query.andWhere('"orders"."total"::numeric = :estimate_amount_value', {
            estimate_amount_value: params.estimate_amount_value,
          });
        } else {
          query.andWhere('"orders"."total"::numeric > :estimate_amount_value', {
            estimate_amount_value: params.estimate_amount_value,
          });
        }
      }
      if (params.actual_amount_key != null && params.actual_amount_value != null) {
        if (params.actual_amount_key == 'greater')
          query.andWhere('"orders"."actual_amount"::numeric   > :actual_amount_value', {
            actual_amount_value: params.actual_amount_value,
          });
        else if (params.actual_amount_key == 'equal')
          query.andWhere('"orders"."actual_amount"::numeric   = :actual_amount_value', {
            actual_amount_value: params.actual_amount_value,
          });
        else
          query.andWhere('"orders"."actual_amount"::numeric   < :actual_amount_value', {
            actual_amount_value: params.actual_amount_value,
          });
      }
      var count = await query.getCount();
      if (params.page != '0') {
        let skip = (parseInt(params.page) - 1) * 10;
        query.offset(skip);
        query.limit(10);
      }
      const sortOrder = params.sort_order === 'asc' ? 'ASC' : 'DESC';
      const sortField = params.sort_field === 'price' ? '"orders"."total"::numeric' : 'orders.created_at';
      query.orderBy(sortField, sortOrder);
      //  query.orderBy('orders.created_at', 'DESC');
      var product_count = 0;
      // console.log(await query.getQuery());
      const result = await query.getRawMany();

      return {
        invoiceList: result,
        count: count,
      };
    } catch (error) {
      console.log(error);
      throw await errorCodeHandlingsService.getErrorMessage(5006);
    }
  }
Leave a Comment