public InOutcommingNoteModel GetInOutcommingNoteModel(string drugStoreCode, int currentUserId,
                                                              int?noteId, int?noteTypeId, int?taskMode)
        {
            var result = new InOutcommingNoteModel()
            {
                NoteId      = noteId ?? 0,
                CreatedById = currentUserId,
                TaskMode    = (int)TaskMode.Create,
                NoteDate    = DateTime.Now
            };

            if (taskMode.HasValue)
            {
                result.TaskMode = taskMode.Value;
            }

            if (noteId > 0) // Edit mode
            {
                var note = _dataFilterService.GetValidInOutCommingNotes(drugStoreCode)
                           .Where(i => i.MaPhieu == noteId)
                           .FirstOrDefault();
                var createdById = note.CreatedBy_UserId.Value;
                result.CreatedById = createdById;
                var createdByName = _dataFilterService.GetValidUsers(drugStoreCode)
                                    .Where(i => i.UserId == createdById).Select(i => i.TenDayDu).FirstOrDefault();

                result.CreatedByName = createdByName;
                result.Description   = note.DienGiai;
                result.PaymentAmount = (double)note.Amount;
                result.ReceiverId    = noteTypeId == (int)InOutCommingType.Incomming ?  (note.KhachHang_MaKhachHang ?? 0) : (note.NhaCungCap_MaNhaCungCap ?? 0);
                result.NoteDate      = note.NgayTao;
                result.NoteNumber    = note.SoPhieu;
                result.NoteTypeId    = note.LoaiPhieu;
                var receiverNoteIds = GetReceiverNoteId(noteId);
                if (receiverNoteIds.Length > 0)
                {
                    result.ReceiverNoteId = (receiverNoteIds.Length > 1) ? AllNoteIds : receiverNoteIds[0];
                }
            }
            else
            {
                result.NoteDate   = DateTime.Now.AddDays(-1);
                result.NoteTypeId = noteTypeId ?? (int)InOutCommingType.Incomming;
                result.NoteNumber = GetInOutCommingNoteNumber(drugStoreCode, result.NoteTypeId);
            }

            return(result);
        }
Example #2
0
        // [Audit]
        public JsonResult SaveInOutCommingNote(InOutcommingNoteModel model)
        {
            IResponseData <int> response = new ResponseData <int>();

            try
            {
                var service = IoC.Container.Resolve <IInOutCommingNoteService>();
                var data    = service.SaveInOutCommingNote(WebSessionManager.Instance.CurrentDrugStoreCode, WebSessionManager.Instance.CurrentUserId, model);
                response.SetData(data);
            }
            catch (ValidationException ex)
            {
                response.SetErrors(ex.Errors);
                response.SetStatus(HttpStatusCode.PreconditionFailed);
            }

            return(Json(response));
        }
