Esempio n. 1
0
        public IList SearchActivePartByBarcode(string barcode, bool active)
        {
            try
            {
                OpenConnection();
                MySql.Data.MySqlClient.MySqlCommand aCommand = new MySql.Data.MySqlClient.MySqlCommand();
                aCommand.Connection = m_connection;
                MySql.Data.MySqlClient.MySqlDataReader aReader;

                aCommand.CommandText = UnitConversion.GetAllByBarcodeSQL(barcode);
                aReader = aCommand.ExecuteReader();
                IList unv = UnitConversion.GetAllStatic(aReader);
                aReader.Close();
                IList result = new ArrayList();
                foreach (UnitConversion c in unv)
                {
                    c.PART                      = PartRepository.GetByID(aCommand, c.PART.ID);
                    c.CONVERSION_UNIT           = PartRepository.GetUnitByID(aCommand, c.CONVERSION_UNIT.ID);
                    c.PART.UNIT_BY_SEARCH       = c.CONVERSION_UNIT;
                    c.PART.SELL_PRICE_BY_SEARCH = c.SELL_PRICE;
                    c.PART.COST_PRICE_BY_SEARCH = c.COST_PRICE;
                    result.Add(c.PART);
                }
                return(result);
            }
            catch (Exception x)
            {
                throw new Exception(getErrorMessage(x));
            }
            finally
            {
                m_connection.Close();
            }
        }
        protected override Event doGet(int ID)
        {
            m_command.CommandText = CustomerInvoice.GetByIDSQL(ID);
            MySql.Data.MySqlClient.MySqlDataReader r = m_command.ExecuteReader();
            CustomerInvoice st = CustomerInvoice.TransformReader(r);

            r.Close();
            m_command.CommandText = CustomerInvoiceItem.GetByEventIDSQL(ID);
            r = m_command.ExecuteReader();
            IList stis = CustomerInvoiceItem.TransformReaderList(r);

            r.Close();
            foreach (CustomerInvoiceItem sti in stis)
            {
                sti.EVENT            = st;
                sti.PART             = PartRepository.GetByID(m_command, sti.PART.ID);
                sti.STOCK_CARD_ENTRY = StockCardEntryRepository.FindStockCardEntryByEventItem(m_command, sti.ID, sti.STOCK_CARD_ENTRY_TYPE);
                if (sti.DO_ITEM.ID > 0)
                {
                    sti.DO_ITEM = DeliveryOrderRepository.FindDeliveryOrderItem(m_command, sti.DO_ITEM.ID);
                }
                st.EVENT_ITEMS.Add(sti);
            }
            return(st);
        }
Esempio n. 3
0
        //For transaction
        public static Part GetByID(MySql.Data.MySqlClient.MySqlCommand cmd, int id)
        {
            cmd.CommandText = Part.GetByIDSQLStatic(id);
            MySql.Data.MySqlClient.MySqlDataReader r = cmd.ExecuteReader();
            Part result = Part.GetPart(r);

            r.Close();
            result.UNIT_CONVERSION_LIST = PartRepository.GetUnitConversionsStatic(cmd, id);
            return(result);
        }
Esempio n. 4
0
        protected override void doUpdate(Event en)
        {
            MySql.Data.MySqlClient.MySqlTransaction trc = m_connection.BeginTransaction();
            m_command.Transaction = trc;
            try
            {
                PurchaseOrder e = (PurchaseOrder)en;
                m_command.CommandText = e.GetUpdateSQL();
                m_command.ExecuteNonQuery();

                foreach (PurchaseOrderItem sti in e.EVENT_ITEMS)
                {
                    sti.PART.UNIT_CONVERSION_LIST = PartRepository.GetUnitConversionsStatic(m_command, sti.PART.ID);
                    if (sti.ID > 0)
                    {
                        m_command.CommandText = sti.GetUpdateSQL();
                        m_command.ExecuteNonQuery();
                    }
                    else
                    {
                        m_command.CommandText = sti.GetInsertSQL();
                        m_command.ExecuteNonQuery();
                        m_command.CommandText = PurchaseOrderItem.SelectMaxIDSQL();
                        sti.ID = Convert.ToInt32(m_command.ExecuteScalar());
                    }
                }
                m_command.CommandText = PurchaseOrderItem.DeleteUpdate(e.ID, e.EVENT_ITEMS);
                m_command.ExecuteNonQuery();
                //m_command.CommandText = PurchaseOrderItem.GetByEventIDSQL(e.ID);
                //MySql.Data.MySqlClient.MySqlDataReader r = m_command.ExecuteReader();
                //IList luc = PurchaseOrderItem.TransformReaderList(r);
                //r.Close();
                //foreach (PurchaseOrderItem chk in luc)
                //{
                //    chk.UPDATED = e.EVENT_ITEMS.Contains(chk);
                //}
                //foreach (PurchaseOrderItem chk in luc)
                //{
                //    if (!chk.UPDATED)
                //    {
                //        m_command.CommandText = PurchaseOrderItem.DeleteSQL(chk.ID);
                //        m_command.ExecuteNonQuery();
                //    }
                //}
                trc.Commit();
            }
            catch (Exception x)
            {
                trc.Rollback();
                throw x;
            }
        }
