using System;
using System.Web.Services;
using System.Configuration;
using System.Globalization;

/* =====================================================================

  File:      InstantOrder.asmx.cs for Adventure Works Cycles Storefront Sample
  Summary:   Web service for placing an immediate order from some other web site.
  Date:	     June 16, 2003

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */

namespace Microsoft.Samples.SqlServer
{

	public class InstantOrder : WebService 
	{

		//*******************************************************
		//
		// InstantOrder.OrderItem() Method 
		//
		// The OrderItem method enables a remote client to programmatically
		// place an order using a webservice. 
		//
		//*******************************************************      

		[WebMethod(Description="The OrderItem method enables a remote client to programmatically place an order using a WebService.", EnableSession=false)]
		public OrderDetails OrderItem(string userName, string password, int productID, int quantity,
			string addressLine1, string addressLine2, string addressCity, int addressStateProvinceID, string addressPostalCode) 
		{
        
			// Login client using provided username and password
			CustomersDB accountSystem = new CustomersDB();
			CustomerDetails customerDetails = accountSystem.Login(userName, password);
			String customerId = customerDetails.CustomerID;
        
			if (customerId == null) 
			{
				throw new Exception("Error: Invalid Login!");
			}

			// Wrap in try/catch block to catch errors in the event that someone types in
			// an invalid value for quantity
			int qty = System.Math.Abs(quantity);
			if (qty == quantity && qty < 1000) 
			{
        
				// Add Item to Shopping Cart
				ShoppingCartDB myShoppingCart = new ShoppingCartDB();
				myShoppingCart.AddItem(customerId, productID, qty);

				bool isExtendedProcessing = 
					 (ConfigurationSettings.AppSettings["ExtendedOrderProcessing"].ToUpper(WorldReady.USCulture) ==
											 "TRUE");
					
				int addressID =
					accountSystem.AccessCustomerAddress(Int32.Parse(
					customerId, CultureInfo.InvariantCulture), "Billing",
																	new
					CustomerAddress(0, addressLine1, addressLine2, addressCity,
									addressStateProvinceID, "", addressPostalCode));


				// Place Order
				OrdersDB orderSystem = new OrdersDB();
				int orderId = orderSystem.PlaceOrder(customerId, customerId,
													 // Use default shipper for instant ordering
													 1,
													 isExtendedProcessing ?
													 Constants.Status.
													 PendingInventoryCheck :
													 Constants.Status.Shipped,
													 addressID,
													 //Use the same address for billing and shipping
													 //for API simplicity reasons, and security.
													 addressID);
																
				

				// Demonstrate service broker features if ExtendedOrderProcessing is true in web.config file.
				if (isExtendedProcessing)
				{
					ExtendedOrderProcessing eop = new ExtendedOrderProcessing();

					eop.PostSalesOrder(orderId, customerId, orderSystem);
				}

				
            
				// Return OrderDetails
				return orderSystem.GetOrderDetails(orderId, customerId);
			}
			else 
			{
				// invalid input 
				return null;
			}
		}
     
		//*******************************************************
		//
		// InstantOrder.CheckStatus() Method 
		//
		// The CheckStatus method enables a remote client to programmatically
		// query the current status of an order in the AdventureWorks System. 
		//
		//*******************************************************  
     
		[WebMethod(Description="The CheckStatus method enables a remote client to programmatically query the current status of an order in the AdventureWorks System.", EnableSession=false)]
		public OrderDetails CheckStatus(string userName, string password, int orderID) 
		{
     
			// Login client using provided username and password
			CustomersDB accountSystem = new CustomersDB();
			CustomerDetails customerDetails = accountSystem.Login(userName, password);
			String customerId = customerDetails.CustomerID;
        
			if (customerId == null) 
			{
				throw new Exception("Error: Invalid Login!");
			}
        
			// Return OrderDetails Status for Specified Order
			OrdersDB orderSystem = new OrdersDB();
			return orderSystem.GetOrderDetails(orderID, customerId);
		}
	}
}