Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 28

select a.

ConsignmentId as ConsignmentId,
a.Infinity_Type as Source_Cost_Center,
a.Destination_cost_center as Destination_cost_center,
a.Trade_type_flag as Trade_type_flag,
a.MSP_flag as MSP_flag,
a.vendor_PriceMode as source_vendor_PriceMode,
a.PO_Profit_sharing as
source_vendor_profit_sharing,
a.CreationDate as CreationDate,
a.status_of_Consignment as status_of_Consignment,
a.Consignment_createdby as Consignment_createdby,
a.Final_Consignment_status as Final_Consignment_status,
a.ConsignmentFreeze_flag as ConsignmentFreeze_flag,
a.Recovery_consignment_flag as
Recovery_consignment_flag,
a.FC_Received_consignment_flag as
FC_Received_consignment_flag,
case
when a.source_facility like '%Veggies_Infinity%' or
a.DestinationFacility like '%Veggies_Infinity%' or
(ifnull(a.Destination_cost_center, 99) <> ifnull(a.Infinity_Type,
99) and
a.Destination_cost_center is not null and a.Infinity_Type is not
null) then 1
else 0 end as Inter_BU_Trade_flag,
a.TradeFinance_flag as TradeFinance_flag,
a.CashBack_flag as CashBack_flag,
a.FixedOrderMode_flag as FixedOrderMode_flag,

a.supply_City as supply_City,
a.source_facility as source_facility,
a.DestinationFacility as DestinationFacility,
a.Destination_City as Destination_City,
a.PO_date as PO_date,
a.sodate as sodate,
date(a.E_patti_uploaded_date ) as
E_patti_uploaded_date,
a.Cn_freezed_date as Cn_freezed_date,
a.TripId as TripId,
a.Payment_Mode as Logistics_payment_type,
a.Total_POs as Total_POs,

a.POs_withPP as POs_withPP,
a.Dispatched_POs as Dispatched_POs,
a.Dispatched_time_at_source as
Dispatched_time_at_source,
a.Arrivedtime as Arrivedtime,
a.allocation_time_stamp as allocation_time_stamp,
a.PO_Ids as PO_Ids,
a.Skus as Skus,
a.SOIds as SOIds,
round(a.Initial_PP_value / a.Total_PO_kg, 2) as Initial_PP,
round(a.PO_value / a.Total_PO_kg, 2) as Final_PP,
a.Total_PO_kg as Total_PO_kg,
a.PO_LotQuantity as Purchase_units,
a.Allocated_units as Allocated_units,
a.Allocated_kg as Allocated_kg,
a.Record_sale_kg as Record_sale_kg,
a.Record_sale_value as Record_sale_value,
a.Sold_units as E_patti_sold_units,
a.sold_kg as E_patti_sold_kg,
a.PO_Sale_diff as PO_Sale_diff,
a.PO_value as PO_value,
a.Advance_amount as PO_Advance_amount,
a.DiscountAmount as DiscountAmount,
a.DestinationCommissionAmount as
DestinationVendorClawback_Amount,
a.Consignment_Service_cost as Consignment_Service_cost,
a.sales_service_cost as sales_service_cost,
a.po_service_cost as po_service_cost,
a.External_Logistics_cost as External_Logistics_cost,
a.NC_Logistics_cost as NC_Logistics_cost,
a.Total_service_cost as Total_service_cost,
a.Landing_value as Landing_value,
a.SOValue as SOValue,
a.Net_SO_value as Net_SO_value,
a.Net_margin_value+ifnull(i.SO_Interest_amount,0)
as Net_margin_value,
round((a.Net_margin_value+ifnull(i.SO_Interest_amount,0))/ a.sold_kg ,2)
as Net_margin_Kg,
a.Net_Collectable_amount as Net_Collectable_amount,

a.CDR_Outstanding_amount as CDR_Outstanding_amount,
a.NEFT_amount as NEFT_amount,
a.Neft_approved_amount as Neft_approved_amount,
a.NEFT_Approved_status as NEFT_Approved_status,
a.NEFT_Rejected_amount as NEFT_Rejected_amount,
a.NEFT_raised_date as NEFT_raised_date,
a.UPI_amount as UPI_amount,
a.cashpayment as cashpayment,
a.ccramount as ccramount,
a.CCR_Id as CCR_Id,
a.Amount_collected_date_CCR as CCR_date,
a.CCR_status as CCR_status,
a.CDR_Date as CDR_Date,
a.max_CDR_date as max_CDR_date,
a.Deposit_VerificationStatus as CDR_status,
a.source_vendor as source_vendor,
a.Destination_vendor as Destination_vendor,
yearweek(a.Cn_freezed_date, 1) as SO_week,
a.AdvanceFixedOrder_flag as AdvanceFixedOrder_flag,
a.Trade_Finace_Type as Trade_Finace_Type,
a.Collected_amount as Collected_amount,
a.collected_amount_by_cash as collected_amount_by_cash,
a.collected_amount_by_online as
collected_amount_by_online,
a.creationType as creationType,
b.BuyerVerification as BuyerVerification,
b.SellerVerificaion as SellerVerificaion,
a.source_vendorid as source_vendorid,
a.Destination_vendorId as Destination_vendorId,
cna.PO_Credit_Note_amount as PO_Credit_Note_amount,
cna.PO_Debit_Note_amount as PO_Debit_Note_amount,
cna.SO_Credit_Note_amount as SO_Credit_Note_amount,
cna.SO_Debit_Note_amount as SO_Debit_Note_amount,
a.Approved_POs as Approved_POs,
a.system_outstanding_amount as
system_outstanding_amount,
TR.TradePlanId as TradePlan_Id,
TR.Asm_AsgardUserId as Asm_AsgardUserId,
TR.Asm_Email as Asm_Email,
TR.Asm_ContactNumber as Asm_ContactNumber,
TR.CH_AsgardUserId as CH_AsgardUserId,
TR.CH_Email as CH_Email,
TR.CH_ContactNumber as CH_ContactNumber,
TR.POShippingTerms as POShipping_Terms,
TR.POTransitWeightLoss as POTransit_WeightLoss,
TR.POPaymentTerms as POPayment_Terms,
TR.SOShippingTerms as SOShipping_Terms,
TR.SOTransitWeightLoss as SOTransit_WeightLoss,
a.cash_payment_approved as cash_payment_approved,
a.CDR_raised_amount as CDR_raised_amount,
a.system_trip_cost as system_trip_cost,
a.ParentSkus as ParentSkus,
a.Net_margin_value +ifnull(i.SO_Interest_amount,0)
+ifnull(cna.PO_Debit_Note_amount,0) + ifnull(cna.SO_Debit_Note_amount,0) -
ifnull(cna.PO_Credit_Note_amount,0)-ifnull(cna.SO_Credit_Note_amount,0) as
Adj_Net_margin_value,
ifnull(i.SO_Interest_amount,0) as SO_Interest_amount,
a.CashBackDestination as CashBackDestination_flag,
a.CashBackSource as CashBackSource_flag

