public void InsertSalesReturn(SalesReturnColumnModel model)
        {
            try
            {
                using (this.unitOfWork)
                {
                    SalesReturn item = new SalesReturn()
                    {
                        MemoNumber        = model.MemoNumber,
                        CustomerId        = model.CustomerId,
                        ReturnDate        = model.ReturnDate,
                        AmountReturn      = model.AmountReturn,
                        Adjustment        = model.Adjustment,
                        TotalCreditAmount = model.TotalCreditAmount,
                        Remarks           = model.Remarks,
                        IsDeleted         = model.IsDeleted,
                        RecordedBy        = model.RecordedByUser,
                        ApprovedBy        = model.ApprovedByUser,
                        AmountUsed        = 0
                    };

                    if (model.Details.Count > 0)
                    {
                        foreach (SalesReturnDetailModel d in model.Details)
                        {
                            SalesReturnDetail detail = new SalesReturnDetail()
                            {
                                PartDetailId  = d.PartDetailId,
                                InvoiceNumber = d.InvoiceNumber,
                                Quantity      = d.Quantity,
                                UnitPrice     = d.UnitPrice,
                                TotalAmount   = d.TotalAmount,
                                Balance       = d.TotalAmount
                            };

                            item.SalesReturnDetail.Add(detail);

                            AutoPartDetail autoDetail = db.AutoPartDetail.FirstOrDefault(a => a.Id == d.PartDetailId);
                            if (autoDetail != null)
                            {
                                autoDetail.Quantity += d.Quantity;
                            }
                        }
                    }

                    this.unitOfWork.Context.AddToSalesReturn(item);

                    string action = string.Format("Added new Sales Return - {0}", item.MemoNumber);
                    this.actionLogController.AddToLog(action, UserInfo.UserId);

                    this.unitOfWork.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #2
0
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (e.Argument != null)
            {
                SalesReturnColumnModel model = (SalesReturnColumnModel)e.Argument;

                if (model.Id == 0)
                {
                    this.salesReturnController.InsertSalesReturn(model);
                }
                else
                {
                    this.salesReturnController.UpdateSalesReturn(model);
                }
            }
        }
예제 #3
0
        private void SaveReturn()
        {
            ToggleButtons(false);
            LoadImage.Visible = true;

            SalesReturnColumnModel model = new SalesReturnColumnModel()
            {
                Id                = ReturnId,
                MemoNumber        = MemoTextbox.Text,
                CustomerId        = (int)CustomerDropdown.SelectedValue,
                IsDeleted         = false,
                ReturnDate        = ReturnDatePicker.Value,
                TotalCreditAmount = list.Sum(a => a.TotalAmount),
                Remarks           = RemarksTextbox.Text,
                Details           = list,
                RecordedByUser    = UserInfo.UserId,
                ApprovedByUser    = approver
            };

            worker.RunWorkerAsync(model);
        }
        public void UpdateSalesReturn(SalesReturnColumnModel model)
        {
            try
            {
                using (this.unitOfWork)
                {
                    var item = FetchSalesReturnById(model.Id);

                    if (item != null)
                    {
                        //Delete old details
                        if (item.SalesReturnDetail.Any())
                        {
                            foreach (var d in item.SalesReturnDetail.ToList())
                            {
                                AutoPartDetail detail = db.AutoPartDetail.FirstOrDefault(a => a.Id == d.PartDetailId);
                                detail.Quantity -= d.Quantity;

                                foreach (var payment in d.SalesPayments.ToList())
                                {
                                    item.AmountUsed -= payment.Amount;
                                    db.DeleteObject(payment);
                                }

                                db.DeleteObject(d);
                            }
                        }

                        //Update details
                        item.MemoNumber        = model.MemoNumber;
                        item.CustomerId        = model.CustomerId;
                        item.ReturnDate        = model.ReturnDate;
                        item.AmountReturn      = model.AmountReturn;
                        item.Adjustment        = model.Adjustment;
                        item.TotalCreditAmount = model.TotalCreditAmount;
                        item.Remarks           = model.Remarks;
                        item.IsDeleted         = model.IsDeleted;
                        item.RecordedBy        = model.RecordedByUser;
                        item.ApprovedBy        = model.ApprovedByUser;

                        //Add the new items
                        if (model.Details.Count > 0)
                        {
                            foreach (SalesReturnDetailModel d in model.Details)
                            {
                                var autoPart = db.AutoPartDetail.FirstOrDefault(a => a.Id == d.PartDetailId);
                                autoPart.Quantity += d.Quantity;

                                SalesReturnDetail detail = new SalesReturnDetail()
                                {
                                    SalesReturn    = item,
                                    AutoPartDetail = autoPart,
                                    InvoiceNumber  = d.InvoiceNumber,
                                    Quantity       = d.Quantity,
                                    UnitPrice      = d.UnitPrice,
                                    TotalAmount    = d.TotalAmount,
                                    Balance        = d.TotalAmount
                                };
                            }
                        }
                    }

                    string action = string.Format("Updated Sales Return - {0}", item.MemoNumber);
                    this.actionLogController.AddToLog(action, UserInfo.UserId);

                    this.unitOfWork.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }