public void Insert(MedicinePlanDetail medicinePlanDetail)
 {
     medicinePlanDetail.Version = 0;
     medicinePlanDetail.LastUpdatedDate = DateTime.Now;
     this.Context.MedicinePlanDetails.Add(medicinePlanDetail);
     this.Context.SaveChanges();
 }
 public void Update(MedicinePlanDetail medicinePlanDetail)
 {
     try
     {
         var oldMedicinePlanDetail = this.Context.MedicinePlanDetails.FirstOrDefault(x => x.Id == medicinePlanDetail.Id);
         if (oldMedicinePlanDetail == null) return;
         oldMedicinePlanDetail.PlanId = medicinePlanDetail.PlanId;
         oldMedicinePlanDetail.MedicineId = medicinePlanDetail.MedicineId;
         oldMedicinePlanDetail.InStock = medicinePlanDetail.InStock;
         oldMedicinePlanDetail.LastMonthUsage = medicinePlanDetail.LastMonthUsage;
         oldMedicinePlanDetail.CurrentMonthUsage = medicinePlanDetail.CurrentMonthUsage;
         oldMedicinePlanDetail.Required = medicinePlanDetail.Required;
         oldMedicinePlanDetail.UnitPrice = medicinePlanDetail.UnitPrice;
         oldMedicinePlanDetail.UnitPrice = medicinePlanDetail.UnitPrice;
         oldMedicinePlanDetail.Amount = medicinePlanDetail.Amount;
         oldMedicinePlanDetail.LastUpdatedDate = DateTime.Now;
         oldMedicinePlanDetail.Version++;
         this.Context.SaveChanges();
     }
     catch (Exception ex)
     {
         throw;
     }
 }
        private void ReloadPlannedInfo()
        {
            // Medicine plan detail
            var medicines = this._medicineRepo.GetAll();
            var stocks = _warehouseRepo.GetAll(AppContext.CurrentClinic.Id);

            var startDate1 = new DateTime(this._medicinePlan.Year, this._medicinePlan.Month, 1);
            var endDate1 = startDate1.AddMonths(1).AddDays(-1);
            var currentMonthDeliverTotal = _deliveryRepo.GetMedicineDeliveryTotal(AppContext.CurrentClinic.Id, startDate1, endDate1);

            var endDate2 = startDate1.AddDays(-1);
            var startDate2 = startDate1.AddMonths(-1);
            var lastMonthDeliverTotal = _deliveryRepo.GetMedicineDeliveryTotal(AppContext.CurrentClinic.Id, startDate2, endDate2);

            // Create Medicine Planning Detail
            this._medicinePlanDetails = new List<MedicinePlanDetail>();
            foreach (var medicine in medicines)
            {
                var medicinePlanDetail = new MedicinePlanDetail {MedicineId = medicine.Id, Version = 0, MedicineName = medicine.Name, UnitName =  medicine.Define.Name, TradeName = medicine.TradeName};

                var warehouseList = stocks.Where(x => x.MedicineId == medicine.Id).ToList();
                foreach (var medicineInStock in warehouseList)
                {
                    medicinePlanDetail.InStock = medicineInStock.Volumn;
                    break;
                }

                foreach (var currentMonthUsage in currentMonthDeliverTotal)
                {
                    if (currentMonthUsage.MedicineId != medicine.Id) continue;
                    medicinePlanDetail.CurrentMonthUsage = currentMonthUsage.Quantity;
                    break;
                }

                foreach (var lastMonthUsage in lastMonthDeliverTotal)
                {
                    if (lastMonthUsage.MedicineId != medicine.Id) continue;
                    medicinePlanDetail.CurrentMonthUsage = lastMonthUsage.Quantity;
                    break;
                }

                medicinePlanDetail.Required = medicinePlanDetail.LastMonthUsage - medicinePlanDetail.CurrentMonthUsage - medicinePlanDetail.InStock;
                if (medicinePlanDetail.Required < 0) medicinePlanDetail.Required = 0;
                this._medicinePlanDetails.Add(medicinePlanDetail);
            }

            bdsPlanning.DataSource = this._medicinePlan;
            bdsPlanningDetail.DataSource = this._medicinePlanDetails;
        }
        private void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                //Insert data to MedicinePlan
                Medical.Data.Entities.MedicinePlan medicinePlan = new Medical.Data.Entities.MedicinePlan();
                medicinePlan.Year = int.Parse(cboYear.SelectedItem.ToString());
                medicinePlan.Month = int.Parse(cboMonth.SelectedItem.ToString());
                // medicinePlan.Status = Constants.Status_Wait;
                medicinePlan.Date = DateTime.Now;
                medicinePlan.Note = txtNote.Text;
                medicinePlan.ClinicId = int.Parse(cbClinic.SelectedValue.ToString());
                repMedicinePlan.Insert(medicinePlan);

                //Insert data to MedicinePlanDetail
                foreach (DataGridViewRow row in grd.Rows)
                {
                    if (ValidateRowData(row))
                    {
                        MedicinePlanDetail item = new MedicinePlanDetail();
                        item.PlanId = medicinePlan.Id;
                        item.MedicineId = int.Parse(row.Cells["MedicineId"].Value.ToString());
                        item.InStock = int.Parse(row.Cells["InStock"].Value.ToString());
                        item.LastMonthUsage = int.Parse(row.Cells["LastMonthUsage"].Value.ToString());
                        item.CurrentMonthUsage = int.Parse(row.Cells["CurrentMonthUsage"].Value.ToString());
                        item.Required = int.Parse(row.Cells["Required"].Value.ToString());
                        repMedicinePlanDetail.Insert(item);
                    }
                }

                MessageBox.Show("Tạo kế hoạch thành công!");
                grd.DataSource = new List<MedicinePlanDetail>();
            }
            catch (Exception ex)
            {

            }
        }
        public List<MedicinePlanDetail> GetByPlan(int clinicId, int year, int month)
        {
            List<MedicinePlanDetail> list = new List<MedicinePlanDetail>();
            try
            {
                var listWareHouse = (from x in Context.WareHouses where x.ClinicId == clinicId select x).ToList();

                foreach (WareHouse whItem in listWareHouse)
                {
                    MedicinePlanDetail item = new MedicinePlanDetail();
                    item.MedicineId = whItem.MedicineId;
                    item.MedicineName = whItem.MedicineName;
                    item.InStock = whItem.Volumn;
                    item.LastMonthUsage = GetMedicineInMonth(year, month - 1, 1, whItem.MedicineId);
                    item.CurrentMonthUsage = GetMedicineInMonth(year, month, 1, whItem.MedicineId);
                    list.Add(item);
                }
            }
            catch (Exception ex)
            { }

            return list;
        }
        private void InitializeData()
        {
            // Init data
            if (this._mode == ViewModes.Add)
            {
                // Medicine plan
                this._medicinePlan = new Data.Entities.MedicinePlan();
                this._medicinePlan.Date = DateTime.Today;
                this._medicinePlan.ClinicId = AppContext.CurrentClinic.Id;

                var date = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1, 0, 0, 0);
                date = date.AddMonths(1);
                this._medicinePlan.Month = date.Month;
                this._medicinePlan.Year = date.Year;
                this._medicinePlan.Status = 0;

                ReloadPlannedInfo();
            }
            else
            {
                this._medicinePlan = this._planingRepo.Get(this._planningId);
                if (this._medicinePlan == null) throw new Exception("Dự trù thuốc không tồn tại");
                bdsPlanning.DataSource = this._medicinePlan;

                this._medicinePlanDetails = this._planingDetailRepo.GetByPlanId(this._planningId);
                var medicines = this._medicineRepo.GetAll();

                foreach (var planDetail in _medicinePlanDetails)
                {
                    foreach (var medicine in medicines)
                    {
                        if (medicine.Id != planDetail.MedicineId) continue;
                        planDetail.TradeName = medicine.TradeName;
                        planDetail.MedicineName = medicine.Define.Name;

                        var medicinePlanDetail = new MedicinePlanDetail
                                                     {
                                                         MedicineId = medicine.Id,
                                                         Version = 0,
                                                         MedicineName = medicine.Name,
                                                         UnitName = medicine.Define.Name,
                                                         TradeName = medicine.TradeName
                                                     };
                    }

                }

                bdsPlanningDetail.DataSource = this._medicinePlanDetails;

                this.txtYear.Enabled = false;
                this.txtMonth.Enabled = false;
            }
        }