def create_analytic_lines(self, cr, uid, ids, context={}): res = super(account_move_line, self).create_analytic_lines(cr, uid, ids, context) matched_invoice_line_ids = [] for move_line in self.browse(cr, uid, ids, context): if move_line.analytic_lines and len( move_line.analytic_lines ) == 1 and move_line.product_id.is_maintenance and move_line.invoice: print "native analytic line creation" print move_line analytic_line = move_line.analytic_lines[ 0] #we assume their is only one analytic line for a maintenance product print analytic_line for invoice_line in move_line.invoice.invoice_line: if invoice_line.id not in matched_invoice_line_ids: #we take care of not matching invoice line twice if invoice_line.product_id and invoice_line.product_id.is_maintenance \ and invoice_line.product_id.id == analytic_line.product_id.id and analytic_line.amount == invoice_line.price_subtotal: matched_invoice_line_ids.append(invoice_line.id) month_qty = int(invoice_line.maintenance_month_qty) splitted_amount = invoice_line.quantity / invoice_line.maintenance_month_qty amount = float(analytic_line.amount) / float( invoice_line.maintenance_month_qty) start_date = DateTime.strptime( invoice_line.maintenance_start_date, '%Y-%m-%d') self.pool.get('account.analytic.line').write( cr, uid, analytic_line.id, { 'invoice_line_id': invoice_line.id, 'date': start_date, 'unit_amount': splitted_amount, 'amount': amount }) for i in range(1, month_qty): start_date += DateTime.RelativeDateTime( months=1) print start_date self.pool.get('account.analytic.line').copy( cr, uid, analytic_line.id, {'date': start_date }) #TODO check month_qty return res
def default_maintenance_start_date(self, cr, uid, context={}): now = DateTime.now() date = DateTime.DateTime(now.year, now.month, fixed_month_init_day, 0, 0, 0.0) + DateTime.RelativeDateTime( months=default_maintenance_start_delta) return date.strftime('%Y-%m-%d')
def maintenance_qty_change(self, cr, uid, ids, maintenance_product_qty=False, maintenance_month_qty=False, maintenance_start_date=False, maintenance_end_date=False, is_maintenance=False, fleet_id=False): result = {} if not is_maintenance: return result result['value'] = {} warning_messages = "" if maintenance_start_date: start = DateTime.strptime(maintenance_start_date, '%Y-%m-%d') if start.day != fixed_month_init_day: warning_messages += "- Start date should should ideally start at day %s of the month; corrected to day %s\n" % ( fixed_month_init_day, fixed_month_init_day) start = DateTime.DateTime(start.year, start.month, fixed_month_init_day) result['value'].update( {'maintenance_start_date': start.strftime('%Y-%m-%d')}) #return result if maintenance_end_date: end = DateTime.strptime(maintenance_end_date, '%Y-%m-%d') en_date_check = end + DateTime.RelativeDateTime( days=fixed_days_before_month_end + 1) if end.month == en_date_check.month or en_date_check.day != 1: warning_messages += "- End date should should end %s days before the end of the month! It has been reset to the correct value.\n" % fixed_days_before_month_end day = end.days_in_month - fixed_days_before_month_end end = DateTime.DateTime(end.year, end.month, day, 0, 0, 0.0) result['value'].update( {'maintenance_end_date': end.strftime('%Y-%m-%d')}) if maintenance_start_date and maintenance_end_date: if end < start: warning_messages += "- End date should be AFTER Start date!\n" day = start.days_in_month - fixed_days_before_month_end #then we set the minimal end date end = DateTime.DateTime(start.year, start.month, day, 0, 0, 0.0) result['value'].update( {'maintenance_end_date': end.strftime('%Y-%m-%d')}) maintenance_month_qty = self._get_maintenance_month_qty_from_start_end( cr, uid, start, end) result['value'].update( {'maintenance_month_qty': maintenance_month_qty}) if maintenance_month_qty < min_maintenance_months: warning_messages += "- we usually try to sell %s months at least!\n" % min_maintenance_months if fleet_id: fleet = self.pool.get('stock.location').browse( cr, uid, fleet_id) theoretic_end = self._get_end_date_from_start_date( cr, uid, start, fleet) if theoretic_end.year != end.year or theoretic_end.month != end.month or theoretic_end.day != end.day: warning_messages += "- Theoretic Maintenance End Date was: %s !\n" % theoretic_end.strftime( '%Y-%m-%d') if maintenance_product_qty and maintenance_month_qty: #only set the default fleet at init result['value'].update({ 'product_uom_qty': maintenance_product_qty * maintenance_month_qty }) result['value'].update({ 'product_uos_qty': maintenance_product_qty * maintenance_month_qty }) # TODO * product_obj.uos_coeff if len(warning_messages) > 1: result['warning'] = { 'title': 'Maintenance Dates Warning', 'message': warning_messages } return result