Esempio n. 1
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);
        }
        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;
            }
        }