Esempio n. 1
0
        /// <summary>
        /// Create Sales Order :
        /// 1. Create Sale Person If New
        /// 2. Add SaleMaster
        /// 3. Add Sales Invoice
        /// 4. Add Pending Payment if any
        /// 5. Add payment transaction
        /// 6. Update Stock
        /// </summary>
        /// <returns></returns>
        public int CreateSalesOrder(Sale _sale)
        {
            int salesOrderId = 0;

            using (DataAccess da = new DataAccess())
            {
                try
                {
                    // add Sale Contact
                    if (_sale.Contact.Id == 0)
                    {
                        _sale.Contact.Id = CreateSalePersonForSale(da, _sale.Contact);
                    }
                    if (_sale.Contact.Id > 0)
                    {
                        double total      = _sale.Quantity * _sale.Price;
                        bool   isPending  = (total > _sale.AmountPaid ? true : false);
                        double pendingAmt = total - _sale.AmountPaid;
                        #region Create Sales Master Object
                        //Add SaleMaster
                        Dictionary <string, string> saleMaster = new Dictionary <string, string>();
                        saleMaster.Add("Id", _sale.Id == 0 ? null : _sale.Id.ToString());
                        saleMaster.Add("StockId", _sale.StockId.ToString());
                        saleMaster.Add("SalePersonId", _sale.Contact.Id.ToString());
                        saleMaster.Add("Quantity", _sale.Quantity.ToString());
                        saleMaster.Add("Price", _sale.Price.ToString());
                        saleMaster.Add("Total", _sale.Total.ToString());
                        saleMaster.Add("AmountPaid", _sale.AmountPaid.ToString());
                        //saleMasterModel.Add("Pending", pendingAmt.ToString());
                        saleMaster.Add("SaleDate", _sale.SaleDate.ToString(ConfigurationManager.AppSettings["DateOnly"]));
                        saleMaster.Add("CreatedDate", DateTime.Now.ToString(ConfigurationManager.AppSettings["DateTimeFormat"]));
                        #endregion
                        salesOrderId = da.InsertOrUpdateSaleMaster(saleMaster, "tblSaleMaster");
                        if (salesOrderId > 0 && _sale.Id == 0)
                        {
                            #region Add Sales Invoice
                            // Adding Sales Invoice
                            Dictionary <string, string> invoice = new Dictionary <string, string>();
                            invoice.Add("Id", null);
                            invoice.Add("SalesId", salesOrderId.ToString());
                            invoice.Add("InvoiceNumber", CommonMethods.GenerateInvoice(salesOrderId, _sale.SaleDate));
                            da.InsertOrUpdateInvoiceMaster(invoice, "tblInvoiceMaster");
                            #endregion

                            #region Add Pending Payment
                            if (isPending)
                            {
                                PendingPayment pendingPayment = new PendingPayment()
                                {
                                    SaleId       = salesOrderId, SalePersonId = _sale.Contact.Id, PendingAmount = pendingAmt
                                    , IsDiscount = _sale.IsDiscounted
                                };
                                CreatePendingPayment(pendingPayment, da);
                            }
                            #endregion

                            #region Add Payment Transaction
                            // add payment transaction
                            PaymentTransaction paymentTransaction = new PaymentTransaction();
                            bool paymentStatus = paymentTransaction.AddPaymentTransaction(Global.UserId, _sale.AmountPaid, CommonEnum.PaymentStatus.SALE_PAYMENT, salesOrderId, da);
                            if (paymentStatus)
                            {
                                #region Update Stock Quantity
                                Dictionary <string, string> stockModel = new Dictionary <string, string>();
                                stockModel.Add("Id", _sale.StockId.ToString());
                                stockModel.Add("AvlQuantity", _sale.Quantity.ToString());
                                stockModel.Add("ModifiedDate", DateTime.Now.ToString(ConfigurationManager.AppSettings["DateTimeFormat"]));
                                int isUpdated = da.UpdateStockQuantity(stockModel, "tblStockMaster");
                                #endregion
                                if (isUpdated == 1)
                                {
                                    SendMailService ms = new SendMailService();
                                    ms.SendSalesOrderCreateMail(_sale);

                                    MessageBoxResult result = MessageBox.Show("Sale Added Successfully", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                                }
                                else
                                {
                                    MessageBoxResult result = MessageBox.Show("Error While Updating Stock!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                                }
                            }
                            else
                            {
                                MessageBoxResult result = MessageBox.Show("Error While Adding Payment Transaction!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                            }
                            #endregion
                        }
                        else
                        {
                            MessageBoxResult result = MessageBox.Show((string)Application.Current.FindResource("StandardProcessingErrorMessage"), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    logger.LogException(ex);
                    da.RollbackTransaction();
                }
            }
            return(salesOrderId);
        }