void prepareEditModel(EditPurchaseOrderHeaderTempViewModel model, int id)
        {
            var poht = PurchasingService.FindPurchaseOrderHeaderTempModel(id, CurrentCompany, false);

            PrepareViewModel(model, EvolutionResources.bnrAddEditPurchase + " - " + EvolutionResources.lblOrderNumber + ": " + model.PurchaseTemp.OrderNumber.ToString(), id, MakeMenuOptionFlags(0, poht.OriginalRowId, 0) + MenuOptionFlag.RequiresNewPurchase);
            model.ParentId = id;

            // Get the landing date from the Shipment record
            if (model.PurchaseTemp.OrderNumber != null)
            {
                var shipmentContent = ShipmentService.FindShipmentContentByPONoModel(CurrentCompany, model.PurchaseTemp.OrderNumber.Value);
                if (shipmentContent != null && shipmentContent.ShipmentId != null)
                {
                    var shipment = ShipmentService.FindShipmentModel(shipmentContent.ShipmentId.Value, CurrentCompany);
                }
            }

            model.LocationList         = LookupService.FindLocationListItemModel(model.CurrentCompany);
            model.SupplierList         = SupplierService.FindSupplierListItemModel(CurrentCompany);
            model.POStatusList         = LookupService.FindPurchaseOrderHeaderStatusListItemModel();
            model.UserList             = MembershipManagementService.FindUserListItemModel();
            model.PaymentTermsList     = LookupService.FindPaymentTermsListItemModel(model.CurrentCompany);
            model.CommercialTermsList  = LookupService.FindLOVItemsListItemModel(model.CurrentCompany, LOVName.CommercialTerms);
            model.PortList             = LookupService.FindPortsListItemModel();
            model.ShipMethodList       = LookupService.FindLOVItemsListItemModel(model.CurrentCompany, LOVName.ShippingMethod);
            model.ContainerTypeList    = LookupService.FindContainerTypeListItemModel();
            model.FreightForwarderList = LookupService.FindFreightForwardersListItemModel(model.CurrentCompany);
            model.CurrencyList         = LookupService.FindCurrenciesListItemModel();
            model.BrandCategoryList    = ProductService.FindBrandCategoryListItemModel(model.CurrentCompany);
        }
        public ActionResult Edit(int id)
        {
            // Called when a user clicks to edit an order
            var model = new EditPurchaseOrderHeaderTempViewModel();

            var purchase = PurchasingService.FindPurchaseOrderHeaderModel(id, CurrentCompany, true);

            // Copy the order into temp tables for editing
            model.PurchaseTemp = PurchasingService.CopyPurchaseOrderToTemp(CurrentCompany, purchase, CurrentUser);
            prepareEditModel(model, model.PurchaseTemp.Id);

            model.LGS  = PurchasingService.LockPurchaseOrderHeader(purchase);
            model.LGST = PurchasingService.LockPurchaseOrderHeaderTemp(model.PurchaseTemp);

            return(View("Edit", model));
        }
        public ActionResult Add()
        {
            // Called when the user clicks 'Create' to create a new order
            var editModel = new EditPurchaseOrderHeaderTempViewModel();

            var purchase = PurchasingService.FindPurchaseOrderHeaderModel(0, CurrentCompany, true);

            purchase.OrderDate       = DateTimeOffset.Now;
            purchase.SalespersonId   = CurrentUser.Id;
            purchase.BrandCategoryId = CurrentUser.DefaultBrandCategoryId;
            purchase.LocationId      = CurrentCompany.DefaultLocationID;
            purchase.CancelMessage   = CurrentCompany.CancelMessage;

            // Copy the order into temp tables for editing
            editModel.PurchaseTemp = PurchasingService.CopyPurchaseOrderToTemp(CurrentCompany, purchase, CurrentUser);

            return(EditDetails(editModel.PurchaseTemp.Id));
        }
        public ActionResult EditDetails(int id)
        {
            // Called when a user is on one of the order screens and clicks the order details option
            var model = new EditPurchaseOrderHeaderTempViewModel();

            // Use the data in the temp tables
            var purchaseTemp = PurchasingService.FindPurchaseOrderHeaderTempModel(id, CurrentCompany, true);

            purchaseTemp.UserId = CurrentUser.Id;

            model.PurchaseTemp = purchaseTemp;
            prepareEditModel(model, id);

            var purchase = PurchasingService.FindPurchaseOrderHeaderModel(purchaseTemp.OriginalRowId, CurrentCompany, true);

            model.LGS  = PurchasingService.LockPurchaseOrderHeader(purchase);
            model.LGST = PurchasingService.LockPurchaseOrderHeaderTemp(model.PurchaseTemp);

            return(View("Edit", model));
        }
        public ActionResult Save(EditPurchaseOrderHeaderTempViewModel model, string command)
        {
            switch (command.ToLower())
            {
            case "save":
                // Save the screen data back to the temp tables, then copy to live tables and exit
                if (ModelState.IsValid)
                {
                    adjustDates(model.PurchaseTemp, model.TZ);

                    var modelError = PurchasingService.InsertOrUpdatePurchaseOrderHeaderTemp(model.PurchaseTemp, CurrentUser, model.LGST);
                    if (modelError.IsError)
                    {
                        model.Error.SetError(modelError.Message,
                                             "Purchase_" + modelError.FieldName);
                    }
                    else
                    {
                        // Copy the temp tables back to the main tables
                        modelError = PurchasingService.CopyTempToPurchaseOrderHeader(model.PurchaseTemp.Id, CurrentUser, model.LGS);
                        if (modelError.IsError)
                        {
                            prepareEditModel(model, model.PurchaseTemp.Id);
                            model.Error.SetError(modelError.Message,
                                                 "Purchase_" + modelError.FieldName);
                        }
                        else
                        {
                            return(RedirectToAction("Purchases"));
                        }
                    }
                }
                prepareEditModel(model, model.PurchaseTemp.Id);
                return(View("Edit", model));

            default:
                return(RedirectToAction("Index", "Purchasing", new { area = "Purchasing" }));
            }
        }