Esempio n. 1
0
        public void SellingReturnRecordAdd(SellingPaymentReturnRecordModel model)
        {
            var net = model.CurrentReturnAmount - model.PrevReturnAmount;

            if (net == 0)
            {
                return;
            }

            var returnRecord = _mapper.Map <SellingPaymentReturnRecord>(model);

            Context.SellingPaymentReturnRecord.Add(returnRecord);

            if (model.AccountId != null)
            {
                BalanceSubtract(model.AccountId.Value, net);
            }
            Context.SaveChanges();
        }
Esempio n. 2
0
        public async Task <DbResponse <int> > BillUpdated(SellingUpdatePostModel model, IUnitOfWork db)
        {
            var response = new DbResponse <int>();

            try
            {
                var stocks      = new List <ProductStock>();
                var productLogs = new List <ProductLogAddModel>();

                var accountCostPercentage = db.Account.GetCostPercentage(model.AccountId.GetValueOrDefault());
                if (model.AddedProductCodes != null)
                {
                    if (model.AddedProductCodes.Any())
                    {
                        var addedStocks = await db.ProductStocks.SellingStockFromCodesAsync(model.AddedProductCodes);

                        addedStocks = addedStocks.Select(s =>
                        {
                            s.IsSold = true;
                            return(s);
                        }).ToList();
                        stocks.AddRange(addedStocks);

                        var logs = addedStocks.Select(c => new ProductLogAddModel
                        {
                            SellingId                = model.SellingId,
                            ProductStockId           = c.ProductStockId,
                            ActivityByRegistrationId = model.UpdateRegistrationId,
                            Details   = $"Product Selling by bill changed",
                            LogStatus = ProductLogStatus.Sale
                        }).ToList();

                        productLogs.AddRange(logs);
                    }
                }

                var selling = Context.Selling.Include(s => s.SellingList).FirstOrDefault(s => s.SellingId == model.SellingId);
                if (selling == null)
                {
                    response.IsSuccess = false;
                    response.Message   = "Not found";
                    return(response);
                }

                var returnRecord = new SellingPaymentReturnRecordModel
                {
                    PrevReturnAmount    = selling.SellingReturnAmount,
                    CurrentReturnAmount = model.SellingReturnAmount,
                    AccountId           = model.AccountId,
                    SellingId           = selling.SellingId,
                    RegistrationId      = model.UpdateRegistrationId
                };



                selling.SellingTotalPrice         = model.SellingTotalPrice;
                selling.SellingDiscountAmount     = model.SellingDiscountAmount;
                selling.SellingReturnAmount       = model.SellingReturnAmount;
                selling.SellingPaidAmount        += model.PaidAmount;
                selling.AccountTransactionCharge += model.AccountTransactionCharge;
                selling.SellingAccountCost       += model.PaidAmount * accountCostPercentage / 100;
                selling.LastUpdateDate            = DateTime.Now.BdTime().Date;
                selling.ServiceCharge             = model.ServiceCharge;
                selling.ServiceCost = model.ServiceCost;
                selling.ServiceChargeDescription = model.ServiceChargeDescription;
                selling.PurchaseId             = model.PurchaseId;
                selling.PurchaseAdjustedAmount = model.PurchaseAdjustedAmount;
                selling.PurchaseDescription    = model.PurchaseDescription;

                if (model.PromisedPaymentDate != null)
                {
                    selling.PromisedPaymentDate = model.PromisedPaymentDate.Value.BdTime().Date;
                }

                var due = (selling.SellingTotalPrice + selling.SellingReturnAmount + selling.ServiceCharge + selling.AccountTransactionCharge) - (selling.SellingDiscountAmount + selling.SellingPaidAmount);
                if (due < 0)
                {
                    response.IsSuccess = false;
                    response.Message   = "Due cannot be less than zero";
                    return(response);
                }

                if (due > selling.SellingDueAmount && db.Customers.IsDueLimitCrossed(selling.CustomerId, due - selling.SellingDueAmount))
                {
                    response.Message   = "Customer Due limit crossed";
                    response.IsSuccess = false;
                    return(response);
                }

                if (model.Products != null)
                {
                    selling.SellingList = model.Products.Select(p => new SellingList
                    {
                        SellingListId = p.SellingListId,
                        SellingId     = model.SellingId,
                        ProductId     = p.ProductId,
                        SellingPrice  = p.SellingPrice,
                        PurchasePrice = p.RemainCodes.Length > 0 ? p.PurchasePrice : 0,
                        Description   = p.Description,
                        Warranty      = p.Warranty,
                        ProductStock  = stocks.Where(s => p.RemainCodes.Contains(s.ProductCode)).ToList()
                    }).ToList();

                    selling.BuyingTotalPrice = model.Products.Sum(p => p.PurchasePrice * p.RemainCodes.Length);
                }
                else
                {
                    selling.SellingList      = null;
                    selling.BuyingTotalPrice = 0;
                }



                if (model.RemovedProductCodes != null)
                {
                    if (model.RemovedProductCodes.Any())
                    {
                        var removedStocks =
                            await db.ProductStocks.SoldBillStockFromCodesAsync(model.SellingId, model.RemovedProductCodes);

                        removedStocks = removedStocks.Select(s =>
                        {
                            s.IsSold        = false;
                            s.SellingListId = null;
                            return(s);
                        }).ToList();

                        if (removedStocks.Any())
                        {
                            Context.ProductStock.UpdateRange(removedStocks);

                            var logs = removedStocks.Select(c => new ProductLogAddModel
                            {
                                SellingId                = model.SellingId,
                                ProductStockId           = c.ProductStockId,
                                ActivityByRegistrationId = model.UpdateRegistrationId,
                                Details   = $"Product return by bill changed",
                                LogStatus = ProductLogStatus.Return
                            }).ToList();

                            productLogs.AddRange(logs);
                        }
                    }
                }

                Context.Selling.Update(selling);

                if (model.PaidAmount > 0)
                {
                    var newSellingPaymentSn = await db.SellingPayments.GetNewSnAsync().ConfigureAwait(false);

                    var payment = new SellingPayment
                    {
                        RegistrationId           = model.UpdateRegistrationId,
                        CustomerId               = selling.CustomerId,
                        ReceiptSn                = newSellingPaymentSn,
                        PaidAmount               = model.PaidAmount,
                        AccountTransactionCharge = model.AccountTransactionCharge,
                        AccountId                = model.AccountId,
                        PaidDate = DateTime.Now.BdTime().Date,
                        AccountCostPercentage = accountCostPercentage,
                        SellingPaymentList    = new List <SellingPaymentList>
                        {
                            new SellingPaymentList
                            {
                                SellingPaidAmount        = model.PaidAmount,
                                AccountTransactionCharge = model.AccountTransactionCharge,
                                SellingId = model.SellingId
                            }
                        }
                    };

                    await Context.SellingPayment.AddAsync(payment);
                }

                //Account add balance
                if (model.PaidAmount > 0 && model.AccountId != null)
                {
                    db.Account.BalanceAdd(model.AccountId.Value, model.PaidAmount);
                }

                await Context.SaveChangesAsync();

                db.Customers.UpdatePaidDue(selling.CustomerId);
                //Product log
                db.ProductLog.AddList(productLogs);

                //Return amount account update
                db.Account.SellingReturnRecordAdd(returnRecord);
            }
            catch (Exception e)
            {
                response.IsSuccess = false;
                response.Message   = e.Message;
                return(response);
            }

            response.IsSuccess = true;
            response.Message   = "Success";
            response.Data      = model.SellingId;

            return(response);
        }