Exemplo n.º 1
0
        private string updateSerials(ReceivingDetailModel model)
        {
            if (!string.IsNullOrEmpty(model.SerialNo))
            {
                List <string> newSerials = model.SerialNo.Trim().Split(new char[] { ',' }).ToList();
                if (newSerials.Count() != model.ThisPurchaseQty)
                {
                    return("Serials must be according to the Quantity");
                }
            }
            if (model.Id > 0)
            {
                ReceivingDetail savedDetail = service.GetSingleReceivingDetail(model.Id);
                if (!string.IsNullOrEmpty(model.SerialNo))
                {
                    List <string> unsavedSerials = model.SerialNo.Trim().Split(new char[] { ',' }).ToList();
                    if (!string.IsNullOrEmpty(savedDetail.SerialNo))
                    {
                        List <string> savedSerials = savedDetail.SerialNo.Split(new char[] { ',' }).ToList();
                        bool          isAvailable  = true;
                        foreach (var serial in savedSerials)
                        {
                            isAvailable = lotService.CheckSerialNumAvailability(AuthenticationHelper.CompanyId.Value, savedDetail.LotNoId.Value, serial);
                            if (!isAvailable)
                            {
                                return("Serial is in use!");
                            }
                        }

                        if (isAvailable)
                        {
                            if (savedSerials.Count() > unsavedSerials.Count())
                            {
                                List <string> tobeDeleted = savedSerials.Take(savedSerials.Count() - unsavedSerials.Count()).ToList();
                                if (tobeDeleted != null && tobeDeleted.Count() > 0)
                                {
                                    foreach (var item in tobeDeleted)
                                    {
                                        SerialNumber serialNum = lotService.GetSerialNo(item, savedDetail.LotNoId.Value, AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId);
                                        if (serialNum != null)
                                        {
                                            lotService.DeleteSerialNum(serialNum.Id.ToString(), AuthenticationHelper.CompanyId.Value);
                                        }
                                    }
                                }
                            }
                            foreach (var serial in unsavedSerials)
                            {
                                SerialNumber entity = lotService.GetSerialNo(serial, savedDetail.LotNoId.Value, AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId);
                                if (entity != null)
                                {
                                    entity.LotNo    = model.LotNo;
                                    entity.SerialNo = serial;
                                    lotService.UpdateSerialNum(entity);
                                }
                                else
                                {
                                    lotService.InsertSerialNum(new SerialNumber
                                    {
                                        CompanyId  = AuthenticationHelper.CompanyId.Value,
                                        CreateBy   = AuthenticationHelper.UserId,
                                        CreateDate = DateTime.Now,
                                        LotNo      = model.LotNo,
                                        LotNoId    = savedDetail.LotNoId.Value,
                                        SerialNo   = serial,
                                        UpdateBy   = null,
                                        UpdateDate = null
                                    });
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (var serial in unsavedSerials)
                        {
                            lotService.InsertSerialNum(new SerialNumber
                            {
                                CompanyId  = AuthenticationHelper.CompanyId.Value,
                                CreateBy   = AuthenticationHelper.UserId,
                                CreateDate = DateTime.Now,
                                LotNo      = model.LotNo,
                                LotNoId    = savedDetail.Id,
                                SerialNo   = serial,
                                UpdateBy   = null,
                                UpdateDate = null
                            });
                        }
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(model.SerialNo))
                {
                    if (!string.IsNullOrEmpty(model.LotNo))
                    {
                        LotNumber lot = lotService.GetLotbyItem(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, model.ItemId, model.LotNo);
                        if (lot != null)
                        {
                            List <string> serials      = model.SerialNo.Trim().Split(new char[] { ',' }).ToList();
                            bool          notAvailable = true;
                            foreach (var serial in serials)
                            {
                                notAvailable = lotService.CheckSerialNumAvailability(AuthenticationHelper.CompanyId.Value, lot.Id, serial);
                                if (notAvailable)
                                {
                                    return("Serial # " + serial + " is already defined");
                                }
                            }
                            if (!notAvailable)
                            {
                                foreach (var serial in serials)
                                {
                                    lotService.InsertSerialNum(new SerialNumber
                                    {
                                        CompanyId  = AuthenticationHelper.CompanyId.Value,
                                        CreateBy   = AuthenticationHelper.UserId,
                                        CreateDate = DateTime.Now,
                                        LotNo      = lot.LotNo,
                                        LotNoId    = lot.Id,
                                        SerialNo   = serial,
                                        UpdateBy   = null,
                                        UpdateDate = null
                                    });
                                }
                            }
                        }
                        else
                        {
                            return("Lot not found!");
                        }
                    }
                    else
                    {
                        return("Serials can not be defined without lot!");
                    }
                }
            }

            return("");
        }
Exemplo n.º 2
0
 public static string UpdateSerialNumber(SerialNumber entity)
 {
     return(service.UpdateSerialNum(entity));
 }