<?xml version="1.0" encoding="UTF-8" ?>
|
|
<sqls>
|
<dataSpace name="report-action">
|
|
<!-- 聚乾进销存 rep_supplier_stock_flow -->
|
<sql name="appendSupplierPurDimension">
|
<![CDATA[
|
INSERT INTO rep_supplier_stock_flow (
|
id, idx, doc_year, doc_month, doc_monthno, doc_date, supplier_id, bu_id, product_id, sku_id,
|
qty_pur, qty_sale, qty_inv, qty_implant)
|
select
|
md5(UUID_SHORT()) id, null idx, YEAR(dimension.doc_date) doc_year, DATE_FORMAT(dimension.doc_date, '%m') doc_month,
|
DATE_FORMAT(dimension.doc_date, '%Y%m') doc_monthno, dimension.*, 0, 0, 0, 0
|
from(
|
select doc.actual_recevied_time doc_date, doc.supplier_id, md_bu_product.bu_id, doc_detail.product_id, sku_id
|
from so_delivery doc
|
inner join so_delivery_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_bu_product on md_bu_product.product_id = doc_detail.product_id
|
group by doc.actual_recevied_time, doc.supplier_id, md_bu_product.bu_id, doc_detail.product_id, sku_id, md_bu_product.bu_id) dimension
|
WHERE not exists (
|
select 1 from rep_supplier_stock_flow
|
where dimension.doc_date = rep_supplier_stock_flow.doc_date
|
and dimension.supplier_id = rep_supplier_stock_flow.supplier_id
|
and dimension.product_id = rep_supplier_stock_flow.product_id
|
and dimension.sku_id = rep_supplier_stock_flow.sku_id
|
and dimension.bu_id = rep_supplier_stock_flow.bu_id)
|
]]>
|
</sql>
|
|
<sql name="calculateSupplierPur">
|
<![CDATA[
|
update rep_supplier_stock_flow
|
set qty_pur = qty_pur + (
|
select qty from (
|
select doc.actual_recevied_time doc_date, supplier_id, md_bu_product.bu_id, doc_detail.product_id, sku_id, sum(doc_detail.qty) qty
|
from so_delivery doc
|
left join so_delivery_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_bu_product on md_bu_product.product_id = doc_detail.product_id
|
group by doc.actual_recevied_time, supplier_id, md_bu_product.bu_id, doc_detail.product_id, sku_id ) doc
|
where doc.doc_date = rep_supplier_stock_flow.doc_date and doc.supplier_id = rep_supplier_stock_flow.supplier_id
|
and doc.product_id = rep_supplier_stock_flow.product_id and doc.sku_id = rep_supplier_stock_flow.sku_id
|
and doc.bu_id = rep_supplier_stock_flow.bu_id)
|
where exists ( select 1 from (
|
select doc.actual_recevied_time doc_date, supplier_id, md_bu_product.bu_id, doc_detail.product_id, sku_id, sum(doc_detail.qty) qty
|
from so_delivery doc
|
left join so_delivery_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_bu_product on md_bu_product.product_id = doc_detail.product_id
|
group by doc.actual_recevied_time, supplier_id, md_bu_product.bu_id, doc_detail.product_id, sku_id ) doc
|
where doc.doc_date = rep_supplier_stock_flow.doc_date and doc.supplier_id = rep_supplier_stock_flow.supplier_id
|
and doc.product_id = rep_supplier_stock_flow.product_id and doc.sku_id = rep_supplier_stock_flow.sku_id
|
and doc.bu_id = rep_supplier_stock_flow.bu_id )
|
]]>
|
</sql>
|
|
<sql name="appendSupplierPurReturnDimension">
|
<![CDATA[
|
INSERT INTO rep_supplier_stock_flow (
|
id, idx, doc_year, doc_month, doc_monthno, doc_date, supplier_id, bu_id, product_id, sku_id,qty_pur, qty_sale, qty_inv, qty_implant)
|
select md5(UUID_SHORT()) id, null idx, YEAR(dimension.doc_date) doc_year,
|
DATE_FORMAT(dimension.doc_date, '%m') doc_month, DATE_FORMAT(dimension.doc_date, '%Y%m') doc_monthno, dimension.*, 0, 0, 0, 0
|
from(
|
select doc.actual_delivery_date doc_date, doc.supplier_id, md_bu_product.bu_id, doc_detail.product_id, sku_id
|
from so_sales_return doc
|
left join so_sales_return_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_bu_product on md_bu_product.product_id = doc_detail.product_id
|
group by doc.actual_delivery_date, doc.supplier_id, md_bu_product.bu_id, doc_detail.product_id, sku_id) dimension
|
WHERE not exists (
|
select 1 from rep_supplier_stock_flow
|
where dimension.doc_date = rep_supplier_stock_flow.doc_date
|
and dimension.supplier_id = rep_supplier_stock_flow.supplier_id
|
and dimension.product_id = rep_supplier_stock_flow.product_id
|
and dimension.sku_id = rep_supplier_stock_flow.sku_id
|
and dimension.bu_id = rep_supplier_stock_flow.bu_id )
|
]]>
|
</sql>
|
|
<sql name="calculateSupplierPurReturn">
|
<![CDATA[
|
update rep_supplier_stock_flow
|
set qty_pur = qty_pur - (select qty from (
|
select doc.actual_delivery_date doc_date, supplier_id, md_bu_product.bu_id, doc_detail.product_id, sku_id, sum(doc_detail.qty) qty
|
from so_sales_return doc
|
left join so_sales_return_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_bu_product on md_bu_product.product_id = doc_detail.product_id
|
group by doc.actual_delivery_date, supplier_id, md_bu_product.bu_id, doc_detail.product_id, sku_id) doc
|
where doc.doc_date = rep_supplier_stock_flow.doc_date and doc.supplier_id = rep_supplier_stock_flow.supplier_id
|
and doc.product_id = rep_supplier_stock_flow.product_id and doc.sku_id = rep_supplier_stock_flow.sku_id
|
and doc.bu_id = rep_supplier_stock_flow.bu_id )
|
where exists (select 1 from (
|
select doc.actual_delivery_date doc_date, supplier_id, md_bu_product.bu_id, doc_detail.product_id, sku_id, sum(doc_detail.qty) qty
|
from so_sales_return doc
|
left join so_sales_return_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_bu_product on md_bu_product.product_id = doc_detail.product_id
|
group by doc.actual_delivery_date, supplier_id, md_bu_product.bu_id, doc_detail.product_id, sku_id) doc
|
where doc.doc_date = rep_supplier_stock_flow.doc_date and doc.supplier_id = rep_supplier_stock_flow.supplier_id
|
and doc.product_id = rep_supplier_stock_flow.product_id and doc.sku_id = rep_supplier_stock_flow.sku_id
|
and doc.bu_id = rep_supplier_stock_flow.bu_id )
|
]]>
|
</sql>
|
|
<sql name="appendSupplierImplantDimension">
|
<![CDATA[
|
INSERT INTO rep_supplier_stock_flow (id, idx, doc_year, doc_month, doc_monthno, doc_date,
|
supplier_id, bu_id, product_id, sku_id, qty_pur, qty_sale, qty_inv, qty_implant)
|
select
|
md5(UUID_SHORT()) id, null idx, YEAR(dimension.doc_date) doc_year, DATE_FORMAT(dimension.doc_date, '%m') doc_month,
|
DATE_FORMAT(dimension.doc_date, '%Y%m') doc_monthno, dimension.*, 0, 0, 0, 0
|
from(
|
select doc.doc_date, md_prod_sku.supplier_id, doc.bu_id, doc_detail.product_id, doc_detail.sku_id from so_implant doc
|
left join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_prod_sku on md_prod_sku.id = doc_detail.sku_id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, md_prod_sku.supplier_id, doc.bu_id, product_id, sku_id) dimension
|
WHERE not exists (
|
select 1 from rep_supplier_stock_flow
|
where dimension.doc_date = rep_supplier_stock_flow.doc_date
|
and dimension.supplier_id = rep_supplier_stock_flow.supplier_id
|
and dimension.product_id = rep_supplier_stock_flow.product_id
|
and dimension.sku_id = rep_supplier_stock_flow.sku_id
|
and dimension.bu_id = rep_supplier_stock_flow.bu_id)
|
]]>
|
</sql>
|
|
<sql name="calculateSupplierImplant">
|
<![CDATA[
|
update rep_supplier_stock_flow
|
set qty_implant = qty_implant + (select qty from (
|
select doc.doc_date, supplier_id, doc.bu_id, doc_detail.product_id, sku_id, sum(doc_detail.qty) qty
|
from so_implant doc
|
left join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_prod_sku on md_prod_sku.id = doc_detail.sku_id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, md_prod_sku.supplier_id, doc.bu_id, doc_detail.product_id, sku_id) doc
|
where doc.doc_date = rep_supplier_stock_flow.doc_date and doc.supplier_id = rep_supplier_stock_flow.supplier_id
|
and doc.product_id = rep_supplier_stock_flow.product_id and doc.sku_id = rep_supplier_stock_flow.sku_id
|
and doc.bu_id = rep_supplier_stock_flow.bu_id )
|
where exists (select 1 from (
|
select doc.doc_date, supplier_id, doc.bu_id, doc_detail.product_id, sku_id, sum(doc_detail.qty) qty
|
from so_implant doc
|
left join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_prod_sku on md_prod_sku.id = doc_detail.sku_id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, md_prod_sku.supplier_id, doc.bu_id, doc_detail.product_id, sku_id ) doc
|
where doc.doc_date = rep_supplier_stock_flow.doc_date and doc.supplier_id = rep_supplier_stock_flow.supplier_id
|
and doc.product_id = rep_supplier_stock_flow.product_id and doc.sku_id = rep_supplier_stock_flow.sku_id
|
and doc.bu_id = rep_supplier_stock_flow.bu_id)
|
]]>
|
</sql>
|
|
<sql name="appendSupplierSaleDimension">
|
<![CDATA[
|
INSERT INTO rep_supplier_stock_flow (id, idx, doc_year, doc_month, doc_monthno, doc_date,
|
supplier_id, bu_id, product_id, sku_id, qty_pur, qty_sale, qty_implant, qty_inv)
|
select md5(UUID_SHORT()) id, null idx, YEAR(dimension.doc_date) doc_year, DATE_FORMAT(dimension.doc_date, '%m') doc_month,
|
DATE_FORMAT(dimension.doc_date, '%Y%m') doc_monthno, dimension.*, 0, 0, 0, 0
|
from (
|
select doc.doc_date, md_prod_sku.supplier_id, doc_detail.bu_id, doc_detail.product_id, sku_id
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_prod_sku on md_prod_sku.id = doc_detail.sku_id
|
where doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.doc_date, md_prod_sku.supplier_id, doc_detail.bu_id, doc_detail.product_id, sku_id) dimension
|
WHERE not exists (
|
select 1 from rep_supplier_stock_flow
|
where dimension.doc_date = rep_supplier_stock_flow.doc_date
|
and dimension.supplier_id = rep_supplier_stock_flow.supplier_id
|
and dimension.product_id = rep_supplier_stock_flow.product_id
|
and dimension.sku_id = rep_supplier_stock_flow.sku_id
|
and dimension.bu_id = rep_supplier_stock_flow.bu_id )
|
]]>
|
</sql>
|
|
<sql name="calculateSupplierSale">
|
<![CDATA[
|
update rep_supplier_stock_flow
|
set qty_sale = qty_sale + (select qty from (
|
select so_order.doc_date, supplier_id, so_order_detail.bu_id, so_order_detail.product_id, sku_id, sum(so_order_detail.qty) qty
|
from so_order
|
left join so_order_detail on so_order_detail.parent_id = so_order.id
|
left join md_prod_sku on md_prod_sku.id = so_order_detail.sku_id
|
where so_order.state_code = 'Open' and so_order_detail.is_delete = '0'
|
group by so_order.doc_date, md_prod_sku.supplier_id, so_order_detail.bu_id, so_order_detail.product_id, sku_id ) doc
|
where doc.doc_date = rep_supplier_stock_flow.doc_date and doc.supplier_id = rep_supplier_stock_flow.supplier_id
|
and doc.product_id = rep_supplier_stock_flow.product_id and doc.sku_id = rep_supplier_stock_flow.sku_id
|
and doc.bu_id = rep_supplier_stock_flow.bu_id )
|
where exists (select 1 from (
|
select so_order.doc_date, supplier_id, so_order_detail.bu_id, md_prod_sku.product_id, sku_id, sum(so_order_detail.qty) qty from so_order
|
left join so_order_detail on so_order_detail.parent_id = so_order.id
|
left join md_prod_sku on md_prod_sku.id = so_order_detail.sku_id
|
where so_order.state_code = 'Open' and so_order_detail.is_delete = '0'
|
group by so_order.doc_date, md_prod_sku.supplier_id, so_order_detail.bu_id, product_id, sku_id ) doc
|
where doc.doc_date = rep_supplier_stock_flow.doc_date and doc.supplier_id = rep_supplier_stock_flow.supplier_id
|
and doc.product_id = rep_supplier_stock_flow.product_id and doc.sku_id = rep_supplier_stock_flow.sku_id
|
and doc.bu_id = rep_supplier_stock_flow.bu_id )
|
]]>
|
</sql>
|
|
<sql name="appendSupplierSaleReturnDimension">
|
<![CDATA[
|
INSERT INTO rep_supplier_stock_flow (id, idx, doc_year, doc_month, doc_monthno, doc_date,
|
supplier_id, bu_id, product_id, sku_id, qty_pur, qty_sale, qty_implant, qty_inv)
|
select md5(UUID_SHORT()) id, null idx, YEAR(dimension.doc_date) doc_year, DATE_FORMAT(dimension.doc_date, '%m') doc_month,
|
DATE_FORMAT(dimension.doc_date, '%Y%m') doc_monthno, dimension.*, 0, 0, 0, 0
|
from (
|
select doc.doc_date, md_prod_sku.supplier_id, doc_detail.bu_id, doc_detail.product_id, sku_id
|
from so_order_return doc
|
inner join so_order_return_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_prod_sku on md_prod_sku.id = doc_detail.sku_id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, md_prod_sku.supplier_id, doc_detail.bu_id, doc_detail.product_id, sku_id) dimension
|
WHERE not exists (
|
select 1 from rep_supplier_stock_flow
|
where dimension.doc_date = rep_supplier_stock_flow.doc_date
|
and dimension.supplier_id = rep_supplier_stock_flow.supplier_id
|
and dimension.product_id = rep_supplier_stock_flow.product_id
|
and dimension.sku_id = rep_supplier_stock_flow.sku_id
|
and dimension.bu_id = rep_supplier_stock_flow.bu_id)
|
]]>
|
</sql>
|
|
<sql name="calculateSupplierSaleReturn">
|
<![CDATA[
|
update rep_supplier_stock_flow
|
set qty_sale = qty_sale - (select qty from (
|
select so_order_return.doc_date, supplier_id, so_order_return_detail.bu_id, so_order_return_detail.product_id, sku_id, sum(so_order_return_detail.qty) qty from so_order_return
|
left join so_order_return_detail on so_order_return_detail.parent_id = so_order_return.id
|
left join md_prod_sku on md_prod_sku.id = so_order_return_detail.sku_id
|
where so_order_return.state_code = 'Open'
|
group by so_order_return.doc_date, md_prod_sku.supplier_id, so_order_return_detail.bu_id, so_order_return_detail.product_id, sku_id ) doc
|
where doc.doc_date = rep_supplier_stock_flow.doc_date and doc.supplier_id = rep_supplier_stock_flow.supplier_id
|
and doc.product_id = rep_supplier_stock_flow.product_id and doc.sku_id = rep_supplier_stock_flow.sku_id
|
and doc.bu_id = rep_supplier_stock_flow.bu_id )
|
where exists (select 1 from (
|
select so_order_return.doc_date, supplier_id, so_order_return_detail.bu_id, so_order_return_detail.product_id, sku_id, sum(so_order_return_detail.qty) qty from so_order_return
|
left join so_order_return_detail on so_order_return_detail.parent_id = so_order_return.id
|
left join md_prod_sku on md_prod_sku.id = so_order_return_detail.sku_id
|
where so_order_return.state_code = 'Open'
|
group by so_order_return.doc_date, md_prod_sku.supplier_id, so_order_return_detail.bu_id, so_order_return_detail.product_id, sku_id ) doc
|
where doc.doc_date = rep_supplier_stock_flow.doc_date and doc.supplier_id = rep_supplier_stock_flow.supplier_id
|
and doc.product_id = rep_supplier_stock_flow.product_id and doc.sku_id = rep_supplier_stock_flow.sku_id
|
and doc.bu_id = rep_supplier_stock_flow.bu_id )
|
]]>
|
</sql>
|
|
<sql name="calculateSupplierInv">
|
<![CDATA[
|
update rep_supplier_stock_flow
|
set qty_inv = qty_pur - qty_sale
|
]]>
|
</sql>
|
|
<!-- 经销商进销存rep_customer_stock_flow -->
|
<sql name="appendCustomerPurDimension">
|
<![CDATA[
|
INSERT INTO rep_customer_stock_flow (id, idx, doc_year, doc_month, doc_monthno, doc_date,
|
customer_id, product_id, sku_id, qty_pur, qty_transfer_in, qty_transfer_out, qty_implant)
|
select md5(UUID_SHORT()) id, null idx, YEAR(dimension.doc_date) doc_month, DATE_FORMAT(dimension.doc_date, '%m') doc_month,
|
DATE_FORMAT(dimension.doc_date, '%Y%m') doc_monthno, dimension.*, 0, 0, 0, 0
|
from (
|
select doc.doc_date, doc.customer_id, product_id, sku_id from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.doc_date, doc.customer_id, product_id, sku_id) dimension
|
WHERE not exists (
|
select 1 from rep_customer_stock_flow
|
where dimension.doc_date = rep_customer_stock_flow.doc_date
|
and dimension.customer_id = rep_customer_stock_flow.customer_id
|
and dimension.product_id = rep_customer_stock_flow.product_id
|
and dimension.sku_id = rep_customer_stock_flow.sku_id )
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPur">
|
<![CDATA[
|
update rep_customer_stock_flow
|
set qty_pur = qty_pur + (select qty from (
|
select so_order.doc_date, customer_id, product_id, sku_id, sum(so_order_detail.qty) qty from so_order
|
left join so_order_detail on so_order_detail.parent_id = so_order.id
|
where so_order.state_code = 'Open' and so_order_detail.is_delete = '0'
|
group by so_order.doc_date, customer_id, product_id, sku_id ) doc
|
where doc.doc_date = rep_customer_stock_flow.doc_date and doc.customer_id = rep_customer_stock_flow.customer_id
|
and doc.product_id = rep_customer_stock_flow.product_id and doc.sku_id = rep_customer_stock_flow.sku_id )
|
where exists (select 1 from (
|
select so_order.doc_date, customer_id, product_id, sku_id, sum(so_order_detail.qty) qty from so_order
|
inner join so_order_detail on so_order_detail.parent_id = so_order.id
|
where so_order.state_code = 'Open' and so_order_detail.is_delete = '0'
|
group by so_order.doc_date, customer_id, product_id, sku_id ) doc
|
where doc.doc_date = rep_customer_stock_flow.doc_date and doc.customer_id = rep_customer_stock_flow.customer_id
|
and doc.product_id = rep_customer_stock_flow.product_id and doc.sku_id = rep_customer_stock_flow.sku_id)
|
]]>
|
</sql>
|
|
<sql name="appendCustomerPurReturnDimension">
|
<![CDATA[
|
INSERT INTO rep_customer_stock_flow (
|
id, idx, doc_year, doc_month, doc_monthno, doc_date, customer_id,
|
product_id, sku_id, qty_pur, qty_transfer_in, qty_transfer_out, qty_implant)
|
select
|
md5(UUID_SHORT()) id, null idx, YEAR(dimension.doc_date) doc_month, DATE_FORMAT(dimension.doc_date, '%m') doc_month,
|
DATE_FORMAT(dimension.doc_date, '%Y%m') doc_monthno, dimension.*, 0, 0, 0, 0
|
from (
|
select doc.doc_date, doc.customer_id, product_id, sku_id from so_order_return doc
|
inner join so_order_return_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, doc.customer_id, product_id, sku_id) dimension
|
WHERE not exists (
|
select 1 from rep_customer_stock_flow
|
where dimension.doc_date = rep_customer_stock_flow.doc_date
|
and dimension.customer_id = rep_customer_stock_flow.customer_id
|
and dimension.product_id = rep_customer_stock_flow.product_id
|
and dimension.sku_id = rep_customer_stock_flow.sku_id)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurReturn">
|
<![CDATA[
|
update rep_customer_stock_flow
|
set qty_pur = qty_pur - (
|
select qty from (
|
select doc.doc_date, doc.customer_id, doc_detail.product_id, doc_detail.sku_id, sum(doc_detail.qty) qty
|
from so_order_return doc
|
inner join so_order_return_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, doc.customer_id, doc_detail.product_id, doc_detail.sku_id) doc
|
where doc.doc_date = rep_customer_stock_flow.doc_date and doc.customer_id = rep_customer_stock_flow.customer_id
|
and doc.product_id = rep_customer_stock_flow.product_id and doc.sku_id = rep_customer_stock_flow.sku_id )
|
where exists (
|
select 1 from (
|
select doc.doc_date, doc.customer_id, doc_detail.product_id, doc_detail.sku_id, sum(doc_detail.qty) qty
|
from so_order_return doc
|
inner join so_order_return_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, doc.customer_id, doc_detail.product_id, doc_detail.sku_id) doc
|
where doc.doc_date = rep_customer_stock_flow.doc_date and doc.customer_id = rep_customer_stock_flow.customer_id
|
and doc.product_id = rep_customer_stock_flow.product_id and doc.sku_id = rep_customer_stock_flow.sku_id)
|
]]>
|
</sql>
|
|
<sql name="appendCustomerTransferInDimension">
|
<![CDATA[
|
INSERT INTO rep_customer_stock_flow (
|
id, idx, doc_year, doc_month, doc_monthno, doc_date, customer_id, product_id,
|
sku_id,qty_pur, qty_transfer_in, qty_transfer_out, qty_implant )
|
select
|
md5(UUID_SHORT()) id, null idx, YEAR(dimension.doc_date) doc_month, DATE_FORMAT(dimension.doc_date, '%m') doc_month,
|
DATE_FORMAT(dimension.doc_date, '%Y%m') doc_monthno, dimension.*, 0, 0, 0, 0
|
from (
|
select doc.doc_date, doc.to_account_id customer_id, product_id, sku_id from wm_transfer doc
|
left join wm_transfer_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, doc.to_account_id, product_id, sku_id) dimension
|
WHERE not exists (
|
select 1 from rep_customer_stock_flow
|
where dimension.doc_date = rep_customer_stock_flow.doc_date
|
and dimension.customer_id = rep_customer_stock_flow.customer_id
|
and dimension.product_id = rep_customer_stock_flow.product_id
|
and dimension.sku_id = rep_customer_stock_flow.sku_id )
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerTransferIn">
|
<![CDATA[
|
update rep_customer_stock_flow
|
set qty_transfer_in = qty_transfer_in + (select qty from (
|
select doc.doc_date, to_account_id customer_id, product_id, sku_id, sum(doc_detail.qty) qty from wm_transfer doc
|
left join wm_transfer_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, to_account_id, product_id, sku_id ) doc
|
where doc.doc_date = rep_customer_stock_flow.doc_date and doc.customer_id = rep_customer_stock_flow.customer_id
|
and doc.product_id = rep_customer_stock_flow.product_id and doc.sku_id = rep_customer_stock_flow.sku_id )
|
where exists (select 1 from (
|
select doc.doc_date, to_account_id customer_id, product_id, sku_id, sum(doc_detail.qty) qty from wm_transfer doc
|
left join wm_transfer_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, to_account_id, product_id, sku_id) doc
|
where doc.doc_date = rep_customer_stock_flow.doc_date and doc.customer_id = rep_customer_stock_flow.customer_id
|
and doc.product_id = rep_customer_stock_flow.product_id and doc.sku_id = rep_customer_stock_flow.sku_id )
|
]]>
|
</sql>
|
|
<sql name="appendCustomerTransferOutDimension">
|
<![CDATA[
|
INSERT INTO rep_customer_stock_flow (
|
id, idx, doc_year, doc_month, doc_monthno, doc_date, customer_id, product_id, sku_id,
|
qty_pur, qty_transfer_in, qty_transfer_out, qty_implant)
|
select
|
md5(UUID_SHORT()) id, null idx, YEAR(dimension.doc_date) doc_month, DATE_FORMAT(dimension.doc_date, '%m') doc_month,
|
DATE_FORMAT(dimension.doc_date, '%Y%m') doc_monthno, dimension.*, 0, 0, 0, 0
|
from (
|
select doc.doc_date, doc.account_id customer_id, product_id, sku_id from wm_transfer doc
|
left join wm_transfer_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, doc.account_id, product_id, sku_id) dimension
|
WHERE not exists (
|
select 1 from rep_customer_stock_flow
|
where dimension.doc_date = rep_customer_stock_flow.doc_date
|
and dimension.customer_id = rep_customer_stock_flow.customer_id
|
and dimension.product_id = rep_customer_stock_flow.product_id
|
and dimension.sku_id = rep_customer_stock_flow.sku_id )
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerTransferOut">
|
<![CDATA[
|
update rep_customer_stock_flow
|
set qty_transfer_out = qty_transfer_out + (select qty from (
|
select doc.doc_date, account_id customer_id, product_id, sku_id, sum(doc_detail.qty) qty from wm_transfer doc
|
left join wm_transfer_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, account_id, product_id, sku_id ) doc
|
where doc.doc_date = rep_customer_stock_flow.doc_date and doc.customer_id = rep_customer_stock_flow.customer_id
|
and doc.product_id = rep_customer_stock_flow.product_id and doc.sku_id = rep_customer_stock_flow.sku_id )
|
where exists (select 1 from (
|
select doc.doc_date, account_id customer_id, product_id, sku_id, sum(doc_detail.qty) qty from wm_transfer doc
|
left join wm_transfer_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, to_account_id, product_id, sku_id) doc
|
where doc.doc_date = rep_customer_stock_flow.doc_date and doc.customer_id = rep_customer_stock_flow.customer_id
|
and doc.product_id = rep_customer_stock_flow.product_id and doc.sku_id = rep_customer_stock_flow.sku_id )
|
]]>
|
</sql>
|
|
<sql name="appendCustomerSkuImplantDimension">
|
<![CDATA[
|
INSERT INTO rep_customer_stock_flow (
|
id, idx, doc_year, doc_month, doc_monthno, doc_date, customer_id, product_id, sku_id,
|
qty_pur, qty_transfer_in, qty_transfer_out, qty_implant)
|
select
|
md5(UUID_SHORT()) id, null idx, YEAR(dimension.doc_date) doc_month, DATE_FORMAT(dimension.doc_date, '%m') doc_month,
|
DATE_FORMAT(dimension.doc_date, '%Y%m') doc_monthno, dimension.*, 0, 0, 0, 0
|
from (
|
select doc.doc_date, doc.customer_id, product_id, sku_id
|
from so_implant doc
|
left join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, doc.customer_id, product_id, sku_id) dimension
|
WHERE not exists (
|
select 1 from rep_customer_stock_flow
|
where dimension.doc_date = rep_customer_stock_flow.doc_date
|
and dimension.customer_id = rep_customer_stock_flow.customer_id
|
and dimension.product_id = rep_customer_stock_flow.product_id
|
and dimension.sku_id = rep_customer_stock_flow.sku_id )
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerSkuImplant">
|
<![CDATA[
|
update rep_customer_stock_flow
|
set qty_implant = (select qty from (
|
select doc.doc_date, customer_id, product_id, sku_id, sum(doc_detail.qty) qty from so_implant doc
|
left join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, customer_id, product_id, sku_id ) doc
|
where doc.doc_date = rep_customer_stock_flow.doc_date and doc.customer_id = rep_customer_stock_flow.customer_id
|
and doc.product_id = rep_customer_stock_flow.product_id and doc.sku_id = rep_customer_stock_flow.sku_id )
|
where exists (select 1 from (
|
select doc.doc_date, customer_id, product_id, sku_id, sum(doc_detail.qty) qty from so_implant doc
|
left join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open'
|
group by doc.doc_date, customer_id, product_id, sku_id ) doc
|
where doc.doc_date = rep_customer_stock_flow.doc_date and doc.customer_id = rep_customer_stock_flow.customer_id
|
and doc.product_id = rep_customer_stock_flow.product_id and doc.sku_id = rep_customer_stock_flow.sku_id )
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerSkuInv">
|
<![CDATA[
|
update rep_customer_stock_flow
|
set qty_inv = qty_pur + qty_transfer_in - qty_transfer_out - qty_implant
|
where qty_inv is null
|
]]>
|
</sql>
|
|
<!-- 经销商植入情况rep_customer_implant -->
|
<sql name="appendCustomerImplantDimension">
|
<![CDATA[
|
INSERT INTO rep_customer_implant (id, idx, year, bu_id, org_id, customer_id, position_id)
|
select md5(UUID_SHORT()) id, null idx, dimension.*
|
from(
|
select doc.year, doc.bu_id, doc.org_id, doc.account_id customer_id , doc.position_id
|
from md_target_dealer doc where doc.type_code = 'implant'
|
group by doc.year, doc.bu_id, doc.org_id, doc.account_id , position_id) dimension
|
WHERE not exists (
|
select 1 from rep_customer_implant
|
where dimension.year = rep_customer_implant.year
|
and dimension.org_id = rep_customer_implant.org_id
|
and dimension.customer_id = rep_customer_implant.customer_id
|
and dimension.bu_id = rep_customer_implant.bu_id
|
and dimension.position_id = rep_customer_implant.position_id)
|
]]>
|
</sql>
|
|
<sql name="setCustomerRegion">
|
<![CDATA[
|
update rep_customer_implant
|
inner join md_position_hierarchy on rep_customer_implant.position_id = md_position_hierarchy.position_id
|
inner join md_position_employee on md_position_employee.position_id = md_position_hierarchy.level2
|
set rep_customer_implant.region_employee_name = md_position_employee.remark
|
where rep_customer_implant.year = YEAR(CURRENT_DATE)
|
]]>
|
</sql>
|
|
<sql name="setCustomerPosition">
|
<![CDATA[
|
update rep_customer_implant
|
inner join md_position_hierarchy on rep_customer_implant.position_id = md_position_hierarchy.position_id
|
inner join md_position_employee on md_position_employee.position_id = md_position_hierarchy.level2
|
set rep_customer_implant.region_employee_name = md_position_employee.remark
|
where rep_customer_implant.year = YEAR(CURRENT_DATE)
|
]]>
|
</sql>
|
|
<sql name="setCustomerAccountInfo">
|
<![CDATA[
|
update rep_customer_implant
|
inner join md_org_account on md_org_account.id = rep_customer_implant.customer_id
|
SET rep_customer_implant.account_state_code = md_org_account.state_code,
|
rep_customer_implant.account_state_name = md_org_account.state_name,
|
rep_customer_implant.open_date = md_org_account.open_date
|
]]>
|
</sql>
|
|
<sql name="setCustomerAgmInfo">
|
<![CDATA[
|
update rep_customer_implant
|
inner join agm_agreement on agm_agreement.account_id = rep_customer_implant.customer_id and year(agm_agreement.date_from) = rep_customer_implant.year
|
SET rep_customer_implant.agm_state_code = agm_agreement.state_code,
|
rep_customer_implant.agm_state_name = agm_agreement.state_name
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantTarget">
|
<![CDATA[
|
update rep_customer_implant
|
inner join md_target_dealer doc on rep_customer_implant.year = doc.year and rep_customer_implant.bu_id = doc.bu_id
|
and rep_customer_implant.org_id = doc.org_id and rep_customer_implant.customer_id = doc.account_id and doc.type_code = 'implant'
|
set year_bp = qty_year , q1_bp = qty_q1, q2_bp = qty_q2, q3_bp = qty_q3, q4_bp = qty_q4,
|
m1_bp = qty_m1 , m2_bp = qty_m2, m3_bp = qty_m3, m4_bp = qty_m4, m5_bp = qty_m5, m6_bp = qty_m6,
|
m7_bp = qty_m7 , m8_bp = qty_m8, m9_bp = qty_m9, m10_bp = qty_m10, m11_bp = qty_m11, m12_bp = qty_m12
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_m1">
|
<![CDATA[
|
update rep_customer_implant
|
left join (
|
select doc.customer_id, YEAR(doc.doc_date) year, sum(doc_detail.qty) qty
|
from so_implant doc
|
inner join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open' and MONTH(doc.doc_date) = 1
|
group by doc.customer_id, YEAR(doc.doc_date) ) doc on rep_customer_implant.customer_id = doc.customer_id
|
and doc.year = rep_customer_implant.year
|
set rep_customer_implant.m1_actual = ifnull(qty,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_m2">
|
<![CDATA[
|
update rep_customer_implant
|
left join (
|
select doc.customer_id, YEAR(doc.doc_date) year, sum(doc_detail.qty) qty
|
from so_implant doc
|
inner join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open' and MONTH(doc.doc_date) = 2
|
group by doc.customer_id, YEAR(doc.doc_date)
|
) doc on rep_customer_implant.customer_id = doc.customer_id and doc.year = rep_customer_implant.year
|
set rep_customer_implant.m2_actual = ifnull(qty,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_m3">
|
<![CDATA[
|
update rep_customer_implant
|
left join (
|
select doc.customer_id, YEAR(doc.doc_date) year, sum(doc_detail.qty) qty
|
from so_implant doc
|
inner join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where MONTH(doc.doc_date) = 3 and doc.state_code = 'Open'
|
group by doc.customer_id, YEAR(doc.doc_date)
|
) doc on rep_customer_implant.customer_id = doc.customer_id and doc.year = rep_customer_implant.year
|
set rep_customer_implant.m3_actual = ifnull(qty,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_m4">
|
<![CDATA[
|
update rep_customer_implant
|
left join (
|
select doc.customer_id, YEAR(doc.doc_date) year, sum(doc_detail.qty) qty
|
from so_implant doc
|
inner join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where MONTH(doc.doc_date) = 4 and doc.state_code = 'Open'
|
group by doc.customer_id, YEAR(doc.doc_date)
|
) doc on rep_customer_implant.customer_id = doc.customer_id and doc.year = rep_customer_implant.year
|
set rep_customer_implant.m4_actual = ifnull(qty,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_m5">
|
<![CDATA[
|
update rep_customer_implant
|
left join (
|
select doc.customer_id, YEAR(doc.doc_date) year, sum(doc_detail.qty) qty
|
from so_implant doc
|
inner join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where MONTH(doc.doc_date) = 5 and doc.state_code = 'Open'
|
group by doc.customer_id, YEAR(doc.doc_date)
|
) doc on rep_customer_implant.customer_id = doc.customer_id and doc.year = rep_customer_implant.year
|
set rep_customer_implant.m5_actual = ifnull(qty,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_m6">
|
<![CDATA[
|
update rep_customer_implant
|
left join (
|
select doc.customer_id, YEAR(doc.doc_date) year, sum(doc_detail.qty) qty
|
from so_implant doc
|
inner join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where MONTH(doc.doc_date) = 6 and doc.state_code = 'Open'
|
group by doc.customer_id, YEAR(doc.doc_date)
|
) doc on rep_customer_implant.customer_id = doc.customer_id and doc.year = rep_customer_implant.year
|
set rep_customer_implant.m6_actual = ifnull(qty,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_m7">
|
<![CDATA[
|
update rep_customer_implant
|
left join (
|
select doc.customer_id, YEAR(doc.doc_date) year, sum(doc_detail.qty) qty
|
from so_implant doc
|
inner join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where MONTH(doc.doc_date) = 7 and doc.state_code = 'Open'
|
group by doc.customer_id, YEAR(doc.doc_date)
|
) doc on rep_customer_implant.customer_id = doc.customer_id and doc.year = rep_customer_implant.year
|
set rep_customer_implant.m7_actual = ifnull(qty,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_m8">
|
<![CDATA[
|
update rep_customer_implant
|
left join (
|
select doc.customer_id, YEAR(doc.doc_date) year, sum(doc_detail.qty) qty
|
from so_implant doc
|
inner join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where MONTH(doc.doc_date) = 8 and doc.state_code = 'Open'
|
group by doc.customer_id, YEAR(doc.doc_date)
|
) doc on rep_customer_implant.customer_id = doc.customer_id and doc.year = rep_customer_implant.year
|
set rep_customer_implant.m8_actual = ifnull(qty,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_m9">
|
<![CDATA[
|
update rep_customer_implant
|
left join (
|
select doc.customer_id, YEAR(doc.doc_date) year, sum(doc_detail.qty) qty
|
from so_implant doc
|
inner join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where MONTH(doc.doc_date) = 9 and doc.state_code = 'Open'
|
group by doc.customer_id, YEAR(doc.doc_date)
|
) doc on rep_customer_implant.customer_id = doc.customer_id and doc.year = rep_customer_implant.year
|
set rep_customer_implant.m9_actual = ifnull(qty,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_m10">
|
<![CDATA[
|
update rep_customer_implant
|
left join (
|
select doc.customer_id, YEAR(doc.doc_date) year, sum(doc_detail.qty) qty
|
from so_implant doc
|
inner join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where MONTH(doc.doc_date) = 10 and doc.state_code = 'Open'
|
group by doc.customer_id, YEAR(doc.doc_date)
|
) doc on rep_customer_implant.customer_id = doc.customer_id and doc.year = rep_customer_implant.year
|
set rep_customer_implant.m10_actual = ifnull(qty,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_m11">
|
<![CDATA[
|
update rep_customer_implant
|
left join (
|
select doc.customer_id, YEAR(doc.doc_date) year, sum(doc_detail.qty) qty
|
from so_implant doc
|
inner join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where MONTH(doc.doc_date) = 11 and doc.state_code = 'Open'
|
group by doc.customer_id, YEAR(doc.doc_date)
|
) doc on rep_customer_implant.customer_id = doc.customer_id and doc.year = rep_customer_implant.year
|
set rep_customer_implant.m11_actual = ifnull(qty,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_m12">
|
<![CDATA[
|
update rep_customer_implant
|
left join (
|
select doc.customer_id, YEAR(doc.doc_date) year, sum(doc_detail.qty) qty
|
from so_implant doc
|
inner join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
where MONTH(doc.doc_date) = 12 and doc.state_code = 'Open'
|
group by doc.customer_id, YEAR(doc.doc_date)
|
) doc on rep_customer_implant.customer_id = doc.customer_id and doc.year = rep_customer_implant.year
|
set rep_customer_implant.m12_actual = ifnull(qty,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_season">
|
<![CDATA[
|
update rep_customer_implant
|
set rep_customer_implant.q1_actual = m1_actual + m2_actual + m3_actual,
|
rep_customer_implant.q2_actual = m4_actual + m5_actual + m6_actual,
|
rep_customer_implant.q3_actual = m7_actual + m8_actual + m9_actual,
|
rep_customer_implant.q4_actual = m10_actual + m11_actual + m12_actual
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantActual_Year">
|
<![CDATA[
|
update rep_customer_implant
|
set rep_customer_implant.year_actual = q1_actual + q2_actual + q3_actual + q4_actual
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerImplantAchieve">
|
<![CDATA[
|
update rep_customer_implant
|
set year_achieve = year_actual*100/year_bp,
|
q1_achieve = q1_actual*100/q1_bp, q2_achieve = q2_actual*100/q2_bp, q3_achieve = q3_actual*100/q3_bp, q4_achieve = q4_actual*100/q4_bp,
|
m1_achieve = m1_actual*100/m1_bp, m2_achieve = m2_actual*100/m2_bp, m3_achieve = m3_actual*100/m3_bp, m4_achieve = m4_actual*100/m4_bp,
|
m5_achieve = m5_actual*100/m5_bp, m6_achieve = m6_actual*100/m6_bp, m7_achieve = m7_actual*100/m7_bp, m8_achieve = m8_actual*100/m8_bp,
|
m9_achieve = m9_actual*100/m9_bp, m10_achieve = m10_actual*100/m10_bp, m11_achieve = m11_actual*100/m11_bp, m12_achieve = m12_actual*100/m12_bp
|
]]>
|
</sql>
|
|
<!-- 经销商买入情况rep_customer_pur -->
|
<sql name="appendCustomerPurTargetDimension">
|
<![CDATA[
|
INSERT INTO rep_customer_pur (id, idx, year, bu_id, org_id, customer_id, position_id)
|
select md5(UUID_SHORT()) id, null idx, dimension.*
|
from(
|
select doc.year, doc.bu_id, doc.org_id, doc.account_id customer_id, doc.position_id
|
from md_target_dealer doc where doc.type_code = 'dealer' and doc.account_id is not null
|
group by doc.year, doc.bu_id, doc.org_id, doc.account_id, doc.position_id ) dimension
|
WHERE not exists (
|
select 1 from rep_customer_pur
|
where dimension.year = rep_customer_pur.year
|
and dimension.org_id = rep_customer_pur.org_id
|
and dimension.customer_id = rep_customer_pur.customer_id
|
and dimension.bu_id = rep_customer_pur.bu_id
|
and dimension.position_id = rep_customer_pur.position_id)
|
]]>
|
</sql>
|
|
<sql name="setCustomerPurRegion">
|
<![CDATA[
|
update rep_customer_pur
|
inner join md_position_hierarchy on rep_customer_pur.position_id = md_position_hierarchy.position_id
|
inner join md_position_employee on md_position_employee.position_id = md_position_hierarchy.level2
|
inner join md_position on md_position.id = rep_customer_pur.position_id
|
set rep_customer_pur.region_employee_name = md_position_employee.remark,
|
rep_customer_pur.region = md_position.region
|
where rep_customer_pur.year = YEAR(CURRENT_DATE)
|
]]>
|
</sql>
|
|
<sql name="setCustomerPurPosition">
|
<![CDATA[
|
update rep_customer_pur
|
inner join md_position_employee on md_position_employee.position_id = rep_customer_pur.position_id
|
set rep_customer_pur.position_employee_name = md_position_employee.remark
|
where rep_customer_pur.year = YEAR(CURRENT_DATE)
|
]]>
|
</sql>
|
|
<sql name="setCustomerPurAccountInfo">
|
<![CDATA[
|
update rep_customer_pur
|
inner join md_org_account on md_org_account.id = rep_customer_pur.customer_id
|
SET rep_customer_pur.account_state_code = md_org_account.state_code,
|
rep_customer_pur.account_state_name = md_org_account.state_name,
|
rep_customer_pur.open_date = md_org_account.open_date,
|
rep_customer_pur.province = md_org_account.authorize_province
|
]]>
|
</sql>
|
|
<sql name="setCustomerPurAgmInfo">
|
<![CDATA[
|
update rep_customer_pur
|
inner join agm_agreement on agm_agreement.account_id = rep_customer_pur.customer_id and year(agm_agreement.date_from) = rep_customer_pur.year
|
SET rep_customer_pur.agm_state_code = agm_agreement.state_code,
|
rep_customer_pur.agm_state_name = agm_agreement.state_name
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurTarget">
|
<![CDATA[
|
update rep_customer_pur
|
inner join md_target_dealer doc on rep_customer_pur.year = doc.year and rep_customer_pur.bu_id = doc.bu_id
|
and rep_customer_pur.org_id = doc.org_id and rep_customer_pur.customer_id = doc.account_id and doc.type_code = 'dealer'
|
set year_bp = amt_year , q1_bp = amt_q1, q2_bp = amt_q2, q3_bp = amt_q3, q4_bp = amt_q4,
|
m1_bp = amt_m1 , m2_bp = amt_m2, m3_bp = amt_m3, m4_bp = amt_m4, m5_bp = amt_m5, m6_bp = amt_m6,
|
m7_bp = amt_m7 , m8_bp = amt_m8, m9_bp = amt_m9, m10_bp = amt_m10, m11_bp = amt_m11, m12_bp = amt_m12
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_m1">
|
<![CDATA[
|
update rep_customer_pur
|
left join (
|
select doc.customer_id, doc.year, sum(doc_detail.amt_total) amt
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.`month` = '01' and doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.customer_id, doc.year
|
) doc on rep_customer_pur.customer_id = doc.customer_id and doc.year = rep_customer_pur.year
|
set rep_customer_pur.m1_actual = ifnull(amt,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_m2">
|
<![CDATA[
|
update rep_customer_pur
|
left join (
|
select doc.customer_id, doc.year, sum(doc_detail.amt_total) amt
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.`month` = '02' and doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.customer_id, doc.year
|
) doc on rep_customer_pur.customer_id = doc.customer_id and doc.year = rep_customer_pur.year
|
set rep_customer_pur.m2_actual = ifnull(amt,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_m3">
|
<![CDATA[
|
update rep_customer_pur
|
left join (
|
select doc.customer_id, doc.year, sum(doc_detail.amt_total) amt
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.`month` = '03' and doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.customer_id, doc.year
|
) doc on rep_customer_pur.customer_id = doc.customer_id and doc.year = rep_customer_pur.year
|
set rep_customer_pur.m3_actual = ifnull(amt,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_m4">
|
<![CDATA[
|
update rep_customer_pur
|
left join (
|
select doc.customer_id, doc.year, sum(doc_detail.amt_total) amt
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.`month` = '04' and doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.customer_id, doc.year
|
) doc on rep_customer_pur.customer_id = doc.customer_id and doc.year = rep_customer_pur.year
|
set rep_customer_pur.m4_actual = ifnull(amt,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_m5">
|
<![CDATA[
|
update rep_customer_pur
|
left join (
|
select doc.customer_id, doc.year, sum(doc_detail.amt_total) amt
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.`month` = '05' and doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.customer_id, doc.year
|
) doc on rep_customer_pur.customer_id = doc.customer_id and doc.year = rep_customer_pur.year
|
set rep_customer_pur.m5_actual = ifnull(amt,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_m6">
|
<![CDATA[
|
update rep_customer_pur
|
left join (
|
select doc.customer_id, doc.year, sum(doc_detail.amt_total) amt
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.`month` = '06' and doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.customer_id, doc.year
|
) doc on rep_customer_pur.customer_id = doc.customer_id and doc.year = rep_customer_pur.year
|
set rep_customer_pur.m6_actual = ifnull(amt,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_m7">
|
<![CDATA[
|
update rep_customer_pur
|
left join (
|
select doc.customer_id, doc.year, sum(doc_detail.amt_total) amt
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.`month` = '07' and doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.customer_id, doc.year
|
) doc on rep_customer_pur.customer_id = doc.customer_id and doc.year = rep_customer_pur.year
|
set rep_customer_pur.m7_actual = ifnull(amt,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_m8">
|
<![CDATA[
|
update rep_customer_pur
|
left join (
|
select doc.customer_id, doc.year, sum(doc_detail.amt_total) amt
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.`month` = '08' and doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.customer_id, doc.year
|
) doc on rep_customer_pur.customer_id = doc.customer_id and doc.year = rep_customer_pur.year
|
set rep_customer_pur.m8_actual = ifnull(amt,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_m9">
|
<![CDATA[
|
update rep_customer_pur
|
left join (
|
select doc.customer_id, doc.year, sum(doc_detail.amt_total) amt
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.`month` = '09' and doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.customer_id, doc.year
|
) doc on rep_customer_pur.customer_id = doc.customer_id and doc.year = rep_customer_pur.year
|
set rep_customer_pur.m9_actual = ifnull(amt,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_m10">
|
<![CDATA[
|
update rep_customer_pur
|
left join (
|
select doc.customer_id, doc.year, sum(doc_detail.amt_total) amt
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.`month` = '10' and doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.customer_id, doc.year
|
) doc on rep_customer_pur.customer_id = doc.customer_id and doc.year = rep_customer_pur.year
|
set rep_customer_pur.m10_actual = ifnull(amt,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_m11">
|
<![CDATA[
|
update rep_customer_pur
|
left join (
|
select doc.customer_id, doc.year, sum(doc_detail.amt_total) amt
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.`month` = '11' and doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.customer_id, doc.year
|
) doc on rep_customer_pur.customer_id = doc.customer_id and doc.year = rep_customer_pur.year
|
set rep_customer_pur.m11_actual = ifnull(amt,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_m12">
|
<![CDATA[
|
update rep_customer_pur
|
left join (
|
select doc.customer_id, doc.year, sum(doc_detail.amt_total) amt
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.`month` = '12' and doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc.customer_id, doc.year
|
) doc on rep_customer_pur.customer_id = doc.customer_id and doc.year = rep_customer_pur.year
|
set rep_customer_pur.m12_actual = ifnull(amt,0)
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_season">
|
<![CDATA[
|
update rep_customer_pur
|
set rep_customer_pur.q1_actual = m1_actual + m2_actual + m3_actual,
|
rep_customer_pur.q2_actual = m4_actual + m5_actual + m6_actual,
|
rep_customer_pur.q3_actual = m7_actual + m8_actual + m9_actual,
|
rep_customer_pur.q4_actual = m10_actual + m11_actual + m12_actual
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurActual_Year">
|
<![CDATA[
|
update rep_customer_pur
|
set rep_customer_pur.year_actual = q1_actual + q2_actual + q3_actual + q4_actual
|
]]>
|
</sql>
|
|
<sql name="calculateCustomerPurAchieve">
|
<![CDATA[
|
update rep_customer_pur
|
set year_achieve = year_actual*100/year_bp,
|
q1_achieve = q1_actual*100/q1_bp, q2_achieve = q2_actual*100/q2_bp, q3_achieve = q3_actual*100/q3_bp, q4_achieve = q4_actual*100/q4_bp,
|
m1_achieve = m1_actual*100/m1_bp, m2_achieve = m2_actual*100/m2_bp, m3_achieve = m3_actual*100/m3_bp, m4_achieve = m4_actual*100/m4_bp,
|
m5_achieve = m5_actual*100/m5_bp, m6_achieve = m6_actual*100/m6_bp, m7_achieve = m7_actual*100/m7_bp, m8_achieve = m8_actual*100/m8_bp,
|
m9_achieve = m9_actual*100/m9_bp, m10_achieve = m10_actual*100/m10_bp, m11_achieve = m11_actual*100/m11_bp, m12_achieve = m12_actual*100/m12_bp
|
]]>
|
</sql>
|
|
<!-- 客户开发情况rep_customer_development -->
|
<sql name="appendCustomerDevelopmentDimension">
|
<![CDATA[
|
INSERT INTO rep_customer_development (id, idx, bu_id, org_id, customer_id)
|
select md5(UUID_SHORT()) id, null idx, md_bu.id bu_id, md_org_account.org_id, md_org_account.id customer_id
|
from md_org_account
|
left join md_bu on 1 =1
|
where md_org_account.state_code in ('Complete', 'Open', 'Close')
|
]]>
|
</sql>
|
|
<sql name="setCustomerDevelopmentRegion">
|
<![CDATA[
|
update rep_customer_development
|
left join md_org_account_position on md_org_account_position.parent_id = rep_customer_development.customer_id
|
left join (select bu_id, position_id from md_position_province group by bu_id, position_id )v on
|
v.bu_id = rep_customer_development.bu_id
|
and rep_customer_development.customer_id = md_org_account_position.parent_id
|
left join md_position on md_position.id = md_org_account_position.position_id
|
SET rep_customer_development.region = md_position.region
|
]]>
|
</sql>
|
|
<sql name="setCustomerDevelopmentInfo">
|
<![CDATA[
|
update rep_customer_development
|
inner join md_org_account on md_org_account.id = rep_customer_development.customer_id
|
left join md_org_account_bu on md_org_account_bu.account_id = rep_customer_development.customer_id and md_org_account_bu.bu_id = rep_customer_development.bu_id
|
set rep_customer_development.open_date = md_org_account.open_date,
|
rep_customer_development.is_open = if(md_org_account_bu.bu_id is not null, 'T', 'F'),
|
rep_customer_development.is_closed = if(state_code = 'Close', 'T', 'F'),
|
rep_customer_development.is_valid = if(state_code in('Open','Complete'), 'T', 'F'),
|
rep_customer_development.open_date = if(state_code = 'Close' and md_org_account_bu.bu_id is null, null, '2024-01-01')
|
]]>
|
</sql>
|
|
<sql name="setCustomerClosedInfo">
|
<![CDATA[
|
update rep_customer_development set rep_customer_development.closed_date = (
|
select max(md_org_account_change.termination_date)
|
from md_org_account_change
|
inner join md_org_account_bu on md_org_account_bu.account_id = md_org_account_change.account_id
|
where md_org_account_bu.bu_id = rep_customer_development.bu_id and md_org_account_change.account_id = rep_customer_development.customer_id and type_code = 'close' and state_code = 'Close')
|
where is_closed = 'T'
|
]]>
|
</sql>
|
|
<sql name="setCustomerPaymentInfo">
|
<![CDATA[
|
update rep_customer_development
|
set rep_customer_development.payment_date = (select min(payment_date) from so_payment
|
where so_payment.customer_id = rep_customer_development.org_id
|
and so_payment.bu_id = rep_customer_development.bu_id ),
|
rep_customer_development.is_payment = (select distinct 'T' from so_payment
|
where so_payment.customer_id = rep_customer_development.org_id
|
and so_payment.bu_id = rep_customer_development.bu_id)
|
]]>
|
</sql>
|
|
<sql name="setNoPaymentCustomer">
|
<![CDATA[
|
update rep_customer_development
|
set is_payment = 'F' , payment_date = '1970-01-01'
|
where payment_date is null
|
]]>
|
</sql>
|
|
<sql name="setCustomerDeliveryInfo">
|
<![CDATA[
|
update rep_customer_development
|
inner join ( select so_order.customer_id, so_order_detail.bu_id, min(so_order.doc_date) delivery_date from so_order
|
inner join so_order_detail on so_order_detail.parent_id = so_order.id
|
group by so_order.customer_id, so_order_detail.bu_id) doc on doc.customer_id = rep_customer_development.customer_id and doc.bu_id = rep_customer_development.bu_id
|
set rep_customer_development.delivery_date = doc.delivery_date,
|
rep_customer_development.is_delivery = 'T'
|
]]>
|
</sql>
|
|
<sql name="setNoDeliveryCustomer">
|
<![CDATA[
|
update rep_customer_development
|
set is_delivery = 'F' , delivery_date = '1970-01-01'
|
where delivery_date is null
|
]]>
|
</sql>
|
|
<sql name="setCustomerImplantInfo">
|
<![CDATA[
|
update rep_customer_development
|
set rep_customer_development.first_implant_date = (select min(doc_date) from so_implant
|
where so_implant.customer_id = rep_customer_development.customer_id
|
and so_implant.bu_id = rep_customer_development.bu_id
|
and so_implant.state_code = 'Open'),
|
rep_customer_development.is_implant = (select distinct 'T' from so_implant
|
where so_implant.customer_id = rep_customer_development.customer_id
|
and so_implant.bu_id = rep_customer_development.bu_id
|
and so_implant.state_code = 'Open')
|
where exists (select 1 from so_implant
|
where so_implant.customer_id = rep_customer_development.customer_id
|
and so_implant.bu_id = rep_customer_development.bu_id
|
and so_implant.state_code = 'Open')
|
]]>
|
</sql>
|
|
<sql name="setCustomerAgmInfo">
|
<![CDATA[
|
update rep_customer_development
|
set rep_customer_development.agm_date_from = (select min(date_from) from agm_agreement
|
where agm_agreement.account_id = rep_customer_development.customer_id
|
and agm_agreement.bu_id = rep_customer_development.bu_id
|
and agm_agreement.state_code = 'Open'),
|
rep_customer_development.agm_date_to = (select max(date_to) from agm_agreement
|
where agm_agreement.account_id = rep_customer_development.customer_id
|
and agm_agreement.bu_id = rep_customer_development.bu_id
|
and agm_agreement.state_code = 'Open'),
|
rep_customer_development.is_agm = (select distinct 'T' from agm_agreement
|
where agm_agreement.account_id = rep_customer_development.customer_id
|
and agm_agreement.bu_id = rep_customer_development.bu_id
|
and agm_agreement.state_code = 'Open')
|
where exists (select 1 from agm_agreement
|
where agm_agreement.account_id = rep_customer_development.customer_id
|
and agm_agreement.bu_id = rep_customer_development.bu_id
|
and agm_agreement.state_code = 'Open' )
|
]]>
|
</sql>
|
|
<!-- 医院开发情况 rep_hospital_development -->
|
<sql name="appendHospitalDevelopmentAuthorizationDimension">
|
<![CDATA[
|
INSERT INTO rep_hospital_development (id, idx, region, hospital_id, bu_id, authorization_id )
|
select MD5(UUID_SHORT())id, null idx, region, hospital_id, bu_id, doc.id authorization_id
|
from md_org_account_hospital doc
|
where state_code = 'Open'
|
and not exists (
|
select 1 from rep_hospital_development
|
where rep_hospital_development.hospital_id = doc.hospital_id
|
and rep_hospital_development.bu_id = doc.bu_id
|
and doc.id = rep_hospital_development.authorization_id
|
and doc.region = rep_hospital_development.region)
|
]]>
|
</sql>
|
|
<sql name="setHospitalDevelopmentAuthorization">
|
<![CDATA[
|
update rep_hospital_development
|
inner join md_org_account_hospital on md_org_account_hospital.id = rep_hospital_development.authorization_id
|
set rep_hospital_development.is_authorization = 'T',
|
rep_hospital_development.authorization_date_from = md_org_account_hospital.date_from,
|
rep_hospital_development.authorization_date_to = md_org_account_hospital.date_to
|
]]>
|
</sql>
|
|
<sql name="appendHospitalDevelopmentImplantDimension">
|
<![CDATA[
|
INSERT INTO rep_hospital_development (id, idx, region, hospital_id, bu_id, authorization_id )
|
select MD5(UUID_SHORT())id, null idx, sale_region, hospital_id, bu_id, null authorization_id
|
from so_implant doc
|
where doc.state_code = 'Open'
|
and not exists (
|
select 1 from rep_hospital_development
|
where rep_hospital_development.hospital_id = doc.hospital_id
|
and rep_hospital_development.bu_id = doc.bu_id
|
and doc.sale_region = rep_hospital_development.region)
|
]]>
|
</sql>
|
|
<sql name="setHospitalDevelopmentImplant">
|
<![CDATA[
|
update rep_hospital_development
|
SET first_implant_date = (
|
select min(so_implant.doc_date) from so_implant
|
where so_implant.hospital_id = rep_hospital_development.hospital_id
|
and so_implant.bu_id = rep_hospital_development.bu_id and so_implant.state_code = 'Open'
|
and so_implant.state_code = 'Open')
|
]]>
|
</sql>
|
|
<sql name="setHospitalDevelopmentIsImplant">
|
<![CDATA[
|
update rep_hospital_development SET is_implant = if(first_implant_date is null, 'F', 'T')
|
]]>
|
</sql>
|
|
<sql name="appendHospitalDevelopmentPositionDimension">
|
<![CDATA[
|
INSERT INTO rep_hospital_development (id, idx, region, hospital_id, bu_id, authorization_id )
|
select MD5(UUID_SHORT())id, null idx, region, hospital_id, bu_id, null authorization_id
|
from md_position_hospital doc
|
left join md_position on md_position.id = doc.position_id
|
where state_code = 'Open' and not exists ( select 1 from rep_hospital_development
|
where rep_hospital_development.hospital_id = doc.hospital_id and rep_hospital_development.bu_id = doc.bu_id )
|
]]>
|
</sql>
|
|
<sql name="setHospitalDevelopmentCentral">
|
<![CDATA[
|
update rep_hospital_development
|
SET is_central = 'T'
|
where exists (
|
select 1 from md_target_hospital
|
where md_target_hospital.hospital_id = rep_hospital_development.hospital_id
|
AND md_target_hospital.bu_id = rep_hospital_development.bu_id )
|
]]>
|
</sql>
|
|
<sql name="setHospitalDevelopmentEnter">
|
<![CDATA[
|
update rep_hospital_development
|
SET enter_date = (
|
select min(so_implant.doc_date) from so_implant
|
where so_implant.hospital_id = rep_hospital_development.hospital_id and so_implant.state_code = 'Open'
|
and so_implant.bu_id = rep_hospital_development.bu_id
|
and so_implant.implant_type in ('temporary','formal')),
|
is_enter ='T'
|
where exists (
|
select min(so_implant.doc_date) from so_implant
|
where so_implant.hospital_id = rep_hospital_development.hospital_id and so_implant.state_code = 'Open'
|
and so_implant.bu_id = rep_hospital_development.bu_id
|
and so_implant.implant_type in ('temporary','formal') )
|
]]>
|
</sql>
|
|
<!-- 产品线开发情况 rep_bu_development -->
|
<sql name="appendBuDevelopmentImplantDimension">
|
<![CDATA[
|
INSERT INTO `huihe`.`rep_bu_development` (
|
`id`, `idx`, year, quarter, month, `doc_date`, `hospital_id`, `bu_id`, `customer_id`, `position_id`, `is_open`, `open_date`,
|
`is_authorization`, `authorization_id`, `authorization_date_from`, `authorization_date_to`,
|
`implant_qty`, `implant_amount`, `payment_amount`, `enter_qty`, `delivery_qty`, `delivery_amount`, `price`)
|
select
|
MD5(UUID_SHORT())`id`,null `idx`, md_peroid.year, md_peroid.quarter, md_peroid.month, doc.`doc_date`, doc.`hospital_id`, doc.`bu_id`, doc.`customer_id`, doc.sale_position_id `position_id`, 'F'`is_open`, null `open_date`,
|
'F'`is_authorization`, null `authorization_id`, null `authorization_date_from`, null`authorization_date_to`,
|
0 `implant_qty`, 0 `implant_amount`, 0 `payment_amount`, 0`enter_qty`, 0`delivery_qty`, 0 `delivery_amount`, 0 `price`
|
from so_implant doc
|
left join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_peroid on md_peroid.date_from <= doc.doc_date and doc.doc_date > md_peroid.date_to
|
where state_code = 'Open' and not EXISTS (select 1 from rep_bu_development
|
where rep_bu_development.bu_id = doc.bu_id and rep_bu_development.customer_id = doc.customer_id and rep_bu_development.hospital_id = doc.hospital_id and rep_bu_development.doc_date = doc.doc_date and rep_bu_development.position_id = doc.sale_position_id )
|
group by doc.bu_id ,doc.customer_id, doc.hospital_id, doc.doc_date,doc.sale_position_id
|
]]>
|
</sql>
|
|
<sql name="updateBuDevelopmentImplant">
|
<![CDATA[
|
update rep_bu_development
|
inner join ( select doc.bu_id ,doc.customer_id, doc.hospital_id, doc.doc_date,doc.sale_position_id position_id, sum(doc_detail.qty) qty , sum(doc_detail.amt) amount, sum(if(doc.type_code = 'implanted', doc_detail.qty, 0)) enter_qty ,
|
min(if(doc.implant_type in ('temporary', 'formal'), doc.doc_date, null)) enter_date
|
from so_implant doc
|
left join so_implant_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_prod_price_detail on md_prod_price_detail.sku_id = doc_detail.sku_id and md_prod_price_detail.bu_id = doc.bu_id
|
where doc.state_code= 'Open'
|
group by doc.bu_id ,doc.customer_id, doc.hospital_id, doc.doc_date,doc.sale_position_id) doc on rep_bu_development.bu_id = doc.bu_id and rep_bu_development.customer_id = doc.customer_id and rep_bu_development.hospital_id = doc.hospital_id and rep_bu_development.doc_date = doc.doc_date and rep_bu_development.position_id = doc.position_id
|
set rep_bu_development.implant_qty = doc.qty, rep_bu_development.implant_amount = doc.amount ,
|
rep_bu_development.enter_date = doc.enter_date,
|
rep_bu_development.is_enter = if(doc.enter_date is not null ,'T', 'F'),
|
rep_bu_development.enter_qty = doc.enter_qty;
|
]]>
|
</sql>
|
|
<sql name="appendBuDevelopmentDeliveryDimension">
|
<![CDATA[
|
INSERT INTO `huihe`.`rep_bu_development` (
|
id, idx, year, quarter, month, doc_date, hospital_id, bu_id, customer_id, position_id, is_open, open_date,
|
is_authorization, authorization_id, authorization_date_from, authorization_date_to,
|
implant_qty, implant_amount, payment_amount, enter_qty, delivery_qty, delivery_amount, price)
|
select
|
MD5(UUID_SHORT())`id`,null `idx`, md_peroid.year, md_peroid.quarter, md_peroid.month, doc.`doc_date`, null `hospital_id`, doc_detail.`bu_id`, doc.`customer_id`, doc.position_id `position_id`, 'F'`is_open`, null `open_date`,
|
'F'`is_authorization`, null `authorization_id`, null `authorization_date_from`, null`authorization_date_to`,
|
0 `implant_qty`, 0 `implant_amount`, 0 `payment_amount`, 0`enter_qty`, 0`delivery_qty`, 0 `delivery_amount`, 0 `price`
|
from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_peroid on md_peroid.date_from <= doc.doc_date and doc.doc_date > md_peroid.date_to
|
where doc.state_code = 'Open' and doc_detail.is_delete = '0' and not EXISTS (
|
select 1 from rep_bu_development
|
where rep_bu_development.bu_id = doc_detail.bu_id and rep_bu_development.customer_id = doc.customer_id
|
and rep_bu_development.doc_date = doc.doc_date and ifnull(rep_bu_development.position_id,'--') = ifnull(doc.position_id,'--'))
|
group by doc_detail.bu_id ,doc.customer_id, doc.doc_date,doc.position_id;
|
]]>
|
</sql>
|
|
<sql name="updateBuDevelopmentDelivery">
|
<![CDATA[
|
update rep_bu_development
|
inner join (
|
select doc_detail.bu_id ,doc.customer_id, doc.doc_date,doc.position_id position_id, sum(doc_detail.qty) qty , sum(doc_detail.amt_total) amount from so_order doc
|
inner join so_order_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open' and doc_detail.is_delete = '0'
|
group by doc_detail.bu_id ,doc.customer_id, doc.doc_date,doc.position_id) doc on rep_bu_development.bu_id = doc.bu_id and rep_bu_development.customer_id = doc.customer_id and rep_bu_development.doc_date = doc.doc_date and rep_bu_development.position_id = doc.position_id
|
set rep_bu_development.delivery_qty = rep_bu_development.delivery_qty + doc.qty,
|
rep_bu_development.delivery_amount = rep_bu_development.delivery_amount + doc.amount ;
|
]]>
|
</sql>
|
|
<sql name="appendBuDevelopmentDeliveryReturnDimension">
|
<![CDATA[
|
INSERT INTO `huihe`.`rep_bu_development` (
|
`id`, `idx`, year, quarter, month, `doc_date`, `hospital_id`, `bu_id`, `customer_id`, `position_id`, `is_open`, `open_date`,
|
`is_authorization`, `authorization_id`, `authorization_date_from`, `authorization_date_to`,
|
`implant_qty`, `implant_amount`, `payment_amount`, `enter_qty`, `delivery_qty`, `delivery_amount`, `price`)
|
select
|
MD5(UUID_SHORT())`id`,null `idx`, md_peroid.year, md_peroid.quarter, md_peroid.month, doc.`doc_date`, null `hospital_id`, doc_detail.`bu_id`, doc.`customer_id`, doc.position_id `position_id`,
|
'F'`is_open`, null `open_date`, 'F'`is_authorization`, null `authorization_id`, null `authorization_date_from`, null`authorization_date_to`,
|
0 `implant_qty`, 0 `implant_amount`, 0 `payment_amount`, 0`enter_qty`, 0`delivery_qty`, 0 `delivery_amount`, 0 `price`
|
from so_order_return doc
|
inner join so_order_return_detail doc_detail on doc_detail.parent_id = doc.id
|
left join md_peroid on md_peroid.date_from <= doc.doc_date and doc.doc_date > md_peroid.date_to
|
where doc.state_code = 'Open' and not EXISTS (select 1 from rep_bu_development
|
where rep_bu_development.bu_id = doc_detail.bu_id and rep_bu_development.customer_id = doc.customer_id and rep_bu_development.doc_date = doc.doc_date and rep_bu_development.position_id = doc.position_id )
|
group by doc_detail.bu_id ,doc.customer_id, doc.doc_date,doc.position_id;
|
]]>
|
</sql>
|
|
<sql name="updateBuDevelopmentDeliveryReturn">
|
<![CDATA[
|
update rep_bu_development
|
inner join (
|
select doc_detail.bu_id ,doc.customer_id, doc.doc_date,doc.position_id position_id, sum(doc_detail.qty) qty , sum(doc_detail.amt_net) amount from so_order_return doc
|
inner join so_order_return_detail doc_detail on doc_detail.parent_id = doc.id
|
where doc.state_code = 'Open'
|
group by doc_detail.bu_id ,doc.customer_id, doc.doc_date,doc.position_id) doc on rep_bu_development.bu_id = doc.bu_id and rep_bu_development.customer_id = doc.customer_id and rep_bu_development.doc_date = doc.doc_date and rep_bu_development.position_id = doc.position_id
|
set rep_bu_development.delivery_qty = rep_bu_development.delivery_qty - doc.qty, rep_bu_development.delivery_amount = rep_bu_development.delivery_amount - doc.amount ;
|
]]>
|
</sql>
|
|
<sql name="appendBuDevelopmentPaymentDimension">
|
<![CDATA[
|
INSERT INTO `huihe`.`rep_bu_development` (
|
`id`, `idx`, year, quarter, month, `doc_date`, `hospital_id`, `bu_id`, `customer_id`, `position_id`,
|
`is_open`, `open_date`, `is_authorization`, `authorization_id`, `authorization_date_from`, `authorization_date_to`,
|
`implant_qty`, `implant_amount`, `payment_amount`, `enter_qty`, `delivery_qty`, `delivery_amount`, `price`)
|
select
|
MD5(UUID_SHORT())`id`,null `idx`, md_peroid.year, md_peroid.quarter, md_peroid.month, doc.payment_date `doc_date`, null `hospital_id`, doc.`bu_id`, doc.`customer_id`, doc.position_id `position_id`,
|
'F'`is_open`, null `open_date`, 'F'`is_authorization`, null `authorization_id`, null `authorization_date_from`, null`authorization_date_to`,
|
0 `implant_qty`, 0 `implant_amount`, 0 `payment_amount`, 0`enter_qty`, 0`delivery_qty`, 0 `delivery_amount`, 0 `price`
|
from so_payment doc
|
left join md_peroid on md_peroid.date_from <= doc.payment_date and doc.payment_date > md_peroid.date_to
|
where not EXISTS (
|
select 1 from rep_bu_development
|
where rep_bu_development.bu_id = doc.bu_id and rep_bu_development.customer_id = doc.customer_id and rep_bu_development.doc_date = doc.payment_date and rep_bu_development.position_id = doc.position_id )
|
group by doc.bu_id ,doc.customer_id, doc.payment_date,doc.position_id;
|
]]>
|
</sql>
|
|
<sql name="updateBuDevelopmentPayment">
|
<![CDATA[
|
update rep_bu_development
|
inner join (
|
select doc.bu_id ,doc.customer_id, doc.payment_date doc_date,doc.position_id position_id, sum(doc.amt) amount from so_payment doc
|
group by doc.bu_id ,doc.customer_id, doc.payment_date,doc.position_id) doc on rep_bu_development.bu_id = doc.bu_id and rep_bu_development.customer_id = doc.customer_id and rep_bu_development.doc_date = doc.doc_date and rep_bu_development.position_id = doc.position_id
|
set rep_bu_development.payment_amount = rep_bu_development.payment_amount + doc.amount;
|
]]>
|
</sql>
|
|
<sql name="appendBuDevelopmentAuthorizationDimension">
|
<![CDATA[
|
INSERT INTO `huihe`.`rep_bu_development` (
|
`id`, `idx`, `doc_date`, `hospital_id`, `bu_id`, `org_id`, `customer_id`, `position_id`,
|
`is_open`, `open_date`, `is_authorization`, `authorization_id`, `authorization_date_from`, `authorization_date_to`,
|
`implant_qty`, `implant_amount`, `payment_amount`, `enter_qty`, `delivery_qty`, `delivery_amount`, `price`)
|
select
|
md5(UUID_SHORT()) id , null idx, md_org_account_hospital.date_from doc_date, md_org_account_hospital.hospital_id, md_org_account_hospital.bu_id, md_org_account.org_id, md_org_account.id customer_id, md_org_account_hospital.position_id,
|
'F'`is_open`,md_org_account.open_date `open_date`, 'T'`is_authorization`, md_org_account_hospital.id `authorization_id`, md_org_account_hospital.date_from `authorization_date_from`, md_org_account_hospital.date_to`authorization_date_to`,
|
0 `implant_qty`, 0`implant_amount`, 0`payment_amount`, 0`enter_qty`, 0 `delivery_qty`, 0 `delivery_amount`, 0`price`
|
from md_org_account_hospital
|
left join md_org_account on md_org_account.org_id = md_org_account_hospital.org_id
|
left join rep_bu_development on rep_bu_development.customer_id = md_org_account.id and rep_bu_development.bu_id = md_org_account_hospital.bu_id and rep_bu_development.position_id = md_org_account_hospital.position_id and rep_bu_development.hospital_id = md_org_account_hospital.hospital_id
|
where rep_bu_development.id is null and md_org_account_hospital.state_code = 'Open';
|
]]>
|
</sql>
|
|
<sql name="appendBuDevelopmentOpenDimension">
|
<![CDATA[
|
INSERT INTO `huihe`.`rep_bu_development` (
|
`id`, `idx`, `doc_date`, `hospital_id`, `bu_id`, org_id,`customer_id`, `position_id`,
|
`is_open`, `open_date`, `is_authorization`, `authorization_id`, `authorization_date_from`, `authorization_date_to`,
|
`implant_qty`, `implant_amount`, `payment_amount`, `enter_qty`, `delivery_qty`, `delivery_amount`, `price`)
|
select
|
md5(UUID_SHORT()) id , null idx, md_org_account.open_date doc_date, md_org_account_hospital.hospital_id, md_org_account_bu.bu_id, md_org_account.org_id, md_org_account.id customer_id, md_org_account_position.position_id,
|
'F'`is_open`,md_org_account.open_date `open_date`, if(md_org_account_hospital.id is null and md_org_account_hospital.state_code = 'Open', 'F', 'T')`is_authorization`, md_org_account_hospital.id `authorization_id`, md_org_account_hospital.date_from `authorization_date_from`, md_org_account_hospital.date_to`authorization_date_to`,
|
0 `implant_qty`, 0`implant_amount`, 0`payment_amount`, 0`enter_qty`, 0 `delivery_qty`, 0 `delivery_amount`, 0`price`
|
from md_org_account_bu
|
inner join md_org_account on md_org_account.id = md_org_account_bu.account_id
|
left join md_org_account_hospital on md_org_account_hospital.org_id = md_org_account.org_id
|
left join md_org_account_position on md_org_account_position.parent_id = md_org_account.id
|
where not EXISTS (select 1 from rep_bu_development
|
where rep_bu_development.customer_id = md_org_account.id and rep_bu_development.bu_id = md_org_account_bu.bu_id) and md_org_account.state_code in ('Open','Complete')
|
]]>
|
</sql>
|
|
<sql name="updateBuDevelopmentOpen">
|
<![CDATA[
|
update rep_bu_development
|
inner join md_org_account on md_org_account.id = rep_bu_development.customer_id and state_code in ('Open','Complete')
|
inner join md_org_account_bu on md_org_account_bu.account_id = md_org_account.id and rep_bu_development.bu_id = md_org_account_bu.bu_id
|
set rep_bu_development.is_open = 'T' ,
|
rep_bu_development.open_date = ifnull(md_org_account.open_date, '2024-01-01'),
|
rep_bu_development.org_id = md_org_account.org_id
|
]]>
|
</sql>
|
|
<sql name="updateBuDevelopmentAuthorization">
|
<![CDATA[
|
update rep_bu_development
|
inner join md_org_account_hospital on rep_bu_development.org_id = md_org_account_hospital.org_id
|
and rep_bu_development.bu_id = md_org_account_hospital.bu_id
|
and rep_bu_development.position_id = md_org_account_hospital.position_id
|
and rep_bu_development.hospital_id = md_org_account_hospital.hospital_id
|
and md_org_account_hospital.state_code = 'Open'
|
set rep_bu_development.is_authorization = 'T',
|
rep_bu_development.authorization_date_from = md_org_account_hospital.date_from,
|
rep_bu_development.authorization_date_to = md_org_account_hospital.date_to,rep_bu_development.authorization_id = md_org_account_hospital.id ;
|
]]>
|
</sql>
|
|
<sql name="updateBuDevelopmentRegion">
|
<![CDATA[
|
update rep_bu_development
|
inner join md_org_account_hospital on rep_bu_development.org_id = md_org_account_hospital.org_id
|
and rep_bu_development.bu_id = md_org_account_hospital.bu_id
|
and rep_bu_development.position_id = md_org_account_hospital.position_id
|
and rep_bu_development.hospital_id = md_org_account_hospital.hospital_id
|
and md_org_account_hospital.state_code = 'Open'
|
set rep_bu_development.is_authorization = 'T',
|
rep_bu_development.authorization_date_from = md_org_account_hospital.date_from,
|
rep_bu_development.authorization_date_to = md_org_account_hospital.date_to,rep_bu_development.authorization_id = md_org_account_hospital.id ;
|
]]>
|
</sql>
|
|
<sql name="updateBuDevelopmentPeriord">
|
<![CDATA[
|
update rep_bu_development
|
set year = YEAR(doc_date), month = LPAD(MONTH(doc_date),2,'0'), quarter = quarter(doc_date)
|
where month is null
|
]]>
|
</sql>
|
|
</dataSpace>
|
|
</sqls>
|