Ejemplo n.º 1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateMandatoryFields())
            {
                return;
            }

            var repoLastUpdated = _uow.Repository.GetById(txtSalesOrderId.Text.Trim()).ModifiedAt;
            var lastUpdated     = DateTime.Parse(txtModifiedAt.Text.Trim());

            var commonHelper = new CommonFunctionHelper();

            if (commonHelper.StandardizeDateTime(lastUpdated) != commonHelper.StandardizeDateTime(repoLastUpdated))
            {
                CommonMessageHelper.DataHasBeenUpdatedPriorToSave(lblSalesNo.Text.Trim());
            }
            else
            {
                using (var dbContextTransaction = dbContext.Database.BeginTransaction())
                {
                    var customerId = cbCustomer.SelectedValue.ToString();
                    var customer   = new UnitOfWork <Customer>(dbContext).Repository.GetById(customerId);
                    var salesman   = new UnitOfWork <Salesman>(dbContext).Repository.GetById(cbSalesman.SelectedValue.ToString());
                    var top        = new UnitOfWork <TermOfPayment>(dbContext).Repository.GetById(cbTermOfPayment.SelectedValue.ToString());
                    var salesArea  = new UnitOfWork <SalesArea>(dbContext).Repository.GetById(cbSalesArea.SelectedValue.ToString());
                    var prov       = new UnitOfWork <Location>(dbContext).Repository.GetById(cbProvince.SelectedValue.ToString());
                    var district   = new UnitOfWork <Location>(dbContext).Repository.GetById(cbDistrict.SelectedValue.ToString());

                    #region parent
                    var uwSalesOrder = new UnitOfWork <SalesOrder>(dbContext);
                    var soToUpdate   = uwSalesOrder.Repository.GetById(txtSalesOrderId.Text.Trim());

                    soToUpdate.CustomerId          = customerId;
                    soToUpdate.CustomerName        = customer.CustomerName;
                    soToUpdate.CustomerAddress     = customer.Address;
                    soToUpdate.CustomerProvinceId  = customer.ProvinceId;
                    soToUpdate.CustomerProvince    = GetLocationName(customer.ProvinceId);
                    soToUpdate.CustomerDistrictId  = customer.DistrictId;
                    soToUpdate.CustomerDistrict    = GetLocationName(customer.DistrictId);
                    soToUpdate.CustomerPostalCode  = customer.PostalCode;
                    soToUpdate.CustomerPhone       = customer.Phone;
                    soToUpdate.CustomerEmail       = customer.Email;
                    soToUpdate.CustomerNpwp        = customer.Npwp;
                    soToUpdate.SalesmanId          = cbSalesman.SelectedValue.ToString();
                    soToUpdate.SalesmanCode        = salesman.SalesmanCode;
                    soToUpdate.TermOfPaymentId     = cbTermOfPayment.SelectedValue.ToString();
                    soToUpdate.TermOfPaymentCode   = top.TermCode;
                    soToUpdate.SalesDate           = dtpSalesOrderDate.Value;
                    soToUpdate.DueDate             = dtpDueDate.Value;
                    soToUpdate.DeliveryAddress     = txtDeliveryAddress.Text;
                    soToUpdate.DeliveryProvinceId  = cbProvince.SelectedValue.ToString();
                    soToUpdate.DeliveryProvince    = prov.Name;
                    soToUpdate.DeliveryDistrictId  = cbDistrict.SelectedValue.ToString();
                    soToUpdate.DeliveryDistrict    = district.Name;
                    soToUpdate.SalesAreaId         = cbSalesArea.SelectedValue.ToString();
                    soToUpdate.SalesAreaCode       = salesArea.AreaCode;
                    soToUpdate.SubTotalAmount      = decimal.Parse(txtSubTotal.Text.Trim(), System.Globalization.NumberStyles.Currency);
                    soToUpdate.ExtraDiscountAmount = decimal.Parse(txtExtraDiscount.Text.Trim(), System.Globalization.NumberStyles.Currency);
                    soToUpdate.TaxBaseAmount       = decimal.Parse(txtTaxBaseAmount.Text.Trim(), System.Globalization.NumberStyles.Currency);
                    soToUpdate.ValueAddedTaxAmount = decimal.Parse(txtValueAddedTaxAmount.Text.Trim(), System.Globalization.NumberStyles.Currency);
                    soToUpdate.GrandTotalAmount    = decimal.Parse(lblTotal.Text.Trim(), System.Globalization.NumberStyles.Currency);
                    soToUpdate.UserId   = Properties.Settings.Default.CurrentUserId;
                    soToUpdate.Username = Properties.Settings.Default.CurrentUser;

                    // Audit fields
                    soToUpdate.ModifiedBy = Properties.Settings.Default.CurrentUserId;
                    soToUpdate.ModifiedAt = DateTime.Now;

                    uwSalesOrder.Repository.Update(soToUpdate);
                    uwSalesOrder.Commit();

                    #endregion parent

                    #region child

                    var uwSoItem = new UnitOfWork <SalesOrderItem>(dbContext);

                    // clear all item for the identified Sales Order
                    var tobeClearedItems = uwSoItem.Repository.GetAll().Where(soi => soi.SalesOrderId == txtSalesOrderId.Text.Trim());
                    uwSoItem.Repository.Delete(tobeClearedItems);
                    uwSoItem.Commit();

                    // add with updated items
                    List <SalesOrderItem> orderItems = new List <SalesOrderItem>();
                    for (int i = 0; i < dgvSalesOrderItem.Rows.Count; ++i)
                    {
                        if (dgvSalesOrderItem.Rows[i].Cells["productCode"].Value == null)
                        {
                            continue;
                        }

                        var expiredDate     = commonHelper.GetEndOfMonth(DateTime.Parse(dgvSalesOrderItem.Rows[i].Cells["expDate"].Value.ToString()));
                        var qty             = Convert.ToInt32(decimal.Parse(dgvSalesOrderItem.Rows[i].Cells["qty"].Value.ToString(), System.Globalization.NumberStyles.Currency));
                        var nettPrice       = Math.Round(decimal.Parse(dgvSalesOrderItem.Rows[i].Cells["price"].Value.ToString(), System.Globalization.NumberStyles.Currency), 5, MidpointRounding.AwayFromZero);
                        var discountPercent = Math.Round(Convert.ToDecimal(ValidateDiscount(dgvSalesOrderItem.Rows[i].Cells["discPercent"].Value.ToString().Replace("%", ""))) / 100, 5, MidpointRounding.AwayFromZero);
                        var grossValue      = Math.Round(qty * nettPrice, 5, MidpointRounding.AwayFromZero);
                        var subTotal        = salesOrderHelper.CalculateTaxBaseAmount(qty, nettPrice, discountPercent);

                        var soiToUpdate = new SalesOrderItem
                        {
                            SalesOrderId       = txtSalesOrderId.Text.Trim(),
                            ProductId          = dgvSalesOrderItem.Rows[i].Cells["productId"].Value.ToString(),
                            ProductCode        = dgvSalesOrderItem.Rows[i].Cells["productCode"].Value.ToString(),
                            ProductName        = dgvSalesOrderItem.Rows[i].Cells["productName"].Value.ToString(),
                            BatchId            = dgvSalesOrderItem.Rows[i].Cells["batchId"].Value.ToString(),
                            BatchCode          = dgvSalesOrderItem.Rows[i].Cells["batchCode"].Value.ToString(),
                            ExpiredDate        = commonHelper.StandardizeDate(expiredDate),
                            Quantity           = qty,
                            UomId              = dgvSalesOrderItem.Rows[i].Cells["uomId"].Value.ToString(),
                            UomCode            = dgvSalesOrderItem.Rows[i].Cells["uomCode"].Value.ToString(),
                            Price              = nettPrice,
                            DiscountPercentage = float.Parse(dgvSalesOrderItem.Rows[i].Cells["discPercent"].Value.ToString().Replace("%", "")),
                            TotalAmount        = subTotal,

                            // Audit Fields
                            CreatedBy  = Properties.Settings.Default.CurrentUserId,
                            CreatedAt  = DateTime.Now,
                            ModifiedBy = Properties.Settings.Default.CurrentUserId,
                            ModifiedAt = DateTime.Now
                        };
                        orderItems.Add(soiToUpdate);
                    }
                    uwSoItem.Repository.Add(orderItems);
                    uwSoItem.Commit();

                    #endregion child

                    dbContextTransaction.Commit();
                }

                btnReload.PerformClick();
                CommonMessageHelper.DataSavedSuccessfully();
            }
        }
