Beispiel #1
0
        public IEnumerable <IResource> CollectIncluded(DepositInvoice depositInvoice, IncludeQuery includeQuery)
        {
            ISet <IResource> included = new HashSet <IResource>();

            var customerIncludeQuery = includeQuery.NestedInclude("customer");

            // one-relations
            if (includeQuery.Contains("order") && depositInvoice.Order != null)
            {
                included.Add(_mapper.Map <OrderDto>(depositInvoice.Order));
            }
            if (includeQuery.Contains("customer") && depositInvoice.Customer != null)
            {
                included.Add(_mapper.Map <CustomerDto>(depositInvoice.Customer, opt => opt.Items["include"] = customerIncludeQuery));
            }
            if (includeQuery.Contains("customer.honorific-prefix") && depositInvoice.Customer != null && depositInvoice.Customer.HonorificPrefix != null)
            {
                included.Add(_mapper.Map <HonorificPrefixDto>(depositInvoice.Customer.HonorificPrefix));
            }
            if (includeQuery.Contains("contact") && depositInvoice.Contact != null)
            {
                included.Add(_mapper.Map <ContactDto>(depositInvoice.Contact));
            }
            if (includeQuery.Contains("building") && depositInvoice.Building != null)
            {
                included.Add(_mapper.Map <BuildingDto>(depositInvoice.Building));
            }
            if (includeQuery.Contains("vat-rate") && depositInvoice.VatRate != null)
            {
                included.Add(_mapper.Map <VatRateDto>(depositInvoice.VatRate));
            }

            return(included);
        }
        public async Task <IHttpActionResult> NewFakeDepositInvoice(NewDepositInvoiceBM model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Zarinpal.PaymentGatewayImplementationServicePortTypeClient zp =
                new Zarinpal.PaymentGatewayImplementationServicePortTypeClient();

            var invoice = new DepositInvoice
            {
                Amount             = model.Amount,
                PaymentGateway     = PaymentGateway.Fake,
                ReceiverCampaignId = model.ReceiverCampaignId,
                AccountName        = model.Email,
                ExtraInfoJSON      = new ExtraInfoJSON {
                    InfoJSON = model.ExtraInfoJSON
                }
            };

            db.DepositInvoices.Add(invoice);
            await db.SaveChangesAsync();

            return(Ok());
        }
        public async Task <IHttpActionResult> GetDepositInvoice(long id)
        {
            DepositInvoice depositInvoice = await db.DepositInvoices.FindAsync(id);

            if (depositInvoice == null)
            {
                return(NotFound());
            }

            return(Ok(depositInvoice));
        }
Beispiel #4
0
        public async Task <DepositInvoice> UpdateAsync(DepositInvoice depositInvoice)
        {
            var query = new QuerySet();

            query.Include.Fields = new string[] { "customer", "order", "building", "contact", "order", "vat-rate" };
            var existingDepositInvoice = await _depositInvoiceDataProvider.GetByIdAsync(depositInvoice.Id, query);

            if (depositInvoice.Id != existingDepositInvoice.Id)
            {
                throw new IllegalArgumentException("IllegalAttribute", "Invoice id cannot be updated.");
            }
            if (depositInvoice.Number != existingDepositInvoice.Number)
            {
                throw new IllegalArgumentException("IllegalAttribute", "Invoice number cannot be updated.");
            }
            if (depositInvoice.BaseAmount == null)
            {
                throw new IllegalArgumentException("IllegalAttribute", "Base amount is required on deposit-invoice.");
            }
            if (depositInvoice.InvoiceDate == null)
            {
                throw new IllegalArgumentException("IllegalAttribute", "Invoice-date is required.");
            }

            // Embedded values cannot be updated since they are not exposed in the DTO

            await EmbedRelationsAsync(depositInvoice, existingDepositInvoice);

            if (depositInvoice.Customer == null)
            {
                throw new IllegalArgumentException("IllegalAttribute", "Customer is required.");
            }
            if (depositInvoice.Order == null)
            {
                throw new IllegalArgumentException("IllegalAttribute", "Order is required.");
            }
            if (depositInvoice.Contact != null && depositInvoice.Contact.Customer != null &&
                depositInvoice.Contact.Customer.Id != depositInvoice.Customer.Id)
            {
                throw new IllegalArgumentException("IllegalAttribute", $"Contact is not attached to customer {depositInvoice.Contact.Id}.");
            }
            if (depositInvoice.Building != null && depositInvoice.Building.Customer != null &&
                depositInvoice.Building.Customer.Id != depositInvoice.Customer.Id)
            {
                throw new IllegalArgumentException("IllegalAttribute", $"Building is not attached to customer {depositInvoice.Customer.Id}.");
            }

            return(await _depositInvoiceDataProvider.UpdateAsync(depositInvoice));
        }