from (
select base.ConsignmentId
as ConsignmentId,
base.Infinity_Type
as Infinity_Type,
sale.Destination_cost_center
as Destination_cost_center,
base.Trade_type_flag
as Trade_type_flag,
base.ConsignmentFreeze_flag,
case when ifnull(msp.MSP_count, 0) > 0 then 1 else 0 end
as MSP_flag,
case when dest.f_typeid = 1 then 1 else 0 end
as Recovery_consignment_flag,
case when dest.dest_f_type = 1 then 1 else 0 end
as FC_Received_consignment_flag,
base.Trade_Finace_Type,
base.flag
as flag,
base.TradeFinance_flag,
base.CreationDate
as CreationDate,
base.status_of_Consignment
as status_of_Consignment,
base.Consignment_createdby
as Consignment_createdby,
case
when round(sale.SOValue, 0) > 0 and
ifnull(fin.Net_Collectable_amount, 0) > 0 and
ifnull(fin.CDR_Outstanding_amount, 0) = 0
then 'Closed'
when round(sale.SOValue, 0) > 0 and
ifnull(fin.Net_Collectable_amount, 0) > 0 and
ifnull(fin.outstanding_amount, 0) = 0 then 'Cash
Collected'
when round(sale.SOValue, 0) > 0 then 'E-patti Progress'
when t.Arrived_time is not null and ifnull(sale.RecQty1, 0) = 0
then 'Received'
when ((count(distinct base.PO_id) =
count(distinct case when base.po_status = 5 then
base.PO_id end) and
count(distinct base.PO_id) > 0 and
count(distinct case when base.po_status = 5 then
base.PO_id end) > 0) or
t.Dispatched_time_at_source is not null) then 'Dispatched'
else base.status_of_Consignment end
as Final_Consignment_status,
ifnull(base.City, dest.source_city)
as supply_City,
ifnull(base.SupplyCluster, dest.source)
as source_facility,
ifnull(sale.Destination_Market, dest.Destination)
as DestinationFacility,
ifnull(sale.Destination_City, dest.Destination_City)
as Destination_City,
group_concat(distinct podate)
as PO_date,
sale.max_sodate
as sodate,
count(distinct base.PO_id)
as Total_POs,
count(distinct case when base.PP_approved_flag=1 then base.PO_id
end) as Approved_POs,
count(distinct case when base.PP_flag = 1 then base.PO_id end)
as POs_withPP,
count(distinct case when base.po_status = 5 then base.PO_id end)
as Dispatched_POs,
t.Dispatched_time_at_source
as Dispatched_time_at_source,
t.Arrived_time
as Arrivedtime,
tc.TripId
as TripId,
tc.Payment_Mode
as Payment_Mode,
al_so.allocated_so_id
as allocated_so_id,
group_concat(distinct base.PO_id separator '|')
as PO_Ids,
group_concat(distinct base.Sku)
as Skus,
group_concat(distinct base.ParentSku)
as ParentSkus,
ifnull(dis.Advance_amount, 0)
as Advance_amount,
ifnull(dis.DiscountAmount, 0)
as DiscountAmount,
ifnull(vc.DestinationCommissionAmount, 0)
as DestinationCommissionAmount,

sum(ifnull(base.PO_Qty, 0))
as Total_PO_kg,
sum(ifnull(base.PO_LotQuantity, 0))
as PO_LotQuantity,
al_so.allocation_time_stamp,
sale.E_patti_uploaded_date
as E_patti_uploaded_date,
sale.SOId
as SOIds,
al_so.al_so
as Allocated_kg,
al_so.Allocated_units
as Allocated_units,
if(ifnull(base.c_type, 0) = 2, ifnull(cc.Kg_per_crate, 22) *
rec_so.Rec_so,
rec_so.Rec_so)
as Record_sale_kg,
ifnull(rec_so.Rec_so_value, 0)
as Record_sale_value,
sale.RecQty1
as sold_kg,
sale.Sold_units
as Sold_units,
sum(ifnull(base.PO_Qty, 0)) - ifnull(sale.RecQty1, 0)
as PO_Sale_diff,
round(sum(ifnull(base.PO_value, 0)), 0)
as PO_value,
round(sum(ifnull(base.Initial_PP_value, 0)), 0)
as Initial_PP_value,
##Initial_PP_value
round(ifnull(cost.cost, 0), 0)
as Consignment_Service_cost,
round(ifnull(sale_cost.sales_service_cost, 0), 0)
as sales_service_cost,
round(ifnull(po_cost.po_service_cost, 0), 0)
as po_service_cost,
round(ifnull(po_cost.Transportation_cost, 0) +
ifnull(cost.Transportation_cost, 0) +
ifnull(sale_cost.Transportation_cost, 0))
as External_Logistics_cost,

ifnull(tc.Logistics_cost, 0)
as NC_Logistics_cost,
round(ifnull(cost.cost, 0), 0) +
round(ifnull(po_cost.po_service_cost, 0), 0) +
round(ifnull(sale_cost.sales_service_cost, 0), 0) +
ifnull(tc.Logistics_cost, 0)
as Total_service_cost,
round(sum(ifnull(base.PO_value, 0)), 0) + round(ifnull(cost.cost,
0), 0) +
round(ifnull(po_cost.po_service_cost, 0), 0) +
ifnull(tc.Logistics_cost, 0)
as Landing_value,
round(sale.SOValue, 0)
as SOValue,
round(sale.SOValue, 0) -
round(ifnull(sale_cost.sales_service_cost, 0), 0)
as Net_SO_value,

round(sale.SOValue, 0) + ifnull(vc.DestinationCommissionAmount, 0)
-
round(sum(ifnull(base.PO_value, 0)), 0) -
(round(ifnull(cost.cost, 0), 0) +
round(ifnull(po_cost.po_service_cost, 0), 0) +
round(ifnull(sale_cost.sales_service_cost, 0), 0) +
ifnull(tc.Logistics_cost, 0))
as Net_margin_value,
round((round(sale.SOValue, 0) +
ifnull(vc.DestinationCommissionAmount, 0) -
round(sum(ifnull(base.PO_value, 0)), 0) -
(round(ifnull(cost.cost, 0), 0) +
round(ifnull(po_cost.po_service_cost, 0), 0) +
round(ifnull(sale_cost.sales_service_cost, 0), 0) +
ifnull(tc.Logistics_cost, 0))) /
sale.RecQty1,
2)
as Net_margin_Kg,
ifnull(fin.Net_Collectable_amount, 0)
as Net_Collectable_amount,
ifnull(fin.outstanding_amount,0)
as system_outstanding_amount,
ifnull(fin.CDR_Outstanding_amount, 0)
as CDR_Outstanding_amount,
ifnull(fin.NEFT_amount, 0)
as NEFT_amount,
ifnull(fin.Neft_approved_amount,0)
as Neft_approved_amount,
ifnull(fin.NEFT_Rejected_amount, 0)
as NEFT_Rejected_amount,
fin.NEFT_raised_date
as NEFT_raised_date,
fin.NEFT_Approved_status
as NEFT_Approved_status,
ifnull(fin.cashpayment, 0)
as cashpayment,
ifnull(fin.ccramount, 0)
as ccramount,
ifnull(fin.CDR_raised_amount,0)
as CDR_raised_amount,
ifnull(fin.Approved_ccramount, 0)
as Approved_ccramount,
ifnull(fin.cash_payment_approved,0)
as cash_payment_approved,
ifnull(fin.UPI_amount, 0)
as UPI_amount,
fin.ccrid
as CCR_Id,
fin.Amount_collected_date_CCR
as Amount_collected_date_CCR,
fin.ccr_status
as CCR_status,
fin.CDR_Date
as CDR_Date,
fin.max_CDR_date
as max_CDR_date,
fin.Deposit_VerificationStatus
as Deposit_VerificationStatus,
group_concat(distinct base.vendor_PriceMode)
as vendor_PriceMode,
group_concat(distinct base.PO_Profits)
as PO_Profit_sharing,
base.Cn_freezed_date,
base.CashBack_flag,
group_concat(distinct base.vendorname)
as source_vendor,
ifnull(sale.Destination_vendor ,al_so.Destination_vendor)
as Destination_vendor,
base.FixedOrderMode_flag,
base.creationType,
base.AdvanceFixedOrder_flag,
case
when ifnull(fin.CDR_Outstanding_amount, 0) <= 0 and
ifnull(fin.Net_Collectable_amount, 0) > 0
then ifnull(fin.Net_Collectable_amount, 0)
else 0 end
as Collected_amount,
case
when ifnull(fin.CDR_Outstanding_amount, 0) <= 0 and
ifnull(fin.Net_Collectable_amount, 0) > 0 then
ifnull(fin.Net_Collectable_amount, 0) -
ifnull(fin.NEFT_amount, 0) -
ifnull(fin.UPI_amount, 0)
else 0 end
as collected_amount_by_cash,
case
when ifnull(fin.CDR_Outstanding_amount, 0) <= 0 and
ifnull(fin.Net_Collectable_amount, 0) > 0
then ifnull(fin.NEFT_amount, 0) + ifnull(fin.UPI_amount, 0)
else 0 end
as collected_amount_by_online,
group_concat(distinct base.vendorid )
as source_vendorid,
ifnull(sale.Destination_vendorId,al_so.Destination_vendorId) as
Destination_vendorId,
ifnull(tc.system_trip_cost,0) as system_trip_cost,
base.CashBackSource as CashBackSource,
base.CashBackDestination

from (
select c.id
as ConsignmentId,
c.Type
as c_type,
if(c.type is not null, 'new flow', 'existing flow')
as flag,
case
when c.TradeFinanceType = 1 then 'Local'
when c.TradeFinanceType = 2
then 'National' end
as Trade_Finace_Type,

case
when ifnull(c.Type, 0) <> 3 and ifnull(c.PriceMode, 0)
= 2 then 'Price realisation'
when ifnull(c.Type, 0) <> 3 and ifnull(c.PriceMode, 0)
= 1
then 'Price commitment' end
as Trade_type_flag,
c.TradeFinance
as TradeFinance_flag,
c.CreationDate
as CreationDate,
c.AdvanceFixedOrder
as AdvanceFixedOrder_flag,
cc.name
as Infinity_Type,
c.Status
as status_of_Consignment,
c.ConsignmentFreeze
as ConsignmentFreeze_flag,
au.UserName
as Consignment_createdby,
ci.name
as City,
f.name
as SupplyCluster,
if(f.TypeId = 1, 1, 0)
as Recovery_consignment_flag,
po.id
as PO_id,
s1.name
as ParentSku,
s.name
as Sku,
po.status
as po_status,
po.Deliverydate
as podate,
pod.SkuQuantity * ifnull(sco.ConversionToKgs, 1)
as PO_Qty,
ifnull(poda.LotQuantity, 0)
as PO_LotQuantity,
pod.PurchasePrice
as PP,
pod.SkuQuantity * pod.PurchasePrice
as PO_value,
case when ifnull(pod.PurchasePrice, 0) > 0 then 1 else 0
end as PP_flag,
po.signOff
as PP_approved_flag,
poda.InitialPurchasePrice
as InitialPurchasePrice,
poda.InitialPurchasePrice * pod.SkuQuantity
as Initial_PP_value,
case
when vdm.PriceMode = 1 then 'Price commitment'
when vdm.PriceMode = 2
then 'Price Realisation' end
as vendor_PriceMode,
vdm.Profits
as PO_Profits,
case
when date(c.FreezedAt) <= '2021-10-24' then
date(c.FreezedAt)
else date(addtime(c.FreezedAt, 05:30:00"")) end
as Cn_freezed_date"
c.CashBack
as CashBack_flag
c.FixedOrderMode
as FixedOrderMode_flag
c.creationType
as creationType
v.name
as vendorname
po.vendorid
as vendorid
c.CashBackSource
as CashBackSource
c.CashBackDestination
as CashBackDestination
from Consignment c
left join GenericMapping gm on gm.FromRefId = c.id and
gm.Deleted = 0
left join PurchaseOrder po
on po.id = gm.ToRefId and gm.ToRefType =
'PURCHASE_ORDER' and po.Deleted = 0 and
ifnull(gm.status 99) not in
('DISCARDED')
left join VendorMarketDetail vdm
on vdm.vendorId = po.vendorid and
vdm.CostCenterId = c.Type and vdm.Deleted = 0
left join Vendor v on v.id = po.vendorId and v.Deleted =
0
left join PurchaseOrderDetails pod on
pod.PurchaseOrderId = po.id and pod.Deleted = 0
left join PurchaseOrderDetailsAdditionInfo poda

on poda.PurchaseOrderDetailsId = pod.id and


poda.Deleted = 0
left join GRNDetails gd on gd.PurchaseOrderDetailsId =
pod.id and gd.Deleted = 0
left join Facility f on f.id = po.FacilityId and
f.Deleted = 0
left join City ci on ci.id = f.CityId and ci.Deleted = 0

left join Sku s on s.id = pod.SkuId


left join SkuToSkuMap sts on sts.SkuId = pod.SkuId and
sts.Deleted = 0
left join Sku s1 on s1.id = sts.ParentSkuid and
s1.Deleted = 0
left join AsgardUser au on au.id = c.CreatedBy and
au.Deleted = 0
left join SkuConfiguration sco on sco.SkuId = pod.SkuId

left join CostCenter cc on cc.id = c.Type and cc.Deleted


= 0
where c.id IN (30193)
and c.Deleted = 0
and po.Type in (1 9)

) base
left join
(
select c.id
as Consignment_ID
cc.name
as Destination_cost_center
max(so.Deliverydate)
as max_sodate
c.CreationDate
as Consignment_Creation_Date
c.status
as Status_of_Consignment
au.username
as Consignment_created_by
group_concat(distinct so.id)
as SOId
so.deleted
as SO_Deleted
group_concat(distinct ci.name)
as Destination_City
group_concat(distinct f.name)
as Destination_Market
group_concat(distinct cu.name)
as Destination_vendor
group_concat(distinct so.customerid )
as Destination_vendorId
group_concat(distinct s.name)
as SKU_Name
s1.name
as ParentSkuName
ifnull(sum(sod.SkuQuantity * ifnull(skuc.ConversionToKgs
1)) 0) as Grade_1_Act_Qty_1
ifnull(sum(sod.fulfilledquantity *
ifnull(skuc.ConversionToKgs 1)) 0) as RecQty1
sum(ifnull(sodm.LotQuantity 0))
as Sold_units
cr.username
as SO_createdby
cd.decode
as SO_Delivery_Status
sod.saleprice
as SO_SP
i.PaymentStatusText
as PaymentStatus

ifnull(round(sum(sod.fulfilledquantity * sod.saleprice)
2) 0) as SOValue

ifnull(case when i.PaymentStatus in (2 3) then


i.TotalInvoiceValue end 0) as SO_PaidAmount
max(iao.E_patti_uploaded_date)
as E_patti_uploaded_date

from Consignment c
left join GenericMapping gm on gm.FromRefId = c.id and
gm.Deleted = 0
left join SaleOrder so on so.id = gm.ToRefId and
so.deleted = 0
left join SaleOrderAdditionalInfo soa on soa.SaleOrderId
= so.id and soa.Deleted = 0
left join AsgardUser au on au.id = c.createdby

left join Facility f on f.id = so.facilityid


left join City ci on ci.id = f.cityid
left join Customer cu on cu.id = so.customerid

left join SaleOrderDetails sod on sod.saleorderid =


so.id and sod.deleted = 0
left join SodMarketDetails sodm on sodm.SodId = sod.id
and sodm.Deleted = 0
left join Sku s on s.id = sod.skuid
left join SkuConfiguration skuc on skuc.skuid =
sod.skuid
left join SkuToSkuMap sts on sts.SkuId = sod.SkuId and
sts.Deleted = 0
left join Sku s1 on s1.id = sts.parentskuid

left join AsgardUser cr on cr.id = so.createdby

left join CodeDecode cd


on cd.code = so.status and cd.codegroup like
'SALEORDER_STATUS' and cd.deleted = 0
left join Invoice i on i.saleorderid = so.id and
i.deleted = 0
left join CostCenter cc on cc.id = soa.CostCenterId and
cc.Deleted = 0
####so patti
left join (
select InvoiceId
group_concat(distinct UploadedInvoiceImageUrl)
as E_patti
date(min(addtime(iao.UploadedInvoiceImageAt
"05:30:00"))) as E_patti_uploaded_date
from InvoiceAdditionalInfo iao
where iao.Deleted = 0
group by iao.InvoiceId
) iao on iao.InvoiceId = i.id
where gm.ToRefType in ('SALE_ORDER' 'EPATTI_SALE_ORDER')

and c.deleted = 0
and so.Type in (90 91 95)
and c.id IN (30193)

group by c.id
) sale on sale.Consignment_ID = base.ConsignmentId
left join
(
select sd.RefId
as ConsignmentId
ifnull(sum(case when sdct.CostType = 'TRANSPORTATION' then
sdi.DocumentValue end)
0)
as Transportation_cost
ifnull(sum(case when sdct.CostType = 'LABOR' then
sdi.DocumentValue end)
0)
as Labour_cost
ifnull(sum(case
when sdct.CostType not in ('LABOR'
'TRANSPORTATION')
then sdi.DocumentValue end)
0)
as Other_cost
sum(sdi.DocumentValue)
as cost

group_concat(distinct sdct.CostSubType separator ' ')


as Cost_type
from ServiceDocument sd
join ServiceDocumentInfo sdi on sdi.ServiceDocumentId =
sd.id and sdi.Deleted = 0
join ServiceDocumentCostType sdct
on sdct.id = sdi.ServiceDocumentCostTypeId and
sdct.Deleted = 0
join Consignment c on c.id = sd.RefId and c.Deleted = 0

where c.id IN (30193)


and sd.RefType = 'CONSIGNMENT'
and sd.Deleted = 0
and sdi.status not in ('REJECTED' 'CANCELLED')

group by sd.RefId
) cost on cost.ConsignmentId = base.ConsignmentId

left join
(
select c.id
as Consignment_ID
c.CreationDate
as Consignment_Creation_Date
c.status
as Status_of_Consignment
so.id
as SOId
ifnull(sum(case when sdct.CostType = 'TRANSPORTATION' then
sdi.DocumentValue end)
0)
as Transportation_cost
ifnull(sum(case when sdct.CostType = 'LABOR' then
sdi.DocumentValue end)
0)
as Labour_cost
ifnull(sum(case when sdct.CostType = 'COMMISSION' then
sdi.DocumentValue end)
0)
as Commission_cost
ifnull(sum(case when sdct.CostType not in ('COMMISSION')
then sdi.DocumentValue end)
0)
as Other_sales_cost

sum(sdi.DocumentValue)
as sales_service_cost

from Consignment c
left join GenericMapping gm on gm.FromRefId = c.id and
gm.Deleted = 0
left join SaleOrder so on so.id = gm.ToRefId and
so.deleted = 0
left join ServiceDocument sd on sd.RefId = so.id and
sd.RefType = 'SALE_ORDER'
join ServiceDocumentInfo sdi on sdi.ServiceDocumentId =
sd.id and sdi.Deleted = 0
join ServiceDocumentCostType sdct
on sdct.id = sdi.ServiceDocumentCostTypeId and
sdct.Deleted = 0
join City ci on ci.id = so.CityId
where gm.ToRefType in ('SALE_ORDER' 'EPATTI_SALE_ORDER')

and sd.Deleted = 0
and ci.Deleted = 0
and c.deleted = 0
and gm.FromRefType = 'CONSIGNMENT'
and c.id IN (30193)
and sdi.status not in ('REJECTED' 'CANCELLED')

group by c.id
) sale_cost on sale_cost.Consignment_ID = base.ConsignmentId

left join
(
select c.id
as Consignment_ID
c.CreationDate
as Consignment_Creation_Date
c.status
as Status_of_Consignment
po.id
as SOId
ifnull(sum(case when sdct.CostType = 'TRANSPORTATION' then
sdi.DocumentValue end)
0)
as Transportation_cost
ifnull(sum(case when sdct.CostType = 'LABOR' then
sdi.DocumentValue end)
0)
as Labour_cost
sum(sdi.DocumentValue)
as po_service_cost

from Consignment c
left join GenericMapping gm on gm.FromRefId = c.id and
gm.Deleted = 0
left join PurchaseOrder po on po.id = gm.ToRefId and
po.deleted = 0
left join ServiceDocument sd on sd.RefId = po.id and
sd.RefType = 'PURCHASE_ORDER'
join ServiceDocumentInfo sdi on sdi.ServiceDocumentId =
sd.id and sdi.Deleted = 0
join ServiceDocumentCostType sdct
on sdct.id = sdi.ServiceDocumentCostTypeId and
sdct.Deleted = 0
join City ci on ci.id = po.CityId
where ToRefType = 'PURCHASE_ORDER'
and sd.Deleted = 0
and ci.Deleted = 0
and c.deleted = 0
and gm.FromRefType = 'CONSIGNMENT'
and c.id IN (30193)
and sdi.status not in ('REJECTED' 'CANCELLED')
group by c.id
) po_cost on po_cost.Consignment_ID = base.ConsignmentId

###finance_ccr_cdr
left join
(
select aa.consignmentId
sum(ifnull(aa.Net_Collectable_amount 0)) as
Net_Collectable_amount
sum(ifnull(aa.NEFT_amount 0)) as
NEFT_amount
sum(ifnull(aa.Neft_approved_amount 0))
as Neft_approved_amount
#
sum(ifnull(aa.NEFT_Rejected_amount 0)) as
NEFT_Rejected_amount
sum(ifnull(aa.cashpayment 0)) as
cashpayment
sum(ifnull(aa.cash_payment_approved 0))
as cash_payment_approved
sum(ifnull(aa.CDR_Outstanding_amount 0)) as
CDR_Outstanding_amount
#
sum(ifnull(aa.outstanding_amount 0)) as
outstanding_amount
sum(ifnull(aa.Approved_ccramount 0)) as
Approved_ccramount
sum(ifnull(aa.ccramount 0))
as ccramount
sum(ifnull(aa.CDR_raised_amount 0))
as CDR_raised_amount
sum(ifnull(aa.UPI_amount 0))
as UPI_amount
group_concat(distinct aa.NEFT_Approved_status) as
NEFT_Approved_status
#NEFT_raised_date
group_concat(distinct aa.NEFT_raised_date) as
NEFT_raised_date

group_concat(distinct aa.ccrid) as
ccrid
group_concat(distinct aa.Amount_collected_date_CCR) as
Amount_collected_date_CCR
group_concat(distinct aa.ccr_status) as
ccr_status
group_concat(distinct aa.CDR_Date) as
CDR_Date
max(aa.max_CDR_date) as
max_CDR_date
group_concat(distinct aa.Deposit_VerificationStatus) as
Deposit_VerificationStatus
from (
select aa.consignmentId
aa.soid
aa.Net_Collectable_amount
aa.outstanding_amount
aa.NEFT_Rejected_amount
aa.cashpayment
aa.ccramount
if(aa.CDR_Date is not null and
ifnull(aa.CDR_Rejected_flag 0) = 0 aa.ccramount
0) as
CDR_amount
aa.NEFT_Raised_amount as NEFT_amount

aa.Neft_approved_amount as Neft_approved_amount

aa.NEFT_Approved_status
aa.Approved_ccramount
aa.CDR_raised_amount
aa.cash_payment_approved
aa.Neft_created_date as
NEFT_raised_date
aa.UPI_amount
aa.CDR_Rejected_flag
aa.ccrid
aa.Amount_collected_date_CCR
aa.ccr_status
aa.CDR_Date
aa.max_CDR_date
aa.Deposit_VerificationStatus
ifnull(aa.Net_Collectable_amount 0)-
ifnull(aa.Neft_approved_amount 0)-ifnull(aa.cash_payment_approved 0)-
ifnull(aa.UPI_amount 0) as CDR_Outstanding_amount
from (
select cs.consignmentid
cs.soid
cs.ccr_createdby
cs.Netcollectable_amount as Net_Collectable_amount
cs.OutstandingAmount as outstanding_amount
ifnull(cs.cash_payment_raised 0) as cashpayment
ifnull(cs.ccr_raised_amount 0) as ccramount
ifnull(cs.CDR_raised_amount 0) as CDR_raised_amount
ifnull(cs.cash_payment_approved 0) as cash_payment_approved
ifnull(cs.NEFT_Raised_amount 0) as NEFT_Raised_amount
ifnull(cs.Neft_approved_amount 0) as Neft_approved_amount
ifnull(cs.NEFT_rejected_amount 0) as NEFT_Rejected_Amount
ifnull(cs.UPI_Payment 0) as UPI_amount
group_concat(distinct cs.NEFT_raised_date) as Neft_created_date

group_concat(distinct cs.NEFT_status) as NEFT_Approved_status

sum(case when cs.CDR_VerificationStatus='APPROVED' then


ifnull(cs.ccr_raised_amount 0) end) as Approved_ccramount
if(group_concat(distinct cs.CDR_VerificationStatus) like "%REJECTED%"
1 0) as CDR_Rejected_flag
group_concat(distinct cs.CCR_Ids) as ccrid
group_concat(distinct cs.ccr_reportdate) as Amount_collected_date_CCR

group_concat(distinct cs.collectionreportstatus) as ccr_status

group_concat(distinct cs.CDR_raised_date) as CDR_Date


max(cs.max_CDR_raised_date) as max_CDR_date
group_concat(distinct cs.CDR_VerificationStatus) as
Deposit_VerificationStatus

from
(
select cc.name as costcenter
c.id as consignmentid
so.id as soid
i.id as invoiceid
i.TotalInvoiceValue as Netcollectable_amount
i.OutstandingAmount as OutstandingAmount
so.Deliverydate as sodate
iao.E_patti_uploaded_date as Epatti_date
i.TotalInvoicevalue as Net_collectable_amount
neft.NEFT_raised_date as NEFT_raised_date
neft.Raised_amount as NEFT_Raised_amount
neft.Neft_approved_amount as Neft_approved_amount
neft.NEFT_rejected_amount as NEFT_rejected_amount
neft.NEFT_status as NEFT_status
upi.T_amount as UPI_Payment
cash.cash_payment_raised as cash_payment_raised
cash.ccr_raised_amount as ccr_raised_amount
cash.CDR_raised_amount as CDR_raised_amount
cash.CollectionReportId as CCR_Ids
cash.CDR_VerificationStatus as CDR_VerificationStatus
cash.ccr_createdby as ccr_createdby
cash.CDR_raised_date as CDR_raised_date
cash.max_CDR_raised_date as max_CDR_raised_date
cash.collectionreportstatus
cash.ccr_reportdate
cash.cash_payment_approved
cu.CustomerTypeId
from

Consignment c
left join GenericMapping gm on gm.FromRefId=c.id and gm.Deleted=0 and gm.ToRefType
in ('SALE_ORDER' 'EPATTI_SALE_ORDER' )
left join SaleOrder so on so.id=gm.ToRefId and so.deleted=0
left join SaleOrderAdditionalInfo soa on soa.SaleOrderId=so.id and soa.Deleted=0

left join Invoice i on i.SaleOrderId = so.id and i.Deleted=0


left join Customer cu on cu.id=so.CustomerId and cu.Deleted=0
left join
(
select InvoiceId group_concat(distinct UploadedInvoiceImageUrl) as E_patti

date(min(addtime(iao.UploadedInvoiceImageAt 05:30:00))) as
E_patti_uploaded_date
" (min(addtime(iao.UploadedInvoiceImageAt" 05:30:00))) as
E_patti_uploaded_time_stamp
sum(ifnull(DestinationCommissionAmount 0)) as DestinationCommissionAmount

from
InvoiceAdditionalInfo iao
where iao.Deleted=0
group by iao.InvoiceId
)iao on iao.InvoiceId=i.id
left join CostCenter cc on cc.id=soa.CostCenterId and cc.Deleted=0

###########NEFT
left join
(
select ReferenceId as SaleOrderId
sum(case when status not in ('REJECTED') then InitiatedAmount end) as
Raised_amount
sum(case when status in ('APPROVED') then InitiatedAmount end) as
Neft_approved_amount
sum(case when status in ('REJECTED') then InitiatedAmount end) as
NEFT_rejected_amount
min(date(addtime(CreatedAt 05:30:00))) as NEFT_raised_date
group_concat(distinct status) as NEFT_status
from BankTransferApproval where
ReferenceType='SALE_ORDER' and Deleted=0
group by ReferenceId
)neft on neft.SaleOrderId=so.id
####UPI payments
left join
(
select i.SaleOrderId as soid
sum(case when ifnull(t.Approved 0)=1 then ifnull(t.Amount 0) end) as
T_amount
from
Transactions t
join Invoice i on i.id=t.InvoiceId and i.Deleted=0
join SaleOrder so on so.id=i.SaleOrderId and so.Deleted=0
where t.PaymentType='WALLET' and t.Deleted=0
and so.Type in (90 91 95)
group by i.SaleOrderId
)upi on upi.soid=so.id

####cash payments
left join
(
select a.soid
group_concat(distinct a.TransactionId) as TransactionId
group_concat(distinct a.CollectionReportId) as CollectionReportId

sum(a.cash_payment_raised) as cash_payment_raised
sum(a.cash_payment_approved) as cash_payment_approved
sum(a.ccr_raised_amount) as ccr_raised_amount
sum(case when a.CDR_raised_date is not null and
ifnull(a.CDR_VerificationStatus 99) not like '%REJECTED%' then
ccr_raised_amount end ) as CDR_amount
sum(case when a.CDR_raised_date is not null then ccr_raised_amount end ) as
CDR_raised_amount

group_concat(distinct a.CDR_VerificationStatus ) as CDR_VerificationStatus

group_concat(distinct a.ccr_createdby) as ccr_createdby


group_concat(distinct a.CDR_raised_date) as CDR_raised_date

group_concat(distinct a.collectionreportstatus) as collectionreportstatus

group_concat(distinct a.ccr_reportdate) as ccr_reportdate


max(a.max_CDR_raised_date) as max_CDR_raised_date
from
(
select i.SaleOrderId as soid
t.id as TransactionId
sum(t.Amount) as cash_payment_raised
group_concat(distinct ctm.CollectionReportId) as CollectionReportId

sum(case when ctm.CollectionReportId is not null then t.Amount end) as


ccr_raised_amount
group_concat(distinct crd.VerificationStatus) as CDR_VerificationStatus

sum(case when t.Approved=1 then t.Amount end) as cash_payment_approved

group_concat(distinct crd.CDR_raised_date) as CDR_raised_date

max(crd.CDR_raised_date) as max_CDR_raised_date
group_concat(distinct crd.ccr_createdby) as ccr_createdby
group_concat(distinct crd.collectionreportstatus) as collectionreportstatus

group_concat(distinct crd.ccr_reportdate) as ccr_reportdate

from
Transactions t
join Invoice i on i.id=t.InvoiceId and i.Deleted=0
join SaleOrder so on so.id=i.SaleOrderId and so.Deleted=0
left join GenericMapping gm on gm.ToRefId=so.id and gm.Deleted=0 and gm.ToRefType
in ('SALE_ORDER' 'EPATTI_SALE_ORDER' )
left join Consignment c on c.id=gm.FromRefId and c.Deleted=0
left join
(
select ctm.CollectionReportId
ctm.TransactionId
group_concat(distinct ctm.Status) as status

addtime(ctm.CreatedAt '05:00:00') as crreporttime

from CollectionReportToTransactionMap ctm

join Transactions t
on t.id = ctm.TransactionId and t.Deleted = 0

join Invoice i on i.id = t.InvoiceId and i.Deleted


= 0
join SaleOrder so on so.id = i.SaleOrderId

left join SaleOrderAdditionalInfo soa on


soa.SaleOrderId=so.id and soa.Deleted=0
left join Consignment c on c.id=soa.ConsignmentId and
c.Deleted=0

where ctm.Deleted = 0
and so.Deleted = 0

and so.Type in (90 91 95)

and c.id IN (30193)

group by ctm.CollectionReportId ctm.TransactionId


) ctm on ctm.TransactionId = t.id
left join
(
select cr.Id as COLLECTION_REP_ID
cr.ActualAmount as ccr_amount
au.UserName as ccr_createdby
sum(case when ifnull(crd.VerificationStatus 99) not in ('REJECTED') then
crd.DepositedAmount end) as DEPOSITED_AMOUNT
group_concat(distinct crd.VerificationStatus) as VerificationStatus

sum(case when ifnull(crd.VerificationStatus 99) not in ('REJECTED') then


crd.DepositedAmount end)/cr.ActualAmount as deposited_per
min(date(addtime(crd.CreatedAt 05:30:00))) as CDR_raised_date
group_concat(distinct case
when cr.status = 1 then 'Created'
when cr.status = 2 then 'Partially_Deposited'
when cr.status = 3 then 'Deposited'
when cr.status = 4 then 'Approved'
end ) as
collectionreportstatus
cr.ReportDate as ccr_reportdate
from
CollectionReport cr
left join CollectionReportDetails crd on crd.CollectionReportId = cr.id

left join AsgardUser au on au.id=cr.CreatedBy and au.Deleted=0


where crd.Deleted=0 and cr.Deleted=0
group by cr.id
)crd on crd.COLLECTION_REP_ID=ctm.CollectionReportId

where t.PaymentModeId=1 and t.Deleted=0


and so.Type in (90 91 95)
and c.id IN (30193)
group by i.SaleOrderId t.id
)a
group by a.soid
)cash on cash.soid=so.id

where so.Deleted=0 and so.Type in (90 91 95)


and c.CreationDate between pikachu and charmandar
and c.Deleted=0
)cs
group by cs.soid

) aa
) aa
group by aa.consignmentId
) fin on fin.ConsignmentId = base.ConsignmentId
" "
##allocated so
left join
(
select c.id
as consignmentId
group_concat(distinct so.id)
as allocated_so_id
" group_concat(distinct cu.name)
as Destination_vendor"
group_concat(distinct so.customerid )
as Destination_vendorId
sum(ifnull(sod.SkuQuantity 0) *
ifnull(sco.conversionToKgs 1)) as al_so
sum(ifnull(sodm.LotQuantity 0))
as Allocated_units
min(date(addtime(so.CreatedAt '05:30:00')))
as allocation_date
min((addtime(so.CreatedAt '05:30:00')))
as allocation_time_stamp
from Consignment c
join GenericMapping gm
on gm.FromRefId = c.id and gm.Deleted = 0 and
gm.FromRefType = 'CONSIGNMENT' and
gm.ToRefType = 'ALLOCATED_SALE_ORDER'

join SaleOrder so on so.id = gm.ToRefId and so.Deleted =


0
left join SaleOrderDetails sod on sod.SaleOrderId =
so.id and sod.Deleted = 0
left join SodMarketDetails sodm on sodm.SodId = sod.id
and sodm.Deleted = 0
left join SkuConfiguration sco on sco.SkuId = sod.SkuId
and sco.Deleted = 0
" left join Customer cu on cu.id=so.CustomerId
and cu.deleted=0"
where so.Type = 93
and c.Deleted = 0
and c.Type is not null
and c.id IN (30193)
group by c.id
) al_so on al_so.consignmentId = base.ConsignmentId

