public async Task <string> SetAppointment(NewAppointment_ViewModel newAppointmentModel, OrderAppointment[] orders, string userName)
        {
            return(await Repository.SetAppointment(newAppointmentModel, orders, userName));

            // Task<string> SetAppointment(NewAppointment_ViewModel newAppointmentModel, OrderAppointment[] orders, string userName)
        }
Example #2
0
        public async Task <string> SetAppointment(NewAppointment_ViewModel newAppointmentModel, Model.OrderAppointment[] orders, string userName)
        {
            using (System.Data.Entity.DbContextTransaction dbTran = context.Database.BeginTransaction())
            {
                try
                {
                    foreach (var order in orders)
                    {
                        Model.Appointment appointmentModel = new Model.Appointment();
                        appointmentModel.CustomerId = order.CustomerId;
                        appointmentModel.PickTicket = order.PickTicketId;
                        appointmentModel.DivisionId = order.DivisionId == 0 ? default(int?) : order.DivisionId;

                        appointmentModel.ScacCode          = newAppointmentModel.ScacCode;
                        appointmentModel.ShippingDate      = newAppointmentModel.ShippingDate;
                        appointmentModel.ShippingTime      = new DateTime(newAppointmentModel.ShippingDate.Year, newAppointmentModel.ShippingDate.Month, newAppointmentModel.ShippingDate.Day, newAppointmentModel.ShippingTime.Hour, newAppointmentModel.ShippingTime.Minute, 0);
                        appointmentModel.AppointmentNumber = newAppointmentModel.AppointmentNumber;
                        appointmentModel.DeliveryTypeId    = newAppointmentModel.DeliveryTypeId;
                        appointmentModel.UserName          = userName;
                        appointmentModel.Pallets           = newAppointmentModel.Pallets;
                        appointmentModel.DriverId          = newAppointmentModel.DriverId;
                        appointmentModel.TruckId           = newAppointmentModel.TruckId;

                        if (newAppointmentModel.ShippingTimeLimit.HasValue)
                        {
                            appointmentModel.ShippingTimeLimit = new DateTime(newAppointmentModel.ShippingDate.Year, newAppointmentModel.ShippingDate.Month, newAppointmentModel.ShippingDate.Day, newAppointmentModel.ShippingTimeLimit.Value.Hour, newAppointmentModel.ShippingTimeLimit.Value.Minute, 0);
                        }

                        appointmentModel.PtBulk = order.PtBulk;


                        Model.OrderAppointment oappt = new Model.OrderAppointment()
                        {
                            PurchaseOrderId = order.PurchaseOrderId,
                            PickTicketId    = order.PickTicketId,
                            PtBulk          = order.PtBulk,
                            CustomerId      = order.CustomerId,
                            ShipFor         = newAppointmentModel.ShippingDate,
                            Notes           = newAppointmentModel.Notes,
                            Status          = 1
                        };

                        var existingAppt = context.Appointments.Where(x => x.CustomerId == appointmentModel.CustomerId && x.PickTicketId == appointmentModel.PickTicket).FirstOrDefault();

                        if (existingAppt != null)
                        {
                            if (existingAppt.Status == "D")
                            {
                                // update the existing one, could be a previous cancelation
                                context.Appointments.Remove(existingAppt);
                            }
                            else
                            {
                                throw new ArgumentException($"Order with picticket {appointmentModel.PickTicket} for client {appointmentModel.CustomerId} already have an appointment {appointmentModel.AppointmentNumber}");
                            }
                        }


                        Entities.Appointment appointment = new Entities.Appointment();
                        appointment.CustomerId   = appointmentModel.CustomerId;
                        appointment.DateAdd      = DateTime.Now;
                        appointment.PickTicketId = appointmentModel.PickTicket;
                        appointment.DivisionId   = appointmentModel.DivisionId;

                        appointment.ScacCode = newAppointmentModel.ScacCode;
                        // appointment.ShipDate = appointmentModel.ShippingDate.Value;
                        appointment.ShipTime          = new DateTime(appointmentModel.ShippingDate.Value.Year, appointmentModel.ShippingDate.Value.Month, appointmentModel.ShippingDate.Value.Day, appointmentModel.ShippingTime.Value.Hour, appointmentModel.ShippingTime.Value.Minute, 0);
                        appointment.AppointmentNumber = appointmentModel.AppointmentNumber;
                        appointment.DeliveryTypeId    = appointmentModel.DeliveryTypeId.Value;
                        appointment.UserName          = userName;
                        appointment.Pallets           = appointmentModel.Pallets;
                        appointment.DriverId          = appointmentModel.DriverId;
                        appointment.TruckId           = appointmentModel.TruckId;

                        if (newAppointmentModel.ShippingTimeLimit.HasValue)
                        {
                            appointment.ShippingTimeLimit = new DateTime(appointmentModel.ShippingDate.Value.Year, appointmentModel.ShippingDate.Value.Month, appointmentModel.ShippingDate.Value.Day, appointmentModel.ShippingTimeLimit.Value.Hour, appointmentModel.ShippingTimeLimit.Value.Minute, 0);
                        }

                        appointment.PtBulk = string.Empty;
                        if (!string.IsNullOrEmpty(appointmentModel.PtBulk))
                        {
                            appointment.PtBulk       = appointmentModel.PtBulk;
                            appointment.PickTicketId = appointmentModel.PtBulk;
                        }

                        context.Appointments.Add(appointment);


                        var ptBulk = string.IsNullOrEmpty(appointmentModel.PtBulk) ? "" : appointmentModel.PtBulk;


                        var entity = await context.OrderAppointments.Where(x => x.PurchaseOrderId == order.PurchaseOrderId && x.PickTicketId == order.PickTicketId && x.CustomerId == order.CustomerId && x.PtBulk == ptBulk).FirstOrDefaultAsync();

                        if (entity != null)
                        {
                            entity.Status  = 1;
                            entity.ShipFor = appointmentModel.ShippingDate.Value;
                            entity.Notes   = !string.IsNullOrEmpty(entity.Notes) ? $"{entity.Notes} / {order.Notes}" : order.Notes;
                        }

                        await context.SaveChangesAsync();
                    }


                    dbTran.Commit();

                    return(null);
                }
                catch (Exception exc)
                {
                    dbTran.Rollback();

                    return($"{exc.Message} : { exc.InnerException?.Message} ");
                }
            }
        }