feat: 运输单分组计算接口 feat #726 (#728)

Co-authored-by: lyx <2027667395@qq.com>
Reviewed-on: daoyoucloud/tachycode#728
Co-authored-by: hello@lv <2256334253@qq.com>
Co-committed-by: hello@lv <2256334253@qq.com>
This commit is contained in:
hello@lv 2024-04-16 15:01:00 +08:00 committed by sealday
parent 5e215fae7e
commit c92e1953d3
2 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
"@hera/plugin-rental": patch
---
运输单分组计算接口

View File

@ -4,6 +4,7 @@ import { SqlLoader } from '@hera/plugin-core';
import { Action, Controller, Inject } from '@nocobase/utils';
import { QueryTypes } from 'sequelize';
import { Waybill } from '../../interfaces/waybill';
import { FilterParser } from '@nocobase/database';
@Controller('waybills')
export class WaybillsController {
@ -34,4 +35,94 @@ export class WaybillsController {
}
ctx.body = await renderWaybill(waybills[0] as Waybill, settings);
}
@Action('group')
async group(ctx: Context) {
const { filter } = ctx.action.params.values;
const collectionName = 'waybills';
const collection = ctx.db.getCollection(collectionName);
const fields = collection.fields;
const filterParser = new FilterParser(filter, {
collection,
});
const { sequelize } = ctx.db;
const { where, include } = filterParser.toSequelizeParams();
const parsedFilterInclude =
include?.map((item) => {
if (fields.get(item.association)?.type === 'belongsToMany') {
item.through = { attributes: [] };
}
return item;
}) || [];
const query = {
where: where || {},
attributes: [
[
sequelize.fn(
'sum',
sequelize.cast(
sequelize.literal(
'COALESCE(weight_or_amount, 0) * COALESCE(unit_price, 0) + COALESCE(additional_cost, 0)',
),
'double precision',
),
),
'price',
],
[sequelize.col('carrier_id'), 'carrier_id'],
[sequelize.col('carrier.name'), 'name'],
[sequelize.col('paid'), 'paid'],
[sequelize.col('checked'), 'checked'],
],
include: [
{
association: 'carrier',
attributes: [],
},
...parsedFilterInclude,
],
// 承运商,付款,结清
group: [
sequelize.col('carrier.name'),
sequelize.col('carrier_id'),
sequelize.col('paid'),
sequelize.col('checked'),
],
order: [],
subQuery: false,
raw: true,
} as any;
// 付款
const records = await ctx.db.getModel('waybills').findAll(query);
const data = {
labels: ['收款人', '小计', '已结清款', '未结清款', '已核对款', '未核对款'],
values: [],
};
const all = ['合计', 0, 0, 0, 0, 0];
records.forEach((item) => {
let res = data.values.find((r) => r[0] === item.name);
if (!res) {
res = [item.name, 0, 0, 0, 0, 0];
data.values.push(res);
}
res[1] += item.price; // 小计
all[1] += item.price;
if (item.paid) {
res[2] += item.price; // 已结清款
all[2] += item.price;
} else {
res[3] += item.price; // 未结清款
all[3] += item.price;
}
if (item.checked) {
res[4] += item.price; // 已核对款
all[4] += item.price;
} else {
res[5] += item.price; // 未核对款
all[5] += item.price;
}
});
data.values.push(all);
ctx.body = data;
}
}