public IStock BindCostFromLedgerLite(IStock stock)
 {
     LedgerService ledgerService = new LedgerService(repository);
     LedgerLite ledger = ledgerService.GetLedgerLite(stock.ItemId, stock.UnitId, stock.ManufacturerId,
                                                 stock.MovingAverageId);
     stock.UnitCost = ledger.UnitCost;
     stock.Margin = ledger.Margin;
     stock.SellingPrice = ledger.SellingPrice;
     return stock;
 }
 public static DataTable SetPrice(DataTable dataTable)
 {
     foreach (DataRow dataRow in dataTable.Rows)
     {
         var ledgerService = new LedgerService();
         var ledgerObject = ledgerService.GetLedger(Convert.ToInt32(dataRow["ItemID"])
                                                        , Convert.ToInt32(dataRow["ItemUnitID"])
                                                        , Convert.ToInt32(dataRow["ManufacturerID"])
                                                        , Convert.ToInt32(dataRow["MovingAverageID"]));
         dataRow["UnitCost"] = Math.Round(ledgerObject.UnitCost, Settings.NoOfDigitsAfterTheDecimalPoint,
                                          MidpointRounding.AwayFromZero);
         dataRow["TotalCost"] = Math.Round(ledgerObject.UnitCost * Convert.ToDecimal(dataRow["PreviousQty"]),
                                           Settings.NoOfDigitsAfterTheDecimalPoint,
                                           MidpointRounding.AwayFromZero);
     }
     return dataTable;
 }
        private void SetPriceAndQuantity(DataRow dr, decimal dispatch, int buinsku, ReceivePallet rp, int itemId, bool isDeliveryNote)
        {
            dr["DeliveryNote"] = isDeliveryNote;
            dr["BoxLevel"] = 0;
            dr["QtyPerPack"] = buinsku;
            dr["Pack"] = dispatch / buinsku;
            decimal packs = dispatch / buinsku;
            dr["QtyInBU"] = dispatch;
            dr["approvedQuantity"] = dispatch;

            if (isDeliveryNote)// (!Settings.UsesMovingAverage)  //We are taking care of this at the receive page.  Therefore, the selling price would have been set when entering here so for now, I'm disabling this condition
            {
                dr["CalculatedCost"] = 0;
                dr["UnitPrice"] = 0;
            }
            else
            {
                // Apply the cost and selling price from the cost tier.
                if (BLL.Settings.UseCostTier)
                {
                    HCMIS.Specification.Finance.CostTier.ILedger ledgerService = new LedgerService();
                    var ledgerObject = ledgerService.GetLedger(Convert.ToInt32(dr["ItemID"]),
                                                               Convert.ToInt32(dr["UnitID"]),
                                                               Convert.ToInt32(dr["ManufacturerID"]),
                                                               MovingAverageGroup.Convert(Convert.ToInt32(dr["StoreID"]))
                                                               );
                    if (BLL.Settings.IsCenter)
                    {
                        dr["CalculatedCost"] =
                            Math.Round(Convert.ToDouble(ledgerObject.UnitCost)*Convert.ToDouble(packs),
                                       BLL.Settings.NoOfDigitsAfterTheDecimalPoint, MidpointRounding.AwayFromZero);
                        dr["UnitPrice"] = Math.Round(ledgerObject.UnitCost,
                                                     BLL.Settings.NoOfDigitsAfterTheDecimalPoint,
                                                     MidpointRounding.AwayFromZero);
                    }
                    else
                    {
                        dr["CalculatedCost"] =
                           Math.Round(Convert.ToDouble(ledgerObject.SellingPrice) * Convert.ToDouble(packs),
                                      BLL.Settings.NoOfDigitsAfterTheDecimalPoint, MidpointRounding.AwayFromZero);
                        dr["UnitPrice"] = Math.Round(ledgerObject.SellingPrice,
                                                     BLL.Settings.NoOfDigitsAfterTheDecimalPoint,
                                                     MidpointRounding.AwayFromZero);

                    }
                }
                else
                {
                    try
                    {
                        //
                        if (BLL.Settings.IsCenter)
                        {
                            //TOFIXXX when selling price is null, it shouldn't be coming here
                            dr["CalculatedCost"] = Convert.ToDouble(packs) * Math.Round(Convert.ToDouble(rp.GetColumn("Cost")), BLL.Settings.NoOfDigitsAfterTheDecimalPoint, MidpointRounding.AwayFromZero);
                            double sPrice = Math.Round(Convert.ToDouble(rp.GetColumn("Cost")), BLL.Settings.NoOfDigitsAfterTheDecimalPoint, MidpointRounding.AwayFromZero);
                            dr["UnitPrice"] = sPrice;
                        }
                        else
                        {
                            //TOFIXXX when selling price is null, it shouldn't be coming here
                            dr["CalculatedCost"] = Convert.ToDouble(packs) * Math.Round(Convert.ToDouble(rp.GetColumn("SellingPrice")), BLL.Settings.NoOfDigitsAfterTheDecimalPoint, MidpointRounding.AwayFromZero);
                            double sPrice = Math.Round(Convert.ToDouble(rp.GetColumn("SellingPrice")), BLL.Settings.NoOfDigitsAfterTheDecimalPoint, MidpointRounding.AwayFromZero);
                            dr["UnitPrice"] = sPrice;
                        }

                    }
                    catch
                    {
                        BLL.Item itm = new Item();
                        itm.LoadByPrimaryKey(itemId);
                        //throw new Exception(string.Format("Selling price not set for {0}", itm.FullItemName));
                    }
                }

            }

            dr["UnitsInSKU"] = buinsku;
        }
        public DataRow priceChangeReport(DataRow dr)
        {
            var costLedgerService = new LedgerService();
            var ledger = costLedgerService.GetLedger(ItemID, ItemUnitID, ManufacturerID,
                                                     MovingAverageID);

            dr["FullItemName"] = ItemName;
            dr["StockCode"] = StockCode;
            dr["Manufacturer"] = ManufacturerName;
            dr["Account"] = AccountName;
            dr["Unit"] = ItemUnitName;
            dr["PreviousUnitCost"] = ledger.UnitCost;
            dr["PreviousMargin"] = ledger.Margin;
            dr["PreviousSellingPrice"] = ledger.SellingPrice;
            dr["NewUnitCost"] = AverageCost;
            dr["NewMargin"] = Margin;
            dr["NewSellingPrice"] = SellingPrice;

            return dr;
        }
        private void btnApprove_Click(object sender, EventArgs e)
        {
            //dxCostingValidation.RemoveControlError(lkPeriod);
            if (!dxCostingValidation.Validate()) return;
             if (lkWarehouse.EditValue != null && lkAccount.EditValue != null && XtraMessageBox.Show(String.Format("Are you sure you want to Approve All Items, Once Approved the item will be available for release"), "Are you sure...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
             {
             try
             {

             ReceiveDocConfirmation receiveDocConfirmation = new ReceiveDocConfirmation();
             string receiveDocs = "";
             string rdPendingAverages = "";
             var ledgerService = new LedgerService();
             DataView dataView = (gridMain.DataSource as DataView);
             dataView.RowFilter = "PricePerPack <>0 and isConfirmed = 1";
             foreach (DataRowView drv in dataView)
             {
                 var costElement = new CostElement
                                       {
                                           ItemID = Convert.ToInt32(drv["ItemID"]),
                                           ItemUnitID = Convert.ToInt32(drv["UnitID"]),
                                           ManufacturerID = Convert.ToInt32(drv["ManufacturerID"]),
                                           MovingAverageID = Convert.ToInt32(drv["MovingAverageID"])
                                       };

                 var ledgerObject = ledgerService.GetLedger(costElement.ItemID, costElement.ItemUnitID,
                                                            costElement.ManufacturerID, costElement.MovingAverageID);

                 costElement.UnitCost = Math.Round(Convert.ToDouble(ledgerObject.UnitCost),
                                                   BLL.Settings.NoOfDigitsAfterTheDecimalPoint,
                                                   MidpointRounding.AwayFromZero);
                 costElement.AverageCost = Math.Round(Convert.ToDouble(drv["PricePerPack"]),
                                                      BLL.Settings.NoOfDigitsAfterTheDecimalPoint,
                                                      MidpointRounding.AwayFromZero);
                 costElement.Margin = Convert.ToDouble(drv["Margin"])/100;
                 costElement.SellingPrice = costElement.AverageCost*(1 + costElement.Margin);
                 var isSound = drv["Remark"].Equals("Sound");
                 if ((Math.Abs(costElement.UnitCost - costElement.AverageCost) == 0) || !isSound)
                 {

                     receiveDocs = receiveDocs != ""
                                       ? receiveDocs + ',' + drv["ReceiveDocIDs"].ToString()
                                       : receiveDocs + drv["ReceiveDocIDs"].ToString();
                 }
                 else if (ReceiveDoc.GetSoundStock(costElement) > 0)
                 {
                     rdPendingAverages = rdPendingAverages != ""
                                             ? rdPendingAverages + ',' + drv["ReceiveDocIDs"].ToString()
                                             : rdPendingAverages + drv["ReceiveDocIDs"].ToString();

                 }
                 else
                 {

                     IJournalService journal = new JournalService();
                     journal.StartRecordingSession();
                     costElement.SavePrice(CurrentContext.UserId, "", journal, ChangeMode.BeginningBalance);
                     journal.CommitRecordingSession();

                     journal.StartRecordingSession();
                     costElement.ConfirmPrice(CurrentContext.UserId, "", journal, ChangeMode.BeginningBalance);
                     journal.CommitRecordingSession();

                     receiveDocs = receiveDocs != ""
                                       ? receiveDocs + ',' + drv["ReceiveDocIDs"].ToString()
                                       : receiveDocs + drv["ReceiveDocIDs"].ToString();
                 }

             }

             receiveDocConfirmation.ChangeStatusByAccountReceiveDocs(receiveDocs,
                                                                     ReceiptConfirmationStatus.Constants.GRV_PRINTED,
                                                                     ReceiptConfirmationStatus.Constants.GRNF_PRINTED);
             receiveDocConfirmation.ChangeStatusByAccountReceiveDocs(rdPendingAverages,
                                                                     ReceiptConfirmationStatus.Constants.
                                                                         PRICE_CALCULATED,
                                                                     ReceiptConfirmationStatus.Constants.GRNF_PRINTED);
             XtraMessageBox.Show("Price has been successfully confirmed", "Success...", MessageBoxButtons.OK,
                                 MessageBoxIcon.Information);
             }
             catch (Exception exception)
             {
                 XtraMessageBox.Show(exception.Message, "Success...", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                 throw;
             }
                      lkWarehouse_EditValueChanged(null, null);
             }
        }
        private void LoadAndCalculate()
        {
            var ledgerService = new LedgerService();
            var ledgerObject = ledgerService.GetLedger(costElement.ItemID, costElement.ItemUnitID, costElement.ManufacturerID,
                                                       costElement.MovingAverageID);

            previousQty = ReceiveDoc.GetSoundStock(costElement);

            previousUnitCost = Math.Round(ledgerObject.UnitCost, BLL.Settings.NoOfDigitsAfterTheDecimalPoint,
                                          MidpointRounding.AwayFromZero);

            AverageCostCalculator averageCostCalculator = new AverageCostCalculator(costElement);

            costElement.AverageCost = averageCostCalculator.CalculateMovingAverage(Convert.ToDouble(previousQty),
                                                                                   Convert.ToDouble(previousUnitCost),
                                                                                   costElement.Qty, costElement.UnitCost);
            costElement.SellingPrice = averageCostCalculator.GetSellingPrice(BLL.Settings.IsCenter?0:costElement.Margin);

            txtItemName.Text = costElement.ItemName;
            txtManufacturer.Text = costElement.ManufacturerName;
            txtUnit.Text = costElement.ItemUnitName;
        }