Пример #1
0
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task <Result <int> > update(MaterialInfo t)
        {
            Result <int> result = new Result <int>();

            try
            {
                using (LayerDbContext context = new LayerDbContext())
                {
                    MaterialInfo model = await context.materialInfos.FindAsync(t.id);;
                    if (model != null)
                    {
                        model.alias             = t.alias;
                        model.material          = t.material;
                        model.materialName      = t.materialName;
                        model.mat_color         = t.mat_color;
                        model.mat_size          = t.mat_size;
                        model.mat_type          = t.mat_type;
                        model.referencePriceIn  = t.referencePriceIn;
                        model.referencePriceOut = t.referencePriceOut;
                        model.remark            = t.remark;
                        model.weight            = t.weight;
                        model.alarmCount        = t.alarmCount;
                    }
                    await context.SaveChangesAsync();
                }
            }
            catch (DbUpdateConcurrencyException err)
            {
                result.addError(err.Message);
            }
            return(result);
        }
Пример #2
0
        /// <summary>
        /// 根据id删除订单一条详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <Result <int> > deleteOrderDetailById(int id, string userName)
        {
            Result <int> result = new Result <int>();

            try
            {
                using (LayerDbContext context = new LayerDbContext())
                {
                    var model = await Task.Factory.StartNew(() => context.orderDetailInfo.Where(x => x.id == id).Single());

                    var orderNo = model.orderNo;

                    //减少总成本
                    var orderInfoModel = await context.orderInfo.FindAsync(orderNo);

                    orderInfoModel.sumPrice       += model.num * model.materialInfo.referencePriceIn;
                    orderInfoModel.lastUpdatedBy   = userName;
                    orderInfoModel.lastUpdatedDate = DateTime.Now;
                    //删除记录
                    context.orderDetailInfo.Remove(model);
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception err)
            {
                result.addError(err.Message);
            }

            return(result);
        }
Пример #3
0
        /// <summary>
        /// 修改某条订单明细
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task <Result <int> > update(OrderDetailInfo t)
        {
            Result <int> result = new Result <int>();

            try
            {
                using (LayerDbContext context = new LayerDbContext())
                {
                    var model = await Task.Factory.StartNew(() => context.orderDetailInfo.Where(x => x.id == t.id).Single());

                    float supplementCount = t.num - model.num;
                    model.factory         = t.factory;
                    model.lastUpdatedDate = t.lastUpdatedDate;
                    model.lastUpdatedBy   = t.lastUpdatedBy;
                    model.num             = t.num;

                    var orderInfoModel = await context.orderInfo.FindAsync(model.orderNo);

                    orderInfoModel.lastUpdatedBy   = model.lastUpdatedBy;
                    orderInfoModel.lastUpdatedDate = model.lastUpdatedDate;
                    orderInfoModel.sumPrice       += model.materialInfo.referencePriceIn * supplementCount;

                    await context.SaveChangesAsync();
                }
            }
            catch (Exception err)
            {
                result.addError(err.Message);
            }
            return(result);
        }
Пример #4
0
        /// <summary>
        /// 增加一条
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task <Result <int> > add(OrderDetailInfo t)
        {
            Result <int> result = new Result <int>();

            try
            {
                using (LayerDbContext context = new LayerDbContext())
                {
                    var containFlag = context.orderDetailInfo.Where(x => x.orderNo == t.orderNo).Select(x => x.materialId).Contains(t.materialId);
                    if (containFlag)
                    {
                        result.addError("订单中已包含本产品");
                    }
                    else
                    {
                        context.orderDetailInfo.Add(t);
                        var materialModel = await Task.Factory.StartNew(() => context.materialInfos.Where(x => x.id == t.materialId).Single());

                        var orderInfoModel = await context.orderInfo.FindAsync(t.orderNo);

                        orderInfoModel.sumPrice       += t.num * materialModel.referencePriceIn;
                        orderInfoModel.lastUpdatedBy   = t.createdBy;
                        orderInfoModel.lastUpdatedDate = t.createdDate;

                        await context.SaveChangesAsync();
                    }
                }
            }
            catch (Exception err)
            {
                result.addError(err.Message);
            }
            return(result);
        }
Пример #5
0
        /// <summary>
        /// 收货订单
        /// </summary>
        /// <param name="list"></param>
        /// <param name="userName"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        /// <remarks>
        /// 1. 增加库存,2. 增加入库记录 3. 修改订单状态
        /// </remarks>
        public async Task <Result <int> > receiveOrder(List <InStorageRecord> list, string userName, OrderStatusEnum status)
        {
            Result <int> result  = new Result <int>();
            string       orderNo = list.First().orderNo;

            try
            {
                using (LayerDbContext context = new LayerDbContext())
                {
                    var model = context.orderInfo.Where(x => x.orderNo == orderNo).Single();
                    if (model.status == status.ToString())
                    {
                        result.addError("进货单已被" + model.receivedBy + "收货");
                    }
                    else
                    {
                        //订单状态
                        model.status       = status.ToString();
                        model.receivedDate = DateTime.Now;
                        model.receivedBy   = userName;
                        model.sumPriceReal = list.Sum(x => x.countReal * x.priceIn);
                        //入库记录
                        context.inStorageRecord.AddRange(list);
                        //库存
                        List <Inventory> inventoryList = new List <Inventory>();
                        Inventory        model1        = null;
                        foreach (var item in list)
                        {
                            model1 = new Inventory()
                            {
                                count      = item.countReal,
                                materialId = item.materialId,
                                priceIn    = item.priceIn
                            };
                            inventoryList.Add(model1);
                        }
                        context.inventory.AddRange(inventoryList);

                        await context.SaveChangesAsync();
                    }
                }
            }
            catch (Exception err)
            {
                result.addError(err.Message);
            }

            return(result);
        }
Пример #6
0
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task <Result <int> > add(MaterialInfo t)
        {
            Result <int> result = new Result <int>();

            try
            {
                using (LayerDbContext context = new LayerDbContext())
                {
                    var aaa = context.materialInfos.Add(t);
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception err)
            {
                result.addError(err.Message);
            }
            return(result);
        }
Пример #7
0
        /// <summary>
        /// 增加单个物料库存
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task <Result <int> > add(Inventory t)
        {
            Result <int> result = new Result <int>();

            try
            {
                using (LayerDbContext context = new LayerDbContext())
                {
                    context.inventory.Add(t);
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception err)
            {
                result.addError(err.Message);
            }
            return(result);
        }
Пример #8
0
        /// <summary>
        /// 通过id删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <Result <int> > deleteById(int id)
        {
            Result <int> result = new Result <int>();

            try
            {
                using (LayerDbContext context = new LayerDbContext())
                {
                    var model = context.materialInfos.Where(x => x.id == id).Single();
                    context.materialInfos.Remove(model);
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception err)
            {
                result.addError(err.Message);
            }
            return(result);
        }
        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="order"></param>
        /// <param name="orderList"></param>
        /// <returns></returns>
        public async Task <Result <List <OrderDetailInfo> > > addOrder(OrderInfo order, List <OrderDetailInfo> orderList)
        {
            Result <List <OrderDetailInfo> > result = new Result <List <OrderDetailInfo> >();

            try
            {
                using (LayerDbContext context = new LayerDbContext())
                {
                    context.orderInfo.Add(order);
                    context.orderDetailInfo.AddRange(orderList);
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception err)
            {
                result.addError(err.Message);
            }
            result.data = orderList;
            return(result);
        }
        /// <summary>
        /// 根据订单号删除订单
        /// 删除订单主表订单
        /// 删除订单附表订单内容
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public async Task <Result <int> > deleteByOrderNo(string orderNo)
        {
            Result <int> result = new Result <int>();

            try
            {
                using (LayerDbContext context = new LayerDbContext())
                {
                    //根据订单与订单状态查询是否存在订单
                    var modelList = context.orderInfo.Where(x => x.orderNo == orderNo).ToList();
                    if (modelList.Count == 0)
                    {
                        result.addError("订单已被删除");
                    }
                    else
                    {
                        var model = modelList[0];
                        if (model.status != "generated")
                        {
                            result.addError("订单状态已改变,无法删除");
                        }
                        else
                        {
                            // 删除订单主表订单
                            context.orderInfo.Remove(model);
                            // 删除订单附表订单内容
                            var sql = context.orderDetailInfo.RemoveRange(context.orderDetailInfo.Where(x => x.orderNo == orderNo));
                        }
                    }
                    var count = await context.SaveChangesAsync();
                }
            }
            catch (Exception err)
            {
                result.addError(err.Message);
            }
            return(result);
        }
        /// <summary>
        /// 收货
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        /// <remarks>
        /// 1. 判断库存是否有货:不需要判断,在第二步会顺带判断
        /// 2. 按照入库时间正序排列,减库存
        /// 3. 增加交易记录与收益记录
        /// </remarks>
        public async Task <Result <string> > receive(List <SaleModel> list)
        {
            Result <string> result = new Result <string>();

            try
            {
                using (LayerDbContext context = new LayerDbContext())
                {
                    //2. 按照入库时间正序排列,减库存
                    List <Inventory> inventoryList = null;
                    foreach (var item in list)
                    {
                        inventoryList = context.inventory.Where(x => x.materialId == item.materialId).OrderBy(x => x.createdDate).ToList();
                        //卖出数量
                        float saleCount = item.count;
                        //成本价综合
                        float sumPriceIn = 0;
                        for (int i = 0; i < inventoryList.Count; i++)
                        {
                            if (inventoryList[i].count > saleCount)
                            {
                                inventoryList[i].count -= saleCount;
                                sumPriceIn             += inventoryList[i].priceIn * saleCount;
                                saleCount = 0;
                                break;
                            }
                            else
                            {
                                saleCount  -= inventoryList[i].count;
                                sumPriceIn += inventoryList[i].priceIn * inventoryList[i].count;
                                context.inventory.Remove(inventoryList[i]);
                            }
                        }
                        //saleCount !=0 说明 库存货不够了,需要报异常,让客户重新选择下单
                        if (saleCount != 0)
                        {
                            throw new Exception(item.materialName + " 库存不足,仅剩余" + (item.count - saleCount).ToString());
                        }
                        //3.1增加交易记录
                        TradingRecord trade = new TradingRecord
                        {
                            cashOrder   = item.cashOrder,
                            count       = item.count,
                            createdBy   = item.createdBy,
                            createdDate = DateTime.Now,
                            materialId  = item.materialId
                        };
                        context.tradingRecord.Add(trade);
                        //3.2 增加收益记录
                        ProfitRecord profit = new ProfitRecord
                        {
                            createdDate = trade.createdDate,
                            priceIn     = sumPriceIn,
                            priceOut    = item.priceOut,
                            profit      = item.priceOut * item.count - sumPriceIn,
                            cashOrder   = item.cashOrder,
                            materialId  = item.materialId
                        };
                        context.profitRecord.Add(profit);
                    }
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception err)
            {
                result.addError(err.Message);
            }
            return(result);
        }
Пример #12
0
        /// <summary>
        /// 处理异常,补充收货
        /// </summary>
        /// <param name="supplementList">补充产品list</param>
        /// <param name="userName">操作人</param>
        /// <returns></returns>
        public async Task <Result <int> > supplementReceiveOrder(List <InStorageRecord> supplementList, string userName)
        {
            Result <int> result  = new Result <int>();
            string       orderNo = supplementList.First().orderNo;

            try
            {
                using (LayerDbContext context = new LayerDbContext())
                {
                    //1.插入入库记录  2.查询本订单下 根据物料group by  应收与实收是否相等,修改订单状态与订单实际总成本   3. 增加库存
                    var orderModel = context.orderInfo.Where(x => x.orderNo == orderNo).Single();
                    context.inStorageRecord.AddRange(supplementList);

                    //入库记录中 本订单 各物料的应收实收数量
                    //DONE: 验证此时的入库记录是否包含补录的记录  不包含
                    //TODO: left join orderdetailinfo
                    var inStorageRecodes         = context.inStorageRecord.Where(x => x.orderNo == orderNo).GroupBy(x => x.materialId).Select(x => new { materialId = x.Key, countReal = x.Sum(item => item.countReal), priceIn = x.Max(item => item.priceIn) });
                    var inStorageRecodesEntities = (from x in inStorageRecodes
                                                    join y in context.orderDetailInfo.Where(x => x.orderNo == orderNo) on x.materialId equals y.materialId
                                                    select new
                    {
                        materialId = x.materialId,
                        priceIn = x.priceIn,
                        countReal = x.countReal,
                        countReference = y.num,
                    }).ToList();
                    List <InStorageRecord> inStorageRecodeCopy = new List <InStorageRecord>();
                    InStorageRecord        model = null;
                    foreach (var item in inStorageRecodesEntities)
                    {
                        model                = new InStorageRecord();
                        model.materialId     = item.materialId;
                        model.priceIn        = item.priceIn;
                        model.countReal      = item.countReal;
                        model.countReference = item.countReference;
                        inStorageRecodeCopy.Add(model);
                    }
                    //订单状态
                    OrderStatusEnum status = OrderStatusEnum.completed;
                    //实际进价总和
                    float sumPriceReal = 0;
                    foreach (var item in supplementList)
                    {
                        inStorageRecodeCopy.Where(x => x.materialId == item.materialId).Single().countReal += item.countReal;
                    }
                    if (inStorageRecodeCopy.Where(x => x.countReal != x.countReference).Count() > 0)
                    {
                        status = OrderStatusEnum.excepted;
                    }
                    sumPriceReal               = inStorageRecodeCopy.Sum(x => x.priceIn * x.countReal);
                    orderModel.status          = status.ToString();
                    orderModel.lastUpdatedBy   = userName;
                    orderModel.lastUpdatedDate = DateTime.Now;
                    orderModel.sumPriceReal    = sumPriceReal;

                    //库存
                    List <Inventory> inventoryList = new List <Inventory>();
                    Inventory        model1        = null;
                    foreach (var item in supplementList)
                    {
                        model1 = new Inventory()
                        {
                            count      = item.countReal,
                            materialId = item.materialId,
                            priceIn    = item.priceIn
                        };
                        inventoryList.Add(model1);
                    }
                    context.inventory.AddRange(inventoryList);

                    await context.SaveChangesAsync();
                }
            }
            catch (Exception err)
            {
                result.addError(err.Message);
            }
            return(result);
        }