Exemplo n.º 1
0
        public ActionResult New(FiscalDocument item)
        {
            TaxpayerBatch batch = null;

            item.Issuer = TaxpayerIssuer.TryFind (item.IssuerId);
            item.Customer = Customer.TryFind (item.CustomerId);
            var recipient = TaxpayerRecipient.TryFind (item.Recipient);

            if (recipient != null) {
                item.Recipient = recipient.Id;
                item.RecipientName = recipient.Name;
            }

            if (item.Issuer != null) {
                batch = item.Issuer.Batches.FirstOrDefault ();
            }

            if (batch == null) {
                ModelState.AddModelError ("IssuerId", Resources.BatchRangeNotFound);
            }

            if (!ModelState.IsValid) {
                return PartialView ("_Create", item);
            }

            // Store
            item.Store = WebConfig.Store;
            item.IssuedAt = item.Store.Address;
            item.IssuedLocation = item.Store.Location;

            // Issuer
            item.IssuerName = item.Issuer.Name;
            item.IssuerRegime = item.Issuer.Regime;
            item.IssuerAddress = item.Issuer.Address;

            // Recipient
            item.RecipientAddress = recipient.Address;

            // Fiscal doc's info
            item.Batch = batch.Batch;
            item.Type = batch.Type;
            item.Currency = WebConfig.BaseCurrency;
            item.ExchangeRate = 1;
            item.PaymentMethod = PaymentMethod.Cash;
            item.PaymentReference = null;
            item.CreationTime = DateTime.Now;
            item.Creator = CurrentUser.Employee;
            item.ModificationTime = item.CreationTime;
            item.Updater = item.Creator;

            using (var scope = new TransactionScope ()) {
                item.CreateAndFlush ();
            }

            return PartialView ("_CreateSuccesful", new FiscalDocument { Id = item.Id });
        }