private List <SalesinvoicesHeader> PrepareSalesinvoicesEntity(OrderDTO orderHeader)
        {
            List <SalesinvoicesHeader> salesinvoicesHeaderList = new List <SalesinvoicesHeader>();
            ///Prepare Salesinvoices Details
            decimal total = 0;

            foreach (OrderDetails item in orderHeader.OrderDetails)
            {
                total = 0;
                ICollection <SalesinvoicesDetials> salesinvoicesDetials = new List <SalesinvoicesDetials>();
                salesinvoicesDetials.Add(new SalesinvoicesDetials()
                {
                    Quantity  = item.Quantity,
                    Weight    = item.Weight,
                    Price     = item.SellingPrice,
                    Byaa      = 3 * item.Quantity,
                    Mashal    = 3 * item.Quantity,
                    OrderDate = orderHeader.OrderHeader.Created,
                    FarmerId  = orderHeader.OrderHeader.FarmerId
                });
                total += (item.Weight * item.SellingPrice) + (AppSettings.MashalRate + AppSettings.ByaaRate) * item.Quantity;
                ///Prepare Salesinvoices Header
                SalesinvoicesHeader salesinvoicesHeader = new SalesinvoicesHeader()
                {
                    SalesinvoicesDate        = orderHeader.OrderHeader.OrderDate,
                    SellerId                 = item.SellerId,
                    SalesinvoicesDetialsList = salesinvoicesDetials,
                    ByaaTotal                = AppSettings.ByaaRate * item.Quantity,
                    MashalTotal              = AppSettings.MashalRate * item.Quantity,
                };
                salesinvoicesHeaderList.Add(salesinvoicesHeader);
            }
            return(salesinvoicesHeaderList);
        }
Esempio n. 2
0
 private IEnumerable <SalesinvoicesDetials> SetSalesinvoicesHeaderId(SalesinvoicesHeader salesinvoicesHeader, IEnumerable <SalesinvoicesDetials> salesinvoicesDetails)
 {
     foreach (var item in salesinvoicesDetails)
     {
         item.SalesinvoicesHeaderId = salesinvoicesHeader.Id;
         item.Id = 0;
     }
     return(salesinvoicesDetails);
 }
Esempio n. 3
0
        public bool Delete(long id, EntitiesDbContext context)
        {
            SalesinvoicesHeader salesinvoicesHeader = GetById(id, context);

            DeleteSalesinvoicesDetials(id, context);
            context.SalesinvoicesHeaders.Remove(salesinvoicesHeader);
            context.SaveChanges();
            return(true);
        }
