Example #1
0
 public bool DeleteData(int userId, int id, out Library.DTO.Notification notification)
 {
     notification = new Library.DTO.Notification {
         Type = Library.DTO.NotificationType.Success
     };
     try
     {
         //check permission on booking
         if (fwFactory.CheckPurchasingCreditNotePermission(userId, id) == 0)
         {
             throw new Exception("You do not have access permission on this invoice");
         }
         using (PurchasingCreditNoteEntities context = CreateContext())
         {
             var dbItem = context.PurchasingCreditNote.Where(o => o.PurchasingCreditNoteID == id).FirstOrDefault();
             foreach (var item in dbItem.PurchasingCreditNoteDetail.ToArray())
             {
                 context.PurchasingCreditNoteDetail.Remove(item);
             }
             foreach (var item in dbItem.PurchasingCreditNoteSparepartDetail.ToArray())
             {
                 context.PurchasingCreditNoteSparepartDetail.Remove(item);
             }
             foreach (var item in dbItem.PurchasingCreditNoteExtendDetail.ToArray())
             {
                 context.PurchasingCreditNoteExtendDetail.Remove(item);
             }
             context.PurchasingCreditNote.Remove(dbItem);
             context.SaveChanges();
         }
         return(true);
     }
     catch (Exception ex)
     {
         notification.Type    = Library.DTO.NotificationType.Error;
         notification.Message = ex.Message;
         notification.DetailMessage.Add(ex.Message);
         if (ex.GetBaseException() != null)
         {
             notification.DetailMessage.Add(ex.GetBaseException().Message);
         }
         return(false);
     }
 }
