Exemplo n.º 1
0
        public static void AssignValues(SaleContract source, ContractInfo target)
        {
            target.SaleClientId          = source.SaleClientId;
            target.DiscountAmount        = source.DiscountAmount;
            target.ContractKey           = source.SaleContractKey;
            target.ContractId            = source.SaleContractId; //增加id
            target.OrderType             = source.OrderType;
            target.CTIME                 = source.SaleCreateTime;
            target.ContractType          = ContractViewModelType.SaleContract;
            target.ContractStatus        = source.ContractStatus;
            target.EntityPrivLevRequired = source.EntityPrivLevRequired;
            target.SaleBalancedPayment   = source.SaleBalancedPayment;
            target.SaleDeposite          = source.SaleDeposite;
            target.TotalAfterDiscount    = source.TotalAfterDiscount;
            target.CreateSysUserKey      = source.OperatorSysUser;

            target.Currency = string.IsNullOrEmpty(source.Currency) ?
                              CurrencyConfigs.SALE_DEFAULT_CURRENCY_VALUE : source.Currency;
            target.CurrencyExchangeRate = source.CurrencyExchangeRate > 0 ?
                                          source.CurrencyExchangeRate : CurrencyConfigs.GetDefaultCurrency(
                target.Currency, (int)target.ContractType);

            target.InitSaleClient();
            target.InitSaleItems();
        }
        internal string UpdateSaleContractStatusCore(ExtendedIdentityDbContext dbContext,
                                                     SaleContract contract, ContractStatus contractStatus, string userName)
        {
            if (contract == null)
            {
                return("SaleContract model为空。");
            }

            try
            {
                contract.ContractStatus = contractStatus;

                string errorMessage2 = AddOrUpdateSaleContractFinancialObjsSecond(
                    dbContext, userName, contract);
                if (!string.IsNullOrWhiteSpace(errorMessage2))
                {
                    return(errorMessage2);
                }

                int effectedRows = dbContext.SaveChanges();

                if (effectedRows < 1)
                {
                    return("修改SaleContract审核状态失败。");
                }
            }
            catch (Exception ee)
            {
                LogHelper.Error("修改SaleContract审核状态失败。", ee);
                return(ee.Message);
            }

            return(string.Empty);
        }
Exemplo n.º 3
0
        public ActionResult DeleteConfirmed(int id)
        {
            SaleContract saleContract = db.SaleContracts.Find(id);

            db.SaleContracts.Remove(saleContract);
            db.SaveChanges();
            return(RedirectToAction("Index", "FinanceEmp"));
        }
Exemplo n.º 4
0
 public bool AddSaleContract(SaleContract saleContract)
 {
     if (dal.AddSaleContract(saleContract))
     {
         return(true);
     }
     return(false);
 }
Exemplo n.º 5
0
 public bool AddSaleContract(SaleContract saleContract)
 {
     if (saleContract != null)
     {
         db.SaleContracts.Add(saleContract);
         db.SaveChanges();
         return(true);
     }
     return(false);
 }
Exemplo n.º 6
0
 public ActionResult Edit([Bind(Include = "SaleContractID,ContractDate,isCashFlag,FinanceRate,FinanceTerm,DueMonthly,BankID,CarSaleFormID")] SaleContract saleContract)
 {
     if (ModelState.IsValid)
     {
         db.Entry(saleContract).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index", "FinanceEmp"));
     }
     ViewBag.CarSaleFormID = new SelectList(db.CarSalesForms, "CarSaleFormID", "VIN", saleContract.CarSaleFormID);
     ViewBag.BankID        = new SelectList(db.PartnerBanks, "BankID", "BankID", saleContract.BankID);
     return(View(saleContract));
 }
