diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-11-10 13:40:58 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-11-10 13:40:58 +0700 |
| commit | 2aee5a44abbe36961dfe23cc3d656aa48e11e0f9 (patch) | |
| tree | 8ec2b6552aaef4e14539aa52ed796552e24180d6 /indoteknik_custom/models/automatic_purchase.py | |
| parent | 6a87e59e7220bdfa78e98b23003ccc4ef41bd0ce (diff) | |
| parent | b4e74170aeaf00937f78e5af9047218ddb17516c (diff) | |
Merge branch 'production' into change/feature/promotion-program
Diffstat (limited to 'indoteknik_custom/models/automatic_purchase.py')
| -rw-r--r-- | indoteknik_custom/models/automatic_purchase.py | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/indoteknik_custom/models/automatic_purchase.py b/indoteknik_custom/models/automatic_purchase.py index e21b411d..502761e0 100644 --- a/indoteknik_custom/models/automatic_purchase.py +++ b/indoteknik_custom/models/automatic_purchase.py @@ -17,6 +17,7 @@ class AutomaticPurchase(models.Model): is_po = fields.Boolean(string='Is PO') purchase_match = fields.One2many('automatic.purchase.match', 'automatic_purchase_id', string='Matches', auto_join=True) vendor_id = fields.Many2one('res.partner', string='Vendor', help='boleh kosong, jika diisi, maka hanya keluar data untuk vendor tersebut') + responsible_id = fields.Many2one('res.users', string='Responsible', required=True) def create_po_from_automatic_purchase(self): if not self.purchase_lines: @@ -30,12 +31,13 @@ class AutomaticPurchase(models.Model): for vendor in vendor_ids: param_header = { 'partner_id': vendor['partner_id'][0], - 'partner_ref': 'Automatic PO', + # '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 + 'date_order': current_time, + 'note_description': 'Automatic PO' } # new_po = self.env['purchase.order'].create([param_header]) products_vendors = self.env['automatic.purchase.line'].search([ @@ -86,7 +88,8 @@ class AutomaticPurchase(models.Model): raise UserError('Sudah digenerate sebelumnya, hapus line terlebih dahulu') query = [ - ('product_min_qty', '>', 0) + ('product_min_qty', '>', 0), + ('product_id.x_manufacture.user_id.id', '=', self.responsible_id.id) ] orderpoints = self.env['stock.warehouse.orderpoint'].search(query) count = 0 @@ -100,13 +103,20 @@ class AutomaticPurchase(models.Model): if self.vendor_id: purchase_price = self.env['purchase.pricelist'].search([ ('product_id', '=', point.product_id.id), + # ('product_id.x_manufacture.user_id.id', '=', self.responsible_id.id), ('vendor_id', '=', self.vendor_id.id) - ], order='product_price asc', limit=1) + ], order='count_trx_po desc, count_trx_po_vendor desc', limit=1) else: - purchase_price = self.env['purchase.pricelist'].search([('product_id', '=', point.product_id.id)], order='product_price asc', limit=1) + purchase_price = self.env['purchase.pricelist'].search([ + ('product_id', '=', point.product_id.id), + # ('product_id.x_manufacture.user_id.id', '=', self.responsible_id.id), + ], order='count_trx_po desc, count_trx_po_vendor desc', limit=1) vendor_id = purchase_price.vendor_id.id - price = purchase_price.product_price or 0 + price = self._get_valid_purchase_price(purchase_price) + + if self.vendor_id and self.vendor_id.id != vendor_id: + continue self.env['automatic.purchase.line'].create([{ 'automatic_purchase_id': self.id, @@ -128,6 +138,19 @@ class AutomaticPurchase(models.Model): _logger.info('Create Automatic Purchase Line %s' % point.product_id.name) self.notification = "Automatic PO Created %s Lines" % count + def _get_valid_purchase_price(self, purchase_price): + p_price = 0 + if purchase_price.system_price > 0 and purchase_price.product_price > 0: + if purchase_price.human_last_update > purchase_price.system_last_update: + p_price = purchase_price.product_price + else: + p_price = purchase_price.system_price + elif purchase_price.system_price > 0 and purchase_price.product_price == 0: + p_price = purchase_price.system_price + elif purchase_price.system_price == 0 and purchase_price.product_price > 0: + p_price = purchase_price.product_price + return p_price + class AutomaticPurchaseLine(models.Model): _name = 'automatic.purchase.line' |
