Example #1
0
    private int InsertPay(DateTime docDate, string cur)
    {
        string counterType = "";
        int    invId       = 0;

        #region invoice mast
        counterType = "AP-PAYMENT-Job";
        C2.XAApPayment inv = new C2.XAApPayment();


        inv.DocDate = docDate;
        string invN = C2Setup.GetNextNo("", counterType, inv.DocDate);
        inv.PartyTo  = "PSAC";
        inv.DocType  = "PS";
        inv.DocType1 = "Job";
        inv.DocNo    = invN.ToString();
        string[] currentPeriod = EzshipHelper.GetAccPeriod(inv.DocDate);

        inv.AcYear   = SafeValue.SafeInt(currentPeriod[1], inv.DocDate.Year);
        inv.AcPeriod = SafeValue.SafeInt(currentPeriod[0], inv.DocDate.Month);
        inv.ChqNo    = "GIRO";
        //

        inv.Remark     = "";
        inv.CurrencyId = cur;
        inv.ExRate     = 1;
        inv.AcCode     = "120401";
        inv.AcSource   = "CR";
        inv.MastRefNo  = "";
        inv.JobRefNo   = "";

        inv.ExportInd      = "N";
        inv.CreateBy       = HttpContext.Current.User.Identity.Name;
        inv.CreateDateTime = DateTime.Now;
        inv.CancelDate     = new DateTime(1900, 1, 1);
        inv.CancelInd      = "N";
        try
        {
            C2.Manager.ORManager.StartTracking(inv, Wilson.ORMapper.InitialState.Inserted);
            C2.Manager.ORManager.PersistChanges(inv);

            invId = inv.SequenceId;

            invN = C2Setup.GetNextNo("AP-PAYMENT-" + inv.DocType1);
            string sql = string.Format("Update XAApPayment set DocNo='{1}',GenerateInd='Y' where SequenceId='{0}'", invId, invN);
            if (ConnectSql.ExecuteSql(sql) > -1)
            {
                C2Setup.SetNextNo("AP-PAYMENT-" + inv.DocType1, invN);
            }
        }
        catch
        {
        }
        #endregion
        return(invId);
    }
Example #2
0
    public static DataSet DsPayTs(string orderNo)
    {
        ObjectQuery query  = new ObjectQuery(typeof(C2.XAApPayment), "DocNo='" + orderNo + "'", "");
        ObjectSet   objSet = C2.Manager.ORManager.GetObjectSet(query);

        if (objSet.Count == 0 || objSet[0] == null)
        {
            return(new DataSet());
        }
        C2.XAApPayment obj = objSet[0] as C2.XAApPayment;
        #region Invoice
        DataTable mast = InitMastDataTable();
        DataRow   row  = mast.NewRow();
        //string mastRefNo = obj.MastRefNo;
        row["DocNo"]     = obj.DocNo;
        row["DocType"]   = obj.DocType;
        row["DocDate"]   = obj.DocDate.ToString("dd/MM/yyyy");
        row["PartyTo"]   = obj.PartyTo;
        row["PartyName"] = obj.OtherPartyName;
        row["ExRate"]    = obj.ExRate;
        row["Currency"]  = obj.CurrencyId;

        row["Remark"] = obj.Remark;

        row["CompanyName"] = System.Configuration.ConfigurationManager.AppSettings["CompanyName"];

        DataTable details = InitDetailDataTable();
        string    sql     = string.Format(@"select sum(LocAmt) as LocAmt,Remark1 from XAApPaymentDet where PayNo='{0}' group by Remark1", orderNo);
        DataTable det     = ConnectSql.GetTab(sql);
        for (int i = 0; i < det.Rows.Count; i++)
        {
            DataRow row1 = details.NewRow();
            row1["Rmk"]    = SafeValue.SafeString(det.Rows[i]["Remark1"]);
            row1["LocAmt"] = SafeValue.SafeString(det.Rows[i]["LocAmt"]);

            details.Rows.Add(row1);
        }
        row["LocAmt"] = obj.LocAmt;
        mast.Rows.Add(row);

        #endregion
        DataSet set = new DataSet();
        set.Tables.Add(mast);
        set.Tables.Add(details);
        //set.Relations.Add("", mast.Columns["DocNo"], details.Columns["DocNo"]);

        return(set);
    }
