예제 #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();
            }
        }
예제 #2
0
        private void btnAddItem_Click(object sender, EventArgs e)
        {
            if (cbProduct.SelectedValue.ToString() == "0")
            {
                CommonMessageHelper.DataCannotBeEmpty("Kode Barang");
                return;
            }

            if (string.IsNullOrEmpty(txtQty.Text.Trim()) || txtQty.Text.Trim() == "0")
            {
                CommonMessageHelper.DataCannotBeEmpty("Qty");
                return;
            }

            UnitOfWork <Product> uowProduct = new UnitOfWork <Product>(dbContext);
            var product = uowProduct.Repository.GetById(cbProduct.SelectedValue.ToString());
            UnitOfWork <Batch> uowBatch = new UnitOfWork <Batch>(dbContext);
            var batch = uowBatch.Repository.GetAll().Where(b => b.BatchCode.Equals(txtBatch.Text.Trim())).FirstOrDefault();
            UnitOfWork <UnitOfMeasurement> uowUom = new UnitOfWork <UnitOfMeasurement>(dbContext);
            var  uom           = uowUom.Repository.GetById(product.UnitId);
            int  rowId         = dgvSalesOrderItem.Rows.Count;
            bool duplicateItem = false;

            // if any existing data, then update
            for (int i = 0; i < dgvSalesOrderItem.Rows.Count; ++i)
            {
                if (dgvSalesOrderItem.Rows[i].Cells["productId"].Value == null)
                {
                    continue;
                }

                if (dgvSalesOrderItem.Rows[i].Cells["productId"].Value.ToString() == cbProduct.SelectedValue.ToString() &&
                    (dgvSalesOrderItem.Rows[i].Cells["batchCode"].Value.ToString() == txtBatch.Text.Trim() || string.IsNullOrEmpty(dgvSalesOrderItem.Rows[i].Cells["batchId"].Value.ToString())) &&
                    commonHelper.GetEndOfMonth(DateTime.Parse(dgvSalesOrderItem.Rows[i].Cells["expDate"].Value.ToString())) == commonHelper.GetEndOfMonth(dtpExpiredDate.Value))
                {
                    rowId         = i;
                    duplicateItem = true;
                    break;
                }
            }

            if (!duplicateItem)
            {
                rowId = dgvSalesOrderItem.Rows.Add();
            }

            DataGridViewRow row = dgvSalesOrderItem.Rows[rowId];

            // Add the data
            var qty                = Convert.ToDecimal(txtQty.Text.Trim());
            var nettPrice          = Convert.ToDecimal(txtPrice.Text.Trim());
            var discountPercentage = Math.Round(Convert.ToDecimal(ValidateDiscount(txtDiscount.Text.Trim())) / 100, 5, MidpointRounding.AwayFromZero);
            var taxBaseAmount      = salesOrderHelper.CalculateTaxBaseAmount(qty, nettPrice, discountPercentage);

            row.Cells["productId"].Value   = product.Id;
            row.Cells["productCode"].Value = product.ProductCode;
            row.Cells["productName"].Value = product.ProductName;
            row.Cells["batchId"].Value     = batch != null ? batch.Id : string.Empty;
            row.Cells["batchCode"].Value   = txtBatch.Text.Trim();
            row.Cells["expDate"].Value     = commonHelper.GetEndOfMonth(dtpExpiredDate.Value).ToString("MM/yyyy");
            row.Cells["qty"].Value         = string.Format("{0:n0}", qty);
            row.Cells["uomId"].Value       = uom.Id;
            row.Cells["uomCode"].Value     = uom.UomCode;
            row.Cells["price"].Value       = string.Format("{0:n0}", nettPrice);
            row.Cells["discPercent"].Value = string.Format("{0:n2}", ValidateDiscount(txtDiscount.Text.Trim())) + "%";
            row.Cells["subTotal"].Value    = string.Format("{0:n0}", taxBaseAmount);

            // hidden
            row.Cells["priceReal"].Value = string.Format("{0:n10}", nettPrice);;

            SetTotalSalesOrder();

            // reset back the add item
            cbProduct.SelectedValue = "0";
            txtBatch.Text           = string.Empty;
            dtpExpiredDate.Value    = DateTime.Today;
            txtQty.Text             = "0";
            txtPrice.Text           = "0";
            txtDiscount.Text        = "0";
            cbProduct.Focus();
        }