public void MaterialConsumption()
        {
            Boolean temp;

            AccpacCOMAPI.AccpacView       ICADE1header;
            AccpacCOMAPI.AccpacViewFields ICADE1headerFields;
            mDBLinkCmpRW.OpenView("IC0120", out ICADE1header);
            ICADE1headerFields = ICADE1header.Fields;

            AccpacCOMAPI.AccpacView       ICADE1detail1;
            AccpacCOMAPI.AccpacViewFields ICADE1detail1Fields;
            mDBLinkCmpRW.OpenView("IC0110", out ICADE1detail1);
            ICADE1detail1Fields = ICADE1detail1.Fields;

            AccpacCOMAPI.AccpacView       ICADE1detail2;
            AccpacCOMAPI.AccpacViewFields ICADE1detail2Fields;
            mDBLinkCmpRW.OpenView("IC0125", out ICADE1detail2);
            ICADE1detail2Fields = ICADE1detail2.Fields;

            AccpacCOMAPI.AccpacView       ICADE1detail3;
            AccpacCOMAPI.AccpacViewFields ICADE1detail3Fields;
            mDBLinkCmpRW.OpenView("IC0115", out ICADE1detail3);
            ICADE1detail3Fields = ICADE1detail3.Fields;

            AccpacCOMAPI.AccpacView       ICADE1detail4;
            AccpacCOMAPI.AccpacViewFields ICADE1detail4Fields;
            mDBLinkCmpRW.OpenView("IC0113", out ICADE1detail4);
            ICADE1detail4Fields = ICADE1detail4.Fields;

            AccpacCOMAPI.AccpacView       ICADE1detail5;
            AccpacCOMAPI.AccpacViewFields ICADE1detail5Fields;
            mDBLinkCmpRW.OpenView("IC0117", out ICADE1detail5);
            ICADE1detail5Fields = ICADE1detail5.Fields;

            AccpacCOMAPI.AccpacView[] array1 = new AccpacCOMAPI.AccpacView[2];
            array1[0] = ICADE1detail1;
            array1[1] = ICADE1detail2;
            Object obj1 = (Object)array1;

            ICADE1header.Compose(ref obj1);

            AccpacCOMAPI.AccpacView[] array2 = new AccpacCOMAPI.AccpacView[10];
            array2[0] = ICADE1header;
            array2[1] = null;
            array2[2] = null;
            array2[3] = null;
            array2[4] = null;
            array2[5] = null;
            array2[6] = null;
            array2[7] = ICADE1detail3;
            array2[8] = ICADE1detail5;
            array2[9] = ICADE1detail4;
            Object obj2 = (Object)array2;

            ICADE1detail1.Compose(ref obj2);

            AccpacCOMAPI.AccpacView[] array3 = new AccpacCOMAPI.AccpacView[1];
            array3[0] = ICADE1header;
            Object obj3 = (Object)array3;

            ICADE1detail2.Compose(ref obj3);

            AccpacCOMAPI.AccpacView[] array4 = new AccpacCOMAPI.AccpacView[1];
            array4[0] = ICADE1detail1;
            Object obj4 = (Object)array4;

            ICADE1detail3.Compose(ref obj4);

            AccpacCOMAPI.AccpacView[] array5 = new AccpacCOMAPI.AccpacView[1];
            array5[0] = ICADE1detail1;
            Object obj5 = (Object)array5;

            ICADE1detail4.Compose(ref obj5);

            AccpacCOMAPI.AccpacView[] array6 = new AccpacCOMAPI.AccpacView[1];
            array6[0] = ICADE1detail1;
            Object obj6 = (Object)array6;

            ICADE1detail5.Compose(ref obj6);

            ICADE1header.Order = 3;
            ICADE1header.FilterSelect("(DELETED = 0)", true, 3, 0);
            ICADE1header.Order = 3;
            ICADE1header.Order = 0;  /////////////////////////////////////////

            ICADE1headerFields.get_FieldByName("ADJENSEQ").PutWithoutVerification("0");

            ICADE1header.Init();
            temp = ICADE1detail1.Exists;
            ICADE1detail1.RecordClear();
            ICADE1header.Order = 3;

            //Object TRANDATE = (Object)"1/25/2016";
            String formatDate = planningDate.Substring(4, 2) + "/" + planningDate.Substring(6, 2) + "/" + planningDate.Substring(0, 4);
            Object TRANSDATE  = (Object)formatDate;

            ICADE1headerFields.get_FieldByName("TRANSDATE").set_Value(ref TRANSDATE);
            ICADE1headerFields.get_FieldByName("PROCESSCMD").PutWithoutVerification("1");     //' Process Command
            ICADE1header.Process();

            for (int i = 0; i < dataGridViewShow.RowCount; i++)
            {
                string dgvIssueQty = dataGridViewShow.Rows[i].Cells[8].Value.ToString();
                if (Convert.ToDecimal(dgvIssueQty) <= 0)
                {
                    goto down;
                }
                temp = ICADE1detail1.Exists;
                ICADE1detail1.RecordClear();
                temp = ICADE1detail1.Exists;
                ICADE1detail1.RecordCreate(0);
                temp = ICADE1detail1.Exists;

                string dgvItemNo = dataGridViewShow.Rows[i].Cells[1].Value.ToString();

                Object ITEMNO = (Object)dgvItemNo;
                ICADE1detail1Fields.get_FieldByName("ITEMNO").set_Value(ref ITEMNO);
                ICADE1detail1Fields.get_FieldByName("PROCESSCMD").PutWithoutVerification("1"); //' Process Command

                ICADE1detail1.Process();

                Object TRANSTYPE = (Object)"6";   //Transtype 5 for increase data and transtype 6 for decrease data
                ICADE1detail1Fields.get_FieldByName("TRANSTYPE").set_Value(ref TRANSTYPE);

                Object MANITEMNO = (Object)planningDate;
                ICADE1detail1Fields.get_FieldByName("MANITEMNO").set_Value(ref MANITEMNO);

                Object COMMENTS = (Object)"RMT-" + productCategory;
                ICADE1detail1Fields.get_FieldByName("COMMENTS").set_Value(ref COMMENTS);


                string dgvLocation = dataGridViewShow.Rows[i].Cells[5].Value.ToString();

                Object LOCATION = (Object)dgvLocation;
                ICADE1detail1Fields.get_FieldByName("LOCATION").set_Value(ref LOCATION);

                Object QUANTITY = (Object)dgvIssueQty;
                ICADE1detail1Fields.get_FieldByName("QUANTITY").set_Value(ref QUANTITY);

                decimal TransferCost     = ICADE1detail1Fields.get_FieldByName("EXTCOST").get_Value();
                Decimal TransferQuantity = Convert.ToDecimal(dgvIssueQty);

                decimal UnitCost = TransferCost / Convert.ToDecimal(dgvIssueQty);
                WIPCost = WIPCost + TransferCost;
                decimal LotQuantity;
                decimal LotCost;

                DataTable dtLot = new DataTable();

                dtLot = sq.get_rs("Select LOTNUM, QTYAVAIL FROM ICXLOT WHERE ITEMNUM = '" + dgvItemNo.Replace("-", "") + "' and location = '" + dgvLocation + "' and QTYAVAIL <> 0 order by EXPIRYDATE");

                while (TransferQuantity != 0)
                {
                    foreach (DataRow rlot in dtLot.Rows)
                    {
                        if (TransferQuantity <= Convert.ToDecimal(rlot["QTYAVAIL"].ToString()))
                        {
                            LotQuantity      = TransferQuantity;
                            LotCost          = TransferCost;
                            TransferQuantity = TransferQuantity - LotQuantity;
                        }
                        else
                        {
                            LotQuantity = Convert.ToDecimal(rlot["QTYAVAIL"].ToString());
                            LotCost     = (UnitCost * LotQuantity);

                            TransferQuantity = TransferQuantity - LotQuantity;
                            TransferCost     = TransferCost - LotCost;
                        }
                        ICADE1detail4.RecordClear();
                        ICADE1detail4.RecordCreate(0);

                        Object LOTNUMF = (Object)rlot["LOTNUM"].ToString();
                        ICADE1detail4Fields.get_FieldByName("LOTNUMF").set_Value(ref LOTNUMF);

                        Object QTY = (Object)LotQuantity;
                        ICADE1detail4Fields.get_FieldByName("QTY").set_Value(ref QTY);


                        Object COST = (Object)LotCost;
                        ICADE1detail4Fields.get_FieldByName("COST").set_Value(ref COST);

                        ICADE1detail4.Insert();

                        ICADE1detail4Fields.get_FieldByName("LOTNUMF").PutWithoutVerification(rlot["LOTNUM"].ToString());

                        ICADE1detail4.Read();

                        if (TransferQuantity == 0)
                        {
                            goto loopOut;
                        }
                    }
loopOut:
                    ;
                }
                ICADE1detail1.Process();
                ICADE1detail1.Insert();

                ICADE1detail1Fields.get_FieldByName("LINENO").PutWithoutVerification(i * -1); //' Line Number

                ICADE1detail1.Read();
                temp = ICADE1detail1.Exists;
                ICADE1detail1.RecordCreate(0);

                ICADE1detail1Fields.get_FieldByName("LINENO").PutWithoutVerification(i * -1); //' Line Number

                ICADE1detail1.Read();
down:
                ;
            }

            ICADE1headerFields.get_FieldByName("HDRDESC").PutWithoutVerification("Automated Adjustment for Production - RMT-" + productCategory.Trim()); // ' Description

            ICADE1headerFields.get_FieldByName("REFERENCE").PutWithoutVerification(planningDate);                                                        //  ' Reference

            ICADE1header.Insert();
            ICADE1header.Order = 0;

            ICADE1headerFields.get_FieldByName("ADJENSEQ").PutWithoutVerification("0"); //  ' Sequence Number

            ICADE1header.Init();
            temp = ICADE1detail1.Exists;
            ICADE1detail1.RecordClear();
            ICADE1header.Order = 3;
            //MessageBox.Show("Posted successfully");
        }
        private void btnPost_Click(object sender, EventArgs e)
        {
            productCategory = cmbProductGroup.Text.Substring(3, 10);
            planningDate    = dateTimePickerHead.Value.ToString("yyyyMMdd");
            DataTable dtpost = new DataTable();

            dtpost = sq.get_rs("update T_PCTRN set TRANSTATUS=1 where tranDate = '" + planningDate + "' and category = '" + cmbProductGroup.Text.Substring(3, 10) + "'");

            postButtonClicked = 1;
            buttonStatus();
            MessageBox.Show("Posted successfully");
            dataGridViewShow.ReadOnly = true;


            //********************for Macro***********************************
            AccpacCOMAPI.AccpacSession session = new AccpacCOMAPI.AccpacSession();
            this.Cursor = Cursors.WaitCursor;
            session.Init("", "IC60A", "IC2000", "60A");

            session.Open("ADMIN", "ADMIN", "TCPDAT", DateTime.Now, 0, "");

            AccpacCOMAPI.AccpacDBLink mDBLinkCmpRW = session.OpenDBLink(AccpacCOMAPI.tagDBLinkTypeEnum.DBLINK_COMPANY, AccpacCOMAPI.tagDBLinkFlagsEnum.DBLINK_FLG_READWRITE);
            AccpacCOMAPI.AccpacDBLink mDBLinkSysRW = session.OpenDBLink(AccpacCOMAPI.tagDBLinkTypeEnum.DBLINK_SYSTEM, AccpacCOMAPI.tagDBLinkFlagsEnum.DBLINK_FLG_READWRITE);

            //MessageBox.Show("Done");

            Boolean temp;

            AccpacCOMAPI.AccpacView       ICADE1header;
            AccpacCOMAPI.AccpacViewFields ICADE1headerFields;
            mDBLinkCmpRW.OpenView("IC0120", out ICADE1header);
            ICADE1headerFields = ICADE1header.Fields;


            AccpacCOMAPI.AccpacView       ICADE1detail1;
            AccpacCOMAPI.AccpacViewFields ICADE1detail1Fields;
            mDBLinkCmpRW.OpenView("IC0110", out ICADE1detail1);
            ICADE1detail1Fields = ICADE1detail1.Fields;

            AccpacCOMAPI.AccpacView       ICADE1detail2;
            AccpacCOMAPI.AccpacViewFields ICADE1detail2Fields;
            mDBLinkCmpRW.OpenView("IC0125", out ICADE1detail2);
            ICADE1detail2Fields = ICADE1detail2.Fields;

            AccpacCOMAPI.AccpacView       ICADE1detail3;
            AccpacCOMAPI.AccpacViewFields ICADE1detail3Fields;
            mDBLinkCmpRW.OpenView("IC0115", out ICADE1detail3);
            ICADE1detail3Fields = ICADE1detail3.Fields;

            AccpacCOMAPI.AccpacView       ICADE1detail4;
            AccpacCOMAPI.AccpacViewFields ICADE1detail4Fields;
            mDBLinkCmpRW.OpenView("IC0113", out ICADE1detail4);
            ICADE1detail4Fields = ICADE1detail4.Fields;


            AccpacCOMAPI.AccpacView       ICADE1detail5;
            AccpacCOMAPI.AccpacViewFields ICADE1detail5Fields;
            mDBLinkCmpRW.OpenView("IC0117", out ICADE1detail5);
            ICADE1detail5Fields = ICADE1detail5.Fields;


            AccpacCOMAPI.AccpacView[] array1 = new AccpacCOMAPI.AccpacView[2];
            array1[0] = ICADE1detail1;
            array1[1] = ICADE1detail2;
            Object obj1 = (Object)array1;

            ICADE1header.Compose(ref obj1);

            AccpacCOMAPI.AccpacView[] array2 = new AccpacCOMAPI.AccpacView[10];
            array2[0] = ICADE1header;
            array2[1] = null;
            array2[2] = null;
            array2[3] = null;
            array2[4] = null;
            array2[5] = null;
            array2[6] = null;
            array2[7] = ICADE1detail3;
            array2[8] = ICADE1detail5;
            array2[9] = ICADE1detail4;
            Object obj2 = (Object)array2;

            ICADE1detail1.Compose(ref obj2);

            AccpacCOMAPI.AccpacView[] array3 = new AccpacCOMAPI.AccpacView[1];
            array3[0] = ICADE1header;
            Object obj3 = (Object)array3;

            ICADE1detail2.Compose(ref obj3);


            AccpacCOMAPI.AccpacView[] array4 = new AccpacCOMAPI.AccpacView[1];
            array4[0] = ICADE1detail1;
            Object obj4 = (Object)array4;

            ICADE1detail3.Compose(ref obj4);


            AccpacCOMAPI.AccpacView[] array5 = new AccpacCOMAPI.AccpacView[1];
            array5[0] = ICADE1detail1;
            Object obj5 = (Object)array5;

            ICADE1detail4.Compose(ref obj5);


            AccpacCOMAPI.AccpacView[] array6 = new AccpacCOMAPI.AccpacView[1];
            array6[0] = ICADE1detail1;
            Object obj6 = (Object)array6;

            ICADE1detail5.Compose(ref obj6);

            ICADE1header.Order = 3;
            ICADE1header.FilterSelect("(DELETED = 0)", true, 3, 0);
            ICADE1header.Order = 3;
            ICADE1header.Order = 0;  /////////////////////////////////////////

            ICADE1headerFields.get_FieldByName("ADJENSEQ").PutWithoutVerification("0");

            ICADE1header.Init();
            temp = ICADE1detail1.Exists;
            ICADE1detail1.RecordClear();
            ICADE1header.Order = 3;


            //Object TRANDATE = (Object)"1/25/2016";

            String formatDate = planningDate.Substring(4, 2) + "/" + planningDate.Substring(6, 2) + "/" + planningDate.Substring(0, 4);


            Object TRANSDATE = (Object)formatDate;

            ICADE1headerFields.get_FieldByName("TRANSDATE").set_Value(ref TRANSDATE);
            ICADE1headerFields.get_FieldByName("PROCESSCMD").PutWithoutVerification("1");     //' Process Command
            ICADE1header.Process();


            for (int i = 0; i < dataGridViewShow.RowCount; i++)
            {
                temp = ICADE1detail1.Exists;
                ICADE1detail1.RecordClear();
                temp = ICADE1detail1.Exists;
                ICADE1detail1.RecordCreate(0);
                temp = ICADE1detail1.Exists;


                string dgvItemNo = dataGridViewShow.Rows[i].Cells[1].Value.ToString();


                Object ITEMNO = (Object)dgvItemNo;
                ICADE1detail1Fields.get_FieldByName("ITEMNO").set_Value(ref ITEMNO);
                ICADE1detail1Fields.get_FieldByName("PROCESSCMD").PutWithoutVerification("1"); //' Process Command

                ICADE1detail1.Process();

                Object TRANSTYPE = (Object)"5";
                ICADE1detail1Fields.get_FieldByName("TRANSTYPE").set_Value(ref TRANSTYPE);


                string dgvLocation = dataGridViewShow.Rows[i].Cells[5].Value.ToString();

                Object LOCATION = (Object)dgvLocation;
                ICADE1detail1Fields.get_FieldByName("LOCATION").set_Value(ref LOCATION);


                string dgvIssueQty = dataGridViewShow.Rows[i].Cells[8].Value.ToString();

                Object QUANTITY = (Object)dgvIssueQty;
                ICADE1detail1Fields.get_FieldByName("QUANTITY").set_Value(ref QUANTITY);

                ICADE1detail4.RecordClear();
                ICADE1detail4.RecordCreate(0);

                Object LOTNUMF = (Object)"lot";
                ICADE1detail4Fields.get_FieldByName("LOTNUMF").set_Value(ref LOTNUMF);

                Object QTY = (Object)dgvIssueQty;
                ICADE1detail4Fields.get_FieldByName("QTY").set_Value(ref QTY);

                decimal transCost = ICADE1detail1Fields.get_FieldByName("EXTCOST").get_Value();
                decimal unitcost  = transCost / Convert.ToDecimal(dgvIssueQty);
                decimal adjcost   = unitcost * Convert.ToDecimal(dgvIssueQty);

                Object COST = (Object)adjcost;
                ICADE1detail4Fields.get_FieldByName("COST").set_Value(ref COST);

                ICADE1detail4.Insert();

                ICADE1detail4Fields.get_FieldByName("LOTNUMF").PutWithoutVerification("lot");



                ICADE1detail4.Read();
                ICADE1detail1.Process();
                ICADE1detail1.Insert();


                ICADE1detail1Fields.get_FieldByName("LINENO").PutWithoutVerification(i * -1); //' Line Number


                ICADE1detail1.Read();
                temp = ICADE1detail1.Exists;
                ICADE1detail1.RecordCreate(0);

                ICADE1detail1Fields.get_FieldByName("LINENO").PutWithoutVerification(i * -1); //' Line Number

                ICADE1detail1.Read();
            }

            /* ICADE1detail1.Read
            *  temp = ICADE1detail1.Exists
            *  ICADE1detail1.RecordCreate 0
            *  temp = ICADE1detail1.Exists
            *
            *  ICADE1detail1Fields("ITEMNO").Value = "10-16010"                      ' Item Number
            *  ICADE1detail1Fields("PROCESSCMD").PutWithoutVerification("1")        ' Process Command
            *
            *  ICADE1detail1.Process
            *
            *  ICADE1detail1Fields("TRANSTYPE").Value = "5"                          ' Transaction Type
            *  ICADE1detail1Fields("LOCATION").Value = "100115"                      ' Location
            *  ICADE1detail1Fields("QUANTITY").Value = "100.0000"                    ' Quantity
            *  ICADE1detail4.RecordClear
            *  ICADE1detail4.RecordCreate 0
            *
            *  ICADE1detail4Fields("LOTNUMF").Value = "lot"                          ' Lot Number
            *  ICADE1detail4Fields("QTY").Value = "100.0000"                         ' Transaction Quantity
            *  ICADE1detail4Fields("COST").PutWithoutVerification("4082.240")       ' Lot Cost
            *
            *  ICADE1detail4.Insert
            *
            *  ICADE1detail4Fields("LOTNUMF").PutWithoutVerification("lot")         ' Lot Number
            *
            *  ICADE1detail4.Read
            *  ICADE1detail1.Process
            *  ICADE1detail1.Insert
            *
            *  ICADE1detail1Fields("LINENO").PutWithoutVerification("-2")           ' Line Number
            *
            *  ICADE1detail1.Read */


            ICADE1headerFields.get_FieldByName("HDRDESC").PutWithoutVerification("DESC MACRO");  // ' Description

            ICADE1headerFields.get_FieldByName("REFERENCE").PutWithoutVerification("REF MACRO"); //  ' Reference

            ICADE1header.Insert();
            ICADE1header.Order = 0;

            ICADE1headerFields.get_FieldByName("ADJENSEQ").PutWithoutVerification("0"); //  ' Sequence Number

            ICADE1header.Init();
            temp = ICADE1detail1.Exists;
            ICADE1detail1.RecordClear();
            ICADE1header.Order = 3;

            MessageBox.Show("Done");
        }