diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2024-01-31 16:19:39 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2024-01-31 16:19:39 +0700 |
| commit | 1f491c22d519edb0df0515eba803dcd2a3436cf8 (patch) | |
| tree | 11576007cab66c7485c075b144e51ea7ed1dcd35 | |
| parent | d62a28e8760c86e2c0a6cb7469f626d9447272be (diff) | |
fix error local variable assignment
| -rw-r--r-- | indoteknik_custom/models/requisition.py | 227 |
1 files changed, 111 insertions, 116 deletions
diff --git a/indoteknik_custom/models/requisition.py b/indoteknik_custom/models/requisition.py index 7ceff6e5..d6b33044 100644 --- a/indoteknik_custom/models/requisition.py +++ b/indoteknik_custom/models/requisition.py @@ -1,6 +1,7 @@ -from odoo import models, fields, api +from odoo import models, fields, api, _ from odoo.exceptions import UserError from datetime import datetime +import math import logging _logger = logging.getLogger(__name__) @@ -28,108 +29,84 @@ class Requisition(models.Model): result = super(Requisition, self).create(vals) return result - # def create_requisition_from_sales_with_price(self): - # if self.requisition_lines: - # raise UserError('Sudah digenerate sebelumnya, hapus line terlebih dahulu') - # if not self.sale_order_id: - # raise UserError('Sale Order harus diisi') - # if self.is_po: - # raise UserError('Sudah jadi PO, tidak bisa di create ulang PO nya') - - # count = 0 - # for order_line in self.sale_order_id.order_line: - # # get purchase price altama, if nothing, then get other cheaper, if nothing then last po - # purchase_price = order_line.purchase_price - # vendor_id = order_line.vendor_id.id - - # # get qty available bandengan - # qty_available = order_line.product_id.qty_onhand_bandengan + order_line.product_id.qty_incoming_bandengan - order_line.product_id.outgoing_qty - # suggest = 'harus beli' - # if qty_available > order_line.product_qty: - # suggest = 'masih cukup' - - # self.env['requisition.line'].create([{ - # 'requisition_id': self.id, - # 'partner_id': vendor_id, - # 'brand_id': order_line.product_id.product_tmpl_id.x_manufacture.id, - # 'product_id': order_line.product_id.id, - # 'qty_purchase': order_line.product_uom_qty, - # 'tax_id': order_line.purchase_tax_id.id, - # 'price_unit': purchase_price, - # 'subtotal': purchase_price * order_line.product_uom_qty, - # 'source': 'sales', - # 'qty_available_store': qty_available, - # 'suggest': suggest, - # }]) - # count+=1 - # _logger.info('Create Requisition %s' % order_line.product_id.name) - # self.notification = "Requisition Created %s lines" % count - - # def create_requisition_from_sales(self): - # if self.requisition_lines: - # raise UserError('Sudah digenerate sebelumnya, hapus line terlebih dahulu') - # if not self.sale_order_id: - # raise UserError('Sale Order harus diisi') - # if self.is_po: - # raise UserError('Sudah jadi PO, tidak bisa di create ulang PO nya') + + def create_po_from_requisition(self): + if not self.requisition_lines: + raise UserError('Tidak ada Lines, belum bisa create PO') + if self.is_po: + raise UserError('Sudah pernah di create PO') + + vendor_ids = self.env['requisition.line'].read_group([ + ('requisition_id', '=', self.id), + ('partner_id', '!=', False) + ], fields=['partner_id'], groupby=['partner_id']) + + po_ids = [] + for vendor in vendor_ids: + result_po = self.create_po_by_vendor(vendor['partner_id'][0]) + po_ids.append(result_po) + return { + 'name': _('Purchase Order'), + 'view_mode': 'tree,form', + 'res_model': 'purchase.order', + 'target': 'current', + 'type': 'ir.actions.act_window', + 'domain': [('id', 'in', po_ids)], + } - # # old_requisition = self.env['requisition'].search([('sale_order_id', '=', self.sale_order_id.id)], limit=1) - # # if old_requisition: - # # raise UserError('Sudah pernah jadi Requisition') - - # count = 0 - # for order_line in self.sale_order_id.order_line: - # # get purchase price altama, if nothing, then get other cheaper, if nothing then last po - # purchase_price = 0 - # vendor_id = 0 - - # # get qty available bandengan - # qty_available = order_line.product_id.qty_onhand_bandengan + order_line.product_id.qty_incoming_bandengan - order_line.product_id.outgoing_qty - # suggest = 'harus beli' - # if qty_available > order_line.product_qty: - # suggest = 'masih cukup' - - # purchase_pricelist = self.env['purchase.pricelist'].search([ - # ('product_id.id', '=', order_line.product_id.id), - # ('vendor_id.id', '=', 5571) - # ], order='product_price asc', limit=1) - # purchase_price = purchase_pricelist.product_price - # vendor_id = purchase_pricelist.vendor_id.id - # source = 'PriceList' - - # if not purchase_price or purchase_price <= 0: - # purchase_pricelist = self.env['purchase.pricelist'].search([('product_id', '=', order_line.product_id.id)], order='product_price asc', limit=1) - # purchase_price = purchase_pricelist.product_price - # vendor_id = purchase_pricelist.vendor_id.id - # source = 'PriceList' - - # if not purchase_price or purchase_price <= 0: - # last_po_line = self.env['purchase.order.line'].search([('product_id', '=', order_line.product_id.id), ('order_id.state', '=', 'done')], order='id desc', limit=1) - # purchase_price = last_po_line.price_unit - # vendor_id = last_po_line.order_id.partner_id.id - # source = 'LastPO' - - # if not purchase_price or purchase_price <= 0: - # purchase_price = 0 - # vendor_id = 5571 - # source = 'Nothing' - - # self.env['requisition.line'].create([{ - # 'requisition_id': self.id, - # 'partner_id': vendor_id, - # 'brand_id': order_line.product_id.product_tmpl_id.x_manufacture.id, - # 'product_id': order_line.product_id.id, - # 'qty_purchase': order_line.product_uom_qty, - # 'tax_id': order_line.purchase_tax_id.id, - # 'price_unit': purchase_price, - # 'subtotal': purchase_price * order_line.product_uom_qty, - # 'source': source, - # 'qty_available_store': qty_available, - # 'suggest': suggest, - # }]) - # count+=1 - # _logger.info('Create Requisition %s' % order_line.product_id.name) - # self.notification = "Requisition Created %s lines" % count + def create_po_by_vendor(self, vendor_id): + current_time = datetime.now() + + PRODUCT_PER_PO = 20 + + requisition_line = self.env['requisition.line'] + + param_header = { + 'partner_id': vendor_id, + # 'partner_ref': self.sale_order_id.name, + 'currency_id': 12, + 'user_id': self.env.user.id, + 'company_id': 1, # indoteknik dotcom gemilang + 'picking_type_id': 28, # indoteknik bandengan receipts + 'date_order': current_time, + 'sale_order_id': self.sale_order_id.id, + 'note_description': 'from Purchase Requisition' + } + + domain = [ + ('requisition_id', '=', self.id), + ('partner_id', '=', vendor_id), + ('qty_purchase', '>', 0) + ] + + products_len = requisition_line.search_count(domain) + page = math.ceil(products_len / PRODUCT_PER_PO) + po_ids = [] + # i start from zero (0) + for i in range(page): + new_po = self.env['purchase.order'].create([param_header]) + new_po.name = new_po.name + "/R/" + str(i + 1) + po_ids.append(new_po.id) + lines = requisition_line.search( + domain, + offset=i * PRODUCT_PER_PO, + limit=PRODUCT_PER_PO + ) + tax = [22] + + for line in lines: + product = line.product_id + param_line = { + 'product_id': product.product_id.id, + 'product_qty': product.qty_purchase, + 'product_uom_qty': product.qty_purchase, + 'price_unit': product.price_unit, + 'taxes_id': tax, + } + new_po_line = self.env['purchase.order.line'].create([param_line]) + line.current_po_id = new_po.id + line.current_po_line_id = new_po_line.id + return po_ids def create_po_from_requisition(self): if not self.requisition_lines: @@ -140,6 +117,7 @@ class Requisition(models.Model): vendor_ids = self.env['requisition.line'].read_group([('requisition_id', '=', self.id), ('partner_id', '!=', False)], fields=['partner_id'], groupby=['partner_id']) counter_po_number = 0 + po_ids = [] for vendor in vendor_ids: param_header = { 'partner_id': vendor['partner_id'][0], @@ -152,25 +130,29 @@ class Requisition(models.Model): 'sale_order_id': self.sale_order_id.id, 'note_description': 'from Purchase Requisition' } - # new_po = self.env['purchase.order'].create([param_header]) - products_vendors = self.env['requisition.line'].search([ + param_requisition_line = [ ('requisition_id', '=', self.id), ('partner_id', '=', vendor['partner_id'][0]), ('qty_purchase', '>', 0) - ], order='brand_id') + ] + # new_po = self.env['purchase.order'].create([param_header]) + products_vendors = self.env['requisition.line'].search(, order='brand_id') count = brand_id = 0 for product in products_vendors: - if count == 200 or brand_id != product.brand_id.id: - count = 0 - counter_po_number += 1 - new_po = self.env['purchase.order'].create([param_header]) - new_po.name = new_po.name + "/R/"+str(counter_po_number) - self.env['requisition.purchase.match'].create([{ - 'requisition_id': self.id, - 'order_id': new_po.id - }]) - self.env.cr.commit() + if count > 200 or brand_id != product.brand_id.id: + continue + + count = 0 + counter_po_number += 1 + new_po = self.env['purchase.order'].create([param_header]) + new_po.name = new_po.name + "/R/"+str(counter_po_number) + self.env['requisition.purchase.match'].create([{ + 'requisition_id': self.id, + 'order_id': new_po.id + }]) + po_ids.append(new_po.id) + self.env.cr.commit() # else: # new_po = self.env['purchase.order'].create([param_header]) brand_id = product.brand_id.id @@ -184,7 +166,7 @@ class Requisition(models.Model): tax = [22] param_line = { - 'order_id': new_po.id, + 'sequence': count, 'product_id': product.product_id.id, 'product_qty': product.qty_purchase, @@ -195,11 +177,24 @@ class Requisition(models.Model): # 'suggest': suggest, } new_line = self.env['purchase.order.line'].create([param_line]) + if new_po: + new_line.write({ + 'order_id': new_po.id, + }) product.current_po_id = new_po.id product.current_po_line_id = new_line.id _logger.info('Create PO Line %s' % product.product_id.name) # self.notification = self.notification + ' %s' % new_po.name self.is_po = True + if po_ids: + return { + 'name': _('Purchase Order'), + 'view_mode': 'tree,form', + 'res_model': 'purchase.order', + 'target': 'current', + 'type': 'ir.actions.act_window', + 'domain': [('id', 'in', po_ids)], + } class RequisitionLine(models.Model): _name = 'requisition.line' |
