Exemplo n.º 1
0
 /// <summary>
 /// Create a new recsys_routine object.
 /// </summary>
 /// <param name="id">Initial value of the id property.</param>
 public static recsys_routine Createrecsys_routine(global::System.Int32 id)
 {
     recsys_routine recsys_routine = new recsys_routine();
     recsys_routine.id = id;
     return recsys_routine;
 }
        private bool TryGenerateOrdersForMaintenance(recsys_maintenance maintenance, recsys_routine routine, out IEnumerable<recsys_order> generatedOrders)
        {
            recsys_relate_customers masterCustomerData;
            Member member;
            List<recsys_relate_customers> customerDatas;
            List<recsys_order> orders;
            DateTime? startCalculationDate = null;
            DateTime? endCalculationDate = null;
            HolidaysController holidayController;
            List<DateTime> holidays;
            IEnumerable<DateTime> orderRepairDates;
            bool isSuccessful = false;

            generatedOrders = new List<recsys_order>();

            //# validation
            if (routine == null)
                return false;
            if (maintenance == null)
                return true;
            if (!routine.repair_date_frequency_in_month.HasValue)
                return true;
            if (!maintenance.start_date.HasValue && !maintenance.confirm_date.HasValue)
                return true;        //# start date of the calculation is counting on either the start date or confirm date; if null is given, just return true to indicate no order is generated
            if (!maintenance.end_date.HasValue)
                return true;        //# if maintenace does not have an end date, do not do anything

            //startCalculationDate = maintenance.confirm_date.HasValue ? maintenance.confirm_date.Value : maintenance.start_date;
            startCalculationDate = maintenance.start_date;
            endCalculationDate = maintenance.end_date;

            holidayController = new HolidaysController();
            holidays = holidayController.GetHolidays(startCalculationDate.Value, endCalculationDate.Value, true);

            orderRepairDates = maintenance.GetOrderRepairDatesByRoutine(routine, holidays);

            //# get old master customer record
            var _masterCustomerData = from m in this._db.recsys_maintenance
                                      join cd in this._db.recsys_relate_customers on m.customer_id equals cd.id into cds
                                      from cd in cds.DefaultIfEmpty()
                                      where m.id == maintenance.id
                                      select new
                                      {
                                          cd
                                      };
            masterCustomerData = _masterCustomerData.Select(theRecord => theRecord.cd).FirstOrDefault();

            //# validation
            if (masterCustomerData == null)
                throw new RecordNotFoundException();

            member = new Member("users");
            customerDatas = new List<recsys_relate_customers>();

            //# customer data
            for (int i = 0; i < orderRepairDates.Count(); i++)
            {
                //# mapping
                recsys_relate_customers customerData = new recsys_relate_customers()
                {
                    address1 = masterCustomerData.address1,
                    address2 = masterCustomerData.address2,
                    center = masterCustomerData.center,
                    code = masterCustomerData.code,
                    contact = masterCustomerData.contact,
                    customer_code = masterCustomerData.customer_code,
                    customer_id = masterCustomerData.customer_id,
                    district1 = masterCustomerData.district1,
                    district2 = masterCustomerData.district2,
                    email = masterCustomerData.email,
                    fax1 = masterCustomerData.fax1,
                    fax2 = masterCustomerData.fax2,
                    group_id = masterCustomerData.group_id,
                    manual_input_code = masterCustomerData.manual_input_code,
                    name = masterCustomerData.name,
                    chi_name = masterCustomerData.chi_name,
                    prefix = masterCustomerData.prefix,
                    relate_type = (int)Rec_System.Models.Customers.CustomerDataRelateType.Quotation,
                    reference_number = masterCustomerData.reference_number,
                    remark = masterCustomerData.remark,
                    tel1 = masterCustomerData.tel1,
                    tel2 = masterCustomerData.tel2,
                    title = (byte)masterCustomerData.title,
                    type = (byte)masterCustomerData.type
                };

                customerDatas.Add(customerData);

                this._db.recsys_relate_customers.AddObject(customerData);
            }

            try
            {
                this._db.SaveChanges();

                this._db.Refresh(System.Data.Objects.RefreshMode.StoreWins, customerDatas);

                orders = new List<recsys_order>();

                for (int j = 0; j < customerDatas.Count; j++)
                {
                    recsys_relate_customers theCustomerData = customerDatas[j];

                    recsys_order order = new recsys_order()
                    {
                        by_system = 1,
                        create_date = DateTime.Now,
                        model = maintenance.model,
                        repair_date = orderRepairDates.ElementAt(j),
                        customer_id = theCustomerData.id,
                        fault = "[" + OrderStatus.AppointedWarranty.Name + "]",            //# logic
                        last_update = DateTime.Now,
                        status = (byte) RecordStatus.Active,
                        update_user_id = (int)member.infoBySession("id"),
                        start_time = null,
                        end_time = null,
                        order_status = OrderStatus.AppointedWarranty.ID,
                        call_pickup_user_id = (int) member.infoBySession("id")
                    };

                    orders.Add(order);

                    this._db.recsys_order.AddObject(order);
                }

                this._db.SaveChanges();

                this._db.Refresh(System.Data.Objects.RefreshMode.StoreWins, orders);

                foreach (recsys_order theOrder in orders)
                {
                    recsys_relate relation = new recsys_relate()
                    {
                        id1 = maintenance.id,
                        id2 = theOrder.id,
                        table1 = "maintenance",
                        table2 = "order"
                    };

                    this._db.recsys_relate.AddObject(relation);
                }

                this._db.SaveChanges();

                generatedOrders = orders;

                isSuccessful = true;

            }
            catch(OptimisticConcurrencyException)
            {
                //# log down
            }

            return isSuccessful;
        }
Exemplo n.º 3
0
 /// <summary>
 /// Deprecated Method for adding a new object to the recsys_routine EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddTorecsys_routine(recsys_routine recsys_routine)
 {
     base.AddObject("recsys_routine", recsys_routine);
 }