/
DocLine.java
996 lines (879 loc) · 24 KB
/
DocLine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
* For the text or an alternative of this public license, you may reach us *
* ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA *
* or via info@compiere.org or http://www.compiere.org/license.html *
*****************************************************************************/
package org.compiere.acct;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Optional;
import java.util.function.IntFunction;
import javax.annotation.OverridingMethodsMustInvokeSuper;
import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.mm.attributes.AttributeSetInstanceId;
import org.adempiere.model.InterfaceWrapperHelper;
import org.adempiere.service.ClientId;
import org.compiere.model.I_C_UOM;
import org.compiere.model.I_M_Product;
import org.compiere.model.I_M_Product_Acct;
import org.compiere.model.I_M_Product_Category_Acct;
import org.compiere.model.MAccount;
import org.compiere.model.MCharge;
import org.compiere.model.PO;
import org.compiere.util.DB;
import org.compiere.util.TimeUtil;
import org.compiere.util.Util;
import org.slf4j.Logger;
import com.google.common.base.MoreObjects;
import de.metas.acct.api.AcctSchema;
import de.metas.acct.api.IAccountDAO;
import de.metas.acct.api.IProductAcctDAO;
import de.metas.acct.api.ProductAcctType;
import de.metas.acct.doc.PostingException;
import de.metas.bpartner.BPartnerId;
import de.metas.costing.CostingLevel;
import de.metas.costing.CostingMethod;
import de.metas.costing.IProductCostingBL;
import de.metas.location.LocationId;
import de.metas.logging.LogManager;
import de.metas.money.CurrencyConversionTypeId;
import de.metas.money.CurrencyId;
import de.metas.order.OrderLineId;
import de.metas.organization.OrgId;
import de.metas.product.IProductBL;
import de.metas.product.IProductDAO;
import de.metas.product.ProductId;
import de.metas.product.acct.api.ActivityId;
import de.metas.quantity.Quantity;
import de.metas.uom.UomId;
import de.metas.util.NumberUtils;
import de.metas.util.Optionals;
import de.metas.util.Services;
import de.metas.util.lang.RepoIdAware;
import lombok.NonNull;
/**
* Standard Document Line
*
* @author Jorg Janke
* @author Armen Rizal, Goodwill Consulting
* <li>BF [ 1745154 ] Cost in Reversing Material Related Docs
* @version $Id: DocLine.java,v 1.2 2006/07/30 00:53:33 jjanke Exp $
*/
public class DocLine<DT extends Doc<? extends DocLine<?>>>
{
// services
private final transient Logger logger = LogManager.getLogger(getClass());
protected final transient IProductBL productBL = Services.get(IProductBL.class);
private final transient IProductAcctDAO productAcctDAO = Services.get(IProductAcctDAO.class);
private final transient IProductCostingBL productCostingBL = Services.get(IProductCostingBL.class);
private final transient IAccountDAO accountDAO = Services.get(IAccountDAO.class);
/** Persistent Object */
private final PO p_po;
/** Parent */
private final DT m_doc;
/** Qty */
private Quantity qty = null;
// -- GL Amounts
/** Debit Journal Amt */
private BigDecimal m_AmtSourceDr = BigDecimal.ZERO;
/** Credit Journal Amt */
private BigDecimal m_AmtSourceCr = BigDecimal.ZERO;
/** Net Line Amt */
private BigDecimal m_LineNetAmt = null;
/** List Amount */
private BigDecimal m_ListAmt = BigDecimal.ZERO;
/** Discount Amount */
private BigDecimal m_DiscountAmt = BigDecimal.ZERO;
/** Converted Amounts */
private BigDecimal m_AmtAcctDr = null;
private BigDecimal m_AmtAcctCr = null;
private I_M_Product _product; // lazy
private Boolean _productIsItem = null; // lazy
private LocalDate m_DateAcct = null;
private LocalDate m_DateDoc = null;
private int m_C_SalesRegion_ID = -1;
private Optional<BPartnerId> _bpartnerId;
private final LocationId locationFromId = null;
private final LocationId locationToId = null;
private Optional<CurrencyId> _currencyId;
private Optional<CurrencyConversionTypeId> currencyConversionTypeIdHolder;
private int m_C_Period_ID = -1;
/** C_Tax_ID (override) */
private Integer m_C_Tax_ID = null;
/** Is Tax Included ? */
private boolean _taxIncluded = false;
private int m_ReversalLine_ID = 0;
public DocLine(@NonNull final PO linePO, @NonNull final DT doc)
{
p_po = linePO;
m_doc = doc;
//
// Document Consistency
if (p_po.getAD_Org_ID() <= 0)
{
p_po.setAD_Org_ID(m_doc.getOrgId().getRepoId());
}
} // DocLine
protected final ClientId getClientId()
{
return m_doc.getClientId();
}
private final PO getPO()
{
return p_po;
}
/**
* @param modelType
* @return underlying model
*/
protected final <T> T getModel(final Class<T> modelType)
{
return InterfaceWrapperHelper.create(getPO(), modelType);
}
public final CurrencyId getCurrencyId()
{
if (_currencyId == null)
{
final CurrencyId lineCurrencyId = CurrencyId.ofRepoIdOrNull(getValue("C_Currency_ID"));
if (lineCurrencyId != null)
{
_currencyId = Optional.of(lineCurrencyId);
}
else
{
_currencyId = Optional.ofNullable(m_doc.getCurrencyId());
}
}
return _currencyId.orElse(null);
}
public final CurrencyConversionTypeId getCurrencyConversionTypeId()
{
Optional<CurrencyConversionTypeId> currencyConversionTypeIdHolder = this.currencyConversionTypeIdHolder;
if (currencyConversionTypeIdHolder == null)
{
CurrencyConversionTypeId currencyConversionTypeId = CurrencyConversionTypeId.ofRepoIdOrNull(getValue("C_ConversionType_ID"));
if (currencyConversionTypeId == null)
{
currencyConversionTypeId = m_doc.getCurrencyConversionTypeId();
}
currencyConversionTypeIdHolder = this.currencyConversionTypeIdHolder = Optional.ofNullable(currencyConversionTypeId);
}
return currencyConversionTypeIdHolder.orElse(null);
}
protected final void setC_ConversionType_ID(final int C_ConversionType_ID)
{
currencyConversionTypeIdHolder = CurrencyConversionTypeId.optionalOfRepoId(C_ConversionType_ID);
}
/**
* Set Amount (DR)
*
* @param sourceAmt source amt
*/
protected final void setAmount(final BigDecimal sourceAmt)
{
m_AmtSourceDr = sourceAmt == null ? BigDecimal.ZERO : sourceAmt;
m_AmtSourceCr = BigDecimal.ZERO;
} // setAmounts
/**
* Set Amounts
*
* @param amtSourceDr source amount dr
* @param amtSourceCr source amount cr
*/
protected final void setAmount(final BigDecimal amtSourceDr, final BigDecimal amtSourceCr)
{
m_AmtSourceDr = amtSourceDr == null ? BigDecimal.ZERO : amtSourceDr;
m_AmtSourceCr = amtSourceCr == null ? BigDecimal.ZERO : amtSourceCr;
} // setAmounts
protected final void setAmountDrOrCr(final BigDecimal amtSource, final boolean isAmountDR)
{
final BigDecimal amtSourceDr = isAmountDR ? amtSource : null;
final BigDecimal amtSourceCr = isAmountDR ? null : amtSource;
setAmount(amtSourceDr, amtSourceCr);
} // setAmounts
/**
* Set Converted Amounts. If converted amounts are set, they will be used as is, and no further convertion will be done.
*
* @param amtAcctDr acct amount dr
* @param amtAcctCr acct amount cr
*/
public final void setConvertedAmt(final BigDecimal amtAcctDr, final BigDecimal amtAcctCr)
{
m_AmtAcctDr = amtAcctDr;
m_AmtAcctCr = amtAcctCr;
} // setConvertedAmt
/**
* Line Net Amount or Dr-Cr
*
* @return balance
*/
public final BigDecimal getAmtSource()
{
return m_AmtSourceDr.subtract(m_AmtSourceCr);
} // getAmount
/**
* Get (Journal) Line Source Dr Amount
*
* @return DR source amount
*/
public final BigDecimal getAmtSourceDr()
{
return m_AmtSourceDr;
} // getAmtSourceDr
/**
* Get (Journal) Line Source Cr Amount
*
* @return CR source amount
*/
public final BigDecimal getAmtSourceCr()
{
return m_AmtSourceCr;
} // getAmtSourceCr
/**
* Line Journal Accounted Dr Amount
*
* @return DR accounted amount
*/
public final BigDecimal getAmtAcctDr()
{
return m_AmtAcctDr;
} // getAmtAcctDr
/**
* Line Journal Accounted Cr Amount
*
* @return CR accounted amount
*/
public final BigDecimal getAmtAcctCr()
{
return m_AmtAcctCr;
} // getAmtAccrCr
public final BigDecimal getChargeAmt()
{
return getValueAsBD("ChargeAmt", BigDecimal.ZERO);
}
/**
* Set Product Amounts
*
* @param LineNetAmt Line Net Amt
* @param PriceList Price List
* @param Qty Qty for discount calc
*/
public final void setAmount(final BigDecimal LineNetAmt, final BigDecimal PriceList, final BigDecimal Qty)
{
m_LineNetAmt = LineNetAmt == null ? BigDecimal.ZERO : LineNetAmt;
if (PriceList != null && Qty != null)
{
m_ListAmt = PriceList.multiply(Qty);
}
if (m_ListAmt.compareTo(BigDecimal.ZERO) == 0)
{
m_ListAmt = m_LineNetAmt;
}
m_DiscountAmt = m_ListAmt.subtract(m_LineNetAmt);
//
setAmount(m_ListAmt, m_DiscountAmt);
}
/**
* Line Discount
*
* @return discount amount
*/
public final BigDecimal getDiscount()
{
return m_DiscountAmt;
} // getDiscount
/**
* Set Line Net Amt Difference
*
* @param diff difference (to be subtracted)
*/
protected final void setLineNetAmtDifference(final BigDecimal diff)
{
final BigDecimal lineNetAmtOld = m_LineNetAmt;
m_LineNetAmt = m_LineNetAmt.subtract(diff);
m_DiscountAmt = m_ListAmt.subtract(m_LineNetAmt);
setAmount(m_ListAmt, m_DiscountAmt);
logger.warn("Diff={} - LineNetAmt={} -> {} - {}", diff, lineNetAmtOld, m_LineNetAmt, this);
}
public final LocalDate getDateAcct()
{
if (m_DateAcct == null)
{
m_DateAcct = Util.coalesceSuppliers(
() -> getValueAsLocalDateOrNull("DateAcct"),
() -> getDoc().getDateAcct());
}
return m_DateAcct;
}
protected final void setDateDoc(final LocalDate dateDoc)
{
m_DateDoc = dateDoc;
} // setDateDoc
public final LocalDate getDateDoc()
{
if (m_DateDoc == null)
{
m_DateDoc = Util.coalesceSuppliers(
() -> getValueAsLocalDateOrNull("DateDoc"),
() -> getValueAsLocalDateOrNull("DateTrx"),
() -> getDoc().getDateAcct());
}
return m_DateDoc;
}
/**
* Line Account from Product (or Charge).
*
* @param acctType see ProductCost.ACCTTYPE_* (0..3)
* @param as Accounting schema
* @return Requested Product Account
*/
@OverridingMethodsMustInvokeSuper
public MAccount getAccount(final ProductAcctType acctType, final AcctSchema as)
{
//
// Charge account
if (getProductId() == null && getC_Charge_ID() > 0)
{
final MAccount acct;
if (!m_doc.isSOTrx())
{
acct = getChargeAccount(as, BigDecimal.ONE); // Expense (+)
}
else
{
acct = getChargeAccount(as, BigDecimal.ONE.negate()); // Revenue (-)
}
if (acct == null)
{
throw newPostingException().setAcctSchema(as).setDetailMessage("No Charge Account for account type: " + acctType);
}
return acct;
}
//
// Product Account
else
{
return getProductAccount(acctType, as);
}
}
private MAccount getProductAccount(final ProductAcctType acctType, final AcctSchema as)
{
// No Product - get Default from Product Category
final ProductId productId = getProductId();
if (productId == null)
{
return getAccountDefault(acctType, as);
}
final I_M_Product_Acct productAcct = productAcctDAO.retrieveProductAcctOrNull(as, productId);
if (productAcct == null)
{
final String productName = Services.get(IProductBL.class).getProductName(productId);
throw newPostingException().setAcctSchema(as).setDetailMessage("Product " + productName + " has no accounting definition records");
}
final Integer validCombinationId = InterfaceWrapperHelper.getValueOrNull(productAcct, acctType.getColumnName());
if (validCombinationId == null || validCombinationId <= 0)
{
final String productName = Services.get(IProductBL.class).getProductName(productId);
throw newPostingException().setAcctSchema(as).setDetailMessage("No Product Account for account type " + acctType + " and product " + productName);
}
return accountDAO.getById(validCombinationId);
}
/**
* Account from Default Product Category
*
* @param AcctType see ACCTTYPE_* (1..8)
* @param as accounting schema
* @return Requested Product Account
*/
private final MAccount getAccountDefault(final ProductAcctType acctType, final AcctSchema as)
{
final I_M_Product_Category_Acct pcAcct = productAcctDAO.retrieveDefaultProductCategoryAcct(as);
final Integer validCombinationId = InterfaceWrapperHelper.getValueOrNull(pcAcct, acctType.getColumnName());
if (validCombinationId == null || validCombinationId <= 0)
{
throw newPostingException().setAcctSchema(as).setDetailMessage("No Default Account for account type: " + acctType);
}
return accountDAO.getById(validCombinationId);
}
/**
* Get Charge
*
* @return C_Charge_ID
*/
protected final int getC_Charge_ID()
{
return getValue("C_Charge_ID");
}
/**
* Get Charge Account
*
* @param as account schema
* @param amount amount for expense(+)/revenue(-)
* @return Charge Account or null
*/
protected final MAccount getChargeAccount(final AcctSchema as, final BigDecimal amount)
{
final int C_Charge_ID = getC_Charge_ID();
if (C_Charge_ID <= 0)
{
return null;
}
return MCharge.getAccount(C_Charge_ID, as.getId(), amount);
}
protected final int getC_Period_ID()
{
if (m_C_Period_ID == -1)
{
m_C_Period_ID = getValue("C_Period_ID");
if (m_C_Period_ID <= 0)
{
m_C_Period_ID = 0;
}
}
return m_C_Period_ID;
} // getC_Period_ID
protected final void setC_Period_ID(final int C_Period_ID)
{
m_C_Period_ID = C_Period_ID;
}
public final int get_ID()
{
return getPO().get_ID();
}
public final OrgId getOrgId()
{
return OrgId.ofRepoId(getPO().getAD_Org_ID());
}
public final ProductId getProductId()
{
return ProductId.ofRepoIdOrNull(getValue("M_Product_ID"));
}
/**
* Is this an stockable item Product (vs. not a Service, a charge)
*
* @return true if we have a stockable product
*/
public final boolean isItem()
{
if (_productIsItem == null)
{
_productIsItem = checkIsItem();
}
return _productIsItem;
}
private final boolean checkIsItem()
{
final I_M_Product product = getProduct();
if (product == null)
{
return false;
}
// NOTE: we are considering the product as Item only if it's stockable.
// Before changing this logic, pls evaluate the Doc_Invoice which is booking on P_InventoryClearing account when the product is stockable
return productBL.isStocked(product);
}
public final boolean isService()
{
return !isItem();
}
public final CostingMethod getProductCostingMethod(final AcctSchema as)
{
return productCostingBL.getCostingMethod(getProductId(), as);
}
public final CostingLevel getProductCostingLevel(final AcctSchema as)
{
return productCostingBL.getCostingLevel(getProductId(), as);
}
public final AttributeSetInstanceId getAttributeSetInstanceId()
{
return AttributeSetInstanceId.ofRepoIdOrNone(getValue("M_AttributeSetInstance_ID"));
}
public final int getM_Locator_ID()
{
return getValue("M_Locator_ID");
}
public final OrderLineId getOrderLineId()
{
return OrderLineId.ofRepoIdOrNull(getValue("C_OrderLine_ID"));
}
public final LocationId getLocationFromId()
{
return locationFromId;
}
public final LocationId getLocationToId()
{
return locationToId;
}
/**
* @return product or null if no product
*/
private final I_M_Product getProduct()
{
if (_product == null)
{
final ProductId productId = getProductId();
if (productId != null)
{
_product = Services.get(IProductDAO.class).getById(productId);
}
}
return _product;
}
protected final I_C_UOM getProductStockingUOM()
{
return productBL.getStockingUOM(getProductId());
}
protected final UomId getProductStockingUOMId()
{
return productBL.getStockingUOMId(getProductId());
}
/**
* Get Revenue Recognition
*
* @return C_RevenueRecognition_ID or 0
*/
public final int getC_RevenueRecognition_ID()
{
final I_M_Product product = getProduct();
if (product != null)
{
return product.getC_RevenueRecognition_ID();
}
return 0;
} // getC_RevenueRecognition_ID
/**
* Quantity UOM
*
* @return Transaction or Storage M_UOM_ID
*/
public final int getC_UOM_ID()
{
// Trx UOM
final int uomId = getValue("C_UOM_ID");
if (uomId > 0)
{
return uomId;
}
// Storage UOM
if (getProductId() != null)
{
final I_C_UOM uom = getProductStockingUOM();
return uom.getC_UOM_ID();
}
return -1;
}
/**
* Quantity
*
* @param quantity transaction Qty
* @param isSOTrx SL order trx (i.e. negative qty)
*/
protected final void setQty(@NonNull final Quantity quantity, final boolean isSOTrx)
{
if (isSOTrx)
{
setQty(quantity.negate());
}
else
{
setQty(quantity);
}
}
protected final void setQty(@NonNull final Quantity quantity)
{
this.qty = quantity;
}
/**
* Quantity
*
* @return transaction Qty
*/
public final Quantity getQty()
{
return qty;
} // getQty
public final String getDescription()
{
return getValueAsString("Description");
}
public final int getC_Tax_ID()
{
if (m_C_Tax_ID == null)
{
m_C_Tax_ID = getValue("C_Tax_ID");
}
return m_C_Tax_ID;
}
public final void setC_Tax_ID(final int taxId)
{
m_C_Tax_ID = taxId;
}
public final int getLine()
{
return getValue("Line");
}
public BPartnerId getBPartnerId()
{
if (_bpartnerId == null)
{
_bpartnerId = Optionals.firstPresentOfSuppliers(
() -> getValueAsOptionalId("C_BPartner_ID", BPartnerId::ofRepoIdOrNull),
() -> Optional.ofNullable(m_doc.getBPartnerId()));
}
return _bpartnerId.orElse(null);
}
protected final void setBPartnerId(final BPartnerId bpartnerId)
{
_bpartnerId = Optional.ofNullable(bpartnerId);
}
private final int getC_BPartner_Location_ID()
{
return Util.coalesceSuppliers(
() -> getValue("C_BPartner_Location_ID"),
() -> m_doc.getC_BPartner_Location_ID());
}
public final OrgId getOrgTrxId()
{
return getValueAsIdOrNull("AD_OrgTrx_ID", OrgId::ofRepoIdOrNull);
}
/**
* Get SalesRegion.
* - get Sales Region from BPartner
*
* @return C_SalesRegion_ID
*/
public final int getC_SalesRegion_ID()
{
if (m_C_SalesRegion_ID == -1) // never tried
{
final int bpartnerLocationId = getC_BPartner_Location_ID();
if (bpartnerLocationId > 0)
// && m_acctSchema.isAcctSchemaElement(MAcctSchemaElement.ELEMENTTYPE_SalesRegion))
{
final String sql = "SELECT COALESCE(C_SalesRegion_ID,0) FROM C_BPartner_Location WHERE C_BPartner_Location_ID=?";
m_C_SalesRegion_ID = DB.getSQLValueEx(ITrx.TRXNAME_None, sql, bpartnerLocationId);
if (m_C_SalesRegion_ID == 0)
{
m_C_SalesRegion_ID = -2; // don't try again
}
}
else
{
m_C_SalesRegion_ID = -2; // don't try again
}
}
if (m_C_SalesRegion_ID < 0)
{
return 0;
}
return m_C_SalesRegion_ID;
} // getC_SalesRegion_ID
public final int getC_Project_ID()
{
return getValue("C_Project_ID");
}
public final int getC_Campaign_ID()
{
return getValue("C_Campaign_ID");
}
public final ActivityId getActivityFromId()
{
return ActivityId.ofRepoIdOrNull(getValue("C_ActivityFrom_ID"));
}
public ActivityId getActivityId()
{
return ActivityId.ofRepoIdOrNull(getValue("C_Activity_ID"));
}
public final int getUser1_ID()
{
return getValue("User1_ID");
}
public final int getUser2_ID()
{
return getValue("User2_ID");
}
private final <T extends RepoIdAware> T getValueAsIdOrNull(final String columnName, final IntFunction<T> idOrNullMapper)
{
final PO po = getPO();
final int index = po.get_ColumnIndex(columnName);
if (index < 0)
{
return null;
}
final Object valueObj = po.get_Value(index);
final Integer valueInt = NumberUtils.asInteger(valueObj, null);
if (valueInt == null)
{
return null;
}
final T id = idOrNullMapper.apply(valueInt);
return id;
}
private final <T extends RepoIdAware> Optional<T> getValueAsOptionalId(final String columnName, final IntFunction<T> idOrNullMapper)
{
final PO po = getPO();
final int index = po.get_ColumnIndex(columnName);
if (index < 0)
{
return Optional.empty();
}
final Object valueObj = po.get_Value(index);
final Integer valueInt = NumberUtils.asInteger(valueObj, null);
if (valueInt == null)
{
return Optional.empty();
}
final T id = idOrNullMapper.apply(valueInt);
if (id == null)
{
return Optional.empty();
}
return Optional.of(id);
}
/**
* Get User Defined Column
*
* @param columnName column name
* @return user defined column value
*/
public final int getValue(final String columnName)
{
final PO po = getPO();
final int index = po.get_ColumnIndex(columnName);
if (index != -1)
{
final Integer valueInt = (Integer)po.get_Value(index);
if (valueInt != null)
{
return valueInt.intValue();
}
}
return 0;
}
private final BigDecimal getValueAsBD(final String columnName, final BigDecimal defaultValue)
{
final PO po = getPO();
final int index = po.get_ColumnIndex(columnName);
if (index != -1)
{
final Object valueObj = po.get_Value(index);
return NumberUtils.asBigDecimal(valueObj, defaultValue);
}
return defaultValue;
}
private final LocalDate getValueAsLocalDateOrNull(final String columnName)
{
final PO po = getPO();
final int index = po.get_ColumnIndex(columnName);
if (index != -1)
{
return TimeUtil.asLocalDate(po.get_Value(index));
}
return null;
}
private final String getValueAsString(final String columnName)
{
final PO po = getPO();
final int index = po.get_ColumnIndex(columnName);
if (index != -1)
{
final Object valueObj = po.get_Value(index);
return valueObj != null ? valueObj.toString() : null;
}
return null;
}
/**
* Set ReversalLine_ID
* store original (voided/reversed) document line
*
* @param ReversalLine_ID
*/
public final void setReversalLine_ID(final int ReversalLine_ID)
{
m_ReversalLine_ID = ReversalLine_ID;
} // setReversalLine_ID
/** @return get original (voided/reversed) document line */
public final int getReversalLine_ID()
{
return m_ReversalLine_ID;
}
/** @return is the reversal document line (not the original) */
public final boolean isReversalLine()
{
return m_ReversalLine_ID > 0 // has a reversal counterpart
&& get_ID() > m_ReversalLine_ID; // this document line was created after it's reversal counterpart
}
@Override
public String toString()
{
return MoreObjects.toStringHelper(this)
.omitNullValues()
.add("id", get_ID())
.add("description", getDescription())
.add("productId", getProductId())
.add("qty", getQty())
.add("amtSource", getAmtSource())
.toString();
}
/**
* Is Tax Included
*
* @return true if tax is included in amounts
*/
public final boolean isTaxIncluded()
{
return _taxIncluded;
} // isTaxIncluded
/**
* Set Tax Included
*
* @param taxIncluded Is Tax Included?
*/
protected final void setIsTaxIncluded(final boolean taxIncluded)
{
_taxIncluded = taxIncluded;
}
/**
* @see Doc#isSOTrx()
*/
public boolean isSOTrx()
{
return m_doc.isSOTrx();
}
/**
* @return document currency precision
* @see Doc#getStdPrecision()
*/
protected final int getStdPrecision()
{
return m_doc.getStdPrecision();
}
/** @return document (header) */
protected final DT getDoc()
{
return m_doc;
}
public final PostingException newPostingException()
{
return m_doc.newPostingException()
.setDocument(getDoc())
.setDocLine(this);
}
public final PostingException newPostingException(final Throwable ex)
{
return m_doc.newPostingException(ex)
.setDocument(getDoc())
.setDocLine(this);
}
}