public ActionResult Create([Bind("ixPickBatch,sPickBatch,ixPickBatchType,bMultiResource,dtStartBy,dtCompleteBy,ixStatus")] PickBatchesPost pickbatches)
        {
            if (ModelState.IsValid)
            {
                pickbatches.UserName = User.Identity.Name;
                _pickbatchesService.Create(pickbatches);
                return(RedirectToAction("Index"));
            }
            ViewBag.ixPickBatchType = new SelectList(_pickbatchesService.selectPickBatchTypes().Select(x => new { x.ixPickBatchType, x.sPickBatchType }), "ixPickBatchType", "sPickBatchType");
            ViewBag.ixStatus        = new SelectList(_pickbatchesService.selectStatuses().Select(x => new { x.ixStatus, x.sStatus }), "ixStatus", "sStatus");

            return(View(pickbatches));
        }
        public Task <Int64> Create(OutboundOrdersPost outboundordersPost)
        {
            // Additional validations

            // Pre-process
            //Custom Code Start | Added Code Block

            //We batch and consign the order
            //As a simple placeholder we use Discrete order picking - single picker
            PickBatchesPost pickBatch = new PickBatchesPost();

            pickBatch.ixPickBatchType = _pickbatchesService.PickBatchTypesDb().Where(x => x.sPickBatchType == "Discrete order picking - single picker").Select(x => x.ixPickBatchType).FirstOrDefault();
            pickBatch.bMultiResource  = false;
            pickBatch.dtStartBy       = DateTime.Now;
            pickBatch.dtCompleteBy    = pickBatch.dtStartBy.Add(new TimeSpan(1, 0, 0, 0));
            pickBatch.ixStatus        = _pickbatchesService.StatusesDb().Where(x => x.sStatus == "Inactive").Select(x => x.ixStatus).FirstOrDefault();
            pickBatch.UserName        = outboundordersPost.UserName;
            var ixPickBatch = _pickbatchesService.Create(pickBatch).Result;

            outboundordersPost.ixPickBatch = ixPickBatch;

            //We start by trying to find an existing active outboundShipment that matches out order
            if (_outboundshipmentsService.IndexDb().Where(x =>
                                                          x.ixFacility == outboundordersPost.ixFacility &&
                                                          x.ixCompany == outboundordersPost.ixCompany &&
                                                          x.ixCarrier == _outboundordersRepository.CarrierServicesDb().Where(c => c.ixCarrierService == outboundordersPost.ixCarrierService).Select(c => c.Carriers.ixCarrier).FirstOrDefault() &&
                                                          x.ixAddress == _outboundordersRepository.BusinessPartnersDb().Where(b => b.ixBusinessPartner == outboundordersPost.ixBusinessPartner).Select(b => b.ixAddress).FirstOrDefault() &&
                                                          x.ixStatus == _outboundordersRepository.StatusesDb().Where(s => s.sStatus == "Active").Select(s => s.ixStatus).FirstOrDefault()
                                                          ).Select(x => x.ixOutboundShipment).Any())
            {
                var ixOutboundShipment = _outboundshipmentsService.IndexDb().Where(x =>
                                                                                   x.ixFacility == outboundordersPost.ixFacility &&
                                                                                   x.ixCompany == outboundordersPost.ixCompany &&
                                                                                   x.ixCarrier == _outboundordersRepository.CarrierServicesDb().Where(c => c.ixCarrier == outboundordersPost.ixCarrierService).Select(c => c.Carriers.ixCarrier).FirstOrDefault() &&
                                                                                   x.ixAddress == _outboundordersRepository.BusinessPartnersDb().Where(b => b.ixBusinessPartner == outboundordersPost.ixBusinessPartner).Select(b => b.ixAddress).FirstOrDefault() &&
                                                                                   x.ixStatus == _outboundordersRepository.StatusesDb().Where(s => s.sStatus == "Active").Select(s => s.ixStatus).FirstOrDefault()
                                                                                   ).Select(x => x.ixOutboundShipment).FirstOrDefault();
                outboundordersPost.ixOutboundShipment = ixOutboundShipment;
            }
            else
            {
                OutboundShipmentsPost outboundShipment = new OutboundShipmentsPost();
                outboundShipment.ixFacility = outboundordersPost.ixFacility;
                outboundShipment.ixCompany  = outboundordersPost.ixCompany;
                outboundShipment.ixCarrier  = _outboundordersRepository.CarrierServicesDb().Where(x => x.ixCarrier == outboundordersPost.ixCarrierService).Select(x => x.Carriers.ixCarrier).FirstOrDefault();
                var carrier = _carriersService.GetPost(outboundShipment.ixCarrier);
                if (carrier.nCarrierConsignmentNumberLastUsed == null || carrier.nCarrierConsignmentNumberLastUsed < carrier.nCarrierConsignmentNumberStart)
                {
                    outboundShipment.sCarrierConsignmentNumber = carrier.sCarrierConsignmentNumberPrefix + carrier.nCarrierConsignmentNumberStart.ToString();
                    carrier.nCarrierConsignmentNumberLastUsed  = carrier.nCarrierConsignmentNumberStart;
                }
                else
                {
                    outboundShipment.sCarrierConsignmentNumber = carrier.sCarrierConsignmentNumberPrefix + (carrier.nCarrierConsignmentNumberLastUsed + 1).ToString();
                    carrier.nCarrierConsignmentNumberLastUsed += 1;
                }
                _carriersService.Edit(carrier);
                outboundShipment.ixStatus  = _outboundordersRepository.StatusesDb().Where(s => s.sStatus == "Active").Select(s => s.ixStatus).FirstOrDefault();
                outboundShipment.ixAddress = _outboundordersRepository.BusinessPartnersDb().Where(b => b.ixBusinessPartner == outboundordersPost.ixBusinessPartner).Select(b => b.ixAddress).FirstOrDefault();
                outboundShipment.UserName  = outboundordersPost.UserName;
                var ixOutboundShipment = _outboundshipmentsService.Create(outboundShipment).Result;
                outboundordersPost.ixOutboundShipment = ixOutboundShipment;
            }
            //Custom Code End


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

            // Post-process

            return(Task.FromResult(outboundordersPost.ixOutboundOrder));
        }