19 package org.turro.financials.handshake;
23 import org.turro.string.Strings;
24 import org.turro.command.Command;
25 import org.turro.command.Context;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.util.DateFormats;
28 import org.turro.elephant.util.Images;
29 import org.turro.financials.contract.ContractCombobox;
30 import org.turro.financials.db.FinancialsPU;
31 import org.turro.financials.entity.Contract;
32 import org.turro.financials.entity.ContractHandshake;
33 import org.turro.financials.entity.Document;
34 import org.turro.financials.entity.DocumentDefinition;
35 import org.turro.financials.entity.DocumentLine;
36 import org.turro.financials.entity.DocumentRelation;
37 import org.turro.financials.entity.Product;
38 import org.turro.financials.entity.ProductByContractor;
39 import org.turro.financials.menu.FinancialsMenu;
40 import org.turro.financials.model.contract.ContractUsualSet;
41 import org.turro.financials.product.ProductCombobox;
42 import org.turro.http.ElephantResponse;
43 import org.turro.i18n.I_;
44 import org.turro.jpa.Dao;
45 import org.turro.zkoss.dialog.InputDialog;
46 import org.turro.zkoss.dialog.InputField;
47 import org.turro.zul.frame.Framework;
48 import org.zkoss.zk.ui.HtmlBasedComponent;
56 private final File file;
57 private final Object handshake;
61 this.handshake = handshake;
101 if(contract !=
null) {
109 if(contract !=
null) {
111 if(document !=
null) {
120 if(product !=
null) {
152 return file.delete();
158 connectContract(hc, command);
163 connectProduct(hp, command);
167 private void connectContract(
final HandshakeContract hc,
final Command command) {
170 I_.
get(
"Connect to..."),
172 new InputField(
"Contract", null, null, 0) {
174 protected HtmlBasedComponent createEditor() {
175 ContractCombobox ccb = new ContractCombobox();
176 ccb.setOnlyActive(true);
177 ccb.setDocumentDefinition(DocumentDefinition.getHandshakeReceiver());
183 public Object execute(Context context) {
184 InputField[] fields = (InputField[]) context.get(
"fields");
185 if (fields.length > 0) {
186 for (InputField f : fields) {
187 if (
"Contract".equals(f.getLabel())) {
188 Contract contract = (Contract) f.
getValue();
189 ContractHandshake ch =
new ContractHandshake();
190 ch.setContract(contract);
194 contract.setContractHandshake(ch);
195 ElephantResponse er = HandshakeUtil.sendAcceptance(contract, ElephantContext.getServerUrl(
"http"), hc.
remoteId);
196 if(ElephantResponse.isCorrect(er)) {
197 new FinancialsPU().saveObject(contract);
198 if(command !=
null) command.execute(context);
208 private void connectProduct(
final HandshakeProduct hp,
final Command command) {
209 InputDialog.getInput(
210 Framework.getCurrent().getPage(),
211 I_.get(
"Connect to..."),
213 new InputField(
"Product", null, null, 0) {
215 protected HtmlBasedComponent createEditor() {
216 return new ProductCombobox();
221 public Object execute(Context context) {
222 InputField[] fields = (InputField[]) context.get(
"fields");
223 if (fields.length > 0) {
224 for (InputField f : fields) {
225 if (
"Product".equals(f.getLabel())) {
226 Product product = (Product) f.getValue();
227 ProductByContractor pbc =
new ProductByContractor();
228 pbc.setContract(hp.contract);
229 pbc.setProduct(product);
230 pbc.setContractorCode(hp.code);
231 pbc.setDescription(hp.description);
232 pbc.setPrice(hp.price);
234 new FinancialsPU().saveObject(pbc);
235 if(command !=
null) command.execute(context);
251 if(createProduct(hp))
return true;
258 Document document = processDocument(dao, hsdoc);
259 if(document !=
null && !document.
isEmpty()) {
266 private Document processDocument(Dao dao, HandshakeDocument hsdoc) {
267 Contract contract = HandshakeContract.getContractFromHandshake(hsdoc.contract);
268 Document document =
new Document();
269 document.setContract(contract);
270 document.setDocumentDefinition(dao.find(DocumentDefinition.class, (Long) hsdoc.definitionId));
271 document.setDocumentNumber(hsdoc.documentNumber);
272 document.setDocumentDate(hsdoc.documentDate);
273 document.setReceiptDate(document.getDocumentDate());
274 Set<Contract> usualStores =
new ContractUsualSet(document.getStores(),
true, document.getUsualPath() +
"st:");
275 Contract store = usualStores.iterator().next();
277 store = document.getDefaultStore();
280 for(HandshakeDocLine hsdl : hsdoc.lines) {
281 DocumentLine dl =
new DocumentLine();
282 dl.setDocument(document);
283 dl.setLineOrder(count++);
284 dl.setConcept(hsdl.concept);
285 dl.setDiscountMoney(hsdl.discountMoney);
286 dl.setDiscountPerCent(hsdl.discountPerCent);
287 dl.setEquivalenceSurcharge(hsdl.equivalenceSurcharge);
288 dl.setPrice(hsdl.price);
289 if(hsdl.product !=
null) setDocumentLineProduct(dl, hsdl.product);
290 dl.setQuantity(hsdl.quantity);
291 dl.setRetention(hsdl.retention);
293 dl.setLineType(document.getDefaultLineType());
295 document.getDocumentLines().add(dl);
297 for(HandshakeDocument hsd : hsdoc.expiries) {
298 Document d = processDocument(dao, hsd);
300 DocumentRelation dr =
new DocumentRelation();
301 dr.setAncestor(document);
303 document.getDescendants().add(dr);
309 private void setDocumentLineProduct(DocumentLine dl, HandshakeProduct product) {
310 product.contract = dl.getDocument().getContract();
311 ProductByContractor pbc = HandshakeProduct.getProductByContractorFromHandshake(product);
313 dl.setProductByContractor(pbc);
314 dl.setProduct(pbc.getProduct());
315 }
else if(!Strings.isBlank(product.description)) {
316 if(Strings.isBlank(dl.getConcept())) {
317 dl.setConcept(product.description);
319 dl.setConcept(product.description +
"\n" + dl.getConcept());
324 private boolean createProduct(HandshakeProduct hp) {
325 Dao dao =
new FinancialsPU();
326 Product product =
new Product();
327 product.setDescription(hp.description);
328 product.setPrice(hp.price);
329 product.setTax(hp.tax);
330 ProductByContractor pbc =
new ProductByContractor();
331 pbc.setContract(hp.contract);
332 pbc.setProduct(product);
333 pbc.setContractorCode(hp.code);
334 pbc.setDescription(hp.description);
335 pbc.setPrice(hp.price);
337 product.getProductByContractors().add(pbc);
338 product.prepareSave();
339 dao.saveObject(product);
static String getImage(String image)
static Contract getContractFromHandshake(HandshakeContract hc)
HandshakeContract contract
static Document getDocumentFromHandshake(Contract contract, HandshakeDocument hd)
void connect(Command command)
HandshakeItemStatus getStatus()
HandshakeItem(File file, Object handshake)
static Product getProductFromHandshake(HandshakeProduct hp)
transient String petitioner
static String get(String msg)
static Framework getCurrent()