Example #1
0
        public transaction convertTransaction(TransactionRequestDto dtos)
        {
            transaction transaction = new transaction();

            transaction.exchange_store_id = dtos.exchangeStoreId;
            transaction.store_id          = dtos.storeId;
            transaction.time                = stampConvert.convertTimeStamp(dtos.time);
            transaction.staff_id            = dtos.staffId;
            transaction.transaction_type_id = dtos.transactionTypeId;
            transaction.status_id           = dtos.statusId;

            foreach (var dto in dtos.detail)
            {
                transaction_detail detail = new transaction_detail();
                detail.trans_id    = dtos.id;
                detail.material_id = int.Parse(dto.material.id);
                detail.amount      = dto.materialAmount;
                db.transaction_detail.Add(detail);
                String[] list = new string[] { "EC", "EX01", "EX02", "IM02" };

                if (dtos.transactionTypeId == "EC")
                {
                    store_material storeMaterial = db.store_material.Where(s => s.store_id == dtos.storeId &&
                                                                           s.material_id == detail.material_id).FirstOrDefault();
                    storeMaterial.amount          = storeMaterial.amount - detail.amount;
                    db.Entry(storeMaterial).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }
            return(transaction);
        }
        private void InsertDate()
        {
            using (InventoryEntities invEntities = new InventoryEntities())
            {
                var new_Master_Id = Int64.MinValue;
                #region insert or update master
                var udm = from m in invEntities.transaction_master
                          where m.transaction_master_id == this.TransactionMasterId
                          select m;
                transaction_master obj = udm.SingleOrDefault();
                if (obj == null)
                {
                    obj = new transaction_master();
                }
                ///set data to object
                obj.transaction_code    = txtTransaction_code.Text.Trim();
                obj.transaction_type_id = 1;
                obj.transaction_date    = dtpTransaction_date.Value;
                if ((Int64)cboReceived_warehouse.SelectedValue != Int64.MinValue)
                {
                    obj.received_warehouse_id = (Int64)cboReceived_warehouse.SelectedValue;
                }
                obj.enabled  = true;
                obj.receiver = txtReceiver.Text.Trim();
                obj.deliver  = txtDeliver.Text.Trim();
                if ((Int64)cboSupplier.SelectedValue != Int64.MinValue)
                {
                    obj.supplier_id = (Int64)cboSupplier.SelectedValue;
                }
                obj.created_date = DateTime.Now;
                obj.create_user  = txtCreate_user.Text.Trim();
                obj.update_date  = DateTime.Now;
                obj.update_user  = txtCreate_user.Text.Trim();
                obj.description  = txtDescription.Text.Trim();
                if (obj.transaction_master_id == 0)
                {
                    invEntities.transaction_master.Add(obj);
                }
                invEntities.SaveChanges();
                new_Master_Id = obj.transaction_master_id;

                #endregion end insert master;
                #region insert detail and item
                for (int i = 0; i < dtTransaction_detail.Rows.Count; i++)
                {
                    if (dtTransaction_detail.Rows[i].RowState == DataRowState.Added || dtTransaction_detail.Rows[i].RowState == DataRowState.Modified)
                    {
                        ///
                        ///Insert or update item
                        ///
                        var temmId = string.IsNullOrEmpty(dtTransaction_detail.Rows[i][colItemId.ColumnName].ToString()) ? Int64.MinValue : (Int64)dtTransaction_detail.Rows[i][colItemId.ColumnName];
                        var udi    = from it in invEntities.items
                                     where it.item_id == temmId
                                     select it;
                        item objItem = udi.SingleOrDefault();
                        if (objItem == null)
                        {
                            objItem = new item();
                        }
                        objItem.item_code        = dtTransaction_detail.Rows[i][colItemCode.ColumnName].ToString();
                        objItem.item_name        = dtTransaction_detail.Rows[i][colItemName.ColumnName].ToString();
                        objItem.unit_id          = (Int64)dtTransaction_detail.Rows[i][colUnitId.ColumnName];
                        objItem.category_item_id = (Int64)dtTransaction_detail.Rows[i][colCategoryItemId.ColumnName];
                        objItem.specifications   = dtTransaction_detail.Rows[i][colSpecifications.ColumnName].ToString();
                        objItem.features         = dtTransaction_detail.Rows[i][colFeatures.ColumnName].ToString();
                        objItem.enabled          = true;
                        objItem.create_user      = obj.create_user;
                        objItem.update_user      = obj.create_user;
                        objItem.created_date     = DateTime.Now;
                        objItem.update_date      = DateTime.Now;
                        if (objItem.item_id == 0)
                        {
                            invEntities.items.Add(objItem);
                        }
                        invEntities.SaveChanges();

                        ///
                        /// insert or update transaction_detail
                        ///
                        var detailId = string.IsNullOrEmpty(dtTransaction_detail.Rows[i][coltransaction_detail_id.DataPropertyName].ToString()) ? Int64.MinValue : (Int64)dtTransaction_detail.Rows[i][coltransaction_detail_id.DataPropertyName];
                        var udd      = from d in invEntities.transaction_detail
                                       where d.transaction_detail_id == detailId
                                       select d;
                        transaction_detail objDetail = udd.SingleOrDefault();
                        if (objDetail == null)
                        {
                            objDetail = new transaction_detail();
                        }
                        objDetail.transaction_master_id = obj.transaction_master_id;
                        objDetail.category_item_id      = (Int64)dtTransaction_detail.Rows[i][colCategoryItemId.ColumnName];
                        objDetail.item_id = objItem.item_id;
                        if (!string.IsNullOrEmpty(dtTransaction_detail.Rows[i][colQuantity.ColumnName].ToString()))
                        {
                            objDetail.quantity = (decimal)dtTransaction_detail.Rows[i][colQuantity.ColumnName];
                        }
                        if (!string.IsNullOrEmpty(dtTransaction_detail.Rows[i][colUnitId.ColumnName].ToString()))
                        {
                            objDetail.unit_id = (Int64)dtTransaction_detail.Rows[i][colUnitId.ColumnName];
                        }
                        objDetail.description  = dtTransaction_detail.Rows[i][colDescription.ColumnName].ToString();
                        objDetail.create_user  = obj.create_user;
                        objDetail.update_user  = obj.create_user;
                        objDetail.created_date = DateTime.Now;
                        objDetail.update_date  = DateTime.Now;
                        if (objDetail.transaction_detail_id == 0)
                        {
                            invEntities.transaction_detail.Add(objDetail);
                        }
                        invEntities.SaveChanges();
                    }
                    else if (dtTransaction_detail.Rows[i].RowState == DataRowState.Deleted)
                    {
                    }
                }

                #endregion end insert detail and item
                #region reload data
                if (new_Master_Id != TransactionMasterId)
                {
                    this.TransactionMasterId = new_Master_Id;
                }
                else
                {
                    getDataCombo();
                    getDataComboForGridView();
                    GetDataById();
                }

                #endregion end reload data
            }
        }