Ejemplo n.º 2
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!ValidateMandatoryFields())
            {
                return;
            }

            using (var dbContextTransaction = dbContext.Database.BeginTransaction())
            {
                var customer  = new UnitOfWork <Customer>(dbContext).Repository.GetById(cbCustomer.SelectedValue.ToString());
                var salesman  = new UnitOfWork <Salesman>(dbContext).Repository.GetById(cbSalesman.SelectedValue.ToString());
                var top       = new UnitOfWork <TermOfPayment>(dbContext).Repository.GetById(cbTermOfPayment.SelectedValue.ToString());
                var salesArea = new UnitOfWork <SalesArea>(dbContext).Repository.GetById(cbSalesArea.SelectedValue.ToString());
                var district  = new UnitOfWork <Location>(dbContext).Repository.GetById(cbDistrict.SelectedValue.ToString());

                #region parent
                var uwSalesOrder = new UnitOfWork <SalesOrder>(dbContext);
                var soToAdd      = new SalesOrder();
                soToAdd.SalesNo             = CreateSalesOrderNo();
                soToAdd.CustomerId          = cbCustomer.SelectedValue.ToString();
                soToAdd.CustomerName        = customer.CustomerName;
                soToAdd.CustomerAddress     = customer.Address;
                soToAdd.CustomerProvinceId  = customer.ProvinceId;
                soToAdd.CustomerProvince    = GetLocationName(customer.ProvinceId);
                soToAdd.CustomerDistrictId  = customer.DistrictId;
                soToAdd.CustomerDistrict    = GetLocationName(customer.DistrictId);
                soToAdd.CustomerPostalCode  = customer.PostalCode;
                soToAdd.CustomerPhone       = customer.Phone;
                soToAdd.CustomerEmail       = customer.Email;
                soToAdd.CustomerNpwp        = customer.Npwp;
                soToAdd.DeliveryAddress     = txtDeliveryAddress.Text;
                soToAdd.DeliveryProvinceId  = cbProvince.SelectedValue.ToString();
                soToAdd.DeliveryProvince    = GetLocationName(cbProvince.SelectedValue.ToString());
                soToAdd.DeliveryDistrictId  = cbDistrict.SelectedValue.ToString();
                soToAdd.DeliveryDistrict    = GetLocationName(cbDistrict.SelectedValue.ToString());
                soToAdd.SalesAreaId         = cbSalesArea.SelectedValue.ToString();
                soToAdd.SalesAreaCode       = salesArea.AreaCode;
                soToAdd.SalesDate           = dtpSalesOrderDate.Value;
                soToAdd.TermOfPaymentId     = cbTermOfPayment.SelectedValue.ToString();
                soToAdd.TermOfPaymentCode   = top.TermCode;
                soToAdd.DueDate             = dtpDueDate.Value;
                soToAdd.PersonInCharge      = Properties.Settings.Default.SalesPicName;
                soToAdd.SipaNo              = Properties.Settings.Default.SalesPicSipaNo;
                soToAdd.SalesmanId          = cbSalesman.SelectedValue.ToString();
                soToAdd.SalesmanCode        = salesman.SalesmanCode;
                soToAdd.SubTotalAmount      = decimal.Parse(txtSubTotal.Text.Trim(), System.Globalization.NumberStyles.Currency);
                soToAdd.ExtraDiscountAmount = decimal.Parse(txtExtraDiscount.Text.Trim(), System.Globalization.NumberStyles.Currency);
                soToAdd.TaxBaseAmount       = decimal.Parse(txtTaxBaseAmount.Text.Trim(), System.Globalization.NumberStyles.Currency);
                soToAdd.ValueAddedTaxAmount = decimal.Parse(txtValueAddedTaxAmount.Text.Trim(), System.Globalization.NumberStyles.Currency);
                soToAdd.GrandTotalAmount    = decimal.Parse(lblTotal.Text.Trim(), System.Globalization.NumberStyles.Currency);
                soToAdd.UserId              = Properties.Settings.Default.CurrentUserId;
                soToAdd.Username            = Properties.Settings.Default.CurrentUser;
                soToAdd.Status              = Constant.RecordStatus.Active;
                soToAdd.CreatedBy           = Properties.Settings.Default.CurrentUserId;
                soToAdd.CreatedAt           = DateTime.Now;
                soToAdd.ModifiedBy          = Properties.Settings.Default.CurrentUserId;
                soToAdd.ModifiedAt          = DateTime.Now;

                uwSalesOrder.Repository.Add(soToAdd);
                uwSalesOrder.Commit();

                #endregion parent

                #region child

                var uwSoItem = new UnitOfWork <SalesOrderItem>(dbContext);
                for (int i = 0; i < dgvSalesOrderItem.Rows.Count; ++i)
                {
                    if (dgvSalesOrderItem.Rows[i].Cells["productCode"].Value == null)
                    {
                        continue;
                    }

                    DateTime expiredDate = DateTime.Parse(dgvSalesOrderItem.Rows[i].Cells["expDate"].Value.ToString());
                    expiredDate = commonHelper.GetEndOfMonth(expiredDate);

                    var qty             = Convert.ToInt32(decimal.Parse(dgvSalesOrderItem.Rows[i].Cells["qty"].Value.ToString(), System.Globalization.NumberStyles.Currency));
                    var nettPrice       = Math.Round(decimal.Parse(dgvSalesOrderItem.Rows[i].Cells["price"].Value.ToString(), System.Globalization.NumberStyles.Currency), 5, MidpointRounding.AwayFromZero);
                    var discountPercent = Math.Round(Convert.ToDecimal(ValidateDiscount(dgvSalesOrderItem.Rows[i].Cells["discPercent"].Value.ToString().Replace("%", ""))) / 100, 5, MidpointRounding.AwayFromZero);
                    var subTotal        = salesOrderHelper.CalculateTaxBaseAmount(qty, nettPrice, discountPercent);

                    var soiToAdd = new SalesOrderItem
                    {
                        SalesOrderId       = soToAdd.Id,
                        ProductId          = dgvSalesOrderItem.Rows[i].Cells["productId"].Value.ToString(),
                        ProductCode        = dgvSalesOrderItem.Rows[i].Cells["productCode"].Value.ToString(),
                        ProductName        = dgvSalesOrderItem.Rows[i].Cells["productName"].Value.ToString(),
                        BatchId            = dgvSalesOrderItem.Rows[i].Cells["batchId"].Value.ToString(),
                        BatchCode          = dgvSalesOrderItem.Rows[i].Cells["batchCode"].Value.ToString(),
                        ExpiredDate        = commonHelper.StandardizeDate(expiredDate),
                        Quantity           = qty,
                        UomId              = dgvSalesOrderItem.Rows[i].Cells["uomId"].Value.ToString(),
                        UomCode            = dgvSalesOrderItem.Rows[i].Cells["uomCode"].Value.ToString(),
                        Price              = nettPrice,
                        DiscountPercentage = float.Parse(dgvSalesOrderItem.Rows[i].Cells["discPercent"].Value.ToString().Replace("%", "")),
                        TotalAmount        = subTotal,

                        // Audit Fields
                        CreatedBy  = Properties.Settings.Default.CurrentUserId,
                        CreatedAt  = DateTime.Now,
                        ModifiedBy = Properties.Settings.Default.CurrentUserId,
                        ModifiedAt = DateTime.Now
                    };
                    uwSoItem.Repository.Add(soiToAdd);
                }
                var res = uwSoItem.Commit();

                #endregion child

                dbContextTransaction.Commit();

                // when commit succeed, update the key fields
                lblSalesNo.Text      = soToAdd.SalesNo;
                txtSalesOrderId.Text = soToAdd.Id;
                txtModifiedAt.Text   = soToAdd.ModifiedAt.ToString();
            }

            NavigateRecord(RecordNavigation.Last);
            CommonMessageHelper.DataSavedSuccessfully();
        }