Beispiel #1
0
        public ResponseBase InsertBTPDay(PhaseDailyModel model)
        {
            var rs = new ResponseBase();

            rs.IsSuccess = true;
            try
            {
                using (var db = new PMSEntities())
                {
                    var now           = DateTime.Now;
                    int oldQuantities = 0;
                    var obj           = db.P_PhaseDaily.FirstOrDefault(x =>
                                                                       x.CreatedDate.Year == now.Year &&
                                                                       x.CreatedDate.Month == now.Month &&
                                                                       x.CreatedDate.Day == now.Day &&
                                                                       x.PhaseId == model.PhaseId);
                    if (obj == null)
                    {
                        obj = new P_PhaseDaily();
                        Parse.CopyObject(model, ref obj);
                        obj.CreatedDate = DateTime.Now;
                        db.P_PhaseDaily.Add(obj);
                    }
                    else
                    {
                        oldQuantities = obj.Quantity;
                        obj.Quantity  = model.Quantity;
                    }
                    db.SaveChanges();
                    var total = db.P_Phase_Assign_Log.FirstOrDefault(x => x.PhaseId == model.PhaseId && x.AssignId == model.AssignId);
                    if (total == null)
                    {
                        total = new P_Phase_Assign_Log()
                        {
                            Quantity    = model.Quantity,
                            AssignId    = model.AssignId,
                            PhaseId     = model.PhaseId,
                            CreatedDate = obj.CreatedDate
                        };
                        db.P_Phase_Assign_Log.Add(total);
                    }
                    else
                    {
                        total.Quantity -= oldQuantities;
                        total.Quantity += model.Quantity;
                    }

                    db.SaveChanges();
                    UpdateLKBTP_HC(model.AssignId, db);
                    rs.IsSuccess = true;
                    rs.Messages.Add(new Message()
                    {
                        Title = "Thông Báo", msg = "Lưu thành công."
                    });
                }
            }
            catch (Exception ex)
            { throw ex; }
            return(rs);
        }
        public bool InsertPhaseQuantities(P_PhaseDaily model, int csp)
        {
            try
            {
                using (var db = new PMSEntities())
                {
                    var phaseLog = db.P_Phase_Assign_Log.FirstOrDefault(x => x.AssignId == csp && x.PhaseId == model.PhaseId);
                    if (phaseLog != null)
                    {
                        switch (model.CommandTypeId)
                        {
                        case (int)eCommandRecive.ProductIncrease: phaseLog.Quantity += model.Quantity; break;

                        case (int)eCommandRecive.ProductReduce: phaseLog.Quantity -= model.Quantity; break;
                        }
                    }
                    else
                    {
                        var newObj = new P_Phase_Assign_Log()
                        {
                            Quantity    = 0,
                            PhaseId     = model.PhaseId,
                            AssignId    = csp,
                            CreatedDate = DateTime.Now
                        };
                        switch (model.CommandTypeId)
                        {
                        case (int)eCommandRecive.ProductIncrease: newObj.Quantity += model.Quantity; break;

                        case (int)eCommandRecive.ProductReduce: newObj.Quantity -= model.Quantity; break;
                        }
                        db.P_Phase_Assign_Log.Add(newObj);
                    }
                    //  model.CreatedDate = DateTime.Now;
                    model.AssignId = csp;
                    db.P_PhaseDaily.Add(model);
                    db.SaveChanges();
                    return(true);
                }
            }
            catch (Exception ex)
            {
            }
            return(false);
        }