diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2024-03-13 16:24:13 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2024-03-13 16:24:13 +0700 |
| commit | d3e3a8abf0ae382442c16a7ac6091c7bb872313f (patch) | |
| tree | 52ce38f2497b89dc11ca3ae3b1d9f39fb2afc78e /indoteknik_custom/models/purchasing_job.py | |
| parent | 00b6739e4f4228c1cc66de0ef63312bc633ae21f (diff) | |
change request purchasing job
Diffstat (limited to 'indoteknik_custom/models/purchasing_job.py')
| -rw-r--r-- | indoteknik_custom/models/purchasing_job.py | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/indoteknik_custom/models/purchasing_job.py b/indoteknik_custom/models/purchasing_job.py index 4afb3e03..b3c25256 100644 --- a/indoteknik_custom/models/purchasing_job.py +++ b/indoteknik_custom/models/purchasing_job.py @@ -1,5 +1,6 @@ -from odoo import fields, models, api, tools +from odoo import fields, models, api, tools, _ import logging +from datetime import datetime _logger = logging.getLogger(__name__) @@ -39,9 +40,54 @@ class PurchasingJob(models.Model): def generate_request_po(self): # print(1) # TODO create document automatic purchase + + current_time = datetime.utcnow() + + automatic_purchase = self.env['automatic.purchase'].create([{ + 'apo_type': 'regular', + 'date_doc': current_time, + }]) + count = 0 for job in self: print(job.product_id.name) + qty_purchase = job.outgoing - (job.onhand + job.incoming) + qty_available = (job.onhand + job.incoming) - job.outgoing + + domain = [ + ('product_id.id', '=', job.product_id.id), + ] + orderby = 'count_trx_po desc, count_trx_po_vendor desc' + purchase_pricelist = self.env['purchase.pricelist'].search(domain, order=orderby, limit=1) + vendor_id = purchase_pricelist.vendor_id + price, taxes = automatic_purchase._get_valid_purchase_price(purchase_pricelist) + last_po_line = self.env['purchase.order.line'].search([('product_id', '=', job.product_id.id), ('order_id.state', '=', 'done')], order='id desc', limit=1) + + self.env['automatic.purchase.line'].create([{ + 'automatic_purchase_id': automatic_purchase.id, + 'product_id': job.product_id.id, + 'qty_purchase': qty_purchase, + 'qty_available': qty_available, + 'partner_id': vendor_id.id, + 'last_price': price, + 'taxes_id': taxes, + 'subtotal': qty_purchase * price, + 'last_order_id': last_po_line.order_id.id, + 'last_orderline_id': last_po_line.id, + 'brand_id': job.product_id.product_tmpl_id.x_manufacture.id + }]) + automatic_purchase._create_sales_matching() + automatic_purchase._create_sync_purchasing_job(job) + count += 1 + _logger.info('Create Automatic Purchase Line %s' % job.product_id.name) + return { + 'name': _('Automatic Purchase'), + 'view_mode': 'tree,form', + 'res_model': 'automatic.purchase', + 'target': 'current', + 'type': 'ir.actions.act_window', + 'domain': [('id', '=', automatic_purchase.id)], + } class OutstandingSales(models.Model): _name = 'v.sales.outstanding' |
