From a4352ddc19c392e0ed5f30d350ce928d98f5b6e2 Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Wed, 23 Aug 2023 11:59:53 +0700 Subject: mail po & note --- indoteknik_custom/models/__init__.py | 1 + indoteknik_custom/models/po_sync_price.py | 14 +++ .../models/procurement_monitoring_detail.py | 6 +- indoteknik_custom/models/purchase_order.py | 78 ++++++++++++++-- indoteknik_custom/models/sale_monitoring_detail.py | 2 +- indoteknik_custom/security/ir.model.access.csv | 1 + indoteknik_custom/views/mail_template_po.xml | 100 ++++++++------------- .../views/procurement_monitoring_detail.xml | 7 +- 8 files changed, 135 insertions(+), 74 deletions(-) create mode 100644 indoteknik_custom/models/po_sync_price.py diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 255a12f6..d884793a 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -81,3 +81,4 @@ from . import account_general_ledger from . import account_report_financial from . import account_report_general_ledger from . import price_group +from . import po_sync_price diff --git a/indoteknik_custom/models/po_sync_price.py b/indoteknik_custom/models/po_sync_price.py new file mode 100644 index 00000000..3e0ef621 --- /dev/null +++ b/indoteknik_custom/models/po_sync_price.py @@ -0,0 +1,14 @@ +from odoo import fields, models, api + + +class POSyncPrice(models.Model): + _name = "po.sync.price" + _description = "PO Sync Price" + + order_line_id = fields.Many2one('purchase.order.line', string='Purchase Order') + # purchase_order_id = fields.Many2one('purchase.order', string='Purchase Order') + # product_variant_id = fields.Many2one('product.product', string='Product Variant') + # manufacture_id = fields.Many2one('x_manufactures',string="Brand") + # unit_price = fields.Float(string="Unit Price") + # vendor_price = fields.Float(string="Vendor Price") + # created = fields.Datetime(string="Create Date") \ No newline at end of file diff --git a/indoteknik_custom/models/procurement_monitoring_detail.py b/indoteknik_custom/models/procurement_monitoring_detail.py index 6031e04a..80e71304 100644 --- a/indoteknik_custom/models/procurement_monitoring_detail.py +++ b/indoteknik_custom/models/procurement_monitoring_detail.py @@ -22,6 +22,7 @@ class SaleMonitoringDetail(models.Model): date_order = fields.Datetime(string="Date Order") status = fields.Char(string="Status") po_ids = fields.Many2many('purchase.order', string='PO', compute='_compute_po') + note = fields.Char(string="Note") def _compute_po(self): for line in self: @@ -58,7 +59,8 @@ class SaleMonitoringDetail(models.Model): get_qty_available(sol.product_id) as qty_available, get_qty_reserved(so.id, sol.product_id) as qty_reserved, get_qty_po(so.id, sol.product_id) AS qty_po, - so.date_order AS date_order + so.date_order AS date_order, + sol.note_procurement as note FROM sale_order so JOIN sale_order_line sol ON sol.order_id = so.id JOIN product_product p ON p.id = sol.product_id @@ -66,7 +68,7 @@ class SaleMonitoringDetail(models.Model): WHERE pt.type IN ('consu','product') AND so.state IN ('sale','done') AND so.create_date >= '2022-08-10' - and so.so_status not in ('terproses') + and so.so_status not in('terproses') ) a --where a.qty_po < a.qty_so ) diff --git a/indoteknik_custom/models/purchase_order.py b/indoteknik_custom/models/purchase_order.py index c968ed53..0020753f 100755 --- a/indoteknik_custom/models/purchase_order.py +++ b/indoteknik_custom/models/purchase_order.py @@ -2,7 +2,14 @@ from odoo import fields, models, api, _ from odoo.exceptions import AccessError, UserError, ValidationError from datetime import datetime, timedelta import logging -from pytz import timezone +from pytz import timezone, utc +import io +from openpyxl import Workbook +import base64 +try: + from odoo.tools.misc import xlsxwriter +except ImportError: + import xlsxwriter _logger = logging.getLogger(__name__) @@ -43,7 +50,7 @@ class PurchaseOrder(models.Model): 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') + current_time = datetime.utcnow() purchase_pricelist = self.env['purchase.pricelist'].search([ ('product_id', '=', line.product_id.id), @@ -224,7 +231,8 @@ class PurchaseOrder(models.Model): raise UserError("Terdapat barang yang tidak bisa diproses") if line.product_id: self.add_product_to_pricelist() - if line.price_unit != line.price_vendor: + if line.price_unit != line.price_vendor and line.price_vendor != 0: + self._send_po_not_sync() send_email = True break @@ -244,9 +252,69 @@ class PurchaseOrder(models.Model): return res + def _send_po_not_sync(self): + # Mengirim data ke model Po Sync Price jika harga po dan purchase pricelist tidak singkron + for line in self.order_line: + if line.price_unit != line.price_vendor and line.price_vendor != 0: + self.env['po.sync.price'].create([{ + 'order_line_id': line.id, + }]) + + + def _send_mail(self): - template_id = self.env.ref('indoteknik_custom.mail_template_po_sync_price').id - template = self.env['mail.template'].browse(template_id) + output = io.BytesIO() + workbook = xlsxwriter.Workbook(output, {'in_memory': True}) + worksheet = workbook.add_worksheet() + + format6 = workbook.add_format({'font_size': 12, 'align': 'center', 'bg_color': '#D3D3D3', 'bold': True}) + format1 = workbook.add_format({'font_size': 11, 'align': 'center', 'valign': 'vcenter'}) + format2 = workbook.add_format({'font_size': 10, 'bold': True}) + format3 = workbook.add_format({'font_size': 10, 'bold': True}) + + worksheet.set_column(0, 0, 10) + worksheet.set_column(1, 1, 20) + worksheet.set_column(2, 2, 20) + worksheet.set_column(3, 3, 20) + worksheet.set_column(4, 4, 15) + worksheet.set_column(5, 5, 15) + + worksheet.write('A1', 'PO', format6) + worksheet.write('B1', 'SKU', format6) + worksheet.write('C1', 'Product', format6) + worksheet.write('D1', 'Brand', format6) + worksheet.write('E1', 'Unit Price', format6) + worksheet.write('F1', 'Vendor Price', format6) + worksheet.write('G1', 'Created On', format6) + + row_number = 1 + po_sync = self.env['po.sync.price'].search([], order='create_date desc') + for po in po_sync: + worksheet.write(row_number, 0, po.order_line_id.order_id.name, format1) + worksheet.write(row_number, 1, po.order_line_id.product_id.default_code, format1) + worksheet.write(row_number, 2, po.order_line_id.product_id.name, format1) + worksheet.write(row_number, 3, po.order_line_id.product_id.x_manufacture.x_name, format1) + worksheet.write(row_number, 4, po.order_line_id.price_unit, format1) + worksheet.write(row_number, 5, po.order_line_id.price_vendor, format1) + worksheet.write(row_number, 6, po.create_date.replace(tzinfo=utc).astimezone(timezone('Asia/Jakarta')).strftime('%Y-%m-%d %H:%M:%S'), format1) + row_number += 1 + + workbook.close() + + output.seek(0) + + template = self.env.ref('indoteknik_custom.mail_template_po_sync_price') + template.attachment_ids.unlink() + attachment_vals = { + 'name': 'Purchase Order.xlsx', + 'datas': base64.b64encode(output.read()), + 'mimetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'res_model': 'mail.template', + 'res_id': template.id, + } + attachment_id = self.env['ir.attachment'].create(attachment_vals) + + template.attachment_ids = [(4, attachment_id.id)] template.send_mail(self.id, force_send=True) def po_approve(self): diff --git a/indoteknik_custom/models/sale_monitoring_detail.py b/indoteknik_custom/models/sale_monitoring_detail.py index 43a8aeb0..dc4caa14 100755 --- a/indoteknik_custom/models/sale_monitoring_detail.py +++ b/indoteknik_custom/models/sale_monitoring_detail.py @@ -55,7 +55,7 @@ class SaleMonitoringDetail(models.Model): get_qty_received(so.id, sol.product_id) AS qty_po_received, get_qty_reserved(so.id, sol.product_id) as qty_reserved, sol.note_procurement as note - FROM sale_order so + FROM sale_order so JOIN sale_order_line sol ON sol.order_id = so.id JOIN product_product p ON p.id = sol.product_id JOIN product_template pt ON pt.id = p.product_tmpl_id diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv index 6f9ba9e4..da389854 100755 --- a/indoteknik_custom/security/ir.model.access.csv +++ b/indoteknik_custom/security/ir.model.access.csv @@ -67,3 +67,4 @@ access_account_move_multi_update,access.account.move.multi_update,model_account_ access_airway_bill,access.airway.bill,model_airway_bill,,1,1,1,1 access_airway_bill_manifest,access.airway.bill.manifest,model_airway_bill_manifest,,1,1,1,1 access_price_group,access.price.group,model_price_group,,1,1,1,1 +access_po_sync_price,access.po.sync.price,model_po_sync_price,,1,1,1,1 diff --git a/indoteknik_custom/views/mail_template_po.xml b/indoteknik_custom/views/mail_template_po.xml index 02053767..f4eb3577 100644 --- a/indoteknik_custom/views/mail_template_po.xml +++ b/indoteknik_custom/views/mail_template_po.xml @@ -12,69 +12,43 @@ style="padding-top: 16px; background-color: #F1F1F1; font-family:Verdana, Arial,sans-serif; color: #454748; width: 100%; border-collapse:separate;"> - - - - - - - - - - - +
- - - - - - - -
- PO
- - ${object.name} - -
-
-
-
- - - - - - - -
-
- Dear Stefanus Darren & Tyas K Putra, -

