Example #1
0
        public async void Create(ICloseable window)
        {
            try
            {
                if (Model.HasErrors || Model.InvoiceLines.Count == 0)
                {
                    return;
                }

                InvoiceDto invoiceDto = await(NewInvoice ? AppService.CreateAsync(Model.GetInput()) :
                                              AppService.UpdateAsync(Model.Id, Model.GetInput()));
                invoiceDto.PatientName = Model.Patient.Name;
                if (NewInvoice)
                {
                    EventAggregator.GetEvent <InvoiceAddedEvent>().Publish(invoiceDto);
                }
                else
                {
                    EventAggregator.GetEvent <InvoiceUpdatedEvent>().Publish(invoiceDto);
                }
                window.Close();
            }
            catch (SqliteException sqlEx)
            {
                ErrorText = sqlEx.Message;
            }
            catch (BusinessException busEx)
            {
                ErrorText = busEx.Message;
            }
        }
Example #2
0
        public async Task <ActionResult> EditInvoice(InvoiceHeaderDto dto)
        {
            foreach (var dtoItem in dto.InvoiceDetails)
            {
                var check = await _stockService.CheckIfExist(dtoItem.ProductId);

                var old = await _invoiceDetailsAppService.GetDetailsAsync(dtoItem.Id);

                if (old != null)
                {
                    if (dtoItem.TotalPieces > old.TotalPieces)
                    {
                        var change  = dtoItem.TotalPieces - old.TotalPieces;
                        var tAmount = dtoItem.PricePerPiece * change;
                        check.TotalPieces -= change;
                        check.Amount      -= tAmount;

                        await _stockService.UpdateAsync(check);
                    }
                    else if (dtoItem.TotalPieces < old.TotalPieces)
                    {
                        var change  = old.TotalPieces - dtoItem.TotalPieces;
                        var tAmount = dtoItem.PricePerPiece * change;
                        check.TotalPieces += change;
                        check.Amount      += tAmount;

                        await _stockService.UpdateAsync(check);
                    }
                }
                else
                {
                    check.TotalPieces -= dtoItem.TotalPieces;
                    check.Amount      -= dtoItem.Amount;

                    await _stockService.UpdateAsync(check);
                }
            }
            var updated = await _invoiceAppService.UpdateAsync(dto);

            foreach (var items in dto.InvoiceDetails)
            {
                var getCheck = await _invoiceDetailsAppService.GetAsync(new EntityDto <int>(items.Id));

                if (getCheck == null)
                {
                    var toCreate = new InvoiceDetailDto
                    {
                        InvoiceHeaderId   = dto.InvoiceId,
                        ProductId         = items.ProductId,
                        Case              = items.Case,
                        ProdCase          = items.ProdCase,
                        Box               = items.Box,
                        ProdPiece         = items.ProdPiece,
                        Piece             = items.Piece,
                        Gross             = items.Gross,
                        Discount          = items.Discount,
                        Net               = items.Net,
                        TotalProductPrice = items.TotalProductPrice,
                    };
                    await _invoiceDetailsAppService.CreateAsync(toCreate);
                }
                else if (getCheck != null)
                {
                    var toUpdate = new InvoiceDetailDto
                    {
                        Id                = items.Id,
                        CreationTime      = items.CreationTime,
                        CreatorUserId     = items.CreatorUserId,
                        TenantId          = items.TenantId,
                        InvoiceHeaderId   = items.InvoiceHeaderId,
                        ProductId         = items.ProductId,
                        Case              = items.Case,
                        ProdCase          = items.ProdCase,
                        Box               = items.Box,
                        ProdPiece         = items.ProdPiece,
                        Piece             = items.Piece,
                        Gross             = items.Gross,
                        Discount          = items.Discount,
                        Net               = items.Net,
                        TotalProductPrice = items.TotalProductPrice,
                    };
                    await _invoiceDetailsAppService.UpdateAsync(toUpdate);
                }
            }

            return(Ok(updated));
        }