Exemple #1
0
        public async Task <ActionResultResponse> Insert(string tenantId, string creatorId, string creatorFullName, SupplierMeta supplierMeta)
        {
            //if (!supplierMeta.Contacts.Any())
            //    return new ActionResultResponse(-1, _sharedResourceService.GetString("Please enter at least one contact."));

            var checkInfo = await _supplierRepository.CheckExistsByName(tenantId, null, supplierMeta.Name);

            if (checkInfo)
            {
                return(new ActionResultResponse(-1, _resourceService.GetString("Supplier name already exists.")));
            }

            var supplierId = Guid.NewGuid().ToString();
            var supplier   = new Supplier
            {
                Id               = supplierId,
                Name             = supplierMeta.Name,
                UnsignName       = supplierMeta.Name.StripVietnameseChars().ToUpper(),
                Description      = supplierMeta.Description,
                IsActive         = supplierMeta.IsActive,
                CreateTime       = DateTime.Now,
                ConcurrencyStamp = Guid.NewGuid().ToString(),
                Address          = supplierMeta.Address,
                TenantId         = tenantId,
                CreatorId        = creatorId,
                CreatorFullName  = creatorFullName,
            };

            var result = await _supplierRepository.Insert(supplier);

            if (result <= 0)
            {
                return(new ActionResultResponse(result, _sharedResourceService.GetString("Something went wrong. Please contact with administrator.")));
            }

            if (supplierMeta.Contacts != null && supplierMeta.Contacts.Any())
            {
                var contacts = new List <Contact>();
                foreach (var contact in supplierMeta.Contacts)
                {
                    contacts.Add(new Contact
                    {
                        Id               = Guid.NewGuid().ToString(),
                        SubjectId        = supplier.Id,
                        FullName         = contact.FullName,
                        PositionName     = contact.PositionName,
                        Email            = contact.Email,
                        Fax              = contact.Fax,
                        PhoneNumber      = contact.PhoneNumber,
                        Description      = contact.Description,
                        UnsignName       = contact.FullName.Trim().StripVietnameseChars().ToUpper(),
                        Status           = contact.Status,
                        ConcurrencyStamp = supplier.ConcurrencyStamp,
                        Type             = contact.Type,
                        CreatorId        = creatorId,
                        CreatorFullName  = creatorFullName,
                    });
                }

                if (contacts.Any())
                {
                    var resultInsertDetail = await _contactRepository.Inserts(contacts);

                    if (resultInsertDetail < 0)
                    {
                        await RollbackInsert(supplierId, tenantId);

                        return(new ActionResultResponse(-5, _resourceService.GetString("Can not insert new Supplier. Please contact with administrator.")));
                    }
                }
            }

            return(new ActionResultResponse(result, _sharedResourceService.GetString("Insert supplier success.")));
        }