######record sale so
left join
(
select c.id
as consignmentId
min(so.Deliverydate)
as allocated_date
time(min(addtime(so.CreatedAt '05:30:00')))
as arrived_time
group_concat(distinct so.id)
as allocated_so_id
sum(ifnull(sod.SkuQuantity 0) *
ifnull(sco.conversionToKgs 1)) as Rec_so
sum(ifnull(sod.SkuQuantity 0) * ifnull(sod.SalePrice
0)) as Rec_so_value
from Consignment c
join GenericMapping gm
on gm.FromRefId = c.id and gm.Deleted = 0 and
gm.FromRefType = 'CONSIGNMENT' and
gm.ToRefType = 'RECORD_SALE_ORDER'

join SaleOrder so on so.id = gm.ToRefId and so.Deleted =


0
join SaleOrderDetails sod on sod.SaleOrderId = so.id and
sod.Deleted = 0
left join SkuConfiguration sco on sco.SkuId = sod.SkuId
and sco.Deleted = 0
where so.Type = 94
and c.Deleted = 0
and c.Type is not null
and c.id IN (30193)
group by c.id
) rec_so on rec_so.consignmentId = base.ConsignmentId

###destination
left join
(
select c.id as consignmentId

group_concat(distinct f.name) as Destination

group_concat(distinct f1.TypeId) as f_typeid

group_concat(distinct f.TypeId) as dest_f_type

group_concat(distinct ci.name) as Destination_city

group_concat(distinct f1.name) as source

group_concat(distinct ci2.name) as source_city

from Consignment c
join ConsignmentFacilityMapping cfm on cfm.ConsignmentId
= c.id and cfm.Deleted = 0
join Facility f on f.id = cfm.DestinationFacilityId and
f.Deleted = 0
left join City ci on ci.id = f.CityId and ci.Deleted = 0

left join Facility f1 on f1.id = cfm.SourceFacilityId


and f1.Deleted = 0
left join City ci2 on ci2.id = f1.CityId and ci2.Deleted
= 0

where c.id IN (30193)


and c.Deleted = 0
group by c.id
) dest on dest.ConsignmentId = base.ConsignmentId

