def process_to_channel_state(self, channel_state): """ Process the sale in tryton based on the state of order when its imported from channel :param channel_state: State on external channel the order was imported """ Sale = Pool().get('sale.sale') Shipment = Pool().get('stock.shipment.out') data = self.channel.get_tryton_action(channel_state) if data['action'] in ['process_manually', 'process_automatically']: Sale.quote([self]) Sale.confirm([self]) if data['action'] == 'process_automatically': Sale.process([self]) for shipment in self.shipments: if shipment.state == 'draft': Shipment.wait([shipment]) if shipment.state == 'waiting': Shipment.assign_try([shipment]) if data['action'] == 'import_as_past': # XXX: mark past orders as completed self.state = 'done' self.save()
def assign_try(cls, shipments): Move = Pool().get('stock.move') to_assign = [m for s in shipments for m in s.moves if m.from_location.type != 'lost_found'] if not to_assign or Move.assign_try(to_assign): cls.assign(shipments) return True else: return False
def do_create_(self, action): Product = Pool().get('product.product') ProductQuantity = Pool().get('product_quantity') Purchase = Pool().get('purchase.purchase') Company = Pool().get('company.company') Date = Pool().get('ir.date') Move = Pool().get('stock.move') Lot = Pool().get('stock.lot') today = str(Date.today()) Config = Pool().get('purchase.configuration') config = Config(1) company = Company.search([('id', '=', Transaction().context['company']) ]) currency = company[0].currency Invoice = Pool().get('account.invoice') ShipmentIn = Pool().get('stock.shipment.in') PurchaseBills = Pool().get('purchase_bills') ShipmentInReturn = Pool().get('stock.shipment.in.return') data = {} for state_name, state in self.states.iteritems(): if isinstance(state, StateView): data[state_name] = getattr(self, state_name)._default_values message = '' for each in data['start']['stock_return_lines']: if each['ensure'] == True and data['start'][ 'order_category'] == 'return': shipmentinreturn = ShipmentInReturn.search([ ('id', '=', each['return_id']), ('state', '!=', 'done') ]) if not shipmentinreturn: message = u'该单号已被处理,请退出后重新查询。' continue whether_move = Move.assign_try( [Move(shipmentinreturn[0].moves[0].id)], grouping=('product', 'lot')) if not whether_move: message += shipmentinreturn[0].moves[ 0].product.code + shipmentinreturn[0].moves[ 0].product.name + u'-批次:' + Lot( shipmentinreturn[0].moves[0].lot ).number + u'实际数量有变,请删除该行项目后重新输入\n' continue ShipmentInReturn.assign(shipmentinreturn) ShipmentInReturn.done(shipmentinreturn) if each['ensure'] == True and data['start'][ 'order_category'] == 'purchase': purchase_bills = PurchaseBills.search([('id', '=', each['purchase_id'])]) purchase = Purchase.search([ ('number', '=', purchase_bills[0].order_code) ]) # ,('state', '!=', 'processing') if not purchase: message = u'该单号已被处理,请退出后重新查询。' continue PurchaseBills.write(purchase_bills, {'state': 'done'}) Purchase.quote(purchase) Purchase.confirm(purchase) Purchase.process(purchase) moves = Move.search([ ('product', '=', purchase_bills[0].product.id), ('purchase', '=', purchase_bills[0].order_code), ('state', '=', 'draft') ]) Move.write( moves, { 'outgoing_audit': '02', 'lot': purchase_bills[0].lot.id, 'quantity': purchase_bills[0].shipment_quantity, 'move_type': '101' }) lv = {} if moves != []: lv['incoming_moves'] = [['add', [moves[0].id]]] lv['reference'] = '' lv['planned_date'] = today lv['return_shipment'] = purchase_bills[0].return_shipment lv['company'] = Transaction().context.get('company') lv['effective_date'] = None lv['cause'] = '00' lv['warehouse'] = config.warehouse.id lv['supplier'] = purchase_bills[0].party.id lv['inventory_moves'] = [] shipments = ShipmentIn.create([lv]) shipment = ShipmentIn.search([('id', '=', shipments[0].id)]) ShipmentIn.receive(shipment) ShipmentIn.done(shipment) invoices = Invoice.search([ ('state', '=', 'draft'), ('id', 'in', [ ids.id for ids in [i for i in [k.invoices for k in purchase]][0] ]) ]) Invoice.write( invoices, { 'invoice_date': purchase_bills[0].purchase_create_time, 'reference': purchase_bills[0].invoice_code, 'description': purchase_bills[0].return_shipment.number, 'amount': purchase_bills[0].amount_of_real_pay }) Invoice.validate_invoice(invoices) Invoice.validate_invoice(invoices) product_quantities = ProductQuantity.search( [('product', '=', purchase_bills[0].product.id)], order=[["sequence", "ASC"]]) move = { u'comment': u'', u'outgoing_audit': u'00', u'product': purchase_bills[0].product.id, u'from_location': config.warehouse.storage_location.id, u'invoice_lines': [], u'starts': u'05', u'move_type': '000', u'company': Transaction().context.get('company'), u'unit_price': purchase_bills[0].product.cost_price, u'currency': currency.id, u'reason': '00', u'lot': purchase_bills[0].lot.id, u'planned_date': today, u'uom': purchase_bills[0].product.default_uom.id, u'origin': None, # u'sale.line,-1', } move_quantity = purchase_bills[0].shipment_quantity for product_quantity in product_quantities: with Transaction().set_context( stock_date_end=Date.today()): # 查看具体库下面的批次对应的数量 warehouse_quant = Product.products_by_location( [product_quantity.location.id], [purchase_bills[0].product.id], with_childs=True) move['to_location'] = product_quantity.location.id warehouse_quantity = warehouse_quant[( product_quantity.location.id, purchase_bills[0].product.id)] if product_quantity.quantity - warehouse_quantity >= move_quantity: move['quantity'] = move_quantity if move['quantity'] < 0: self.raise_user_error(u'当前库存已超过最大库存!') moves = Move.create([move]) Move.do(moves) break else: move[ 'quantity'] = product_quantity.quantity - warehouse_quantity if move['quantity'] < 0: self.raise_user_error(u'当前库存已超过最大库存!') move_quantity = move_quantity + warehouse_quantity - product_quantity.quantity moves = Move.create([move]) Move.do(moves) if message: self.raise_user_error(message) return action, {}
def do_create_(self, action): Config = Pool().get('purchase.configuration') config = Config(1) # 库存地配置 ShipmentInternal = Pool().get('stock.shipment.internal') internal = Pool().get('stock.shipment.internal') MoveNumber = Pool().get('stock.move') new_return = Pool().get('hrp_new_product.new_return') Product = Pool().get('product.product') UOM = Pool().get('product.uom') data = {} for state_name, state in self.states.iteritems(): if isinstance(state, StateView): data[state_name] = getattr(self, state_name)._default_values lv = {} lv['company'] = 1 lv['starts'] = data['start']['type'] lv['to_location'] = data['start']['to_location'] lv['from_location'] = data['start']['from_location'] lv['state'] = u'draft' if data['start']['type'] == '02': states = '02' else: states = '03' Move = data['start']['moves'] list = [] for each in Move: if each['is_collar'] == True: dict = {} dict['origin'] = None # each['origin'] dict['to_location'] = data['start']['to_location'] dict['product'] = each['product'] dict['from_location'] = data['start']['from_location'] dict['invoice_lines'] = () # each['invoice_lines'] dict['company'] = 1 # each['company'] dict['unit_price'] = None # each['unit_price']产品的价格 dict['lot'] = each['lot'] # 产品批次 dict['uom'] = each['uom'] # 产品单位 dict['starts'] = states dict['real_number'] = each['proposal'] # 产品的请领数量 dict['quantity'] = each['proposal'] ProductUom = Product.search([('id', '=', each['product'])]) uom = ProductUom[0].template.default_uom cost_p = Product.search([('id', '=', each['product']) ])[0].cost_price list_p = Product.search([('id', '=', each['product']) ])[0].list_price eachuom = UOM.search([('id', '=', each['uom'])])[0] cost_prices = Uom.compute_price(uom, cost_p, eachuom) list_prices = Uom.compute_price(uom, list_p, eachuom) done_list = decimal.Decimal( str( float(list_prices * decimal.Decimal(str(each['proposal']))))) # 批发总价 list_price = decimal.Decimal(done_list).quantize( decimal.Decimal('0.00')) done_cost = decimal.Decimal( str( float(cost_prices * decimal.Decimal(str(each['proposal']))))) # 零售总价 cost_price = decimal.Decimal(done_cost).quantize( decimal.Decimal('0.00')) dict['list_price'] = list_price dict['cost_price'] = cost_price list.append(dict) lv['moves'] = [['create', list]] lv['planned_date'] = data['start']['planned_date'] hrp_new_return = new_return.search([ ('product', '=', each['product']), ('to_location', '=', data['start']['to_location']), ('from_location', '=', data['start']['from_location']), ]) if hrp_new_return: return_dicts = {} number = hrp_new_return[0].return_quantity if number == None: number = 0 Numbe = number + each['proposal'] return_dicts['return_quantity'] = Numbe new_return.write(hrp_new_return, return_dicts) else: if data['start']['type'] == '03': pass else: return_dict = {} return_dict['return_quantity'] = each[ 'proposal'] # 产品的冻结/非限制数量 return_dict['to_location'] = data['start'][ 'to_location'] return_dict['code'] = each['code'] return_dict['product'] = each['product'] return_dict['drug_specifications'] = each[ 'drug_specifications'] return_dict['from_location'] = data['start'][ 'from_location'] if data['start'][ 'from_location'] == config.warehouse.storage_location.id: return_dict['examine'] = '00' else: return_dict['examine'] = '01' return_dict['drug_type'] = each['drug_type'] return_dict['retrieve_the_code'] = each[ 'retrieve_the_code'] return_dict['lot'] = each['lot'] # 产品批次 # return_dict['list_price'] = done_list # return_dict['cost_price'] = done_cost ProductUom = Product.search([('id', '=', each['product'])]) uom = ProductUom[0].template.default_uom.id return_dict['uom'] = uom new_return.create([return_dict]) # 创建到退药的表 internal.create([lv]) Internal = internal.search([ ('to_location', '=', data['start']['to_location']), ('from_location', '=', data['start']['from_location']), ('planned_date', '=', data['start']['planned_date']), ('starts', '=', data['start']['type']), ]) moves1 = Internal[0].moves move_id = [] for i in moves1: move_id.append(i.id) move_ = MoveNumber.search([('id', 'in', move_id)]) move_number = MoveNumber.assign_try(move_, grouping=('product', 'lot')) ShipmentInternal.wait(Internal) ShipmentInternal.assign(Internal) ShipmentInternal.done(Internal) if move_number == True: ShipmentInternal.wait(Internal) ShipmentInternal.assign(Internal) ShipmentInternal.done(Internal) else: product_name = move_[0].product.name product_lot = move_[0].lot.number return self.raise_user_error(product_name + u',批次:' + product_lot + u'实际数量有变 请删除该行项目后 重新输入') return action, {}