public ActionResult Edit([Bind("ixInboundOrder,sInboundOrder,sOrderReference,ixInboundOrderType,ixFacility,ixCompany,ixBusinessPartner,dtExpectedAt,ixStatus")] InboundOrdersPost inboundorders)
        {
            if (ModelState.IsValid)
            {
                inboundorders.UserName = User.Identity.Name;
                _inboundordersService.Edit(inboundorders);
                return(RedirectToAction("Index"));
            }
            ViewBag.ixBusinessPartner  = new SelectList(_inboundordersService.selectBusinessPartners().Select(x => new { x.ixBusinessPartner, x.sBusinessPartner }), "ixBusinessPartner", "sBusinessPartner", inboundorders.ixBusinessPartner);
            ViewBag.ixCompany          = new SelectList(_inboundordersService.selectCompanies().Select(x => new { x.ixCompany, x.sCompany }), "ixCompany", "sCompany", inboundorders.ixCompany);
            ViewBag.ixFacility         = new SelectList(_inboundordersService.selectFacilities().Select(x => new { x.ixFacility, x.sFacility }), "ixFacility", "sFacility", inboundorders.ixFacility);
            ViewBag.ixInboundOrderType = new SelectList(_inboundordersService.selectInboundOrderTypes().Select(x => new { x.ixInboundOrderType, x.sInboundOrderType }), "ixInboundOrderType", "sInboundOrderType", inboundorders.ixInboundOrderType);
            ViewBag.ixStatus           = new SelectList(_inboundordersService.selectStatuses().Select(x => new { x.ixStatus, x.sStatus }), "ixStatus", "sStatus", inboundorders.ixStatus);

            return(View(inboundorders));
        }
        public Task <Int64> Create(ReceivingPost receivingPost)
        {
            // Additional validations

            // Pre-process

            //Custom Code Start | Added Code Block
            //If the handling unit does not exist we create it
            if (!HandlingUnitsDb().Where(x => x.sHandlingUnit == receivingPost.sReceipt.Trim()).Any())
            {
                HandlingUnitsPost handlingUnitsPost = new HandlingUnitsPost();
                handlingUnitsPost.sHandlingUnit      = receivingPost.sReceipt;
                handlingUnitsPost.ixHandlingUnitType = receivingPost.ixHandlingUnitType ?? 0;
                handlingUnitsPost.UserName           = receivingPost.UserName;
                receivingPost.ixHandlingUnit         = _handlingunitsService.Create(handlingUnitsPost).Result;
            }
            else
            {
                receivingPost.ixHandlingUnit = HandlingUnitsDb().Where(x => x.sHandlingUnit == receivingPost.sReceipt.Trim()).Select(x => x.ixHandlingUnit).FirstOrDefault();
            }

            InventoryUnitsPost inventoryUnit = new InventoryUnitsPost();
            var inboundOrder = _inboundordersService.GetPost(receivingPost.ixInboundOrder);

            inventoryUnit.ixFacility          = inboundOrder.ixFacility;
            inventoryUnit.ixCompany           = inboundOrder.ixCompany;
            inventoryUnit.ixMaterial          = receivingPost.ixMaterial;
            inventoryUnit.ixInventoryState    = receivingPost.ixInventoryState;
            inventoryUnit.ixHandlingUnit      = receivingPost.ixHandlingUnit;
            inventoryUnit.ixInventoryLocation = receivingPost.ixInventoryLocation;
            inventoryUnit.sSerialNumber       = receivingPost.sSerialNumber;
            inventoryUnit.sBatchNumber        = receivingPost.sBatchNumber;
            inventoryUnit.dtExpireAt          = receivingPost.dtExpireAt;
            inventoryUnit.UserName            = receivingPost.UserName;

            if ((inventoryUnit.sSerialNumber ?? "").Trim() != "")
            {
                //We create an iu
                inventoryUnit.nBaseUnitQuantity = receivingPost.nBaseUnitQuantityReceived;
                _inventoryunitsService.Create(inventoryUnit, _ixInventoryUnitTransactionContext);
            }
            else if (_inventoryunitsService.IndexDb().Where(x =>
                                                            x.ixFacility == inventoryUnit.ixFacility &&
                                                            x.ixCompany == inventoryUnit.ixCompany &&
                                                            x.ixMaterial == inventoryUnit.ixMaterial &&
                                                            x.ixInventoryState == inventoryUnit.ixInventoryState &&
                                                            x.ixHandlingUnit == inventoryUnit.ixHandlingUnit &&
                                                            x.ixInventoryLocation == inventoryUnit.ixInventoryLocation &&
                                                            x.sBatchNumber == inventoryUnit.sBatchNumber &&
                                                            x.dtExpireAt == inventoryUnit.dtExpireAt && x.ixStatus == 5
                                                            ).Select(x => x.ixInventoryUnit).Any()
                     )
            {
                //We edit the iu
                inventoryUnit.ixInventoryUnit = _inventoryunitsService.IndexDb().Where(x =>
                                                                                       x.ixFacility == inventoryUnit.ixFacility &&
                                                                                       x.ixCompany == inventoryUnit.ixCompany &&
                                                                                       x.ixMaterial == inventoryUnit.ixMaterial &&
                                                                                       x.ixInventoryState == inventoryUnit.ixInventoryState &&
                                                                                       x.ixHandlingUnit == inventoryUnit.ixHandlingUnit &&
                                                                                       x.ixInventoryLocation == inventoryUnit.ixInventoryLocation &&
                                                                                       x.sBatchNumber == inventoryUnit.sBatchNumber &&
                                                                                       x.dtExpireAt == inventoryUnit.dtExpireAt && x.ixStatus == 5
                                                                                       ).Select(x => x.ixInventoryUnit).FirstOrDefault();
                inventoryUnit.nBaseUnitQuantity = _inventoryunitsService.GetPost(inventoryUnit.ixInventoryUnit).nBaseUnitQuantity + receivingPost.nBaseUnitQuantityReceived;
                _inventoryunitsService.Edit(inventoryUnit, _ixInventoryUnitTransactionContext);
            }
            else
            {
                //We create an iu
                inventoryUnit.nBaseUnitQuantity = receivingPost.nBaseUnitQuantityReceived;
                _inventoryunitsService.Create(inventoryUnit, _ixInventoryUnitTransactionContext);
            }


            //Custom Code End

            // Process
            this._receivingRepository.RegisterCreate(receivingPost);
            try
            {
                this._receivingRepository.Commit();
            }
            catch (Exception ex)
            {
                this._receivingRepository.Rollback();
                // Log exception
                throw;
            }

            // Post-process

            //Custom Code Start | Added Code Block
            //Now we update the inbound order lines - for now we apply entire qty to the matching line
            var _ixInboundOrderLine = _inboundorderlinesService.IndexDb().Where(x => x.ixInboundOrder == receivingPost.ixInboundOrder && x.ixMaterial == receivingPost.ixMaterial).Select(x => x.ixInboundOrderLine).FirstOrDefault();
            var inboundOrderLine    = _inboundorderlinesService.GetPost(_ixInboundOrderLine);

            inboundOrderLine.nBaseUnitQuantityReceived += receivingPost.nBaseUnitQuantityReceived;
            inboundOrderLine.UserName = receivingPost.UserName;
            if ((inboundOrderLine.nBaseUnitQuantityExpected - inboundOrderLine.nBaseUnitQuantityReceived) == 0)
            {
                inboundOrderLine.ixStatus = _commonLookUps.getStatuses().Where(x => x.sStatus == "Complete").Select(x => x.ixStatus).FirstOrDefault();
            }
            _inboundorderlinesService.Edit(inboundOrderLine);

            //If there are no open qtys we set the inbound order/line status to complete
            if (_inboundorderlinesService.IndexDb().Where(x => x.ixInboundOrder == receivingPost.ixInboundOrder).Select(x => x.nBaseUnitQuantityExpected - x.nBaseUnitQuantityReceived).Sum() == 0)
            {
                var inboundorder = _inboundordersService.GetPost(receivingPost.ixInboundOrder);
                inboundorder.ixStatus = _commonLookUps.getStatuses().Where(x => x.sStatus == "Complete").Select(x => x.ixStatus).FirstOrDefault();
                _inboundordersService.Edit(inboundorder);
            }

            //Custom Code End

            return(Task.FromResult(receivingPost.ixReceipt));
        }