Example #1
0
 public static void SysInit()
 {
     var dc = new VehicleDataContext();
     try
     {
         var l = dc.OrderHeaders.Where(h => h.OrderDetails.Count() == 0 && !string.IsNullOrEmpty(h.OrderNumber));
         var p = dc.SaleOrderPayments.Where(h => h.OrderHeader.OrderDetails.Count() == 0 && !string.IsNullOrEmpty(h.OrderHeader.OrderNumber));
         dc.SaleOrderPayments.DeleteAllOnSubmit(p);
         dc.OrderHeaders.DeleteAllOnSubmit(l);
         dc.SubmitChanges();
     }
     catch { }
     finally
     {
         dc.Dispose();
     }
 }
        public bool DoCloseAll(DateTime defaultCloseDate)
        {
            if (this.Closing) return false;
            this.Closing = true;
            VehicleDataContext vdc = new VehicleDataContext();
            PartDataContext pdc = new PartDataContext();

            DateTime crrMonth = DataFormat.DateOfFirstDayInMonth(DateTime.Now);
            LogMessage(string.Format("Starting Close {0} ----------", this.ForceClose ? ", forced by user" : "Automaticaly"));
            foreach (var d in pdc.Dealers)
            {
                bool hasW = false;
                LogMessage(string.Format("Closing dealer {0}: ", d.DealerCode));
                try
                {
                    // close warehouses
                    foreach (var w in d.ActiveWarehouses.Where(w => w.Type == VDMS.II.Entity.WarehouseType.Vehicle))
                    {
                        LogBeginMessage(string.Format("     Close warehouse {0}: ", w.Code));
                        try
                        {
                            hasW = true;
                            SaleInventoryLock wlck = InventoryHelper.GetInventoryLock(w.DealerCode, w.Code);
                            if (wlck == null)
                            {
                                InventoryHelper.DoCloseW(w.Code, w.DealerCode, defaultCloseDate.Month, defaultCloseDate.Year, vdc);
                            }
                            else
                            {
                                DateTime lastWLock = new DateTime((int)wlck.Year, (int)wlck.Month, 1);
                                lastWLock = lastWLock.AddMonths(1);
                                while (lastWLock < crrMonth)
                                {
                                    InventoryHelper.DoCloseW(w.Code, w.DealerCode, lastWLock.Month, lastWLock.Year, vdc);
                                    lastWLock = lastWLock.AddMonths(1);
                                }
                            }
                            LogEndMessage("Done!");
                        }
                        catch (Exception ex)
                        {
                            LogEndMessage(string.Format("Failed: {0}", DataFormat.TraceExceptionMessage(ex)));
                        }
                    }
                    // close Dealers
                    if (hasW)
                    {
                        SaleInventoryLock dlck = InventoryHelper.GetInventoryLock(d.DealerCode, 0);
                        if (dlck == null)
                        {
                            InventoryHelper.DoCloseD(d.DealerCode, defaultCloseDate.Month, defaultCloseDate.Year, vdc);
                        }
                        else
                        {
                            DateTime lastDLock = new DateTime((int)dlck.Year, (int)dlck.Month, 1);
                            lastDLock = lastDLock.AddMonths(1);
                            while (lastDLock < crrMonth)
                            {
                                InventoryHelper.DoCloseD(d.DealerCode, lastDLock.Month, lastDLock.Year, vdc);
                                lastDLock = lastDLock.AddMonths(1);
                            }
                        }
                        LogMessage(string.Format("Close dealer {0} done!", d.DealerCode));
                    }
                    else
                    {
                        LogMessage(string.Format("{0} has no warehouses!", d.DealerCode));
                    }
                }
                catch (Exception ex)
                {
                    LogMessage(string.Format("Close dealer {0} failed: {1}", d.DealerCode, DataFormat.TraceExceptionMessage(ex)));
                }
                LogEndMessage(" ");
            }
            LogMessage("AutoClose finished ----------");
            LogEndMessage(" ");

            vdc.Dispose();
            pdc.Dispose();
            this.Closing = false;
            return true;
        }