<commit_before>/*******************************************************************************
 * Copyright (c) 2005-2016, <NAME> and Elexis
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial implementation
 * 	  MEDEVIT - major refactorings due to multi-stock implementation
 *******************************************************************************/
package ch.elexis.data;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.IProgressMonitor;

import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.data.interfaces.IFall;
import ch.elexis.core.data.util.IRunnableWithProgress;
import ch.elexis.core.model.IStockEntry;
import ch.elexis.core.model.article.IArticle;
import ch.rgw.tools.Money;
import ch.rgw.tools.StringTool;
import ch.rgw.tools.TimeTool;

public class Artikel extends VerrechenbarAdapter implements IArticle {
	
	public static final String TABLENAME = "ARTIKEL";
	
	public static final String FLD_EAN = "EAN";
	public static final String FLD_CODECLASS = "Codeclass";
	public static final String FLD_KLASSE = "Klasse";
	public static final String XID_PHARMACODE = "www.xid.ch/id/pharmacode/ch";
	public static final String FLD_SUB_ID = "SubID";
	public static final String ARTIKEL = "Artikel";
	public static final String FLD_PHARMACODE = "Pharmacode";
	public static final String FLD_EXTID = "ExtID";
	public static final String VERKAUFSEINHEIT = "Verkaufseinheit";
	public static final String VERPACKUNGSEINHEIT = "Verpackungseinheit";
	public static final String FLD_VK_PREIS = "VK_Preis";
	public static final String FLD_EK_PREIS = "EK_Preis";
	public static final String EIGENNAME = "Eigenname";
	public static final String FLD_TYP = "Typ";
	public static final String FLD_NAME = "Name";
	public static final String FLD_ATC_CODE = "ATC_code";
	
	/** Deprecated - will be removed in 3.3 (https://redmine.medelexis.ch/issues/5204) **/
	@Deprecated
	public static final String LIEFERANT_ID = "LieferantID";
	@Deprecated
	public static final String ISTBESTAND = "Istbestand";
	@Deprecated
	public static final String ANBRUCH = "Anbruch";
	@Deprecated
	public static final String MINBESTAND = "Minbestand";
	@Deprecated
	public static final String MAXBESTAND = "Maxbestand";
	/** END **/
	
	public static final Pattern NAME_VE_PATTERN = Pattern.compile(".+ ([0-9]+) Stk.*");
	
	static {
		addMapping(TABLENAME, LIEFERANT_ID, FLD_NAME, MAXBESTAND, MINBESTAND, ISTBESTAND,
			FLD_EK_PREIS, FLD_VK_PREIS, FLD_TYP, FLD_EXTINFO, FLD_EAN, FLD_SUB_ID,
			EIGENNAME + "=Name_intern", FLD_CODECLASS, FLD_KLASSE, FLD_ATC_CODE, FLD_EXTID);
		Xid.localRegisterXIDDomainIfNotExists(XID_PHARMACODE, FLD_PHARMACODE,
			Xid.ASSIGNMENT_REGIONAL);
	}
	
	@Override
	protected String getTableName(){
		return TABLENAME;
	}
	
