Commit 03adc541 by 泽锋 李

历史调拨运费改成算在途+在库

parent 2c6b90b3
...@@ -115,67 +115,7 @@ namespace AutoTurnOver.DB ...@@ -115,67 +115,7 @@ namespace AutoTurnOver.DB
/// <param name="log_data"></param> /// <param name="log_data"></param>
public static void Calculation(order_transfer_status_log log_data) public static void Calculation(order_transfer_status_log log_data)
{ {
dc_base_transfer_freight_log data_log = new dc_base_transfer_freight_log CalculationSingle(log_data.bailun_sku, log_data.warehouse_code);
{
action_date = log_data.gmt_create_date,
action_log_id = log_data.data_id.ToString(),
bailun_sku = log_data.bailun_sku,
order_no = log_data.system_order_no,
update_time = DateTime.Now,
warehouse_code = log_data.warehouse_code
};
try
{
// 查询该流水是否已经计算过
var oldId = _connection.QuerySingleOrDefault<int>(" select id from dc_base_transfer_freight_log where action_log_id=@action_log_id limit 1 ", new { action_log_id = data_log.action_log_id });
if (oldId > 0)
{
return;
}
var history_avg_list = _connection.Query<transfer_price_dto>(@"
select t2.sku ,t1.targetwareno,t2.deliverycount,t1.estimatedcost,t3.deliverycount as 'order_deliverycount' from dc_base_transfer_info as t1
left join dc_base_transfer_info_skus as t2 on t1.dataid = t2.dataid
left join ( select dataid,sum(deliverycount) as 'deliverycount' from dc_base_transfer_info_skus GROUP BY dataid ) as t3 on t1.dataid = t3.dataid
where t2.sku=@sku and t1.targetwareno=@targetwareno and t1.estimatedcost>0 and t1.creationtime<=@date and t1.stateid=5 and t1.isdeleted=0 order by t1.creationtime DESC
", new
{
sku = data_log.bailun_sku,
targetwareno = data_log.warehouse_code,
date = data_log.action_date
}).AsList();
data_log.freight_unit_price = history_avg_list.Average(s => s.estimatedcost / s.order_deliverycount);
// 记录到最新运费表
dc_base_transfer_freight new_data = new dc_base_transfer_freight()
{
bailun_sku = data_log.bailun_sku,
freight_unit_price = data_log.freight_unit_price,
update_time = DateTime.Now,
warehouse_code = data_log.warehouse_code,
id = (_connection.QueryFirstOrDefault<int?>(" select id from dc_base_transfer_freight where bailun_sku=@bailun_sku and warehouse_code=@warehouse_code ", new
{
bailun_sku = data_log.bailun_sku,
warehouse_code = data_log.warehouse_code
}) ?? 0)
};
if (new_data.id > 0)
{
_connection.Update(new_data);
}
else
{
_connection.Insert(new_data);
}
}
catch (Exception ex)
{
data_log.remarks = ex.Message + "---" + ex.StackTrace;
}
_connection.Insert(data_log);
} }
...@@ -190,44 +130,118 @@ where t2.hq_type!='国内仓' "; ...@@ -190,44 +130,118 @@ where t2.hq_type!='国内仓' ";
var datas = _connection.Query<stock_dto>(sql).ToList(); var datas = _connection.Query<stock_dto>(sql).ToList();
foreach (var log_data in datas) foreach (var log_data in datas)
{ {
CalculationSingle(log_data.bailun_sku, log_data.warehouse_code);
}
}
// 计算单个
public static void CalculationSingle(string bailun_sku,string warehouse_code)
{
try try
{ {
dc_base_transfer_freight_log data_log = new dc_base_transfer_freight_log dc_base_transfer_freight_log data_log = new dc_base_transfer_freight_log
{ {
action_date = new DateTime(2021, 02, 22), action_date = new DateTime(2021, 02, 22),
action_log_id = "", action_log_id = "",
bailun_sku = log_data.bailun_sku, bailun_sku = bailun_sku,
order_no = "init", order_no = "init",
update_time = DateTime.Now, update_time = DateTime.Now,
warehouse_code = log_data.warehouse_code warehouse_code = warehouse_code
}; };
var history_avg_list = _connection.Query<transfer_price_dto>(@" var history_avg_list = _connection.Query<transfer_price_dto>(@"
select t2.sku ,t1.targetwareno,t2.deliverycount,t1.estimatedcost,t3.deliverycount as 'order_deliverycount' from dc_base_transfer_info as t1 select t2.sku ,t1.targetwareno,t2.deliverycount,t2.storagecount,t1.estimatedcost,t3.deliverycount as 'order_deliverycount' from dc_base_transfer_info as t1
left join dc_base_transfer_info_skus as t2 on t1.dataid = t2.dataid left join dc_base_transfer_info_skus as t2 on t1.dataid = t2.dataid
left join ( select dataid,sum(deliverycount) as 'deliverycount' from dc_base_transfer_info_skus GROUP BY dataid ) as t3 on t1.dataid = t3.dataid left join ( select dataid,sum(deliverycount) as 'deliverycount' from dc_base_transfer_info_skus GROUP BY dataid ) as t3 on t1.dataid = t3.dataid
where t2.sku=@sku and t1.targetwareno=@targetwareno and t1.estimatedcost>0 and t1.stateid=5 and t1.isdeleted=0 order by t1.creationtime DESC where t1.isdeleted=0 and t1.stateid not in (5,11,12)
and t2.deliverycount > t2.storagecount
and t1.isexception = 0
and t1.expectarrivaltime >='1991-01-01' and t2.sku=@sku and t1.targetwareno=@targetwareno and t1.estimatedcost>0 order by t1.creationtime DESC
", new ", new
{ {
sku = data_log.bailun_sku, sku = data_log.bailun_sku,
targetwareno = data_log.warehouse_code targetwareno = data_log.warehouse_code
}).AsList(); }).AsList();
data_log.freight_unit_price = history_avg_list.Average(s => s.estimatedcost / s.order_deliverycount); // 在途部分的总价
data_log.id = _connection.QuerySingleOrDefault<int?>(" select * from dc_base_transfer_freight_log where bailun_sku=@bailun_sku and warehouse_code=@warehouse_code ", new { data_log.freight = history_avg_list.Sum(s => s.estimatedcost / s.order_deliverycount * (s.deliverycount - s.storagecount));
bailun_sku = data_log.bailun_sku, var sum_quantity = history_avg_list.Sum(s => s.deliverycount - s.storagecount);
warehouse_code = data_log.warehouse_code,
}) ??0;
if (data_log.id <= 0) DynamicParameters parameters = new DynamicParameters();
parameters.Add("bailun_sku", bailun_sku);
parameters.Add("warehouse_code", warehouse_code);
// 在库库存分析
var stock = _connection.QueryFirstOrDefault<int?>(" select usable_stock from dc_base_stock as t1 where t1.bailun_sku=@bailun_sku and t1.warehouse_code=@warehouse_code ", parameters) ?? 0;
if (stock > 0)
{
var logs = _connection.Query<order_transfer_status_log>(" select * from order_transfer_status_log as t1 where t1.bailun_sku=@bailun_sku and t1.warehouse_code=@warehouse_code and is_delete=0 ", parameters).ToList();
if (logs != null && logs.Count >= 1)
{ {
// 按实际排降序取够库存
var stock_orders = new List<transfer_price_dto>();
foreach (var item in logs.OrderByDescending(s => s.date))
{
if (stock > 0)
{
var use_stock = stock > item.count ? item.count : stock;
if (use_stock > 0)
{
stock -= use_stock;
// 查询相关调拨单的运费单价
var temp_transfer_price_data = _connection.QueryFirstOrDefault<transfer_price_dto>(@"
select t1.estimatedcost,t3.deliverycount as 'order_deliverycount' from dc_base_transfer_info as t1
left join dc_base_transfer_info_skus as t2 on t1.dataid = t2.dataid
left join ( select dataid,sum(deliverycount) as 'deliverycount' from dc_base_transfer_info_skus GROUP BY dataid ) as t3 on t1.dataid = t3.dataid
where t1.`code`=@code limit 1
", new { code = item.system_order_no });
stock_orders.Add(new transfer_price_dto { estimatedcost = temp_transfer_price_data.estimatedcost,order_deliverycount = temp_transfer_price_data.order_deliverycount,deliverycount = use_stock });
}
}
}
var stock_freight = stock_orders.Sum(s => s.estimatedcost / s.order_deliverycount * s.deliverycount);
var stock_sum_quantity = stock_orders.Sum(s => s.deliverycount);
data_log.formula = $" ( 在途总运费 {Math.Round(data_log.freight)} + 在库总运费 {Math.Round(stock_freight)} ) / ( 在途总数量 {sum_quantity} + 在库总数量 {stock_sum_quantity } )";
data_log.freight += stock_freight;
sum_quantity += stock_sum_quantity;
data_log.freight_unit_price = Math.Round(data_log.freight / sum_quantity, 2);
}
}
_connection.Insert(data_log); _connection.Insert(data_log);
_connection.Insert(new dc_base_transfer_freight var mainData = new dc_base_transfer_freight
{ {
bailun_sku = data_log.bailun_sku, bailun_sku = data_log.bailun_sku,
warehouse_code = data_log.warehouse_code, warehouse_code = data_log.warehouse_code,
freight_unit_price = data_log.freight_unit_price, freight_unit_price = data_log.freight_unit_price,
update_time = DateTime.Now freight = data_log.freight,
}); update_time = DateTime.Now,
formula = data_log.formula
};
mainData.id = _connection.QuerySingleOrDefault<int?>(" select id from dc_base_transfer_freight where bailun_sku=@bailun_sku and warehouse_code=@warehouse_code ", new
{
bailun_sku = mainData.bailun_sku,
warehouse_code = mainData.warehouse_code,
}) ?? 0;
if (mainData.id <= 0)
{
_connection.Insert(mainData);
}
else
{
_connection.Update(mainData);
} }
} }
...@@ -235,8 +249,6 @@ where t2.sku=@sku and t1.targetwareno=@targetwareno and t1.estimatedcost>0 and t ...@@ -235,8 +249,6 @@ where t2.sku=@sku and t1.targetwareno=@targetwareno and t1.estimatedcost>0 and t
{ {
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
} }
}
} }
public class stock_dto public class stock_dto
...@@ -251,6 +263,7 @@ where t2.sku=@sku and t1.targetwareno=@targetwareno and t1.estimatedcost>0 and t ...@@ -251,6 +263,7 @@ where t2.sku=@sku and t1.targetwareno=@targetwareno and t1.estimatedcost>0 and t
public string sku { get; set; } public string sku { get; set; }
public string targetwareno { get; set; } public string targetwareno { get; set; }
public int deliverycount { get; set; } public int deliverycount { get; set; }
public int storagecount { get; set; }
/// <summary> /// <summary>
/// 订单总运费 /// 订单总运费
......
...@@ -73,6 +73,10 @@ namespace AutoTurnOver.Models.Report ...@@ -73,6 +73,10 @@ namespace AutoTurnOver.Models.Report
/// </summary> /// </summary>
public int quantity_final_advise_ocean; public int quantity_final_advise_ocean;
/// <summary> /// <summary>
/// 调拨周期
/// </summary>
public int quantity_final_advise_ocean_transfer_days;
/// <summary>
/// 海运供应链长度销量 /// 海运供应链长度销量
/// </summary> /// </summary>
public dynamic turnover_sales_ocean; public dynamic turnover_sales_ocean;
......
...@@ -16,9 +16,15 @@ namespace AutoTurnOver.Models ...@@ -16,9 +16,15 @@ namespace AutoTurnOver.Models
public decimal freight_unit_price { get; set; } public decimal freight_unit_price { get; set; }
/// <summary> /// <summary>
/// 当前总运费
/// </summary>
public decimal freight { get; set; }
/// <summary>
/// 更新时间 /// 更新时间
/// </summary> /// </summary>
public DateTime update_time { get; set; } public DateTime update_time { get; set; }
public string formula { get; set; }
} }
...@@ -35,6 +41,10 @@ namespace AutoTurnOver.Models ...@@ -35,6 +41,10 @@ namespace AutoTurnOver.Models
/// 运费单价 /// 运费单价
/// </summary> /// </summary>
public decimal freight_unit_price { get; set; } public decimal freight_unit_price { get; set; }
/// <summary>
/// 总价
/// </summary>
public decimal freight { get; set; }
/// <summary> /// <summary>
...@@ -59,5 +69,6 @@ namespace AutoTurnOver.Models ...@@ -59,5 +69,6 @@ namespace AutoTurnOver.Models
/// </summary> /// </summary>
public string action_log_id { get; set; } public string action_log_id { get; set; }
public string remarks { get; set; } public string remarks { get; set; }
public string formula { get; set; }
} }
} }
...@@ -24,7 +24,7 @@ namespace ResetOutofstock ...@@ -24,7 +24,7 @@ namespace ResetOutofstock
//dc_base_trans_temp_schedule_dao.SynchroTransTempScheduleData(true); //dc_base_trans_temp_schedule_dao.SynchroTransTempScheduleData(true);
//dc_auto_turnover.CalculationProfitAnalysis("LM-EO-007", "MMDUSFBA"); //dc_auto_turnover.CalculationProfitAnalysis("LM-EO-007", "MMDUSFBA");
//dc_auto_turnover.SynchroCalculationProfitAnalysis(); //dc_auto_turnover.SynchroCalculationProfitAnalysis();
//dc_base_transfer_freight_dao.CalculationSingle("LM-EO-007", "MMDUSFBA");
} }
catch (Exception ex) catch (Exception ex)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment