Пример #1
0
        public ActionResult SaveShipment(ShipmentViewDTO model)
        {
            CustomJsonResult result = new CustomJsonResult();

            if (model.Id < 1)
            {
                // create shipment
                if (model.LstPackage != null && model.LstPackage.Any())
                {
                    // lấy destination của 1 package đầu tiên
                    var         packageFirst = model.LstPackage[0];
                    tblShipment shipment     = new tblShipment();
                    shipment.WarehouseId = packageFirst.WarehouseId;
                    //shipment.Destination = packageFirst.Destination;
                    shipment.Destination = model.Destination;
                    if (string.IsNullOrEmpty(model.ShipmentCode))
                    {
                        string startCode = Lib.Business.Common.ServiceHelper.GetDestionationCodeById(packageFirst.Destination);
                        model.ShipmentCode = $"{startCode}{DateTime.Now.ToString("ddMMyy")}{_shipmentService.Admin_CountShipmentOnDateByDestination(packageFirst.Destination) + 1}";
                    }
                    shipment.ShipmentCode = model.ShipmentCode;
                    shipment.TotalWeight  = Math.Round(model.LstPackage.Sum(x => x.Weight), 2);
                    shipment.CreateTime   = (int)model.CreateTime;
                    shipment.Id           = _shipmentService.Admin_CreateShipment(shipment);
                    if (shipment.Id < 1)
                    {
                        result.Message = "Có lỗi xảy ra khi tạo shipment";
                        result.Result  = -1;
                    }
                    else
                    {
                        _shipmentService.Admin_UpdateShipmentForPackage(model.LstPackage.Select(x => x.Id).ToList(), shipment.Id, shipment.ShipmentCode);
                    }
                }
                else
                {
                    result.Message = "Thông tin tạo shipment không hợp lệ!";
                    result.Result  = -1;
                }
            }
            else
            {
                // update shipment
                tblShipment shipment = model.BindToModel <tblShipment>();
                if (model.LstPackage.Count > 0)
                {
                    shipment.TotalWeight = Math.Round(model.LstPackage.Sum(x => x.Weight), 2);
                }
                if (_shipmentService.Admin_UpdateShipment(shipment) > 0)
                {
                    var shipmentOld = _shipmentService.Admin_GeDetailShipmentHasPackageById(model.Id);
                    if (shipmentOld.LstPackage == null)
                    {
                        shipmentOld.LstPackage = new List <PackageViewDTO>();
                    }
                    if (model.LstPackage != null && model.LstPackage.Any())
                    {
                        // lấy các shipment remove
                        List <long> lstPackageIdRemove = new List <long>();
                        foreach (var item in shipmentOld.LstPackage)
                        {
                            var o = model.LstPackage.FirstOrDefault(x => x.Id == item.Id);
                            if (o == null)
                            {
                                lstPackageIdRemove.Add(item.Id);
                            }
                        }
                        if (lstPackageIdRemove.Any())
                        {
                            _shipmentService.Admin_UpdateShipmentForPackage(lstPackageIdRemove, null, null);
                        }
                        // các shipment thêm mới
                        var lstPackagenew = model.LstPackage.Where(x => x.ShipmentId < 1).ToList();
                        if (lstPackagenew != null && lstPackagenew.Any())
                        {
                            _shipmentService.Admin_UpdateShipmentForPackage(lstPackagenew.Select(x => x.Id).ToList(), shipmentOld.Id, shipmentOld.ShipmentCode);
                        }
                    }
                    else
                    {
                        // xóa hết các package hiện có của shipment này
                        _shipmentService.Admin_UpdateShipmentForPackage(shipmentOld.LstPackage.Select(x => x.Id).ToList(), null, null);
                    }
                }
                else
                {
                    result.Message = "Có lỗi xảy ra khi cập nhật shipment";
                    result.Result  = -1;
                }
            }
            return(Json(result));
        }