Example #1
0
        public async Task <int> UpdateAsync(int id, SalesReceiptModel model)
        {
            using (var transaction = _DbContext.Database.BeginTransaction())
            {
                try
                {
                    if (model.SalesReceiptDetails != null)
                    {
                        List <int> detailIds = await _DetailDbSet.Where(w => w.SalesReceiptId.Equals(id) && !w.IsDeleted).Select(s => s.Id).ToListAsync();

                        foreach (var itemId in detailIds)
                        {
                            SalesReceiptDetailModel data = model.SalesReceiptDetails.FirstOrDefault(prop => prop.Id.Equals(itemId));
                            if (data == null)
                            {
                                var detailModel = await ReadByIdAsync(itemId);

                                EntityExtension.FlagForDelete(detailModel, _IdentityService.Username, _UserAgent, true);
                            }
                            else
                            {
                                EntityExtension.FlagForUpdate(data, _IdentityService.Username, _UserAgent);
                            }
                        }

                        foreach (SalesReceiptDetailModel item in model.SalesReceiptDetails)
                        {
                            if (item.Id == 0)
                            {
                                EntityExtension.FlagForCreate(item, _IdentityService.Username, _UserAgent);
                            }
                        }
                    }

                    EntityExtension.FlagForUpdate(model, _IdentityService.Username, _UserAgent);
                    _DbSet.Update(model);

                    foreach (var item in model.SalesReceiptDetails)
                    {
                        var updateModel = new SalesReceiptUpdateModel()
                        {
                            TotalPaid = item.Paid,
                            IsPaidOff = item.IsPaidOff,
                        };

                        UpdateToSalesInvoice(item.SalesInvoiceId, updateModel);
                    }
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new Exception(e.Message);
                }
            }

            return(await _DbContext.SaveChangesAsync());
        }
Example #2
0
        public async Task <int> CreateAsync(SalesReceiptModel model)
        {
            int result = 0;

            using (var transaction = _DbContext.Database.BeginTransaction())
            {
                try
                {
                    int index = 0;

                    do
                    {
                        model.Code = CodeGenerator.Generate();
                    }while (_DbSet.Any(d => d.Code.Equals(model.Code)));
                    model.SalesReceiptDetails = model.SalesReceiptDetails.Where(s => s.Nominal > 0).ToList();

                    SalesReceiptNumberGenerator(model, index);

                    foreach (var detail in model.SalesReceiptDetails)
                    {
                        EntityExtension.FlagForCreate(detail, _IdentityService.Username, _UserAgent);
                    }

                    EntityExtension.FlagForCreate(model, _IdentityService.Username, _UserAgent);
                    _DbSet.Add(model);

                    index++;

                    result = await _DbContext.SaveChangesAsync();

                    foreach (var item in model.SalesReceiptDetails)
                    {
                        var updateModel = new SalesReceiptUpdateModel()
                        {
                            TotalPaid = item.Paid,
                            IsPaidOff = item.IsPaidOff,
                        };

                        UpdateToSalesInvoice(item.SalesInvoiceId, updateModel);
                    }

                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new Exception(e.Message);
                }
            }

            return(result);
        }
Example #3
0
        public async Task <int> DeleteAsync(int id)
        {
            using (var transaction = _DbContext.Database.BeginTransaction())
            {
                try
                {
                    SalesReceiptModel model = await _DbSet.Where(p => p.SalesReceiptDetails.Select(d => d.SalesReceiptModel.Id)
                                                                 .Contains(p.Id)).Include(p => p.SalesReceiptDetails).FirstOrDefaultAsync(d => d.Id.Equals(id) && d.IsDeleted.Equals(false));

                    model.SalesReceiptDetails = model.SalesReceiptDetails.OrderBy(s => s.Id).ToArray();

                    if (model != null)
                    {
                        SalesReceiptModel salesReceiptModel = new SalesReceiptModel();

                        salesReceiptModel = model;

                        model = await ReadByIdAsync(id);

                        foreach (var detail in model.SalesReceiptDetails)
                        {
                            EntityExtension.FlagForDelete(detail, _IdentityService.Username, _UserAgent, true);
                        }

                        EntityExtension.FlagForDelete(model, _IdentityService.Username, _UserAgent, true);
                        _DbSet.Update(model);

                        foreach (var item in model.SalesReceiptDetails)
                        {
                            var updateModel = new SalesReceiptUpdateModel()
                            {
                                TotalPaid = model.TotalPaid - item.Nominal,
                                IsPaidOff = false,
                            };

                            UpdateToSalesInvoice(item.SalesInvoiceId, updateModel);
                        }
                    }
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw new Exception(e.Message);
                }
            }
            return(await _DbContext.SaveChangesAsync());
        }
Example #4
0
        private void UpdateToSalesInvoice(int id, SalesReceiptUpdateModel model)
        {
            string salesInvoiceUri = "sales/sales-invoices/update-from-sales-receipt/";

            string Uri  = $"{APIEndpoint.Sales}{salesInvoiceUri}{id}";
            var    data = new
            {
                model.TotalPaid,
                model.IsPaidOff,
            };

            IHttpClientService httpClient = (IHttpClientService)this._serviceProvider.GetService(typeof(IHttpClientService));
            var response = httpClient.PutAsync(Uri, new StringContent(JsonConvert.SerializeObject(data).ToString(), Encoding.UTF8, General.JsonMediaType)).Result; if (!response.IsSuccessStatusCode)
            {
                throw new Exception(string.Format("{0}, {1}, {2}", response.StatusCode, response.Content, APIEndpoint.Purchasing));
            }
        }