from __future__ import unicode_literals
import frappe
from frappe.utils import cint, cstr,flt
import json
import math


def on_submit(doc, method):
	for row in doc.items:
		item_qty = frappe.db.get_value("Item", {"name":row.item_code}, "goods_in_transit")	
		goods_in_transit = flt(item_qty)+flt(row.qty)
		frappe.db.set_value("Item", row.item_code, "goods_in_transit", goods_in_transit)


def on_update_after_submit(doc, method):
	if doc.delivered:
		for row in doc.items:
			item_qty = frappe.db.get_value("Item", {"name":row.item_code}, "goods_in_transit")	
			goods_in_transit = flt(item_qty)-flt(row.qty)
			frappe.db.set_value("Item", row.item_code, "goods_in_transit", goods_in_transit)


def save(doc, method):
	set_items(doc)

def carton_num(doc):

	indx=0
	for i in doc.items:
		is_packaging=frappe.db.get_value("Item",i.item_code,"item_group")
		print(is_packaging)
		if is_packaging in ["Carton","packaging material"]:
			
			start_indx=""
			end_indx=""
			
			i.carton_no=str(start_indx)+"-"+str(end_indx)
			i.carton_qty = str(start_indx)+"-"+str(end_indx)
			i.no_of_items_can_be_packed = str(start_indx)+"-"+str(end_indx)
		else:
			start_indx=indx+1
			end_indx=start_indx+i.carton_qty-1
			i.carton_no=str(start_indx)+"-"+str(end_indx)
			indx=end_indx

def set_items(doc):
	
	for item in doc.items:
		is_packaging_item = frappe.db.get_value("Item", item.item_code, "no_of_items_can_be_packed")
		is_carton_req = frappe.db.get_value("Item", item.item_code, "carton")
		item_weight = frappe.db.get_value("Item", item.item_code, "weight_per_unit")
		available_qty = frappe.db.sql_list("""select sum(actual_qty) from tabBin 
							where item_code='{0}' and warehouse='{1}'""".format(item.item_code, item.warehouse))
		if is_packaging_item and is_carton_req:
			carton_item_doc_name = frappe.get_cached_doc("Item", {"item_code": is_carton_req})
			item.carton_name = carton_item_doc_name.item_code
			qty = item.qty / is_packaging_item
			item.carton_qty = math.ceil(qty)
			item.no_of_items_can_be_packed = is_packaging_item
			item.per_carton_weight_in_kg = carton_item_doc_name.per_carton_weight_kgs
			item.total_carton_weight_in_kg = item_weight * item.qty + item.per_carton_weight_in_kg * item.carton_qty
			item.dimension = carton_item_doc_name.dimension
			item.available_stock = available_qty[0]
			item.used_qty = item.qty
		elif not is_packaging_item:
			item_link = "<a target=_blank href='#Form/Item/{0}'>{1}</a>".format(item.item_code, item.item_code)
			msg = "Kindly Update No. of Item can be packed Field for Item {0}".format(item_link)
			frappe.throw(msg)
	carton_num(doc)