WYB
2021-03-22 91b8cdad021ab052e4991f3d41834a6f0ddc36b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using JiepeiWMS.Common.Enums;
using JiepeiWMS.Common.Helper;
using JiepeiWMS.Extends;
using JiepeiWMS.IServices;
using JiepeiWMS.Model;
using JiepeiWMS.Model.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
 
namespace JiepeiWMS.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Permissions.Name)]
    public class WMStockLogController : ControllerBase
    {
        /// <summary>
        /// 库存流水日志
        /// </summary>
        private readonly IWMStockLogServices _wMStockLogServices;
        private readonly IWMProductListServices _wMProductListServices;
        private readonly IWMProductClassServices _wMProductClassServices;
        private readonly IWMMaterialCostServices _wMMaterialCostServices;
 
        public WMStockLogController(IWMStockLogServices WMStockLogServices,
            IWMProductListServices WMProductListServices,
            IWMProductClassServices WMProductClassServices,
            IWMMaterialCostServices WMMaterialCostServices)
        {
            _wMStockLogServices = WMStockLogServices;
            _wMProductListServices = WMProductListServices;
            _wMProductClassServices = WMProductClassServices;
            _wMMaterialCostServices = WMMaterialCostServices;
        }
 
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="typeoptions">台账类型</param>
        /// <param name="warehouseoptions">所属仓库</param>
        /// <param name="startdate">开始时间</param>
        /// <param name="enddate">结束时间</param>
        /// <param name="key">关键字</param>
        /// <param name="intPageSize">条数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<WMStockLog>>> Index(string key = "", string typeoptions = "", string warehouseoptions = "", DateTime? startdate = null, DateTime? enddate = null, int page = 1, int intPageSize = 20)
        {
            if (string.IsNullOrWhiteSpace(key))
                key = "";
            if (string.IsNullOrWhiteSpace(typeoptions))
                typeoptions = "";
            if (string.IsNullOrWhiteSpace(warehouseoptions))
                warehouseoptions = "";
 
            var PageList = await _wMStockLogServices.GetWMStockLogPage(page, intPageSize, key, typeoptions, warehouseoptions, startdate, enddate);
 
            return new MessageModel<PageModel<WMStockLog>>()
            {
                msg = "获取成功",
                success = PageList.dataCount >= 0,
                response = PageList
            };
        }
 
        /// <summary>
        /// 导出物料初期、初末实况表格
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> ExportStockLog(int type)
        {
            var stockLogList = await _wMStockLogServices.QuerySql("select sl.Id,case when sl.Type=1 then (select PurchaseCode from WMPurchaseInfo where Id=OrderId) when sl.Type=2 then (select SaleCode from WMSaleInfo where Id=OrderId) when sl.Type=4 then (select QuoteCode from WMQuoteInfo where Id=OrderId) end CodeNo,Code,pl.Name ProductName,ps.Name ProductSpec,wh.Name WareHouseName,case when sl.Type=0 then '初始化库存' when sl.Type=1 then '采购入库' when sl.Type=2 then '销售出库' when sl.Type=3 then '库存盘点' when sl.Type=4 then '报价入库' end TypeName,case when sl.Quantity > 0 then case when sl.Operation=1 then '+' else '-' end + CAST(sl.Quantity AS varchar) else '' end QuantitySign,pl.Price,TotalPrice,RemainQuantity,RemainPrice,TotalRemainPrice,sl.CreateTime,ui.uRealName AdminName from WMStockLog sl left join WMProductList pl on sl.ProductListId = pl.id left join WMProductSpec ps on sl.ProductSpecId = ps.id left join WMWareHouse wh on sl.WareHouseId = wh.id left join sysUserInfo ui on sl.AdminId = ui.uID");
 
            if (stockLogList != null && stockLogList.Any())
            {
                var classId = 0;
                var date = DateTime.Now;
                var StartCreateTime = date.ToString("yyyy-MM-01").ObjToDate();//当前月份得第一天
                var EndCreateTime = StartCreateTime.AddMonths(1).AddSeconds(-1);//当月月份得最后一天
 
                var LList = stockLogList.Where(x => x.CreateTime >= StartCreateTime && x.CreateTime < EndCreateTime).Select(c => new { c.Id, c.CodeNo, c.Code, c.ProductName, c.ProductSpec, c.WareHouseName, c.TypeName, c.QuantitySign, c.Price, c.TotalPrice, c.RemainQuantity, c.RemainPrice, c.TotalRemainPrice, c.CreateTime, c.AdminName }).ToList();
 
                var heads = new List<string>() { "编号", "订单编号", "商品编号", "商品名称", "商品规格", "仓库名称", "台账类型", "台账数量", "台账单价", "台账总价", "库存数量", "库存均价", "库存总价", "创建时间", "创建人" };
                var stream = ExcelHelper.CreateExcelStreamFromList(LList, heads);
 
                foreach (var item in await _wMStockLogServices.Query(x => x.CreateTime >= StartCreateTime && x.CreateTime < EndCreateTime))
                {
                    var DepId = item.DepId.Value;
                    var ProductListId = item.ProductListId;
                    var Year = item.CreateTime.Year;
                    var Month = item.CreateTime.Month;
 
                    var ProductListModel = await _wMProductListServices.QueryById(item.ProductListId);
                    if (ProductListModel != null)
                    {
                        var ProductClassModel = await _wMProductClassServices.QueryById(ProductListModel.ClassId);
                        if (ProductClassModel != null)
                            classId = ProductClassModel.Id;
                    }
                    var PeriodEndCost = item.RemainQuantity * item.RemainPrice;//库存数量 * 库存均价
 
                    Expression<Func<WMMaterialCost, bool>> whereExpression = x => x.Id > 0;
                    if (DepId > 0)
                        whereExpression = whereExpression.And(x => x.DepId == DepId);
                    if (classId > 0)
                        whereExpression = whereExpression.And(x => x.ClassId == classId);
                    if (ProductListId > 0)
                        whereExpression = whereExpression.And(x => x.ProductId == ProductListId);
                    if (Year > 0)
                        whereExpression = whereExpression.And(x => x.Years == Year);
                    if (Month > 0)
                        whereExpression = whereExpression.And(x => x.Months == Month);
 
                    var MaterialCostModel = await _wMMaterialCostServices.GetModel(whereExpression);
                    if (MaterialCostModel != null)
                    {
                        MaterialCostModel.UpdateTime = DateTime.Now;
                        MaterialCostModel.PeriodEndCost = PeriodEndCost;
                        var result = await _wMMaterialCostServices.Update(MaterialCostModel);
                    }
                    else
                    {
                        WMMaterialCost model = new WMMaterialCost()
                        {
                            DepId = DepId,
                            ClassId = classId,
                            ProductId = ProductListId,
                            Years = Year,
                            Months = Month,
                            CreateTime = DateTime.Now,
                            PeriodEndCost = PeriodEndCost
                        };
                        var result1 = await _wMMaterialCostServices.Add(model);
                    }
                }
 
                return File(stream, "application/octet-stream", type == 1 ? "物料初期实况" + StartCreateTime.ToString("yyyy-MM-dd") + "~" + EndCreateTime.ToString("yyyy-MM-dd") + ".xlsx" : type == 2 ? "物料初末实况.xlsx" : "");
            }
            return null;
        }
    }
}