001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.control.user.purchase.server.command;
018
019import com.echothree.control.user.purchase.common.form.CreatePurchaseInvoiceLineForm;
020import com.echothree.control.user.purchase.common.result.PurchaseResultFactory;
021import com.echothree.model.control.accounting.server.control.AccountingControl;
022import com.echothree.model.control.invoice.common.InvoiceTypes;
023import com.echothree.model.control.invoice.server.control.InvoiceControl;
024import com.echothree.model.control.invoice.server.logic.InvoiceLogic;
025import com.echothree.model.control.invoice.server.logic.PurchaseInvoiceLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.invoice.server.entity.InvoiceLine;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseSimpleCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import com.echothree.util.server.persistence.Session;
040import com.echothree.util.server.validation.Validator;
041import java.util.Arrays;
042import java.util.Collections;
043import java.util.List;
044import javax.enterprise.context.RequestScoped;
045
046@RequestScoped
047public class CreatePurchaseInvoiceLineCommand
048        extends BaseSimpleCommand<CreatePurchaseInvoiceLineForm> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
057                    new SecurityRoleDefinition(SecurityRoleGroups.PurchaseInvoiceLine.name(), SecurityRoles.Create.name())
058                    )))
059                )));
060        
061        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("InvoiceName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("InvoiceLineSequence", FieldType.UNSIGNED_INTEGER, false, null, null),
064                new FieldDefinition("ParentInvoiceLine", FieldType.UNSIGNED_INTEGER, false, null, null),
065                new FieldDefinition("Amount", FieldType.COST_UNIT, true, null, null),
066                new FieldDefinition("InvoiceLineTypeName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, false, null, null),
068                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
069                ));
070    }
071    
072    /** Creates a new instance of CreatePurchaseInvoiceLineCommand */
073    public CreatePurchaseInvoiceLineCommand() {
074        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
075    }
076    
077    @Override
078    protected void setupValidator(Validator validator) {
079        var invoiceName = form.getInvoiceName();
080        var invoice = invoiceName == null? null: PurchaseInvoiceLogic.getInstance().getInvoiceByName(invoiceName);
081        
082        if(invoice != null) {
083            validator.setCurrency(InvoiceLogic.getInstance().getInvoiceCurrency(invoice));
084        }
085    }
086    
087    @Override
088    protected BaseResult execute() {
089        var invoiceControl = Session.getModelController(InvoiceControl.class);
090        var result = PurchaseResultFactory.getCreatePurchaseInvoiceLineResult();
091        InvoiceLine invoiceLine = null;
092        var invoiceName = form.getInvoiceName();
093        var invoiceType = invoiceControl.getInvoiceTypeByName(InvoiceTypes.PURCHASE_INVOICE.name());
094        var invoice = invoiceControl.getInvoiceByName(invoiceType, invoiceName);
095        
096        if(invoice != null) {
097            var rawParentInvoiceLine = form.getParentInvoiceLine();
098            var intParentInvoiceLine = rawParentInvoiceLine == null? null: Integer.valueOf(rawParentInvoiceLine);
099            var parentInvoiceLine = intParentInvoiceLine == null? null: invoiceControl.getInvoiceLine(invoice, intParentInvoiceLine);
100            
101            if(rawParentInvoiceLine == null || parentInvoiceLine != null) {
102                var invoiceTypeName = form.getInvoiceLineTypeName();
103                var invoiceLineType = invoiceControl.getInvoiceLineTypeByName(invoiceType, invoiceTypeName);
104                
105                if(invoiceLineType != null) {
106                    var accountingControl = Session.getModelController(AccountingControl.class);
107                    var glAccountName = form.getGlAccountName();
108                    var glAccount = glAccountName == null? null: accountingControl.getGlAccountByName(glAccountName);
109                    
110                    if(glAccountName == null || glAccount != null) {
111                        var strInvoiceLineSequence = form.getInvoiceLineSequence();
112                        var invoiceLineSequence = strInvoiceLineSequence == null? null: Integer.valueOf(strInvoiceLineSequence);
113                        
114                        if(invoiceLineSequence == null || invoiceControl.getInvoiceLine(invoice, invoiceLineSequence) == null) {
115                            var amount = Long.valueOf(form.getAmount());
116                            var description = form.getDescription();
117
118                            invoiceLine = PurchaseInvoiceLogic.getInstance().createInvoiceLine(this, invoice, invoiceLineSequence, parentInvoiceLine, amount, invoiceLineType, glAccount,
119                                    description, getPartyPK());
120                        } else {
121                            addExecutionError(ExecutionErrors.DuplicateInvoiceLineSequence.name(), invoiceName, strInvoiceLineSequence);
122                        }
123                    } else {
124                        addExecutionError(ExecutionErrors.UnknownGlAccountName.name(), glAccountName);
125                    }
126                } else {
127                    addExecutionError(ExecutionErrors.UnknownInvoiceLineTypeName.name(), InvoiceTypes.PURCHASE_INVOICE.name(), invoiceTypeName);
128                }
129            } else {
130                addExecutionError(ExecutionErrors.UnknownParentInvoiceLine.name(), invoiceName, rawParentInvoiceLine);
131            }
132        } else {
133            addExecutionError(ExecutionErrors.UnknownPurchaseInvoiceName.name(), invoiceName);
134        }
135        
136        if(invoiceLine != null) {
137            var invoiceLineDetail = invoiceLine.getLastDetail();
138            
139            result.setEntityRef(invoiceLine.getPrimaryKey().getEntityRef());
140            result.setInvoiceName(invoiceLineDetail.getInvoice().getLastDetail().getInvoiceName());
141            result.setInvoiceLineSequence(invoiceLineDetail.getInvoiceLineSequence().toString());
142        }
143        
144        return result;
145    }
146    
147}