Exemplo n.º 7
0
        // GET: SaleContracts/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            SaleContract saleContract = db.SaleContracts.Find(id);

            if (saleContract == null)
            {
                return(HttpNotFound());
            }
            return(View(saleContract));
        }
        public string UpdateSaleContractCore(ExtendedIdentityDbContext dbContext,
                                             ContractInfo model, string userName)
        {
            if (model == null)
            {
                return("SaleContract model为空。");
            }

            try
            {
                SaleContract contract = null;
                if (model.ContractId > 0)
                {
                    contract = dbContext.SaleContracts.Find(model.ContractId);
                }

                if (contract == null)
                {
                    contract = dbContext.SaleContracts.Create();
                }

                string errorMessage1 = this.UpdateSaleContractFirst(dbContext, model,
                                                                    userName, contract);
                if (!string.IsNullOrWhiteSpace(errorMessage1))
                {
                    return(errorMessage1);
                }

                string errorMessage2 = AddOrUpdateSaleContractFinancialObjsSecond(
                    dbContext, userName, contract);
                if (!string.IsNullOrWhiteSpace(errorMessage2))
                {
                    return(errorMessage2);
                }

                int effectedRows = dbContext.SaveChanges();

                if (effectedRows < 1)
                {
                    return("修改SaleContract失败。");
                }
            }
            catch (Exception ee)
            {
                LogHelper.Error("修改SaleContract失败。", ee);
                return(ee.Message);
            }

            return(string.Empty);
        }
Exemplo n.º 9
0
        // GET: SaleContracts/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            SaleContract saleContract = db.SaleContracts.Find(id);

            if (saleContract == null)
            {
                return(HttpNotFound());
            }
            ViewBag.CarSaleFormID = new SelectList(db.CarSalesForms, "CarSaleFormID", "VIN", saleContract.CarSaleFormID);
            ViewBag.BankID        = new SelectList(db.PartnerBanks, "BankID", "BankID", saleContract.BankID);
            return(View(saleContract));
        }
        private string AddOrUpdateSaleContractFinancialObjsSecond(
            ExtendedIdentityDbContext db, //SaleContract model,
            string userName, SaleContract contract)
        {
            if (contract.ContractStatus == ContractStatus.AuditPassed)//已经审核
            {
                if (contract.SaleContractId < 1)
                {//Add
                    AddSaleContractFinancialObjsSecond(db, contract);
                }
                else
                {//exists
                    UpdateSaleContractFinancialObjsSecond(db, contract);
                }
            }

            return(string.Empty);
        }
Exemplo n.º 11
0
        public void TestSaveToDB()
        {
           /* var builder = new CreditSafeLtdModelBuilder();
            var creditSafeSerializer = new XmlSerializer(typeof(CreditSafeLtdResponse), new XmlRootAttribute("xmlresponse"));
            var creditSafeResponse = (CreditSafeLtdResponse)creditSafeSerializer.Deserialize(new StringReader(response));
            var data = builder.Build(creditSafeResponse);
            data.ServiceLogID = 1;
            data.InsertDate=DateTime.UtcNow;
            data.AnnualReturnDate = data.AnnualReturnDate ?? DateTime.UtcNow;
            data.IncorporationDate = data.IncorporationDate ?? DateTime.UtcNow;
            data.AccountsFilingDate = data.AccountsFilingDate ?? DateTime.UtcNow;
            data.LatestAccountsDate = data.LatestAccountsDate ?? DateTime.UtcNow;
            data.CCJDateFrom = data.CCJDateFrom ?? DateTime.UtcNow;
            data.CCJDateTo = data.CCJDateTo ?? DateTime.UtcNow;*/
            //ParseCreditSafeLtd test = new ParseCreditSafeLtd(1);
            //test.Execute();
            //ParseCreditSafeLtd saveTest = new ParseCreditSafeLtd(1);
            //saveTest.Execute();

            /*var con = m_oDB.GetPersistent();
            con.BeginTransaction();
            var id = m_oDB.ExecuteScalar<long>(
                    con,
                    "SaveCreditSafeBaseData",
                    CommandSpecies.StoredProcedure,
                    m_oDB.CreateTableParameter<CreditSafeBaseData>("Tbl", new List<CreditSafeBaseData> { data })
                    );
            if (data.CreditRatings.Any())
            {
                foreach (var rating in data.CreditRatings)
                {
                    rating.CreditSafeBaseDataID = id;
                }
                m_oDB.ExecuteNonQuery(
                    con,
                    "SaveCreditSafeCreditRatings",
                    CommandSpecies.StoredProcedure,
                    m_oDB.CreateTableParameter<CreditSafeCreditRatings>("Tbl", data.CreditRatings)
                    );
            }
            con.Commit();*/
            var test = new SaleContract(model);
            test.Execute();
        }
        private string HandleStockItemMove(ExtendedIdentityDbContext dbContext,
                                           SaleContract contract, SaleProductItem saleItem, string userName)
        {
            if (contract.ContractStatus != ContractStatus.AuditPassed ||
                contract.OrderType == 0)
            {
                return(string.Empty);
            }

            StockItem stockItem = dbContext.StockItems.FirstOrDefault(
                p => saleItem.StockItemId.HasValue && p.StockItemId == saleItem.StockItemId.Value && p.IsAllSold == false);

            if (stockItem == null)
            {
                return("找不到对应的库存商品项,或者对应的商品项已经被销售完毕。");
            }

            if (stockItem.Quantity <= saleItem.Quantity)
            {
                stockItem.IsAllSold   = true;
                stockItem.StockStatus = StockStatus.InStockSelling;
            }
            else
            {//stockItem.Quantity > saleItem.Quantity
                //销售只卖掉一部分的情况
                StockItem newItem = dbContext.StockItems.Create();
                newItem.ProductItemId         = stockItem.ProductItemId;
                newItem.StockInDate           = stockItem.StockInDate;
                newItem.StockStatus           = StockStatus.InStock;
                newItem.StockWeight           = stockItem.StockWeight;
                newItem.StoreHouseId          = stockItem.StoreHouseId;
                newItem.StoreHouseMountNumber = stockItem.StoreHouseMountNumber;
                newItem.Quantity = stockItem.Quantity - saleItem.Quantity;

                dbContext.StockItems.Add(newItem);

                stockItem.Quantity    = saleItem.Quantity;
                stockItem.StockStatus = StockStatus.InStockSelling;
                stockItem.IsAllSold   = true;
            }

            return(string.Empty);
        }
