Q:

How to update date_expected value in stock_move document based on custom datetime field in sale_order_line

I am new in odoo. I am trying to update the date_expected value in stock_move document based on the user given value (Custom field) in sale_order_line. Here is what I am trying in code:
class SOLModel(models.Model):
   _inherit = 'sale.order.line'

   date_expected = fields.Datetime('Expected Date')

   sale_delay=fields.Float(related='product_id.product_tmpl_id.sale_delay');

   @api.multi
   def _prepare_procurement_values(self, group_id=False):
       res = super(SOLModel, self)._prepare_procurement_values(group_id)
       date_expected_do = fields.Datetime.from_string(self.date_expected) - timedelta(days=int(self.sale_delay))
       res.update({'date_expected': date_expected_do})
       return res

class StockRuleInherit(models.Model):
   _inherit = 'stock.rule'

   def _get_stock_move_values(self, product_id, product_qty, product_uom, location_id, name, origin, values, group_id):
       res = super(StockRuleInherit, self)._get_stock_move_values(product_id, product_qty, product_uom, location_id,
                                                               name, origin, values, group_id)
       res['date_expected'] = values.get('date_expected', False)
       return res

Here is my xml
<record model="ir.ui.view" id="sale_order_line_view_inherit">
     <field name="name">sale.order.line.view.inherit1</field>
     <field name="model">sale.order</field>
     <field name="inherit_id" ref="sale.view_order_form"/>
     <field name="arch" type="xml">
         <xpath expr="/form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='name']" position="after">
            <field name="date_expected" force_save="1" options="{'datepicker':{'minDate': 'now'}}"
             attrs="{'readonly': [('state', 'in', ('done', 'cancel'))],'required': True,}"/>
             <field name="sale_delay" invisible="1"/>
         </xpath>
     </field>
</record>

But I am getting error when try to confirm/save Sale Order. 
The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set

[object with reference: date_expected - date.expected]

In debug log I can see the
 bad query: b'INSERT INTO "stock_move" ("id", "create_uid", "create_date", "write_uid", "write_date", "additional", "company_id", "date", "date_expected", "group_id", "is_done", "location_dest_id", "location_id", "name", "origin", "partner_id", "picking_type_id", "priority", "procure_method", "product_id", "product_uom", "product_uom_qty", "propagate", "rule_id", "scrapped", "sequence", "state", "to_refund", "warehouse_id") VALUES (nextval(\'stock_move_id_seq\'), 1, (now() at time zone \'UTC\'), 1, (now() at time zone \'UTC\'), false, 1, \'2019-09-12 11:49:04\', NULL, 1668, false, 15, 12, \'VF01: Output \xe2\x86\x92 Customers\', \'SO074\', 9620, 3, \'1\', \'make_to_order\', 23836, 1, \'1.000\', true, 4, false, 10, \'draft\', false, 1) RETURNING id'
ERROR: null value in column "date_expected" violates not-null constraint

Can anybody help me about this issue?
Thanks in advance.

A:

Short answer: Your immediate problem seems to be that when you call _get_stock_move_values, values has no expected_date so values.get('date_expected', False) evaluates to False which, in turn, is converted to NULL when attempting to store into the database.
Then, maybe you're overloading the wrong _prepare_procurement_values, as there are four of them:
<path_to_v12>/addons/sale/models/sale.py:1429:    def _prepare_procurement_values(self, group_id=False):
<path_to_v12>/addons/sale_stock/models/sale_order.py:325:    def _prepare_procurement_values(self, group_id=False):
<path_to_v12>/addons/stock/models/stock_warehouse.py:971:    def _prepare_procurement_values(self, product_qty, date=False, group=False):
<path_to_v12>/addons/stock/models/stock_move.py:781:    def _prepare_procurement_values(self):

May I also suggest to changing the field name from expected_date to something like expected_date_custom because there are many expected_date values generated by the functions you're messing with. Per instance:
196     def _get_stock_move_values(self, product_id, product_qty, product_uom, location_id, name, origin, values, group_id):
...
203         date_expected = fields.Datetime.to_string(
204             fields.Datetime.from_string(values['date_planned']) - relativedelta(days=self.delay or 0)
205         )
...
209         move_values = {
...
226             'date': date_expected,
227             'date_expected': date_expected,
...
230         }
...
234         return move_values

On how False ends up NULL when writing to the database
<path_to_v12>/odoo/models.py
3361     def _write(self, vals):
3362         # low-level implementation of write()
...
3379         for name, val in vals.items():
...
3386             if field.column_type:
...
3389                     val = field.convert_to_column(val, self, vals)

<path_to_v12>/odoo/fields.py
755     def convert_to_column(self, value, record, values=None):
756         """ Convert ``value`` from the ``write`` format to the SQL format. """
757         if value is None or value is False:
758             return None
...

