Esempio n. 1
0
        public IExtendedMaterialModel ProcessMaterialEditRequest(int?materialId,
                                                                 string name,
                                                                 string nominalAmountText,
                                                                 int materialInventoryId,
                                                                 bool automaticBatches,
                                                                 bool requiresPrice,
                                                                 bool requiresProductionPrice,
                                                                 bool requiresIncvoice,
                                                                 bool requiresSupplierReference,
                                                                 bool autofinalize,
                                                                 bool canBeDigital,
                                                                 IEnumerable <string> components,
                                                                 string thresholdText
                                                                 )
        {
            name = name?.Trim();

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new InvalidOperationException("Materiál musí mít název");
            }

            if (string.IsNullOrWhiteSpace(nominalAmountText))
            {
                throw new ArgumentException("Materiál musí mít vlastní množství a jednotku");
            }

            var nominalAmountEntry = MaterialEntry.Parse(nominalAmountText, true);

            var nominalUnit = ValidateAmountUnit(nominalAmountEntry);

            using (var tx = m_database.OpenTransaction())
            {
                var material = m_materialRepository.UpsertMaterial(
                    materialId,
                    name,
                    nominalAmountEntry.Amount,
                    nominalUnit.Id,
                    materialInventoryId,
                    automaticBatches,
                    requiresPrice,
                    requiresProductionPrice,
                    requiresIncvoice,
                    requiresSupplierReference, autofinalize, canBeDigital);

                if (thresholdText == null)
                {
                    m_materialThresholdRepository.DeleteThreshold(material.Id);
                }
                else
                {
                    try
                    {
                        var thresholdEntry = MaterialEntry.Parse(thresholdText, true);

                        var thresholdUnit = m_unitRepository.GetUnitBySymbol(thresholdEntry.UnitName);
                        if (thresholdUnit == null)
                        {
                            throw new InvalidOperationException($"Neznámý symbol jednotky \"{thresholdEntry.UnitName}\"");
                        }

                        m_materialThresholdRepository.SaveThreshold(material.Id,
                                                                    thresholdEntry.Amount,
                                                                    thresholdUnit.Id);
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException($"Nelze nastavit sledování minimálního množství - {ex.Message}", ex);
                    }
                }

                tx.Commit();
                m_virtualProductRepository.CleanCache();
                m_materialRepository.CleanCache();
                return(m_materialRepository.GetMaterialById(material.Id));
            }
        }
 public void DeleteVirtualProduct(int vpId)
 {
     m_virtualProductRepository.DeleteVirtualProduct(vpId);
     m_materialRepository.CleanCache();
 }