001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.CreatePurchaseInvoiceForm; 020import com.echothree.control.user.purchase.common.result.PurchaseResultFactory; 021import com.echothree.model.control.accounting.server.control.AccountingControl; 022import com.echothree.model.control.contact.server.control.ContactControl; 023import com.echothree.model.control.invoice.common.workflow.PurchaseInvoiceStatusConstants; 024import com.echothree.model.control.invoice.server.logic.PurchaseInvoiceLogic; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.control.shipment.server.logic.FreeOnBoardLogic; 030import com.echothree.model.control.term.server.logic.TermLogic; 031import com.echothree.model.control.vendor.server.control.VendorControl; 032import com.echothree.model.data.invoice.server.entity.Invoice; 033import com.echothree.model.data.party.server.entity.Party; 034import com.echothree.model.data.user.common.pk.UserVisitPK; 035import com.echothree.model.data.vendor.server.entity.Vendor; 036import com.echothree.util.common.command.BaseResult; 037import com.echothree.util.common.message.ExecutionErrors; 038import com.echothree.util.common.validation.FieldDefinition; 039import com.echothree.util.common.validation.FieldType; 040import com.echothree.util.server.control.BaseSimpleCommand; 041import com.echothree.util.server.control.CommandSecurityDefinition; 042import com.echothree.util.server.control.PartyTypeDefinition; 043import com.echothree.util.server.control.SecurityRoleDefinition; 044import com.echothree.util.server.persistence.Session; 045import java.util.Arrays; 046import java.util.Collections; 047import java.util.List; 048 049public class CreatePurchaseInvoiceCommand 050 extends BaseSimpleCommand<CreatePurchaseInvoiceForm> { 051 052 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 053 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 054 055 static { 056 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 059 new SecurityRoleDefinition(SecurityRoleGroups.PurchaseInvoice.name(), SecurityRoles.Create.name()) 060 ))) 061 ))); 062 063 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 064 new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null), 065 new FieldDefinition("BillFromPartyName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("BillFromContactMechanismName", FieldType.ENTITY_NAME, false, null, null), 067 new FieldDefinition("VendorName", FieldType.ENTITY_NAME, false, null, null), 068 new FieldDefinition("BillToPartyName", FieldType.ENTITY_NAME, false, null, null), 069 new FieldDefinition("BillToContactMechanismName", FieldType.ENTITY_NAME, false, null, null), 070 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null), 071 new FieldDefinition("InvoicedTime", FieldType.DATE_TIME, false, null, null), 072 new FieldDefinition("DueTime", FieldType.DATE_TIME, false, null, null), 073 new FieldDefinition("PaidTime", FieldType.DATE_TIME, false, null, null), 074 new FieldDefinition("TermName", FieldType.ENTITY_NAME, false, null, null), 075 new FieldDefinition("FreeOnBoardName", FieldType.ENTITY_NAME, false, null, null), 076 new FieldDefinition("Reference", FieldType.STRING, false, 1L, 40L), 077 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 078 )); 079 } 080 081 /** Creates a new instance of CreatePurchaseInvoiceCommand */ 082 public CreatePurchaseInvoiceCommand(UserVisitPK userVisitPK, CreatePurchaseInvoiceForm form) { 083 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 084 } 085 086 @Override 087 protected BaseResult execute() { 088 var result = PurchaseResultFactory.getCreatePurchaseInvoiceResult(); 089 Invoice invoice = null; 090 var accountingControl = Session.getModelController(AccountingControl.class); 091 var currencyIsoName = form.getCurrencyIsoName(); 092 var currency = currencyIsoName == null ? null : accountingControl.getCurrencyByIsoName(currencyIsoName); 093 094 if(currencyIsoName == null || currency != null) { 095 var termName = form.getTermName(); 096 var freeOnBoardName = form.getFreeOnBoardName(); 097 var term = termName == null ? null : TermLogic.getInstance().getTermByName(this, termName); 098 var freeOnBoard = freeOnBoardName == null ? null : FreeOnBoardLogic.getInstance().getFreeOnBoardByName(this, freeOnBoardName); 099 100 if(!hasExecutionErrors()) { 101 var vendorControl = Session.getModelController(VendorControl.class); 102 var vendorName = form.getVendorName(); 103 var billFromPartyName = form.getBillFromPartyName(); 104 var parameterCount = (vendorName == null ? 0 : 1) + (billFromPartyName == null ? 0 : 1); 105 106 if(parameterCount == 1) { 107 var partyControl = Session.getModelController(PartyControl.class); 108 Party billFrom = null; 109 110 if(vendorName == null) { 111 billFrom = partyControl.getPartyByName(billFromPartyName); 112 113 if(billFrom != null) { 114 if(!billFrom.getLastDetail().getPartyType().getPartyTypeName().equals(PartyTypes.VENDOR.name())) { 115 addExecutionError(ExecutionErrors.InvalidBillFromPartyType.name()); 116 } 117 } else { 118 addExecutionError(ExecutionErrors.UnknownBillFromPartyName.name(), billFromPartyName); 119 } 120 } else { 121 Vendor vendor = vendorControl.getVendorByName(vendorName); 122 123 if(vendor != null) { 124 billFrom = vendor.getParty(); 125 } else { 126 addExecutionError(ExecutionErrors.UnknownVendorName.name(), vendorName); 127 } 128 } 129 130 if(!hasExecutionErrors()) { 131 var contactControl = Session.getModelController(ContactControl.class); 132 var billFromContactMechanismName = form.getBillFromContactMechanismName(); 133 var billFromContactMechanism = billFromContactMechanismName == null? null: contactControl.getPartyContactMechanismByContactMechanismName(this, billFrom, billFromContactMechanismName); 134 135 if(billFromContactMechanismName == null || billFromContactMechanism != null) { 136 var companyName = form.getCompanyName(); 137 var billToPartyName = form.getBillToPartyName(); 138 139 parameterCount = (companyName == null ? 0 : 1) + (billToPartyName == null ? 0 : 1); 140 141 if(parameterCount < 2) { 142 Party billTo = null; 143 144 if(companyName != null) { 145 var partyCompany = partyControl.getPartyCompanyByName(companyName); 146 147 if(partyCompany != null) { 148 billTo = partyCompany.getParty(); 149 } else { 150 addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName); 151 } 152 } else if(billToPartyName != null) { 153 billTo = partyControl.getPartyByName(billToPartyName); 154 155 if(billTo != null) { 156 if(!billTo.getLastDetail().getPartyType().getPartyTypeName().equals(PartyTypes.COMPANY.name())) { 157 addExecutionError(ExecutionErrors.InvalidBillToPartyType.name()); 158 } 159 } else { 160 addExecutionError(ExecutionErrors.UnknownBillToPartyName.name(), billToPartyName); 161 } 162 } else { 163 billTo = getUserSession().getPartyRelationship().getFromParty(); 164 } 165 166 if(!hasExecutionErrors()) { 167 var billToContactMechanismName = form.getBillToContactMechanismName(); 168 var billToContactMechanism = billToContactMechanismName == null ? null : contactControl.getPartyContactMechanismByContactMechanismName(this, billTo, billToContactMechanismName); 169 170 if(billToContactMechanismName == null || billToContactMechanism != null) { 171 var strInvoicedTime = form.getInvoicedTime(); 172 var invoicedTime = strInvoicedTime == null ? null : Long.valueOf(strInvoicedTime); 173 var strDueTime = form.getDueTime(); 174 var dueTime = strDueTime == null ? null : Long.valueOf(strDueTime); 175 var strPaidTime = form.getPaidTime(); 176 var paidTime = strPaidTime == null ? null : Long.valueOf(strPaidTime); 177 var reference = form.getReference(); 178 var description = form.getDescription(); 179 180 invoice = PurchaseInvoiceLogic.getInstance().createInvoice(session, this, billFrom, billFromContactMechanism, billTo, 181 billToContactMechanism, currency, term, freeOnBoard, reference, description, invoicedTime, dueTime, paidTime, 182 PurchaseInvoiceStatusConstants.WorkflowEntrance_NEW_ENTRY, getPartyPK()); 183 } 184 } 185 } else { 186 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 187 } 188 } 189 } 190 } else { 191 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 192 } 193 } 194 } else { 195 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 196 } 197 198 if(invoice != null) { 199 result.setEntityRef(invoice.getPrimaryKey().getEntityRef()); 200 result.setInvoiceName(invoice.getLastDetail().getInvoiceName()); 201 } 202 203 return result; 204 } 205 206}