From a2cb7fefe587a215f5bebe52ee85ef7bf440df0a Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Fri, 18 Aug 2023 14:02:52 +0700 Subject: automatic purchase pricelist --- indoteknik_custom/models/purchase_order.py | 29 ++++++++++++++++++++++++++ indoteknik_custom/models/purchase_pricelist.py | 20 +++++++++++++++--- indoteknik_custom/views/product_sla.xml | 14 ++++++------- indoteknik_custom/views/purchase_pricelist.xml | 7 +++++-- 4 files changed, 58 insertions(+), 12 deletions(-) diff --git a/indoteknik_custom/models/purchase_order.py b/indoteknik_custom/models/purchase_order.py index 9e851ef9..c968ed53 100755 --- a/indoteknik_custom/models/purchase_order.py +++ b/indoteknik_custom/models/purchase_order.py @@ -2,6 +2,7 @@ from odoo import fields, models, api, _ from odoo.exceptions import AccessError, UserError, ValidationError from datetime import datetime, timedelta import logging +from pytz import timezone _logger = logging.getLogger(__name__) @@ -40,6 +41,31 @@ class PurchaseOrder(models.Model): count_line_product = fields.Float('Total Item', compute='compute_count_line_product') note_description = fields.Char(string='Note', help='bisa diisi sebagai informasi indent barang tertentu atau apapun') + def add_product_to_pricelist(self): + for line in self.order_line: + current_time = fields.Datetime.now(timezone('Asia/Jakarta')).strftime('%Y-%m-%d %H:%M:%S') + + purchase_pricelist = self.env['purchase.pricelist'].search([ + ('product_id', '=', line.product_id.id), + ('vendor_id', '=', line.order_id.partner_id.id) + ]) + purchase_pricelist = purchase_pricelist.with_context(update_by='system') + if not purchase_pricelist: + purchase_pricelist.create([{ + 'vendor_id': line.order_id.partner_id.id, + 'product_id': line.product_id.id, + 'product_price': 0.00, + 'system_price': line.price_unit, + 'system_last_update': current_time, + }]) + return True + + for pricelist in purchase_pricelist: + pricelist.write({ + 'system_last_update': current_time, + 'system_price': line.price_unit + }) + def _compute_date_planned(self): for order in self: if order.date_approve: @@ -196,12 +222,15 @@ class PurchaseOrder(models.Model): for line in self.order_line: if not line.product_id.purchase_ok: raise UserError("Terdapat barang yang tidak bisa diproses") + if line.product_id: + self.add_product_to_pricelist() if line.price_unit != line.price_vendor: send_email = True break if send_email: self._send_mail() + self.approval_status = 'approved' self.po_status = 'menunggu' diff --git a/indoteknik_custom/models/purchase_pricelist.py b/indoteknik_custom/models/purchase_pricelist.py index 4ea2b9ef..419ca8e0 100755 --- a/indoteknik_custom/models/purchase_pricelist.py +++ b/indoteknik_custom/models/purchase_pricelist.py @@ -1,5 +1,7 @@ -from odoo import models, fields, api - +from odoo import fields, models, api, _ +from odoo.exceptions import AccessError, UserError, ValidationError +from datetime import datetime, timedelta +from pytz import timezone class PurchasePricelist(models.Model): _name = 'purchase.pricelist' @@ -8,8 +10,20 @@ class PurchasePricelist(models.Model): name = fields.Char(string='Name', compute="_compute_name") product_id = fields.Many2one('product.product', string="Product", required=True) vendor_id = fields.Many2one('res.partner', string="Vendor", required=True) - product_price = fields.Float(string='Price', required=True) + product_price = fields.Float(string='Human Price', required=True) + system_price = fields.Float(string='System Price', required=True) + human_last_update = fields.Datetime(string='Human Update') + system_last_update = fields.Datetime(string='System Update') @api.depends('product_id', 'vendor_id') def _compute_name(self): self.name = self.vendor_id.name + ', ' + self.product_id.name + + @api.constrains('product_price','system_price','vendor_id','product_id') + def _contrains_product_price(self): + current_time = fields.Datetime.now(timezone('Asia/Jakarta')).strftime('%Y-%m-%d %H:%M:%S') + update_by = self._context.get('update_by') + if update_by == 'system': + self.system_last_update = current_time + else: + self.human_last_update = current_time diff --git a/indoteknik_custom/views/product_sla.xml b/indoteknik_custom/views/product_sla.xml index cec3f980..d0d3f84a 100644 --- a/indoteknik_custom/views/product_sla.xml +++ b/indoteknik_custom/views/product_sla.xml @@ -18,15 +18,15 @@
+ - - - - - - + + + + - + +
diff --git a/indoteknik_custom/views/purchase_pricelist.xml b/indoteknik_custom/views/purchase_pricelist.xml index d6abcec1..f9fd52ba 100755 --- a/indoteknik_custom/views/purchase_pricelist.xml +++ b/indoteknik_custom/views/purchase_pricelist.xml @@ -8,6 +8,9 @@ + + + @@ -15,7 +18,7 @@ purchase.pricelist.form purchase.pricelist - +
@@ -23,8 +26,8 @@ + -
-- cgit v1.2.3