Esempio n. 4
0
        public bool Update(SalesinvoicesHeader salesinvoicesHeader, EntitiesDbContext context)
        {
            context.SalesinvoicesHeaders.Update(salesinvoicesHeader);
            context.SaveChanges();

            //DeleteSalesinvoicesDetials(salesinvoicesHeader.Id, context);
            //SetSalesinvoicesHeaderId(salesinvoicesHeader, salesinvoicesHeader.SalesinvoicesDetialsList);
            //AddSalesinvoicesDetials(salesinvoicesHeader.SalesinvoicesDetialsList, context);
            //UpdateSalesinvoiceTotal(salesinvoicesHeader.SalesinvoicesDetialsList, context);
            return(true);
        }
 private Safe PrepareSellerSafeEntity(SalesinvoicesHeader entity, decimal total, long orderId)
 {
     return(new Safe()
     {
         Date = entity.SalesinvoicesDate,
         AccountId = entity.SellerId,
         AccountTypeId = (int)AccountTypesEnum.Sellers,
         Outcoming = total,
         Notes = $" رقم الكشف :{entity.Id}",
         IsHidden = true,
         HeaderId = entity.Id,
         OrderId = orderId
     });
 }
        public bool Add(OrderDTO entity)
        {
            var options = GetOptions();

            using (var context = new EntitiesDbContext(options))
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        //[1] Save Order (Header and Details)
                        _orderHeaderOperationsRepo.Add(entity.OrderHeader, context);                        //Save in OrderHeader Table (Master Table)
                        entity.OrderDetails = SetOrderHeaderId(entity.OrderHeader.Id, entity.OrderDetails); //set OrderHeaderId to OrderDetails table
                        _orderDetailsOperationsRepo.AddRange(entity.OrderDetails, context);                 //Save in OrderDetails table (details for master table)

                        //[2] Save Purecchase pill
                        PurechasesHeader purechasesHeader = PreparePurechasesEntity(entity); //Prepare PurchaseEntity(Header and details for Cleint)
                        _purechasesOperationsRepo.Add(purechasesHeader, context);            //Add Purchase(Header and Details)

                        //[3] Save farmer safe in Safe Entity as hidden rows
                        Safe farmerSafe = PrepareFarmerSafeEntity(purechasesHeader, entity.OrderHeader.Id); //Prepare Safe for Client
                        _safeOperationsRepo.Add(farmerSafe, context);                                       //Add Client Safe row

                        //[4]Save Order_Purechase
                        Order_Purechase order_Purechase = PrepareOrder_Purechase(entity.OrderHeader.Id, purechasesHeader.Id);
                        _order_PurechaseOperationsRepo.Add(order_Purechase, context);

                        //[5] Save Salesinvoice pill
                        var salesinvoicesHeaderList = PrepareSalesinvoicesEntity(entity);//Prepare Salesinvoice(Header and Details)
                        foreach (var salesinvoicesHeader in salesinvoicesHeaderList)
                        {
                            SalesinvoicesHeader updatedSalesinvoicesHeader = _salesinvoicesOperationsRepo.Add(salesinvoicesHeader, entity.OrderHeader.Id, context); //Save in Salesinvoice(Header and Details) and update Total
                            _safeOperationsRepo.DeleteByHeaderId(updatedSalesinvoicesHeader.Id, AccountTypesEnum.Sellers, context);                                 //Delete old record in safe related to this Seller
                            var sellerSafe = PrepareSellerSafeEntity(updatedSalesinvoicesHeader, updatedSalesinvoicesHeader.Total, entity.OrderHeader.Id);
                            _safeOperationsRepo.Add(sellerSafe, context);
                        }
                        transaction.Commit();
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        return(false);
                    }
                }
            }
        }
Esempio n. 7
0
        public SalesinvoicesHeader Add(SalesinvoicesHeader salesinvoicesHeader, long orderHeaderId, EntitiesDbContext context)
        {
            //To-Do
            //Check if Seller has at least one salesinvoice at this day or no
            SalesinvoicesHeader exsistedSalesHeader = GetSalesinvoiceHeaderByDateAndSellerId(salesinvoicesHeader.SalesinvoicesDate, salesinvoicesHeader.SellerId, context);

            if (exsistedSalesHeader == null)//It is the first salesinvoice to this Seller in this day
            {
                context.Entry(salesinvoicesHeader).State = EntityState.Added;
                context.SaveChanges();
            }
            if (salesinvoicesHeader.Id == 0)
            {
                salesinvoicesHeader.Id = exsistedSalesHeader.Id;
            }

            SetSalesinvoicesHeaderId(salesinvoicesHeader, salesinvoicesHeader.SalesinvoicesDetialsList); //Set SalesinvoiceHeaderId
            AddSalesinvoicesDetials(salesinvoicesHeader.SalesinvoicesDetialsList, context);              //Add SalesinvoiceDetails
            return(UpdateSalesinvoiceTotal(salesinvoicesHeader.SalesinvoicesDetialsList, context));      //Return updated salesinvoiceHeader
        }
