Example #1
0
        private void AttemptToAllocateCastings(IEnumerable <ScheduleWithQueues> schs, MazakSchedulesAndLoadActions mazakData)
        {
            // go through each job and check for castings in an input queue that have not yet been assigned to a job
            foreach (var job in schs.Where(s => !s.LowerPriorityScheduleMatchingPartSkipped).OrderBy(s => s.Job.RouteStartingTimeUTC).GroupBy(s => s.Unique))
            {
                var uniqueStr = job.Key;
                var partName  = job.First().Job.PartName;
                var numProc   = job.First().Job.NumProcesses;
                log.Debug("Checking job {uniq} with for unallocated castings", uniqueStr);

                bool needRecheckProc1 = false;

                // check for any raw material to assign to the job
                var proc1Queues =
                    job
                    .Select(s => s.Procs[1].InputQueue)
                    .Where(q => !string.IsNullOrEmpty(q))
                    .Distinct();
                foreach (var queue in proc1Queues)
                {
                    var inQueue = _log.GetMaterialInQueue(queue);

                    var remain = job.Select(s =>
                                            s.SchRow.PlanQuantity - CountCompletedOrMachiningStarted(s)
                                            ).Sum();
                    var alreadyAssigned =
                        inQueue
                        .Where(m => m.Unique == uniqueStr && FindNextProcess(m.MaterialID) == 1)
                        .Count();
                    var partsToAllocate =
                        inQueue
                        .Where(m => string.IsNullOrEmpty(m.Unique) && m.PartName == partName)
                        .Any();
                    log.Debug("Checking unique {uniq} for unallocated castings in queue {queue}, " +
                              " found {remain} remaining parts, {assigned} already assigned parts, {toAllocate} parts to allocate",
                              uniqueStr, queue, remain, alreadyAssigned, partsToAllocate);

                    if (remain > alreadyAssigned && partsToAllocate)
                    {
                        var newMats = _log.AllocateCastingsInQueue(queue, partName, uniqueStr, numProc, remain - alreadyAssigned);
                        log.Debug("Alloacted new ids {@matIds} to {unique}, recalculating proc1 castings", newMats, uniqueStr);
                        needRecheckProc1 = true;
                    }
                }

                if (needRecheckProc1)
                {
                    CheckCastingsForProc1(job, mazakData);
                }
            }
        }