Beispiel #5
0
        public async Task <DepositInvoice> UpdateAsync(DepositInvoice depositInvoice)
        {
            var depositInvoiceRecord = await FindByIdAsync(depositInvoice.Id);

            _mapper.Map(depositInvoice, depositInvoiceRecord);

            await EmbedCustomerAttributesAsync(depositInvoiceRecord);
            await CalculateAmountAndVatAsync(depositInvoiceRecord);

            _context.Invoices.Update(depositInvoiceRecord);
            await _context.SaveChangesAsync();

            // Deposit invoice hub doesn't need to be updated since none of the attributes can change

            return(_mapper.Map <DepositInvoice>(depositInvoiceRecord));
        }
Beispiel #6
0
        public async Task <DepositInvoice> CreateAsync(DepositInvoice depositInvoice)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var depositInvoiceRecord = _mapper.Map <DataProvider.Models.Invoice>(depositInvoice);

                    depositInvoiceRecord.Number = await _sequenceDataProvider.GetNextInvoiceNumberAsync();

                    depositInvoiceRecord.Currency = "EUR";
                    depositInvoiceRecord.Year     = (short)DateTimeOffset.UtcNow.UtcDateTime.Year;

                    await EmbedCustomerAttributesAsync(depositInvoiceRecord);
                    await CalculateAmountAndVatAsync(depositInvoiceRecord);

                    _context.Invoices.Add(depositInvoiceRecord);
                    await _context.SaveChangesAsync();

                    var depositInvoiceHub = new DepositInvoiceHub();
                    depositInvoiceHub.CustomerId       = depositInvoice.Customer.Id;
                    depositInvoiceHub.OrderId          = depositInvoice.Order.Id;
                    depositInvoiceHub.Date             = DateTimeOffset.UtcNow.UtcDateTime;
                    depositInvoiceHub.DepositInvoiceId = depositInvoiceRecord.Id;

                    var invoice = await _context.Orders.Where(o => o.Id == depositInvoice.Order.Id).Select(o => o.Invoice).FirstOrDefaultAsync();

                    if (invoice != null)
                    {
                        depositInvoiceHub.InvoiceId = invoice.Id;
                    }

                    _context.DepositInvoices.Add(depositInvoiceHub);
                    await _context.SaveChangesAsync();

                    transaction.Commit();

                    return(_mapper.Map <DepositInvoice>(depositInvoiceRecord));
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw e;
                }
            }
        }
Beispiel #7
0
        public async Task <DepositInvoice> CreateAsync(DepositInvoice depositInvoice)
        {
            if (depositInvoice.Id != 0)
            {
                throw new IllegalArgumentException("IllegalAttribute", "Deposit-invoice cannot have an id on create.");
            }
            if (depositInvoice.Number != 0)
            {
                throw new IllegalArgumentException("IllegalAttribute", "Deposit-invoice cannot have a number on create.");
            }
            if (depositInvoice.BaseAmount == null)
            {
                throw new IllegalArgumentException("IllegalAttribute", "Base amount is required on deposit-invoice.");
            }
            if (depositInvoice.InvoiceDate == null)
            {
                throw new IllegalArgumentException("IllegalAttribute", "Deposit-Invoice-date is required.");
            }
            if (depositInvoice.Customer == null)
            {
                throw new IllegalArgumentException("IllegalAttribute", "Customer is required on deposit invoice creation.");
            }
            if (depositInvoice.Order == null)
            {
                throw new IllegalArgumentException("IllegalAttribute", "Order is required on deposit invoice creation.");
            }

            // Embedded values cannot be set since they are not exposed in the DTO

            await EmbedRelationsAsync(depositInvoice);

            if (depositInvoice.Contact != null && depositInvoice.Contact.Customer.Id != depositInvoice.Customer.Id)
            {
                throw new IllegalArgumentException("IllegalAttribute", $"Contact is not attached to customer {depositInvoice.Contact.Id}.");
            }
            if (depositInvoice.Building != null && depositInvoice.Building.Customer.Id != depositInvoice.Customer.Id)
            {
                throw new IllegalArgumentException("IllegalAttribute", $"Building is not attached to customer {depositInvoice.Customer.Id}.");
            }

            return(await _depositInvoiceDataProvider.CreateAsync(depositInvoice));
        }
        public async Task <IHttpActionResult> NewZarinpalDepositInvoice(NewDepositInvoiceBM model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Zarinpal.PaymentGatewayImplementationServicePortTypeClient zp =
                new Zarinpal.PaymentGatewayImplementationServicePortTypeClient();

            var invoice = new DepositInvoice
            {
                Amount             = model.Amount,
                PaymentGateway     = PaymentGateway.ZarinPal,
                ReceiverCampaignId = model.ReceiverCampaignId,
                AccountName        = model.Email,
                ExtraInfoJSON      = new ExtraInfoJSON {
                    InfoJSON = model.ExtraInfoJSON
                }
            };

            db.DepositInvoices.Add(invoice);
            await db.SaveChangesAsync();

            //NOTE: We do not provide Zarinpal with our customers' email or mobile number
            //NOTE that we used Async version and changed the original source code, we should check whether this works
            var resp = await zp.PaymentRequestAsync("YOUR-ZARINPAL-MERCHANT-CODE", model.Amount, model.Description,
                                                    "", "", Url.Link("VerifyZarinpal", new { id = invoice.Id })
                                                    );

            var status    = resp.Body.Status;
            var Authority = resp.Body.Authority;

            if (status == 100)
            {
                return(Created("DefaultApi", new { Authority = Authority }));
            }
            else
            {
                return(InternalServerError(new Exception("Zarinpal gateway error, status:" + status.ToString())));
            }
        }