Exemplo n.º 13
0
        public static void AssignValues(ContractInfo source, SaleContract target)
        {
            target.SaleClientId    = Convert.ToInt32(source.SaleClientId); //客户信息
            target.SaleContractKey = source.ContractKey;
            target.SaleContractId  = source.ContractId.GetValueOrDefault();
            target.OrderType       = source.OrderType.GetValueOrDefault();
            target.SaleCreateTime  = source.CTIME.GetValueOrDefault();
            //target.ContractType = ContractViewModelType.SaleContract;
            target.ContractStatus        = source.ContractStatus;
            target.EntityPrivLevRequired = source.EntityPrivLevRequired.GetValueOrDefault();
            //target.SaleBalancedPayment = source.SaleBalancedPayment.GetValueOrDefault();
            target.SaleDeposite    = source.SaleDeposite.GetValueOrDefault();
            target.OperatorSysUser = source.CreateSysUserKey;

            target.CurrencyExchangeRate = source.CurrencyExchangeRate.HasValue ?
                                          source.CurrencyExchangeRate.Value : CurrencyConfigs.GetDefaultCurrency(
                source.Currency, (int)source.ContractType);
            target.Currency = source.Currency;
        }
        public JsonResult Audit(AuditSaleContractViewModel model)
        {
            if (ModelState.IsValid)
            {
                SaleContract contract = dxContext.SaleContracts.Find(model.SaleContractId);
                if (contract != null && (contract.ContractStatus == ContractStatus.AuditNoPass ||
                                         contract.ContractStatus == ContractStatus.NotAudited))
                {
                    string errorMessage = AppBusinessManager.Instance.UpdateSaleContractStatusCore(
                        dxContext, contract, model.ContractStatus, HttpContext.User.Identity.Name);

                    if (!string.IsNullOrEmpty(errorMessage))
                    {
                        ModelState.AddModelError(string.Empty, errorMessage);
                    }
                }
                //FIXED 审核销售订单
            }
            return(Json(this.GetModelStateErrors(ModelState)));
        }
        private string AddSaleContractFirst(ExtendedIdentityDbContext dbContext,
                                            ContractInfo model, string userName, SaleContract contract)
        {
            ContractInfo.AssignValues(model, contract);
            contract.SaleCreateTime        = DateTime.Now;
            contract.OperatorSysUser       = userName;
            contract.EntityPrivLevRequired = PrivilegeManager.Instance.GetEntityPrivilegeLevel(userName);
            StringBuilder builder = new StringBuilder();

            foreach (var item in model.SaleProductItems)
            {
                if (item != null)
                {
                    SaleProductItem saleItem = dbContext.SaleProductItems.Create();
                    SaleProductItemInfo.AssignValues(item, saleItem);
                    saleItem.SaleContract = contract;

                    string errMsg = this.HandleStockItemMove(dbContext, contract, saleItem, userName);
                    if (!string.IsNullOrWhiteSpace(errMsg))
                    {
                        builder.AppendLine(errMsg);
                    }

                    dbContext.SaleProductItems.Add(saleItem);
                }
            }

            dbContext.SaleContracts.Add(contract);

            string temp = builder.ToString();

            if (!string.IsNullOrWhiteSpace(temp))
            {
                return(temp.Trim());
            }

            return(string.Empty);
        }
        private string UpdateSaleContractFirst(ExtendedIdentityDbContext dbContext,
                                               ContractInfo model, string userName, SaleContract contract)
        {
            ContractInfo.AssignValues(model, contract);
            //contract.SaleCreateTime = DateTime.Now;
            //contract.OperatorSysUser = userName;
            StringBuilder builder = new StringBuilder();

            //需要删除的:contract里面有,但是model里面没有的,这一点应该先做
            List <SaleProductItem> tobeDeleted = new List <SaleProductItem>();

            System.Diagnostics.Debug.Assert(model.OrderType == contract.OrderType);
            if (model.OrderType == 0)
            {
                var lookup0 = model.SaleProductItems.ToLookup <SaleProductItemInfo, int>(
                    sp => sp.ProductItemId.HasValue ? sp.ProductItemId.Value : 0);
                var result0 = from one in contract.SaleProducts
                              where one.ProductItemId.HasValue && !lookup0.Contains(one.ProductItemId.Value)
                              select one;
                if (result0 != null && result0.Count() > 0)
                {
                    dbContext.SaleProductItems.RemoveRange(result0);
                    tobeDeleted.AddRange(result0);
                }
            }
            else if (model.OrderType == 1)
            {
                var lookup1 = model.SaleProductItems.ToLookup <SaleProductItemInfo, int>(
                    sp => sp.ProductItemId.HasValue ? sp.ProductItemId.Value : 0);
                var result1 = from one in contract.SaleProducts
                              where one.StockItem != null && !lookup1.Contains(one.StockItem.ProductItemId)
                              select one;
                if (result1 != null && result1.Count() > 0)
                {
                    dbContext.SaleProductItems.RemoveRange(result1);
                    tobeDeleted.AddRange(result1);
                }
            }

            var toDeleteIds = tobeDeleted.ToLookup <SaleProductItem, int>(
                sp => sp.SaleProductItemId);

            foreach (var item in model.SaleProductItems)
            {
                if (item != null)
                {
                    SaleProductItem saleItem = null;
                    if (model.OrderType == 0)
                    {
                        saleItem = contract.SaleProducts.FirstOrDefault(
                            s => (s.ProductItemId.HasValue && item.ProductItemId.HasValue &&
                                  s.ProductItemId.Value == item.ProductItemId.Value));
                    }
                    else if (model.OrderType == 1)
                    {
                        saleItem = contract.SaleProducts.FirstOrDefault(
                            s => (item.ProductItemId.HasValue && s.StockItem != null &&
                                  s.StockItem.ProductItemId == item.ProductItemId.Value));
                    }

                    if (saleItem == null)
                    {
                        saleItem = dbContext.SaleProductItems.Create();
                        contract.SaleProducts.Add(saleItem);
                    }
                    SaleProductItemInfo.AssignValues(item, saleItem);
                    saleItem.SaleContract = contract;

                    string errMsg = string.Empty;
                    if (contract.OrderType == 1)
                    {//现货
                        errMsg = this.HandleStockItemMove(dbContext, contract, saleItem, userName);
                    }
                    if (!string.IsNullOrWhiteSpace(errMsg))
                    {
                        builder.AppendLine(errMsg);
                    }
                    //dbContext.SaleProductItems.Add(saleItem);
                }
            }

            //var tp = from it in contract.SaleProducts
            //         where model.SaleProductItems.Any(s =>
            //         (s.SaleProductItemId != it.ProductItemId && contract.OrderType == 0)
            //         || (s.SaleProductItemId != it.SaleProductItemId && contract.OrderType == 1))
            //         // m => m.ProductItemId == it.SaleProductItemId)
            //         select it;

            //if (tp != null && tp.Count() > 0)
            //{
            //    foreach (var i in tp)
            //    {
            //        contract.SaleProducts.Remove(i);
            //    }
            //    dbContext.SaleProductItems.RemoveRange(tp);
            //}
            //dbContext.SaleContracts.Add(contract);

            if (tobeDeleted != null && tobeDeleted.Count() > 0)
            {
                for (int k = contract.SaleProducts.Count - 1; k >= 0; k--)
                {
                    var tempk = contract.SaleProducts.ElementAt(k);
                    if (toDeleteIds.Contains(tempk.ProductItemId.GetValueOrDefault()))
                    {
                        contract.SaleProducts.Remove(tempk);
                    }
                }
            }

            string temp = builder.ToString();

            if (!string.IsNullOrWhiteSpace(temp))
            {
                return(temp.Trim());
            }

            return(string.Empty);
        }
        private void AddSaleContractFinancialObjsSecond(ExtendedIdentityDbContext db, SaleContract contract)
        {
            var receive1 = db.AccountsReceivables.Create();

            receive1.SaleContract = contract;
            receive1.PayStatus    = 0;
            receive1.EventType    = AccountingEventType.SalesDeposite;
            receive1.CTIME        = DateTime.Now;
            receive1.Amount       = contract.SaleDeposite;
            db.AccountsReceivables.Add(receive1);

            var receive2 = db.AccountsReceivables.Create();

            receive2.SaleContract = contract;
            receive2.PayStatus    = 0;
            receive2.EventType    = AccountingEventType.SalesBalancedPayment;
            receive2.CTIME        = DateTime.Now;
            receive2.Amount       = contract.SaleBalancedPayment;
            db.AccountsReceivables.Add(receive2);
        }
        //public string AddOrUpdateSaleBargain(ExtendedIdentityDbContext
        //    dbContext, SaleBargain bargain)
        //{
        //    return m_saleContractImpl.AddOrUpdateSaleContractBargainCore(dbContext, bargain);
        //}

        public string UpdateSaleContractStatusCore(ExtendedIdentityDbContext dbContext,
                                                   SaleContract contract, ContractStatus contractStatus, string userName)
        {
            return(m_saleContractImpl.UpdateSaleContractStatusCore(dbContext,
                                                                   contract, contractStatus, userName));
        }
