@ -10,12 +10,12 @@ from frappe.utils import add_days, cint, cstr, flt, today
import erpnext
from erpnext . accounts . doctype . account . test_account import get_inventory_account
from erpnext . controllers . buying_controller import QtyMismatchError
from erpnext . stock . doctype . item . test_item import create_item , make_item
from erpnext . stock . doctype . purchase_receipt . purchase_receipt import make_purchase_invoice
from erpnext . stock . doctype . serial_no . serial_no import SerialNoDuplicateError , get_serial_nos
from erpnext . stock . doctype . warehouse . test_warehouse import create_warehouse
from erpnext . stock . stock_ledger import SerialNoExistsInFutureTransaction
from erpnext . controllers . buying_controller import QtyMismatchError
from erpnext . tests . utils import ERPNextTestCase
@ -28,8 +28,13 @@ class TestPurchaseReceipt(ERPNextTestCase):
1. Test if received qty is validated against accepted + rejected
2. Test if received qty is auto set on save
"""
pr = make_purchase_receipt ( qty = 1 , rejected_qty = 1 , received_qty = 3 ,
item_code = " _Test Item Home Desktop 200 " , do_not_save = True )
pr = make_purchase_receipt (
qty = 1 ,
rejected_qty = 1 ,
received_qty = 3 ,
item_code = " _Test Item Home Desktop 200 " ,
do_not_save = True
)
self . assertRaises ( QtyMismatchError , pr . save )
pr . items [ 0 ] . received_qty = 0
@ -43,16 +48,29 @@ class TestPurchaseReceipt(ERPNextTestCase):
pr = make_purchase_receipt ( qty = 0.5 , item_code = " _Test Item Home Desktop 200 " )
sl_entry = frappe . db . get_all ( " Stock Ledger Entry " , { " voucher_type " : " Purchase Receipt " ,
" voucher_no " : pr . name } , [ ' actual_qty ' ] )
sl_entry = frappe . db . get_all (
" Stock Ledger Entry " ,
{
" voucher_type " : " Purchase Receipt " ,
" voucher_no " : pr . name
} ,
[ ' actual_qty ' ]
)
self . assertEqual ( len ( sl_entry ) , 1 )
self . assertEqual ( sl_entry [ 0 ] . actual_qty , 0.5 )
pr . cancel ( )
sl_entry_cancelled = frappe . db . get_all ( " Stock Ledger Entry " , { " voucher_type " : " Purchase Receipt " ,
" voucher_no " : pr . name } , [ ' actual_qty ' ] , order_by = ' creation ' )
sl_entry_cancelled = frappe . db . get_all (
" Stock Ledger Entry " ,
{
" voucher_type " : " Purchase Receipt " ,
" voucher_no " : pr . name
} ,
[ ' actual_qty ' ] ,
order_by = ' creation '
)
self . assertEqual ( len ( sl_entry_cancelled ) , 2 )
self . assertEqual ( sl_entry_cancelled [ 1 ] . actual_qty , - 0.5 )
@ -78,8 +96,15 @@ class TestPurchaseReceipt(ERPNextTestCase):
} ]
} ) . insert ( )
template = frappe . db . get_value ( ' Payment Terms Template ' , ' _Test Payment Terms Template For Purchase Invoice ' )
old_template_in_supplier = frappe . db . get_value ( " Supplier " , " _Test Supplier " , " payment_terms " )
template = frappe . db . get_value (
" Payment Terms Template " ,
" _Test Payment Terms Template For Purchase Invoice "
)
old_template_in_supplier = frappe . db . get_value (
" Supplier " ,
" _Test Supplier " ,
" payment_terms "
)
frappe . db . set_value ( " Supplier " , " _Test Supplier " , " payment_terms " , template )
pr = make_purchase_receipt ( do_not_save = True )
@ -105,30 +130,59 @@ class TestPurchaseReceipt(ERPNextTestCase):
# teardown
pi . delete ( ) # draft PI
pr . cancel ( )
frappe . db . set_value ( " Supplier " , " _Test Supplier " , " payment_terms " , old_template_in_supplier )
frappe . get_doc ( ' Payment Terms Template ' , ' _Test Payment Terms Template For Purchase Invoice ' ) . delete ( )
frappe . db . set_value (
" Supplier " ,
" _Test Supplier " ,
" payment_terms " ,
old_template_in_supplier
)
frappe . get_doc (
" Payment Terms Template " ,
" _Test Payment Terms Template For Purchase Invoice "
) . delete ( )
def test_purchase_receipt_no_gl_entry ( self ) :
from erpnext . stock . doctype . stock_entry . test_stock_entry import make_stock_entry
company = frappe . db . get_value ( ' Warehouse ' , ' _Test Warehouse - _TC ' , ' company ' )
existing_bin_qty , existing_bin_stock_value = frappe . db . get_value ( " Bin " , { " item_code " : " _Test Item " ,
" warehouse " : " _Test Warehouse - _TC " } , [ " actual_qty " , " stock_value " ] )
existing_bin_qty , existing_bin_stock_value = frappe . db . get_value (
" Bin " ,
{
" item_code " : " _Test Item " ,
" warehouse " : " _Test Warehouse - _TC "
} ,
[ " actual_qty " , " stock_value " ]
)
if existing_bin_qty < 0 :
make_stock_entry ( item_code = " _Test Item " , target = " _Test Warehouse - _TC " , qty = abs ( existing_bin_qty ) )
make_stock_entry (
item_code = " _Test Item " ,
target = " _Test Warehouse - _TC " ,
qty = abs ( existing_bin_qty )
)
pr = make_purchase_receipt ( )
stock_value_difference = frappe . db . get_value ( " Stock Ledger Entry " ,
{ " voucher_type " : " Purchase Receipt " , " voucher_no " : pr . name ,
" item_code " : " _Test Item " , " warehouse " : " _Test Warehouse - _TC " } , " stock_value_difference " )
stock_value_difference = frappe . db . get_value (
" Stock Ledger Entry " ,
{
" voucher_type " : " Purchase Receipt " ,
" voucher_no " : pr . name ,
" item_code " : " _Test Item " ,
" warehouse " : " _Test Warehouse - _TC "
} ,
" stock_value_difference "
)
self . assertEqual ( stock_value_difference , 250 )
current_bin_stock_value = frappe . db . get_value ( " Bin " , { " item_code " : " _Test Item " ,
" warehouse " : " _Test Warehouse - _TC " } , " stock_value " )
current_bin_stock_value = frappe . db . get_value (
" Bin " ,
{
" item_code " : " _Test Item " ,
" warehouse " : " _Test Warehouse - _TC "
} ,
" stock_value "
)
self . assertEqual ( current_bin_stock_value , existing_bin_stock_value + 250 )
self . assertFalse ( get_gl_entries ( " Purchase Receipt " , pr . name ) )
@ -150,13 +204,17 @@ class TestPurchaseReceipt(ERPNextTestCase):
pr = make_purchase_receipt ( item_code = item . name , qty = 5 , rate = 500 )
self . assertTrue ( frappe . db . get_value ( ' Batch ' , { ' item ' : item . name , ' reference_name ' : pr . name } ) )
self . assertTrue (
frappe . db . get_value ( ' Batch ' , { ' item ' : item . name , ' reference_name ' : pr . name } )
)
pr . load_from_db ( )
batch_no = pr . items [ 0 ] . batch_no
pr . cancel ( )
self . assertFalse ( frappe . db . get_value ( ' Batch ' , { ' item ' : item . name , ' reference_name ' : pr . name } ) )
self . assertFalse (
frappe . db . get_value ( ' Batch ' , { ' item ' : item . name , ' reference_name ' : pr . name } )
)
self . assertFalse ( frappe . db . get_all ( ' Serial No ' , { ' batch_no ' : batch_no } ) )
def test_duplicate_serial_nos ( self ) :
@ -175,42 +233,78 @@ class TestPurchaseReceipt(ERPNextTestCase):
pr = make_purchase_receipt ( item_code = item . name , qty = 2 , rate = 500 )
pr . load_from_db ( )
serial_nos = frappe . db . get_value ( ' Stock Ledger Entry ' ,
{ ' voucher_type ' : ' Purchase Receipt ' , ' voucher_no ' : pr . name , ' item_code ' : item . name } , ' serial_no ' )
serial_nos = frappe . db . get_value (
" Stock Ledger Entry " ,
{
" voucher_type " : " Purchase Receipt " ,
" voucher_no " : pr . name ,
" item_code " : item . name
} ,
" serial_no "
)
serial_nos = get_serial_nos ( serial_nos )
self . assertEquals ( get_serial_nos ( pr . items [ 0 ] . serial_no ) , serial_nos )
# Then tried to receive same serial nos in difference company
pr_different_company = make_purchase_receipt ( item_code = item . name , qty = 2 , rate = 500 ,
serial_no = ' \n ' . join ( serial_nos ) , company = ' _Test Company 1 ' , do_not_submit = True ,
warehouse = ' Stores - _TC1 ' )
pr_different_company = make_purchase_receipt (
item_code = item . name ,
qty = 2 ,
rate = 500 ,
serial_no = ' \n ' . join ( serial_nos ) ,
company = ' _Test Company 1 ' ,
do_not_submit = True ,
warehouse = ' Stores - _TC1 '
)
self . assertRaises ( SerialNoDuplicateError , pr_different_company . submit )
# Then made delivery note to remove the serial nos from stock
dn = create_delivery_note ( item_code = item . name , qty = 2 , rate = 1500 , serial_no = ' \n ' . join ( serial_nos ) )
dn = create_delivery_note (
item_code = item . name ,
qty = 2 ,
rate = 1500 ,
serial_no = ' \n ' . join ( serial_nos )
)
dn . load_from_db ( )
self . assertEquals ( get_serial_nos ( dn . items [ 0 ] . serial_no ) , serial_nos )
posting_date = add_days ( today ( ) , - 3 )
# Try to receive same serial nos again in the same company with backdated.
pr1 = make_purchase_receipt ( item_code = item . name , qty = 2 , rate = 500 ,
posting_date = posting_date , serial_no = ' \n ' . join ( serial_nos ) , do_not_submit = True )
pr1 = make_purchase_receipt (
item_code = item . name ,
qty = 2 ,
rate = 500 ,
posting_date = posting_date ,
serial_no = ' \n ' . join ( serial_nos ) ,
do_not_submit = True
)
self . assertRaises ( SerialNoExistsInFutureTransaction , pr1 . submit )
# Try to receive same serial nos with different company with backdated.
pr2 = make_purchase_receipt ( item_code = item . name , qty = 2 , rate = 500 ,
posting_date = posting_date , serial_no = ' \n ' . join ( serial_nos ) , company = ' _Test Company 1 ' , do_not_submit = True ,
warehouse = ' Stores - _TC1 ' )
pr2 = make_purchase_receipt (
item_code = item . name ,
qty = 2 ,
rate = 500 ,
posting_date = posting_date ,
serial_no = ' \n ' . join ( serial_nos ) ,
company = " _Test Company 1 " ,
do_not_submit = True ,
warehouse = " Stores - _TC1 "
)
self . assertRaises ( SerialNoExistsInFutureTransaction , pr2 . submit )
# Receive the same serial nos after the delivery note posting date and time
make_purchase_receipt ( item_code = item . name , qty = 2 , rate = 500 , serial_no = ' \n ' . join ( serial_nos ) )
make_purchase_receipt (
item_code = item . name ,
qty = 2 ,
rate = 500 ,
serial_no = ' \n ' . join ( serial_nos )
)
# Raise the error for backdated deliver note entry cancel
self . assertRaises ( SerialNoExistsInFutureTransaction , dn . cancel )
@ -253,11 +347,23 @@ class TestPurchaseReceipt(ERPNextTestCase):
def test_subcontracting ( self ) :
from erpnext . stock . doctype . stock_entry . test_stock_entry import make_stock_entry
frappe . db . set_value ( " Buying Settings " , None , " backflush_raw_materials_of_subcontract_based_on " , " BOM " )
make_stock_entry ( item_code = " _Test Item " , target = " _Test Warehouse 1 - _TC " , qty = 100 , basic_rate = 100 )
make_stock_entry ( item_code = " _Test Item Home Desktop 100 " , target = " _Test Warehouse 1 - _TC " ,
qty = 100 , basic_rate = 100 )
pr = make_purchase_receipt ( item_code = " _Test FG Item " , qty = 10 , rate = 500 , is_subcontracted = " Yes " )
frappe . db . set_value (
" Buying Settings " , None ,
" backflush_raw_materials_of_subcontract_based_on " , " BOM "
)
make_stock_entry (
item_code = " _Test Item " , qty = 100 ,
target = " _Test Warehouse 1 - _TC " , basic_rate = 100
)
make_stock_entry (
item_code = " _Test Item Home Desktop 100 " , qty = 100 ,
target = " _Test Warehouse 1 - _TC " , basic_rate = 100
)
pr = make_purchase_receipt (
item_code = " _Test FG Item " , qty = 10 ,
rate = 500 , is_subcontracted = " Yes "
)
self . assertEqual ( len ( pr . get ( " supplied_items " ) ) , 2 )
rm_supp_cost = sum ( d . amount for d in pr . get ( " supplied_items " ) )
@ -267,17 +373,33 @@ class TestPurchaseReceipt(ERPNextTestCase):
def test_subcontracting_gle_fg_item_rate_zero ( self ) :
from erpnext . stock . doctype . stock_entry . test_stock_entry import make_stock_entry
frappe . db . set_value ( " Buying Settings " , None , " backflush_raw_materials_of_subcontract_based_on " , " BOM " )
frappe . db . set_value (
" Buying Settings " , None ,
" backflush_raw_materials_of_subcontract_based_on " , " BOM "
)
se1 = make_stock_entry ( item_code = " _Test Item " , target = " Work In Progress - TCP1 " ,
qty = 100 , basic_rate = 100 , company = " _Test Company with perpetual inventory " )
se1 = make_stock_entry (
item_code = " _Test Item " ,
target = " Work In Progress - TCP1 " ,
qty = 100 , basic_rate = 100 ,
company = " _Test Company with perpetual inventory "
)
se2 = make_stock_entry ( item_code = " _Test Item Home Desktop 100 " , target = " Work In Progress - TCP1 " ,
qty = 100 , basic_rate = 100 , company = " _Test Company with perpetual inventory " )
se2 = make_stock_entry (
item_code = " _Test Item Home Desktop 100 " ,
target = " Work In Progress - TCP1 " ,
qty = 100 , basic_rate = 100 ,
company = " _Test Company with perpetual inventory "
)
pr = make_purchase_receipt ( item_code = " _Test FG Item " , qty = 10 , rate = 0 , is_subcontracted = " Yes " ,
company = " _Test Company with perpetual inventory " , warehouse = ' Stores - TCP1 ' ,
supplier_warehouse = ' Work In Progress - TCP1 ' )
pr = make_purchase_receipt (
item_code = " _Test FG Item " ,
qty = 10 , rate = 0 ,
is_subcontracted = " Yes " ,
company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " ,
supplier_warehouse = " Work In Progress - TCP1 "
)
gl_entries = get_gl_entries ( " Purchase Receipt " , pr . name )
@ -311,13 +433,23 @@ class TestPurchaseReceipt(ERPNextTestCase):
po = create_purchase_order ( item_code = item_code , qty = 1 , include_exploded_items = 0 ,
is_subcontracted = " Yes " , supplier_warehouse = " _Test Warehouse 1 - _TC " )
#stock raw materials in a warehouse before transfer
se1 = make_stock_entry ( target = " _Test Warehouse - _TC " ,
item_code = " Test Extra Item 1 " , qty = 10 , basic_rate = 100 )
se2 = make_stock_entry ( target = " _Test Warehouse - _TC " ,
item_code = " _Test FG Item " , qty = 1 , basic_rate = 100 )
se3 = make_stock_entry ( target = " _Test Warehouse - _TC " ,
item_code = " Test Extra Item 2 " , qty = 1 , basic_rate = 100 )
# stock raw materials in a warehouse before transfer
make_stock_entry (
target = " _Test Warehouse - _TC " ,
item_code = " Test Extra Item 1 " ,
qty = 10 , basic_rate = 100
)
make_stock_entry (
target = " _Test Warehouse - _TC " ,
item_code = " _Test FG Item " ,
qty = 1 , basic_rate = 100
)
make_stock_entry (
target = " _Test Warehouse - _TC " ,
item_code = " Test Extra Item 2 " ,
qty = 1 , basic_rate = 100
)
rm_items = [
{
" item_code " : item_code ,
@ -351,11 +483,17 @@ class TestPurchaseReceipt(ERPNextTestCase):
def test_serial_no_supplier ( self ) :
pr = make_purchase_receipt ( item_code = " _Test Serialized Item With Series " , qty = 1 )
self . assertEqual ( frappe . db . get_value ( " Serial No " , pr . get ( " items " ) [ 0 ] . serial_no , " supplier " ) ,
pr . supplier )
pr_row_1_serial_no = pr . get ( " items " ) [ 0 ] . serial_no
self . assertEqual (
frappe . db . get_value ( " Serial No " , pr_row_1_serial_no , " supplier " ) ,
pr . supplier
)
pr . cancel ( )
self . assertFalse ( frappe . db . get_value ( " Serial No " , pr . get ( " items " ) [ 0 ] . serial_no , " warehouse " ) )
self . assertFalse (
frappe . db . get_value ( " Serial No " , pr_row_1_serial_no , " warehouse " )
)
def test_rejected_serial_no ( self ) :
pr = frappe . copy_doc ( test_records [ 0 ] )
@ -382,18 +520,33 @@ class TestPurchaseReceipt(ERPNextTestCase):
pr . cancel ( )
def test_purchase_return_partial ( self ) :
pr = make_purchase_receipt ( company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " , supplier_warehouse = " Work in Progress - TCP1 " )
pr = make_purchase_receipt (
company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " ,
supplier_warehouse = " Work in Progress - TCP1 "
)
return_pr = make_purchase_receipt ( company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " , supplier_warehouse = " Work in Progress - TCP1 " ,
is_return = 1 , return_against = pr . name , qty = - 2 , do_not_submit = 1 )
return_pr = make_purchase_receipt (
company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " ,
supplier_warehouse = " Work in Progress - TCP1 " ,
is_return = 1 ,
return_against = pr . name ,
qty = - 2 ,
do_not_submit = 1
)
return_pr . items [ 0 ] . purchase_receipt_item = pr . items [ 0 ] . name
return_pr . submit ( )
# check sle
outgoing_rate = frappe . db . get_value ( " Stock Ledger Entry " , { " voucher_type " : " Purchase Receipt " ,
" voucher_no " : return_pr . name } , " outgoing_rate " )
outgoing_rate = frappe . db . get_value (
" Stock Ledger Entry " ,
{
" voucher_type " : " Purchase Receipt " ,
" voucher_no " : return_pr . name
} ,
" outgoing_rate "
)
self . assertEqual ( outgoing_rate , 50 )
@ -457,11 +610,21 @@ class TestPurchaseReceipt(ERPNextTestCase):
pr . cancel ( )
def test_purchase_return_full ( self ) :
pr = make_purchase_receipt ( company = " _Test Company with perpetual inventory " , warehouse = " Stores - TCP1 " ,
supplier_warehouse = " Work in Progress - TCP1 " )
pr = make_purchase_receipt (
company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " ,
supplier_warehouse = " Work in Progress - TCP1 "
)
return_pr = make_purchase_receipt ( company = " _Test Company with perpetual inventory " , warehouse = " Stores - TCP1 " ,
supplier_warehouse = " Work in Progress - TCP1 " , is_return = 1 , return_against = pr . name , qty = - 5 , do_not_submit = 1 )
return_pr = make_purchase_receipt (
company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " ,
supplier_warehouse = " Work in Progress - TCP1 " ,
is_return = 1 ,
return_against = pr . name ,
qty = - 5 ,
do_not_submit = 1
)
return_pr . items [ 0 ] . purchase_receipt_item = pr . items [ 0 ] . name
return_pr . submit ( )
@ -483,15 +646,41 @@ class TestPurchaseReceipt(ERPNextTestCase):
rejected_warehouse = " _Test Rejected Warehouse - TCP1 "
if not frappe . db . exists ( " Warehouse " , rejected_warehouse ) :
get_warehouse ( company = " _Test Company with perpetual inventory " ,
abbr = " - TCP1 " , warehouse_name = " _Test Rejected Warehouse " ) . name
get_warehouse (
company = " _Test Company with perpetual inventory " ,
abbr = " - TCP1 " ,
warehouse_name = " _Test Rejected Warehouse "
) . name
pr = make_purchase_receipt ( company = " _Test Company with perpetual inventory " , warehouse = " Stores - TCP1 " , supplier_warehouse = " Work in Progress - TCP1 " , received_qty = 4 , qty = 2 , rejected_warehouse = rejected_warehouse )
pr = make_purchase_receipt (
company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " ,
supplier_warehouse = " Work in Progress - TCP1 " ,
qty = 2 ,
rejected_qty = 2 ,
rejected_warehouse = rejected_warehouse
)
return_pr = make_purchase_receipt ( company = " _Test Company with perpetual inventory " , warehouse = " Stores - TCP1 " , supplier_warehouse = " Work in Progress - TCP1 " , is_return = 1 , return_against = pr . name , received_qty = - 4 , qty = - 2 , rejected_warehouse = rejected_warehouse )
return_pr = make_purchase_receipt (
company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " ,
supplier_warehouse = " Work in Progress - TCP1 " ,
is_return = 1 ,
return_against = pr . name ,
qty = - 2 ,
rejected_qty = - 2 ,
rejected_warehouse = rejected_warehouse
)
actual_qty = frappe . db . get_value ( " Stock Ledger Entry " , { " voucher_type " : " Purchase Receipt " ,
" voucher_no " : return_pr . name , ' warehouse ' : return_pr . items [ 0 ] . rejected_warehouse } , " actual_qty " )
actual_qty = frappe . db . get_value (
" Stock Ledger Entry " ,
{
" voucher_type " : " Purchase Receipt " ,
" voucher_no " : return_pr . name ,
" warehouse " : return_pr . items [ 0 ] . rejected_warehouse
} ,
" actual_qty "
)
self . assertEqual ( actual_qty , - 2 )
@ -516,8 +705,13 @@ class TestPurchaseReceipt(ERPNextTestCase):
" purchase_document_no " : pr . name
} )
return_pr = make_purchase_receipt ( item_code = " _Test Serialized Item With Series " , qty = - 1 ,
is_return = 1 , return_against = pr . name , serial_no = serial_no )
return_pr = make_purchase_receipt (
item_code = " _Test Serialized Item With Series " ,
qty = - 1 ,
is_return = 1 ,
return_against = pr . name ,
serial_no = serial_no
)
_check_serial_no_values ( serial_no , {
" warehouse " : " " ,
@ -539,9 +733,21 @@ class TestPurchaseReceipt(ERPNextTestCase):
} )
row . db_update ( )
pr = make_purchase_receipt ( item_code = item_code , qty = 1 , uom = " Box " , conversion_factor = 1.0 )
return_pr = make_purchase_receipt ( item_code = item_code , qty = - 10 , uom = " Unit " ,
stock_uom = " Box " , conversion_factor = 0.1 , is_return = 1 , return_against = pr . name )
pr = make_purchase_receipt (
item_code = item_code ,
qty = 1 ,
uom = " Box " ,
conversion_factor = 1.0
)
return_pr = make_purchase_receipt (
item_code = item_code ,
qty = - 10 ,
uom = " Unit " ,
stock_uom = " Box " ,
conversion_factor = 0.1 ,
is_return = 1 ,
return_against = pr . name
)
self . assertEqual ( abs ( return_pr . items [ 0 ] . stock_qty ) , 1.0 )
@ -557,13 +763,19 @@ class TestPurchaseReceipt(ERPNextTestCase):
pr . submit ( )
update_purchase_receipt_status ( pr . name , " Closed " )
self . assertEqual ( frappe . db . get_value ( " Purchase Receipt " , pr . name , " status " ) , " Closed " )
self . assertEqual (
frappe . db . get_value ( " Purchase Receipt " , pr . name , " status " ) , " Closed "
)
pr . reload ( )
pr . cancel ( )
def test_pr_billing_status ( self ) :
# PO -> PR1 -> PI and PO -> PI and PO -> PR2
""" Flow:
1. PO - > PR1 - > PI
2. PO - > PI
3. PO - > PR2 .
"""
from erpnext . buying . doctype . purchase_order . purchase_order import (
make_purchase_invoice as make_purchase_invoice_from_po ,
)
@ -627,21 +839,39 @@ class TestPurchaseReceipt(ERPNextTestCase):
pr_doc = make_purchase_receipt ( item_code = item_code ,
qty = 1 , serial_no = serial_no )
self . assertEqual ( serial_no , frappe . db . get_value ( " Serial No " ,
{ " purchase_document_type " : " Purchase Receipt " , " purchase_document_no " : pr_doc . name } , " name " ) )
self . assertEqual (
serial_no ,
frappe . db . get_value (
" Serial No " ,
{
" purchase_document_type " : " Purchase Receipt " ,
" purchase_document_no " : pr_doc . name
} ,
" name "
)
)
pr_doc . cancel ( )
#check for the auto created serial nos
# check for the auto created serial nos
item_code = " Test Auto Created Serial No "
if not frappe . db . exists ( " Item " , item_code ) :
item = make_item ( item_code , dict ( has_serial_no = 1 , serial_no_series = " KLJL.### " ) )
make_item ( item_code , dict ( has_serial_no = 1 , serial_no_series = " KLJL.### " ) )
new_pr_doc = make_purchase_receipt ( item_code = item_code , qty = 1 )
serial_no = get_serial_nos ( new_pr_doc . items [ 0 ] . serial_no ) [ 0 ]
self . assertEqual ( serial_no , frappe . db . get_value ( " Serial No " ,
{ " purchase_document_type " : " Purchase Receipt " , " purchase_document_no " : new_pr_doc . name } , " name " ) )
self . assertEqual (
serial_no ,
frappe . db . get_value (
" Serial No " ,
{
" purchase_document_type " : " Purchase Receipt " ,
" purchase_document_no " : new_pr_doc . name
} ,
" name "
)
)
new_pr_doc . cancel ( )
@ -717,8 +947,12 @@ class TestPurchaseReceipt(ERPNextTestCase):
def test_purchase_receipt_cost_center ( self ) :
from erpnext . accounts . doctype . cost_center . test_cost_center import create_cost_center
cost_center = " _Test Cost Center for BS Account - TCP1 "
create_cost_center ( cost_center_name = " _Test Cost Center for BS Account " , company = " _Test Company with perpetual inventory " )
create_cost_center (
cost_center_name = " _Test Cost Center for BS Account " ,
company = " _Test Company with perpetual inventory "
)
if not frappe . db . exists ( ' Location ' , ' Test Location ' ) :
frappe . get_doc ( {
@ -726,10 +960,16 @@ class TestPurchaseReceipt(ERPNextTestCase):
' location_name ' : ' Test Location '
} ) . insert ( )
pr = make_purchase_receipt ( cost_center = cost_center , company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " , supplier_warehouse = " Work in Progress - TCP1 " )
pr = make_purchase_receipt (
cost_center = cost_center ,
company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " ,
supplier_warehouse = " Work in Progress - TCP1 "
)
stock_in_hand_account = get_inventory_account ( pr . company , pr . get ( " items " ) [ 0 ] . warehouse )
stock_in_hand_account = get_inventory_account (
pr . company , pr . get ( " items " ) [ 0 ] . warehouse
)
gl_entries = get_gl_entries ( " Purchase Receipt " , pr . name )
self . assertTrue ( gl_entries )
@ -753,9 +993,16 @@ class TestPurchaseReceipt(ERPNextTestCase):
' doctype ' : ' Location ' ,
' location_name ' : ' Test Location '
} ) . insert ( )
pr = make_purchase_receipt ( company = " _Test Company with perpetual inventory " , warehouse = " Stores - TCP1 " , supplier_warehouse = " Work in Progress - TCP1 " )
stock_in_hand_account = get_inventory_account ( pr . company , pr . get ( " items " ) [ 0 ] . warehouse )
pr = make_purchase_receipt (
company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " ,
supplier_warehouse = " Work in Progress - TCP1 "
)
stock_in_hand_account = get_inventory_account (
pr . company , pr . get ( " items " ) [ 0 ] . warehouse
)
gl_entries = get_gl_entries ( " Purchase Receipt " , pr . name )
self . assertTrue ( gl_entries )
@ -783,7 +1030,11 @@ class TestPurchaseReceipt(ERPNextTestCase):
po = create_purchase_order ( )
pr = create_pr_against_po ( po . name )
pr1 = make_purchase_receipt ( is_return = 1 , return_against = pr . name , qty = - 1 , do_not_submit = True )
pr1 = make_purchase_receipt (
qty = - 1 ,
is_return = 1 , return_against = pr . name ,
do_not_submit = True
)
pr1 . items [ 0 ] . purchase_order = po . name
pr1 . items [ 0 ] . purchase_order_item = po . items [ 0 ] . name
pr1 . items [ 0 ] . purchase_receipt_item = pr . items [ 0 ] . name
@ -816,7 +1067,11 @@ class TestPurchaseReceipt(ERPNextTestCase):
pi1 . save ( )
pi1 . submit ( )
pr2 = make_purchase_receipt ( is_return = 1 , return_against = pr1 . name , qty = - 2 , do_not_submit = True )
pr2 = make_purchase_receipt (
qty = - 2 ,
is_return = 1 , return_against = pr1 . name ,
do_not_submit = True
)
pr2 . items [ 0 ] . purchase_receipt_item = pr1 . items [ 0 ] . name
pr2 . submit ( )
@ -858,14 +1113,22 @@ class TestPurchaseReceipt(ERPNextTestCase):
pr1 . cancel ( )
def test_stock_transfer_from_purchase_receipt_with_valuation ( self ) :
create_warehouse ( " _Test Warehouse for Valuation " , company = " _Test Company with perpetual inventory " ,
properties = { " account " : ' _Test Account Stock In Hand - TCP1 ' } )
create_warehouse (
" _Test Warehouse for Valuation " ,
company = " _Test Company with perpetual inventory " ,
properties = { " account " : ' _Test Account Stock In Hand - TCP1 ' }
)
pr1 = make_purchase_receipt ( warehouse = ' _Test Warehouse for Valuation - TCP1 ' ,
company = " _Test Company with perpetual inventory " )
pr1 = make_purchase_receipt (
warehouse = ' _Test Warehouse for Valuation - TCP1 ' ,
company = " _Test Company with perpetual inventory "
)
pr = make_purchase_receipt ( company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " , do_not_save = 1 )
pr = make_purchase_receipt (
company = " _Test Company with perpetual inventory " ,
warehouse = " Stores - TCP1 " ,
do_not_save = 1
)
pr . items [ 0 ] . from_warehouse = ' _Test Warehouse for Valuation - TCP1 '
pr . supplier_warehouse = ' '
@ -947,10 +1210,24 @@ class TestPurchaseReceipt(ERPNextTestCase):
}
rm_items = [
{ " item_code " : item_code , " rm_item_code " : " Sub Contracted Raw Material 3 " , " item_name " : " _Test Item " ,
" qty " : 300 , " warehouse " : " _Test Warehouse - _TC " , " stock_uom " : " Nos " , " name " : po . supplied_items [ 0 ] . name } ,
{ " item_code " : item_code , " rm_item_code " : " Sub Contracted Raw Material 3 " , " item_name " : " _Test Item " ,
" qty " : 200 , " warehouse " : " _Test Warehouse - _TC " , " stock_uom " : " Nos " , " name " : po . supplied_items [ 0 ] . name }
{
" item_code " : item_code ,
" rm_item_code " : " Sub Contracted Raw Material 3 " ,
" item_name " : " _Test Item " ,
" qty " : 300 ,
" warehouse " : " _Test Warehouse - _TC " ,
" stock_uom " : " Nos " ,
" name " : po . supplied_items [ 0 ] . name
} ,
{
" item_code " : item_code ,
" rm_item_code " : " Sub Contracted Raw Material 3 " ,
" item_name " : " _Test Item " ,
" qty " : 200 ,
" warehouse " : " _Test Warehouse - _TC " ,
" stock_uom " : " Nos " ,
" name " : po . supplied_items [ 0 ] . name
}
]
rm_item_string = json . dumps ( rm_items )
@ -960,8 +1237,14 @@ class TestPurchaseReceipt(ERPNextTestCase):
se . items [ 1 ] . batch_no = ste2 . items [ 0 ] . batch_no
se . submit ( )
supplied_qty = frappe . db . get_value ( " Purchase Order Item Supplied " ,
{ " parent " : po . name , " rm_item_code " : " Sub Contracted Raw Material 3 " } , " supplied_qty " )
supplied_qty = frappe . db . get_value (
" Purchase Order Item Supplied " ,
{
" parent " : po . name ,
" rm_item_code " : " Sub Contracted Raw Material 3 "
} ,
" supplied_qty "
)
self . assertEqual ( supplied_qty , 500.00 )
@ -1033,10 +1316,18 @@ class TestPurchaseReceipt(ERPNextTestCase):
company = ' _Test Company with perpetual inventory '
service_item = ' _Test Non Stock Item '
before_test_value = frappe . db . get_value ( ' Company ' , company , ' enable_perpetual_inventory_for_non_stock_items ' )
frappe . db . set_value ( ' Company ' , company , ' enable_perpetual_inventory_for_non_stock_items ' , 1 )
before_test_value = frappe . db . get_value (
' Company ' , company , ' enable_perpetual_inventory_for_non_stock_items '
)
frappe . db . set_value (
' Company ' , company ,
' enable_perpetual_inventory_for_non_stock_items ' , 1
)
srbnb_account = ' Stock Received But Not Billed - TCP1 '
frappe . db . set_value ( ' Company ' , company , ' service_received_but_not_billed ' , srbnb_account )
frappe . db . set_value (
' Company ' , company ,
' service_received_but_not_billed ' , srbnb_account
)
pr = make_purchase_receipt (
company = company , item = service_item ,
@ -1068,7 +1359,10 @@ class TestPurchaseReceipt(ERPNextTestCase):
self . assertEqual ( len ( item_one_gl_entry ) , 1 )
self . assertEqual ( len ( item_two_gl_entry ) , 1 )
frappe . db . set_value ( ' Company ' , company , ' enable_perpetual_inventory_for_non_stock_items ' , before_test_value )
frappe . db . set_value (
' Company ' , company ,
' enable_perpetual_inventory_for_non_stock_items ' , before_test_value
)
def test_purchase_receipt_with_exchange_rate_difference ( self ) :
from erpnext . accounts . doctype . purchase_invoice . purchase_invoice import (
@ -1093,10 +1387,19 @@ class TestPurchaseReceipt(ERPNextTestCase):
pr . submit ( )
# Get exchnage gain and loss account
exchange_gain_loss_account = frappe . db . get_value ( ' Company ' , pr . company , ' exchange_gain_loss_account ' )
exchange_gain_loss_account = frappe . db . get_value (
' Company ' , pr . company , ' exchange_gain_loss_account '
)
# fetching the latest GL Entry with exchange gain and loss account account
amount = frappe . db . get_value ( ' GL Entry ' , { ' account ' : exchange_gain_loss_account , ' voucher_no ' : pr . name } , ' credit ' )
amount = frappe . db . get_value (
' GL Entry ' ,
{
' account ' : exchange_gain_loss_account ,
' voucher_no ' : pr . name
} ,
' credit '
)
discrepancy_caused_by_exchange_rate_diff = abs ( pi . items [ 0 ] . base_net_amount - pr . items [ 0 ] . base_net_amount )
self . assertEqual ( discrepancy_caused_by_exchange_rate_diff , amount )
@ -1266,9 +1569,12 @@ def make_purchase_receipt(**args):
if args . get_multiple_items :
pr . items = [ ]
for item in get_items ( warehouse = args . warehouse , cost_center = args . cost_center or frappe . get_cached_value ( ' Company ' , pr . company , ' cost_center ' ) ) :
pr . append ( " items " , item )
company_cost_center = frappe . get_cached_value ( ' Company ' , pr . company , ' cost_center ' )
cost_center = args . cost_center or company_cost_center
for item in get_items ( warehouse = args . warehouse , cost_center = cost_center ) :
pr . append ( " items " , item )
if args . get_taxes_and_charges :
for tax in get_taxes ( ) :