public static void MarkOrdersForAllocation(List<OrderHeaderViewModel> orders)
        {
            var db = new IstokDoorsDBContext();
            var ordersForAllocation =
                db.OrderHeaders.Include(o => o.Customer)
                    .Include(o => o.Factory)
                    .Include(o => o.OrderFulfillmentSchedule)
                    .Include(o => o.OrderStatu)
                    .Include(o => o.PaymentTerm)
                    .Where(o => o.OrderStatu.OrderStatusId == 0 && o.IsSelectedForAllocation == false && o.IsCancelled == false);

            foreach (var orderToAllocate in ordersForAllocation)
            {
                for (int i = 0; i < orders.Count; i++)
                {
                    if (orderToAllocate.OrderId == orders[i].OrderId && orders[i].IsSelectedForAllocation == true)
                    {
                        orderToAllocate.IsSelectedForAllocation = true;
                        orderToAllocate.OrderStatusId = 2;
                    }

                }

            }

            db.SaveChanges();

        }
        public static void SaveOrderItems(IEnumerable<OrderItem> orderItems)
        {
            var db = new IstokDoorsDBContext();

            foreach (var item in orderItems)
            {
                item.OrderId = LastOrderId.OrderId;

                {
                    db.usp_CreateOrderItem(item.OrderId, item.DoorId, item.Quantity, item.Discount);
                    //db.OrderItems.Add(item);
                    db.SaveChanges();
                }

            }

        }
        public static void PlanQuantitiesForOrderCompletion(List<ViewPlanningJournal> myOrderCompletionJrnl)
        {
            var db = new IstokDoorsDBContext();

            var dbOrderCompletionJrnl = db.PlanningJournals.ToList();

            //Save IsManufacturedForOrder to PlanningJournal
            foreach (var item in dbOrderCompletionJrnl)
            {
                for (int i = 0; i < myOrderCompletionJrnl.Count; i++)
                {

                    if (item.OrderId == myOrderCompletionJrnl[i].OrderId &&
                        item.OrderItemId == myOrderCompletionJrnl[i].OrderItemId)
                    {

                        if (item.IsManufacturedForOrder != myOrderCompletionJrnl[i].IsManufacturedForOrder)
                        {
                            //Calculate the difference between present and former IsManufacturedForOrder quantity
                            int calculatedManufacturedForOrder = myOrderCompletionJrnl[i].IsManufacturedForOrder -
                                                         item.IsManufacturedForOrder??0;

                            db.usp_PlanQuantitiesForOrderCompletion(myOrderCompletionJrnl[i].FactoryId,
                                myOrderCompletionJrnl[i].OrderId, myOrderCompletionJrnl[i].OrderItemId,
                                myOrderCompletionJrnl[i].DoorId, calculatedManufacturedForOrder);

                            item.IsManufacturedForOrder = myOrderCompletionJrnl[i].IsManufacturedForOrder;
                        }

                        //Check wether Order is Done (OrderStatus 4)

                      
                    }

                }
            }

          
            db.SaveChanges();

        }
        public static void UpdateDateOfBatchCompletion(List<ProductionJournal> myProductionJournal)
        {
            var db = new IstokDoorsDBContext();

            var dbProdJrnlList = db.ProductionJournals.ToList();

            foreach (var item in dbProdJrnlList)
            {
                for (int i = 0; i < myProductionJournal.Count; i++)
                {


                    if (myProductionJournal[i].IsManufacturedForBatch == item.ToBeManufacturedForBatch &&
                        item.DateOfBatchCompletion == null && myProductionJournal[i].FactoryId == item.FactoryId && myProductionJournal[i].DoorId == item.DoorId)
                    {
                        item.DateOfBatchCompletion = DateTime.Now;
                    }
                }

            }

            db.SaveChanges();
        }
        public static void SaveProductionJournalUpdate(List<ProductionJournal> myProductionJournal)
        {
            var db = new IstokDoorsDBContext();

            //var  dbProdJrnlList = db.ProductionJournals.ToList();

            for (int i = 0; i < myProductionJournal.Count; i++)
            {

                var linqBatchId = myProductionJournal[i].BatchId;
                var linqFactoryId = myProductionJournal[i].FactoryId;
                var linqDoorId = myProductionJournal[i].DoorId;
                var dbItem = db.ProductionJournals.SingleOrDefault(b=>b.BatchId == linqBatchId && b.DoorId == linqDoorId && b.FactoryId == linqFactoryId);


                int? manufacturedDifference = myProductionJournal[i].IsManufacturedForBatch - dbItem.IsManufacturedForBatch;

                db.usp_UpdateAwaitingAllocQuantityForPlanningJournal(manufacturedDifference,
               dbItem.FactoryId, dbItem.BatchId, dbItem.DoorId);
               
                //Update InventoryJournal and InventoryBalance with Manufactured Quantities
                db.usp_UpdateInvenotriesWithManufacturedQuantities(dbItem.FactoryId, dbItem.DoorId, manufacturedDifference);

                //int quantityManufacuturedForBatch =  myProductionJournal[i].IsManufacturedForBatch;

                //dbItem.IsManufacturedForBatch = 2; // quantityManufacuturedForBatch;
                db.SaveChanges();



            }

            for (int i = 0; i < myProductionJournal.Count; i++)
            {

                var linqBatchId = myProductionJournal[i].BatchId;
                var linqFactoryId = myProductionJournal[i].FactoryId;
                var linqDoorId = myProductionJournal[i].DoorId;
                var dbItem = db.ProductionJournals.SingleOrDefault(b => b.BatchId == linqBatchId && b.DoorId == linqDoorId && b.FactoryId == linqFactoryId);


               
                int quantityManufacuturedForBatch =  myProductionJournal[i].IsManufacturedForBatch;

                dbItem.IsManufacturedForBatch = quantityManufacuturedForBatch;
               



            }
            db.SaveChanges();
            //for (int i = 0; i < myProductionJournal.Count; i++)
            //{
            //    foreach (var item in dbProdJrnlList)

            //    {
            //        if (myProductionJournal[i].BatchId == item.BatchId && myProductionJournal[i].DoorId == item.DoorId && myProductionJournal[i].IsManufacturedForBatch > item.IsManufacturedForBatch && myProductionJournal[i].FactoryId == item.FactoryId && myProductionJournal[i].DateOfBatchCompletion == null)
            //        {
            //            int? manufacturedDifference = myProductionJournal[i].IsManufacturedForBatch - item.IsManufacturedForBatch;

            //            db.usp_UpdateAwaitingAllocQuantityForPlanningJournal(manufacturedDifference,
            //           item.FactoryId, item.BatchId, item.DoorId);

            //            //Update InventoryJournal and InventoryBalance with Manufactured Quantities
            //            db.usp_UpdateInvenotriesWithManufacturedQuantities(item.FactoryId, item.DoorId, manufacturedDifference);


            //            item.IsManufacturedForBatch = myProductionJournal[i].IsManufacturedForBatch;

            //            //db.SaveChanges();

            //        }

            //    }

            //}

            //for (int i = 0; i < myProductionJournal.Count; i++)
            //{
            //    for (int j = 0; j < dbProdJrnlList.Count; j++)

            //    {
            //        if (myProductionJournal[i].BatchId == dbProdJrnlList[j].BatchId && myProductionJournal[i].DoorId == dbProdJrnlList[j].DoorId && myProductionJournal[i].IsManufacturedForBatch > dbProdJrnlList[j].IsManufacturedForBatch && myProductionJournal[i].FactoryId == dbProdJrnlList[j].FactoryId && myProductionJournal[i].DateOfBatchCompletion == null)
            //        {
            //            int? manufacturedDifference = myProductionJournal[i].IsManufacturedForBatch - dbProdJrnlList[j].IsManufacturedForBatch;

            //            db.usp_UpdateAwaitingAllocQuantityForPlanningJournal(manufacturedDifference,
            //            myProductionJournal[i].FactoryId, myProductionJournal[i].BatchId, myProductionJournal[i].DoorId);

            //            //Update InventoryJournal and InventoryBalance with Manufactured Quantities
            //            db.usp_UpdateInvenotriesWithManufacturedQuantities(myProductionJournal[i].FactoryId, myProductionJournal[i].DoorId, manufacturedDifference);


            //            //dbProdJrnlList[j].IsManufacturedForBatch = myProductionJournal[i].IsManufacturedForBatch;
            //            var linqFactoryId = myProductionJournal[i].FactoryId;
            //            var linqBatchId = myProductionJournal[i].BatchId;
            //            var iinqDoorId = myProductionJournal[i].DoorId;

            //            var batchToUpdate = db.ProductionJournals.SingleOrDefault(b => b.FactoryId == linqFactoryId && b.DoorId == iinqDoorId && b.BatchId == linqBatchId);

            //            batchToUpdate.IsManufacturedForBatch = myProductionJournal[i].IsManufacturedForBatch;

            //            db.SaveChanges();

            //        }
            //    }



        }
        //================================== PLAN QUANTITIES FOR PRODUCTION ===========================
        public static void PlanQuantitiesForProduction(List<ViewPlanningJournal> myPlanJrnlList)
        {
            var db = new IstokDoorsDBContext();

            for (int i = 0; i < myPlanJrnlList.Count; i++)
            {
                db.usp_PlanQuantitiesForProduction(myPlanJrnlList[i].OrderId, myPlanJrnlList[i].OrderItemId,
                    myPlanJrnlList[i].FactoryId, myPlanJrnlList[i].AllocateFromInventory);


            }

            db.SaveChanges();

        }
        //================================== MOVE ORDERS FROM PLANNING JOURNAL TO PRODUCTION JOURNAL ===========================


        public static void SetTrueIsSelectedForProductionPlanJrnl(List<ViewPlanningJournal> myPlanJrnlList)
        {
            var db = new IstokDoorsDBContext();

            var dbPlanningJrnl = db.PlanningJournals.Include(oh => oh.OrderHeader).ToList();

            foreach (var item in dbPlanningJrnl)
            {
                for (int i = 0; i < myPlanJrnlList.Count; i++)
                {
                    if (item.OrderId == myPlanJrnlList[i].OrderId)
                    {
                        item.IsSelectedForProduction = true;
                       

                    }

                }

            }

            db.SaveChanges();


        }
        public static void MoveOrdersFromAllJournlToPlanJournl(List<AllocationJournal> myAllJournalList)
        {
            var db = new IstokDoorsDBContext();


            var orderHeaders = db.OrderHeaders.ToList();
            var allocOrders = db.AllocationJournals.ToList();

            //db.usp_FindHowManyDoorsInProductionPerFactory();

            foreach (var orderHeader in orderHeaders)
            {
                for (int i = 0; i < myAllJournalList.Count; i++)
                {
                    if (myAllJournalList[i].FactoryId > 0 && myAllJournalList[i].OrderId == orderHeader.OrderId)
                    {
                        orderHeader.OrderStatusId = 2;
                        orderHeader.FactoryId = myAllJournalList[i].FactoryId;

                        //Remove Allocated Order from Allocation Journal
                        AllocationJournal allocatedJournal = db.AllocationJournals.Find(myAllJournalList[i].OrderId);
                        db.AllocationJournals.Remove(allocatedJournal);


                    }

                }
            }


            db.SaveChanges();




        }
        public static void AllocateOrdersToFactories(List<AllocationJournal> myAllJournalList)
        {
            var db = new IstokDoorsDBContext();


            for (int i = 0; i < myAllJournalList.Count; i++)
            {
                db.usp_CalculateQuantityInProduction(myAllJournalList[i].FactoryId);
                AllocationJournal allocationjournal = db.AllocationJournals.Find(myAllJournalList[i].OrderId);
                allocationjournal.FactoryId = myAllJournalList[i].FactoryId;

                db.SaveChanges();
            }


        }
        public static void SaveOrder(OrderHeader orderHeader)
        {
            var db = new IstokDoorsDBContext();


            var myOrderHeader = new OrderHeader();
            myOrderHeader.OrderId = orderHeader.OrderId;
            myOrderHeader.CustomerId = orderHeader.CustomerId;
            myOrderHeader.PaymentTermId = orderHeader.PaymentTermId;
            myOrderHeader.DateOfCreation = orderHeader.DateOfCreation;
            myOrderHeader.Memo = orderHeader.Memo;
            myOrderHeader.OrderStatusId = 0;


            db.OrderHeaders.Add(myOrderHeader);


            var myOrderSchedule = new OrderFulfillmentSchedule();
            myOrderSchedule.OrderId = orderHeader.OrderId;
            myOrderSchedule.PlannedDateOfManufactoring = orderHeader.OrderFulfillmentSchedule.PlannedDateOfManufactoring;
            myOrderSchedule.PlannedDateOfShipping = orderHeader.OrderFulfillmentSchedule.PlannedDateOfShipping;


            db.OrderFulfillmentSchedules.Add(myOrderSchedule);

            db.SaveChanges();

            var lastOrderId = db.OrderHeaders.OrderByDescending(o => o.OrderId).FirstOrDefault();
            LastOrderId.OrderId = lastOrderId.OrderId;
        }
        public static void SaveOrderSchedule(OrderScheduleViewModel myOrderScheduleViewModel)
        {
            var db = new IstokDoorsDBContext();

            var myOrderScheduleMvc = db.OrderFulfillmentSchedules.Find(myOrderScheduleViewModel.OrderId);


            myOrderScheduleMvc.DateEntryToFactory = myOrderScheduleViewModel.DateEntryToFactory;
            myOrderScheduleMvc.PlannedDateOfManufactoring = myOrderScheduleViewModel.PlannedDateOfManufacture;
            myOrderScheduleMvc.PlannedDateOfShipping = myOrderScheduleViewModel.PlannedDateOfShipping;
            myOrderScheduleMvc.ActualDateOfManufactoring = myOrderScheduleViewModel.ActualDateOfManufacture;
            myOrderScheduleMvc.ActualDateOfShipping = myOrderScheduleViewModel.ActualDateOfShipping;

            db.SaveChanges();

        }
        public static void AddDetailsToSchedule(DateTime? plannedDateOfShipping, DateTime? acutalDateOfManufacturing,
            DateTime? plannedDateOfManufacturing, DateTime? actualDateOfShipping, int OrderId)
        {
            var db = new IstokDoorsDBContext();
            var scheduleFromDb = db.OrderFulfillmentSchedules.Find(OrderId);

            scheduleFromDb.PlannedDateOfManufactoring = plannedDateOfManufacturing;
            scheduleFromDb.ActualDateOfManufactoring = acutalDateOfManufacturing;
            scheduleFromDb.PlannedDateOfShipping = plannedDateOfShipping;
            scheduleFromDb.ActualDateOfShipping = actualDateOfShipping;
            db.SaveChanges();

        }