"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.manifestReport = void 0;
exports.manifestReport = {
    async manifestReport(_, params, context) {
        try {
            const { tx } = context.state;
            let bizplace = params.filters.find(items => items.name === 'bizplace');
            let trackingNo = params.filters.findIndex(items => items.name === 'trackingNo');
            let releaseGoodNo = params.filters.find(items => items.name === 'releaseGoodNo');
            let fromDate = params.filters.find(items => items.name === 'fromDate');
            let toDate = params.filters.find(items => items.name === 'toDate');
            let hasTrackingNo = params.filters.find(items => items.name === 'hasTrackingNo');
            if (!bizplace)
                throw 'Invalid input';
            let hasTrackingNoFilter = '';
            if ((hasTrackingNo === null || hasTrackingNo === void 0 ? void 0 : hasTrackingNo.value) == true) {
                hasTrackingNoFilter += `AND op.tracking_no NOTNULL`;
            }
            else if ((hasTrackingNo === null || hasTrackingNo === void 0 ? void 0 : hasTrackingNo.value) == false) {
                hasTrackingNoFilter += `AND op.tracking_no ISNULL`;
            }
            let releaseGoodFilter = '';
            if (releaseGoodNo) {
                releaseGoodFilter += `AND rg.name = '${releaseGoodNo.value}'`;
            }
            let trackingNoFilterValue;
            let trackingNoFilter = '';
            if (trackingNo >= 0) {
                trackingNoFilterValue = params.filters[trackingNo].value
                    .split(',')
                    .map(value => `'${value.trim().toUpperCase()}'`);
                params.filters.splice(trackingNo, 1);
                trackingNoFilter += `AND op.tracking_no in (${trackingNoFilterValue})`;
            }
            await tx.query(`
					CREATE TEMP TABLE manifest_report ON COMMIT DROP AS (
						SELECT 
							rg.created_at AS "ordered_at", 
							rg.status, 
							op.tracking_no,
							m.name AS "manifest_no", 
							m.created_at AS "manifested_at", 
							op.transporter AS "transporter",
							u.name AS "manifested_by",
							b.name AS "company",
							rg.ref_no AS "ref_no",
							rg.name AS "ro_no",
							op.gross_weight
						FROM order_packages op 
						LEFT JOIN 
							manifests m on m.id = op.manifest_id 
						LEFT JOIN 
							users u on u.id = op.manifested_by_id
						INNER JOIN 
							bizplaces b on op.bizplace_id = b.id 
						INNER JOIN
							release_goods rg on op.release_good_id = rg.id
						WHERE
							op.domain_id = $1
							AND op.bizplace_id = '${bizplace.value}'
							AND op.created_at >= $2
							AND op.created_at <= $3
							${releaseGoodFilter}
							${trackingNoFilter}
							${hasTrackingNoFilter}
					)
				`, [context.state.domain.id, fromDate.value, toDate.value]);
            const total = await tx.query(`
				SELECT count(*) FROM manifest_report
			`);
            const result = await tx.query(`
				SELECT * FROM manifest_report
				offset $1 limit $2
			`, [(params.pagination.page - 1) * params.pagination.limit, params.pagination.limit]);
            let items = result.map(itm => {
                return {
                    bizplace: itm.company,
                    refNo: itm.ref_no,
                    roNo: itm.ro_no,
                    status: itm.status,
                    trackingNo: itm.tracking_no,
                    manifestNo: itm.manifest_no,
                    manifestedAt: itm.manifested_at,
                    manifestedBy: itm.manifested_by,
                    orderedAt: itm.ordered_at,
                    transporter: itm.transporter,
                    grossWeight: itm.gross_weight
                };
            });
            return { items, total: total[0].count };
        }
        catch (e) {
            throw e;
        }
    }
};
//# sourceMappingURL=manifest-report.js.map