global class NewQuoteEntryWebService {

    private static Map<String, String> paymentTermsMap = new Map<String, String> {
        'Z001' => '预收款/预付款',
        'Z002' => '立即付款',
        'Z003' => '月结30天内付款',
        'Z004' => '月结60天内付款',
        'Z005' => '月结90天内付款',
        'Z006' => '月结120天内付款',
        'Z007' => '月结25天内付款',
        'Z008' => '月结55天内付款',
        'Z009' => '月结85天内付款',
        'Z010' => '月结115天内付款',
        'Z011' => '月结175天内付款',
        'Z012' => '月结205天内付款',
        'Z013' => '半月结，15日之前发货，当月15日到期',
        'Z014' => '月结30天内付款',
        'Z015' => '验收后15天电汇',
        'Z020' => '100%发货后30天内支付',
        'Z021' => '100%发货后60天内电汇支付',
        'Z030' => '收到发票后30天内电汇',
        'Z045' => '月结45天内付款',
        'Z060' => '收到发票后60天内电汇',
        'Z075' => '收到发票后75天内电汇',
        'Z090' => '收到发票后90天内电汇',
        'Z0LC' => '100% L/C (90/10)',
        'Z120' => '收到发票后120天内电汇',
        'Z1LC' => '100% L/C (80/20)',
        'Z1TT' => '90%TT ADV，10%TT AFTER',
        'Z2LC' => '100% L/C (70/30)',
        'Z2TT' => '80%TT ADV，20%TT AFTER',
        'Z30E' => '下月末',
        'Z3LC' => '100% L/C 90 days',
        'Z3TT' => '70%TT ADV，30%TT AFTER',
        'Z4LC' => '90%L/CTransfer 10%TT AFTER',
        'Z4TT' => '100% TT AFTER',
        'Z5LC' => '100%L/C at sight Transfer',
        'Z5TT' => '30% TT ADV，50% TT AT SIGHT，20% TT AFTER',
        'Z60E' => '下2个月末',
        'Z6LC' => '100%L/C (85/15)',
        'Z6TT' => '30% TT ADV，60% TT AT SIGHT，10% TT AFTER',
        'Z7LC' => '100% L/C (60/40)',
        'Z7TT' => '100％TT AFTER(80/20)',
        'Z8TT' => '100% TT AFTER（70/30）',
        'Z9TT' => '100%TT发货后20天内支付',
        'ZCOD' => 'C.O.D.',
        'ZD60' => '立即应付的 到期净值',
        'ZFR1' => '半月结，15日之前发货，当月15日到期',
        'ZL6T' => '90% LC 60 days，10% TT AFTER',
        'ZL95' => '95% L/C AFTER,5% L/C AFTER 6M FROM ACCEPTANCE DATE',
        'ZLC1' => '100% L/C (90/10) Transfer',
        'ZLC2' => '100% L/C AT SIGHT (95/5)',
        'ZLT1' => '90%LC，10%TT',
        'ZLT2' => '80%LC，20%TT',
        'ZLT3' => '70%LC，30%TT',
        'ZLT4' => '95%LC，5%TT',
        'ZLT5' => '50%L/C，50%TT after',
        'ZT20' => '20% T/T ADV,80% T/T AFTER',
        'ZT30' => '30% T/T ADV, 70%T/T AFTER',
        'ZT35' => '35% TT ADV，65% TT AFTER',
        'ZT40' => '40% TT ADV，60% TT AFTER',
        'ZT50' => '50% TT ADV，50% TT AFTER',
        'ZT55' => '95%TT ADV，5%TT AFTER',
        'ZT90' => '验收合格后T/T支付90%，设备正常运行三个月T/T支付10%',
        'ZTT1' => 'TT AFTER DELIVERY 45 DAY',
        'ZTT2' => 'TT AFTER DELIVERY 120 DAY',
        'ZTT3' => '100%TT发货后15个工作日内支付',
        'ZTT4' => '验收合格后T/T支付80%，设备正常运行三个月T/T支付20%',
        'ZZ60' => '60 DAYS AFTER ISSUING INVOICE',
        'ZZ95' => '95% L/C AFTER,5% T/T AFTER 6M FROM ACCEPTANCE DATE',
        'ZZLC' => '100% L/C AT SIGHT',
        'ZZTT' => '100% TT IN ADVANCE'
    };

	WebService static String selectQuotation(String oppId, String quoId) {
        String rs = '';

        List<Quote> quotes = [select Id,Name,QuoteNumber,Account.name,ExpirationDate,Subtotal,Discount,TotalPrice,GrandTotal,PaymentTerms__c
                                from Quote
                               where Id = :quoId];
        List<QuoteLineItem> qlis = [select Id,Product2.name,Product2.ProductCode,Quantity,Subtotal,TotalPrice,PricebookEntryId,UnitPrice,Discount,Description
                                      from QuoteLineItem
                                     where Quoteid = :quoId
                                     Order by Id];

        Savepoint sp = Database.setSavepoint();
        try {
            if (quotes.size() > 0) {
                Quote quo = quotes[0];

                Opportunity o = New Opportunity();
                List<Opportunity> os = [select Id From Opportunity Where Id =:oppId];
                if (os.size()>0) {
                    o = os[0];
                    o.SyncedQuoteId = quo.Id;
                    o.Quote_PaymentTerms_Text__c = paymentTermsMap.get(quo.PaymentTerms__c);
                    update o;

 /*                   List<OpportunityLineItem> ols = [select Id from OpportunityLineItem Where OpportunityId =:oppId];
                    if (ols.size()>0) {
                        delete ols;
                    }

                    ols = New List<OpportunityLineItem>();
                    for (QuoteLineItem qli : qlis) {
                        OpportunityLineItem ol = New OpportunityLineItem();
                        ol.PricebookEntryId = qli.PricebookEntryId;
                        ol.OpportunityId = oppId;
                        ol.Quantity =  qli.Quantity;
                        ol.UnitPrice = qli.UnitPrice;
                        ol.Description = qli.Description;
                        ols.add(ol);
                    }

                    if (ols.size()>0) {
                        insert ols;
                    }*/
                }
            }

            rs = '1';
        } catch (Exception e) {
            Database.rollback(sp);
            rs = e.getMessage();
        }

        return rs;
	}
}