ShipmentHeaderApp.cs 25.6 KB
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// 出库单主表
    /// </summary>

    public partial class ShipmentHeaderApp
    {
        private IUnitWork _unitWork;
        public IRepository<ShipmentHeader> _app;
        public IRepository<ShipmentDetail> _appsd;
        public IRepository<Task> _appt;
        public IRepository<TaskDetail> _apptd;
        private BaseDBContext _context;
        private static IHostingEnvironment _hostingEnvironment;
        ExcelHelper imp = new ExcelHelper(_hostingEnvironment);

        public ShipmentHeaderApp(IUnitWork unitWork, IRepository<ShipmentHeader> repository, IRepository<Task> repositoryt, IRepository<TaskDetail> repositorytd, IRepository<ShipmentDetail> repositorysd, IHostingEnvironment hostingEnvironment, BaseDBContext context)
        {
            _unitWork = unitWork;
            _app = repository;
            _appsd = repositorysd;
            _appt = repositoryt;
            _apptd = repositorytd;
            _context = context;
            _hostingEnvironment = hostingEnvironment;
        }

        public ShipmentHeaderApp SetLoginInfo(LoginInfo loginInfo)
        {
            _app._loginInfo = loginInfo;
            _appsd._loginInfo = loginInfo;
            _appt._loginInfo = loginInfo;
            _apptd._loginInfo = loginInfo;
            return this;
        }

        public TableData Load(PageReq pageRequest, ShipmentHeader entity)
        {
            return _app.Load(pageRequest, entity);
        }

        public void Ins(ShipmentHeader entity)
        {
            //  var rw = _unitWork.FindSingle<Location>(u => u.Type.Equals(entity.WarehouseType)).Roadway;
            entity.TotalQty = 0;
            entity.TotalLines = 0;
            entity.UploadStatus = 0;
            entity.FirstStatus = ShipmentHeaderStatus.新建;
            entity.LastStatus = ShipmentHeaderStatus.新建;
            // entity.Station = _unitWork.FindSingle<StationRoadway>(u => u.RoadWay.Equals(rw)).StationCode;
            if (entity.Priority == null)
            {
                entity.Priority = 0;
            }
            entity.Code = _app.GetTaskNo(entity.Type);
            entity.ShipTo = _unitWork.FindSingle<Customer>(u => u.Code.Equals(entity.ShipTo)).Code;
            _app.Add(entity);
        }

        public void Upd(ShipmentHeader entity)
        {
            if (entity.FirstStatus != ShipmentHeaderStatus.新建 || entity.LastStatus != ShipmentHeaderStatus.新建)
            {
                throw new Exception("当前状态不可编辑!Id: " + entity.Id);
            }
            _app.Update(entity);
        }

        public void DelByIds(int[] ids)
        {
            foreach (var item in ids)
            {
                ShipmentHeader entity = _app.FindSingle(u => u.Id == item);
                if (entity.FirstStatus != ShipmentHeaderStatus.新建 || entity.LastStatus != ShipmentHeaderStatus.新建)
                {
                    throw new Exception("订单号:" + entity.Code + "进入订单池后,不允许删除");
                }
                else
                {
                    _app.Delete(entity);
                    _unitWork.Delete<ShipmentDetail>(u => u.ShipmentId.Equals(entity.Id));
                }
            }
        }

        public ShipmentHeader FindSingle(Expression<Func<ShipmentHeader, bool>> exp)
        {
            return _app.FindSingle(exp);
        }

        public IQueryable<ShipmentHeader> Find(Expression<Func<ShipmentHeader, bool>> exp)
        {
            return _app.Find(exp);
        }

        public Response ImportIn(IFormFile excelfile)
        {
            Response result = new Infrastructure.Response();
            List<ShipmentHeader> exp = imp.ConvertToModel<ShipmentHeader>(excelfile);
            string sErrorMsg = "";

            for (int i = 0; i < exp.Count; i++)
            {
                try
                {
                    ShipmentHeader e = exp[i];
                    e.Id = null;
                    _app.Add(e);
                }
                catch (Exception ex)
                {
                    sErrorMsg += "第" + (i + 2) + "行:" + ex.Message + "<br>";
                    result.Message = sErrorMsg;
                    break;
                }
            }
            if (sErrorMsg.Equals(string.Empty))
            {
                if (exp.Count == 0)
                {
                    sErrorMsg += "没有发现有效数据, 请确定模板是否正确, 或是否有填充数据!";
                    result.Message = sErrorMsg;
                }
                else
                {
                    result.Message = "导入完成";
                }
            }
            else
            {
                result.Status = false;
                result.Message = result.Message;
            }
            return result;
        }

        public TableData ExportData(ShipmentHeader entity)
        {
            return _app.ExportData(entity);
        }

        public TableData Query(ShipmentHeader entity)
        {
            var result = new TableData();
            var data = _app.Find(EntityToExpression<ShipmentHeader>.GetExpressions(entity));

            GetData(data, result);
            result.count = data.Count();

            return result;
        }

        public void GetData(IQueryable<ShipmentHeader> data, TableData result, PageReq pageRequest = null)
        {
            _app.GetData(data, result, pageRequest);
        }
        public TableData GetPrintDetailData(string spno)
        {
            var result = new TableData();
            var data = from rd in _unitWork.Find<ShipmentDetail>(u => u.ShipmentCode == spno)
                       join mt in _unitWork.Find<Material>(null)
                       on rd.MaterialCode equals mt.Code
                       select new
                       {
                           rd.MaterialCode,
                           mt.Name,
                           rd.Batch,
                           rd.Lot,
                           rd.Qty,
                           PBarCode = rd.MaterialCode,
                       };
            result.data = data.ToList();
            result.count = data.Count();

            if (result.count == 0)
            {
                result.msg = "还未建立明细数据!";
            }
            return result;
        }

        public string AddTaskInfo(ShipmentDetail shipmentDetail)
        {
            string strError = "";
            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
                    ShipmentHeader shipmentHeader = _unitWork.Find<ShipmentHeader>(n => n.Code == shipmentDetail.ShipmentCode).FirstOrDefault();

                    //建立单据主任务和明细任务
                    Task task = new Task();
                    var tno = "";
                    int tcount = _unitWork.Find<Task>(n => n.OrderCode == shipmentDetail.ShipmentCode).Count();
                    if (tcount > 0)
                    {
                        var tasknum = _unitWork.Find<Task>(n => n.OrderCode == shipmentDetail.ShipmentCode).Select(a => a.TaskNo).First();
                        tno = tasknum;
                    }
                    else
                    {
                        tno = _app.GetTaskNo(TaskNo.出库自动分配);
                        task.TaskNo = tno;
                        task.SourceCode = shipmentDetail.SourceCode;
                        task.OrderCode = shipmentDetail.ShipmentCode;
                        task.BusinessType = shipmentHeader.Type;
                        task.TotalQty = shipmentHeader.TotalQty;
                        task.TotalLines = shipmentHeader.TotalLines;
                        task.FirstStatus = TaskStatus.待下发任务;
                        task.LastStatus = TaskStatus.待下发任务;
                        task.Project = shipmentDetail.Project;
                        task.CreateTime = DateTime.Now;
                        _appt.Add(task);
                    }

                    List<string> container = _unitWork.Find<Container>(n => n.IsLock != ContainerLock.盘点锁).Select(a => a.Code).ToList();
                    List<Inventory> inventory = _unitWork.Find<Inventory>(n => n.MaterialCode == shipmentDetail.MaterialCode && container.Contains(n.ContainerCode) && n.TaskStatus == "idle").OrderBy(a => a.CreateTime).ToList();
                    var sqty = shipmentDetail.Qty;
                    foreach (Inventory inv in inventory)
                    {
                        TaskDetail taskDetail = new TaskDetail();
                        //var outtype = _unitWork.Find<SysDictData>(u => u.DictType == "shipmentType").Select(a => a.DictValue).ToList();
                        if (_unitWork.IsExist<TaskDetail>(n => n.MaterialCode == shipmentDetail.MaterialCode && n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成 && n.ContainerCode == inv.ContainerCode && !n.TaskNo.StartsWith("I")))
                        {
                            decimal? taskhadqty = _unitWork.Find<TaskDetail>(n => n.MaterialCode == shipmentDetail.MaterialCode && n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成 && n.ContainerCode == inv.ContainerCode && !n.TaskNo.StartsWith("I")).Select(a => a.ContainerQty).Sum();
                            inv.Qty = inv.Qty - taskhadqty;
                        }
                        else
                        {
                            inv.Qty = inv.Qty - 0;
                        }

                        if (inv.Qty == 0)
                        {
                            continue;
                        }

                        if (inv.Qty >= sqty)
                        {
                            taskDetail.TaskType = TaskType.分拣出库;
                            taskDetail.ContainerQty = sqty;
                            taskDetail.TaskNo = tno;
                            taskDetail.SourceCode = shipmentDetail.SourceCode;
                            taskDetail.OrderCode = shipmentDetail.ShipmentCode;
                            taskDetail.ContainerCode = inv.ContainerCode;

                            taskDetail.MaterialCode = shipmentDetail.MaterialCode;
                            taskDetail.MaterialName = _unitWork.Find<Material>(n => n.Code == shipmentDetail.MaterialCode).Select(a => a.Name).First();
                            taskDetail.Batch = shipmentDetail.Batch;
                            taskDetail.Lot = shipmentDetail.Lot;
                            taskDetail.OderQty = shipmentDetail.Qty;

                            taskDetail.HadQty = 0;
                            taskDetail.SourceLocation = inv.LocationCode;
                            taskDetail.DestinationLocation = shipmentHeader.Station;
                            Location lcount = _unitWork.Find<Location>(n => n.Code == inv.LocationCode).FirstOrDefault();
                            if (lcount != null)
                            {
                                taskDetail.Roadway = lcount.Roadway;
                            }
                            else
                            {
                                taskDetail.Roadway = 0;
                            }
                            taskDetail.Station = shipmentHeader.Station;
                            taskDetail.Priority = shipmentHeader.Priority;
                            taskDetail.Status = TaskStatus.待下发任务;
                            taskDetail.CreateTime = DateTime.Now;
                            _apptd.Add(taskDetail);
                            //建立容器出库任务
                            if (_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.MaterialCode == shipmentDetail.MaterialCode && n.OrderCode == shipmentDetail.ShipmentCode))
                            {
                                int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == taskDetail.ContainerCode && n.TaskType == TaskType.容器出库).Count();
                                if (ptdcount < 1)
                                {
                                    Task ptask = new Task();
                                    TaskDetail ptaskDetail = new TaskDetail();
                                    var taskNo = _app.GetTaskNo(TaskNo.容器出库);
                                    ptask.TaskNo = taskNo;
                                    ptask.OrderCode = taskNo;
                                    ptask.BusinessType = BusinessType.出库_其他出库单;
                                    ptask.FirstStatus = TaskStatus.待下发任务;
                                    ptask.LastStatus = TaskStatus.待下发任务;
                                    ptask.CreateTime = DateTime.Now;
                                    _appt.Add(ptask);

                                    ptaskDetail.TaskNo = taskNo;
                                    ptaskDetail.OrderCode = taskNo;
                                    ptaskDetail.TaskType = TaskType.容器出库;
                                    ptaskDetail.ContainerCode = taskDetail.ContainerCode;
                                    ptaskDetail.SourceLocation = taskDetail.SourceLocation;
                                    ptaskDetail.DestinationLocation = taskDetail.DestinationLocation;
                                    ptaskDetail.OderQty = 0;
                                    ptaskDetail.ContainerQty = 0;
                                    ptaskDetail.HadQty = 0;
                                    ptaskDetail.Roadway = taskDetail.Roadway;
                                    ptaskDetail.Station = taskDetail.Station;
                                    ptaskDetail.Status = TaskStatus.待下发任务;
                                    if (taskDetail.Priority != null)
                                    {
                                        ptaskDetail.Priority = taskDetail.Priority;
                                    }
                                    else
                                    {
                                        ptaskDetail.Priority = 0;
                                    }
                                    ptaskDetail.CreateTime = DateTime.Now;
                                    _apptd.Add(ptaskDetail);
                                }
                            }
                            break;
                        }
                        else
                        {
                            taskDetail.TaskType = TaskType.整盘出库;
                            taskDetail.ContainerQty = inv.Qty;
                            taskDetail.TaskNo = tno;
                            taskDetail.SourceCode = shipmentDetail.SourceCode;
                            taskDetail.OrderCode = shipmentDetail.ShipmentCode;
                            taskDetail.ContainerCode = inv.ContainerCode;

                            taskDetail.MaterialCode = shipmentDetail.MaterialCode;
                            taskDetail.MaterialName = _unitWork.Find<Material>(n => n.Code == shipmentDetail.MaterialCode).Select(a => a.Name).First();
                            taskDetail.Batch = shipmentDetail.Batch;
                            taskDetail.Lot = shipmentDetail.Lot;
                            taskDetail.OderQty = shipmentDetail.Qty;
                            taskDetail.HadQty = 0;
                            taskDetail.SourceLocation = inv.LocationCode;
                            taskDetail.DestinationLocation = shipmentHeader.Station;
                            Location lcount = _unitWork.Find<Location>(n => n.Code == inv.LocationCode).FirstOrDefault();
                            if (lcount != null)
                            {
                                taskDetail.Roadway = lcount.Roadway;
                            }
                            else
                            {
                                taskDetail.Roadway = 0;
                            }
                            taskDetail.Station = shipmentHeader.Station;
                            taskDetail.Priority = shipmentHeader.Priority;
                            taskDetail.Status = TaskStatus.待下发任务;
                            taskDetail.CreateTime = DateTime.Now;
                            _apptd.Add(taskDetail);
                            sqty = sqty - inv.Qty;
                            //建立容器出库任务
                            if (_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.MaterialCode == shipmentDetail.MaterialCode && n.OrderCode == shipmentDetail.ShipmentCode))
                            {
                                int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == taskDetail.ContainerCode && n.TaskType == TaskType.容器出库).Count();
                                if (ptdcount < 1)
                                {
                                    Task ptask = new Task();
                                    TaskDetail ptaskDetail = new TaskDetail();
                                    var taskNo = _app.GetTaskNo(TaskNo.容器出库);
                                    ptask.TaskNo = taskNo;
                                    ptask.OrderCode = taskNo;
                                    ptask.BusinessType = BusinessType.出库_其他出库单;
                                    ptask.FirstStatus = TaskStatus.待下发任务;
                                    ptask.LastStatus = TaskStatus.待下发任务;
                                    ptask.CreateTime = DateTime.Now;
                                    _appt.Add(ptask);

                                    ptaskDetail.TaskNo = taskNo;
                                    ptaskDetail.OrderCode = taskNo;
                                    ptaskDetail.TaskType = TaskType.容器出库;
                                    ptaskDetail.ContainerCode = taskDetail.ContainerCode;
                                    ptaskDetail.SourceLocation = taskDetail.SourceLocation;
                                    ptaskDetail.DestinationLocation = taskDetail.DestinationLocation;
                                    ptaskDetail.OderQty = 0;
                                    ptaskDetail.ContainerQty = 0;
                                    ptaskDetail.HadQty = 0;
                                    ptaskDetail.Roadway = taskDetail.Roadway;
                                    ptaskDetail.Station = taskDetail.Station;
                                    ptaskDetail.Status = TaskStatus.待下发任务;
                                    if (taskDetail.Priority != null)
                                    {
                                        ptaskDetail.Priority = taskDetail.Priority;
                                    }
                                    else
                                    {
                                        ptaskDetail.Priority = 0;
                                    }
                                    ptaskDetail.CreateTime = DateTime.Now;
                                    _apptd.Add(ptaskDetail);
                                }
                            }
                        }
                    }



                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    strError = ex.Message;
                    return strError;
                    //  throw new Exception(ex.Message);
                }
                return strError;
            }
        }

        public TableData AutoDivideToTask(List<ShipmentHeader> shipmentheaderlist)
        {
            TableData tab = new TableData
            {
                code = 200
            };
            int dcount = 0;
            int totalLinescount = 0;
            string sErrorMsg = "";
            try
            {
                foreach (ShipmentHeader shipmentheader in shipmentheaderlist)
                {
                    List<ShipmentDetail> shipDels = _unitWork.Find<ShipmentDetail>(n => n.ShipmentCode == shipmentheader.Code && n.Qty != n.QtyDivided).ToList();

                    string str = "";
                    foreach (ShipmentDetail t in shipDels)
                    {
                        decimal? qtysum = _unitWork.Find<Inventory>(n => n.MaterialCode == t.MaterialCode && n.TaskStatus == "idle").Sum(a => a.Qty);
                        if (qtysum > 0)
                        {
                            str = AddTaskInfo(t);
                            if (str == "")
                            {
                                var dividedsum = _unitWork.Find<TaskDetail>(n => n.OrderCode == t.ShipmentCode && n.MaterialCode == t.MaterialCode).Sum(a => a.ContainerQty);
                                if (t.Qty == dividedsum)
                                {
                                    t.Status = ShipmentHeaderStatus.分配完成;
                                    if (_unitWork.IsExist<OrderAlert>(n => n.OrderNo == t.ShipmentCode && n.MaterialNo == t.MaterialCode))
                                    {
                                        _unitWork.Delete<OrderAlert>(n => n.OrderNo == t.ShipmentCode && n.MaterialNo == t.MaterialCode);
                                    }
                                }
                                //订单预警插入
                                else
                                {
                                    if (_unitWork.IsExist<OrderAlert>(n => n.OrderNo == t.ShipmentCode && n.MaterialNo == t.MaterialCode))
                                    {
                                        OrderAlert orderAlert = _unitWork.Find<OrderAlert>(n => n.OrderNo == t.ShipmentCode && n.MaterialNo == t.MaterialCode).FirstOrDefault();
                                        orderAlert.Qty = (int)t.Qty - (int)t.QtyDivided;
                                        orderAlert.InventoryQty = (int)_unitWork.Find<Inventory>(n => n.MaterialCode == t.MaterialCode).Sum(a => a.Qty);
                                        _unitWork.Update(orderAlert);
                                    }
                                    else{
                                        OrderAlert orderAlert = new OrderAlert
                                        {
                                            OrderNo = t.ShipmentCode,
                                            MaterialNo = t.MaterialCode,
                                            Qty = (int)t.Qty - (int)t.QtyDivided,
                                            InventoryQty = (int)_unitWork.Find<Inventory>(n => n.MaterialCode == t.MaterialCode).Sum(a => a.Qty),
                                            CreateTime = DateTime.Now
                                        };
                                        _unitWork.Add(orderAlert);
                                    }
                                }
                                t.QtyDivided = dividedsum;
                                _appsd.UpdateByTracking(t);

                                ShipmentHeader shipmentHeader = _unitWork.Find<ShipmentHeader>(n => n.Code == t.ShipmentCode && n.LastStatus <= ShipmentHeaderStatus.分配完成).FirstOrDefault();
                                int statuscount = _unitWork.Find<ShipmentDetail>(n => n.ShipmentCode == t.ShipmentCode && n.Status < ShipmentHeaderStatus.分配完成 && n.Qty > n.QtyDivided).Count();
                                if (statuscount > 0)
                                {
                                    shipmentHeader.FirstStatus = ShipmentHeaderStatus.分配完成;
                                    _app.UpdateByTracking(shipmentHeader);

                                }
                                else
                                {
                                    shipmentHeader.FirstStatus = ShipmentHeaderStatus.分配完成;
                                    shipmentHeader.LastStatus = ShipmentHeaderStatus.分配完成;
                                    _app.UpdateByTracking(shipmentHeader);
                                }

                                dcount = dcount + 1;
                            }
                            else
                            {
                                sErrorMsg += "单号为:" + t.ShipmentCode + "料号:" + t.MaterialCode + str + "分配失败" + str + "<br>";
                                tab.msg = sErrorMsg;
                            }
                        }
                        else
                        {
                            sErrorMsg += "单号为:" + t.ShipmentCode + "料号:" + t.MaterialCode + "为0库存或者正在盘点,无法分配!<br>";
                            tab.msg = sErrorMsg;
                        }
                    }
                    totalLinescount = totalLinescount + Convert.ToInt32(shipmentheader.TotalLines);
                }

                if (totalLinescount <= dcount)
                {
                    tab.code = 200;
                    tab.msg = "订单全部成功分配完成!";
                }
                else
                {
                    tab.code = 300;
                    tab.msg = "订单全部分配完成!部分未成功分配信息如下:<br>" + tab.msg;
                }
            }
            catch (Exception ex)
            {
                tab.code = 300;
                tab.msg += ex.Message;
            }
            return tab;
        }
    }
}