Esempio n. 5
0
        public static SalesReturn GetSalesReturnForCreditNote(MySql.Data.MySqlClient.MySqlCommand cmd, SalesReturn p)
        {
            cmd.CommandText = SalesReturnItem.GetByEventIDSQL(p.ID);
            MySql.Data.MySqlClient.MySqlDataReader r = cmd.ExecuteReader();
            p.EVENT_ITEMS = SalesReturnItem.TransformReaderList(r);
            r.Close();

            foreach (SalesReturnItem t in p.EVENT_ITEMS)
            {
                if ((t.DO_ITEM == null) && (t.DO_ITEM.ID == 0))
                {
                    continue;
                }


                cmd.CommandText = DeliveryOrderItem.GetByIDSQL(t.DO_ITEM.ID);
                r         = cmd.ExecuteReader();
                t.DO_ITEM = DeliveryOrderItem.TransformReader(r);
                r.Close();

                if ((t.DO_ITEM.SO_ITEM == null))
                {
                    continue;
                }
                if (t.DO_ITEM.SO_ITEM.ID == 0)
                {
                    continue;
                }

                cmd.CommandText = SalesOrderItem.GetByIDSQL(t.DO_ITEM.SO_ITEM.ID);
                r = cmd.ExecuteReader();
                t.DO_ITEM.SO_ITEM = SalesOrderItem.TransformReader(r);
                r.Close();

                t.DO_ITEM.PART = PartRepository.GetByID(cmd, t.DO_ITEM.PART.ID);
                t.DO_ITEM.PART.UNIT_CONVERSION_LIST = PartRepository.GetUnitConversionsStatic(cmd, t.DO_ITEM.SO_ITEM.PART.ID);
                t.PART = t.DO_ITEM.SO_ITEM.PART = t.DO_ITEM.PART;

                double subamount = (t.DO_ITEM.SO_ITEM.SUBTOTAL / t.DO_ITEM.SO_ITEM.GetAmountInSmallestUnit()) * t.GetAmountInSmallestUnit();
                p.TOTAL_AMOUNT_FROM_SO += subamount;

                t.DO_ITEM.SO_ITEM.EVENT = SalesOrderRepository.GetHeaderOnly(cmd, t.DO_ITEM.SO_ITEM.EVENT.ID);
                p.CURRENCY = ((SalesOrder)t.DO_ITEM.SO_ITEM.EVENT).CURRENCY;
            }
            return(p);
        }
        public IList GetSupplierInvoiceItem(int partID)
        {
            MySql.Data.MySqlClient.MySqlDataReader rdr;
            Part      p      = PartRepository.GetByID(m_command, partID);
            ArrayList result = new ArrayList();

            m_command.CommandText = SupplierInvoiceItem.GetByPartIDOrderByDateSQL(partID);
            rdr = m_command.ExecuteReader();
            IList piis = SupplierInvoiceItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (SupplierInvoiceItem itm in piis)
            {
                itm.EVENT = SupplierInvoiceRepository.GetHeaderOnly(m_command, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }

            m_command.CommandText = StockTakingItems.GetByPartIDOrderByDateSQL(partID);
            rdr = m_command.ExecuteReader();
            IList sti = StockTakingItems.TransformReaderList(rdr);

            rdr.Close();
            foreach (StockTakingItems itm in sti)
            {
                itm.EVENT = StockTakingRepository.GetHeaderOnly(m_command, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }

            m_command.CommandText = OpeningStockItem.GetByPartIDOrderByDateSQL(partID);
            rdr = m_command.ExecuteReader();
            IList opi = OpeningStockItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (OpeningStockItem itm in opi)
            {
                itm.EVENT = OpeningStockRepository.GetHeaderOnly(m_command, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }

            result.Sort(new Profit.Server.PartRepository.EventDateComparer());
            return(result);
        }
Esempio n. 7
0
        protected override void doSave(Event e)
        {
            MySql.Data.MySqlClient.MySqlTransaction trc = m_connection.BeginTransaction();
            try
            {
                m_command.Transaction = trc;

                DateTime trDate     = DateTime.Today;
                string   codesample = AutoNumberSetupRepository.GetCodeSampleByDomainName(m_command, "GoodReceiveNote");
                Event    codeDate   = FindLastCodeAndTransactionDate(codesample);
                string   lastCode   = codeDate == null ? string.Empty : codeDate.CODE;
                DateTime lastDate   = codeDate == null ? trDate : codeDate.TRANSACTION_DATE;
                int      trCount    = RecordCount();
                e.CODE = AutoNumberSetupRepository.GetAutoNumberByDomainName(m_command, "GoodReceiveNote", e.CODE, lastCode, lastDate, trDate, trCount == 0);

                GoodReceiveNote stk = (GoodReceiveNote)e;
                m_command.CommandText = e.GetInsertSQL();
                m_command.ExecuteNonQuery();
                m_command.CommandText = GoodReceiveNote.SelectMaxIDSQL();
                stk.ID = Convert.ToInt32(m_command.ExecuteScalar());
                foreach (GoodReceiveNoteItem item in stk.EVENT_ITEMS)
                {
                    item.PART.UNIT_CONVERSION_LIST = PartRepository.GetUnitConversionsStatic(m_command, item.PART.ID);
                    m_command.CommandText          = item.GetInsertSQL();
                    m_command.ExecuteNonQuery();
                    m_command.CommandText = GoodReceiveNoteItem.SelectMaxIDSQL();
                    item.ID = Convert.ToInt32(m_command.ExecuteScalar());
                }
                trc.Commit();
            }
            catch (Exception x)
            {
                e.ID = 0;
                foreach (EventItem item in e.EVENT_ITEMS)
                {
                    item.ID = 0;
                }
                trc.Rollback();
                throw x;
            }
        }
Esempio n. 8
0
        public OpeningStock GetOpeningStockByNotes(string note)
        {
            m_command.CommandText = OpeningStock.GetByNotesSQL(note);
            MySql.Data.MySqlClient.MySqlDataReader r = m_command.ExecuteReader();
            OpeningStock st = OpeningStock.TransformReader(r);

            r.Close();
            m_command.CommandText = OpeningStockItem.GetByEventIDSQL(st.ID);
            r = m_command.ExecuteReader();
            IList stis = OpeningStockItem.TransformReaderList(r);

            r.Close();
            foreach (OpeningStockItem sti in stis)
            {
                sti.EVENT            = st;
                sti.PART             = PartRepository.GetByID(m_command, sti.PART.ID);
                sti.STOCK_CARD_ENTRY = StockCardEntryRepository.FindStockCardEntryByEventItem(m_command, sti.ID, sti.STOCK_CARD_ENTRY_TYPE);
                st.EVENT_ITEMS.Add(sti);
            }
            return(st);
        }
Esempio n. 9
0
        protected override Event doGet(int ID)
        {
            m_command.CommandText = PurchaseOrder.GetByIDSQL(ID);
            MySql.Data.MySqlClient.MySqlDataReader r = m_command.ExecuteReader();
            PurchaseOrder st = PurchaseOrder.TransformReader(r);

            r.Close();
            m_command.CommandText = PurchaseOrderItem.GetByEventIDSQL(ID);
            r = m_command.ExecuteReader();
            IList stis = PurchaseOrderItem.TransformReaderList(r);

            r.Close();
            foreach (PurchaseOrderItem sti in stis)
            {
                sti.EVENT            = st;
                sti.PART             = PartRepository.GetByID(m_command, sti.PART.ID);
                sti.STOCK_CARD_ENTRY = StockCardEntryRepository.FindStockCardEntryByEventItem(m_command, sti.ID, sti.STOCK_CARD_ENTRY_TYPE);
                st.EVENT_ITEMS.Add(sti);
            }
            return(st);
        }
Esempio n. 10
0
        protected override void doUpdate(Event en)
        {
            MySql.Data.MySqlClient.MySqlTransaction trc = m_connection.BeginTransaction();
            m_command.Transaction = trc;
            try
            {
                CustomerInvoice e = (CustomerInvoice)en;
                m_command.CommandText = e.GetUpdateSQL();
                m_command.ExecuteNonQuery();

                foreach (CustomerInvoiceItem sti in e.EVENT_ITEMS)
                {
                    sti.PART.UNIT_CONVERSION_LIST = PartRepository.GetUnitConversionsStatic(m_command, sti.PART.ID);
                    if (sti.ID > 0)
                    {
                        m_command.CommandText = sti.GetUpdateSQL();
                        m_command.ExecuteNonQuery();
                    }
                    else
                    {
                        m_command.CommandText = sti.GetInsertSQL();
                        m_command.ExecuteNonQuery();
                        m_command.CommandText = CustomerInvoiceItem.SelectMaxIDSQL();
                        sti.ID = Convert.ToInt32(m_command.ExecuteScalar());
                    }
                }
                m_command.CommandText = CustomerInvoiceItem.DeleteUpdate(e.ID, e.EVENT_ITEMS);
                m_command.ExecuteNonQuery();
                trc.Commit();
            }
            catch (Exception x)
            {
                trc.Rollback();
                throw x;
            }
        }
Esempio n. 11
0
        public IList FindSRForARCreditNote(string find, int supID, DateTime trdate, IList added)
        {
            m_command.CommandText = ARCreditNoteItem.GetSRUsedByARCR();
            MySql.Data.MySqlClient.MySqlDataReader r = m_command.ExecuteReader();
            if (r.HasRows)
            {
                while (r.Read())
                {
                    int id = Convert.ToInt32(r[0]);
                    if (!added.Contains(id))
                    {
                        added.Add(id);
                    }
                }
            }
            r.Close();
            StringBuilder poisSB = new StringBuilder();

            foreach (int i in added)
            {
                poisSB.Append(i.ToString());
                poisSB.Append(',');
            }
            string pois = poisSB.ToString();

            pois = added.Count > 0 ? pois.Substring(0, pois.Length - 1) : "";

            m_command.CommandText = SalesReturn.GetSearchSRNoForARCR(find, supID, pois, trdate);
            r = m_command.ExecuteReader();
            IList result = SalesReturn.TransformReaderList(r);

            r.Close();
            foreach (SalesReturn p in result)
            {
                m_command.CommandText = SalesReturnItem.GetByEventIDSQL(p.ID);
                r             = m_command.ExecuteReader();
                p.EVENT_ITEMS = SalesReturnItem.TransformReaderList(r);
                r.Close();

                foreach (SalesReturnItem t in p.EVENT_ITEMS)
                {
                    if ((t.DO_ITEM == null) && (t.DO_ITEM.ID == 0))
                    {
                        continue;
                    }


                    m_command.CommandText = DeliveryOrderItem.GetByIDSQL(t.DO_ITEM.ID);
                    r         = m_command.ExecuteReader();
                    t.DO_ITEM = DeliveryOrderItem.TransformReader(r);
                    r.Close();

                    if ((t.DO_ITEM.SO_ITEM == null))
                    {
                        continue;
                    }
                    if (t.DO_ITEM.SO_ITEM.ID == 0)
                    {
                        continue;
                    }

                    m_command.CommandText = SalesOrderItem.GetByIDSQL(t.DO_ITEM.SO_ITEM.ID);
                    r = m_command.ExecuteReader();
                    t.DO_ITEM.SO_ITEM = SalesOrderItem.TransformReader(r);
                    r.Close();

                    t.DO_ITEM.PART = PartRepository.GetByID(m_command, t.DO_ITEM.PART.ID);
                    t.DO_ITEM.PART.UNIT_CONVERSION_LIST = PartRepository.GetUnitConversionsStatic(m_command, t.DO_ITEM.SO_ITEM.PART.ID);
                    t.PART = t.DO_ITEM.SO_ITEM.PART = t.DO_ITEM.PART;

                    double subamount = (t.DO_ITEM.SO_ITEM.SUBTOTAL / t.DO_ITEM.SO_ITEM.GetAmountInSmallestUnit()) * t.GetAmountInSmallestUnit();
                    p.TOTAL_AMOUNT_FROM_SO += subamount;

                    t.DO_ITEM.SO_ITEM.EVENT = SalesOrderRepository.GetHeaderOnly(m_command, t.DO_ITEM.SO_ITEM.EVENT.ID);
                    p.CURRENCY = ((SalesOrder)t.DO_ITEM.SO_ITEM.EVENT).CURRENCY;
                }
            }
            return(result);
        }
Esempio n. 12
0
        public DataSet GetStockReport(bool allPart, string partStart, string partEnd, bool allGroup,
                                      string groupStart, string groupEnd, DateTime asOfDate)
        {
            DataSet ds = new DataSet();

            m_cmd.CommandText = "delete from table_temp";
            m_cmd.ExecuteNonQuery();

            m_cmd.CommandText = Part.GetStockReport(allPart, partStart, partEnd, allGroup, groupStart, groupEnd);
            MySql.Data.MySqlClient.MySqlDataReader rdr = m_cmd.ExecuteReader();
            IList result = Part.GetAllStaticForReport(rdr);

            rdr.Close();
            //IList stockcards = new ArrayList();
            foreach (Part p in result)
            {
                p.UNIT       = PartRepository.GetUnitByID(m_cmd, p.UNIT.ID);
                p.PART_GROUP = PartRepository.GetPartGroupByID(m_cmd, p.PART_GROUP.ID);

                StockCard sc = new StockCard(p, null, null);
                m_cmd.CommandText = StockCardEntry.FindByStockCardEntryByPart(p.ID, new DateTime(2000, 1, 1), asOfDate);
                rdr = m_cmd.ExecuteReader();
                sc.STOCK_CARD_ENTRIES = StockCardEntry.TransformReaderList(rdr);
                rdr.Close();
                foreach (StockCardEntry e in sc.STOCK_CARD_ENTRIES)
                {
                    if (e.STOCK_CARD_ENTRY_TYPE == StockCardEntryType.SupplierInvoice)
                    {
                        m_cmd.CommandText = SupplierInvoiceItem.GetByIDSQL(e.EVENT_ITEM.ID);
                        rdr          = m_cmd.ExecuteReader();
                        e.EVENT_ITEM = SupplierInvoiceItem.TransformReader(rdr);
                        rdr.Close();
                    }
                    if (e.STOCK_CARD_ENTRY_TYPE == StockCardEntryType.CustomerInvoice)
                    {
                        m_cmd.CommandText = CustomerInvoiceItem.GetByIDSQL(e.EVENT_ITEM.ID);
                        rdr          = m_cmd.ExecuteReader();
                        e.EVENT_ITEM = CustomerInvoiceItem.TransformReader(rdr);
                        rdr.Close();
                    }
                }
                sc.recalculateAvailable();
                m_cmd.CommandText = string.Format(@"insert into table_temp 
                    (
                        string1,string2,string3,double1,double2
                    )
                    values   
                    (
                        '{0}','{1}','{2}',{3},{4}
                    )",
                                                  p.PART_GROUP.NAME + " ~ " + p.PART_GROUP.CODE,
                                                  p.CODE,
                                                  p.UNIT.NAME,
                                                  sc.BALANCE_AVAILABLE,
                                                  p.MINIMUM_STOCK
                                                  );
                m_cmd.ExecuteNonQuery();
                //stockcards.Add(sc);
            }
            return(ds);
        }
Esempio n. 13
0
        public static double GetLatestPriceMovementItemPeriod(MySql.Data.MySqlClient.MySqlCommand m_command,
                                                              int partID, DateTime start, DateTime end)
        {
            MySql.Data.MySqlClient.MySqlDataReader rdr;
            Part      p      = PartRepository.GetByID(m_command, partID);
            ArrayList result = new ArrayList();

            m_command.CommandText = SupplierInvoiceItem.GetByPartIDOrderByDateRangeSQL(partID, start, end);
            rdr = m_command.ExecuteReader();
            IList piis = SupplierInvoiceItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (SupplierInvoiceItem itm in piis)
            {
                itm.EVENT = SupplierInvoiceRepository.GetHeaderOnly(m_command, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }

            m_command.CommandText = StockTakingItems.GetByPartIDOrderByDateSQL(partID);
            rdr = m_command.ExecuteReader();
            IList sti = StockTakingItems.TransformReaderList(rdr);

            rdr.Close();
            foreach (StockTakingItems itm in sti)
            {
                itm.EVENT = StockTakingRepository.GetHeaderOnly(m_command, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }

            m_command.CommandText = OpeningStockItem.GetByPartIDOrderByDateSQL(partID);
            rdr = m_command.ExecuteReader();
            IList opi = OpeningStockItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (OpeningStockItem itm in opi)
            {
                itm.EVENT = OpeningStockRepository.GetHeaderOnly(m_command, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }
            result.Sort(new Profit.Server.PartRepository.EventDateComparer());

            double   average      = 0;
            double   price        = 0;
            double   priceBaseCcy = 0;
            Currency baseccy      = CurrencyRepository.GetBaseCurrency(m_command);

            for (int i = 0; i < result.Count; i++)
            {
                if (result[i] is OpeningStockItem)
                {
                    price = ((OpeningStockItem)result[i]).TOTAL_AMOUNT / ((OpeningStockItem)result[i]).GetAmountInSmallestUnit();
                    if (baseccy.ID == ((OpeningStock)((OpeningStockItem)result[i]).EVENT).CURRENCY.ID)
                    {
                        priceBaseCcy = price;
                    }
                    else
                    {
                        priceBaseCcy = CurrencyRepository.ConvertToBaseCurrency(m_command, ((OpeningStock)((OpeningStockItem)result[i]).EVENT).CURRENCY, price, DateTime.Today);
                    }
                }
                if (result[i] is StockTakingItems)
                {
                    price = ((StockTakingItems)result[i]).TOTAL_AMOUNT / ((StockTakingItems)result[i]).GetAmountInSmallestUnit();
                    if (baseccy.ID == ((StockTaking)((StockTakingItems)result[i]).EVENT).CURRENCY.ID)
                    {
                        priceBaseCcy = price;
                    }
                    else
                    {
                        priceBaseCcy = CurrencyRepository.ConvertToBaseCurrency(m_command, ((StockTaking)((StockTakingItems)result[i]).EVENT).CURRENCY, price, DateTime.Today);
                    }
                }
                if (result[i] is SupplierInvoiceItem)
                {
                    price = ((SupplierInvoiceItem)result[i]).SUBTOTAL / ((SupplierInvoiceItem)result[i]).GetAmountInSmallestUnit();
                    if (baseccy.ID == ((SupplierInvoice)((SupplierInvoiceItem)result[i]).EVENT).CURRENCY.ID)
                    {
                        priceBaseCcy = price;
                    }
                    else
                    {
                        priceBaseCcy = CurrencyRepository.ConvertToBaseCurrency(m_command, ((SupplierInvoice)((SupplierInvoiceItem)result[i]).EVENT).CURRENCY, price, DateTime.Today);
                    }
                }
                if (i == 0)
                {
                    average = priceBaseCcy;
                }
                else
                {
                    average = (average + priceBaseCcy) / 2;
                }
            }

            return(average < 0 ? -average : average);
        }
Esempio n. 14
0
        //-------------
        //public void UpdatePart()
        //{
        //    MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
        //    cmd.Connection = m_connection;
        //    m_connection.Open();
        //    cmd.CommandText = m_entity.GetAllSQL();

        //    MySql.Data.MySqlClient.MySqlDataReader r = cmd.ExecuteReader();
        //    IList result = new ArrayList();
        //    while (r.Read())
        //    {
        //        PartUnit pu = new PartUnit();
        //        pu.PART = Convert.ToInt32(r["part_id"]);
        //        pu.UNIT = Convert.ToInt32(r["unit_id"]);
        //        result.Add(pu);
        //    }
        //    r.Close();
        //    foreach (PartUnit e in result)
        //    {
        //        cmd.CommandText = UnitConversion.GetByPartAndUnitConIDSQL(e.PART, e.UNIT);
        //        r = cmd.ExecuteReader();
        //        UnitConversion uc = UnitConversion.GetUnitConversion(r);
        //        r.Close();
        //        if (uc == null)
        //        {
        //            uc = new UnitConversion();
        //            uc.BARCODE = e.BARCODE;
        //            uc.CONVERSION_QTY = 1;
        //            uc.CONVERSION_UNIT = e.UNIT;
        //            uc.COST_PRICE = e.COST_PRICE;
        //            uc.ORIGINAL_QTY = 1;
        //            uc.PART = e;
        //            uc.SELL_PRICE = e.SELL_PRICE;
        //            cmd.CommandText = uc.GetInsertSQL();
        //            cmd.ExecuteNonQuery();
        //        }
        //    }
        //}
        //private class PartUnit
        //{
        //    public int PART, UNIT;

        //}
        public IList GetAllEvents(int partID)
        {
            ArrayList result = new ArrayList();

            OpenConnection();
            MySql.Data.MySqlClient.MySqlDataReader rdr;
            Part p = PartRepository.GetByID(m_cmd, partID);

            m_cmd.CommandText = PurchaseOrderItem.GetByPartIDSQL(partID);
            rdr = m_cmd.ExecuteReader();
            IList pois = PurchaseOrderItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (PurchaseOrderItem itm in pois)
            {
                itm.EVENT = PurchaseOrderRepository.GetHeaderOnly(m_cmd, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }

            m_cmd.CommandText = SalesOrderItem.GetByPartIDSQL(partID);
            rdr = m_cmd.ExecuteReader();
            IList sois = SalesOrderItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (SalesOrderItem itm in sois)
            {
                itm.EVENT = SalesOrderRepository.GetHeaderOnly(m_cmd, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }


            m_cmd.CommandText = GoodReceiveNoteItem.GetByPartIDSQL(partID);
            rdr = m_cmd.ExecuteReader();
            IList grnis = GoodReceiveNoteItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (GoodReceiveNoteItem itm in grnis)
            {
                itm.EVENT = GoodReceiveNoteRepository.GetHeaderOnly(m_cmd, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }

            m_cmd.CommandText = PurchaseReturnItem.GetByPartIDSQL(partID);
            rdr = m_cmd.ExecuteReader();
            IList prnis = PurchaseReturnItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (PurchaseReturnItem itm in prnis)
            {
                itm.EVENT = PurchaseReturnRepository.GetHeaderOnly(m_cmd, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }

            m_cmd.CommandText = SalesReturnItem.GetByPartIDSQL(partID);
            rdr = m_cmd.ExecuteReader();
            IList srnis = SalesReturnItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (SalesReturnItem itm in srnis)
            {
                itm.EVENT = SalesReturnRepository.GetHeaderOnly(m_cmd, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }

            m_cmd.CommandText = SupplierInvoiceItem.GetByPartIDSQL(partID);
            rdr = m_cmd.ExecuteReader();
            IList piis = SupplierInvoiceItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (SupplierInvoiceItem itm in piis)
            {
                itm.EVENT = SupplierInvoiceRepository.GetHeaderOnly(m_cmd, itm.EVENT.ID);
                itm.PART  = p;
                if (itm.GRN_ITEM.ID == 0)
                {
                    result.Add(itm);
                }
            }

            m_cmd.CommandText = CustomerInvoiceItem.GetByPartIDSQL(partID);
            rdr = m_cmd.ExecuteReader();
            IList ciis = CustomerInvoiceItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (CustomerInvoiceItem itm in ciis)
            {
                itm.EVENT = CustomerInvoiceRepository.GetHeaderOnly(m_cmd, itm.EVENT.ID);
                itm.PART  = p;
                if (itm.DO_ITEM.ID == 0)
                {
                    result.Add(itm);
                }
            }

            m_cmd.CommandText = POSItem.GetByPartIDSQL(partID);
            rdr = m_cmd.ExecuteReader();
            IList posi = POSItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (POSItem itm in posi)
            {
                itm.EVENT = POSRepository.GetHeaderOnly(m_cmd, itm.EVENT.ID);
                itm.PART  = p;
                if (itm.DO_ITEM.ID == 0)
                {
                    result.Add(itm);
                }
            }

            m_cmd.CommandText = StockTakingItems.GetByPartIDSQL(partID);
            rdr = m_cmd.ExecuteReader();
            IList sti = StockTakingItems.TransformReaderList(rdr);

            rdr.Close();
            foreach (StockTakingItems itm in sti)
            {
                itm.EVENT = StockTakingRepository.GetHeaderOnly(m_cmd, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }

            m_cmd.CommandText = OpeningStockItem.GetByPartIDSQL(partID);
            rdr = m_cmd.ExecuteReader();
            IList opi = OpeningStockItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (OpeningStockItem itm in opi)
            {
                itm.EVENT = OpeningStockRepository.GetHeaderOnly(m_cmd, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }

            m_cmd.CommandText = DeliveryOrderItem.GetByPartIDSQL(partID);
            rdr = m_cmd.ExecuteReader();
            IList doi = DeliveryOrderItem.TransformReaderList(rdr);

            rdr.Close();
            foreach (DeliveryOrderItem itm in doi)
            {
                itm.EVENT = DeliveryOrderRepository.GetHeaderOnly(m_cmd, itm.EVENT.ID);
                itm.PART  = p;
                result.Add(itm);
            }

            result.Sort(new EventDateComparer());
            return(result);
        }
        public void ProcessTransaction(int currentPeriodId, Employee emp)
        {
            OpenConnection();
            MySql.Data.MySqlClient.MySqlTransaction trc = m_connection.BeginTransaction();
            try
            {
                m_command.Transaction = trc;
                Period crntPeriod = PeriodRepository.FindPeriod(m_command, currentPeriodId) as Period;
                if (crntPeriod == null)
                {
                    throw new Exception("Current Period Not Found!");
                }
                if (crntPeriod.PERIOD_STATUS != PeriodStatus.Current)
                {
                    throw new Exception("Period is not in Active Month!");
                }
                GeneralSetup gs = GeneralSetupRepository.GetGeneralSetup(m_command);
                if (gs.START_ENTRY_PERIOD == null)
                {
                    throw new Exception("Start Entry Month Not Found!");
                }

                IList invTrs = this.GetAllCodeListOfNotPostedEvent(crntPeriod.START_DATE, crntPeriod.END_DATA);

                string invCodes = string.Empty;

                if (invTrs.Count > 0)
                {
                    foreach (string code in invTrs)
                    {
                        invCodes += code + "\r\n";
                    }
                }
                if (invTrs.Count > 0)
                {
                    throw new Exception("Please Post Transaction : \r\n" + invCodes);
                }

                Period nextPeriod = this.GetNextPeriod(crntPeriod) as Period;
                if (nextPeriod == null)
                {
                    throw new Exception("Next Period Not Define!");
                }

                IList stockcards = StockCardRepository.FindStockCardByPeriod(m_command, crntPeriod.ID);

                nextPeriod.PERIOD_STATUS = PeriodStatus.Current;
                nextPeriod.CLOSED_DATE   = DateTime.Now;
                PeriodRepository.UpdatePeriod(m_command, nextPeriod);

                crntPeriod.PERIOD_STATUS = PeriodStatus.Close;
                crntPeriod.CLOSED_DATE   = DateTime.Now;

                PeriodRepository.UpdatePeriod(m_command, crntPeriod);

                IList newSCards = new ArrayList();

                OpeningStock ops = new OpeningStock();
                ops.TRANSACTION_DATE = nextPeriod.START_DATE;
                ops.CURRENCY         = CurrencyRepository.GetBaseCurrency(m_command);
                ops.DOCUMENT_DATE    = DateTime.Today;
                ops.EMPLOYEE         = emp;
                ops.MODIFIED_BY      = emp.NAME;
                ops.MODIFIED_DATE    = DateTime.Today;
                ops.NOTES            = "AUTO" + nextPeriod.START_DATE.ToString(Utils.DATE_FORMAT_SHORT);
                ops.NOTICE_DATE      = DateTime.Today;
                ops.WAREHOUSE        = getCommonStore();
                double total = 0;

                for (int i = 0; i < stockcards.Count; i++)
                {
                    StockCard sCard = stockcards[i] as StockCard;
                    if (sCard.BALANCE == 0)
                    {
                        continue;
                    }
                    OpeningStockItem opi = new OpeningStockItem();
                    opi.EVENT = ops;
                    opi.PART  = sCard.PART;
                    opi.PRICE = PartRepository.GetLatestPriceMovementItemPeriod(m_command, sCard.PART.ID,
                                                                                crntPeriod.START_DATE, crntPeriod.END_DATA);
                    opi.QYTAMOUNT    = sCard.BALANCE;
                    opi.TOTAL_AMOUNT = opi.PRICE * sCard.BALANCE;
                    Part p = PartRepository.GetByID(m_command, sCard.PART.ID);
                    opi.UNIT      = p.UNIT;
                    opi.WAREHOUSE = sCard.WAREHOUSE;
                    ops.EVENT_ITEMS.Add(opi);
                    total += opi.TOTAL_AMOUNT;
                    // newSCards.Add(sCard.Create(nextPeriod));
                }
                ops.AMOUNT = total;
                r_openingStock.SaveNoTransaction(ops, m_command);
                r_openingStock.ConfirmNoTransaction(ops.ID, m_command);
                //foreach (StockCard sc in newSCards)
                //{
                // StockCardRepository.SaveHeader(m_command, sc);
                // }

                IList vbalances    = VendorBalanceRepository.FindVendorBalanceByPeriod(m_command, crntPeriod.ID);
                IList newvbalances = new ArrayList();
                for (int i = 0; i < vbalances.Count; i++)
                {
                    VendorBalance vb = vbalances[i] as VendorBalance;
                    newvbalances.Add(vb.Create(nextPeriod));
                    //if (vb.VENDOR_BALANCE_TYPE == VendorBalanceType.Customer)
                    //{
                    //    CustomerOutStandingInvoice cosi = new CustomerOutStandingInvoice();
                    //    cosi.CURRENCY = vb.CURRENCY;
                    //    cosi.TRANSACTION_DATE = nextPeriod.START_DATE;
                    //    cosi.EMPLOYEE = emp;
                    //    cosi.MODIFIED_BY = emp.NAME;
                    //    cosi.MODIFIED_COMPUTER_NAME = Environment.MachineName;
                    //    cosi.MODIFIED_DATE = DateTime.Now;
                    //    cosi.NOTES = "AUTO";
                    //    cosi.NOTICE_DATE = DateTime.Now;
                    //    cosi.VENDOR = vb.VENDOR;
                    //    CustomerOutStandingInvoiceItem cosii = new CustomerOutStandingInvoiceItem();
                    //    cosii.EVENT_JOURNAL = cosi;
                    //    cosii.INVOICE_NO = "AUTO_OPENING_BALANCE";
                    //    cosii.INVOICE_DATE = DateTime.Today;
                    //    cosii.TOP = getCommonTOP();
                    //    cosii.DUE_DATE = DateTime.Today;
                    //    cosii.EMPLOYEE = emp;
                    //    cosii.AMOUNT = vb.BALANCE;
                    //    cosii.
                    //}
                }
                foreach (VendorBalance sc in newvbalances)
                {
                    VendorBalanceRepository.SaveHeader(m_command, sc);
                }

                trc.Commit();
            }
            catch (Exception x)
            {
                trc.Rollback();
                throw x;
            }
        }