Beispiel #9
0
        // Embed relations in deposit invoice resource: reuse old relation if there is one and it hasn't changed
        private async Task EmbedRelationsAsync(DepositInvoice depositInvoice, DepositInvoice oldDepositInvoice = null)
        {
            try {
                if (depositInvoice.VatRate != null)
                {
                    if (oldDepositInvoice != null && oldDepositInvoice.VatRate != null && oldDepositInvoice.VatRate.Id == depositInvoice.VatRate.Id)
                    {
                        depositInvoice.VatRate = oldDepositInvoice.VatRate;
                    }
                    else
                    {
                        depositInvoice.VatRate = await _vatRateDataProvider.GetByIdAsync(int.Parse(depositInvoice.VatRate.Id));
                    }
                }

                // Customer cannot be updated. Take customer of oldDepositInvoice on update.
                if (oldDepositInvoice != null)
                {
                    depositInvoice.Customer = oldDepositInvoice.Customer;
                }
                else
                {
                    depositInvoice.Customer = await _customerDataProvider.GetByNumberAsync(depositInvoice.Customer.Id);
                }

                // Order cannot be updated. Take order of oldDepositInvoice on update.
                if (oldDepositInvoice != null)
                {
                    depositInvoice.Order = oldDepositInvoice.Order;
                }
                else if (depositInvoice.Order != null) // isolated invoice doesn't have an order attached
                {
                    depositInvoice.Order = await _orderDataProvider.GetByIdAsync(depositInvoice.Order.Id);
                }

                var includeCustomer = new QuerySet();
                includeCustomer.Include.Fields = new string[] { "customer" };

                // Contact can only be updated through CaseManager. Take contact of oldDepositInvoice on update.
                if (oldDepositInvoice != null)
                {
                    depositInvoice.Contact = oldDepositInvoice.Contact;
                }
                else if (depositInvoice.Contact != null)
                {
                    depositInvoice.Contact = await _contactDataProvider.GetByIdAsync(depositInvoice.Contact.Id, includeCustomer);
                }

                // Building can only be updated through CaseManager. Take building of oldDepositInvoice on update.
                if (oldDepositInvoice != null)
                {
                    depositInvoice.Building = oldDepositInvoice.Building;
                }
                else if (depositInvoice.Building != null)
                {
                    depositInvoice.Building = await _buildingDataProvider.GetByIdAsync(depositInvoice.Building.Id, includeCustomer);
                }
            }
            catch (EntityNotFoundException)
            {
                _logger.LogDebug($"Failed to find a related entity");
                throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist.");
            }
        }
Beispiel #10
0
        DepositInvoiceRelationshipsDto ITypeConverter <DepositInvoice, DepositInvoiceRelationshipsDto> .Convert(DepositInvoice source, DepositInvoiceRelationshipsDto destination, ResolutionContext context)
        {
            var relationships = new DepositInvoiceRelationshipsDto();

            relationships.Order    = GetOneRelationship <Order>("deposit-invoices", source.Id, "order", source.Order, context);
            relationships.Customer = GetOneRelationship <Customer>("deposit-invoices", source.Id, "customer", source.Customer, context);
            relationships.Building = GetOneRelationship <Building>("deposit-invoices", source.Id, "building", source.Building, context);
            relationships.Contact  = GetOneRelationship <Contact>("deposit-invoices", source.Id, "contact", source.Contact, context);
            relationships.VatRate  = GetOneRelationship <VatRate>("deposit-invoices", source.Id, "vat-rate", source.VatRate, context);
            return(relationships);
        }