Exemplo n.º 19
0
        public async Task <IActionResult> PutSaleContract(long id, SaleContract saleContract)
        {
            if (id != saleContract.SaleContractId)
            {
                return(BadRequest());
            }

            _context.Entry(saleContract).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();

                var receivableExist = _context.Receivables.Where(x => x.CustomerId == saleContract.CustomerId && x.IsActive == true).FirstOrDefault();

                var payableExist = _context.Payables.Where(x => x.PayableName == saleContract.SaleContractNumber && x.IsActive == true).FirstOrDefault();

                if (receivableExist != null)
                {
                    var tblAccountSaleContractExist = _context.TblAccounts.Where(x => x.AccountCode == saleContract.SaleContractNumber && x.PayableId == payableExist.PayableId).FirstOrDefault();

                    // Edit Sale Contract Account

                    tblAccountSaleContractExist.AccountCode           = saleContract.SaleContractNumber;
                    tblAccountSaleContractExist.AccountDescription    = saleContract.SaleContractNumber;
                    tblAccountSaleContractExist.AccountTitle          = saleContract.SaleContractNumber;
                    tblAccountSaleContractExist.AccountTypeId         = _context.AccountTypes.Where(x => x.AccountTypeName == "Liabilities").Select(x => x.AccountTypeId).FirstOrDefault();
                    tblAccountSaleContractExist.DateAdded             = DateTime.Now;
                    tblAccountSaleContractExist.IsActive              = true;
                    tblAccountSaleContractExist.PayableId             = payableExist.PayableId;
                    _context.Entry(tblAccountSaleContractExist).State = EntityState.Modified;
                    await _context.SaveChangesAsync();

                    var tblAccountReceivable = _context.TblAccounts.Where(x => x.AccountCode == receivableExist.ReceivableId.ToString() && x.AccountTitle == receivableExist.ReceivableName && x.ReceivablesId == receivableExist.ReceivableId).FirstOrDefault();
                    // Edit Double Entry of Receivable (DR) and Sale Contract Account (CR)

                    var accountTransactionExist = _context.AccountTransactions.Where(x => x.AccountCreditCode == tblAccountSaleContractExist.AccountCode && x.AccountCreditId == tblAccountSaleContractExist.AccountId && x.Type == _context.TransactionTypes.Where(x => x.TransactionTypeName == "SalesContract").Select(x => x.TransactionTypeId).FirstOrDefault()).FirstOrDefault();

                    accountTransactionExist.Type               = _context.TransactionTypes.Where(x => x.TransactionTypeName == "SalesContract").Select(x => x.TransactionTypeId).FirstOrDefault();
                    accountTransactionExist.AccountDebitId     = tblAccountReceivable.AccountId;
                    accountTransactionExist.AccountCreditId    = tblAccountSaleContractExist.AccountId;
                    accountTransactionExist.AccountDebitCode   = tblAccountReceivable.AccountCode;
                    accountTransactionExist.AccountCreditCode  = tblAccountSaleContractExist.AccountCode;
                    accountTransactionExist.Narration          = "Sale Contract Creation";
                    accountTransactionExist.AmountDebit        = saleContract.TotalAmount;
                    accountTransactionExist.AmountCredit       = saleContract.TotalAmount;
                    accountTransactionExist.SaleContractNumber = saleContract.SaleContractNumber;
                    accountTransactionExist.DateUpdated        = DateTime.Now;
                    accountTransactionExist.IsActive           = true;

                    _context.Entry(accountTransactionExist).State = EntityState.Modified;
                    await _context.SaveChangesAsync();

                    //AccountTransaction accountTransaction = new AccountTransaction()
                    //{
                    //    Type = _context.TransactionTypes.Where(x => x.TransactionTypeName == "SalesContract").Select(x => x.TransactionTypeId).FirstOrDefault(),
                    //    AccountDebitId = tblAccountReceivable.AccountId,
                    //    AccountCreditId = tblAccountSaleContract.AccountId,
                    //    AccountDebitCode = tblAccountReceivable.AccountCode,
                    //    AccountCreditCode = tblAccountSaleContract.AccountCode,
                    //    Narration = "Sale Contract Creation",
                    //    AmountDebit = saleContract.TotalAmount,
                    //    AmountCredit = saleContract.TotalAmount,
                    //    SaleContractNumber = saleContract.SaleContractNumber,
                    //    DateAdded = DateTime.Now,
                    //    IsActive = true
                    //};
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SaleContractExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 20
0
        public async Task <ActionResult <SaleContract> > PostSaleContract(SaleContract saleContract)
        {
            _context.SaleContracts.Add(saleContract);
            await _context.SaveChangesAsync();

            var receivableExist = _context.Receivables.Where(x => x.CustomerId == saleContract.CustomerId && x.IsActive == true).FirstOrDefault();

            //var payableExist = _context.Payables.Where(x => x.PayableName == saleContract.SaleContractNumber && x.IsActive==true).FirstOrDefault();

            if (receivableExist != null)
            {
                //var tblAccountSaleContractExist = _context.TblAccounts.Where(x => x.AccountCode == saleContract.SaleContractNumber && x.PayableId == saleContract.SaleContractId);

                //Add Payable
                Payable payable = new Payable()
                {
                    DateAdded          = DateTime.Now,
                    IsActive           = true,
                    PayableAddress     = saleContract.ShipmentDetails,
                    PayableDescription = "Sale Contract is Paybale",
                    PayableName        = saleContract.SaleContractNumber,
                    PayablePhone       = receivableExist.ReceivablePhone
                };
                _context.Payables.Add(payable);
                await _context.SaveChangesAsync();


                // Add Sale Contract Account
                TblAccount tblAccountSaleContract = new TblAccount()
                {
                    AccountCode        = saleContract.SaleContractNumber,
                    AccountDescription = saleContract.SaleContractNumber,
                    AccountTitle       = saleContract.SaleContractNumber,
                    AccountTypeId      = _context.AccountTypes.Where(x => x.AccountTypeName == "Liabilities").Select(x => x.AccountTypeId).FirstOrDefault(),
                    DateAdded          = DateTime.Now,
                    IsActive           = true,
                    PayableId          = payable.PayableId
                };
                _context.TblAccounts.Add(tblAccountSaleContract);
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    var x = 0;
                }

                var tblAccountReceivableExist = _context.TblAccounts.Where(x => x.AccountCode == receivableExist.ReceivableId.ToString() && x.AccountTitle == receivableExist.ReceivableName && x.ReceivablesId == receivableExist.ReceivableId).FirstOrDefault();
                if (tblAccountReceivableExist == null)
                {
                    //Add Receiveable Account
                    TblAccount tblAccountReceivable = new TblAccount()
                    {
                        AccountCode        = receivableExist.ReceivableId.ToString(),
                        AccountDescription = saleContract.SaleContractNumber,
                        AccountTitle       = receivableExist.ReceivableName,
                        AccountTypeId      = _context.AccountTypes.Where(x => x.AccountTypeName == "Receivables").Select(x => x.AccountTypeId).FirstOrDefault(),
                        DateAdded          = DateTime.Now,
                        IsActive           = true,
                        ReceivablesId      = receivableExist.ReceivableId
                    };
                    _context.TblAccounts.Add(tblAccountReceivable);
                    await _context.SaveChangesAsync();

                    // Add Double Entry of Receivable (DR) and Sale Contract Account (CR)
                    AccountTransaction accountTransaction = new AccountTransaction()
                    {
                        Type               = _context.TransactionTypes.Where(x => x.TransactionTypeName == "SalesContract").Select(x => x.TransactionTypeId).FirstOrDefault(),
                        AccountDebitId     = tblAccountReceivable.AccountId,
                        AccountCreditId    = tblAccountSaleContract.AccountId,
                        AccountDebitCode   = tblAccountReceivable.AccountCode,
                        AccountCreditCode  = tblAccountSaleContract.AccountCode,
                        Narration          = "Sale Contract Creation",
                        AmountDebit        = saleContract.TotalAmount,
                        AmountCredit       = saleContract.TotalAmount,
                        SaleContractNumber = saleContract.SaleContractNumber,
                        DateAdded          = DateTime.Now,
                        IsActive           = true
                    };
                    _context.AccountTransactions.Add(accountTransaction);
                    try
                    {
                        await _context.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        var x = 0;
                    }
                }
                else
                {
                    // Add Double Entry of Receivable (DR) and Sale Contract Account (CR)
                    AccountTransaction accountTransaction = new AccountTransaction()
                    {
                        Type               = _context.TransactionTypes.Where(x => x.TransactionTypeName == "SalesContract").Select(x => x.TransactionTypeId).FirstOrDefault(),
                        AccountDebitId     = tblAccountReceivableExist.AccountId,
                        AccountCreditId    = tblAccountSaleContract.AccountId,
                        AccountDebitCode   = tblAccountReceivableExist.AccountCode,
                        AccountCreditCode  = tblAccountSaleContract.AccountCode,
                        Narration          = "Sale Contract Creation",
                        AmountDebit        = saleContract.TotalAmount,
                        AmountCredit       = saleContract.TotalAmount,
                        SaleContractNumber = saleContract.SaleContractNumber,
                        SaleContractId     = saleContract.SaleContractId,
                        DateAdded          = DateTime.Now,
                        IsActive           = true
                    };
                    _context.AccountTransactions.Add(accountTransaction);
                    try
                    {
                        await _context.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        var x = 0;
                    }
                }
            }

            if (receivableExist == null)
            {
                //Add Receiveable
                var customer = await _context.Customers.FindAsync(saleContract.CustomerId);

                Receivable receivable = new Receivable()
                {
                    CustomerId            = customer.CustomerId,
                    DateAdded             = DateTime.Now,
                    IsActive              = true,
                    ReceivableAddress     = customer.Address,
                    ReceivableDescription = "Customer with Sale Contract",
                    ReceivableName        = customer.CustomerName,
                    ReceivablePhone       = customer.Contact
                };
                _context.Receivables.Add(receivable);
                await _context.SaveChangesAsync();

                //Add Payable
                Payable payable = new Payable()
                {
                    DateAdded          = DateTime.Now,
                    IsActive           = true,
                    PayableAddress     = saleContract.ShipmentDetails,
                    PayableDescription = "Sale Contract is Paybale",
                    PayableName        = saleContract.SaleContractNumber,
                    PayablePhone       = customer.Contact
                };
                _context.Payables.Add(payable);
                await _context.SaveChangesAsync();

                //Add Receiveable Account
                TblAccount tblAccountReceivable = new TblAccount()
                {
                    AccountCode        = receivable.ReceivableId.ToString(),
                    AccountDescription = saleContract.SaleContractNumber,
                    AccountTitle       = receivable.ReceivableName,
                    AccountTypeId      = _context.AccountTypes.Where(x => x.AccountTypeName == "Receivables").Select(x => x.AccountTypeId).FirstOrDefault(),
                    DateAdded          = DateTime.Now,
                    IsActive           = true,
                    ReceivablesId      = receivable.ReceivableId
                };
                _context.TblAccounts.Add(tblAccountReceivable);

                // Add Sale Contract Account
                TblAccount tblAccountSaleContract = new TblAccount()
                {
                    AccountCode        = saleContract.SaleContractNumber,
                    AccountDescription = saleContract.SaleContractNumber,
                    AccountTitle       = saleContract.SaleContractNumber,
                    AccountTypeId      = _context.AccountTypes.Where(x => x.AccountTypeName == "Liabilities").Select(x => x.AccountTypeId).FirstOrDefault(),
                    DateAdded          = DateTime.Now,
                    IsActive           = true,
                    PayableId          = payable.PayableId
                };
                _context.TblAccounts.Add(tblAccountSaleContract);

                await _context.SaveChangesAsync();

                // Add Double Entry of Receivable (DR) and Sale Contract Account (CR)
                AccountTransaction accountTransaction = new AccountTransaction()
                {
                    Type               = _context.TransactionTypes.Where(x => x.TransactionTypeName == "SalesContract").Select(x => x.TransactionTypeId).FirstOrDefault(),
                    AccountDebitId     = tblAccountReceivable.AccountId,
                    AccountCreditId    = tblAccountSaleContract.AccountId,
                    AccountDebitCode   = tblAccountReceivable.AccountCode,
                    AccountCreditCode  = tblAccountSaleContract.AccountCode,
                    Narration          = "Sale Contract Creation",
                    AmountDebit        = saleContract.TotalAmount,
                    AmountCredit       = saleContract.TotalAmount,
                    SaleContractNumber = saleContract.SaleContractNumber,
                    SaleContractId     = saleContract.SaleContractId,
                    DateAdded          = DateTime.Now,
                    IsActive           = true
                };

                _context.AccountTransactions.Add(accountTransaction);
                await _context.SaveChangesAsync();
            }


            return(CreatedAtAction("GetSaleContract", new { id = saleContract.SaleContractId }, saleContract));
        }
        private void UpdateSaleContractFinancialObjsSecond(ExtendedIdentityDbContext db, SaleContract contract)
        {
            var receive1 = db.AccountsReceivables.FirstOrDefault(
                m => m.EventType == AccountingEventType.SalesDeposite &&
                m.SaleContractId.HasValue &&
                m.SaleContractId.Value == contract.SaleContractId);

            var receive2 = db.AccountsReceivables.FirstOrDefault(
                m => m.EventType == AccountingEventType.SalesBalancedPayment &&
                m.SaleContractId.HasValue &&
                m.SaleContractId.Value == contract.SaleContractId);

            if (receive1 == null)
            {
                receive1 = db.AccountsReceivables.Create();
                receive1.SaleContractId = contract.SaleContractId;
                receive1.PayStatus      = 0;
                receive1.EventType      = AccountingEventType.SalesDeposite;
                receive1.CTIME          = DateTime.Now;
                receive1.Amount         = contract.SaleDeposite;
                db.AccountsReceivables.Add(receive1);
            }
            else
            {
                receive1.Amount = contract.SaleDeposite;
            }

            if (receive2 == null)
            {
                receive2 = db.AccountsReceivables.Create();
                receive2.SaleContractId = contract.SaleContractId;
                receive2.PayStatus      = 0;
                receive2.EventType      = AccountingEventType.SalesBalancedPayment;
                receive2.CTIME          = DateTime.Now;
                receive2.Amount         = contract.SaleBalancedPayment;
                db.AccountsReceivables.Add(receive2);
            }
            else
            {
                receive2.Amount = contract.SaleBalancedPayment;
            }
        }
Exemplo n.º 22
0
 public bool UpdateSaleContract(SaleContract saleContract)
 {
     throw new NotImplementedException();
 }