public void SetBatchPreferrence(BatchKey batchKey)
        {
            var materialId  = batchKey.GetMaterialId(m_batchRepository);
            var batchNumber = batchKey.GetBatchNumber(m_batchRepository);

            var existingPreferrence = LoadPreferrences().FirstOrDefault(p => p.MaterialId == materialId) ?? m_database.New <IPackingPreferredBatch>(
                p =>
            {
                p.MaterialId = materialId;
                p.UserId     = m_session.User.Id;
            });

            if (existingPreferrence.BatchNumber?.Equals(batchNumber, StringComparison.InvariantCultureIgnoreCase) !=
                true)
            {
                m_cache.Remove(GetPreferredBatchesCacheKey());
            }
            else if ((DateTime.Now - existingPreferrence.LastActivity).TotalMinutes < 5)
            {
                //to reduce db writes
                return;
            }

            existingPreferrence.BatchNumber  = batchNumber;
            existingPreferrence.LastActivity = DateTime.Now;

            m_database.Save(existingPreferrence);
        }
Esempio n. 2
0
        public IEnumerable <Tuple <IPurchaseOrder, decimal> > GetOrdersByUsedBatch(BatchKey bqatch, int pageSize, int pageNumber)
        {
            var orderIds = new List <Tuple <long, decimal> >();

            m_database.Sql().Call("GetOrderIdsByUsedBatch").WithParam("@projectId", m_session.Project.Id)
            .WithParam("@materialId", bqatch.GetMaterialId(m_batchFacade))
            .WithParam("@batchNumber", bqatch.GetBatchNumber(m_batchFacade))
            .WithParam("@skip", pageSize * pageNumber).WithParam("@take", pageSize).ReadRows <long, int, string, decimal>(
                (orderId, prio, orderNum, qty) =>
            {
                orderIds.Add(new Tuple <long, decimal>(orderId, qty));
            });

            var entities = m_database.SelectFrom <IPurchaseOrder>()
                           .Where(o => o.Id.InCsv(orderIds.Select(i => i.Item1)))
                           .Execute().ToList();

            foreach (var id in orderIds)
            {
                var ett = entities.FirstOrDefault(e => e.Id == id.Item1);
                if (ett != null)
                {
                    yield return(new Tuple <IPurchaseOrder, decimal>(ett, id.Item2));
                }
            }
        }
Esempio n. 3
0
        public IEnumerable <IMaterialBatch> GetBatches(BatchKey key)
        {
            var materialId  = key.GetMaterialId(this);
            var batchNumber = key.GetBatchNumber(this);

            var entities = m_database.SelectFrom <IMaterialBatch>().Where(b => b.ProjectId == m_session.Project.Id)
                           .Where(b => b.CloseDt == null).Where(b => b.MaterialId == materialId && b.BatchNumber == batchNumber)
                           .OrderBy(b => b.Created).Execute().ToList();

            return(entities.Select(e => new MaterialBatchAdapter(e, m_serviceLocator)));
        }
Esempio n. 4
0
        public IEnumerable <IMaterialStockEvent> GetBatchEvents(BatchKey key)
        {
            var batchNumber = key.GetBatchNumber(m_batchRepository);
            var materialId  = key.GetMaterialId(m_batchRepository);

            return
                (m_database.SelectFrom <IMaterialStockEvent>()
                 .Join(e => e.Batch)
                 .Where(m => m.ProjectId == m_session.Project.Id)
                 .Where(m => m.Batch.MaterialId == materialId && m.Batch.BatchNumber == batchNumber)
                 .Execute().Select(b => new MaterialStockEventAdapter(m_serviceLocator, b)));
        }
Esempio n. 5
0
        public IEnumerable <ISaleEventAllocation> GetAllocationsByBatch(BatchKey key)
        {
            var batchNumber = key.GetBatchNumber(m_batchFacade);
            var materialId  = key.GetMaterialId(m_batchFacade);

            var allocs = m_database.SelectFrom <ISaleEventAllocation>()
                         .Join(a => a.Batch)
                         .Where(b => b.Batch.MaterialId == materialId &&
                                b.Batch.BatchNumber == batchNumber &&
                                b.Batch.ProjectId == m_session.Project.Id)
                         .Execute();

            return(allocs.Select(a => new SaleEventAllocationAdapter(m_serviceLocator, a)));
        }