Example #3
0
        public int SaveInOutCommingNote(string drugStoreCode, int currentUserId, InOutcommingNoteModel model)
        {
            var retVal   = 0;
            var noteDate = model.NoteDate.WithCurrentTime();

            var inOutCommingRepo = IoC.Container.Resolve <BaseRepositoryV2 <MedDbContext, PhieuThuChi> >();
            var receiverRepo     = IoC.Container.Resolve <BaseRepositoryV2 <MedDbContext, InOutPaymentReceiverNote> >();
            var deliveryRepo     = IoC.Container.Resolve <BaseRepositoryV2 <MedDbContext, PhieuXuat> >();
            var receiptRepo      = IoC.Container.Resolve <BaseRepositoryV2 <MedDbContext, PhieuNhap> >();

            if (model.NoteId > 0) // Edit mode
            {
                inOutCommingRepo.UpdateMany(i => i.MaPhieu == model.NoteId, i => new PhieuThuChi()
                {
                    ModifiedBy_UserId  = currentUserId,
                    Modified           = DateTime.Now,
                    UserProfile_UserId = currentUserId,
                    DienGiai           = model.Description,
                    Amount             = (decimal)model.PaymentAmount,
                    NgayTao            = noteDate
                });
                retVal = model.NoteId;
            }
            else // New mode
            {
                var newNote = new PhieuThuChi()
                {
                    Active              = true,
                    Created             = DateTime.Now,
                    DienGiai            = model.Description,
                    Amount              = (decimal)model.PaymentAmount,
                    LoaiPhieu           = model.NoteTypeId,
                    NhaThuoc_MaNhaThuoc = drugStoreCode,
                    CreatedBy_UserId    = currentUserId,
                    SoPhieu             = (int)model.NoteNumber,
                    NgayTao             = noteDate,
                    UserProfile_UserId  = currentUserId
                };
                if (model.NoteTypeId == (int)InOutCommingType.Incomming)
                {
                    newNote.KhachHang_MaKhachHang = model.ReceiverId;
                }
                else
                {
                    newNote.NhaCungCap_MaNhaCungCap = model.ReceiverId;
                }
                inOutCommingRepo.Add(newNote);
                inOutCommingRepo.Commit();
                retVal = newNote.MaPhieu;

                if (model.ReceiverNoteId == AllNoteIds && model.ReceiverNoteIds != null && model.ReceiverNoteIds.Length > 0) // All notes
                {
                    var paymentNotes = model.ReceiverNoteIds.Select(i => new InOutPaymentReceiverNote()
                    {
                        DrugStoreCode      = drugStoreCode,
                        InOutCommingNoteId = newNote.MaPhieu,
                        IsDeleted          = false,
                        ReceiverNoteId     = i,
                        ReceiverNoteTypeId = model.NoteTypeId == (int)InOutCommingType.Incomming ?
                                             (int)NoteInOutType.Delivery : (int)NoteInOutType.Receipt,
                        CreatedDateTime = DateTime.Now
                    }).ToList();
                    receiverRepo.InsertMany(paymentNotes);
                    if (model.NoteTypeId == (int)InOutCommingType.Incomming && model.ReceiverNoteIds.Any())
                    {
                        deliveryRepo.UpdateMany(i => model.ReceiverNoteIds.Contains(i.MaPhieuXuat), i => new PhieuXuat()
                        {
                            IsDebt = false
                        });
                    }
                    else if (model.ReceiverNoteIds.Any())
                    {
                        receiptRepo.UpdateMany(i => model.ReceiverNoteIds.Contains(i.MaPhieuNhap), i => new PhieuNhap()
                        {
                            IsDebt = false
                        });
                    }
                }
                else if (model.ReceiverNoteId > 0) // Specific note
                {
                    receiverRepo.Add(new InOutPaymentReceiverNote()
                    {
                        DrugStoreCode      = drugStoreCode,
                        InOutCommingNoteId = newNote.MaPhieu,
                        IsDeleted          = false,
                        ReceiverNoteId     = model.ReceiverNoteId,
                        ReceiverNoteTypeId = model.NoteTypeId == (int)InOutCommingType.Incomming ?
                                             (int)NoteInOutType.Delivery : (int)NoteInOutType.Receipt,
                        CreatedDateTime = DateTime.Now
                    });
                    receiptRepo.Commit();
                    if (model.NoteTypeId == (int)InOutCommingType.Incomming)
                    {
                        deliveryRepo.UpdateMany(i => model.ReceiverNoteId == i.MaPhieuXuat && model.PaymentAmountWithEsp >= (double)(i.TongTien - i.DaTra),
                                                i => new PhieuXuat()
                        {
                            IsDebt = false
                        });
                    }
                    else
                    {
                        receiptRepo.UpdateMany(i => model.ReceiverNoteId == i.MaPhieuNhap && model.PaymentAmountWithEsp >= (double)(i.TongTien - i.DaTra),
                                               i => new PhieuNhap()
                        {
                            IsDebt = false
                        });
                    }
                }
            }

            return(retVal);
        }