KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > CalloutOrder


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.math.*;
17 import java.util.*;
18 import java.sql.*;
19
20 import org.compiere.util.*;
21
22 /**
23  * Order Callouts.
24  *
25  * @author Jorg Janke
26  * @version $Id: CalloutOrder.java,v 1.2 2003/11/06 07:08:05 jjanke Exp $
27  */

28 public class CalloutOrder extends CalloutEngine
29 {
30
31     /**
32      * Order Header Change - DocType.
33      * - InvoiceRuld/DeliveryRule/PaymentRule
34      * - IsApproved
35      * - temporary Document
36      * Context:
37      * - DocSubTypeSO
38      * - HasCharges
39      * - (re-sets Business Partner info of required)
40      *
41      * @param ctx Context
42      * @param WindowNo current Window No
43      * @param mTab Model Tab
44      * @param mField Model Field
45      * @param value The new value
46      * @return Error message or ""
47      */

48     public String JavaDoc docType (Properties ctx, int WindowNo, MTab mTab, MField mField, Object JavaDoc value)
49     {
50         Integer JavaDoc C_DocType_ID = (Integer JavaDoc)value; // Actually C_DocTypeTarget_ID
51
if (C_DocType_ID == null || C_DocType_ID.intValue() == 0)
52             return "";
53
54         // Re-Create new DocNo, if there is a doc number already
55
// and the existing source used a different Sequence number
56
String JavaDoc oldDocNo = (String JavaDoc)mTab.getValue("DocumentNo");
57         boolean newDocNo = (oldDocNo == null);
58         if (!newDocNo && oldDocNo.startsWith("<") && oldDocNo.endsWith(">"))
59             newDocNo = true;
60         Integer JavaDoc oldC_DocType_ID = (Integer JavaDoc)mTab.getValue("C_DocType_ID");
61
62         try
63         {
64             String JavaDoc SQL = "SELECT d.DocSubTypeSO,d.HasCharges,d.IsApproved," // 1..3
65
+ "d.IsDocNoControlled,s.CurrentNext,s.CurrentNextSys," // 4..6
66
+ "s.AD_Sequence_ID,d.IsSOTrx " // 7..8
67
+ "FROM C_DocType d, AD_Sequence s "
68                 + "WHERE C_DocType_ID=?" // #1
69
+ " AND d.DocNoSequence_ID=s.AD_Sequence_ID(+)";
70             int AD_Sequence_ID = 0;
71
72             // Get old AD_SeqNo for comparison
73
if (!newDocNo && oldC_DocType_ID.intValue() != 0)
74             {
75                 PreparedStatement pstmt = DB.prepareStatement(SQL);
76                 pstmt.setInt(1, oldC_DocType_ID.intValue());
77                 ResultSet rs = pstmt.executeQuery();
78                 if (rs.next())
79                     AD_Sequence_ID = rs.getInt(6);
80                 rs.close();
81                 pstmt.close();
82             }
83
84             PreparedStatement pstmt = DB.prepareStatement(SQL);
85             pstmt.setInt(1, C_DocType_ID.intValue());
86             ResultSet rs = pstmt.executeQuery();
87             String JavaDoc DocSubTypeSO = "";
88             boolean IsSOTrx = true;
89             if (rs.next()) // we found document type
90
{
91                 // Set Context: Document Sub Type for Sales Orders
92
DocSubTypeSO = rs.getString(1);
93                 if (DocSubTypeSO == null)
94                     DocSubTypeSO = "--";
95                 Env.setContext(ctx, WindowNo, "OrderType", DocSubTypeSO);
96
97                 if (DocSubTypeSO.equals(MOrder.DocSubTypeSO_Prepay))
98                 {
99                     mTab.setValue ("InvoiceRule", MOrder.INVOICERULE_Immediate);
100                     mTab.setValue ("DeliveryRule", MOrder.DELIVERYRULE_AfterReceipt);
101                 }
102                 else
103                 {
104                     mTab.setValue ("InvoiceRule", MOrder.INVOICERULE_AfterDelivery);
105                     mTab.setValue ("DeliveryRule", MOrder.DELIVERYRULE_Availability);
106                 }
107                 if (DocSubTypeSO.equals(MOrder.DocSubTypeSO_POS)) // POS Order
108
mTab.setValue("PaymentRule", MOrder.PAYMENTRULE_Cash);
109                 else
110                     mTab.setValue("PaymentRule", MOrder.PAYMENTRULE_OnCredit);
111
112                 // IsSOTrx
113
if ("N".equals(rs.getString(8)))
114                     IsSOTrx = false;
115
116                 // Set Context:
117
Env.setContext(ctx, WindowNo, "HasCharges", rs.getString(2));
118                 // Approval required?
119
String JavaDoc YN = "Y";
120                 if (rs.getString(3).equals("Y"))
121                     YN = "N";
122                 mTab.setValue("IsApproved", YN);
123                 Env.setContext(ctx, WindowNo, "IsApproved", YN); // otherwise overwritten by default
124
// DocumentNo
125
if (rs.getString(4).equals("Y")) // IsDocNoControlled
126
{
127                     if (!newDocNo && AD_Sequence_ID != rs.getInt(7))
128                         newDocNo = true;
129                     if (newDocNo)
130                         if (Ini.getPropertyBool(Ini.P_COMPIERESYS) && Env.getAD_Client_ID(Env.getCtx()) < 1000000)
131                             mTab.setValue("DocumentNo", "<" + rs.getString(6) + ">");
132                         else
133                             mTab.setValue("DocumentNo", "<" + rs.getString(5) + ">");
134                 }
135             }
136             rs.close();
137             pstmt.close();
138             // When BPartner is changed, the Rules are not set if
139
// it is a POS or Credit Order (i.e. defaults from Standard BPartner)
140
// This re-reads the Rules and applies them.
141
if (DocSubTypeSO.equals(MOrder.DocSubTypeSO_POS) || DocSubTypeSO.equals(MOrder.DocSubTypeSO_Prepay)) // not for POS/PrePay
142
;
143             else
144             {
145                 SQL = "SELECT PaymentRule,C_PaymentTerm_ID," // 1..2
146
+ "InvoiceRule,DeliveryRule," // 3..4
147
+ "FreightCostRule,DeliveryViaRule, " // 5..6
148
+ "PaymentRulePO,PO_PaymentTerm_ID "
149                     + "FROM C_BPartner "
150                     + "WHERE C_BPartner_ID=?"; // #1
151
pstmt = DB.prepareStatement(SQL);
152                 int C_BPartner_ID = Env.getContextAsInt(ctx, WindowNo, "C_BPartner_ID");
153                 pstmt.setInt(1, C_BPartner_ID);
154                 //
155
rs = pstmt.executeQuery();
156                 if (rs.next())
157                 {
158                     // PaymentRule
159
String JavaDoc s = rs.getString(IsSOTrx ? "PaymentRule" : "PaymentRulePO");
160                     if (s != null && s.length() != 0)
161                     {
162                         if (IsSOTrx && (s.equals("B") || s.equals("S") || s.equals("U"))) // No Cash/Check/Transfer for SO_Trx
163
s = "P"; // Payment Term
164
if (!IsSOTrx && (s.equals("B"))) // No Cash for PO_Trx
165
s = "P"; // Payment Term
166
mTab.setValue("PaymentRule", s);
167                     }
168                     // Payment Term
169
Integer JavaDoc ii =new Integer JavaDoc(rs.getInt(IsSOTrx ? "C_PaymentTerm_ID" : "PO_PaymentTerm_ID"));
170                     if (!rs.wasNull())
171                         mTab.setValue("C_PaymentTerm_ID", ii);
172                     // InvoiceRule
173
s = rs.getString(3);
174                     if (s != null && s.length() != 0)
175                         mTab.setValue("InvoiceRule", s);
176                     // DeliveryRule
177
s = rs.getString(4);
178                     if (s != null && s.length() != 0)
179                         mTab.setValue("DeliveryRule", s);
180                     // FreightCostRule
181
s = rs.getString(5);
182                     if (s != null && s.length() != 0)
183                         mTab.setValue("FreightCostRule", s);
184                     // DeliveryViaRule
185
s = rs.getString(6);
186                     if (s != null && s.length() != 0)
187                         mTab.setValue("DeliveryViaRule", s);
188                 }
189                 rs.close();
190                 pstmt.close();
191             } // re-read customer rules
192
}
193         catch (SQLException e)
194         {
195             log.error("docType", e);
196             return e.getLocalizedMessage();
197         }
198
199         return "";
200     } // docType
201

202
203     /**
204      * Order Header - BPartner.
205      * - M_PriceList_ID (+ Context)
206      * - BillTo_ID
207      * - C_BPartner_Location_ID
208      * - AD_User_ID
209      * - POReference
210      * - SO_Description
211      * - IsDiscountPrinted
212      * - InvoiceRule/DeliveryRule/PaymentRule/FreightCost/DeliveryViaRule
213      * - C_PaymentTerm_ID
214      * @param ctx Context
215      * @param WindowNo current Window No
216      * @param mTab Model Tab
217      * @param mField Model Field
218      * @param value The new value
219      * @return Error message or ""
220      */

221     public String JavaDoc bPartner (Properties ctx, int WindowNo, MTab mTab, MField mField, Object JavaDoc value)
222     {
223         Integer JavaDoc C_BPartner_ID = (Integer JavaDoc)value;
224         if (C_BPartner_ID == null || C_BPartner_ID.intValue() == 0)
225             return "";
226
227         String JavaDoc sql = "SELECT p.AD_Language,p.C_PaymentTerm_ID,"
228             + "p.M_PriceList_ID,p.PaymentRule,p.POReference,"
229             + "p.SO_Description,p.IsDiscountPrinted,"
230             + "p.InvoiceRule,p.DeliveryRule,p.FreightCostRule,DeliveryViaRule,"
231             + "p.SO_CreditLimit-p.SO_CreditUsed AS CreditAvailable,"
232             + "lship.C_BPartner_Location_ID,c.AD_User_ID,"
233             + "p.PO_PriceList_ID, p.PaymentRulePO, p.PO_PaymentTerm_ID,"
234             + "lbill.C_BPartner_Location_ID AS BillTo_ID "
235             + "FROM C_BPartner p"
236             + " LEFT OUTER JOIN C_BPartner_Location lbill ON (p.C_BPartner_ID=lbill.C_BPartner_ID AND lbill.IsBillTo='Y' AND lbill.IsActive='Y')"
237             + " LEFT OUTER JOIN C_BPartner_Location lship ON (p.C_BPartner_ID=lship.C_BPartner_ID AND lship.IsShipTo='Y' AND lship.IsActive='Y')"
238             + " LEFT OUTER JOIN AD_User c ON (p.C_BPartner_ID=c.C_BPartner_ID) "
239             + "WHERE p.C_BPartner_ID=? AND p.IsActive='Y'"; // #1
240

241         boolean IsSOTrx = "Y".equals(Env.getContext(ctx, WindowNo, "IsSOTrx"));
242
243         try
244         {
245             PreparedStatement pstmt = DB.prepareStatement(sql);
246             pstmt.setInt(1, C_BPartner_ID.intValue());
247             ResultSet rs = pstmt.executeQuery();
248             if (rs.next())
249             {
250                 // PriceList (indirect: IsTaxIncluded & Currency)
251
Integer JavaDoc ii = new Integer JavaDoc(rs.getInt(IsSOTrx ? "M_PriceList_ID" : "PO_PriceList_ID"));
252                 if (!rs.wasNull())
253                     mTab.setValue("M_PriceList_ID", ii);
254                 else
255                 { // get default PriceList
256
int i = Env.getContextAsInt(ctx, "#M_PriceList_ID");
257                     if (i != 0)
258                         mTab.setValue("M_PriceList_ID", new Integer JavaDoc(i));
259                 }
260
261                 // Bill-To Location
262
int billTo_ID = rs.getInt("BillTo_ID");
263                 if (billTo_ID == 0)
264                     mTab.setValue("BillTo_ID", null);
265                 else
266                     mTab.setValue("BillTo_ID", new Integer JavaDoc(billTo_ID));
267                 // Ship-To Location
268
int shipTo_ID = rs.getInt("C_BPartner_Location_ID");
269                 // overwritten by InfoBP selection - works only if InfoWindow
270
// was used otherwise creates error (uses last value, may belong to differnt BP)
271
if (C_BPartner_ID.toString().equals(Env.getContext(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "C_BPartner_ID")))
272                 {
273                     String JavaDoc loc = Env.getContext(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "C_BPartner_Location_ID");
274                     if (loc.length() > 0)
275                         shipTo_ID = Integer.parseInt(loc);
276                 }
277                 if (shipTo_ID == 0)
278                     mTab.setValue("C_BPartner_Location_ID", null);
279                 else
280                     mTab.setValue("C_BPartner_Location_ID", new Integer JavaDoc(shipTo_ID));
281
282                 // Contact - overwritten by InfoBP selection
283
int contID = rs.getInt("AD_User_ID");
284                 if (C_BPartner_ID.toString().equals(Env.getContext(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "C_BPartner_ID")))
285                 {
286                     String JavaDoc cont = Env.getContext(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "AD_User_ID");
287                     if (cont.length() > 0)
288                         contID = Integer.parseInt(cont);
289                 }
290                 if (contID == 0)
291                     mTab.setValue("AD_User_ID", null);
292                 else
293                     mTab.setValue("AD_User_ID", new Integer JavaDoc(contID));
294
295                 // CreditAvailable
296
double CreditAvailable = rs.getDouble("CreditAvailable");
297                 if (!rs.wasNull() && CreditAvailable < 0)
298                     mTab.fireDataStatusEEvent("CreditLimitOver",
299                         DisplayType.getNumberFormat(DisplayType.Amount).format(CreditAvailable));
300
301                 // PO Reference
302
String JavaDoc s = rs.getString("POReference");
303                 if (s != null && s.length() != 0)
304                     mTab.setValue("POReference", s);
305                 else
306                     mTab.setValue("POReference", null);
307                 // SO Description
308
s = rs.getString("SO_Description");
309                 if (s != null && s.trim().length() != 0)
310                     mTab.setValue("Description", s);
311                 // IsDiscountPrinted
312
s = rs.getString("IsDiscountPrinted");
313                 if (s != null && s.length() != 0)
314                     mTab.setValue("IsDiscountPrinted", s);
315                 else
316                     mTab.setValue("IsDiscountPrinted", "N");
317
318                 // Defaults, if not Walkin Receipt or Walkin Invoice
319
String JavaDoc OrderType = Env.getContext(ctx, WindowNo, "OrderType");
320                 mTab.setValue("InvoiceRule", MOrder.INVOICERULE_AfterDelivery);
321                 mTab.setValue("DeliveryRule", MOrder.DELIVERYRULE_Availability);
322                 mTab.setValue("PaymentRule", MOrder.PAYMENTRULE_OnCredit);
323                 if (OrderType.equals(MOrder.DocSubTypeSO_Prepay))
324                 {
325                     mTab.setValue("InvoiceRule", MOrder.INVOICERULE_Immediate);
326                     mTab.setValue("DeliveryRule", MOrder.DELIVERYRULE_AfterReceipt);
327                 }
328                 else if (OrderType.equals(MOrder.DocSubTypeSO_POS)) // for POS
329
mTab.setValue("PaymentRule", MOrder.PAYMENTRULE_Cash);
330                 else
331                 {
332                     // PaymentRule
333
s = rs.getString(IsSOTrx ? "PaymentRule" : "PaymentRulePO");
334                     if (s != null && s.length() != 0)
335                     {
336                         if (s.equals("B")) // No Cache in Non POS
337
s = "P";
338                         if (IsSOTrx && (s.equals("S") || s.equals("U"))) // No Check/Transfer for SO_Trx
339
s = "P"; // Payment Term
340
mTab.setValue("PaymentRule", s);
341                     }
342                     // Payment Term
343
ii = new Integer JavaDoc(rs.getInt(IsSOTrx ? "C_PaymentTerm_ID" : "PO_PaymentTerm_ID"));
344                     if (!rs.wasNull())
345                         mTab.setValue("C_PaymentTerm_ID", ii);
346                     // InvoiceRule
347
s = rs.getString("InvoiceRule");
348                     if (s != null && s.length() != 0)
349                         mTab.setValue("InvoiceRule", s);
350                     // DeliveryRule
351
s = rs.getString("DeliveryRule");
352                     if (s != null && s.length() != 0)
353                         mTab.setValue("DeliveryRule", s);
354                     // FreightCostRule
355
s = rs.getString("FreightCostRule");
356                     if (s != null && s.length() != 0)
357                         mTab.setValue("FreightCostRule", s);
358                     // DeliveryViaRule
359
s = rs.getString("DeliveryViaRule");
360                     if (s != null && s.length() != 0)
361                         mTab.setValue("DeliveryViaRule", s);
362                 }
363             }
364             rs.close();
365             pstmt.close();
366         }
367         catch (SQLException e)
368         {
369             log.error("bPartner", e);
370             return e.getLocalizedMessage();
371         }
372
373         return "";
374     } // bPartner
375

376
377     /**
378      * Order Header - PriceList.
379      * (used also in Invoice)
380      * - C_Currency_ID
381      * - IsTaxIncluded
382      * Window Context:
383      * - EnforcePriceLimit
384      * - StdPrecision
385      * - M_PriceList_Version_ID
386      * @param ctx Context
387      * @param WindowNo current Window No
388      * @param mTab Model Tab
389      * @param mField Model Field
390      * @param value The new value
391      */

392     public String JavaDoc priceList (Properties ctx, int WindowNo, MTab mTab, MField mField, Object JavaDoc value)
393     {
394         Integer JavaDoc M_PriceList_ID = (Integer JavaDoc)value;
395         if (M_PriceList_ID == null || M_PriceList_ID.intValue()== 0)
396             return "";
397
398         String JavaDoc SQL = "SELECT pl.IsTaxIncluded,pl.EnforcePriceLimit,pl.C_Currency_ID,c.StdPrecision,"
399             + "plv.M_PriceList_Version_ID,plv.ValidFrom "
400             + "FROM M_PriceList pl,C_Currency c,M_PriceList_Version plv "
401             + "WHERE pl.C_Currency_ID=c.C_Currency_ID"
402             + " AND pl.M_PriceList_ID=plv.M_PriceList_ID"
403             + " AND pl.M_PriceList_ID=? " // 1
404
+ "ORDER BY plv.ValidFrom DESC";
405         // Use newest price list - may not be future
406
try
407         {
408             PreparedStatement pstmt = DB.prepareStatement(SQL);
409             pstmt.setInt(1, M_PriceList_ID.intValue());
410             ResultSet rs = pstmt.executeQuery();
411             if (rs.next())
412             {
413                 // Tax Included
414
mTab.setValue("IsTaxIncluded", rs.getString(1));
415                 // Price Limit Enforce
416
Env.setContext(ctx, WindowNo, "EnforcePriceLimit", rs.getString(2));
417                 // Currency
418
Integer JavaDoc ii = new Integer JavaDoc(rs.getInt(3));
419                 mTab.setValue("C_Currency_ID", ii);
420                 // Currency Precision
421
Env.setContext(ctx, WindowNo, "StdPrecision", rs.getInt(4));
422                 // PriceList Version
423
Env.setContext(ctx, WindowNo, "M_PriceList_Version_ID", rs.getInt(5));
424             }
425             rs.close();
426             pstmt.close();
427         }
428         catch (SQLException e)
429         {
430             log.error("priceList", e);
431             return e.getLocalizedMessage();
432         }
433
434         return "";
435     } // priceList
436

437     /*************************************************************************/
438
439
440     /**
441      * Order Line - Product.
442      * - reset C_Charge_ID / M_AttributeSetInstance_ID
443      * - PriceList, PriceStd, PriceLimit, C_Currency_ID, EnforcePriceLimit
444      * - UOM
445      * Calls Tax
446      *
447      * @param ctx Context
448      * @param WindowNo current Window No
449      * @param mTab Model Tab
450      * @param mField Model Field
451      * @param value The new value
452      */

453     public String JavaDoc product (Properties ctx, int WindowNo, MTab mTab, MField mField, Object JavaDoc value)
454     {
455         Integer JavaDoc M_Product_ID = (Integer JavaDoc)value;
456         if (M_Product_ID == null || M_Product_ID.intValue() == 0)
457             return "";
458         setCalloutActive(true);
459         mTab.setValue("C_Charge_ID", null);
460         // Set Attribute
461
if (Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "M_Product_ID") == M_Product_ID.intValue()
462             && Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID") != 0)
463             mTab.setValue("M_AttributeSetInstance_ID", new Integer JavaDoc(Env.getContextAsInt(ctx, Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID")));
464         else
465             mTab.setValue("M_AttributeSetInstance_ID", null);
466             
467         // get order date - or default to today's date
468
Timestamp orderDate = (Timestamp)mTab.getValue("DateOrdered");
469         if (orderDate == null)
470             orderDate = new Timestamp(System.currentTimeMillis());
471
472         int M_PriceList_Version_ID = Env.getContextAsInt(ctx, WindowNo, "M_PriceList_Version_ID");
473         MProductPriceVO pp = MProductPriceVO.getBOMPricesPLV(M_Product_ID.intValue(), M_PriceList_Version_ID);
474         // Get via Price List
475
if (pp == null)
476         {
477             int M_PriceList_ID = Env.getContextAsInt(ctx, WindowNo, "M_PriceList_ID");
478             pp = MProductPriceVO.getBOMPricesPL(M_Product_ID.intValue(), M_PriceList_ID, orderDate);
479         }
480         // Get via Default PL
481
if (pp == null)
482         {
483             boolean isSOTrx = "Y".equals(Env.getContext(ctx, WindowNo, "IsSOTrx"));
484             pp = MProductPriceVO.getBOMPrices(M_Product_ID.intValue(), isSOTrx, orderDate);
485         }
486         
487         // Set Prices
488
if (pp != null)
489         {
490             mTab.setValue("PriceList", pp.PriceList);
491             mTab.setValue("PriceLimit", pp.PriceLimit);
492             mTab.setValue("PriceActual", pp.PriceStd);
493             mTab.setValue("C_Currency_ID", new Integer JavaDoc(pp.getC_Currency_ID()));
494             Env.setContext(ctx, WindowNo, "EnforcePriceLimit", pp.isEnforcePriceLimit() ? "Y" : "N");
495         }
496
497         // Check/Update Warehouse Setting
498
// int M_Warehouse_ID = Env.getContextAsInt(Env.WINDOW_INFO, "M_Warehouse_ID");
499
// Integer wh = (Integer)mTab.getValue("M_Warehouse_ID");
500
// if (wh.intValue() != M_Warehouse_ID)
501
// {
502
// mTab.setValue("M_Warehouse_ID", new Integer(M_Warehouse_ID));
503
// ADialog.warn(WindowNo, "WarehouseChanged");
504
// }
505

506         int C_UOM_ID = DB.getSQLValue("SELECT C_UOM_ID FROM M_Product WHERE M_Product_ID=?", M_Product_ID.intValue());
507         if (C_UOM_ID != 0)
508             mTab.setValue("C_UOM_ID", new Integer JavaDoc(C_UOM_ID));
509         setCalloutActive(false);
510         //
511
return tax (ctx, WindowNo, mTab, mField, value);
512     } // product
513

514     /**
515      * Order Line - Charge.
516      * - updates PriceActual from Charge
517      * - sets PriceLimit, PriceList to zero
518      * Calles tax
519      * @param ctx Context
520      * @param WindowNo current Window No
521      * @param mTab Model Tab
522      * @param mField Model Field
523      * @param value The new value
524      */

525     public String JavaDoc charge (Properties ctx, int WindowNo, MTab mTab, MField mField, Object JavaDoc value)
526     {
527         Integer JavaDoc C_Charge_ID = (Integer JavaDoc)value;
528         if (C_Charge_ID == null || C_Charge_ID.intValue() == 0)
529             return "";
530         // No Product defined
531
if (mTab.getValue("M_Product_ID") != null)
532         {
533             mTab.setValue("C_Charge_ID", null);
534             return "ChargeExclusively";
535         }
536
537         try
538         {
539             String JavaDoc SQL = "SELECT ChargeAmt FROM C_Charge WHERE C_Charge_ID=?";
540             PreparedStatement pstmt = DB.prepareStatement(SQL);
541             pstmt.setInt(1, C_Charge_ID.intValue());
542             ResultSet rs = pstmt.executeQuery();
543             if (rs.next())
544             {
545                 mTab.setValue ("PriceActual", rs.getBigDecimal (1));
546                 mTab.setValue ("PriceLimit", Env.ZERO);
547                 mTab.setValue ("PriceList", Env.ZERO);
548                 mTab.setValue ("Discount", Env.ZERO);
549             }
550             rs.close();
551             pstmt.close();
552         }
553         catch (SQLException e)
554         {
555             log.error("charge" + e);
556             return e.getLocalizedMessage();
557         }
558         //
559
return tax (ctx, WindowNo, mTab, mField, value);
560     } // charge
561

562
563     /**
564      * Order Line - Tax.
565      * - basis: Product, Charge, BPartner Location
566      * - sets C_Tax_ID
567      * Calles Amount
568      * @param ctx Context
569      * @param WindowNo current Window No
570      * @param mTab Model Tab
571      * @param mField Model Field
572      * @param value The new value
573      */

574     public String JavaDoc tax (Properties ctx, int WindowNo, MTab mTab, MField mField, Object JavaDoc value)
575     {
576         String JavaDoc column = mField.getColumnName();
577         if (value == null)
578             return "";
579
580         // Check Product
581
int M_Product_ID = 0;
582         if (column.equals("M_Product_ID"))
583             M_Product_ID = ((Integer JavaDoc)value).intValue();
584         else
585             M_Product_ID = Env.getContextAsInt(ctx, WindowNo, "M_Product_ID");
586         int C_Charge_ID = 0;
587         if (column.equals("C_Charge_ID"))
588             C_Charge_ID = ((Integer JavaDoc)value).intValue();
589         else
590             C_Charge_ID = Env.getContextAsInt(ctx, WindowNo, "C_Charge_ID");
591         log.debug("Product=" + M_Product_ID + ", C_Charge_ID=" + C_Charge_ID);
592         if (M_Product_ID == 0 && C_Charge_ID == 0)
593             return "";
594
595         // Check Partner Location
596
int shipC_BPartner_Location_ID = 0;
597         if (column.equals("C_BPartner_Location_ID"))
598             shipC_BPartner_Location_ID = ((Integer JavaDoc)value).intValue();
599         else
600             shipC_BPartner_Location_ID = Env.getContextAsInt(ctx, WindowNo, "C_BPartner_Location_ID");
601         if (shipC_BPartner_Location_ID == 0)
602             return "";
603         log.debug("Ship BP_Location=" + shipC_BPartner_Location_ID);
604
605         //
606
Timestamp billDate = Env.getContextAsDate(ctx, WindowNo, "DateOrdered");
607         log.debug("Bill Date=" + billDate);
608
609         Timestamp shipDate = Env.getContextAsDate(ctx, WindowNo, "DatePromised");
610         log.debug("Ship Date=" + shipDate);
611
612         int AD_Org_ID = Env.getContextAsInt(ctx, WindowNo, "AD_Org_ID");
613         log.debug("Org=" + AD_Org_ID);
614
615         int M_Warehouse_ID = Env.getContextAsInt(ctx, WindowNo, "M_Warehouse_ID");
616         log.debug("Warehouse=" + M_Warehouse_ID);
617
618         int billC_BPartner_Location_ID = Env.getContextAsInt(ctx, WindowNo, "BillTo_ID");
619         if (billC_BPartner_Location_ID == 0)
620             billC_BPartner_Location_ID = shipC_BPartner_Location_ID;
621         log.debug("Bill BP_Location=" + billC_BPartner_Location_ID);
622
623         //
624
int C_Tax_ID = Tax.get (ctx, M_Product_ID, C_Charge_ID, billDate, shipDate,
625             AD_Org_ID, M_Warehouse_ID, billC_BPartner_Location_ID, shipC_BPartner_Location_ID,
626             "Y".equals(Env.getContext(ctx, WindowNo, "IsSOTrx")));
627         log.debug("Tax ID=" + C_Tax_ID);
628         //
629
if (C_Tax_ID == 0)
630             mTab.fireDataStatusEEvent(Log.retrieveError());
631         else
632             mTab.setValue("C_Tax_ID", new Integer JavaDoc(C_Tax_ID));
633         //
634
return amt(ctx, WindowNo, mTab, mField, value);
635     } // tax
636

637
638     /**
639      * Order Line - Amount.
640      * - called from QtyOrdered, Discount and PriceActual
641      * - calculates Discount or Actual Amount
642      * - calculates LineNetAmt
643      * - enforces PriceLimit
644      * @param ctx Context
645      * @param WindowNo current Window No
646      * @param mTab Model Tab
647      * @param mField Model Field
648      * @param value The new value
649      */

650     public String JavaDoc amt (Properties ctx, int WindowNo, MTab mTab, MField mField, Object JavaDoc value)
651     {
652         if (isCalloutActive() || value == null)
653             return "";
654         setCalloutActive(true);
655
656         BigDecimal QtyOrdered, PriceActual, PriceLimit, Discount, PriceList;
657         int StdPrecision = Env.getContextAsInt(ctx, WindowNo, "StdPrecision");
658
659
660         // get values
661
QtyOrdered = (BigDecimal)mTab.getValue("QtyOrdered");
662         PriceActual = ((BigDecimal)mTab.getValue("PriceActual")).setScale(StdPrecision, BigDecimal.ROUND_HALF_UP);
663         Discount = (BigDecimal)mTab.getValue("Discount");
664         //
665
PriceLimit = ((BigDecimal)mTab.getValue("PriceLimit")).setScale(StdPrecision, BigDecimal.ROUND_HALF_UP);
666         PriceList = (BigDecimal)mTab.getValue("PriceList");
667         log.debug("Ordered=" + QtyOrdered + ", List=" + PriceList + ", Limit=" + PriceLimit + ", Precision=" + StdPrecision);
668         log.debug("~ Actual=" + PriceActual + ", Discount=" + Discount);
669
670         // calculate Actual if discount entered
671
if (mField.getColumnName().equals("Discount"))
672         {
673             PriceActual = new BigDecimal ((100.0 - Discount.doubleValue()) / 100.0 * PriceList.doubleValue());
674             if (PriceActual.scale() > StdPrecision)
675                 PriceActual = PriceActual.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP);
676             mTab.setValue("PriceActual", PriceActual);
677         }
678         // calculate Discount
679
else
680         {
681             if (PriceList.intValue() == 0)
682                 Discount = Env.ZERO;
683             else
684                 Discount = new BigDecimal ((PriceList.doubleValue() - PriceActual.doubleValue()) / PriceList.doubleValue() * 100.0);
685             if (Discount.scale() > 2)
686                 Discount = Discount.setScale(2, BigDecimal.ROUND_HALF_UP);
687             mTab.setValue("Discount", Discount);
688         }
689         log.debug("= Actual=" + PriceActual + ", Discount=" + Discount);
690
691         // Check PriceLimit
692
if (!mField.getColumnName().equals("QtyOrdered"))
693         {
694             String JavaDoc epl = Env.getContext(ctx, WindowNo, "EnforcePriceLimit");
695             // Check Price Limit?
696
if (epl != null && epl.equals("Y") && PriceLimit.doubleValue() != 0.0
697               && PriceActual.compareTo(PriceLimit) < 0)
698             {
699                 mTab.setValue ("PriceActual", PriceLimit);
700                 mTab.fireDataStatusEEvent ("UnderLimitPrice", "");
701                 PriceActual = PriceLimit;
702                 // Repeat Discount calc
703
if (PriceList.intValue() != 0)
704                 {
705                     Discount = new BigDecimal ((PriceList.doubleValue () - PriceActual.doubleValue ()) / PriceList.doubleValue () * 100.0);
706                     if (Discount.scale () > 2)
707                         Discount = Discount.setScale (2, BigDecimal.ROUND_HALF_UP);
708                     mTab.setValue ("Discount", Discount);
709                 }
710             }
711         }
712
713         // Multiply
714
BigDecimal LineNetAmt = QtyOrdered.multiply(PriceActual);
715         if (LineNetAmt.scale() > StdPrecision)
716             LineNetAmt = LineNetAmt.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP);
717         mTab.setValue("LineNetAmt", LineNetAmt);
718         log.debug("LineNetAmt=" + LineNetAmt);
719         //
720
setCalloutActive(false);
721         return "";
722     } // amt
723

724 } // CalloutOrder
725

726
Popular Tags