Esempio n. 8
0
        private SalesinvoicesHeader UpdateSalesinvoiceTotal(IEnumerable <SalesinvoicesDetials> salesinvoicesDetialsList, EntitiesDbContext context)
        {
            decimal total         = 0; //Calculate Total
            decimal mashalTotal   = 0; //Calculate Mashal Total
            decimal byaaTotal     = 0; //Calculate Byaa Total
            long    salesHeaderId = 0;

            foreach (SalesinvoicesDetials item in salesinvoicesDetialsList)
            {
                salesHeaderId = item.SalesinvoicesHeaderId;
                total        += (item.Weight * item.Price) + (AppSettings.MashalRate + AppSettings.ByaaRate) * item.Quantity;
                mashalTotal  += AppSettings.MashalRate * item.Quantity;
                byaaTotal    += AppSettings.ByaaRate * item.Quantity;
            }
            SalesinvoicesHeader salesinvoicesHeader = context.SalesinvoicesHeaders.SingleOrDefault(x => x.Id == salesHeaderId);

            salesinvoicesHeader.Total       = salesinvoicesHeader.Total + total;
            salesinvoicesHeader.MashalTotal = salesinvoicesHeader.MashalTotal + mashalTotal;
            salesinvoicesHeader.ByaaTotal   = salesinvoicesHeader.ByaaTotal + byaaTotal;

            context.SaveChanges();
            return(salesinvoicesHeader);
        }
        public bool Update(OrderDTO entity)
        {
            var options = GetOptions();

            using (var context = new EntitiesDbContext(options))
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        //[1] Update Order (Master and Details)
                        _orderHeaderOperationsRepo.Update(entity.OrderHeader, context);                       //Update Order table(Master)
                        _orderHeaderOperationsRepo.DeleteRelatedOrderDetials(entity.OrderHeader.Id, context); //Delete order details related to this order header
                        SetOrderHeaderId(entity.OrderHeader.Id, entity.OrderDetails);
                        _orderDetailsOperationsRepo.AddRange(entity.OrderDetails, context);                   //Adding of new Order Details

                        //[2] Update Purechase (Master and Details)
                        PurechasesHeader purechasesHeader = PreparePurechasesEntity(entity);
                        var orderHeader = _order_PurechaseOperationsRepo.GetByOrderHeaderId(entity.OrderHeader.Id);
                        if (orderHeader != null && orderHeader.PurechasesHeaderId != 0)
                        {
                            purechasesHeader.Id = orderHeader.PurechasesHeaderId;
                            _purechasesOperationsRepo.Update(purechasesHeader, context);
                        }
                        else
                        {
                            _purechasesOperationsRepo.Add(purechasesHeader, context);
                        }

                        //[3] Update Client Safe
                        _safeOperationsRepo.DeleteByOrderId(entity.OrderHeader.Id, context);
                        var safe = PrepareFarmerSafeEntity(purechasesHeader, entity.OrderHeader.Id);
                        _safeOperationsRepo.Add(safe, context);

                        //[4]Update Order_Purechase
                        Order_Purechase order_Purechase = PrepareOrder_Purechase(entity.OrderHeader.Id, purechasesHeader.Id);
                        _order_PurechaseOperationsRepo.Update(order_Purechase);

                        //[5] Update Salesinvoice
                        List <SalesinvoicesDetials> deletedSalesinvoicesDetials = _salesinvoicesOperationsRepo.DeleteSalesinvoiceDetails(entity.OrderHeader, context); //Delete SalesinvoiceDetails
                        UpdateSalesInvoicTotal(deletedSalesinvoicesDetials, context);
                        _salesinvoicesOperationsRepo.DeleteSalesinvoiceHeader(entity.OrderHeader.Created, context);                                                    //Delete Old Salesinvoice related to this Order
                        var salesinvoicesHeaderList = PrepareSalesinvoicesEntity(entity);                                                                              //Prepare Salesinvoice(Header and Details)
                        foreach (var salesinvoicesHeader in salesinvoicesHeaderList)
                        {
                            SalesinvoicesHeader updatedSalesinvoicesHeader = _salesinvoicesOperationsRepo.Add(salesinvoicesHeader, entity.OrderHeader.Id, context); //Save in Salesinvoice(Header and Details) and update Total
                            _safeOperationsRepo.DeleteByHeaderId(salesinvoicesHeader.Id, AccountTypesEnum.Sellers, context);                                        //Delete old record in safe related to this Seller
                            var sellerSafe = PrepareSellerSafeEntity(updatedSalesinvoicesHeader, updatedSalesinvoicesHeader.Total, entity.OrderHeader.Id);
                            _safeOperationsRepo.Add(sellerSafe, context);
                        }

                        transaction.Commit();
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        LogFile.WriteLog(ex.Message);
                        transaction.Rollback();
                        return(false);
                    }
                }
            }
        }
Esempio n. 10
0
 public bool UpdateInPrinting(SalesinvoicesHeader entity)
 {
     entity.IsPrinted = true;
     return(Update(entity));
 }
 public JsonResult UpdateInPrinting(SalesinvoicesHeader salesinvoicesHeader)
 {
     return(Json(_salesinvoicesOperationsRepo.UpdateInPrinting(salesinvoicesHeader)));
 }