<?xml version="1.0" encoding="UTF-8" ?>
|
<?DOCTYPE sqls SYSTEM "../config/sql.dtd" ?>
|
|
<sqls>
|
<dataSpace name="stock">
|
|
<sql name = "refreshStatementAccount">
|
<![CDATA[
|
insert into so_account (id, year, month, customer_id, customer_code,
|
customer_name, amt_order, amt_invoice, amt_receipt,
|
amt_statement, account_balance, create_time)
|
select UUID_SHORT(), year(CONCAT(doc_date, '-01')) as year,
|
month(CONCAT(doc_date, '-01')) as month,
|
so_order.customer_id, so_order.customer_code, so_order.customer_name,
|
ifnull(order_amt_total, 0), ifnull(invoice_amt_total, 0),
|
ifnull(receipt_amt_total, 0), ifnull(statement_amt_total, 0),
|
ifnull(receipt_amt_total - statement_amt_total, 0), now()
|
from (select DATE_FORMAT(doc_date, '%Y-%m') as doc_date,
|
customer_id, customer_code, customer_name, ncc_customer_code,
|
sum(ifnull(amt_total, 0)) as order_amt_total
|
from so_order
|
where year(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{year}'
|
and month(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{month}'
|
and state_code not in ('Input', 'Cancel', 'UnderApproval', 'Rejected')
|
and ifnull(amt_total, '') <> ''
|
group by DATE_FORMAT(doc_date, '%Y-%m'), customer_id, customer_code, customer_name, ncc_customer_code) so_order
|
inner join (select customer_id, customer_code, customer_code_ncc,
|
customer_name,
|
sum(ifnull(amt_total, 0)) as invoice_amt_total
|
from so_invoice
|
where year(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{year}'
|
and month(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{month}'
|
and state_code = 'Open' and is_red = 'F'
|
group by customer_id, customer_code, customer_code_ncc, customer_name) so_invoice on so_order.customer_id = so_invoice.customer_id
|
left join (select customer_id, customer_code, customer_code_ncc,
|
customer_name, sum(ifnull(amt, 0)) as receipt_amt_total
|
from so_receipt
|
where year(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{year}'
|
and month(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{month}'
|
and state_code = 'Open' and is_red = 'F'
|
group by customer_id, customer_code, customer_code_ncc, customer_name) so_receipt on so_order.customer_id = so_receipt.customer_id
|
left join (select customer_id, customer_code, customer_code_ncc,
|
customer_name,
|
sum(ifnull(amt_total, 0)) as statement_amt_total
|
from so_statement
|
where year(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{year}'
|
and month(DATE_FORMAT(doc_date, '%Y-%m-%d')) = '@{month}'
|
and state_code = 'Open' and is_red = 'F'
|
group by customer_id, customer_code, customer_code_ncc, customer_name) so_statement on so_order.customer_id = so_statement.customer_id
|
]]>
|
</sql>
|
|
<sql name = "addStatementAccount">
|
<![CDATA[
|
insert into so_account (id, year, month, customer_id, customer_code,
|
customer_name, amt_order, amt_invoice, amt_receipt,
|
amt_statement, account_balance, create_time)
|
select UUID_SHORT(), year(doc_date) as year, month(doc_date) as month,
|
so_order.customer_id, so_order.customer_code, so_order.customer_name,
|
ifnull(order_amt_total, 0), ifnull(invoice_amt_total, 0),
|
ifnull(receipt_amt_total, 0), ifnull(statement_amt_total, 0),
|
ifnull(receipt_amt_total - statement_amt_total, 0), now()
|
from (select DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month), '%Y-%m-%d') as doc_date,
|
customer_id, customer_code, customer_name, ncc_customer_code,
|
sum(ifnull(amt_total, 0)) as order_amt_total
|
from so_order
|
where DATE_FORMAT(doc_date, '%Y-%m') = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month), '%Y-%m')
|
and state_code not in ('Input', 'Cancel', 'UnderApproval', 'Rejected')
|
and (amt_total is not null or amt_total <> '')
|
group by customer_id, customer_code, customer_name, ncc_customer_code) so_order
|
inner join (select customer_id, customer_code, customer_code_ncc,
|
customer_name,
|
sum(ifnull(amt_total, 0)) as invoice_amt_total
|
from so_invoice
|
where DATE_FORMAT(doc_date, '%Y-%m') = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month), '%Y-%m')
|
and state_code = 'Open' and is_red = 'F'
|
group by customer_id, customer_code, customer_code_ncc, customer_name) so_invoice on so_order.customer_id = so_invoice.customer_id
|
left join (select customer_id, customer_code, customer_code_ncc,
|
customer_name, sum(ifnull(amt, 0)) as receipt_amt_total
|
from so_receipt
|
where DATE_FORMAT(doc_date, '%Y-%m') = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month), '%Y-%m')
|
and state_code = 'Open' and is_red = 'F'
|
group by customer_id, customer_code, customer_code_ncc, customer_name) so_receipt on so_order.customer_id = so_receipt.customer_id
|
left join (select customer_id, customer_code, customer_code_ncc,
|
customer_name,
|
sum(ifnull(amt_total, 0)) as statement_amt_total
|
from so_statement
|
where DATE_FORMAT(doc_date, '%Y-%m') = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month), '%Y-%m')
|
and state_code = 'Open' and is_red = 'F'
|
group by customer_id, customer_code, customer_code_ncc, customer_name) so_statement on so_order.customer_id = so_statement.customer_id
|
]]>
|
</sql>
|
|
<sql name = "addBookGeneralChannel">
|
<![CDATA[
|
insert into wm_book_general_channel (
|
id, year, month, org_id, org_code, org_name,
|
product_id, product_code, product_name, material_code, material_name, spec,
|
qty_total, qty_frozen, qty_available, amt, create_time
|
)
|
select UUID_SHORT(), @{year}, @{month},
|
org_id as org_id, org_code as org_code, org_name as org_name,
|
product_id, product_code, product_name, material_code, material_name, spec,
|
sum(qty_total) as qty_total, sum(qty_frozen) as qty_frozen,
|
sum(qty_available) as qty_available,
|
sum(amt) as amt, now()
|
from wm_book_detail
|
group by org_id, org_code, org_name
|
]]>
|
</sql>
|
|
<sql name = "getBookGeneralChannel">
|
<![CDATA[
|
select *
|
from (
|
select *
|
from wm_book_general_channel
|
union all
|
select UUID_SHORT(), '', YEAR(now()) AS year, MONTH (now()) AS month,
|
org_id as org_id, org_code as org_code, org_name as org_name,
|
product_id, product_code, product_name, material_code, material_name, spec,
|
sum(qty_total) as qty_total, sum(qty_frozen) as qty_frozen,
|
sum(qty_available) as qty_available, sum(amt) as amt,
|
now()
|
from wm_book_detail
|
group by org_id, org_code, org_name,
|
product_id, product_code, product_name, material_code, material_name, spec
|
) wm_book_general_channel
|
where @{filter}
|
order by year desc, month desc, org_code
|
@{limit}
|
]]>
|
</sql>
|
|
<sql name = "getBookFlowState">
|
<![CDATA[
|
select distinct doc_type as code, doc_type as name
|
from wm_book_flow
|
]]>
|
</sql>
|
|
<sql name = "getFreezeWarehouseByDetail">
|
<![CDATA[
|
select wm_warehouse.*, wm_warehouse.id as warehouse_id,
|
wm_warehouse.code as warehouse_code,
|
wm_warehouse.name as warehouse_name
|
from wm_warehouse
|
inner join (select *
|
from wm_book_detail
|
where id = '@{id}') wm_book_detail on wm_warehouse.org_id = wm_book_detail.org_id
|
where wm_warehouse.type_code = 'Freeze'
|
]]>
|
</sql>
|
|
<sql name="lockStockBook">
|
update wm_book_general set batch_mark = '@{batch_mark}'
|
where warehouse_id = '@{warehouse_id}'
|
and exists (
|
select 1 from @{detailTableName} document_detail
|
where document_detail.parent_id = '@{document_id}'
|
and document_detail.product_id = wm_book_general.product_id
|
)
|
and wm_book_general.order_right_code = '@{order_right_code}'
|
and wm_book_general.stock_type_code = '@{stock_type_code}'
|
</sql>
|
|
<sql name="getStockFlowBasicFields">
|
<![CDATA[
|
select
|
document_detail.id as doc_detail_id,
|
@{warehouse_id} as warehouse_id, @{warehouse_code} as warehouse_code, @{warehouse_name} as warehouse_name,
|
@{document_doc_type} as document_doc_type, document.code doc_code, document.doc_date,
|
document_detail.product_id, document_detail.product_code,
|
document_detail.product_id material_code, document_detail.product_name material_name,
|
document_detail.batch_no, document_detail.batch_sn, document_detail.valid_from, document_detail.valid_to,
|
@{document_order_right} as document_order_right,
|
@{document_stock_type} as document_stock_type,
|
@{document_qty} as qty,
|
document.bu_name,md_bu.id bu_id
|
from @{detailTableName} document_detail
|
left join @{tableName} document on document_detail.parent_id = document.id
|
left join md_bu on md_bu.name = document.bu_name
|
where @{filter}
|
]]>
|
</sql>
|
|
<sql name="getStockFlowBasicFieldsByImplantCancel">
|
<![CDATA[
|
select
|
document_detail.id as doc_detail_id,
|
@{warehouse_id} as warehouse_id, @{warehouse_code} as warehouse_code, @{warehouse_name} as warehouse_name,
|
@{document_doc_type} as document_doc_type, document.code doc_code, document.doc_date,
|
document_detail.product_id, document_detail.product_code,
|
document_detail.product_id material_code, document_detail.product_name material_name,
|
document_detail.batch_no, document_detail.batch_sn, document_detail.valid_from, document_detail.valid_to,
|
@{document_order_right} as document_order_right, document.bu_id, document.bu_name,
|
@{document_stock_type} as document_stock_type,
|
-@{document_qty} as qty
|
from @{detailTableName} document_detail
|
left join @{tableName} document on document_detail.parent_id = document.host_id
|
where @{filter}
|
]]>
|
</sql>
|
|
<sql name="getFlowFields">
|
<![CDATA[
|
select flow.*, null qty
|
from @{tableName} flow
|
where 1 <> 1
|
]]>
|
</sql>
|
|
<sql name="writeStockFlowBookFields">
|
<![CDATA[
|
update wm_book_flow book_flow
|
left join wm_book_detail book_detail
|
on book_detail.warehouse_id = book_flow.warehouse_id
|
and book_detail.product_id = book_flow.product_id
|
and ifnull(book_detail.batch_no, '') = ifnull(book_flow.batch_no, '')
|
and ifnull(book_detail.batch_sn, '') = ifnull(book_flow.batch_sn, '')
|
and ifnull(book_detail.valid_from, '') = ifnull(book_flow.valid_from, '')
|
and ifnull(book_detail.valid_to, '') = ifnull(book_flow.valid_to, '')
|
and book_detail.order_right_code = book_flow.order_right_code
|
and book_detail.stock_type_code = book_flow.stock_type_code
|
left join md_prod_sku on book_flow.product_id = md_prod_sku.id
|
left join wm_order_right on book_flow.order_right_code = wm_order_right.code
|
left join wm_stock_type on book_flow.stock_type_code = wm_stock_type.code
|
set book_flow.book_detail_id = book_detail.id,
|
book_flow.record_operator = IF( book_detail.id IS NULL, 'insert', 'update' ),
|
book_flow.product_code = md_prod_sku.product_code,
|
book_flow.product_name = md_prod_sku.product_name,
|
book_flow.spec = md_prod_sku.spec,
|
book_flow.order_right_name = wm_order_right.name,
|
book_flow.stock_type_name = wm_stock_type.name
|
where book_flow.batch_mark = '@{batch_mark}'
|
]]>
|
</sql>
|
|
<sql name="writeInsertStockDetail">
|
<![CDATA[
|
insert into wm_book_detail (
|
batch_mark, id, bu_id,bu_name,
|
org_id, org_code, org_name, account_id,
|
warehouse_id, warehouse_code, warehouse_name,
|
product_id, product_code, product_name, batch_no, batch_sn,
|
valid_from, valid_to, order_right_code, stock_type_code,
|
qty_total, qty_frozen, qty_available, type_code, type_name,
|
create_time, update_time
|
)
|
select '@{batch_mark}' as batch_mark, UUID_SHORT() as id, bu_id,bu_name,
|
'@{org_id}', '@{org_code}', '@{org_name}',account_id,
|
warehouse_id, warehouse_code, warehouse_name,
|
product_id, product_code, product_name, batch_no, batch_sn,
|
valid_from, valid_to, order_right_code, stock_type_code,
|
sum_total, sum_frozen, sum_total - sum_frozen, type_code, type_name,
|
@{create_time}, @{update_time}
|
from (
|
select
|
bu_id,bu_name,account_id,
|
warehouse_id, warehouse_code, warehouse_name,
|
product_id, product_code, product_name,
|
batch_no, batch_sn, valid_from, valid_to,
|
order_right_code, stock_type_code, type_code, type_name,
|
sum(IFNULL(qty_add, 0)) - sum(IFNULL(qty_delete, 0)) as sum_total,
|
sum(IFNULL(qty_freeze, 0)) - sum(IFNULL(qty_unfreeze, 0)) as sum_frozen
|
from wm_book_flow
|
where batch_mark = '@{batch_mark}' and record_operator = 'insert'
|
group by warehouse_id, warehouse_code, warehouse_name, product_id, batch_no, valid_from, valid_to, batch_sn, order_right_code, stock_type_code, type_code, type_name
|
) book_detail
|
]]>
|
</sql>
|
|
<sql name="getNegativeStockDetail">
|
<![CDATA[
|
select *
|
from wm_book_detail
|
where batch_mark = '@{batch_mark}'
|
and ((qty_total < 0) or (qty_available < 0) or (qty_total < qty_frozen));
|
]]>
|
</sql>
|
|
<sql name="resetStockDetailOtherFields">
|
<![CDATA[
|
update wm_book_detail book_detail
|
left join md_prod_sku on book_detail.product_id = md_prod_sku.id
|
left join wm_order_right on book_detail.order_right_code = wm_order_right.code
|
left join wm_stock_type on book_detail.stock_type_code = wm_stock_type.code
|
set book_detail.product_code = md_prod_sku.product_code,
|
book_detail.product_name = md_prod_sku.product_name,
|
book_detail.spec = md_prod_sku.spec,
|
book_detail.unit = md_prod_sku.unit,
|
book_detail.order_right_name = wm_order_right.name,
|
book_detail.stock_type_name = wm_stock_type.name,
|
book_detail.qty_available = book_detail.qty_total - book_detail.qty_frozen
|
where book_detail.batch_mark = '@{batch_mark}'
|
]]>
|
</sql>
|
|
<sql name="writeUpdateStockDetail">
|
<![CDATA[
|
update wm_book_detail book_detail
|
inner join (
|
select book_detail_id,
|
(sum(IFNULL(qty_add, 0)) - sum(IFNULL(qty_delete, 0))) as sum_total_change,
|
(sum(IFNULL(qty_freeze, 0)) - sum(IFNULL(qty_unfreeze, 0))) as sum_frozen_change
|
from wm_book_flow
|
where batch_mark = '@{batch_mark}' and record_operator = 'update'
|
group by book_detail_id
|
) book_flow
|
on book_flow.book_detail_id = book_detail.id
|
set book_detail.qty_total = book_detail.qty_total + sum_total_change,
|
book_detail.qty_frozen = book_detail.qty_frozen + sum_frozen_change,
|
book_detail.batch_mark = '@{batch_mark}',
|
book_detail.update_time = @{update_time}
|
where exists (
|
select 1 from wm_book_flow
|
where wm_book_flow.book_detail_id = book_detail.id
|
and wm_book_flow.batch_mark = '@{batch_mark}'
|
)
|
]]>
|
</sql>
|
|
<sql name="writeDeleteStockDetail">
|
delete from wm_book_detail
|
where batch_mark = '@{batch_mark}' and qty_total = 0;
|
</sql>
|
|
<sql name="writeInsertStockGeneral">
|
<![CDATA[
|
insert into wm_book_general (
|
id,
|
org_id, org_code, org_name,
|
warehouse_id, warehouse_code, warehouse_name,
|
product_id, order_right_code, stock_type_code,
|
qty_total, qty_frozen, qty_available,
|
create_time, update_time
|
)
|
select UUID_SHORT(),
|
'@{org_id}', '@{org_code}', '@{org_name}',
|
book_detail.warehouse_id, book_detail.warehouse_code, book_detail.warehouse_name,
|
book_detail.product_id,
|
book_detail.order_right_code, book_detail.stock_type_code,
|
book_detail.qty_total, book_detail.qty_frozen,
|
book_detail.qty_total - book_detail.qty_frozen as qty_available,
|
@{create_time}, @{update_time}
|
from (
|
select org_id,
|
warehouse_id, warehouse_code, warehouse_name,
|
product_id, order_right_code, stock_type_code,
|
sum(qty_total) as qty_total, sum(qty_frozen) as qty_frozen
|
from wm_book_detail
|
where batch_mark = '@{batch_mark}'
|
group by org_id, warehouse_id, warehouse_code, warehouse_name, product_id, valid_from, valid_to, order_right_code, stock_type_code
|
) book_detail
|
left join wm_book_general book_general
|
on book_general.org_id = book_detail.org_id
|
and book_general.warehouse_id = book_detail.warehouse_id
|
and book_general.product_id = book_detail.product_id
|
and book_general.order_right_code = book_detail.order_right_code
|
and book_general.stock_type_code = book_general.stock_type_code
|
where book_detail.org_id = '@{org_id}'
|
and book_detail.warehouse_id = '@{warehouse_id}'
|
and book_general.id is null;
|
]]>
|
</sql>
|
|
<sql name="resetStockGeneralOtherFields">
|
<![CDATA[
|
update wm_book_general book_general
|
left join md_sku on book_general.product_id = md_sku.id
|
left join wm_order_right on book_general.order_right_code = wm_order_right.code
|
left join wm_stock_type on book_general.stock_type_code = wm_stock_type.code
|
set book_general.category_code = md_sku.authorize_category_code,
|
book_general.category_name = md_sku.authorize_category_name,
|
book_general.product_code = md_sku.code,
|
book_general.product_name = md_sku.name,
|
book_general.material_code = md_sku.material_code,
|
book_general.material_name = md_sku.material_name,
|
book_general.order_right_name = wm_order_right.name,
|
book_general.stock_type_name = wm_stock_type.name,
|
book_general.qty_available = book_general.qty_total - book_general.qty_frozen
|
where book_general.batch_mark = '@{batch_mark}'
|
]]>
|
</sql>
|
|
<sql name="writeUpdateStockGeneral">
|
<![CDATA[
|
update wm_book_general book_general
|
set book_general.qty_total = (
|
select sum(qty_total) from wm_book_detail book_detail
|
where book_general.warehouse_id = book_detail.warehouse_id
|
and book_general.product_id = book_detail.product_id
|
and book_general.order_right_code = book_detail.order_right_code
|
and book_general.stock_type_code = book_general.stock_type_code
|
),
|
book_general.qty_frozen = (
|
select sum(qty_frozen) from wm_book_detail book_detail
|
where book_general.warehouse_id = book_detail.warehouse_id
|
and book_general.product_id = book_detail.product_id
|
and book_general.order_right_code = book_detail.order_right_code
|
and book_general.stock_type_code = book_general.stock_type_code
|
),
|
book_general.batch_mark = '@{batch_mark}',
|
book_general.update_time = @{update_time}
|
where exists (
|
select 1 from wm_book_detail book_detail
|
where book_detail.batch_mark = '@{batch_mark}'
|
and book_general.warehouse_id = book_detail.warehouse_id
|
and book_general.product_id = book_detail.product_id
|
and book_general.order_right_code = book_detail.order_right_code
|
and book_general.stock_type_code = book_general.stock_type_code
|
);
|
]]>
|
</sql>
|
|
<sql name="writeDeleteStockGeneral">
|
delete from wm_book_general
|
where batch_mark = '@{batch_mark}' and qty_total = 0;
|
</sql>
|
|
</dataSpace>
|
|
<dataSpace name="stock-validator">
|
<sql name="getNotEnoughImplantDetails">
|
<![CDATA[
|
select so_implant_detail.*, wm_book_detail.qty_available
|
from so_implant_detail
|
left join wm_book_detail
|
on so_implant_detail.warehouse_id = wm_book_detail.warehouse_id
|
and so_implant_detail.product_id = wm_book_detail.product_id
|
and so_implant_detail.order_right_code = wm_book_detail.order_right_code
|
and so_implant_detail.stock_type_code = wm_book_detail.stock_type_code
|
and ifnull(so_implant_detail.batch_no, '') = ifnull(wm_book_detail.batch_no, '')
|
and ifnull(so_implant_detail.batch_sn, '') = ifnull(wm_book_detail.batch_sn, '')
|
and ifnull(so_implant_detail.valid_from, '') = ifnull(wm_book_detail.valid_from, '')
|
and ifnull(so_implant_detail.valid_to, '') = ifnull(wm_book_detail.valid_to, '')
|
where parent_id = '@{document_id}'
|
and so_implant_detail.qty > IFNull(wm_book_detail.qty_available, 0)
|
]]>
|
</sql>
|
</dataSpace>
|
|
</sqls>
|