Esempio n. 1
0
        protected void Save()
        {
            try
            {                                                         //added by ather for file attachment url
                string  path       = "~/Uploads/AttachedFiles/Sale/"; //path without filename to save file
                bool    fileupMsg  = uploadFile(fuAttacheFile, path, "");
                decimal balanceAmt = 0;                               // Convert.ToDecimal(txtBalanceAmt.Text);
                decimal paidAmnt   = 0;

                if (!string.IsNullOrEmpty(txtGivenAmt.Text))
                {
                    paidAmnt = Convert.ToDecimal(txtGivenAmt.Text);
                }
                if (!string.IsNullOrEmpty(txtBalanceAmt.Text))
                {
                    balanceAmt = Convert.ToDecimal(txtBalanceAmt.Text);
                }
                tbl_sale sale = new tbl_sale();
                if (fileupMsg)
                {
                    path = path + Path.GetFileName(fuAttacheFile.PostedFile.FileName); //path with filename to save in DB
                    sale.attachmentUrl = path;
                }
                sale.company_id       = companyId;
                sale.branch_id        = branchId;
                sale.financialyear_id = financialYearId;
                sale.paymentmode_id   = Convert.ToInt32(ddlPaymentMode.SelectedValue);
                sale.status           = true;
                sale.party_id         = Convert.ToInt32(ddlVendor.SelectedValue);
                sale.InvoiceNumber    = txtSONo.Text;
                sale.sale_date        = DateTime.Parse(txtdate.Text, new CultureInfo("en-US"));
                sale.created_by       = User_id;
                sale.created_date     = DateTime.Now;
                sale.Note             = txtSaleNote.Text;
                //sale.OtherExpLabel = txtOtherExpLabel.Text;
                //sale.other_expenses = Convert.ToDecimal(txtotherexpence.Text);

                //insert into Sale Payment Details
                tbl_SalePaymentDetails salePaymentDetails = new tbl_SalePaymentDetails();
                salePaymentDetails.TaxAmount   = Convert.ToDecimal(lblTaxAmount.Text);
                salePaymentDetails.SubTotal    = Convert.ToDecimal(lblsubtotal.Text);
                salePaymentDetails.GrandTotal  = Convert.ToDecimal(lblGrandTotal.Text);
                salePaymentDetails.PaidAmnt    = paidAmnt;
                salePaymentDetails.GivenAmnt   = paidAmnt;
                salePaymentDetails.BalanceAmnt = balanceAmt;
                salePaymentDetails.FromTable   = "Sale";
                salePaymentDetails.CreatedBy   = User_id;
                salePaymentDetails.CreatedDate = DateTime.Now;


                salePaymentDetails.OtherExpLabel = txtOtherExpLabel.Text;
                salePaymentDetails.OtherExp      = Convert.ToDecimal(txtotherexpence.Text);

                //string otherExp = txtotherexpence.Text;
                //if (otherExp.Contains("-"))
                //{
                //    var calculatedDiscount = Convert.ToDecimal(lblDiscountAmt.Text);
                //    var overAllDisc = Convert.ToDecimal(otherExp.Substring(otherExp.LastIndexOf('-') + 0));
                //    var totalDiscount = calculatedDiscount - overAllDisc;

                //    salePaymentDetails.DiscountAmount = totalDiscount;
                //}
                //else
                //{
                //    salePaymentDetails.DiscountAmount = Convert.ToDecimal(lblDiscountAmt.Text);
                //}

                salePaymentDetails.DiscountAmount = Convert.ToDecimal(lblDiscountAmt.Text);
                sale.tbl_SalePaymentDetails.Add(salePaymentDetails);

                for (int i = 0; i <= gvSalesdetails.Rows.Count - 1; i++)
                {
                    int         productId = Convert.ToInt32(gvSalesdetails.Rows[i].Cells[3].Text);
                    int         batchId   = Convert.ToInt32(gvSalesdetails.Rows[i].Cells[8].Text);
                    decimal     qty       = Convert.ToDecimal(gvSalesdetails.Rows[i].Cells[4].Text);
                    tbl_product product   = context.tbl_product.Where(w => w.product_id == productId).FirstOrDefault();

                    //Add into sale Details table for each product
                    tbl_saledetails saleDetails = new tbl_saledetails();
                    saleDetails.product_id   = productId;
                    saleDetails.batch_id     = batchId;
                    saleDetails.unit_id      = product.unit_id;
                    saleDetails.tax_amt      = Convert.ToDecimal(gvSalesdetails.Rows[i].Cells[10].Text);
                    saleDetails.dicount_amt  = Convert.ToDecimal(gvSalesdetails.Rows[i].Cells[7].Text);
                    saleDetails.quantity     = qty;
                    saleDetails.amount       = Convert.ToDecimal(gvSalesdetails.Rows[i].Cells[11].Text);
                    saleDetails.created_by   = Convert.ToString(User_id);
                    saleDetails.created_date = Convert.ToDateTime(DateTime.Now);
                    saleDetails.status       = true;


                    int groupId = Convert.ToInt32(gvSalesdetails.Rows[i].Cells[12].Text);
                    //var PurchasegroupId = Convert.ToInt32(gvSalesdetails.Rows[i].Cells[10].Text);

                    DataTable taxgroupTypes1 = helper.LINQToDataTable(context.SelectPurcahseProductTaxGroup(batchId, productId, qty, groupId));
                    ViewState["TotalTaxPercent"] = null;
                    for (int j = 0; j <= taxgroupTypes1.Rows.Count - 1; j++)
                    {
                        ViewState["TotalTaxPercent"] = taxgroupTypes1.Rows[j].Field <decimal>("totalTaxPercetage");
                    }
                    //insert into tax group Sale
                    tbl_saleTaxGroup saleTaxGroup = new tbl_saleTaxGroup();
                    saleTaxGroup.group_id           = groupId;
                    saleTaxGroup.product_id         = productId;
                    saleTaxGroup.totalTaxPercentage = (Decimal)ViewState["TotalTaxPercent"];
                    saleTaxGroup.group_name         = gvSalesdetails.Rows[i].Cells[9].Text;
                    sale.tbl_saleTaxGroup.Add(saleTaxGroup);

                    //Get the Tax type saved from db
                    //insert into tax group detailes


                    for (int j = 0; j <= taxgroupTypes1.Rows.Count - 1; j++)
                    {
                        tbl_saleTaxGroupDetailes saleTaxDetails = new tbl_saleTaxGroupDetailes();
                        saleTaxDetails.type_id        = taxgroupTypes1.Rows[j].Field <int>("type_id");
                        saleTaxDetails.tax_percentage = taxgroupTypes1.Rows[j].Field <decimal>("tax_percentage");
                        saleTaxGroup.tbl_saleTaxGroupDetailes.Add(saleTaxDetails);
                    }

                    //Enter Details In tbl_ActualsaleTaxAndPrice : to get the original Values at the time of sale Return
                    tbl_ActualSalesTaxAndPrice actualSale = new tbl_ActualSalesTaxAndPrice();
                    actualSale.product_id       = productId;
                    actualSale.status           = true;
                    actualSale.discount_percent = Convert.ToDecimal(gvSalesdetails.Rows[i].Cells[6].Text);
                    actualSale.sale_rate        = Convert.ToDecimal(gvSalesdetails.Rows[i].Cells[5].Text);
                    actualSale.discount_amnt    = Convert.ToDecimal(gvSalesdetails.Rows[i].Cells[7].Text);
                    actualSale.created_by       = Convert.ToString(User_id);
                    actualSale.created_date     = Convert.ToDateTime(DateTime.Now);
                    //Add into Actual sale Tax And Return Table
                    sale.tbl_ActualSalesTaxAndPrice.Add(actualSale);

                    //update the Stock against the product
                    tbl_stock stock = new tbl_stock();
                    stock               = context.tbl_stock.Where(w => w.company_id == companyId && w.branch_id == branchId && w.product_id == productId && w.batch_id == batchId).FirstOrDefault();
                    stock.qty           = stock.qty - Convert.ToInt32(gvSalesdetails.Rows[i].Cells[4].Text);
                    stock.modified_by   = Convert.ToString(User_id);
                    stock.modified_date = Convert.ToDateTime(DateTime.Now);

                    sale.tbl_saledetails.Add(saleDetails);
                }

                context.tbl_sale.Add(sale);
                context.SaveChanges();
                Session["sale_id"] = sale.sale_id;
                string order = sale.InvoiceNumber;
                ClientScript.RegisterStartupScript(this.GetType(), "Pop", "openalert('Saved successfully, Your order number is " + order + "');", true);
            }
            catch (Exception ex)
            {
                ErrorLog.saveerror(ex);
            }
        }