[IMP] purchase: set schedule_date of po lines as per date_planned of PO

In this commit -
1. Previously, date_planned of PO was computed and set while the creation of PO.
   In this commit, removed the compute logic and now field will be empty until
   user fills it.
2. ‘Set date to all order lines’ button is removed.
3. Now if user have filled the date_planned of PO, it will be copied to all po
   lines.
4. Schedule date of PO line will be readonly if date_planned have any value
   else will be editable.
5. Update the test cases of computation of date_planned.

Task-2032417
This commit is contained in:
Ankita Raval
2019-08-01 08:11:59 +00:00
committed by Simon Lejeune
parent 8bf1486da5
commit a2a39ef41e
3 changed files with 22 additions and 29 deletions
+19 -21
View File
@@ -31,18 +31,6 @@ class PurchaseOrder(models.Model):
'amount_total': amount_untaxed + amount_tax,
})
@api.depends('order_line.date_planned', 'date_order')
def _compute_date_planned(self):
for order in self:
min_date = False
for line in order.order_line:
if not min_date or line.date_planned and line.date_planned < min_date:
min_date = line.date_planned
if min_date:
order.date_planned = min_date
else:
order.date_planned = order.date_order
@api.depends('state', 'order_line.qty_invoiced', 'order_line.qty_received', 'order_line.product_qty')
def _get_invoiced(self):
precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
@@ -109,7 +97,7 @@ class PurchaseOrder(models.Model):
], string='Billing Status', compute='_get_invoiced', store=True, readonly=True, copy=False, default='no')
# There is no inverse function on purpose since the date may be different on each line
date_planned = fields.Datetime(string='Scheduled Date', compute='_compute_date_planned', store=True, index=True)
date_planned = fields.Datetime(string='Receipt Date', index=True)
amount_untaxed = fields.Monetary(string='Untaxed Amount', store=True, readonly=True, compute='_amount_all', tracking=True)
amount_tax = fields.Monetary(string='Taxes', store=True, readonly=True, compute='_amount_all')
@@ -169,6 +157,12 @@ class PurchaseOrder(models.Model):
vals['name'] = self.env['ir.sequence'].next_by_code('purchase.order') or '/'
return super(PurchaseOrder, self).create(vals)
def write(self, vals):
res = super(PurchaseOrder, self).write(vals)
if vals.get('date_planned'):
self.order_line.write({'date_planned': vals['date_planned']})
return res
def unlink(self):
for order in self:
if not order.state == 'cancel':
@@ -178,10 +172,13 @@ class PurchaseOrder(models.Model):
def copy(self, default=None):
new_po = super(PurchaseOrder, self).copy(default=default)
for line in new_po.order_line:
seller = line.product_id._select_seller(
partner_id=line.partner_id, quantity=line.product_qty,
date=line.order_id.date_order and line.order_id.date_order.date(), uom_id=line.product_uom)
line.date_planned = line._get_date_planned(seller)
if new_po.date_planned:
line.date_planned = new_po.date_planned
else:
seller = line.product_id._select_seller(
partner_id=line.partner_id, quantity=line.product_qty,
date=line.order_id.date_order and line.order_id.date_order.date(), uom_id=line.product_uom)
line.date_planned = line._get_date_planned(seller)
return new_po
def _track_subtype(self, init_values):
@@ -409,10 +406,6 @@ class PurchaseOrder(models.Model):
result['context']['default_reference'] = self.partner_ref
return result
def action_set_date_planned(self):
for order in self:
order.order_line.update({'date_planned': order.date_planned})
class PurchaseOrderLine(models.Model):
_name = 'purchase.order.line'
@@ -549,6 +542,11 @@ class PurchaseOrderLine(models.Model):
if values.get('display_type', self.default_get(['display_type'])['display_type']):
values.update(product_id=False, price_unit=0, product_uom_qty=0, product_uom=False, date_planned=False)
order_id = values.get('order_id')
if 'date_planned' not in values:
order = self.env['purchase.order'].browse(order_id)
if order.date_planned:
values['date_planned'] = order.date_planned
line = super(PurchaseOrderLine, self).create(values)
if line.order_id.state == 'purchase':
msg = _("Extra line with %s ") % (line.product_id.display_name,)
+1 -6
View File
@@ -206,7 +206,7 @@
context="{'partner_id':parent.partner_id, 'quantity':product_qty,'uom':product_uom, 'company_id': parent.company_id}"
force_save="1" domain="[('purchase_ok', '=', True), '|', ('company_id', '=', False), ('company_id', '=', parent.company_id)]"/>
<field name="name" widget="section_and_note_text"/>
<field name="date_planned" optional="hide" attrs="{'required': [('display_type', '=', False)]}"/>
<field name="date_planned" optional="hide" attrs="{'required': [('display_type', '=', False)], 'readonly': [('parent.date_planned', '!=', False)]}"/>
<field name="company_id" groups="base.group_multi_company" options="{'no_create': True}"/>
<field name="account_analytic_id" optional="hide" context="{'default_partner_id':parent.partner_id}" groups="analytic.group_analytic_accounting"/>
<field name="analytic_tag_ids" optional="hide" groups="analytic.group_analytic_tags" widget="many2many_tags" options="{'color_field': 'color'}"/>
@@ -342,11 +342,6 @@
<label for="date_planned"/>
<div>
<field name="date_planned" attrs="{'readonly': [('state', 'not in', ('draft', 'sent'))]}"/>
<button name="action_set_date_planned" type="object"
states="draft,sent"
string="Set date to all order lines"
help="This changes the scheduled date of all order lines to the given date"
class="fa fa-calendar o_icon_button oe_edit_only"/>
</div>
</group>
<group>
@@ -33,7 +33,7 @@ class TestPurchaseLeadTime(TestPurchase):
# Check scheduled date of purchase order
schedule_date = order_date + timedelta(days=self.product_1.seller_ids.delay)
self.assertEqual(purchase.date_planned, schedule_date, 'Schedule date should be equal to: Order date of Purchase order + Delivery Lead Time.')
self.assertEqual(purchase.order_line.date_planned, schedule_date, 'Schedule date should be equal to: Order date of Purchase order + Delivery Lead Time.')
# check the picking created or not
self.assertTrue(purchase.picking_ids, "Picking should be created.")
@@ -78,7 +78,7 @@ class TestPurchaseLeadTime(TestPurchase):
# Check scheduled date of purchase order
po_schedule_date = min(schedule_date_1, schedule_date_2)
self.assertEqual(purchase2.date_planned, po_schedule_date, 'Schedule date of purchase order should be minimum of schedule dates of purchase order lines.')
self.assertEqual(purchase2.order_line[1].date_planned, po_schedule_date, 'Schedule date of purchase order should be minimum of schedule dates of purchase order lines.')
# Check the picking created or not
self.assertTrue(purchase2.picking_ids, "Picking should be created.")