コード例 #1
0
        private string save(ReceivingModel model)
        {
            Receiving entity = getEntityByModel(model);

            if (model.Id > 0)
            {
                List <ReceivingDetailModel> receivingDetail = service.GetAllReceivingDetail(model.Id).Select(rec => new ReceivingDetailModel(rec, true)).ToList();
                if (receivingDetail != null && receivingDetail.Count() > 0)
                {
                    foreach (var item in receivingDetail)
                    {
                        if (!model.ReceivingDetail.Any(rec => rec.PODetailId == item.PODetailId))
                        {
                            string serialResult = deleteSerials(item);
                            if (string.IsNullOrEmpty(serialResult))
                            {
                                service.DeleteReceivingDetail(item.Id);
                                deleteLot(item);
                            }
                            else
                            {
                                return(serialResult);
                            }
                        }
                    }
                }
            }

            List <ReceivingDetailModel> tobeUpdatedDetail = model.ReceivingDetail.Where(rec => rec.LocatorId > 0 && rec.WarehouseId > 0).ToList();

            string result = string.Empty;

            if (entity.IsValid())
            {
                bool goodToSave = false;
                foreach (var item in tobeUpdatedDetail)
                {
                    ReceivingDetailModel updatedModel = item;
                    string lotResult = updateLot(updatedModel);
                    int    outVal;
                    bool   isNumeric = int.TryParse(lotResult, out outVal);
                    if (isNumeric || string.IsNullOrEmpty(lotResult))
                    {
                        item.LotNoId = isNumeric ? (long?)Convert.ToInt64(lotResult) : null;
                        string serialResult = updateSerials(updatedModel);
                        if (string.IsNullOrEmpty(serialResult))
                        {
                            goodToSave = true;
                        }
                        else
                        {
                            if (item.LotNoId != null)
                            {
                                LotNumber lot = lotService.GetSingle(item.LotNoId.Value.ToString(), AuthenticationHelper.CompanyId.Value);
                                lot.Qty = lot.Qty - item.ThisPurchaseQty;
                                lotService.Update(lot);
                            }
                            return(serialResult);
                        }
                    }
                    else
                    {
                        return(lotResult);
                    }
                }
                if (goodToSave)
                {
                    if (model.Id > 0)
                    {
                        result = service.Update(entity);
                    }
                    else
                    {
                        result = service.Insert(entity);
                    }

                    if (!string.IsNullOrEmpty(result))
                    {
                        var savedLines = getReceivingDetail(result);
                        if (savedLines.Count() > tobeUpdatedDetail.Count())
                        {
                            var tobeDeleted = savedLines.Take(savedLines.Count() - tobeUpdatedDetail.Count());
                            foreach (var item in tobeDeleted)
                            {
                                string serialResult = deleteSerials(item);
                                if (string.IsNullOrEmpty(serialResult))
                                {
                                    string lotResult = deleteLot(item);
                                    if (string.IsNullOrEmpty(lotResult))
                                    {
                                        service.DeleteReceivingDetail(item.Id);
                                    }
                                    else
                                    {
                                        return("Record can not be deleted");
                                    }
                                }
                                else
                                {
                                    return("Record can not be deleted");
                                }
                            }
                            savedLines = getReceivingDetail(result);
                        }

                        foreach (var detail in tobeUpdatedDetail)
                        {
                            ReceivingDetail detailEntity = getEntityByModel(detail);
                            if (detailEntity.IsValid())
                            {
                                detailEntity.ReceiptId = Convert.ToInt64(result);
                                if (savedLines.Count() > 0)
                                {
                                    detailEntity.Id = savedLines.FirstOrDefault().Id;
                                    savedLines.Remove(savedLines.FirstOrDefault(rec => rec.Id == detailEntity.Id));

                                    string receivingDetailId = service.Update(detailEntity);

                                    if (detailEntity.LotNoId != null)
                                    {
                                        LotNumber lottobeUpdated = lotService.GetSingle(detailEntity.LotNoId.ToString(), AuthenticationHelper.CompanyId.Value);
                                        lottobeUpdated.SourceId = Convert.ToInt64(receivingDetailId);
                                        lotService.Update(lottobeUpdated);
                                    }
                                }
                                else
                                {
                                    string receivingDetailId = service.Insert(detailEntity);

                                    if (detailEntity.LotNoId != null)
                                    {
                                        LotNumber lottobeUpdated = lotService.GetSingle(detailEntity.LotNoId.ToString(), AuthenticationHelper.CompanyId.Value);
                                        lottobeUpdated.SourceId = Convert.ToInt64(receivingDetailId);
                                        lotService.Update(lottobeUpdated);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return("");
        }