private void simpleButton1_Click(object sender, EventArgs e)
        {
            var dt = (DataView) gridView1.DataSource;
            dt.RowFilter = "Requested >0";
            if (dt == null) return;
            var po = new PO();
            po.AddNew();
            var serverDateTime = DateTimeHelper.ServerDateTime;
            po.PODate = serverDateTime;
            po.DateOfEntry = serverDateTime;
            po.PurchaseType = POType.INTERNAL;
            po.PaymentTypeID = PaymentType.Constants.STV;
            po.TermOfPayement = TermOfPayment.List[6].ID;
            po.PurchaseOrderStatusID = 9; //PurchaseOrderStatus ==> Processed
            po.SavedbyUserID = CurrentContext.LoggedInUser.ID;
            po.Save();
            var purchaseOrderDetail = new PurchaseOrderDetail();
            foreach (DataRowView dr in dt)
            {
                var itemUnit = new ItemUnitBase();
                itemUnit.LoadByPrimaryKey(Convert.ToInt32(dr["UnitID"]));

                purchaseOrderDetail.AddNew();
                purchaseOrderDetail.ItemID = Convert.ToInt32(dr["ItemID"]);
                purchaseOrderDetail.UnitOfIssueID = itemUnit.UnitOfIssueID;
                purchaseOrderDetail.Quantity = Convert.ToInt32(dr["Requested"]);
                purchaseOrderDetail.PurchaseOrderID = po.ID;
                purchaseOrderDetail.Rowguid = Guid.NewGuid();
                purchaseOrderDetail.ApprovedQuantity = 0;

            }
            purchaseOrderDetail.Save();
            lookUpEdit1_EditValueChanged(null, null);
        }
        public void SavePoDetail()
        {
            if (!ValidateOrderDetails())
            {
                return;
            }

            var itemData = (DataView)gridSelectedItemsView.DataSource;
            foreach (DataRowView row in itemData)
            {
                if (row["PurchaseOrderDetailID"] == DBNull.Value)
                {
                    _poDetail.AddNew();
                }
                else
                {
                    _poDetail.LoadByPrimaryKey(Convert.ToInt32(row["PurchaseOrderDetailID"]));
                }
                var itemUnit = new ItemUnitBase();
                var unitID = Convert.ToInt32(row["UnitID"]);
                itemUnit.LoadByPrimaryKey(unitID);

                _poDetail.PurchaseOrderID = _poid;
                _poDetail.ItemID = Convert.ToInt32(row["ItemID"]);
                _poDetail.Quantity = Convert.ToDecimal(row["Quantity"]);
                _poDetail.Remark = string.Empty;
                _poDetail.UnitOfIssueID = itemUnit.UnitOfIssueID;
                _poDetail.Rowguid = Guid.NewGuid();
                _poDetail.ApprovedQuantity = 0;
                _poDetail.Amount = Convert.ToDecimal(row["Amount"]);
                if (row["PreferredManufacturerID"] != DBNull.Value)
                {
                    _poDetail.PreferredManufacturerID = Convert.ToInt32(row["PreferredManufacturerID"]);
                }
                _poDetail.Save();
            }
            MessageBox.Show("Order detail saved!", "Confirmation", MessageBoxButtons.OK,
                       MessageBoxIcon.Information);
            Close();
        }
        private void BindVolumeData()
        {
            DataTable selectedItems = (orderGrid.DataSource as DataView).Table;
            VolumeMetrics vm = new VolumeMetrics();
            ItemUnitBase iub = new ItemUnitBase();
            vm.LoadVolumeDataForItems(selectedItems.AsEnumerable().Select(t => t.Field<int>("ItemID")).ToArray<int>());
            iub.LoadAll();

            DataTable allVolumeData = vm.DefaultView.Table;

            if (orderGrid.DataSource == null) return;

            DataTable units = iub.DefaultView.Table ;

            var volDataTemp = (from v in allVolumeData.AsEnumerable()
                           join u in units.AsEnumerable()
                           on
                           new
                           {
                                 ItemID = v.Field<int>("ItemID")
                               , UnitID = v.Field<int>("UnitOfIssueID")
                           }
                           equals
                           new
                           {
                                 ItemID = u.Field<int>("ItemID")
                               , UnitID = u.Field<int>("UnitOfIssueID")
                           }
                           join s in selectedItems.AsEnumerable()
                           on
                           new
                           {
                                ItemID = u.Field<int>("ItemID")
                               , UnitID = u.Field<int>("ID")
                           }
                           equals
                           new
                           {
                                 ItemID = s.Field<int>("ItemID")
                               , UnitID = s.Field<int>("UnitID")
                           }
                           select new ItemVolumeQuantityViewModel
                           {
                               ItemID = v.Field<int>("ItemID"),
                               UnitID = s.Field<int>("UnitID"),
                               HeightMM = v.Field<int?>("HeightMM") ?? 1,
                               WidthMM = v.Field<int?>("WidthMM") ?? 1,
                               DepthMM = v.Field<int?>("LengthMM") ?? 1,
                               Weight = v.Field<int?>("WeightG") ?? 1,
                               Quantity = s.Field<decimal?>("Pack") ?? 0
                           }).ToList();

            var volData = new List<ItemVolumeQuantityViewModel>();

            foreach (ItemVolumeQuantityViewModel i in volDataTemp)
            {
                if (!volData.Any(t => t.ItemID == i.ItemID && t.UnitID == i.UnitID))
                    volData.Add(i);
            }

            grdVolumeGrid.DataSource = volData;
        }
        private void EditPurchaseOrderDetail(DataTable datasource)
        {
            var pdetail = new PurchaseOrderDetail();
            pdetail.LoadByPo(_poid);
            var iunit = new ItemUnitBase();
            if (_dtselectedItemsTable != null)
            {
                _dtselectedItemsTable.Clear();
            }

            pdetail.Rewind();
            while (!pdetail.EOF)
            {
                if (datasource != null)
                {

                    iunit.LoadUnit(pdetail.UnitOfIssueID, pdetail.ItemID);
                    DataRow[] dataRows = datasource.Select(String.Format("ItemID = {0} and UnitID = {1}", pdetail.ItemID, iunit.ID));
                    if (dataRows.Length > 0)
                    {
                        // _dtselectedItemsTable.ImportRow(dataRows[0]);
                        var selectedRow = _dtselectedItemsTable.NewRow();
                        selectedRow["FullItemName"] = dataRows[0]["FullItemName"];
                        selectedRow["StockCode"] = dataRows[0]["StockCode"];
                        selectedRow["Unit"] = dataRows[0]["Unit"];
                        selectedRow["UnitID"] = iunit.ID;
                        selectedRow["ItemID"] = pdetail.ItemID;
                        selectedRow["Quantity"] = pdetail.Quantity;
                        selectedRow["Amount"] = pdetail.Amount;
                        selectedRow["PreferredManufacturerID"] = pdetail.PreferredManufacturerID;
                        selectedRow["PurchaseOrderDetailID"] = pdetail.PurchaseOrderDetailID;
                        //Check all
                        dataRows[0]["IsSelected"] = true;

                        _dtselectedItemsTable.Rows.Add(selectedRow);
                    }
                }
                pdetail.MoveNext();
            }

            gridItemsList.DataSource = datasource.DefaultView;
            gridSelectedItems.DataSource = _dtselectedItemsTable.DefaultView;
        }