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);
                    }
                }
            }
        }
Example #2
0
        public void DeleteSalesinvoiceHeader(DateTime orderHeaderCreatedDate, EntitiesDbContext context)
        {
            List <SalesinvoicesHeader> salesinvoicesHeaderList = context.SalesinvoicesHeaders.Where(x => x.Created.ToShortDateString() == orderHeaderCreatedDate.ToShortDateString()).ToList();

            foreach (SalesinvoicesHeader salesinvoicesHeader in salesinvoicesHeaderList)
            {
                List <SalesinvoicesDetials> salesinvoices = context.SalesinvoicesDetials.Where(x => x.SalesinvoicesHeaderId == salesinvoicesHeader.Id).ToList();
                if (salesinvoices.Count == 0)
                {
                    _safeOperationsRepo.DeleteByHeaderId(salesinvoicesHeader.Id, AccountTypesEnum.Sellers, context);//Delete old record in safe related to this Seller
                    Delete(salesinvoicesHeader.Id, context);
                }
            }
        }