예제 #1
0
        /// <summary>
        /// Generate Shipments
        /// </summary>
        /// <returns>info</returns>
        protected override String DoIt()
        {
            bool isExpense = false;

            if (ConsolidateDocument == "Y")
            {
                string        sqlSelect = "select * from s_timeexpenseline where processed = 'Y' and (ARInvoice = 'Y' or billtocustomer = 'Y')  and Ref_C_Invoice_ID is null";
                StringBuilder sqlWhere  = new StringBuilder();
                if (C_Order_ID != 0)
                {
                    sqlWhere.Append(" AND C_Order_ID = " + C_Order_ID);
                }
                else
                {
                    if (_C_BPartner_ID != 0)
                    {
                        sqlWhere.Append(" AND C_BPartner_ID = " + _C_BPartner_ID);
                    }
                    if (AD_Org_ID != 0)
                    {
                        sqlWhere.Append(" AND AD_Org_ID = " + AD_Org_ID);
                    }
                }

                if (sqlWhere.Length > 0)
                {
                    sql = sqlSelect + sqlWhere.ToString();
                }
                else
                {
                    sql = sqlSelect;
                }

                sql = sql + " order by C_order_ID, c_bpartner_id";

                IDataReader idr = null;
                try
                {
                    idr = DB.ExecuteReader(sql, null, null);
                    while (idr.Read())
                    {
                        if (C_BPartner_ID.Contains(Util.GetValueOfInt(idr["C_BPartner_ID"])))
                        {
                            VAdvantage.Model.MOrder ord = new VAdvantage.Model.MOrder(GetCtx(), Util.GetValueOfInt(idr["C_Order_ID"]), null);
                            bool chk = false;
                            for (int i = 0; i < invoices.Count; i++)
                            {
                                VAdvantage.Model.MInvoice inv = new VAdvantage.Model.MInvoice(GetCtx(), Util.GetValueOfInt(invoices[i]), null);

                                if ((inv.GetC_PaymentTerm_ID() == ord.GetC_PaymentTerm_ID()) && (inv.GetM_PriceList_ID() == ord.GetM_PriceList_ID()))
                                {
                                    chk = true;
                                    break;
                                }
                            }
                            if (!chk)
                            {
                                VAdvantage.Model.X_S_TimeExpenseLine tLine = new VAdvantage.Model.X_S_TimeExpenseLine(GetCtx(), Util.GetValueOfInt(idr["s_timeexpenseline_id"]), null);
                                VAdvantage.Model.X_S_TimeExpense     tExp  = new VAdvantage.Model.X_S_TimeExpense(GetCtx(), Util.GetValueOfInt(tLine.GetS_TimeExpense_ID()), null);
                                int C_Invoice_ID = GenerateInvoice(tLine, tExp, isExpense);
                                invoices.Add(C_Invoice_ID);
                            }
                        }
                        else
                        {
                            C_BPartner_ID.Add(Util.GetValueOfInt(idr["C_BPartner_ID"]));
                            VAdvantage.Model.X_S_TimeExpenseLine tLine = new VAdvantage.Model.X_S_TimeExpenseLine(GetCtx(), Util.GetValueOfInt(idr["s_timeexpenseline_id"]), null);
                            VAdvantage.Model.X_S_TimeExpense     tExp  = new VAdvantage.Model.X_S_TimeExpense(GetCtx(), Util.GetValueOfInt(tLine.GetS_TimeExpense_ID()), null);
                            int C_Invoice_ID = GenerateInvoice(tLine, tExp, isExpense);
                            invoices.Add(C_Invoice_ID);
                        }
                    }
                    if (idr != null)
                    {
                        idr.Close();
                        idr = null;
                    }

                    //  isExpense = false;

                    if (invoices.Count > 0)
                    {
                        for (int k = 0; k < invoices.Count; k++)
                        //  foreach (KeyValuePair<int, int> pair in BPInvoice)
                        {
                            sqlWhere = new StringBuilder();
                            if (C_Order_ID != 0)
                            {
                                sqlWhere.Append(" AND C_Order_ID = " + C_Order_ID);
                            }
                            else
                            {
                                if (_C_BPartner_ID != 0)
                                {
                                    sqlWhere.Append(" AND C_BPartner_ID = " + _C_BPartner_ID);
                                }
                                if (AD_Org_ID != 0)
                                {
                                    sqlWhere.Append(" AND AD_Org_ID = " + AD_Org_ID);
                                }
                            }

                            if (sqlWhere.Length > 0)
                            {
                                sql = sqlSelect + sqlWhere.ToString();
                            }
                            else
                            {
                                sql = sqlSelect;
                            }

                            sql = sql + " order by C_order_ID, c_bpartner_id";

                            DataSet ds = DB.ExecuteDataset(sql, null, null);
                            if (ds != null)
                            {
                                if (ds.Tables[0].Rows.Count > 0)
                                {
                                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                                    {
                                        int  invID = 0;
                                        bool chk1  = false;
                                        for (int l = 0; l < invoices.Count; l++)
                                        {
                                            VAdvantage.Model.MOrder   ord  = new VAdvantage.Model.MOrder(GetCtx(), Util.GetValueOfInt(ds.Tables[0].Rows[j]["C_Order_ID"]), null);
                                            VAdvantage.Model.MInvoice inv1 = new VAdvantage.Model.MInvoice(GetCtx(), Util.GetValueOfInt(invoices[l]), null);
                                            if ((inv1.GetC_PaymentTerm_ID() == ord.GetC_PaymentTerm_ID()) && (inv1.GetC_BPartner_ID() == ord.GetC_BPartner_ID()) &&
                                                (inv1.GetM_PriceList_ID() == ord.GetM_PriceList_ID()))
                                            {
                                                chk1  = true;
                                                invID = inv1.GetC_Invoice_ID();
                                                break;
                                            }
                                        }
                                        if (chk1)
                                        {
                                            //CreateLine(Util.GetValueOfInt(ds.Tables[0].Rows[j]["S_TimeExpenseLine_ID"]), Util.GetValueOfInt(pair.Value));
                                            CreateLine(Util.GetValueOfInt(ds.Tables[0].Rows[j]["S_TimeExpenseLine_ID"]), invID);
                                        }
                                    }

                                    for (int m = 0; m < invoices.Count; m++)
                                    {
                                        //MInvoice inv = new MInvoice(GetCtx(), Util.GetValueOfInt(pair.Value), null);
                                        VAdvantage.Model.MInvoice inv = new VAdvantage.Model.MInvoice(GetCtx(), Util.GetValueOfInt(invoices[m]), null);
                                        if (docAction == "CO")
                                        {
                                            string comp = inv.CompleteIt();
                                            if (comp == "CO")
                                            {
                                                inv.SetDocAction("CL");
                                                inv.SetDocStatus("CO");
                                                inv.Save();
                                            }
                                        }
                                        else if (docAction == "PR")
                                        {
                                            string prp = inv.PrepareIt();
                                            if (prp == "IP")
                                            {
                                                inv.SetDocAction("PR");
                                                inv.SetDocStatus("IP");
                                                inv.Save();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch
                {
                    if (idr != null)
                    {
                        idr.Close();
                        idr = null;
                    }
                    return(Msg.GetMsg(GetCtx(), "ProcessTerminated"));
                }
            }

            // If Consolidate Document is Not Checked
            else
            {
                string        sqlSelect = "select * from s_timeexpenseline where processed = 'Y' and (ARInvoice = 'Y' or billtocustomer = 'Y')  and Ref_C_Invoice_ID is null";
                StringBuilder sqlWhere  = new StringBuilder();
                if (C_Order_ID != 0)
                {
                    sqlWhere.Append(" AND C_Order_ID = " + C_Order_ID);
                }
                else
                {
                    if (_C_BPartner_ID != 0)
                    {
                        sqlWhere.Append(" AND C_BPartner_ID = " + _C_BPartner_ID);
                    }
                    if (AD_Org_ID != 0)
                    {
                        sqlWhere.Append(" AND AD_Org_ID = " + AD_Org_ID);
                    }
                }

                if (sqlWhere.Length > 0)
                {
                    sql = sqlSelect + sqlWhere.ToString();
                }
                else
                {
                    sql = sqlSelect;
                }

                sql = sql + " order by C_order_ID, c_bpartner_id";
                IDataReader idr = null;
                try
                {
                    idr = DB.ExecuteReader(sql, null, null);
                    int C_Invoice_ID = 0;
                    while (idr.Read())
                    {
                        if (orders.Contains(Util.GetValueOfInt(idr["C_Order_ID"])))
                        {
                            CreateLine(Util.GetValueOfInt(idr["s_timeexpenseline_id"]), C_Invoice_ID);
                        }
                        else
                        {
                            orders.Add(Util.GetValueOfInt(idr["C_Order_ID"]));
                            VAdvantage.Model.X_S_TimeExpenseLine tLine = new VAdvantage.Model.X_S_TimeExpenseLine(GetCtx(), Util.GetValueOfInt(idr["s_timeexpenseline_id"]), null);
                            VAdvantage.Model.X_S_TimeExpense     tExp  = new VAdvantage.Model.X_S_TimeExpense(GetCtx(), Util.GetValueOfInt(tLine.GetS_TimeExpense_ID()), null);
                            C_Invoice_ID = GenerateInvoice(tLine, tExp, isExpense);
                            invoices.Add(C_Invoice_ID);
                            CreateLine(Util.GetValueOfInt(idr["s_timeexpenseline_id"]), C_Invoice_ID);
                        }
                    }
                    for (int m = 0; m < invoices.Count; m++)
                    {
                        //MInvoice inv = new MInvoice(GetCtx(), Util.GetValueOfInt(pair.Value), null);
                        VAdvantage.Model.MInvoice inv = new VAdvantage.Model.MInvoice(GetCtx(), Util.GetValueOfInt(invoices[m]), null);
                        if (docAction == "CO")
                        {
                            string comp = inv.CompleteIt();
                            if (comp == "CO")
                            {
                                inv.SetDocAction("CL");
                                inv.SetDocStatus("CO");
                                inv.Save();
                            }
                        }
                        else if (docAction == "PR")
                        {
                            string prp = inv.PrepareIt();
                            if (prp == "IP")
                            {
                                inv.SetDocAction("PR");
                                inv.SetDocStatus("IP");
                                inv.Save();
                            }
                        }
                    }

                    if (idr != null)
                    {
                        idr.Close();
                        idr = null;
                    }
                }
                catch
                {
                    if (idr != null)
                    {
                        idr.Close();
                        idr = null;
                    }
                }
            }

            string docNo = "";

            for (int i = 0; i < invoices.Count; i++)
            {
                VAdvantage.Model.MInvoice inv = new VAdvantage.Model.MInvoice(GetCtx(), Util.GetValueOfInt(invoices[i]), null);
                docNo = docNo + ", " + inv.GetDocumentNo();
            }
            if (docNo != "")
            {
                docNo = docNo.Remove(0, 2);
            }
            return(Msg.GetMsg(GetCtx(), "InvoicesCreated" + " : " + docNo));
            // return Msg.GetMsg(GetCtx(), "ProcessCompleted");
        }