Esempio n. 1
0
        /// <summary>
        /// Perform Process.
        /// </summary>
        /// <returns>Message (variables are parsed)</returns>
        protected override String DoIt()
        {
            log.Info("doIt - C_CommissionRun_ID=" + GetRecord_ID());
            //	Load Data
            MCommissionRun comRun = new MCommissionRun(GetCtx(), GetRecord_ID(), Get_Trx());

            if (comRun.Get_ID() == 0)
            {
                throw new ArgumentException("CommissionAPInvoice - No Commission Run");
            }
            if (Env.ZERO.CompareTo(comRun.GetGrandTotal()) == 0)
            {
                throw new ArgumentException("@GrandTotal@ = 0");
            }
            MCommission com = new MCommission(GetCtx(), comRun.GetC_Commission_ID(), Get_Trx());

            if (com.Get_ID() == 0)
            {
                throw new ArgumentException("CommissionAPInvoice - No Commission");
            }
            if (com.GetC_Charge_ID() == 0)
            {
                throw new ArgumentException("CommissionAPInvoice - No Charge on Commission");
            }
            MBPartner bp = new MBPartner(GetCtx(), com.GetC_BPartner_ID(), Get_Trx());

            if (bp.Get_ID() == 0)
            {
                throw new ArgumentException("CommissionAPInvoice - No BPartner");
            }

            //	Create Invoice
            MInvoice invoice = new MInvoice(GetCtx(), 0, null);

            invoice.SetClientOrg(com.GetAD_Client_ID(), com.GetAD_Org_ID());
            invoice.SetC_DocTypeTarget_ID(MDocBaseType.DOCBASETYPE_APINVOICE);  //	API
            invoice.SetBPartner(bp);
            //	invoice.setDocumentNo (comRun.getDocumentNo());		//	may cause unique constraint
            invoice.SetSalesRep_ID(GetAD_User_ID());    //	caller
            //
            if (com.GetC_Currency_ID() != invoice.GetC_Currency_ID())
            {
                throw new ArgumentException("CommissionAPInvoice - Currency of PO Price List not Commission Currency");
            }
            //
            if (!invoice.Save())
            {
                throw new Exception("CommissionAPInvoice - cannot save Invoice");
            }

            //	Create Invoice Line
            MInvoiceLine iLine = new MInvoiceLine(invoice);

            iLine.SetC_Charge_ID(com.GetC_Charge_ID());
            iLine.SetQty(1);
            iLine.SetPrice(comRun.GetGrandTotal());
            iLine.SetTax();
            if (!iLine.Save())
            {
                throw new Exception("CommissionAPInvoice - cannot save Invoice Line");
            }
            //
            return("@C_Invoice_ID@ = " + invoice.GetDocumentNo());
        }
        /// <summary>
        /// Perform Process.
        /// </summary>
        /// <returns>Message (variables are parsed)</returns>
        protected override String DoIt()
        {
            log.Info("doIt - C_CommissionRun_ID=" + GetRecord_ID());
            //	Load Data
            MCommissionRun comRun = new MCommissionRun(GetCtx(), GetRecord_ID(), Get_Trx());

            if (comRun.Get_ID() == 0)
            {
                throw new ArgumentException("CommissionAPInvoice - No Commission Run");
            }
            if (Env.ZERO.CompareTo(comRun.GetGrandTotal()) == 0)
            {
                throw new ArgumentException("@GrandTotal@ = 0");
            }
            MCommission com = new MCommission(GetCtx(), comRun.GetC_Commission_ID(), Get_Trx());

            if (com.Get_ID() == 0)
            {
                throw new ArgumentException("CommissionAPInvoice - No Commission");
            }
            if (com.GetC_Charge_ID() == 0)
            {
                throw new ArgumentException("CommissionAPInvoice - No Charge on Commission");
            }
            MBPartner bp = new MBPartner(GetCtx(), com.GetC_BPartner_ID(), Get_Trx());

            if (bp.Get_ID() == 0)
            {
                throw new ArgumentException("CommissionAPInvoice - No BPartner");
            }

            //	Create Invoice
            MInvoice invoice = new MInvoice(GetCtx(), 0, null);

            invoice.SetClientOrg(com.GetAD_Client_ID(), com.GetAD_Org_ID());
            invoice.SetC_DocTypeTarget_ID(MDocBaseType.DOCBASETYPE_APINVOICE);  //	API
            invoice.SetBPartner(bp);

            // JID_0101: When we generate the AP invoice from Commission run window, its giving price list error.
            if (invoice.GetM_PriceList_ID() == 0)
            {
                string sql = "SELECT M_PriceList_ID FROM M_PriceList WHERE IsActive = 'Y' AND AD_Client_ID = " + com.GetAD_Client_ID() + " AND AD_Org_ID = " + com.GetAD_Org_ID()
                             + " AND IsDefault='Y' AND IsSOPriceList='N'";
                int pricelist = Util.GetValueOfInt(DB.ExecuteScalar(sql, null, Get_Trx()));
                if (pricelist == 0)
                {
                    pricelist = MPriceList.GetDefault(GetCtx(), false).Get_ID();
                }
                if (pricelist > 0)
                {
                    invoice.SetM_PriceList_ID(pricelist);
                }
            }
            //	invoice.setDocumentNo (comRun.getDocumentNo());		//	may cause unique constraint
            invoice.SetSalesRep_ID(GetAD_User_ID());    //	caller
            //
            if (com.GetC_Currency_ID() != invoice.GetC_Currency_ID())
            {
                throw new ArgumentException("CommissionAPInvoice - Currency of PO Price List not Commission Currency");
            }
            //
            if (!invoice.Save())
            {
                //return GetReterivedError(invoice, "CommissionAPInvoice - cannot save Invoice");
                throw new Exception("CommissionAPInvoice - cannot save Invoice");
            }

            //	Create Invoice Line
            MInvoiceLine iLine = new MInvoiceLine(invoice);

            iLine.SetC_Charge_ID(com.GetC_Charge_ID());
            iLine.SetQty(1);
            iLine.SetPrice(comRun.GetGrandTotal());
            iLine.SetTax();
            if (!iLine.Save())
            {
                //return GetReterivedError(iLine, "CommissionAPInvoice - cannot save Invoice Line");
                throw new Exception("CommissionAPInvoice - cannot save Invoice Line");
            }
            //
            return("@C_Invoice_ID@ = " + invoice.GetDocumentNo());
        }