Example #2
0
        public bool UpdatePurchasingCreditNote(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            DTO.PurchasingCreditNote dtoCreditNote = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.PurchasingCreditNote>();
            dtoCreditNote.UpdatedBy = userId;
            try
            {
                using (PurchasingCreditNoteEntities context = CreateContext())
                {
                    PurchasingCreditNote dbItem;
                    if (id > 0)
                    {
                        //check permission on booking
                        if (fwFactory.CheckPurchasingCreditNotePermission(userId, id) == 0)
                        {
                            throw new Exception("You do not have access permission on this invoice");
                        }

                        dbItem = context.PurchasingCreditNote.Where(o => o.PurchasingCreditNoteID == id).FirstOrDefault();

                        //check credit note is confirmed
                        if (dbItem != null && dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed == true)
                        {
                            throw new Exception("Credit note has been confirmed. You can not change.");
                        }
                    }
                    else
                    {
                        //check permission
                        if (dtoCreditNote.PurchasingInvoiceID.HasValue || dtoCreditNote.SupplierID.HasValue)
                        {
                            //check permission on invoice
                            if (dtoCreditNote.PurchasingInvoiceID.HasValue && fwFactory.CheckPurchasingInvoicePermission(userId, dtoCreditNote.PurchasingInvoiceID.Value) == 0)
                            {
                                throw new Exception("You do not have access permission on this invoice");
                            }
                            if (dtoCreditNote.SupplierID.HasValue && fwFactory.CheckSupplierPermission(userId, dtoCreditNote.SupplierID.Value) == 0)
                            {
                                throw new Exception("You do not have access permission on this factory");
                            }
                        }
                        else
                        {
                            throw new Exception("There are not invoice or supplier to check permission to create credit note");
                        }
                        dbItem = new PurchasingCreditNote();
                        context.PurchasingCreditNote.Add(dbItem);
                    }

                    if (dbItem != null)
                    {
                        //check concurrency
                        if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoCreditNote.ConcurrencyFlag_String)))
                        {
                            throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT);
                        }

                        //check put in factory balance ?
                        if (string.IsNullOrEmpty(dtoCreditNote.Season))
                        {
                            throw new Exception("You need fill-in season to create credit note");
                        }
                        if (context.PurchasingCreditNoteMng_function_CheckCreditNotePutInBalance(dtoCreditNote.PurchasingInvoiceID, dtoCreditNote.FactoryID, dtoCreditNote.Season).FirstOrDefault().Value > 0)
                        {
                            throw new Exception("This supplier was make balance sheet in season " + dtoCreditNote.Season + ". You can not create credit note");
                        }

                        //check permission product item
                        List <int?> supplierIDs = fwFactory.GetListSupplierByUser(userId);

                        List <int?> dtoInvoiceDetailIDs    = dtoCreditNote.PurchasingCreditNoteDetails.Where(o => o.PurchasingCreditNoteDetailID < 0).Select(s => s.PurchasingInvoiceDetailID).ToList();
                        List <int?> dtoCreditNoteDetailIDs = dtoCreditNote.PurchasingCreditNoteDetails.Where(o => o.PurchasingCreditNoteDetailID > 0).Select(s => s.PurchasingCreditNoteDetailID).ToList();
                        if (dtoInvoiceDetailIDs.Count() > 0 && context.PurchasingCreditNoteMng_PurchasingInvoiceDetail_View.Where(o => dtoInvoiceDetailIDs.Contains(o.PurchasingInvoiceDetailID)).Select(s => s.SupplierID).ToList().Except(supplierIDs).Count() > 0)
                        {
                            throw new Exception("You do not have access permission on these products that you added to credit note");
                        }
                        if (dtoCreditNoteDetailIDs.Count() > 0 && context.PurchasingCreditNoteMng_PurchasingCreditNoteDetail_View.Where(o => dtoCreditNoteDetailIDs.Contains(o.PurchasingCreditNoteDetailID)).Select(s => s.SupplierID).ToList().Except(supplierIDs).Count() > 0)
                        {
                            throw new Exception("You do not have access permission on these products that you added to credit note");
                        }

                        //check permission sparepart detail
                        List <int?> dtoInvoiceSparepartDetailIDs    = dtoCreditNote.PurchasingCreditNoteSparepartDetails.Where(o => o.PurchasingCreditNoteSparepartDetailID < 0).Select(s => s.PurchasingInvoiceSparepartDetailID).ToList();
                        List <int?> dtoCreditNoteSparepartDetailIDs = dtoCreditNote.PurchasingCreditNoteSparepartDetails.Where(o => o.PurchasingCreditNoteSparepartDetailID > 0).Select(s => s.PurchasingCreditNoteSparepartDetailID).ToList();
                        if (dtoInvoiceSparepartDetailIDs.Count() > 0 && context.PurchasingCreditNoteMng_PurchasingInvoiceSparepartDetail_View.Where(o => dtoInvoiceSparepartDetailIDs.Contains(o.PurchasingInvoiceSparepartDetailID)).Select(s => s.SupplierID).ToList().Except(supplierIDs).Count() > 0)
                        {
                            throw new Exception("You do not have access permission on these sparepart that you added to credit note");
                        }
                        if (dtoCreditNoteSparepartDetailIDs.Count() > 0 && context.PurchasingCreditNoteMng_PurchasingCreditNoteSparepartDetail_View.Where(o => dtoCreditNoteSparepartDetailIDs.Contains(o.PurchasingCreditNoteSparepartDetailID)).Select(s => s.SupplierID).ToList().Except(supplierIDs).Count() > 0)
                        {
                            throw new Exception("You do not have access permission on these sparepart that you added to credit note");
                        }

                        //check quantity
                        foreach (var item in dtoCreditNote.PurchasingCreditNoteDetails.Where(o => o.Quantity.HasValue))
                        {
                            if (item.UnitPrice.HasValue && item.UnitPrice.Value < 0)
                            {
                                throw new Exception("Price must be not negavitve");
                            }
                            if (item.Quantity > 0)
                            {
                                throw new Exception("Quantity in credit note must be negative");
                            }
                            else
                            {
                                int?compareValue = 0;
                                if (item.PurchasingCreditNoteDetailID < 0)
                                {
                                    compareValue = item.Quantity;  // -2
                                }
                                else
                                {
                                    var objOldValue = context.PurchasingCreditNoteDetail.Where(o => o.PurchasingCreditNoteDetailID == item.PurchasingCreditNoteDetailID).FirstOrDefault(); //-2
                                    compareValue = item.Quantity - objOldValue.Quantity;                                                                                                   // -10 - (-2) = -8
                                }

                                var objRemaining = context.PurchasingCreditNoteMng_PurchasingInvoiceDetail_View.Where(o => o.PurchasingInvoiceDetailID == item.PurchasingInvoiceDetailID);
                                int?remaintQnt   = (objRemaining != null && objRemaining.Count() > 0 ? objRemaining.FirstOrDefault().Quantity : 0);
                                if (-compareValue > remaintQnt)
                                {
                                    throw new Exception("Credit note quantity must be less than purchasing invoice quantity remaining");
                                }
                            }
                        }

                        //check quantity
                        foreach (var item in dtoCreditNote.PurchasingCreditNoteSparepartDetails.Where(o => o.Quantity.HasValue))
                        {
                            if (item.UnitPrice.HasValue && item.UnitPrice.Value < 0)
                            {
                                throw new Exception("Price must be not negavitve");
                            }
                            if (item.Quantity > 0)
                            {
                                throw new Exception("Quantity in credit note must be negative");
                            }
                            else
                            {
                                int?compareValue = 0;
                                if (item.PurchasingCreditNoteSparepartDetailID < 0)
                                {
                                    compareValue = item.Quantity;  // -2
                                }
                                else
                                {
                                    var objOldValue = context.PurchasingCreditNoteSparepartDetail.Where(o => o.PurchasingCreditNoteSparepartDetailID == item.PurchasingCreditNoteSparepartDetailID).FirstOrDefault(); //-2
                                    compareValue = item.Quantity - objOldValue.Quantity;                                                                                                                              // -10 - (-2) = -8
                                }

                                var objRemaining = context.PurchasingCreditNoteMng_PurchasingInvoiceSparepartDetail_View.Where(o => o.PurchasingInvoiceSparepartDetailID == item.PurchasingInvoiceSparepartDetailID);
                                int?remaintQnt   = (objRemaining != null && objRemaining.Count() > 0 ? objRemaining.FirstOrDefault().Quantity : 0);
                                if (-compareValue > remaintQnt)
                                {
                                    throw new Exception("Credit note quantity must be less than purchasing invoice quantity remaining");
                                }
                            }
                        }

                        foreach (var item in dtoCreditNote.PurchasingCreditNoteExtendDetails.Where(o => o.Amount.HasValue))
                        {
                            if (item.Amount > 0)
                            {
                                throw new Exception("Amount in credit note must be negative");
                            }
                        }

                        converter.DTO2DB_PurchasingCreditNote(dtoCreditNote, ref dbItem);

                        //remove orphan item
                        context.PurchasingCreditNoteDetail.Local.Where(o => o.PurchasingCreditNote == null).ToList().ForEach(o => context.PurchasingCreditNoteDetail.Remove(o));
                        context.PurchasingCreditNoteSparepartDetail.Local.Where(o => o.PurchasingCreditNote == null).ToList().ForEach(o => context.PurchasingCreditNoteSparepartDetail.Remove(o));
                        context.PurchasingCreditNoteExtendDetail.Local.Where(o => o.PurchasingCreditNote == null).ToList().ForEach(o => context.PurchasingCreditNoteExtendDetail.Remove(o));

                        context.SaveChanges();
                        dtoItem = GetPurchasingCreditNote(userId, dbItem.PurchasingCreditNoteID, out notification);
                    }
                    else
                    {
                        throw new Exception(Library.Helper.TEXT_UPDATED_UNSUCCESS_NOTFOUND);
                    }
                    return(true);
                }
            }
            catch (Exception ex) {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                notification.DetailMessage.Add(ex.Message);
                if (ex.GetBaseException() != null)
                {
                    notification.DetailMessage.Add(ex.GetBaseException().Message);
                }
                return(false);
            }
        }