public async Task <IHttpActionResult> Createcustomer_order([FromBody] CustomerOrderProductViewModel customer_order) { ResponseDataDTO <customer_order> response = new ResponseDataDTO <customer_order>(); try { var c = customer_order; //Id user now new BaseController(); var current_id = BaseController.current_id; if (c.customer.cu_id == 0) { #region [Create Customer] //Cach truong bat buoc if (c.customer.cu_fullname == null) { response.Code = HttpCode.INTERNAL_SERVER_ERROR; response.Message = "Họ và tên không được để trống"; response.Data = null; return(Ok(response)); } if (c.customer.cu_mobile == null) { response.Code = HttpCode.INTERNAL_SERVER_ERROR; response.Message = "Số điện thoại không được để trống"; response.Data = null; return(Ok(response)); } if (c.customer.cu_email == null) { response.Code = HttpCode.INTERNAL_SERVER_ERROR; response.Message = "Email không được để trống"; response.Data = null; return(Ok(response)); } if (c.customer.cu_type == 0) { response.Code = HttpCode.INTERNAL_SERVER_ERROR; response.Message = "Loại khách hàng không được để trống"; response.Data = null; return(Ok(response)); } if (c.customer.customer_group_id == 0) { response.Code = HttpCode.INTERNAL_SERVER_ERROR; response.Message = "Nhóm khách hàng không được để trống"; response.Data = null; return(Ok(response)); } if (c.customer.source_id == 0) { response.Code = HttpCode.INTERNAL_SERVER_ERROR; response.Message = "Nguồn không được để trống"; response.Data = null; return(Ok(response)); } // get data from formdata CustomerCreateViewModel customerCreateViewModel = new CustomerCreateViewModel { cu_mobile = Convert.ToString(c.customer.cu_mobile), cu_email = Convert.ToString(c.customer.cu_email), cu_fullname = Convert.ToString(c.customer.cu_fullname), customer_group_id = Convert.ToInt32(c.customer.customer_group_id), source_id = Convert.ToInt32(c.customer.source_id), cu_type = Convert.ToByte(c.customer.cu_type), }; //Bat cac dieu kien rang buoc if (CheckEmail.IsValidEmail(customerCreateViewModel.cu_email) == false && customerCreateViewModel.cu_email == "") { response.Message = "Định dạng email không hợp lệ !"; response.Data = null; return(Ok(response)); } if (CheckNumber.IsPhoneNumber(customerCreateViewModel.cu_mobile) == false && customerCreateViewModel.cu_mobile == "") { response.Message = "Số điện thoại không hợp lệ"; response.Data = null; return(Ok(response)); } //bat cac truog con lai if (c.customer.cu_birthday == null) { customerCreateViewModel.cu_birthday = null; } else { customerCreateViewModel.cu_birthday = Convert.ToDateTime(c.customer.cu_birthday); } if (c.customer.cu_address == null) { customerCreateViewModel.cu_address = null; } else { customerCreateViewModel.cu_address = Convert.ToString(c.customer.cu_address); } if (c.customer.cu_note == null) { customerCreateViewModel.cu_note = null; } else { customerCreateViewModel.cu_note = Convert.ToString(c.customer.cu_note); } if (c.customer.cu_geocoding == null) { customerCreateViewModel.cu_geocoding = null; } else { customerCreateViewModel.cu_geocoding = Convert.ToString(c.customer.cu_geocoding); } if (c.customer.cu_curator_id == 0) { customerCreateViewModel.cu_curator_id = null; } else { customerCreateViewModel.cu_curator_id = Convert.ToInt32(c.customer.cu_curator_id); } if (c.customer.cu_age == 0) { customerCreateViewModel.cu_age = null; } else { customerCreateViewModel.cu_age = Convert.ToInt32(c.customer.cu_age); } if (c.customer.cu_status == 0) { customerCreateViewModel.cu_status = null; } else { customerCreateViewModel.cu_status = Convert.ToByte(c.customer.cu_status); } customerCreateViewModel.staff_id = Convert.ToInt32(current_id); customerCreateViewModel.cu_create_date = DateTime.Now; var cu = _customerservice.GetLast(); customerCreateViewModel.cu_code = Utilis.CreateCode("CU", cu.cu_id, 7); // mapping view model to entity var createdcustomer = _mapper.Map <customer>(customerCreateViewModel); // save new customer _customerservice.Create(createdcustomer); var cu_last = _customerservice.GetLast(); c.customer.cu_id = cu_last.cu_id; #endregion } // get data from formdata CustomerOrderCreateViewModel customer_orderCreateViewModel = new CustomerOrderCreateViewModel { }; customer_orderCreateViewModel.customer_id = c.customer.cu_id; customer_orderCreateViewModel.staff_id = Convert.ToInt32(current_id); customer_orderCreateViewModel.cuo_payment_status = c.cuo_payment_status; customer_orderCreateViewModel.cuo_payment_type = c.cuo_payment_type; customer_orderCreateViewModel.cuo_ship_tax = c.cuo_ship_tax; customer_orderCreateViewModel.cuo_total_price = c.cuo_total_price; customer_orderCreateViewModel.cuo_discount = c.cuo_discount; customer_orderCreateViewModel.cuo_status = c.cuo_status; customer_orderCreateViewModel.cuo_date = DateTime.Now; // mapping view model to entity var createdcustomer_order = _mapper.Map <customer_order>(customer_orderCreateViewModel); // save new customer_order _customer_orderservice.Create(createdcustomer_order); //create order product OrderProductCreateViewModel orderCreateViewModel = new OrderProductCreateViewModel { }; var op_last = _customer_orderservice.GetLast(); foreach (productorderviewmodel i in c.list_product) { orderCreateViewModel.customer_order_id = op_last.cuo_id; orderCreateViewModel.op_discount = i.op_discount; orderCreateViewModel.op_note = i.op_note; orderCreateViewModel.op_quantity = i.op_quantity; orderCreateViewModel.product_id = i.product_id; var createdorderproduct = _mapper.Map <order_product>(orderCreateViewModel); _order_productservice.Create(createdorderproduct); } // return response response.Code = HttpCode.OK; response.Message = MessageResponse.SUCCESS; response.Data = null; return(Ok(response)); } catch (Exception ex) { response.Code = HttpCode.INTERNAL_SERVER_ERROR; response.Message = ex.Message; response.Data = null; Console.WriteLine(ex.ToString()); return(Ok(response)); } }