001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.List; 042import javax.enterprise.context.Dependent; 043 044@Dependent 045public class CreatePurchaseInvoiceLineCommand 046 extends BaseSimpleCommand<CreatePurchaseInvoiceLineForm> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.PurchaseInvoiceLine.name(), SecurityRoles.Create.name()) 056 )) 057 )); 058 059 FORM_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("InvoiceName", FieldType.ENTITY_NAME, true, null, null), 061 new FieldDefinition("InvoiceLineSequence", FieldType.UNSIGNED_INTEGER, false, null, null), 062 new FieldDefinition("ParentInvoiceLine", FieldType.UNSIGNED_INTEGER, false, null, null), 063 new FieldDefinition("Amount", FieldType.COST_UNIT, true, null, null), 064 new FieldDefinition("InvoiceLineTypeName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 067 ); 068 } 069 070 /** Creates a new instance of CreatePurchaseInvoiceLineCommand */ 071 public CreatePurchaseInvoiceLineCommand() { 072 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 073 } 074 075 @Override 076 protected void setupValidator(Validator validator) { 077 var invoiceName = form.getInvoiceName(); 078 var invoice = invoiceName == null? null: PurchaseInvoiceLogic.getInstance().getInvoiceByName(invoiceName); 079 080 if(invoice != null) { 081 validator.setCurrency(InvoiceLogic.getInstance().getInvoiceCurrency(invoice)); 082 } 083 } 084 085 @Override 086 protected BaseResult execute() { 087 var invoiceControl = Session.getModelController(InvoiceControl.class); 088 var result = PurchaseResultFactory.getCreatePurchaseInvoiceLineResult(); 089 InvoiceLine invoiceLine = null; 090 var invoiceName = form.getInvoiceName(); 091 var invoiceType = invoiceControl.getInvoiceTypeByName(InvoiceTypes.PURCHASE_INVOICE.name()); 092 var invoice = invoiceControl.getInvoiceByName(invoiceType, invoiceName); 093 094 if(invoice != null) { 095 var rawParentInvoiceLine = form.getParentInvoiceLine(); 096 var intParentInvoiceLine = rawParentInvoiceLine == null? null: Integer.valueOf(rawParentInvoiceLine); 097 var parentInvoiceLine = intParentInvoiceLine == null? null: invoiceControl.getInvoiceLine(invoice, intParentInvoiceLine); 098 099 if(rawParentInvoiceLine == null || parentInvoiceLine != null) { 100 var invoiceTypeName = form.getInvoiceLineTypeName(); 101 var invoiceLineType = invoiceControl.getInvoiceLineTypeByName(invoiceType, invoiceTypeName); 102 103 if(invoiceLineType != null) { 104 var accountingControl = Session.getModelController(AccountingControl.class); 105 var glAccountName = form.getGlAccountName(); 106 var glAccount = glAccountName == null? null: accountingControl.getGlAccountByName(glAccountName); 107 108 if(glAccountName == null || glAccount != null) { 109 var strInvoiceLineSequence = form.getInvoiceLineSequence(); 110 var invoiceLineSequence = strInvoiceLineSequence == null? null: Integer.valueOf(strInvoiceLineSequence); 111 112 if(invoiceLineSequence == null || invoiceControl.getInvoiceLine(invoice, invoiceLineSequence) == null) { 113 var amount = Long.valueOf(form.getAmount()); 114 var description = form.getDescription(); 115 116 invoiceLine = PurchaseInvoiceLogic.getInstance().createInvoiceLine(this, invoice, invoiceLineSequence, parentInvoiceLine, amount, invoiceLineType, glAccount, 117 description, getPartyPK()); 118 } else { 119 addExecutionError(ExecutionErrors.DuplicateInvoiceLineSequence.name(), invoiceName, strInvoiceLineSequence); 120 } 121 } else { 122 addExecutionError(ExecutionErrors.UnknownGlAccountName.name(), glAccountName); 123 } 124 } else { 125 addExecutionError(ExecutionErrors.UnknownInvoiceLineTypeName.name(), InvoiceTypes.PURCHASE_INVOICE.name(), invoiceTypeName); 126 } 127 } else { 128 addExecutionError(ExecutionErrors.UnknownParentInvoiceLine.name(), invoiceName, rawParentInvoiceLine); 129 } 130 } else { 131 addExecutionError(ExecutionErrors.UnknownPurchaseInvoiceName.name(), invoiceName); 132 } 133 134 if(invoiceLine != null) { 135 var invoiceLineDetail = invoiceLine.getLastDetail(); 136 137 result.setEntityRef(invoiceLine.getPrimaryKey().getEntityRef()); 138 result.setInvoiceName(invoiceLineDetail.getInvoice().getLastDetail().getInvoiceName()); 139 result.setInvoiceLineSequence(invoiceLineDetail.getInvoiceLineSequence().toString()); 140 } 141 142 return result; 143 } 144 145}