Esempio n. 1
0
 public void EnsureCompatibleUnit(IExtendedMaterialModel material, IMaterialUnit unit)
 {
     if (!m_conversionHelper.AreCompatible(material.NominalUnit.Id, unit.Id))
     {
         throw new InvalidOperationException($"Jednotku '{unit.Symbol}' nelze použít pro materiál '{material.Name}'");
     }
 }
Esempio n. 2
0
        public IExtendedMaterialModel CreateBatch(decimal batchAmount, IMaterialUnit preferredBatchUnit, IUnitConversionHelper conversions)
        {
            // Nominal = 1kg
            // Batch = 500g
            var batchUnit = conversions.GetPrefferedUnit(preferredBatchUnit, NominalUnit);                            //g
            var convertedNominalAmount = conversions.ConvertAmount(NominalUnit.Id, batchUnit.Id, NominalAmount);      // 1000g
            var convertedBatchAmount   = conversions.ConvertAmount(preferredBatchUnit.Id, batchUnit.Id, batchAmount); //500g

            var conversionFactor = convertedBatchAmount / convertedNominalAmount;                                     // 0.5

            var batch = new ExtendedMaterial(Adaptee)
            {
                BatchUnit = batchUnit, BatchAmount = convertedBatchAmount
            };

            var batchComponents = new List <MaterialComponent>(m_components.Count);

            foreach (var sourceComponent in m_components)
            {
                var componentBatchAmount = sourceComponent.Amount * conversionFactor;

                var batchedComponentMaterial = sourceComponent.Material.CreateBatch(
                    componentBatchAmount,
                    sourceComponent.Unit,
                    conversions);

                var batchComponent = new MaterialComponent(sourceComponent.Unit, batchedComponentMaterial, componentBatchAmount, null);
                batchComponents.Add(batchComponent);
            }

            return(batch);
        }
Esempio n. 3
0
 public MaterialComponent(IMaterialUnit unit, IExtendedMaterialModel material, decimal amount, int?compositionId)
 {
     Unit          = unit;
     Material      = material;
     Amount        = amount;
     CompositionId = compositionId;
 }
Esempio n. 4
0
 public CompositionViewModel(IExtendedMaterialModel material, decimal amount, IMaterialUnit unit, int depth)
 {
     Material = material;
     Amount   = amount;
     Unit     = unit;
     Depth    = depth;
 }
Esempio n. 5
0
        public MaterialBatchComponent CreateProductionBatch(int materialId,
                                                            string batchNumber,
                                                            decimal amount,
                                                            IMaterialUnit unit,
                                                            decimal productionWorkPrice)
        {
            if (string.IsNullOrWhiteSpace(batchNumber))
            {
                throw new InvalidOperationException("Musí být číslo šarže");
            }

            if (unit == null)
            {
                throw new InvalidOperationException("Musí být vybrána měrná jednotka");
            }

            var material = m_materialRepository.GetMaterialById(materialId);

            if (material == null)
            {
                throw new InvalidOperationException("Invalid entity reference");
            }

            if (!m_conversionHelper.AreCompatible(material.Adaptee.NominalUnitId, unit.Id))
            {
                throw new InvalidOperationException($"Požadovaná měrná jednotka \"{unit.Symbol}\" není platná pro materiál \"{material.Name}\" protože není převoditelná na nominální jednotku materiálu \"{material.NominalUnit.Symbol}\" ");
            }

            var alreadyExisting = GetMaterialBatches(
                DateTime.Now.AddYears(-100),
                DateTime.Now.AddYears(100),
                false,
                materialId,
                true,
                true,
                true).FirstOrDefault(m => m.Batch.BatchNumber.Equals(batchNumber, StringComparison.InvariantCultureIgnoreCase));

            if (alreadyExisting != null)
            {
                throw new InvalidOperationException($"Již existuje šarže {batchNumber} materiálu {material.Name}");
            }

            var entity = m_database.New <IMaterialBatch>();

            entity.BatchNumber         = batchNumber;
            entity.UnitId              = unit.Id;
            entity.AuthorId            = m_session.User.Id;
            entity.Created             = DateTime.Now;
            entity.ProjectId           = m_session.Project.Id;
            entity.MaterialId          = material.Id;
            entity.Volume              = amount;
            entity.Note                = string.Empty;
            entity.ProductionWorkPrice = productionWorkPrice;

            m_database.Save(entity);

            return(GetBatchById(entity.Id));
        }
Esempio n. 6
0
        public Amount(decimal value, IMaterialUnit unit)
        {
            if (unit == null)
            {
                throw new ArgumentNullException(nameof(unit));
            }

            Value = value;
            Unit  = unit;
        }