Example #3
0
    private void InsertPayment()
    {
        for (int a = 0; a < list.Count; a++)
        {
            int trpId = list[a].id;
            #region Select Trip
            string sql = string.Format(@"with pri as (
select * from ctm_MastData where [Type]='tripcode'
),
tb1 as (
select det2.Id,det2.JobNo,det2.ContainerNo,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='DP' and ChgCode='Trip') as Incentive1,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='DP' and ChgCode='OverTime') as Incentive2,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='DP' and ChgCode='Standby') as Incentive3,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='DP' and ChgCode='PSA') as Incentive4,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='CL' and ChgCode='DHC') as Charge1,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='CL' and ChgCode='WEIGHING') as Charge2,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='CL' and ChgCode='WASHING') as Charge3,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='CL' and ChgCode='REPAIR') as Charge4,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='CL' and ChgCode='DETENTION') as Charge5,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='CL' and ChgCode='DEMURRAGE') as Charge6,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='CL' and ChgCode='LIFT_ON_OFF') as Charge7,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='CL' and ChgCode='C_SHIPMENT') as Charge8,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='CL' and ChgCode='EMF') as Charge9,
(select isnull(sum(isnull(Qty*Price,0)),0) from job_cost where TripNo=det2.Id and LineType='CL' and ChgCode='OTHER') as Charge10,
det1.SealNo,det1.ContainerType,det1.ScheduleDate,det2.FromDate,det2.FromTime,det2.ToDate,det2.ToTime,det2.TripCode,det2.TowheadCode,det2.Overtime,det2.OverDistance,
det2.ChessisCode,det2.FromCode,det2.ToCode,job.JobType,det2.ParkingZone,det2.DriverCode,
isnull((select top 1 case dri.ServiceLevel when 'Level1' then pri.Price1 when 'Level2' then pri.Price2 when 'Level3' then pri.Price3 else 0 end from pri where pri.Code=det2.TripCode),0) as TripCodePrice,
isnull((select top 1 case dri.ServiceLevel when 'Level1' then pri.Price1 when 'Level2' then pri.Price2 when 'Level3' then pri.Price3 else 0 end from pri where pri.Code=det2.Overtime),0) as OverTimePrice,
case when det2.OverDistance='Y' then isnull((select top 1 case dri.ServiceLevel when 'Level1' then pri.Price1 when 'Level2' then pri.Price2 when 'Level3' then pri.Price3 else 0 end from pri where pri.Code='OJ'),0) else 0 end as QJPrice
from CTM_JobDet2 as det2
left outer join CTM_JobDet1 as det1 on det2.Det1Id=det1.Id
left outer join CTM_Job as job on job.jobNo=det2.JobNo
left outer join CTM_Driver as dri on dri.Code=det2.DriverCode ");
            if (search_DateFrom.Date < new DateTime(1900, 1, 1))
            {
                search_DateFrom.Date = DateTime.Now.AddDays(-15);
            }
            if (search_DateTo.Date < new DateTime(1900, 1, 1))
            {
                search_DateTo.Date = DateTime.Now.AddDays(8);
            }

            string sql_part1 = string.Format(@")
select *,TripCodePrice+OverTimePrice+QJPrice as Total,isnull(Charge1,0)+isnull(Charge2,0)+isnull(Charge3,0)+isnull(Charge4,0)+isnull(Charge5,0)+isnull(Charge6,0)+isnull(Charge7,0)+isnull(Charge8,0)+isnull(Charge9,0)+isnull(Charge10,0)  as TotalCharge from tb1");
            string where = string.Format(@" where det2.Id={2} and det2.Statuscode='C' and DATEDIFF(d,det2.ToDate,'{0}')<=0 and DATEDIFF(d,det2.ToDate,'{1}')>=0", search_DateFrom.Date, search_DateTo.Date, trpId);
            if (search_Driver.Text.Trim().Length > 0)
            {
                where += string.Format(@" and det2.DriverCode='{0}'", search_Driver.Text);
            }
            if (search_TowheadCode.Text.Trim().Length > 0)
            {
                where += string.Format(@" and det2.TowheadCode='{0}'", search_TowheadCode.Text);
            }
            if (cbb_Trip_TripCode.Text.Trim() != "")
            {
                where += string.Format(@" and det2.TripCode='{0}'", cbb_Trip_TripCode.Text);
            }
            if (cbb_zone.Text.Trim() != "")
            {
                where += string.Format(@" and det2.ParkingZone='{0}'", cbb_zone.Text);
            }
            #endregion
            string    new_sql = sql + where + sql_part1;
            DataTable tab     = ConnectSql.GetTab(new_sql);
            int       id      = 0;
            string    str     = "";
            for (int i = 0; i < tab.Rows.Count; i++)
            {
                #region
                string   tripId     = SafeValue.SafeString(tab.Rows[i]["Id"]);
                DateTime toDate     = SafeValue.SafeDate(tab.Rows[i]["ToDate"], DateTime.Today);
                string   contNo     = SafeValue.SafeString(tab.Rows[i]["ContainerNo"]);
                string   cntType    = SafeValue.SafeString(tab.Rows[i]["ContainerType"]);
                string   primeMover = SafeValue.SafeString(tab.Rows[i]["TowheadCode"]);
                string   trailer    = SafeValue.SafeString(tab.Rows[i]["ChessisCode"]);
                string   driver     = SafeValue.SafeString(tab.Rows[i]["DriverCode"]);
                string   jobNo      = SafeValue.SafeString(tab.Rows[i]["JobNo"]);
                decimal  dhc        = SafeValue.SafeDecimal(tab.Rows[i]["Charge1"], 0);
                decimal  weight     = SafeValue.SafeDecimal(tab.Rows[i]["Charge2"], 0);
                decimal  washing    = SafeValue.SafeDecimal(tab.Rows[i]["Charge3"], 0);
                decimal  repair     = SafeValue.SafeDecimal(tab.Rows[i]["Charge4"], 0);
                decimal  detention  = SafeValue.SafeDecimal(tab.Rows[i]["Charge5"], 0);
                decimal  demurrage  = SafeValue.SafeDecimal(tab.Rows[i]["Charge6"], 0);
                decimal  lift       = SafeValue.SafeDecimal(tab.Rows[i]["Charge7"], 0);
                decimal  c_Shipment = SafeValue.SafeDecimal(tab.Rows[i]["Charge8"], 0);
                decimal  emf        = SafeValue.SafeDecimal(tab.Rows[i]["Charge10"], 0);
                decimal  other      = SafeValue.SafeDecimal(tab.Rows[i]["Charge9"], 0);

                DateTime voucher_date = date_voucher.Date;

                if (dhc > 0 || weight > 0 || washing > 0 || repair > 0 || detention > 0 || demurrage > 0 || lift > 0 || c_Shipment > 0 || emf > 0 || other > 0)
                {
                    sql = string.Format(@"select count(*) from XAApPayment where PartyTo='CASH' and OtherPartyName='{0}'  and convert(nvarchar(100),DocDate,23)='{1}'", driver, voucher_date.ToString("yyyy-MM-dd"));
                    int cnt = SafeValue.SafeInt(Helper.Sql.One(sql), 0);
                    if (cmb_voucher_mode.Value.ToString() == "New" && a == 0)
                    {
                        cnt = 0;
                    }

                    XAApPayment inv = null;
                    if (cnt == 0)
                    {
                        #region Payment
                        inv = new C2.XAApPayment();

                        inv.PartyTo        = "CASH";
                        inv.OtherPartyName = SafeValue.SafeString(driver);
                        string invN = C2Setup.GetNextNo("PS", "AP-PAYMENT-Cash", voucher_date);
                        inv.DocNo    = invN;
                        inv.DocType  = "PS";
                        inv.DocType1 = "Cash";
                        inv.DocDate  = voucher_date;
                        string[] currentPeriod = EzshipHelper.GetAccPeriod(voucher_date);

                        inv.AcYear     = SafeValue.SafeInt(currentPeriod[1], voucher_date.Year);
                        inv.AcPeriod   = SafeValue.SafeInt(currentPeriod[0], voucher_date.Month);
                        inv.ChqDate    = voucher_date;
                        inv.Remark     = "";
                        inv.CurrencyId = "SGD";
                        inv.ExRate     = 1;
                        inv.PoNo       = "";
                        if (inv.ExRate <= 0)
                        {
                            inv.ExRate = 1;
                        }
                        inv.AcCode   = System.Configuration.ConfigurationManager.AppSettings["DefaultBankCode"];
                        inv.AcSource = "CR";
                        if (inv.DocType1.ToLower() == "refund")
                        {
                            inv.AcSource = "DB";
                        }
                        //inv.BankName = bankName.Text;
                        inv.ChqNo          = "";
                        inv.ExportInd      = "N";
                        inv.DocAmt         = 0;
                        inv.LocAmt         = inv.DocAmt * inv.ExRate;
                        inv.Pic            = "";
                        inv.BankRec        = "";
                        inv.BankDate       = new DateTime(1900, 1, 1);
                        inv.CancelDate     = new DateTime(1900, 1, 1);
                        inv.CancelInd      = "N";
                        inv.CreateBy       = HttpContext.Current.User.Identity.Name;
                        inv.CreateDateTime = DateTime.Now;
                        inv.UpdateBy       = HttpContext.Current.User.Identity.Name;
                        inv.UpdateDateTime = DateTime.Now;
                        inv.PostBy         = "";
                        inv.PostDateTime   = new DateTime(1900, 1, 1);
                        inv.GenerateInd    = "N";
                        try
                        {
                            C2.Manager.ORManager.StartTracking(inv, Wilson.ORMapper.InitialState.Inserted);
                            C2.Manager.ORManager.PersistChanges(inv);
                            //inv.DocNo = inv.SequenceId.ToString();
                            C2.Manager.ORManager.StartTracking(inv, Wilson.ORMapper.InitialState.Updated);
                            C2.Manager.ORManager.PersistChanges(inv);
                            C2Setup.SetNextNo("", "AP-PAYMENT-Cash", inv.DocNo, voucher_date);
                        }
                        catch
                        {
                        }
                        #endregion
                        #region det
                        if (dhc > 0)
                        {
                            Insert_Det(inv.SequenceId, inv.DocNo, inv.DocType, 1, dhc, "DHC", contNo, jobNo, tripId);
                        }
                        if (weight > 0)
                        {
                            Insert_Det(inv.SequenceId, inv.DocNo, inv.DocType, 1, weight, "WEIGHING", contNo, jobNo, tripId);
                        }
                        if (washing > 0)
                        {
                            Insert_Det(inv.SequenceId, inv.DocNo, inv.DocType, 1, washing, "WASHING", contNo, jobNo, tripId);
                        }
                        if (repair > 0)
                        {
                            Insert_Det(inv.SequenceId, inv.DocNo, inv.DocType, 1, repair, "REPAIR", contNo, jobNo, tripId);
                        }
                        if (detention > 0)
                        {
                            Insert_Det(inv.SequenceId, inv.DocNo, inv.DocType, 1, detention, "DETENTION", contNo, jobNo, tripId);
                        }
                        if (demurrage > 0)
                        {
                            Insert_Det(inv.SequenceId, inv.DocNo, inv.DocType, 1, demurrage, "DEMURRAGE", contNo, jobNo, tripId);
                        }
                        if (lift > 0)
                        {
                            Insert_Det(inv.SequenceId, inv.DocNo, inv.DocType, 1, lift, "LIFT ON/OFF", contNo, jobNo, tripId);
                        }
                        if (c_Shipment > 0)
                        {
                            Insert_Det(inv.SequenceId, inv.DocNo, inv.DocType, 1, c_Shipment, "C/SHIPMENT", contNo, jobNo, tripId);
                        }
                        if (emf > 0)
                        {
                            Insert_Det(inv.SequenceId, inv.DocNo, inv.DocType, 1, emf, "EMF", contNo, jobNo, tripId);
                        }
                        if (other > 0)
                        {
                            Insert_Det(inv.SequenceId, inv.DocNo, inv.DocType, 1, other, "OTHER", contNo, jobNo, tripId);
                        }
                        #endregion
                        str = jobNo + " | " + contNo + " | " + cntType;
                        UpdateApPayment(inv.SequenceId);
                        id = inv.SequenceId;
                    }
                    else
                    {
                        str += "\n" + jobNo + " | " + contNo + " | " + cntType;
                        sql  = string.Format(@"select top 1 SequenceId from XAApPayment where PartyTo='CASH' and OtherPartyName='{0}' and convert(nvarchar(100),DocDate,23)='{1}'  order by sequenceid desc", driver, voucher_date.ToString("yyyy-MM-dd"));
                        id   = SafeValue.SafeInt(Helper.Sql.One(sql), 0);
                        #region det
                        Wilson.ORMapper.OPathQuery query = new Wilson.ORMapper.OPathQuery(typeof(C2.XAApPayment), "SequenceId=" + id + "");
                        C2.XAApPayment             inv1  = C2.Manager.ORManager.GetObject(query) as C2.XAApPayment;
                        if (dhc > 0)
                        {
                            Insert_Det(inv1.SequenceId, inv1.DocNo, inv1.DocType, 1, dhc, "DHC", contNo, jobNo, tripId);
                        }
                        if (weight > 0)
                        {
                            Insert_Det(inv1.SequenceId, inv1.DocNo, inv1.DocType, 1, weight, "WEIGHING", contNo, jobNo, tripId);
                        }
                        if (washing > 0)
                        {
                            Insert_Det(inv1.SequenceId, inv1.DocNo, inv1.DocType, 1, washing, "WASHING", contNo, jobNo, tripId);
                        }
                        if (repair > 0)
                        {
                            Insert_Det(inv1.SequenceId, inv1.DocNo, inv1.DocType, 1, repair, "REPAIR", contNo, jobNo, tripId);
                        }
                        if (detention > 0)
                        {
                            Insert_Det(inv1.SequenceId, inv1.DocNo, inv1.DocType, 1, detention, "DETENTION", contNo, jobNo, tripId);
                        }
                        if (demurrage > 0)
                        {
                            Insert_Det(inv1.SequenceId, inv1.DocNo, inv1.DocType, 1, demurrage, "DEMURRAGE", contNo, jobNo, tripId);
                        }
                        if (lift > 0)
                        {
                            Insert_Det(inv1.SequenceId, inv1.DocNo, inv1.DocType, 1, lift, "LIFT ON/OFF", contNo, jobNo, tripId);
                        }
                        if (c_Shipment > 0)
                        {
                            Insert_Det(inv1.SequenceId, inv1.DocNo, inv1.DocType, 1, c_Shipment, "C/SHIPMENT", contNo, jobNo, tripId);
                        }
                        if (emf > 0)
                        {
                            Insert_Det(inv1.SequenceId, inv1.DocNo, inv1.DocType, 1, emf, "EMF", contNo, jobNo, tripId);
                        }
                        if (other > 0)
                        {
                            Insert_Det(inv1.SequenceId, inv1.DocNo, inv1.DocType, 1, other, "OTHER", contNo, jobNo, tripId);
                        }
                        #endregion



                        inv1.Remark = "";//str;
                        C2.Manager.ORManager.StartTracking(inv1, Wilson.ORMapper.InitialState.Updated);
                        C2.Manager.ORManager.PersistChanges(inv1);

                        UpdateApPayment(id);
                    }
                }
                else
                {
                    //sql = string.Format(@"select SequenceId,DocId,DocType from XAApPayment where PartyTo='{0}' and DocDate='{1}'", driver, DateTime.Now);
                    //DataTable in = ConnectSql.GetTab(sql);
                    //int id = SafeValue.SafeInt(Helper.Sql.One(sql), 0);
                }
                #endregion
            }
        }
    }
    protected void ASPxGridView1_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        string      action = e.Parameters;
        ASPxTextBox oidCtr = this.ASPxGridView1.FindEditFormTemplateControl("txt_Oid") as ASPxTextBox;
        ASPxTextBox docN   = this.ASPxGridView1.FindEditFormTemplateControl("txt_DocNo") as ASPxTextBox;

        if (action == "S")
        {
            ASPxComboBox partyTo        = this.ASPxGridView1.FindEditFormTemplateControl("cmb_PartyTo") as ASPxComboBox;
            ASPxTextBox  otherPartyName = this.ASPxGridView1.FindEditFormTemplateControl("txt_OtherPartyName") as ASPxTextBox;
            ASPxComboBox docType        = this.ASPxGridView1.FindEditFormTemplateControl("cbo_DocType") as ASPxComboBox;
            ASPxComboBox docType1       = this.ASPxGridView1.FindEditFormTemplateControl("cbo_DocType1") as ASPxComboBox;
            ASPxDateEdit docDate        = this.ASPxGridView1.FindEditFormTemplateControl("txt_DocDt") as ASPxDateEdit;
            ASPxMemo     remark         = this.ASPxGridView1.FindEditFormTemplateControl("txt_Remarks1") as ASPxMemo;

            ASPxTextBox    docCurr = this.ASPxGridView1.FindEditFormTemplateControl("txt_Currency") as ASPxTextBox;
            ASPxSpinEdit   exRate  = this.ASPxGridView1.FindEditFormTemplateControl("txt_DocExRate") as ASPxSpinEdit;
            ASPxButtonEdit acCode  = this.ASPxGridView1.FindEditFormTemplateControl("txt_AcCode") as ASPxButtonEdit;

            ASPxTextBox  bankName = this.ASPxGridView1.FindEditFormTemplateControl("txt_BankName") as ASPxTextBox;
            ASPxTextBox  acDorc   = this.ASPxGridView1.FindEditFormTemplateControl("txt_AcDorc") as ASPxTextBox;
            ASPxTextBox  chqNo    = this.ASPxGridView1.FindEditFormTemplateControl("txt_ChequeNo") as ASPxTextBox;
            ASPxDateEdit chqDt    = this.ASPxGridView1.FindEditFormTemplateControl("txt_ChqDt") as ASPxDateEdit;
            ASPxTextBox  pic      = this.ASPxGridView1.FindEditFormTemplateControl("txt_Pic") as ASPxTextBox;

            ASPxSpinEdit   docAmt = this.ASPxGridView1.FindEditFormTemplateControl("spin_DocAmt") as ASPxSpinEdit;
            C2.XAApPayment inv    = Manager.ORManager.GetObject(typeof(XAApPayment), SafeValue.SafeInt(oidCtr.Text, 0)) as XAApPayment;
            bool           isNew  = false;
            if (null == inv)            // first insert invoice
            {
                isNew = true;
                //string invN = C2Setup.GetNextNo("PS", "AP-PAYMENT-Job", docDate.Date)
                string invN = C2Setup.GetNextNo("PS", "AP-PAYMENT-" + docType1.Value.ToString(), docDate.Date);                //C2Setup.GetNextNo("AR-RECEIPT");
                //string invN = C2Setup.GetNextNo("AP-PAYMENT");
                inv = new C2.XAApPayment();
                if (partyTo != null)
                {
                    inv.PartyTo = SafeValue.SafeString(partyTo.Value, "");
                }
                inv.DocNo = invN;
                if (docType != null)
                {
                    inv.DocType = docType.Value.ToString();
                }
                if (docType1 != null)
                {
                    inv.DocType1 = docType1.Value.ToString();
                }
                if (docDate != null)
                {
                    inv.DocDate = docDate.Date;
                }
                string[] currentPeriod = EzshipHelper.GetAccPeriod(docDate.Date);

                inv.AcYear   = SafeValue.SafeInt(currentPeriod[1], docDate.Date.Year);
                inv.AcPeriod = SafeValue.SafeInt(currentPeriod[0], docDate.Date.Month);
                inv.ChqDate  = chqDt.Date;
                inv.Remark   = remark.Text;
                //if (docCurr != null)
                inv.CurrencyId = "SGD";                 //docCurr.Text.ToString();
                inv.ExRate     = SafeValue.SafeDecimal(exRate.Value, 1);
                if (inv.ExRate <= 0)
                {
                    inv.ExRate = 1;
                }
                if (acCode != null)
                {
                    inv.AcCode = acCode.Text;
                }
                inv.AcSource = "CR";
                if (inv.DocType1.ToLower() == "refund")
                {
                    inv.AcSource = "DB";
                }
                //inv.BankName = bankName.Text;
                if (chqNo != null)
                {
                    inv.ChqNo = chqNo.Text;
                }
                inv.ExportInd = "N";
                inv.DocAmt    = SafeValue.SafeDecimal(docAmt.Value, 0);
                inv.LocAmt    = inv.DocAmt * inv.ExRate;
                if (pic != null)
                {
                    inv.Pic = pic.Text;
                }
                inv.BankRec    = "";
                inv.BankDate   = new DateTime(1900, 1, 1);
                inv.CancelDate = new DateTime(1900, 1, 1);
                inv.CancelInd  = "N";

                inv.OtherPartyName = otherPartyName.Text;
                inv.CreateBy       = HttpContext.Current.User.Identity.Name;
                inv.CreateDateTime = DateTime.Now;
                inv.UpdateBy       = HttpContext.Current.User.Identity.Name;
                inv.UpdateDateTime = DateTime.Now;
                inv.PostBy         = "";
                inv.PostDateTime   = new DateTime(1900, 1, 1);
                inv.GenerateInd    = "N";
                try
                {
                    C2.Manager.ORManager.StartTracking(inv, Wilson.ORMapper.InitialState.Inserted);
                    C2.Manager.ORManager.PersistChanges(inv);
                    //inv.DocNo = inv.SequenceId.ToString();
                    C2.Manager.ORManager.StartTracking(inv, Wilson.ORMapper.InitialState.Updated);
                    C2.Manager.ORManager.PersistChanges(inv);
                    //C2Setup.SetNextNo("AP-PAYMENT", invN);
                    //C2Setup.SetNextNo("", "AP-PAYMENT-Job", invN, inv.DocDate);
                    C2Setup.SetNextNo("", "AP-PAYMENT-" + inv.DocType1, invN, inv.DocDate);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message + ex.StackTrace);
                }
            }
            else
            {
                inv.PartyTo = SafeValue.SafeString(partyTo.Value, "");
                inv.DocDate = docDate.Date;
                inv.ChqDate = chqDt.Date;
                inv.Remark  = remark.Text;
                if (docCurr != null)
                {
                    inv.CurrencyId = docCurr.Text.ToString();
                }
                inv.ExRate = SafeValue.SafeDecimal(exRate.Value, 1);
                if (inv.ExRate <= 0)
                {
                    inv.ExRate = 1;
                }
                if (acCode != null)
                {
                    inv.AcCode = acCode.Text;
                }
                //inv.BankName = bankName.Text;
                if (chqNo != null)
                {
                    inv.ChqNo = chqNo.Text;
                }
                inv.DocAmt = SafeValue.SafeDecimal(docAmt.Value, 0);
                inv.LocAmt = inv.DocAmt * inv.ExRate;
                string[] currentPeriod = EzshipHelper.GetAccPeriod(docDate.Date);
                if (pic != null)
                {
                    inv.Pic = pic.Text;
                }

                inv.AcYear   = SafeValue.SafeInt(currentPeriod[1], docDate.Date.Year);
                inv.AcPeriod = SafeValue.SafeInt(currentPeriod[0], docDate.Date.Month);

                inv.OtherPartyName = otherPartyName.Text;
                inv.UpdateBy       = HttpContext.Current.User.Identity.Name;
                inv.UpdateDateTime = DateTime.Now;
                try
                {
                    Manager.ORManager.StartTracking(inv, InitialState.Updated);
                    Manager.ORManager.PersistChanges(inv);
                }
                catch
                {
                }
            }

            if (isNew)
            {
                Session["ApPsWhere"] = "SequenceId=" + inv.SequenceId;
                this.dsApPayment.FilterExpression = Session["ApPsWhere"].ToString();
                if (this.ASPxGridView1.GetRow(0) != null)
                {
                    this.ASPxGridView1.StartEdit(0);
                }
            }
        }
        else if (action == "D")
        {
        }
    }
    protected void ASPxGridView1_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        ASPxTextBox oidCtr = this.ASPxGridView1.FindEditFormTemplateControl("txt_Oid") as ASPxTextBox;
        //ASPxTextBox acYear = this.ASPxGridView1.FindEditFormTemplateControl("txt_AcYear") as ASPxTextBox;
        //ASPxTextBox acPeriod = this.ASPxGridView1.FindEditFormTemplateControl("txt_AcPeriod") as ASPxTextBox;
        // ASPxTextBox party = this.ASPxGridView1.FindEditFormTemplateControl("txt_PartyCode") as ASPxTextBox;
        ASPxComboBox partyTo = this.ASPxGridView1.FindEditFormTemplateControl("cmb_PartyTo") as ASPxComboBox;
        ASPxTextBox  docN    = this.ASPxGridView1.FindEditFormTemplateControl("txt_DocNo") as ASPxTextBox;
        ASPxComboBox docType = this.ASPxGridView1.FindEditFormTemplateControl("cbo_DocType") as ASPxComboBox;
        ASPxDateEdit docDate = this.ASPxGridView1.FindEditFormTemplateControl("txt_DocDt") as ASPxDateEdit;
        ASPxMemo     remark  = this.ASPxGridView1.FindEditFormTemplateControl("txt_Remarks1") as ASPxMemo;

        ASPxTextBox  docCurr = this.ASPxGridView1.FindEditFormTemplateControl("txt_Currency") as ASPxTextBox;
        ASPxSpinEdit exRate  = this.ASPxGridView1.FindEditFormTemplateControl("txt_DocExRate") as ASPxSpinEdit;
        ASPxTextBox  acCode  = this.ASPxGridView1.FindEditFormTemplateControl("txt_AcCode") as ASPxTextBox;

        ASPxTextBox  bankName = this.ASPxGridView1.FindEditFormTemplateControl("txt_BankName") as ASPxTextBox;
        ASPxTextBox  acDorc   = this.ASPxGridView1.FindEditFormTemplateControl("txt_AcDorc") as ASPxTextBox;
        ASPxTextBox  chqNo    = this.ASPxGridView1.FindEditFormTemplateControl("txt_ChequeNo") as ASPxTextBox;
        ASPxDateEdit chqDt    = this.ASPxGridView1.FindEditFormTemplateControl("txt_ChqDt") as ASPxDateEdit;

        ASPxSpinEdit docAmt = this.ASPxGridView1.FindEditFormTemplateControl("spin_DocAmt") as ASPxSpinEdit;

        C2.XAApPayment inv   = Manager.ORManager.GetObject(typeof(XAApPayment), SafeValue.SafeInt(oidCtr.Text, 0)) as XAApPayment;
        bool           isNew = false;

        if (null == inv)// first insert invoice
        {
            isNew = true;
            string invN = C2Setup.GetNextNo("AP-PAYMENT");
            inv            = new C2.XAApPayment();
            inv.PartyTo    = SafeValue.SafeString(partyTo.Value, "");
            inv.DocNo      = invN;
            inv.DocType    = docType.Value.ToString();
            inv.DocDate    = docDate.Date;
            inv.ChqDate    = chqDt.Date;
            inv.Remark     = remark.Text;
            inv.CurrencyId = docCurr.Text.ToString();
            inv.ExRate     = SafeValue.SafeDecimal(exRate.Value, 1);
            if (inv.ExRate <= 0)
            {
                inv.ExRate = 1;
            }
            inv.AcCode   = acCode.Text;
            inv.AcSource = "DB";
            //inv.BankName = bankName.Text;
            inv.ChqNo  = chqNo.Text;
            inv.DocAmt = SafeValue.SafeDecimal(docAmt.Value, 0);
            inv.LocAmt = inv.DocAmt * inv.ExRate;

            inv.BankRec   = "";
            inv.BankDate  = new DateTime(1900, 1, 1);
            inv.ExportInd = "N";
            inv.CloseInd  = "N";
            inv.CancelInd = "N";
            string[] currentPeriod = EzshipHelper.GetAccPeriod(docDate.Date);

            inv.AcYear         = SafeValue.SafeInt(currentPeriod[1], docDate.Date.Year);
            inv.AcPeriod       = SafeValue.SafeInt(currentPeriod[0], docDate.Date.Month);
            inv.CreateBy       = HttpContext.Current.User.Identity.Name;
            inv.CreateDateTime = DateTime.Now;
            inv.UpdateBy       = HttpContext.Current.User.Identity.Name;
            inv.UpdateDateTime = DateTime.Now;
            inv.PostBy         = "";
            inv.PostDateTime   = new DateTime(1900, 1, 1);
            try
            {
                C2.Manager.ORManager.StartTracking(inv, Wilson.ORMapper.InitialState.Inserted);
                C2.Manager.ORManager.PersistChanges(inv);
                C2Setup.SetNextNo("AP-PAYMENT", invN);
            }
            catch
            {
            }
        }
        else
        {
            inv.PartyTo    = SafeValue.SafeString(partyTo.Value, "");
            inv.DocDate    = docDate.Date;
            inv.ChqDate    = chqDt.Date;
            inv.Remark     = remark.Text;
            inv.CurrencyId = docCurr.Text.ToString();
            inv.ExRate     = SafeValue.SafeDecimal(exRate.Value, 1);
            if (inv.ExRate <= 0)
            {
                inv.ExRate = 1;
            }
            inv.AcCode = acCode.Text;
            //inv.BankName = bankName.Text;
            inv.ChqNo  = chqNo.Text;
            inv.DocAmt = SafeValue.SafeDecimal(docAmt.Value, 0);
            inv.LocAmt = inv.DocAmt * inv.ExRate;
            string[] currentPeriod = EzshipHelper.GetAccPeriod(docDate.Date);

            inv.AcYear         = SafeValue.SafeInt(currentPeriod[1], docDate.Date.Year);
            inv.AcPeriod       = SafeValue.SafeInt(currentPeriod[0], docDate.Date.Month);
            inv.UpdateBy       = HttpContext.Current.User.Identity.Name;
            inv.UpdateDateTime = DateTime.Now;

            try
            {
                Manager.ORManager.StartTracking(inv, InitialState.Updated);
                Manager.ORManager.PersistChanges(inv);
            }
            catch
            { }
        }
        if (isNew)
        {
            Session["ApSRWhere"] = "SequenceId=" + inv.SequenceId;
            this.dsApPayment.FilterExpression = Session["ApSRWhere"].ToString();
            if (this.ASPxGridView1.GetRow(0) != null)
            {
                this.ASPxGridView1.StartEdit(0);
            }
        }
    }