Esempio n. 6
0
        private void PopulateCompositionAmounts(BatchKey componentBatchId, List <BatchReportEntryBase> report)
        {
            foreach (var reportRow in report.OfType <BatchReportEntry>())
            {
                var componentBatches = m_batchRepository.GetBatches(componentBatchId).ToList();
                if (!componentBatches.Any())
                {
                    continue;
                }

                Amount theAmount = null;

                foreach (var compnentBatch in componentBatches)
                {
                    foreach (var compositionBatch in m_batchRepository.GetBatches(reportRow.BatchKey))
                    {
                        foreach (var componentEntity in compositionBatch.Components)
                        {
                            var componentBatch = componentEntity.Component;
                            if (componentBatch.MaterialId != componentBatchId.GetMaterialId(m_batchRepository) ||
                                (!componentBatch.BatchNumber.Equals(componentBatchId.GetBatchNumber(m_batchRepository),
                                                                    StringComparison.InvariantCultureIgnoreCase)))
                            {
                                continue;
                            }

                            var usedAmount = new Amount(componentEntity.Volume, componentEntity.Unit);

                            theAmount = m_amountProcessor.Add(theAmount, usedAmount);
                        }
                    }
                }

                reportRow.CustomField1 = theAmount?.ToString();
            }
        }
Esempio n. 7
0
        private List <BatchReportEntryBase> PopulateRelativeToOrder(IPurchaseOrder order, List <BatchReportEntryBase> batches)
        {
            var concreteItems = m_ordersFacade.GetAllConcreteOrderItems(order).ToList();
            var result        = new List <BatchReportEntryBase>(concreteItems.Count * 2);

            foreach (var orderItem in concreteItems)
            {
                var itemName = orderItem.KitParent?.PlacedName ?? orderItem.PlacedName;

                var assignedBatches = orderItem.AssignedBatches.ToList();
                if (!assignedBatches.Any())
                {
                    assignedBatches.Add(null);
                }

                foreach (var assignment in assignedBatches)
                {
                    var assignmentBatchKey = new BatchKey(assignment.MaterialBatchId);

                    var assignmentBatchMaterialId = assignmentBatchKey.GetMaterialId(m_batchFacade);
                    var assignmentBatchNumber     = assignmentBatchKey.GetBatchNumber(m_batchFacade);

                    var sourceRecord = batches.OfType <BatchReportEntry>().FirstOrDefault(b => b.MaterialId == assignmentBatchMaterialId && b.BatchNumber.Equals(assignmentBatchNumber, StringComparison.InvariantCultureIgnoreCase));

                    var user = assignment == null ? string.Empty : m_userRepository.GetUserNick(assignment.UserId);

                    var reportRow = new BatchReportEntry(BatchKey.Parse(sourceRecord.BatchId))
                    {
                        CustomField1         = StringUtil.FormatDecimal(assignment?.Quantity ?? orderItem.Quantity),
                        CustomField2         = user,
                        CustomField3         = itemName,
                        InventoryName        = sourceRecord?.InventoryName,
                        BatchNumber          = sourceRecord?.BatchNumber ?? string.Empty,
                        MaterialName         = sourceRecord?.MaterialName ?? string.Empty,
                        MaterialId           = sourceRecord?.MaterialId ?? -1,
                        BatchVolume          = sourceRecord?.BatchVolume ?? string.Empty,
                        AvailableAmount      = sourceRecord?.AvailableAmount ?? string.Empty,
                        CreateDt             = sourceRecord?.CreateDt ?? string.Empty,
                        IsClosed             = sourceRecord?.IsClosed ?? false,
                        IsLocked             = sourceRecord?.IsLocked ?? false,
                        IsAvailable          = sourceRecord?.IsAvailable ?? false,
                        NumberOfComponents   = sourceRecord?.NumberOfComponents ?? 0,
                        NumberOfCompositions = sourceRecord?.NumberOfCompositions ?? 0,
                        NumberOfOrders       = sourceRecord?.NumberOfOrders ?? 0,
                        Price              = sourceRecord?.Price ?? string.Empty,
                        InvoiceNumber      = sourceRecord?.InvoiceNumber ?? string.Empty,
                        NumberOfSaleEvents = sourceRecord?.NumberOfSaleEvents ?? 0,
                        NumberOfSegments   = sourceRecord?.NumberOfSegments ?? 0
                    };

                    result.Add(reportRow);
                }
            }

            result.Sort(
                new GenericComparer <BatchReportEntryBase>(
                    (a, b) =>
                    string.Compare((a as BatchReportEntry)?.CustomField2,
                                   (b as BatchReportEntry)?.CustomField2,
                                   StringComparison.Ordinal)));

            return(result);
        }