/
DocLine_Movement.java
157 lines (139 loc) · 5.09 KB
/
DocLine_Movement.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
package org.compiere.acct;
import org.adempiere.model.InterfaceWrapperHelper;
import org.adempiere.warehouse.api.IWarehouseDAO;
import org.compiere.Adempiere;
import org.compiere.model.I_M_MovementLine;
import de.metas.acct.api.AcctSchema;
import de.metas.acct.api.AcctSchemaId;
import de.metas.costing.AggregatedCostAmount;
import de.metas.costing.CostAmount;
import de.metas.costing.CostDetailCreateRequest;
import de.metas.costing.CostDetailReverseRequest;
import de.metas.costing.CostElement;
import de.metas.costing.CostingDocumentRef;
import de.metas.costing.ICostingService;
import de.metas.organization.OrgId;
import de.metas.quantity.Quantity;
import de.metas.util.Services;
import lombok.NonNull;
/*
* #%L
* de.metas.acct.base
* %%
* Copyright (C) 2018 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* 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, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
class DocLine_Movement extends DocLine<Doc_Movement>
{
public DocLine_Movement(final I_M_MovementLine movementLine, final Doc_Movement doc)
{
super(InterfaceWrapperHelper.getPO(movementLine), doc);
setQty(Quantity.of(movementLine.getMovementQty(), getProductStockingUOM()), false);
setReversalLine_ID(movementLine.getReversalLine_ID());
}
public final int getM_AttributeSetInstanceTo_ID()
{
final I_M_MovementLine movementLine = getModel(I_M_MovementLine.class);
return movementLine.getM_AttributeSetInstanceTo_ID();
}
private final OrgId getFromOrgId()
{
return Services.get(IWarehouseDAO.class).retrieveOrgIdByLocatorId(getM_Locator_ID());
}
private final OrgId getToOrgId()
{
return Services.get(IWarehouseDAO.class).retrieveOrgIdByLocatorId(getM_LocatorTo_ID());
}
public final int getM_LocatorTo_ID()
{
final I_M_MovementLine movementLine = getModel(I_M_MovementLine.class);
return movementLine.getM_LocatorTo_ID();
}
public final AggregatedCostAmount getCreateInboundCosts(final AcctSchema as)
{
final ICostingService costDetailService = Adempiere.getBean(ICostingService.class);
if (isReversalLine())
{
return costDetailService.createReversalCostDetails(CostDetailReverseRequest.builder()
.acctSchemaId(as.getId())
.reversalDocumentRef(CostingDocumentRef.ofInboundMovementLineId(get_ID()))
.initialDocumentRef(CostingDocumentRef.ofInboundMovementLineId(getReversalLine_ID()))
.date(getDateAcct())
.build());
}
else
{
final AggregatedCostAmount outboundCostResult = getCreateOutboundCosts(as);
final AggregatedCostAmount inboundCostResult = outboundCostResult.getCostElements()
.stream()
.map(costElement -> createInboundCostDetailCreateRequest(as.getId(), costElement, outboundCostResult.getCostAmountForCostElement(costElement).negate()))
.map(costDetailService::createCostDetail)
.reduce(AggregatedCostAmount::merge)
.orElse(null);
return inboundCostResult;
}
}
public final AggregatedCostAmount getCreateOutboundCosts(final AcctSchema as)
{
final ICostingService costDetailService = Adempiere.getBean(ICostingService.class);
if (isReversalLine())
{
return costDetailService.createReversalCostDetails(CostDetailReverseRequest.builder()
.acctSchemaId(as.getId())
.reversalDocumentRef(CostingDocumentRef.ofOutboundMovementLineId(get_ID()))
.initialDocumentRef(CostingDocumentRef.ofOutboundMovementLineId(getReversalLine_ID()))
.date(getDateAcct())
.build());
}
else
{
return costDetailService.createCostDetail(createOutboundCostDetailCreateRequest(as));
}
}
private CostDetailCreateRequest createOutboundCostDetailCreateRequest(final AcctSchema as)
{
return CostDetailCreateRequest.builder()
.acctSchemaId(as.getId())
.clientId(getClientId())
.orgId(getFromOrgId())
.productId(getProductId())
.attributeSetInstanceId(getAttributeSetInstanceId())
.documentRef(CostingDocumentRef.ofOutboundMovementLineId(get_ID()))
.qty(getQty().negate())
.amt(CostAmount.zero(as.getCurrencyId())) // expect to be calculated
.date(getDateAcct())
.build();
}
private CostDetailCreateRequest createInboundCostDetailCreateRequest(
@NonNull final AcctSchemaId acctSchemaId,
@NonNull final CostElement costElement,
@NonNull final CostAmount amt)
{
return CostDetailCreateRequest.builder()
.acctSchemaId(acctSchemaId)
.clientId(getClientId())
.orgId(getToOrgId())
.productId(getProductId())
.attributeSetInstanceId(getAttributeSetInstanceId())
.documentRef(CostingDocumentRef.ofInboundMovementLineId(get_ID()))
.qty(getQty())
.amt(amt)
.costElement(costElement)
.date(getDateAcct())
.build();
}
}