/// <summary>
        /// Initializes this instance.
        /// </summary>
        private void Initialize()
        {
            this._vWareHouseDetailList = new List<VWareHouseDetail>();
            this.bdsMedicine.DataSource = _medicineRepo.GetAll();
            this._medDeliveryAllocationList = new List<MedicineDeliveryAllocationEntity>();
            this._prescription = _prescriptionRepo.Get(this._prescriptionId);
            this.bdsPrescription.DataSource = this._prescription;
            this._prescriptionDetailList = _prescriptionDetailRepo.GetByPrescription(this._prescriptionId);
            if (_prescription == null || this._prescriptionDetailList == null) throw new Exception("Data dosenot existed");

            this._medicineDelivery = this._medicineDeliveryRepo.GetByPrescriptionId(this._prescriptionId);
            this._formMode = this._medicineDelivery == null ? ViewModes.Add : ViewModes.View;

            if (this._formMode == ViewModes.Add)
            {
                // Createe MachineDelivery
                // Create MachineDeliveryDetail
                // Craete MedicindeDeliveryDetailAllocate
                // Get WareHouseDetail
                // Get WareHouse;
                // this._warehouseDetailList  = this._warehouseDetailRepo
                var medincineIdList = this._prescriptionDetailList.Select(item => item.MedicineId).ToList();
                this._warehouseList = this._warehouseRepo.GetByMedicineId(medincineIdList, AppContext.CurrentClinic.Id);
                this._vWareHouseDetailList = this._vWareHouseDetailRepo.GetByMedicine(medincineIdList);
                this._medicineDelivery = CreatePrescription(this._prescription);
                this._medicineDeliveryDetailList = CreateMedicineDeliveryDetail(this._prescriptionDetailList);
                this.AutoAllocate(this._medicineDeliveryDetailList, this._vWareHouseDetailList);
                // this._warehouseDetailList = this._warehouseDetailRepo.GetByMedicine(medincineIdList, AppContext.CurrentClinic.Id);)
                // this._mdecidineDeliveryDetailAllocate = AutoAllocate(this._medicineDeliveryDetailList, this._warehouseDetailList);
                // var item = this._warehouseDetailList.Select(x => new { x.MedicineId, x.LotNo, x.ExpiredDate}).GroupBy(x => new { x.MedicineId, x.LotNo, x.ExpiredDate }).ToList();

                var no = 1;
                foreach (var deliveryItem in this._medicineDeliveryDetailList)
                {
                    // var allocatedList = this._mdecidineDeliveryDetailAllocate.Where(x => x.MedicineDeliveryDetailId == deliveryItem.Id).ToList();
                    var warehouse = this._warehouseList.FirstOrDefault(x => x.MedicineId == deliveryItem.MedicineId);
                    var item = new MedicineDeliveryAllocationEntity(no++, deliveryItem, warehouse);
                    this._medDeliveryAllocationList.Add(item);

                    var subNo = 1;
                    foreach (var itm in deliveryItem.AllocatedWareHouseDetail)
                    {
                        var subItem = new MedicineDeliveryAllocationEntity(subNo++, itm);
                        _medDeliveryAllocationList.Add(subItem);
                    }
                }
                this.bindingSource1.DataSource = this._medDeliveryAllocationList;
            } else {
                var medincineIdList = this._prescriptionDetailList.Select(item => item.MedicineId).ToList();
                this._warehouseList = this._warehouseRepo.GetByMedicineId(medincineIdList, AppContext.CurrentClinic.Id);
                this._vWareHouseDetailList = this._vWareHouseDetailRepo.GetByMedicine(medincineIdList);

                this._medicineDelivery = this._medicineDeliveryRepo.GetByPrescriptionId(this._prescription.Id);
                this._medicineDeliveryDetailList = this._medicineDeliveryDetailRepo.GetByDelivery(this._medicineDelivery.Id);
                var deliveryDetailList = this._medicineDeliveryDetailList.Select(x => x.Id).ToList();
                this._vMedicineDeliveryDetailAllocateds = this._medicineDeliveryDetailRepo.GetDeliveryDetailAllocateds(deliveryDetailList);
                // this._mdecidineDeliveryDetailAllocate = AutoAllocate(this._medicineDeliveryDetailList, this._warehouseDetailList);
                // var item = this._warehouseDetailList.Select(x => new { x.MedicineId, x.LotNo, x.ExpiredDate}).GroupBy(x => new { x.MedicineId, x.LotNo, x.ExpiredDate }).ToList();
                // this._warehouseDetailList = this._warehouseDetailRepo.GetByMedicine(medincineIdList, AppContext.CurrentClinic.Id);)

                var no = 1;
                foreach (var deliveryItem in this._medicineDeliveryDetailList) {
                    // var allocatedList = this._mdecidineDeliveryDetailAllocate.Where(x => x.MedicineDeliveryDetailId == deliveryItem.Id).ToList();
                    var warehouse = this._warehouseList.FirstOrDefault(x => x.MedicineId == deliveryItem.MedicineId);
                    var item = new MedicineDeliveryAllocationEntity(no++, deliveryItem, warehouse);
                    this._medDeliveryAllocationList.Add(item);

                    List<VMedicineDeliveryDetailAllocated> allocatedWareHouseDetailList =
                        this._vMedicineDeliveryDetailAllocateds.Where(x => x.MedicineDeliveryDetailId == deliveryItem.Id)
                            .ToList();
                    var subNo = 1;
                    foreach (var itm in allocatedWareHouseDetailList) {
                        var subItem = new MedicineDeliveryAllocationEntity(subNo++, itm);
                        _medDeliveryAllocationList.Add(subItem);
                    }
                }
                this.dataGridViewX1.Columns[6].Visible = false;
                this.dataGridViewX1.Columns[8].Visible = false;
                this.bindingSource1.DataSource = this._medDeliveryAllocationList;

            }
        }
        public void Insert(MedicineDelivery medicineDelivery, List<MedicineDeliveryDetail> medicineDeliveryDetails)
        {
            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })) {

                // Insert DeliveryDetail
                // Get WareHouseDetail
                // Allocate Qty
                // Insert Allocate
                // Update WareHouseDetail
                // Update WareHouse
                medicineDelivery.SetInfo(false);
                this.Context.MedicineDeliveries.Add(medicineDelivery);
                this.Context.SaveChanges();

                // Get WareHouse Detail by MedicineID to allocated
                var medicineId = new List<int>();
                foreach (var detailItem in medicineDeliveryDetails) {
                    detailItem.MedicineDeliveryId = medicineDelivery.Id;
                    detailItem.SetInfo(false);
                    detailItem.Medicine = null;
                    this.Context.MedicineDeliveryDetails.Add(detailItem);
                    if (!medicineId.Contains(detailItem.MedicineId)) medicineId.Add(detailItem.MedicineId);
                }
                this.Context.SaveChanges();

                // Get WareHouse
                var warehouseList = this.Context.WareHouses.Where(x => medicineId.Contains(x.MedicineId) && x.ClinicId == AppContext.CurrentClinic.Id);
                var warehouseIdList = warehouseList.Select(x => x.Id).ToList();

                // Get WareHouseDetail
                var wareHouseDetails =
                    this.Context.WareHouseDetails.Where(x => medicineId.Contains(x.MedicineId) && x.CurrentVolumn > 0 && warehouseIdList.Contains(x.WareHouseId)).
                        ToList();

                // Allocated
                var allocateList = new List<MedicineDeliveryDetailAllocate>();
                foreach (var detailItem in medicineDeliveryDetails) {
                    // Update WareHouse
                    var warehouseItem = warehouseList.FirstOrDefault(x => x.MedicineId == detailItem.MedicineId);
                    if (warehouseItem == null) throw new Exception("Data is incompleted");
                    warehouseItem.Volumn -= detailItem.Volumn;

                    // Update WareHouseAllocated
                    var allocatedWareHouseDetail = detailItem.AllocatedWareHouseDetail;
                    foreach (var wareHouseDetailItem in allocatedWareHouseDetail) {
                        int qty = wareHouseDetailItem.AllocatedQty;
                        List<WareHouseDetail> wareHouseDetailAllocatedList =
                            wareHouseDetails.Where(
                                x =>
                                x.MedicineId == wareHouseDetailItem.MedicineId &&
                                x.ExpiredDate == wareHouseDetailItem.ExpiredDate &&
                                (x.LotNo == wareHouseDetailItem.LotNo ||
                                 (String.IsNullOrEmpty(x.LotNo) && String.IsNullOrEmpty(wareHouseDetailItem.LotNo)))).ToList();

                        foreach (var allocatedItem in wareHouseDetailAllocatedList) {
                            var allocated = new MedicineDeliveryDetailAllocate {
                                MedicineDeliveryDetailId = detailItem.Id,
                                WareHouseDetailId = allocatedItem.Id,
                                Unit = allocatedItem.Unit
                            };

                            if (wareHouseDetailItem.AllocatedQty <= allocatedItem.CurrentVolumn) {
                                allocated.Volumn = qty;
                                allocatedItem.CurrentVolumn -= qty;
                                qty = 0;
                            } else {
                                allocated.Volumn = allocatedItem.CurrentVolumn;
                                qty -= allocatedItem.CurrentVolumn;
                                allocatedItem.CurrentVolumn = 0;
                            }
                            allocateList.Add(allocated);
                            if (qty == 0) break;
                        }

                        if (qty != 0) { throw new DataException("Cannot allocated because of data changed, please update data before saved.");}
                    }
                }

                foreach (var allocatedItem in allocateList)
                {
                    allocatedItem.SetInfo(false);
                    this.Context.MedicineDeliveryDetailAllocates.Add(allocatedItem);
                }

                this.Context.SaveChanges();
                scope.Complete();
            }
        }
 /// <summary>
 /// Creates the prescription.
 /// </summary>
 /// <param name="prescription">The prescription.</param>
 /// <returns></returns>
 private MedicineDelivery CreatePrescription(Prescription prescription)
 {
     var medicineDelivery = new MedicineDelivery
                                {
                                    ClinicId = AppContext.CurrentClinic.Id,
                                    Date = DateTime.Today,
                                    PatientId = prescription.PatientId,
                                    PrescriptionId = prescription.Id
                                };
     return medicineDelivery;
 }
 public void Update(MedicineDelivery user)
 {
     throw new NotImplementedException();
 }