	/**
	 * @param qbe
	 * @param clazz
	 * @deprecated to be removed in 3.3
	 * @see https://redmine.medelexis.ch/issues/5204
	 */
	public static void transferAllStockInformationToNew32StockModel(Query<? extends Artikel> qbe,
		Class<? extends Artikel> clazz){
		if (!CoreHub.globalCfg.get(clazz.getSimpleName() + "StocksMigratedTo32", false)) {
			IRunnableWithProgress irwp = new IRunnableWithProgress() {
				@Override
				public void run(IProgressMonitor monitor)
					throws InvocationTargetException, InterruptedException{
					log.debug("Migrating stock information");
					qbe.startGroup();
					qbe.add(MINBESTAND, Query.NOT_EQUAL, null);
					qbe.or();
					qbe.add(ISTBESTAND, Query.NOT_EQUAL, null);
					qbe.add(MAXBESTAND, Query.NOT_EQUAL, null);
					qbe.add(LIEFERANT_ID, Query.NOT_EQUAL, null);
					qbe.endGroup();
					List<? extends Artikel> stockArticles = qbe.execute();
					monitor.beginTask(
						"Migrating " + clazz.getSimpleName() + " to new stock format.",
						stockArticles.size());
					Stock stdStock = Stock.load(Stock.DEFAULT_STOCK_ID);
					for (Artikel art : stockArticles) {
						if (art.isProduct()) {
							log.warn("Article is product with stock [{}].", art.getId());
							continue;
						}
						log.debug("Migrating stock information for [{}]", art.getLabel());
						IStockEntry se = CoreHub.getStockService().storeArticleInStock(stdStock,
							art.storeToString());
						String[] fields = new String[] {
							MINBESTAND, ISTBESTAND, MAXBESTAND, LIEFERANT_ID
						};
						String[] values = art.get(false, fields);
						String anbruch = art.getExt(ANBRUCH);
						if (anbruch != null && anbruch.length() > 0) {
							int anbruchValue = 0;
							try {
								anbruchValue = Integer.valueOf(anbruch);
							} catch (NumberFormatException nfe) {
								log.warn(<commit_msg>[9159] Migrate article  if stock cur > 0 or max >0 (#206)<commit_after>/*******************************************************************************
 * Copyright (c) 2005-2016, <NAME> and Elexis
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial implementation
 * 	  MEDEVIT - major refactorings due to multi-stock implementation
 *******************************************************************************/
package ch.elexis.data;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.IProgressMonitor;

import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.data.interfaces.IFall;
import ch.elexis.core.data.util.IRunnableWithProgress;
import ch.elexis.core.model.IStockEntry;
import ch.elexis.core.model.article.IArticle;
import ch.rgw.tools.Money;
import ch.rgw.tools.StringTool;
import ch.rgw.tools.TimeTool;

public class Artikel extends VerrechenbarAdapter implements IArticle {
	
	public static final String TABLENAME = "ARTIKEL";
	
	public static final String FLD_EAN = "EAN";
	public static final String FLD_CODECLASS = "Codeclass";
	public static final String FLD_KLASSE = "Klasse";
	public static final String XID_PHARMACODE = "www.xid.ch/id/pharmacode/ch";
	public static final String FLD_SUB_ID = "SubID";
	public static final String ARTIKEL = "Artikel";
	public static final String FLD_PHARMACODE = "Pharmacode";
	public static final String FLD_EXTID = "ExtID";
	public static final String VERKAUFSEINHEIT = "Verkaufseinheit";
	public static final String VERPACKUNGSEINHEIT = "Verpackungseinheit";
	public static final String FLD_VK_PREIS = "VK_Preis";
	public static final String FLD_EK_PREIS = "EK_Preis";
	public static final String EIGENNAME = "Eigenname";
	public static final String FLD_TYP = "Typ";
	public static final String FLD_NAME = "Name";
	public static final String FLD_ATC_CODE = "ATC_code";
	
	/** Deprecated - will be removed in 3.3 (https://redmine.medelexis.ch/issues/5204) **/
	@Deprecated
	public static final String LIEFERANT_ID = "LieferantID";
	@Deprecated
	public static final String ISTBESTAND = "Istbestand";
	@Deprecated
	public static final String ANBRUCH = "Anbruch";
	@Deprecated
	public static final String MINBESTAND = "Minbestand";
	@Deprecated
	public static final String MAXBESTAND = "Maxbestand";
	/** END **/
	
	public static final Pattern NAME_VE_PATTERN = Pattern.compile(".+ ([0-9]+) Stk.*");
	
	static {
		addMapping(TABLENAME, LIEFERANT_ID, FLD_NAME, MAXBESTAND, MINBESTAND, ISTBESTAND,
			FLD_EK_PREIS, FLD_VK_PREIS, FLD_TYP, FLD_EXTINFO, FLD_EAN, FLD_SUB_ID,
			EIGENNAME + "=Name_intern", FLD_CODECLASS, FLD_KLASSE, FLD_ATC_CODE, FLD_EXTID);
		Xid.localRegisterXIDDomainIfNotExists(XID_PHARMACODE, FLD_PHARMACODE,
			Xid.ASSIGNMENT_REGIONAL);
	}
	
	@Override
	protected String getTableName(){
		return TABLENAME;
	}
	
	/**
	 * @param qbe
	 * @param clazz
	 * @deprecated to be removed in 3.3
	 * @see https://redmine.medelexis.ch/issues/5204
	 */
	public static void transferAllStockInformationToNew32StockModel(Query<? extends Artikel> qbe,
		Class<? extends Artikel> clazz){
		if (!CoreHub.globalCfg.get(clazz.getSimpleName() + "StocksMigratedTo32", false)) {
			IRunnableWithProgress irwp = new IRunnableWithProgress() {
				@Override
				public void run(IProgressMonitor monitor)
					throws InvocationTargetException, InterruptedException{
					log.debug("Migrating stock information");
					qbe.startGroup();
					qbe.add(ISTBESTAND, Query.GREATER, "0");
					qbe.or();
					qbe.add(MAXBESTAND, Query.GREATER, "0");
					qbe.endGroup();
					List<? extends Artikel> stockArticles = qbe.execute();
					monitor.beginTask(
						"Migrating " + clazz.getSimpleName() + " to new stock format.",
						stockArticles.size());
					Stock stdStock = Stock.load(Stock.DEFAULT_STOCK_ID);
					for (Artikel art : stockArticles) {
						if (art.isProduct()) {
							log.warn("Article is product with stock [{}].", art.getId());
							continue;
						}
						log.debug("Migrating stock information for [{}]", art.getLabel());
						IStockEntry se = CoreHub.getStockService().storeArticleInStock(stdStock,
							art.storeToString());
						String[] fields = new String[] {
							MINBESTAND, ISTBESTAND, MAXBESTAND, LIEFERANT_ID
						};
						String[] values = art.get(false, fields);
						String anbruch = art.getExt(ANBRUCH);
						if (anbruch != null && anbruch.length() > 0) {
							int anbruchValue = 0;
							try {
								anbruchValue = Integer.valueOf(anbruch);
							} catch (NumberFormatException nfe) {
								log.warn(
{'fraction_non_alphanumeric': 0.1368068251066423, 'fraction_numerical': 0.007617306520414382, 'mean_word_length': 4.401535929786067, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4435439', 'n_tokens_mistral': 3713, 'n_tokens_neox': 3151, 'n_words': 785}