public async Task <IActionResult> SendOrderToSupplier(int order_id) { var hasOrder = await _context.Orders.AnyAsync(o => o.Id == order_id); if (!hasOrder) { return(BadRequest("order " + order_id + " not exists! ")); } var dealerId = _config["DealerId"]; var apiUrl = _config["ApiUrl"]; var siteName = _config["SiteName"]; var sales_note = _context.Orders.Where(o => o.Id == order_id).FirstOrDefault().SalesNote; var shipping_method = _context.Orders.Where(o => o.Id == order_id).FirstOrDefault().ShippingMethod; var freight = _context.Orders.Where(o => o.Id == order_id).FirstOrDefault().Freight; var shippingInfo = _context.ShippingInfo.Where(o => o.orderId == order_id).FirstOrDefault(); var shppingInfoDto = new ShippingInfoDto(); if (shippingInfo != null) { shppingInfoDto = new ShippingInfoDto() { id = shippingInfo.id, sender = shippingInfo.sender, sender_phone = shippingInfo.sender_phone, sender_address = shippingInfo.sender_address, sender_city = shippingInfo.sender_city, sender_country = shippingInfo.sender_country, orderId = shippingInfo.orderId, note = shippingInfo.note, receiver = shippingInfo.receiver, receiver_address1 = shippingInfo.receiver_address1, receiver_address2 = shippingInfo.receiver_address2, receiver_address3 = shippingInfo.receiver_address3, receiver_city = shippingInfo.receiver_city, receiver_company = shippingInfo.receiver_company, receiver_contact = shippingInfo.receiver_contact, receiver_country = shippingInfo.receiver_country, receiver_phone = shippingInfo.receiver_phone, receiver_zip = shippingInfo.zip, oversea = false }; } else { shppingInfoDto = null; } var orderItems = _context.OrderItem.Where(oi => oi.Id == order_id) .Select(i => new CartItemDto { code = i.Code.ToString(), quantity = i.Quantity.ToString(), barcode = i.Barcode, name = i.ItemName, id = i.Id, note = i.Note, supplier_code = i.SupplierCode }).ToList(); var newCreateOrderByDealerId = new CreateOrderByDealerIdDto() { freight = freight, sales_note = sales_note, shipping_method = shipping_method, ShippingInfo = shppingInfoDto, cartItems = orderItems }; try { using (var client = new HttpClient()) { client.BaseAddress = new Uri(apiUrl); //check if this order has been sent already! var responseTask = client.GetAsync("/" + siteName + "/api/dealer/order/po/eCom_Managment_" + order_id); responseTask.Wait(); var final = responseTask.Result; if (final.IsSuccessStatusCode) { var readTask = final.Content.ReadAsAsync <bool>(); readTask.Wait(); var myfinal = readTask.Result; if (myfinal) { return(BadRequest("order exists already!")); } } var content = newCreateOrderByDealerId; var postTask = client.PostAsJsonAsync <CreateOrderByDealerIdDto>("/" + siteName + "/api/dealer/order/createOrderByDealerId/" + dealerId + "/" + order_id, content); postTask.Wait(); var reault = postTask.Result; if (reault.IsSuccessStatusCode) { return(Ok("order sent!")); } else { return(BadRequest("something wrong!")); } } } catch (Exception ex) { return(BadRequest(ex.ToString())); } }
public async Task <IActionResult> CreateOrderByDealerIdDto(int dealerId, string orderId, [FromBody] CreateOrderByDealerIdDto createOrderByDealerId) { if (!ModelState.IsValid) { string errorString = ""; foreach (var modelState in ModelState.Values) { foreach (ModelError error in modelState.Errors) { errorString += error + "\r\n"; } } return(BadRequest(errorString)); } var dealerExists = await _context.Card.AnyAsync(c => c.Id == dealerId); if (!dealerExists) { return(NotFound("Dealer not exists!")); } var orderExists = await _context.Orders.AnyAsync(o => o.PoNumber == orderId); if (orderExists) { return(NotFound("Order exists!")); } // get dealer price level var priceLevel = _context.Card.Where(c => c.Id == dealerId).FirstOrDefault().PriceLevel; if (priceLevel == 0) { priceLevel = 1; } double totalWeight = 0; decimal totalAmount = 0; var itemsInCart = new List <CartItemDto>(); foreach (var i in createOrderByDealerId.cartItems) { var item = new CartItemDto(); item.sales_price = _iitem.getLevelPrice(int.Parse(i.code), priceLevel).ToString(); //get levelprice item.quantity = i.quantity; item.total = Math.Round(Convert.ToDouble(item.sales_price) * Convert.ToDouble(item.quantity), 2); //get total for levelprice item.barcode = i.barcode; item.name = i.name; item.id = i.id; item.code = i.code; item.inner_pack = i.inner_pack; item.outer_pack = i.outer_pack; item.moq = i.moq; item.note = i.note; item.supplier_code = i.supplier_code; item.weight = i.weight; item.total_points = i.total_points; itemsInCart.Add(item); totalWeight += Convert.ToDouble(item.weight); totalAmount += Convert.ToDecimal(item.total); } //create invoice var inoviceInfo = new object(); var branch_id = 1; var newOrder = new Orders(); newOrder.CardId = dealerId; newOrder.PoNumber = "eCom_Managment_" + orderId; newOrder.Branch = branch_id; newOrder.Freight = (decimal)createOrderByDealerId.freight; newOrder.OrderTotal = totalAmount; newOrder.ShippingMethod = (byte)createOrderByDealerId.shipping_method; newOrder.CustomerGst = createOrderByDealerId.customer_gst; newOrder.IsWebOrder = true; newOrder.WebOrderStatus = 1; newOrder.Status = 1; newOrder.Number = newOrder.Id; newOrder.SalesNote = createOrderByDealerId.sales_note; using (var dbContextTransaction = _context.Database.BeginTransaction()) { try { await _context.Orders.AddAsync(newOrder); await _context.SaveChangesAsync(); var newOrderId = newOrder.Id; var customerGst = newOrder.CustomerGst; var totalGstInc = Math.Round(totalAmount * (1 + (decimal)customerGst), 2); //create order await inputOrderItem(itemsInCart, newOrderId, customerGst); //input shopping info await inputShippingInfo(createOrderByDealerId.ShippingInfo, newOrderId); await _context.SaveChangesAsync(); dbContextTransaction.Commit(); string invoiceNumber = null; return(Ok(new { newOrderId, invoiceNumber, totalGstInc })); // return Ok(); } catch (Exception ex) { dbContextTransaction.Rollback(); return(BadRequest(ex.ToString())); } finally { NLog.LogManager.Shutdown(); } } }