# -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. import re from datetime import datetime, timedelta from odoo import Command from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT from odoo.addons.stock_account.tests.test_anglo_saxon_valuation_reconciliation_common import ValuationReconciliationTestCommon from odoo.exceptions import UserError from odoo.tests import Form, tagged, freeze_time @freeze_time("2021-01-14 09:12:15") @tagged('post_install', '-at_install') class TestPurchaseOrder(ValuationReconciliationTestCommon): @classmethod def setUpClass(cls): super().setUpClass() cls.product_id_1 = cls.env['product.product'].create({'name': 'Large Desk', 'purchase_method': 'purchase'}) cls.product_id_2 = cls.env['product.product'].create({'name': 'Conference Chair', 'purchase_method': 'purchase'}) cls.po_vals = { 'partner_id': cls.partner_a.id, 'order_line': [ (0, 0, { 'name': cls.product_id_1.name, 'product_id': cls.product_id_1.id, 'product_qty': 5.0, 'product_uom': cls.product_id_1.uom_po_id.id, 'price_unit': 500.0, 'date_planned': datetime.today().replace(hour=9).strftime(DEFAULT_SERVER_DATETIME_FORMAT), }), (0, 0, { 'name': cls.product_id_2.name, 'product_id': cls.product_id_2.id, 'product_qty': 5.0, 'product_uom': cls.product_id_2.uom_po_id.id, 'price_unit': 250.0, 'date_planned': datetime.today().replace(hour=9).strftime(DEFAULT_SERVER_DATETIME_FORMAT), })], } def test_00_purchase_order_flow(self): # Ensure product_id_2 doesn't have res_partner_1 as supplier if self.partner_a in self.product_id_2.seller_ids.partner_id: id_to_remove = self.product_id_2.seller_ids.filtered(lambda r: r.partner_id == self.partner_a).ids[0] if self.product_id_2.seller_ids.filtered(lambda r: r.partner_id == self.partner_a) else False if id_to_remove: self.product_id_2.write({ 'seller_ids': [(2, id_to_remove, False)], }) self.assertFalse(self.product_id_2.seller_ids.filtered(lambda r: r.partner_id == self.partner_a), 'Purchase: the partner should not be in the list of the product suppliers') self.po = self.env['purchase.order'].create(self.po_vals) self.assertTrue(self.po, 'Purchase: no purchase order created') self.assertEqual(self.po.invoice_status, 'no', 'Purchase: PO invoice_status should be "Not purchased"') self.assertEqual(self.po.order_line.mapped('qty_received'), [0.0, 0.0], 'Purchase: no product should be received"') self.assertEqual(self.po.order_line.mapped('qty_invoiced'), [0.0, 0.0], 'Purchase: no product should be invoiced"') self.po.button_confirm() self.assertEqual(self.po.state, 'purchase', 'Purchase: PO state should be "Purchase"') self.assertEqual(self.po.invoice_status, 'to invoice', 'Purchase: PO invoice_status should be "Waiting Invoices"') self.assertTrue(self.product_id_2.seller_ids.filtered(lambda r: r.partner_id == self.partner_a), 'Purchase: the partner should be in the list of the product suppliers') seller = self.product_id_2._select_seller(partner_id=self.partner_a, quantity=2.0, date=self.po.date_planned, uom_id=self.product_id_2.uom_po_id) price_unit = seller.price if seller else 0.0 if price_unit and seller and self.po.currency_id and seller.currency_id != self.po.currency_id: price_unit = seller.currency_id._convert(price_unit, self.po.currency_id, self.po.company_id, self.po.date_order) self.assertEqual(price_unit, 250.0, 'Purchase: the price of the product for the supplier should be 250.0.') self.assertEqual(self.po.incoming_picking_count, 1, 'Purchase: one picking should be created"') self.picking = self.po.picking_ids[0] self.picking.move_line_ids.write({'quantity': 5.0}) self.picking.move_ids.picked = True self.picking.button_validate() self.assertEqual(self.po.order_line.mapped('qty_received'), [5.0, 5.0], 'Purchase: all products should be received"') move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice')) move_form.partner_id = self.partner_a move_form.purchase_vendor_bill_id = self.env['purchase.bill.union'].browse(-self.po.id) self.invoice = move_form.save() self.assertEqual(self.po.order_line.mapped('qty_invoiced'), [5.0, 5.0], 'Purchase: all products should be invoiced"') def test_02_po_return(self): """ Test a PO with a product on Incoming shipment. Validate the PO, then do a return of the picking with Refund. """ # Draft purchase order created self.po = self.env['purchase.order'].create(self.po_vals) self.assertTrue(self.po, 'Purchase: no purchase order created') self.assertEqual(self.po.order_line.mapped('qty_received'), [0.0, 0.0], 'Purchase: no product should be received"') self.assertEqual(self.po.order_line.mapped('qty_invoiced'), [0.0, 0.0], 'Purchase: no product should be invoiced"') self.po.button_confirm() self.assertEqual(self.po.state, 'purchase', 'Purchase: PO state should be "Purchase"') self.assertEqual(self.po.invoice_status, 'to invoice', 'Purchase: PO invoice_status should be "Waiting Invoices"') # Confirm the purchase order self.po.button_confirm() self.assertEqual(self.po.state, 'purchase', 'Purchase: PO state should be "Purchase') self.assertEqual(self.po.incoming_picking_count, 1, 'Purchase: one picking should be created"') self.picking = self.po.picking_ids[0] self.picking.move_line_ids.write({'quantity': 5.0}) self.picking.move_ids.picked = True self.picking.button_validate() self.assertEqual(self.po.order_line.mapped('qty_received'), [5.0, 5.0], 'Purchase: all products should be received"') #After Receiving all products create vendor bill. move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice')) move_form.invoice_date = move_form.date move_form.partner_id = self.partner_a move_form.purchase_vendor_bill_id = self.env['purchase.bill.union'].browse(-self.po.id) self.invoice = move_form.save() self.invoice.action_post() self.assertEqual(self.po.order_line.mapped('qty_invoiced'), [5.0, 5.0], 'Purchase: all products should be invoiced"') # Check quantity received received_qty = sum(pol.qty_received for pol in self.po.order_line) self.assertEqual(received_qty, 10.0, 'Purchase: Received quantity should be 10.0 instead of %s after validating incoming shipment' % received_qty) # Create return picking pick = self.po.picking_ids stock_return_picking_form = Form(self.env['stock.return.picking'] .with_context(active_ids=pick.ids, active_id=pick.ids[0], active_model='stock.picking')) return_wiz = stock_return_picking_form.save() return_wiz.product_return_moves.write({'quantity': 2.0, 'to_refund': True}) # Return only 2 res = return_wiz.action_create_returns() return_pick = self.env['stock.picking'].browse(res['res_id']) # Validate picking return_pick.move_line_ids.write({'quantity': 2}) return_pick.move_ids.picked = True return_pick.button_validate() # Check Received quantity self.assertEqual(self.po.order_line[0].qty_received, 3.0, 'Purchase: delivered quantity should be 3.0 instead of "%s" after picking return' % self.po.order_line[0].qty_received) #Create vendor bill for refund qty move_form = Form(self.env['account.move'].with_context(default_move_type='in_refund')) move_form.invoice_date = move_form.date move_form.partner_id = self.partner_a # Not supposed to see/change the purchase order of a refund invoice by default # #