public void post_undo_fa(saoitrinvhd saoitrinvhd)
        {
            var    data = db.faartrpaymentdtls.Where(d => d.trans_bk == saoitrinvhd.trans_bk && d.trans_no == saoitrinvhd.trans_no).ToList();;
            String ls_trans_no, ls_trans_bk;

            foreach (var ll_currentrow in data)
            {
                ls_trans_no = ll_currentrow.voucher_no.Substring(0, 4);
                ls_trans_bk = ll_currentrow.voucher_no.Substring(5, 11);

                faartrinvhd faartrinvhd = db.faartrinvhds.Where(d => d.trans_bk == saoitrinvhd.trans_bk && d.trans_no == saoitrinvhd.trans_no).FirstOrDefault();;

                if (faartrinvhd != null)
                {
                    faartrinvhd.action_flag     = "0";
                    faartrinvhd.paid_amt        = faartrinvhd.paid_amt - ll_currentrow.apply_amt;
                    faartrinvhd.disctaken_amt   = faartrinvhd.disctaken_amt - ll_currentrow.disctaken_amt;
                    faartrinvhd.balance_amt     = faartrinvhd.inv_amt - (faartrinvhd.paid_amt + faartrinvhd.disctaken_amt) + (ll_currentrow.apply_amt + ll_currentrow.disctaken_amt);
                    db.Entry(faartrinvhd).State = EntityState.Modified;
                }
            }
        }
        public int post_dtl(saoitrinvhd data)
        {
            decimal ldec_price, ldec_net_amt1, ldec_apply_amt;

            if (data.trans_type == "I")
            {
                db.Database.ExecuteSqlCommand("delete from faartrinvdtl where trans_no = '" + data.trans_no + "' && trans_bk = '" + data.trans_bk + "'");
            }

            if (data.trans_type == "C")
            {
                post_undo_fa(data);
            }

            db.Database.ExecuteSqlCommand("delete from faartrpaymentdtl where trans_no = '" + data.trans_no + "' && trans_bk = '" + data.trans_bk + "'");

            if (data.trans_type == "C")
            {
                foreach (var ll_currentrow in data.details)
                {
                    ldec_price = ll_currentrow.item_price ?? 0;

                    if (ll_currentrow.trans_qty != 0)
                    {
                        ldec_price = ll_currentrow.net_amt ?? 0 / ll_currentrow.trans_qty ?? 0;
                    }

                    ldec_net_amt1 = ll_currentrow.item_amt ?? 0;
                    if (ldec_net_amt1 <= 0 || ldec_net_amt1 == null || ll_currentrow.ref_no == null)
                    {
                        //Continue
                    }
                    else
                    {
                        var temp = db.faartrinvhds.Where(d => d.trans_bk == "1234" && d.trans_no == ll_currentrow.ref_no).FirstOrDefault();;
                        if (temp.balance_amt > 0)
                        {
                            if (ldec_net_amt1 > temp.balance_amt)
                            {
                                ldec_apply_amt = temp.balance_amt ?? 0;
                            }
                            else
                            {
                                ldec_apply_amt = ldec_net_amt1;

                                faartrinvhd faartrinvhd_1 = db.faartrinvhds.Where(d => d.trans_bk == "1234" && d.trans_no == ll_currentrow.ref_no).FirstOrDefault();;

                                if (faartrinvhd_1 != null)
                                {
                                    faartrinvhd_1.action_flag     = "0";
                                    faartrinvhd_1.paid_amt        = faartrinvhd_1.paid_amt + ldec_apply_amt;
                                    faartrinvhd_1.balance_amt     = faartrinvhd_1.balance_amt - ldec_apply_amt;
                                    db.Entry(faartrinvhd_1).State = EntityState.Modified;
                                }

                                saoitrinvhd saoitrinvhd_1 = db.saoitrinvhds.Where(d => d.trans_bk == "1234" && d.trans_no == ll_currentrow.ref_no).FirstOrDefault();;

                                if (saoitrinvhd_1 != null)
                                {
                                    saoitrinvhd_1.update_flag = "V";
                                    db.saoitrinvhds.Add(saoitrinvhd_1);
                                }


                                faartrpaymentdtl faartrpaymentdtl_1 = new faartrpaymentdtl();
                                faartrpaymentdtl_1.trans_bk      = ll_currentrow.trans_bk;
                                faartrpaymentdtl_1.trans_no      = ll_currentrow.trans_no;
                                faartrpaymentdtl_1.serial_no     = "100";
                                faartrpaymentdtl_1.trans_dt      = ll_currentrow.trans_dt;
                                faartrpaymentdtl_1.apply_flag    = "N";
                                faartrpaymentdtl_1.voucher_no    = "1234" + ll_currentrow.ref_no;
                                faartrpaymentdtl_1.inv_no        = "1234" + ll_currentrow.ref_no;;
                                faartrpaymentdtl_1.inv_dt        = ll_currentrow.ref_dt;
                                faartrpaymentdtl_1.original_amt  = temp.inv_amt;
                                faartrpaymentdtl_1.balance_amt   = temp.balance_amt;
                                faartrpaymentdtl_1.apply_amt     = ldec_apply_amt;
                                faartrpaymentdtl_1.disctaken_amt = 0;
                                faartrpaymentdtl_1.company_id    = ll_currentrow.company_id;
                                faartrpaymentdtl_1.user_cd       = ll_currentrow.user_cd;
                                faartrpaymentdtl_1.update_dt     = DateTime.Now;
                                faartrpaymentdtl_1.update_flag   = "V";
                                faartrpaymentdtl_1.voucher_dt    = ll_currentrow.ref_dt;
                                faartrpaymentdtl_1.due_dt        = temp.due_dt;

                                db.faartrpaymentdtls.Add(faartrpaymentdtl_1);
                            }
                        }
                    }
                }
            }
            return(200);
        }