Exemple #1
0
        public void insert(InitMaterielTable record, bool isDisplayMessageBox = true)
        {
            double price       = 0;
            double materielNum = 0;

            string insert = "INSERT INTO [dbo].[INIT_STORAGE_STOCK]([MATERIEL_ID],[VALUE],[PRICE]) VALUES (";

            price       = record.price;
            materielNum = record.value;

            /* 插入逻辑
             * 1、检查库存表是否已经存在该物料的库存信息
             * 2、得到物料的计价方式,后进先出或是先进后出或是移动加权平均
             * 3、如果是移动加权平均,计算移动加权单价
             * 4、如果是后进先出或是先进后出计价方式,直接插入到数据库
             * */
            if (checkMaterielIsExist(record.materielID))
            {
                InitMaterielTable storageExistMaterielRecord = getMaterielInfoFromMaterielID(record.materielID);

                // 根据id,得到物料的详细信息,进一步得到物料的计价方式
                MaterielTable materielInfo  = Materiel.getInctance().getMaterielInfoFromPkey(storageExistMaterielRecord.materielID);
                int           valuation     = materielInfo.valuation;
                string        valuationName = AuxiliaryMaterial.getInctance().getAuxiliaryMaterialNameFromPkey("BASE_VALUATION_TYPE_LIST", valuation);

                if (valuation == 0 || valuationName.IndexOf("加权") != -1)
                {
                    double newPrice = MobileWeightedUnitPrice.calculateMaterielNewPrice(
                        storageExistMaterielRecord.price,
                        storageExistMaterielRecord.value,
                        price, materielNum);

                    InitMaterielTable newRecord = new InitMaterielTable();
                    newRecord.pkey       = storageExistMaterielRecord.pkey;
                    newRecord.materielID = storageExistMaterielRecord.materielID;
                    newRecord.price      = newPrice;
                    newRecord.value      = materielNum + storageExistMaterielRecord.value;

                    update(newRecord.pkey, newRecord, isDisplayMessageBox);
                    return;
                }
            }

            // 如果不是加权平均或仓库中还不存在该物料信息,直接插入本条记录
            insert += record.materielID + ",";
            insert += materielNum + ",";
            insert += "'" + Convert.ToString(price) + "'";
            insert += ")";

            try
            {
                DatabaseAccessFactoryInstance.Instance.ExecuteCommand(FormMain.DB_NAME, insert);

                if (isDisplayMessageBox)
                {
                    MessageBoxExtend.messageOK("数据保存成功");
                }

                load();
            }
            catch (Exception error)
            {
                MessageBoxExtend.messageWarning(error.Message);
                return;
            }
        }