diff options
Diffstat (limited to 'indoteknik_custom/models/automatic_purchase.py')
| -rw-r--r-- | indoteknik_custom/models/automatic_purchase.py | 237 |
1 files changed, 184 insertions, 53 deletions
diff --git a/indoteknik_custom/models/automatic_purchase.py b/indoteknik_custom/models/automatic_purchase.py index a7b24b4f..070db78f 100644 --- a/indoteknik_custom/models/automatic_purchase.py +++ b/indoteknik_custom/models/automatic_purchase.py @@ -1,7 +1,7 @@ from odoo import models, api, fields from odoo.exceptions import UserError from datetime import datetime -import logging +import logging, math _logger = logging.getLogger(__name__) @@ -50,64 +50,195 @@ class AutomaticPurchase(models.Model): raise UserError('Tidak ada Lines, belum bisa create PO') if self.is_po: raise UserError('Sudah pernah di create PO') + current_time = datetime.now() - vendor_ids = self.env['automatic.purchase.line'].read_group([('automatic_purchase_id', '=', self.id), ('partner_id', '!=', False)], fields=['partner_id'], groupby=['partner_id']) - - counter_po_number = 0 + vendor_ids = self.env['automatic.purchase.line'].read_group( + [('automatic_purchase_id', '=', self.id), ('partner_id', '!=', False)], + fields=['partner_id'], + groupby=['partner_id'] + ) + + counter = 0 for vendor in vendor_ids: - param_header = { - 'partner_id': vendor['partner_id'][0], - # 'partner_ref': 'Automatic PO', - '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, - 'note_description': 'Automatic PO' - } + self.create_po_by_vendor(vendor['partner_id'][0]) + + # param_header = { + # 'partner_id': vendor['partner_id'][0], + # '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, + # 'note_description': 'Automatic PO' + # } + + # products_vendors = self.env['automatic.purchase.line'].search([ + # ('automatic_purchase_id', '=', self.id), + # ('partner_id', '=', vendor['partner_id'][0]), + # ('qty_purchase', '>', 0) + # ], order='brand_id') + # counter += 1 # new_po = self.env['purchase.order'].create([param_header]) - products_vendors = self.env['automatic.purchase.line'].search([ - ('automatic_purchase_id', '=', self.id), - ('partner_id', '=', vendor['partner_id'][0]), - ('qty_purchase', '>', 0) - ], 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 + "/A/"+str(counter_po_number) - self.env['automatic.purchase.match'].create([{ - 'automatic_purchase_id': self.id, - 'order_id': new_po.id - }]) - self.env.cr.commit() - # else: - # new_po = self.env['purchase.order'].create([param_header]) - brand_id = product.brand_id.id - count += 10 - - qty_available = product.product_id.qty_onhand_bandengan + product.product_id.qty_incoming_bandengan - product.product_id.outgoing_qty - suggest = 'harus beli' - if qty_available > product.qty_purchase: - suggest = 'masih cukup' + # new_po.name = new_po.name + "/A/" + str(counter) + # self.env['automatic.purchase.match'].create([{ + # 'automatic_purchase_id': self.id, + # 'order_id': new_po.id + # }]) + # self.env.cr.commit() + + # count = 0 + # for product in products_vendors: + # if count == 20: + # self.create_purchase_order_sales_match(new_po) + + # if count == 20: + # counter += 1 + # new_po = self.env['purchase.order'].create([param_header]) + # new_po.name = new_po.name + "/B/" + str(counter) + # self.env['automatic.purchase.match'].create([{ + # 'automatic_purchase_id': self.id, + # 'order_id': new_po.id + # }]) + + # self.create_purchase_order_sales_match(new_po) + # self.env.cr.commit() + + # count += 1 + # qty_available = ( + # product.product_id.qty_onhand_bandengan + + # product.product_id.qty_incoming_bandengan - + # product.product_id.outgoing_qty + # ) + # suggest = 'harus beli' if qty_available <= product.qty_purchase else 'masih cukup' + # param_line = { + # 'order_id': new_po.id, + # 'product_id': product.product_id.id, + # 'product_qty': product.qty_purchase, + # 'qty_available_store': qty_available, + # 'suggest': suggest, + # 'product_uom_qty': product.qty_purchase, + # 'price_unit': product.last_price, + # } + # new_line = self.env['purchase.order.line'].create([param_line]) + # product.current_po_id = new_po.id + # product.current_po_line_id = new_line.id + + # self.create_purchase_order_sales_match(new_po) + + self.notification = 'PO Created successfully' + self.is_po = True + + def create_po_by_vendor(self, vendor_id): + current_time = datetime.now() + + PRODUCT_PER_PO = 20 + + auto_purchase_line = self.env['automatic.purchase.line'] + + param_header = { + 'partner_id': vendor_id, + '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, + 'note_description': 'Automatic PO' + } + + domain = [ + ('automatic_purchase_id', '=', self.id), + ('partner_id', '=', vendor_id), + ('qty_purchase', '>', 0) + ] + + products_len = auto_purchase_line.search_count(domain) + page = math.ceil(products_len / PRODUCT_PER_PO) + + # 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 + "/A/" + str(i + 1) + + self.env['automatic.purchase.match'].create([{ + 'automatic_purchase_id': self.id, + 'order_id': new_po.id + }]) + + lines = auto_purchase_line.search( + domain, + offset=i * PRODUCT_PER_PO, + limit=PRODUCT_PER_PO + ) + + for line in lines: + product = line.product_id param_line = { 'order_id': new_po.id, - 'sequence': count, - 'product_id': product.product_id.id, - 'product_qty': product.qty_purchase, - 'qty_available_store': qty_available, - 'suggest': suggest, - 'product_uom_qty': product.qty_purchase, - 'price_unit': product.last_price, + 'product_id': product.id, + 'product_qty': line.qty_purchase, + 'qty_available_store': product.qty_available_bandengan, + 'suggest': product._get_po_suggest(line.qty_purchase), + 'product_uom_qty': line.qty_purchase, + 'price_unit': line.last_price, } - new_line = self.env['purchase.order.line'].create([param_line]) - product.current_po_id = new_po.id - product.current_po_line_id = new_line.id - _logger.info('Automatic Create PO Line %s' % product.product_id.name) - self.notification = self.notification + ' %s' % new_po.name - self.is_po = True + 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 + + self.create_purchase_order_sales_match(new_po) + + + def create_purchase_order_sales_match(self, purchase_order): + matches_so_product_ids = [line.product_id.id for line in purchase_order.order_line] + + matches_so = self.env['automatic.purchase.sales.match'].search([ + ('automatic_purchase_id', '=', self.id), + ('sale_line_id.product_id', 'in', matches_so_product_ids), + ]) + + for sale_order in matches_so: + matches_so_line = { + 'purchase_order_id': purchase_order.id, + 'sale_id': sale_order.sale_id.id, + 'sale_line_id': sale_order.sale_line_id.id, + 'picking_id': sale_order.picking_id.id, + 'move_id': sale_order.move_id.id, + 'partner_id': sale_order.partner_id.id, + 'partner_invoice_id': sale_order.partner_invoice_id.id, + 'salesperson_id': sale_order.salesperson_id.id, + 'product_id': sale_order.product_id.id, + 'qty_so': sale_order.qty_so, + 'qty_po': sale_order.qty_po, + } + po_matches_so_line = self.env['purchase.order.sales.match'].create([matches_so_line]) + + self.create_sales_order_purchase_match(purchase_order) + + def create_sales_order_purchase_match(self, purchase_order): + #TODO add matches po to sales order by automatic_purchase.sales.match + matches_po_product_ids = [line.product_id.id for line in purchase_order.order_line] + + sales_match_line = self.env['automatic.purchase.sales.match'].search([ + ('automatic_purchase_id', '=', self.id), + ('sale_line_id.product_id', 'in', matches_po_product_ids), + ]) + + for sales_match in sales_match_line: + purchase_line = self.env['automatic.purchase.line'].search([ + ('automatic_purchase_id', '=', self.id), + ('product_id', 'in', [sales_match.product_id.id]), + ]) + + matches_po_line = { + 'sales_order_id' : sales_match.sale_id.id, + 'purchase_order_id' : purchase_line.current_po_id.id, + 'purchase_line_id' : purchase_line.current_po_line_id.id, + 'product_id' : sales_match.product_id.id, + 'qty_so' : sales_match.qty_so, + 'qty_po' : sales_match.qty_po, + } + + sales_order_purchase_match = self.env['sales.order.purchase.match'].create([matches_po_line]) def generate_regular_purchase(self): if self.apo_type == 'reordering': |
