JavaScript:
frappe.provide("erpnext.utils");
laying_flocks = {};
var wrapper = $(cur_frm.fields_dict['htmlpopulate'].wrapper);
cur_frm.set_value('batched_egg_yield', '');
cur_frm.set_value('collection_date', moment());
// ! also set today date as default
// Render template of flocks with text input fields to tally the eggs collected
frappe.ui.form.on('Egg Collection', {
onload: function (frm) {
frappe.call({
method: "get_laying_flocks",
doc: cur_frm.doc,
callback: function (r) {
//console.log(r.message);
laying_flocks = r.message;
wrapper.html(frappe.render_template("eggcollection", {"laying flocks" : laying_flocks}))
}
});
// fetch and populate egg defaults
frappe.call({
method: "egg_defaults",
doc: cur_frm.doc,
callback: function (r) {
cur_frm.set_value('at_enter_as_item', r.message[0].egg_stock_item);
cur_frm.set_value('at_enter_as_uom', r.message[0].egg_default_uom);
cur_frm.set_value('destination_warehouse',r.message[0].egg_default_storage_location);
}
})
}
});
// pass to server on validate
frappe.ui.form.on('Egg Collection', {
validate: function (frm) {
egg_yields = (wrapper.find(".egg_yield"));
collected_eggs = [];
batched_egg_yield = 0;
// assign to global egg yield dict
for (i in laying_flocks) {
egg_count = parseInt(egg_yields[i].value)
collected_eggs.push(laying_flocks[i].name, egg_count)
batched_egg_yield += egg_count || 0;
};
var egg_dict = {};
collected_eggs.forEach(function(val, i) {
if (i % 2 === 1) return; // Skip all even elements (= odd indexes)
egg_dict[val] = collected_eggs[i + 1]; // Assign the next element as a value of the object,using the current value as key
});
cur_frm.set_value('batched_egg_yield', batched_egg_yield);
console.log(egg_dict)
frappe.call({
method: "parse_data_types",
doc: cur_frm.doc,
args: JSON.stringify(egg_dict), // send egg yield
})
.fail(fail => console.log("failure", fail))
.done(success => console.log("success", success.message));
}
})
Python:
from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
import json
class EggCollection(Document):
def get_laying_flocks(self):
flock_dict = frappe.get_all("Poultry Flock",
fields=["name"],
filters={"flock_purpose": "Laying"},
order_by='name')
return flock_dict
def egg_defaults(self):
return frappe.db.get_values_from_single(["egg_default_uom","egg_default_storage_location","egg_stock_item"], None, "Livestock Settings", as_dict=True)
def collect_egg_yield(self, collected_eggs):
print collected_eggs
# frappe.msgprint(str(collected_eggs), "collected_eggs")
# collected_eggs = json.loads(collected_eggs, parse_float=True, parse_int=True)
# frappe.msgprint(collected_eggs)
#for key, value in collected_eggs:
# yield make_egg_detail(self, collected_eggs)
# frappe.msgprint(str(collected_eggs),"Egg Collection Records Saved")
#pass_to_stock(self)
def make_egg_detail(self, collected_eggs):
make_egg_detail = frappe.new_doc('Egg Collection Detail')
make_egg_detail.poultry_flock = collected_eggs[key]
make_egg_detail.egg_yield = collected_eggs[value]
make_egg_detail.date_collected = self.collection_date
make_egg_detail.save()
# enter the rest of of the detail here
def pass_to_stock(self):
# this appears to be working except for the conversion table
pass_to_stock=frappe.new_doc('Stock Entry')
pass_to_stock.purpose = "Material Receipt"
pass_to_stock.append("items", {
"item_code": self.at_enter_as_item,
"qty": self.batched_egg_yield,
"uom": "Nos",
"stock_uom": self.at_enter_as_uom,
"conversion_factor": "1",
"transfer_qty": self.batched_egg_yield
})
pass_to_stock.company = frappe.db.get_value("Global Defaults", None, "default_company")
pass_to_stock.to_warehouse = "Finished Goods - " + str((frappe.db.get_value("Company", pass_to_stock.company, "abbr")))
pass_to_stock.date = self.collection_date
# pass_to_stock.time = self.time_collected
pass_to_stock.save()
frappe.msgprint("Material Receipt Saved")
def parse_data_types(*args,**kwargs):
frappe.msgprint(kwargs,"parse data types")
for i in kwargs.iteritems():
if i[0] not in ['cmd']: # cmd, data, method
try:
print("key = {0}, value = {1}, type_of_value = {2}".format(i[0], json.loads(i[1]), type(json.loads(i[1]))))
except Exception as e:
print ("ERROR", i[1]) # This will print what didn't decode as expected
# print kwargs
# frappe.msgprint(str(kwargs),"parse data types")
return kwargs # self.collect_egg_yield(kwargs)