Beispiel #1
0
        public messageModel save_draft(bookCarDetailModel value)
        {
            messageModel result = new messageModel();

            try
            {
                System.Data.Entity.Core.Objects.ObjectParameter myOutputParamInt = new System.Data.Entity.Core.Objects.ObjectParameter("r_id", typeof(Int32));
                using (var context = new StandardCanEntities())
                {
                    if (String.IsNullOrEmpty(value.user_id))
                    {
                        throw new Exception("Unauthorized Access");
                    }
                    var userId = JwtHelper.GetUserIdFromToken(value.user_id);
                    if (String.IsNullOrEmpty(userId))
                    {
                        throw new Exception("Unauthorized Access");
                    }

                    string save_type = "1"; // save_draft
                    if (value.method.Equals("save_send"))
                    {
                        save_type = "2";
                        if (!String.IsNullOrEmpty(value.start_date))
                        {
                            var startDate = DateTime.ParseExact(value.start_date + " " + value.stop_time, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
                            var bfDateNow = DateTime.Now.AddDays(2);
                            if (bfDateNow >= startDate)
                            {
                                throw new Exception("Please book at least 2 days");
                            }
                        }
                    }
                    else if (value.method.Equals("save_update"))
                    {
                        save_type = "";
                        if (!String.IsNullOrEmpty(value.start_date))
                        {
                            var startDate = DateTime.ParseExact(value.start_date + " " + value.stop_time, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
                            var bfDateNow = DateTime.Now.AddDays(2);
                            if (bfDateNow >= startDate)
                            {
                                throw new Exception("Please book at least 2 days");
                            }
                        }
                    }

                    JavaScriptSerializer js = new JavaScriptSerializer();
                    string json             = js.Serialize(value);

                    context.interface_log.Add(new interface_log
                    {
                        ID          = 1,
                        data_log    = json,
                        module      = "book_car_save_draft",
                        update_date = DateTime.Now
                    });
                    context.SaveChanges();

                    int ret = context.sp_bookcar_save(value.id, value.topic, value.start_date, value.start_time, value.stop_date, value.stop_time, value.person_total, value.car_type_id, value.car_id, value.reason_id, value.dest_id, value.remark, save_type, userId, value.bc_request, myOutputParamInt);

                    if (myOutputParamInt.Value != null)
                    {
                        int r_id = Convert.ToInt32(myOutputParamInt.Value);
                        value.id = r_id.ToString();
                    }


                    context.sp_bookcar_delete_emp(value.id);
                    if (value.emp_list != null)
                    {
                        foreach (var item in value.emp_list)
                        {
                            context.sp_bookcar_insert_emp(value.id, item.emp_code);
                        }
                    }
                }
                result.status  = "S";
                result.message = "";
                result.value   = value.id;
            }
            catch (Exception ex)
            {
                result.status  = "E";
                result.message = ex.Message.ToString();
            }

            return(result);
        }