- Terdapat PO yang harga Unit Price nya tidak sama dengan yang ada di purchase pricelist nya. -

- Berikut adalah rincian PO: - % for line in object.order_line: - % if line.price_unit != line.price_vendor -
    -
  • Nama Produk: ${line.product_id.name}
  • -
  • Harga Unit dalam PO: Rp ${'{:,.2f}'.format(line.price_unit)}
  • -
  • Harga Unit dalam Purchase Pricelist: Rp ${'{:,.2f}'.format(line.price_vendor)}
  • -
- % endif - % endfor - Silahkan periksa dan lakukan koreksi jika diperlukan. -

- Terima kasih. -
-
-
-
-
+ + +
+
+ Dear Stefanus Darren & Tyas K Putra, +

+ Terdapat PO yang harga Unit Price nya tidak sama dengan yang ada di purchase pricelist nya. +

+ Berikut adalah rincian PO: +
+ + + + + + + + % for line in object.order_line: + % if line.price_vendor != 0 and line.price_unit != line.price_vendor + + + + + + + % endif + % endfor +
Nama ProdukBrandHarga Unit dalam POHarga Unit dalam Purchase Pricelist
${line.product_id.name}${line.product_id.x_manufacture.x_name}Rp ${'{:,.2f}'.format(line.price_unit)}Rp ${'{:,.2f}'.format(line.price_vendor)}
+
+ Silahkan periksa dan lakukan koreksi jika diperlukan. +

+ Terima kasih. +
+
diff --git a/indoteknik_custom/views/procurement_monitoring_detail.xml b/indoteknik_custom/views/procurement_monitoring_detail.xml index 8f3827c1..e4939213 100644 --- a/indoteknik_custom/views/procurement_monitoring_detail.xml +++ b/indoteknik_custom/views/procurement_monitoring_detail.xml @@ -17,10 +17,11 @@ + -- cgit v1.2.3