Esempio n. 7
0
        public IMaterialUnit GetSmallestCompatibleUnit(IMaterialUnit u)
        {
            IUnitConversion smallest = null;

            foreach (var conversion in GetAllConversions().Where(c => c.SourceUnitId == u.Id))
            {
                if ((smallest?.Multiplier ?? 1) < conversion.Multiplier)
                {
                    smallest = conversion;
                }
            }

            if (smallest == null)
            {
                return(u);
            }

            return(m_unitRepository.GetUnit(smallest.TargetUnitId));
        }
Esempio n. 8
0
        public MaterialBatchComponent SaveBottomLevelMaterialBatch(int id, IMaterial material, decimal amount, IMaterialUnit unit, string batchNr, DateTime receiveDt, decimal price, string invoiceNr, string supplierName, string currencySymbol, string variableSymbol)
        {
            if ((material.ProjectId != m_session.Project.Id) || (unit.ProjectId != m_session.Project.Id))
            {
                throw new InvalidOperationException("Illegal entity reference");
            }

            int?supplierId = null;

            if (!string.IsNullOrWhiteSpace(supplierName))
            {
                var supplier =
                    m_supplierRepository.GetSuppliers()
                    .FirstOrDefault(s => s.Name.Equals(supplierName, StringComparison.InvariantCultureIgnoreCase));

                if (supplier == null)
                {
                    throw new InvalidOperationException($"Nenalezen dodavatel \"{supplierName}\"");
                }

                supplierId = supplier.Id;
            }

            MaterialBatchComponent result;

            using (var tx = m_database.OpenTransaction())
            {
                IMaterialBatch entity;
                if (id > 0)
                {
                    throw new InvalidOperationException("UPDATE not implemented => Currency conversion");

                    entity =
                        GetBatchQuery()
                        .Where(b => (b.Id == id) && (b.ProjectId == m_session.Project.Id))
                        .Execute()
                        .FirstOrDefault();
                    if (entity == null)
                    {
                        throw new InvalidOperationException("Illegal entity reference");
                    }
                }
                else
                {
                    entity           = m_database.New <IMaterialBatch>();
                    entity.AuthorId  = m_session.User.Id;
                    entity.ProjectId = m_session.Project.Id;

                    var materialId = material.Id;

                    var existingBatchNr =
                        m_database.SelectFrom <IMaterialBatch>()
                        .Where(
                            b =>
                            (b.ProjectId == m_session.Project.Id) && (b.MaterialId == materialId) &&
                            (b.BatchNumber == batchNr))
                        .Execute()
                        .FirstOrDefault();

                    if (existingBatchNr != null)
                    {
                        throw new InvalidOperationException("Již existuje šarže se zadaným číslem");
                    }
                }

                if (!m_conversionHelper.AreCompatible(material.NominalUnitId, unit.Id))
                {
                    throw new InvalidOperationException(
                              $"Nelze použít jednotku '{unit.Symbol}' pro materiál '{material.Name}' protože není převoditelná na nominální jednotku materiálu '{material.NominalUnit}'");
                }

                if ((material.RequiresPrice == true) && (Math.Abs(price) < 0.000001m))
                {
                    throw new InvalidOperationException("Cena je povinný údaj");
                }

                if ((material.RequiresInvoiceNr == true) && string.IsNullOrWhiteSpace(invoiceNr))
                {
                    throw new InvalidOperationException("Číslo faktury je povinný údaj");
                }

                if ((material.RequiresInvoiceNr == true) && string.IsNullOrWhiteSpace(variableSymbol))
                {
                    throw new InvalidOperationException("Var. symbol je povinný údaj");
                }

                if ((material.RequiresSupplierReference == true) && (supplierId == null))
                {
                    throw new InvalidOperationException("Dodavatel je povinný údaj");
                }

                entity.BatchNumber = batchNr;
                entity.Created     = receiveDt;
                entity.MaterialId  = material.Id;
                entity.Volume      = amount;
                entity.UnitId      = unit.Id;

                entity.Price = m_currencyConversionHelper.TryConvertToPrimaryCurrency(currencySymbol,
                                                                                      price,
                                                                                      c => entity.PriceConversionId = c.Id);

                entity.Note             = string.Empty;
                entity.IsAvailable      = true;
                entity.InvoiceNr        = invoiceNr;
                entity.InvoiceVarSymbol = variableSymbol;
                entity.SupplierId       = supplierId;

                m_database.Save(entity);

                result = GetBatchById(entity.Id);

                if (result.Components.Any())
                {
                    throw new InvalidOperationException($"Materiál '{material.Name}' nelze naskladnit, protože se skládá z jiných materiálů. Použijte prosím funkci Výroba");
                }

                m_materialBatchFacade.Value.ReleaseBatchAmountCache(result.Batch);

                tx.Commit();
            }

            return(result);
        }
Esempio n. 9
0
 public IMaterialUnit GetPrefferedUnit(IMaterialUnit a, IMaterialUnit b)
 {
     //TODO
     return(a);
 }
Esempio n. 10
0
 public Amount Convert(Amount a, IMaterialUnit to)
 {
     return m_conversionHelper.ConvertAmount(a, to.Id);
 }