####to get crate conversion


left join
(
select aa.conisgnmentId
aa.Kg_per_crate
aa.Weight_bridge_tonnage
from (
select c.id
as conisgnmentId
tmpi.FullWeight
as FullWeight
tmpi.EmptyWeight
as EmptyWeight
tmpi.CrateCount
as CrateCount
(ifnull(tmpi.FullWeight 0) -
ifnull(tmpi.EmptyWeight 0)) as Weight_bridge_tonnage
(ifnull(tmpi.FullWeight 0) -
ifnull(tmpi.EmptyWeight 0)) /
ifnull(tmpi.CrateCount 0)
as Kg_per_crate
from Consignment c
join GenericMapping gm on gm.FromRefId = c.id
and gm.FromRefType = 'CONSIGNMENT' and
gm.ToRefType = 'TRIP'

join TripProcurementVendorMap tpvm on


tpvm.TripId = gm.ToRefId and tpvm.Deleted = 0
join TripMarketPointInfo tmpi on tmpi.TpvmId =
tpvm.id and tmpi.Deleted = 0
where c.Type = 2
and c.id IN (30193)
having Kg_per_crate is not null
) aa
group by aa.conisgnmentId
) cc on cc.conisgnmentId = base.ConsignmentId
left join
(
select a.ConsignmentId
as ConsignmentId
a.Arrived_time
a.Dispatched_from_source
as Dispatched_time_at_source
timediff(a.Arrived_time Dispatched_from_source)
as time_duration
round(hour(timediff(a.Arrived_time
Dispatched_from_source)) / 24 2) as Transit_days
from (
select c.id
as ConsignmentId
min(case
when tpvm.RoutePointClassificationId = 1

then addtime(tpvm.VehicleLeftTime
"05:30:00") end) as Dispatched_from_source
max(case
when tpvm.RoutePointClassificationId = 3

then addtime(tpvm.VehicleReachedTime
"05:30:00") end) as Arrived_time
from GenericMapping gm
left join TripProcurementVendorMap tpvm

on tpvm.TripProcurementRouteMapId =
gm.ToRefId and tpvm.Deleted = 0
left join Consignment c on c.id = gm.FromRefId
and c.Deleted = 0
where c.id IN (30193)
and gm.FromRefType = 'CONSIGNMENT'
and gm.ToRefType = 'TPRM'
group by c.id
) a
) t on t.ConsignmentId = base.ConsignmentId
####Tripcost
left join
(
select c.id
as consignmentId
t.id
as TripId
case when tad.TripPaymentType = 'NINJACART' then t.cost
else 0 end as Logistics_cost
t.cost
as system_trip_cost
tad.TripPaymentType
as Payment_Mode
t.DriverName
as DriverName
t.VehicleNumber
as VehicleNumber
tpvm.id
as tpvm_id
cd.Decode
as Trip_class
tpvm.Status
as tpvm_status
from Consignment c
join GenericMapping gm
on gm.FromRefId = c.id and gm.FromRefType =
'CONSIGNMENT' and gm.ToRefType = 'TRIP' and
gm.Deleted = 0
join Trip t on t.id = gm.ToRefId and t.Deleted = 0

left join CodeDecode as cd


on cd.code = t.TripClassification and
CodeGroup = 'TRIP CLASSIFICATION' and
cd.deleted = 0
left join TripAdditionalDetail tad on t.id = tad.TripId
and tad.Deleted = 0
left join TripProcurementVendorMap tpvm on tpvm.TripId =
t.id and tpvm.Deleted = 0
where c.id IN (30193)
and c.Deleted = 0
group by c.id
) tc on tc.consignmentId = base.consignmentId
##bulk discount
left join
(
select a.ConsignmentId
sum(ifnull(a.DiscountAmount 0)) as DiscountAmount

sum(ifnull(a.TokenAmount 0)) as Advance_amount

from (
select c.id
as ConsignmentId
po.id
as poid
c.Type
as c_type
if(c.type is not null 'new flow' 'existing
flow') as flag
c.CreationDate
as CreationDate
case
when c.Type = 1 then 'Onion'

when c.Type = 2 then 'Tomato'

when c.Type = 3 then 'CC'


when c.Type = 5 then 'GGC'
when c.Type = 6 then 'Potato' end
as Infinity_Type
c.Status
as status_of_Consignment
poa.DiscountAmount
as DiscountAmount
poa.TokenAmount
as TokenAmount

from Consignment c
left join GenericMapping gm on gm.FromRefId =
c.id and gm.Deleted = 0
left join PurchaseOrder po
on po.id = gm.ToRefId and
gm.ToRefType = 'PURCHASE_ORDER' and
po.Deleted = 0 and
ifnull(gm.status 99) not in ('DISCARDED')
left join PurchaseOrderAdditionInfo poa

on poa.PurchaseOrderId = po.id and


poa.Deleted = 0
where c.id IN (30193)

and c.Deleted = 0
and ((po.Type = 1 and po.SubType = 47) or (po.Type = 9
and po.SubType = 46))
group by c.id po.id
) a
group by a.ConsignmentId
) dis on dis.ConsignmentId = base.ConsignmentId
###destination vendor commission
left join
(
select aa.Consignment_ID as
Consignment_ID
sum(ifnull(aa.DestinationCommissionAmount 0)) as
DestinationCommissionAmount
from (
select c.id as Consignment_ID

so.id as soid

iao.DestinationCommissionAmount as
DestinationCommissionAmount

from Consignment c
left join GenericMapping gm on gm.FromRefId =
c.id and gm.Deleted = 0
left join SaleOrder so on so.id = gm.ToRefId
and so.deleted = 0
left join Invoice i on i.SaleOrderId = so.id
and i.Deleted = 0
left join
(
select InvoiceId
group_concat(distinct
UploadedInvoiceImageUrl) as E_patti

date(min(addtime(iao.UploadedInvoiceImageAt "05:30:00"))) as
E_patti_uploaded_date
sum(ifnull(DestinationCommissionAmount
0)) as DestinationCommissionAmount
from InvoiceAdditionalInfo iao

where iao.Deleted = 0
group by iao.InvoiceId
) iao on iao.InvoiceId = i.id
where gm.ToRefType in ('SALE_ORDER'
'EPATTI_SALE_ORDER')
and c.deleted = 0
and so.Type = 95
and c.id IN (30193)
group by so.id
having DestinationCommissionAmount > 0

) aa
group by aa.Consignment_ID
) vc on vc.Consignment_ID = base.ConsignmentId
left join
(
select c.id
as consignmentId
count(distinct case when sodmd.MinSellingPrice is not null
then SOD.SkuId end) as MSP_count
from SaleOrder so
join Customer C on so.CustomerId = C.id and C.Deleted =
0
join AsgardFacilityConfiguration AFC on C.id =
AFC.CustomerId and AFC.Deleted = 0
join GenericMapping gm
on gm.FromRefType = 'CONSIGNMENT' and gm.ToRefType
= 'INTERFACILITY_SALEORDER' and
cast(gm.ToRefId as unsigned) = so.id and
gm.Deleted = 0
join SaleOrderDetails SOD on so.id = SOD.SaleOrderId and
SOD.Deleted = 0
join SodMarketDetails sodmd on sodmd.SodId = SOD.id and
sodmd.Deleted = 0
left join Consignment c on c.id = gm.FromRefId and
c.Deleted = 0
where MinSellingPrice is not null
and so.Deleted = 0
and c.id IN (30193)
group by c.id
) msp on msp.consignmentId = base.ConsignmentId
group by base.ConsignmentId
) a
left join
(select c.Id as consignmentId
c.CreationDate as creationDate
poadd.OtpStatus as SellerVerificaion
soadd.OtpStatus as BuyerVerification
c.FixedOrderMode as Fixedordermode
c.TradeFinance as Tradefinance
from Consignment c
join GenericMapping pogm on c.Id = pogm.FromRefId and pogm.FromRefType =
'CONSIGNMENT'
and pogm.ToRefType = 'PURCHASE_ORDER'
join PurchaseOrder po on po.Id = pogm.ToRefId
join PurchaseOrderAdditionInfo poadd on po.id = poadd.PurchaseOrderId

join GenericMapping sogm on c.Id = sogm.FromRefId and sogm.FromRefType =


'CONSIGNMENT'
and sogm.ToRefType = 'ALLOCATED_SALE_ORDER'
join SaleOrder so on so.Id = sogm.ToRefId
join SaleOrderAdditionalInfo soadd on soadd.SaleOrderId = so.Id

where c.creationType = 5
and c.id IN (30193)
and c.Deleted = 0
group by c.Id
)b
on b.consignmentId= a.ConsignmentId
left join
(
select cn.consignmentid
sum(case when cn.Reference_doc_type='PURCHASE_ORDER' and
cn.Instrument='CREDIT_NOTE' then cn.Value_of_note end) as PO_Credit_Note_amount

sum(case when cn.Reference_doc_type='PURCHASE_ORDER' and


cn.Instrument='DEBIT_NOTE' then cn.Value_of_note end) as PO_Debit_Note_amount

sum(case when cn.Reference_doc_type='SALE_ORDER' and


cn.Instrument='CREDIT_NOTE' then cn.Value_of_note end) as SO_Credit_Note_amount

sum(case when cn.Reference_doc_type='SALE_ORDER' and


cn.Instrument='DEBIT_NOTE' then cn.Value_of_note end) as SO_Debit_Note_amount

from
(
select
cdn.DocumentDate as Note_date
cdn.DocumentType as Instrument
cdn.RefType as Reference_doc_type
cdn.RefId as Ref_doc_id
cc.name as Cost_center
po.DeliveryDate as 'POdate/SOdate'
cdn.Id as Note_Id
cdn.TotalNoteAmount as Value_of_note
cdn.Status as Note_Status
ccc.Id as consignmentid
from
CreditDebitNote as cdn
left join PurchaseOrder po on po.id = cdn.RefId and po.Deleted=0
left join PurchaseOrderAdditionInfo poa on poa.PurchaseOrderId = po.id and
poa.Deleted=0
left join CostCenter cc on cc.id=poa.CostCenterId and cc.Deleted=0

left join GenericMapping gm on po.id = gm.ToRefId and gm.ToRefType =


'PURCHASE_ORDER' and po.Deleted = 0 and
ifnull(gm.status 99) not in
('DISCARDED')
left join Consignment ccc on ccc.Id = gm.FromRefId
where cdn.RefType = 'PURCHASE_ORDER'
and po.Type in (1 9)
and ccc.id IN (30193)
and cdn.status in ('PAID')
and cdn.Deleted=0
union
select
cdn.DocumentDate as Note_date
cdn.DocumentType as Instrument
cdn.RefType as Reference_doc_type
cdn.RefId as Ref_doc_id
cc.name as Cost_center
so.DeliveryDate as 'POdate/SOdate'
cdn.Id as Note_Id
cdn.TotalNoteAmount as Value_of_note
cdn.Status as Note_Status
ccc.Id as consignmentid
from
CreditDebitNote as cdn
left join SaleOrder so on so.id = cdn.RefId and so.Deleted=0
left join SaleOrderAdditionalInfo soa on soa.SaleOrderId = so.id and soa.Deleted=0

left join CostCenter cc on cc.id=soa.CostCenterId and cc.Deleted=0

left join GenericMapping gm on so.id = gm.ToRefId and gm.ToRefType =


'EPATTI_SALE_ORDER'
left join Consignment ccc on ccc.Id = gm.FromRefId
where cdn.RefType = 'SALE_ORDER'
and so.Type in (95)
and cdn.status in ('FINANCE_APPROVED')
and ccc.id IN (30193)
and cdn.Deleted=0
)cn
group by cn.consignmentid
)cna on cna.consignmentid=a.ConsignmentId

