import os
import utils.models as utils
import stocks.models as stocks

def save_customers(institution_id):
	lines = list()
	institution = utils.Institution.objects.filter(id=institution_id).first()

	with open("clients.csv", "r", encoding="utf-8") as f:
		lines = f.readlines()  # Liste des lignes
		#print(lignes)

	id_code = 1110
	for line in lines:
		id_code += 1
		if "lient" in line.lower():
			data = line.split(",")
			name = data[0]
			tel = data[12]
			code = "CLI"+str(id_code) 
			client = stocks.Customer(m_code=code,m_name=name,m_tel=tel,m_institution=institution)
			client.save()
			print(name)
			
def save_products(institution_id):
	lines = list()
	institution = utils.Institution.objects.filter(id=institution_id).first()

	with open("products.csv", "r", encoding="utf-8") as f:
		lines = f.readlines()  # Liste des lignes
	id_code = 1001
	for line in lines:
		id_code += 1
		data = line.split(",")
		name = data[0]
		code = name[:3]
		qte = data[3]
		type_product = stocks.TypeProduct.objects.filter(m_name=code).first()
		if type_product == None:
			type_product = stocks.TypeProduct(m_name=code,m_institution=institution)
			type_product.save()
		price = data[1]
		product = stocks.Product(m_name=name,m_code = ( code +str(id_code) ), m_price=price, m_qte=qte, m_type=type_product )
		product.save()
		print(product)
