fix(shipment): change shipment test and refactor shipment.js
This commit is contained in:
parent
ac3c1f1493
commit
28055f483d
@ -619,6 +619,7 @@ def make_shipment(source_name, target_doc=None):
|
||||
"name": "prevdoc_detail_docname",
|
||||
"parent": "prevdoc_docname",
|
||||
"parenttype": "prevdoc_doctype",
|
||||
"base_amount": "grand_total"
|
||||
}
|
||||
}
|
||||
}, target_doc, postprocess)
|
||||
|
@ -23,76 +23,20 @@ frappe.ui.form.on('Shipment', {
|
||||
},
|
||||
onload: function(frm) {
|
||||
frm.set_query("delivery_address_name", () => {
|
||||
let link_doctype = '';
|
||||
let link_name = '';
|
||||
let is_your_company_address = 0;
|
||||
if (frm.doc.delivery_to_type == 'Customer') {
|
||||
link_doctype = 'Customer';
|
||||
link_name = frm.doc.delivery_customer;
|
||||
}
|
||||
if (frm.doc.delivery_to_type == 'Supplier') {
|
||||
link_doctype = 'Supplier';
|
||||
link_name = frm.doc.delivery_supplier;
|
||||
}
|
||||
if (frm.doc.delivery_to_type == 'Company') {
|
||||
link_doctype = 'Company';
|
||||
link_name = frm.doc.delivery_company;
|
||||
is_your_company_address = 1;
|
||||
}
|
||||
return frm.events.address_query(frm, link_doctype, link_name, is_your_company_address);
|
||||
let delivery_to = `delivery_${frappe.model.scrub(frm.doc.delivery_to_type)}`
|
||||
return frm.events.address_query(frm, frm.doc.delivery_to_type, frm.doc[delivery_to], frm.doc.delivery_to_type === 'Company' ? 1 : 0);
|
||||
});
|
||||
frm.set_query("pickup_address_name", () => {
|
||||
let link_doctype = '';
|
||||
let link_name = '';
|
||||
let is_your_company_address = 0;
|
||||
if (frm.doc.pickup_from_type == 'Customer') {
|
||||
link_doctype = 'Customer';
|
||||
link_name = frm.doc.pickup_customer;
|
||||
}
|
||||
if (frm.doc.pickup_from_type == 'Supplier') {
|
||||
link_doctype = 'Supplier';
|
||||
link_name = frm.doc.pickup_supplier;
|
||||
}
|
||||
if (frm.doc.pickup_from_type == 'Company') {
|
||||
link_doctype = 'Company';
|
||||
link_name = frm.doc.pickup_company;
|
||||
is_your_company_address = 1;
|
||||
}
|
||||
return frm.events.address_query(frm, link_doctype, link_name, is_your_company_address);
|
||||
let pickup_from = `pickup_${frappe.model.scrub(frm.doc.pickup_from_type)}`
|
||||
return frm.events.address_query(frm, frm.doc.pickup_from_type, frm.doc[pickup_from], frm.doc.pickup_from_type === 'Company' ? 1 : 0);
|
||||
});
|
||||
frm.set_query("delivery_contact_name", () => {
|
||||
let link_doctype = '';
|
||||
let link_name = '';
|
||||
if (frm.doc.delivery_to_type == 'Customer') {
|
||||
link_doctype = 'Customer';
|
||||
link_name = frm.doc.delivery_customer;
|
||||
}
|
||||
if (frm.doc.delivery_to_type == 'Supplier') {
|
||||
link_doctype = 'Supplier';
|
||||
link_name = frm.doc.delivery_supplier;
|
||||
}
|
||||
if (frm.doc.delivery_to_type == 'Company') {
|
||||
link_doctype = 'Company';
|
||||
link_name = frm.doc.delivery_company;
|
||||
}
|
||||
return frm.events.contact_query(frm, link_doctype, link_name);
|
||||
let delivery_to = `delivery_${frappe.model.scrub(frm.doc.delivery_to_type)}`
|
||||
return frm.events.contact_query(frm, frm.doc.delivery_to_type, frm.doc[delivery_to]);
|
||||
});
|
||||
frm.set_query("pickup_contact_name", () => {
|
||||
let link_doctype = '';
|
||||
let link_name = '';
|
||||
if (frm.doc.pickup_from_type == 'Customer') {
|
||||
link_doctype = 'Customer';
|
||||
link_name = frm.doc.pickup_customer;
|
||||
}
|
||||
if (frm.doc.pickup_from_type == 'Supplier') {
|
||||
link_doctype = 'Supplier';
|
||||
link_name = frm.doc.pickup_supplier;
|
||||
}
|
||||
if (frm.doc.pickup_from_type == 'Company') {
|
||||
link_doctype = 'Company';
|
||||
link_name = frm.doc.pickup_company;
|
||||
}
|
||||
return frm.events.contact_query(frm, link_doctype, link_name);
|
||||
let pickup_from = `pickup_${frappe.model.scrub(frm.doc.pickup_from_type)}`
|
||||
return frm.events.contact_query(frm, frm.doc.pickup_from_type, frm.doc[pickup_from]);
|
||||
});
|
||||
frm.set_query("delivery_note", "shipment_delivery_note", function() {
|
||||
let customer = '';
|
||||
@ -120,24 +64,10 @@ frappe.ui.form.on('Shipment', {
|
||||
$('div[data-fieldname=delivery_contact] > div > .clearfix').hide();
|
||||
},
|
||||
before_save: function(frm) {
|
||||
if (frm.doc.delivery_to_type == 'Company') {
|
||||
frm.set_value("delivery_to", frm.doc.delivery_company);
|
||||
}
|
||||
if (frm.doc.delivery_to_type == 'Customer') {
|
||||
frm.set_value("delivery_to", frm.doc.delivery_customer);
|
||||
}
|
||||
if (frm.doc.delivery_to_type == 'Supplier') {
|
||||
frm.set_value("delivery_to", frm.doc.delivery_supplier);
|
||||
}
|
||||
if (frm.doc.pickup_from_type == 'Company') {
|
||||
frm.set_value("pickup", frm.doc.pickup_company);
|
||||
}
|
||||
if (frm.doc.pickup_from_type == 'Customer') {
|
||||
frm.set_value("pickup", frm.doc.pickup_customer);
|
||||
}
|
||||
if (frm.doc.pickup_from_type == 'Supplier') {
|
||||
frm.set_value("pickup", frm.doc.pickup_supplier);
|
||||
}
|
||||
let delivery_to = `delivery_${frappe.model.scrub(frm.doc.delivery_to_type)}`
|
||||
frm.set_value("delivery_to", frm.doc[delivery_to]);
|
||||
let pickup_from = `pickup_${frappe.model.scrub(frm.doc.pickup_from_type)}`
|
||||
frm.set_value("pickup", frm.doc[pickup_from]);
|
||||
},
|
||||
set_pickup_company_address: function(frm) {
|
||||
frappe.db.get_value('Address', {
|
||||
@ -476,18 +406,11 @@ frappe.ui.form.on('Shipment', {
|
||||
current_min = '00';
|
||||
current_hour = Number(current_hour)+1;
|
||||
}
|
||||
if (Number(current_hour) > 19 || Number(current_hour) === 19){
|
||||
frappe.throw(__("Today's pickup time is over, please select different date"));
|
||||
}
|
||||
current_hour = (current_hour < 10) ? '0' + current_hour : current_hour;
|
||||
let pickup_time = current_hour +':'+ current_min;
|
||||
return pickup_time;
|
||||
},
|
||||
set_pickup_to_time: function(frm) {
|
||||
let pickup_to_hour = Number(frm.doc.pickup_from.split(':')[0])+5;
|
||||
if (Number(pickup_to_hour) > 19 || Number(pickup_to_hour) === 19){
|
||||
pickup_to_hour = 19;
|
||||
}
|
||||
let pickup_to_min = frm.doc.pickup_from.split(':')[1];
|
||||
let pickup_to = pickup_to_hour +':'+ pickup_to_min;
|
||||
frm.set_value("pickup_to", pickup_to);
|
||||
|
@ -460,13 +460,28 @@
|
||||
],
|
||||
"is_submittable": 1,
|
||||
"links": [],
|
||||
"modified": "2020-11-20 16:19:06.157106",
|
||||
"modified": "2020-11-23 16:26:28.132608",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Stock",
|
||||
"name": "Shipment",
|
||||
"owner": "Administrator",
|
||||
"permissions": [
|
||||
{
|
||||
"cancel": 1,
|
||||
"create": 1,
|
||||
"delete": 1,
|
||||
"email": 1,
|
||||
"export": 1,
|
||||
"print": 1,
|
||||
"read": 1,
|
||||
"report": 1,
|
||||
"role": "Stock Manager",
|
||||
"share": 1,
|
||||
"submit": 1,
|
||||
"write": 1
|
||||
},
|
||||
{
|
||||
"cancel": 1,
|
||||
"create": 1,
|
||||
"delete": 1,
|
||||
"email": 1,
|
||||
@ -476,6 +491,7 @@
|
||||
"report": 1,
|
||||
"role": "System Manager",
|
||||
"share": 1,
|
||||
"submit": 1,
|
||||
"write": 1
|
||||
}
|
||||
],
|
||||
|
@ -7,89 +7,19 @@ from datetime import date, timedelta
|
||||
|
||||
import frappe
|
||||
import unittest
|
||||
from erpnext.stock.doctype.shipment.shipment import fetch_shipping_rates
|
||||
from erpnext.stock.doctype.shipment.shipment import create_shipment
|
||||
from erpnext.stock.doctype.shipment.shipment import update_tracking
|
||||
from erpnext.stock.doctype.delivery_note.delivery_note import make_shipment
|
||||
|
||||
class TestShipment(unittest.TestCase):
|
||||
pass
|
||||
|
||||
def test_shipment_booking(self):
|
||||
shipment = create_test_shipment()
|
||||
try:
|
||||
shipment.submit()
|
||||
except:
|
||||
frappe.throw('Error occurred on submit shipment')
|
||||
doc, rate, tracking_data = make_shipment_transaction(shipment)
|
||||
if doc and rate and tracking_data:
|
||||
self.assertEqual(doc.service_provider, rate.get('service_provider'))
|
||||
self.assertEqual(doc.shipment_amount, rate.get('actual_price'))
|
||||
self.assertEqual(doc.carrier, rate.get('carrier'))
|
||||
self.assertEqual(doc.tracking_status, tracking_data.get('tracking_status'))
|
||||
self.assertEqual(doc.tracking_url, tracking_data.get('tracking_url'))
|
||||
|
||||
def test_shipment_from_delivery_note(self):
|
||||
delivery_note = create_test_delivery_note()
|
||||
try:
|
||||
delivery_note.submit()
|
||||
except:
|
||||
frappe.throw('An error occurred.')
|
||||
|
||||
delivery_note.submit()
|
||||
shipment = create_test_shipment([ delivery_note ])
|
||||
try:
|
||||
shipment.submit()
|
||||
except:
|
||||
frappe.throw('Error occurred on submit shipment')
|
||||
doc, rate, tracking_data = make_shipment_transaction(shipment)
|
||||
if doc and rate and tracking_data:
|
||||
self.assertEqual(doc.service_provider, rate.get('service_provider'))
|
||||
self.assertEqual(doc.shipment_amount, rate.get('actual_price'))
|
||||
self.assertEqual(doc.carrier, rate.get('carrier'))
|
||||
self.assertEqual(doc.tracking_status, tracking_data.get('tracking_status'))
|
||||
self.assertEqual(doc.tracking_url, tracking_data.get('tracking_url'))
|
||||
|
||||
|
||||
|
||||
def make_shipment_transaction(shipment):
|
||||
shipment_parcel = convert_shipmet_parcel(shipment.shipment_parcel)
|
||||
shipment_rates = fetch_shipping_rates(shipment.pickup_from_type, shipment.delivery_to_type,
|
||||
shipment.pickup_address_name, shipment.delivery_address_name,
|
||||
shipment_parcel, shipment.description_of_content,
|
||||
shipment.pickup_date, shipment.value_of_goods,
|
||||
pickup_contact_name=shipment.pickup_contact_name,
|
||||
delivery_contact_name=shipment.delivery_contact_name
|
||||
)
|
||||
if len(shipment_rates) > 0:
|
||||
# We are taking the first shipment rate
|
||||
rate = shipment_rates[0]
|
||||
new_shipment = create_shipment(
|
||||
shipment=shipment.name,
|
||||
pickup_from_type=shipment.pickup_from_type,
|
||||
delivery_to_type=shipment.delivery_to_type,
|
||||
pickup_address_name=shipment.pickup_address_name,
|
||||
delivery_address_name=shipment.delivery_address_name,
|
||||
shipment_parcel=shipment_parcel,
|
||||
description_of_content=shipment.description_of_content,
|
||||
pickup_date=shipment.pickup_date,
|
||||
pickup_contact_name=shipment.pickup_contact_name,
|
||||
delivery_contact_name=shipment.delivery_contact_name,
|
||||
value_of_goods=shipment.value_of_goods,
|
||||
service_data=json.dumps(rate),
|
||||
shipment_notific_email=None,
|
||||
tracking_notific_email=None,
|
||||
delivery_notes=None
|
||||
)
|
||||
service_provider = rate.get('service_provider')
|
||||
shipment_id = new_shipment.get('shipment_id')
|
||||
tracking_data = update_tracking(
|
||||
shipment.name,
|
||||
service_provider,
|
||||
shipment_id,
|
||||
delivery_notes=None
|
||||
)
|
||||
doc = frappe.get_doc('Shipment', shipment.name)
|
||||
return doc, rate, tracking_data
|
||||
return None, None, None
|
||||
shipment.submit()
|
||||
second_shipment = make_shipment(delivery_note.name)
|
||||
self.assertEqual(second_shipment.value_of_goods, delivery_note.grand_total)
|
||||
self.assertEqual(second_shipment.grand_total, delivery_note.grand_total)
|
||||
self.assertEqual(len(second_shipment.shipment_delivery_note), 1)
|
||||
self.assertEqual(second_shipment.shipment_delivery_note[0].delivery_note, delivery_note.name)
|
||||
|
||||
def create_test_delivery_note():
|
||||
company = get_shipment_company()
|
||||
@ -316,18 +246,3 @@ def create_shipment_item(item_name, company_name):
|
||||
except:
|
||||
frappe.throw('An error occurred.')
|
||||
return item
|
||||
|
||||
|
||||
def convert_shipmet_parcel(shipmet_parcel):
|
||||
data = []
|
||||
for parcel in shipmet_parcel:
|
||||
data.append(
|
||||
{
|
||||
"length": parcel.length,
|
||||
"width": parcel.width,
|
||||
"height": parcel.height,
|
||||
"weight": parcel.weight,
|
||||
"count": parcel.count
|
||||
}
|
||||
)
|
||||
return json.dumps(data)
|
||||
|
Loading…
x
Reference in New Issue
Block a user