##TradeplanID
left join
(select
c.id as ConsignmentId
c.TradePlanId as TradePlanId
au1.id as Asm_AsgardUserId
au1.Email as Asm_Email
au1.ContactNumber as Asm_ContactNumber
au2.id as CH_AsgardUserId
au2.Email as CH_Email
au2.ContactNumber as CH_ContactNumber
cid.POShippingTerms as POShippingTerms
cid.POTransitWeightLoss as POTransitWeightLoss
cid.POPaymentTerms as POPaymentTerms
cid.SOShippingTerms as SOShippingTerms
cid.SOTransitWeightLoss as SOTransitWeightLoss

from Consignment c
left join ConsignmentInvoiceDetail cid on cid.ConsignmentId=c.id and cid.deleted=0

left join AsgardUser au1 on au1.id=POHelperUserId and au1.deleted=0

left join AsgardUser au2 on au2.id=SOHelperUserId and au2.deleted=0

where c.id IN (30193)


and c.Type is not null
and c.Deleted = 0
group by c.id
)TR on TR.ConsignmentId=a.ConsignmentId

Left join (
select c.id as conid
group_concat(i.id) as InvoiceIds
sum(ifnull(iao.TotalInterestAmount 0)) as SO_Interest_amount
from
Consignment c
left join GenericMapping gm on gm.FromRefId=c.id and gm.Deleted=0 and gm.ToRefType
in ('SALE_ORDER' 'EPATTI_SALE_ORDER' )
left join SaleOrder so on so.id=gm.ToRefId and so.deleted=0
left join SaleOrderAdditionalInfo soa on soa.SaleOrderId=so.id and soa.Deleted=0

left join Invoice i on i.SaleOrderId = so.id and i.Deleted=0


left join InvoiceAdditionalInfo iao on iao.InvoiceId=i.id and iao.Deleted=0

where c.Deleted=0 and iao.TotalInterestAmount>0


and c.id IN (30193)
group by c.id
)i on i.conid=a.consignmentId

c.id IN (30193)

You might also like