def getVATAmount(self): """ Compute VATAmount """ try: vatamount = self.getTotalPrice() - Decimal(self.getPrice()) except: vatamount = Decimal('0.00') return vatamount.quantize(Decimal('0.00'))
def getTotal(self): """ Compute TotalPrice """ total = 0 for lineitem in self.supplyorder_lineitems: total += Decimal(lineitem['Quantity']) * \ Decimal(lineitem['Price']) * \ ((Decimal(lineitem['VAT']) /100) + 1) return total
def getTotalPrice(self): """ compute total price """ price = self.getPrice() price = Decimal(price or '0.00') vat = Decimal(self.getVAT()) price = price and price or 0 vat = vat and vat / 100 or 0 price = price + (price * vat) return price.quantize(Decimal('0.00'))
def getVATAmount(self): """ Compute VAT """ for record in self: record.VATAmount = Decimal(record.getTotal()) - Decimal( record.getSubtotal())
def getVATAmount(self): """ Compute VAT """ return Decimal(self.getTotal()) - Decimal(self.getSubtotal())
def set(self, instance, service_uids, prices=None, specs=None, **kwargs): """Set the 'Analyses' field value, by creating and removing Analysis objects from the AR. service_uids is a list: The UIDs of all services which should exist in the AR. If a service is not included here, the corrosponding Analysis will be removed. prices is a dictionary: key = AnalysisService UID value = price specs is a dictionary: key = AnalysisService UID value = dictionary: defined in ResultsRange field definition """ if not service_uids: return assert type(service_uids) in (list, tuple) bsc = getToolByName(instance, 'bika_setup_catalog') workflow = getToolByName(instance, 'portal_workflow') # one can only edit Analyses up to a certain state. ar_state = workflow.getInfoFor(instance, 'review_state', '') assert ar_state in ('sample_registered', 'sampled', 'to_be_sampled', 'to_be_preserved', 'sample_due', 'sample_received', 'attachment_due', 'to_be_verified') # - Modify existing AR specs with new form values for selected analyses. # - new analysis requests are also using this function, so ResultsRange # may be undefined. in this case, specs= will contain the entire # AR spec. rr = instance.getResultsRange() specs = specs if specs else [] for s in specs: s_in_rr = False for i, r in enumerate(rr): if s['keyword'] == r['keyword']: rr[i].update(s) s_in_rr = True if not s_in_rr: rr.append(s) instance.setResultsRange(rr) new_analyses = [] proxies = bsc(UID=service_uids) for proxy in proxies: service = proxy.getObject() service_uid = service.UID() keyword = service.getKeyword() price = prices[service_uid] if prices and service_uid in prices \ else service.getPrice() vat = Decimal(service.getVAT()) # analysis->InterimFields calc = service.getCalculation() interim_fields = calc and list(calc.getInterimFields()) or [] # override defaults from service->InterimFields service_interims = service.getInterimFields() sif = dict([(x['keyword'], x.get('value', '')) for x in service_interims]) for i, i_f in enumerate(interim_fields): if i_f['keyword'] in sif: interim_fields[i]['value'] = sif[i_f['keyword']] service_interims = [x for x in service_interims if x['keyword'] != i_f['keyword']] # Add remaining service interims to the analysis for v in service_interims: interim_fields.append(v) # create the analysis if it doesn't exist if shasattr(instance, keyword): analysis = instance._getOb(keyword) else: analysis = create_analysis( instance, service, keyword, interim_fields ) new_analyses.append(analysis) for i, r in enumerate(rr): if r['keyword'] == analysis.getService().getKeyword(): r['uid'] = analysis.UID() # XXX Price? # analysis.setPrice(price) # We add rr to the AR after we create all the analyses instance.setResultsRange(rr) # delete analyses delete_ids = [] for analysis in instance.objectValues('Analysis'): service_uid = analysis.Schema()['Service'].getRaw(analysis) if service_uid not in service_uids: # If it is verified or published, don't delete it. if workflow.getInfoFor(analysis, 'review_state') in ('verified', 'published'): continue # log it # If it is assigned to a worksheet, unassign it before deletion. elif workflow.getInfoFor(analysis, 'worksheetanalysis_review_state') == 'assigned': ws = analysis.getBackReferences("WorksheetAnalysis")[0] ws.removeAnalysis(analysis) # Unset the partition reference analysis.edit(SamplePartition=None) delete_ids.append(analysis.getId()) if delete_ids: # Note: subscriber might promote the AR instance.manage_delObjects(ids=delete_ids) return new_analyses
def __call__(self): context = self.context workflow = getToolByName(context, 'portal_workflow') # Collect related data and objects invoice = context.getInvoice() sample = context.getSample() samplePoint = sample.getSamplePoint() reviewState = workflow.getInfoFor(context, 'review_state') # Collection invoice information if invoice: self.invoiceId = invoice.getId() else: self.invoiceId = _('Proforma (Not yet invoiced)') # Collect verified invoice information verified = reviewState in VERIFIED_STATES if verified: self.verifiedBy = context.getVerifier() self.verified = verified self.request['verified'] = verified # Collect published date datePublished = context.getDatePublished() if datePublished is not None: datePublished = self.ulocalized_time(datePublished, long_format=1) self.datePublished = datePublished # Collect received date dateReceived = context.getDateReceived() if dateReceived is not None: dateReceived = self.ulocalized_time(dateReceived, long_format=1) self.dateReceived = dateReceived # Collect general information self.reviewState = reviewState contact = context.getContact() self.contact = contact.Title() if contact else "" self.clientOrderNumber = context.getClientOrderNumber() self.clientReference = context.getClientReference() self.clientSampleId = sample.getClientSampleID() self.sampleType = sample.getSampleType().Title() self.samplePoint = samplePoint and samplePoint.Title() self.requestId = context.getRequestID() self.headers = [ { 'title': 'Invoice ID', 'value': self.invoiceId }, { 'title': 'Client Reference', 'value': self.clientReference }, { 'title': 'Sample Type', 'value': self.sampleType }, { 'title': 'Request ID', 'value': self.requestId }, { 'title': 'Date Received', 'value': self.dateReceived }, ] if not isAttributeHidden('AnalysisRequest', 'ClientOrderNumber'): self.headers.append({ 'title': 'Client Sample Id', 'value': self.clientOrderNumber }) if not isAttributeHidden('AnalysisRequest', 'SamplePoint'): self.headers.append({ 'title': 'Sample Point', 'value': self.samplePoint }) if self.verified: self.headers.append({ 'title': 'Verified By', 'value': self.verifiedBy }) if self.datePublished: self.headers.append({ 'title': 'datePublished', 'value': self.datePublished }) # <tal:published tal:condition="view/datePublished"> # <th i18n:translate="">Date Published</th> # <td tal:content="view/datePublished"></td> # </tal:published> #</tr> analyses = [] profiles = [] # Retrieve required data from analyses collection all_analyses, all_profiles, analyses_from_profiles = context.getServicesAndProfiles( ) # Relating category with solo analysis for analysis in all_analyses: service = analysis.getService() categoryName = service.getCategory().Title() # Find the category try: category = (o for o in analyses if o['name'] == categoryName).next() except: category = {'name': categoryName, 'analyses': []} analyses.append(category) # Append the analysis to the category category['analyses'].append({ 'title': analysis.Title(), 'price': analysis.getPrice(), 'priceVat': "%.2f" % analysis.getVATAmount(), 'priceTotal': "%.2f" % analysis.getTotalPrice(), }) # Relating analysis services with their profiles # We'll take the analysis contained on each profile for profile in all_profiles: # If profile's checkbox "Use Analysis Profile Price" is enabled, only the profile price will be displayed. # Otherwise each analysis will display its own price. pservices = [] if profile.getUseAnalysisProfilePrice(): # We have to use the profiles price only for pservice in profile.getService(): pservices.append({ 'title': pservice.Title(), 'price': None, 'priceVat': None, 'priceTotal': None, }) profiles.append({ 'name': profile.title, 'price': profile.getAnalysisProfilePrice(), 'priceVat': profile.getVATAmount(), 'priceTotal': profile.getTotalPrice(), 'analyses': pservices }) else: # We need the analyses prices instead of profile price for pservice in profile.getService(): # We want the analysis instead of the service, because we want the price for the client # (for instance the bulk price) panalysis = self._getAnalysisForProfileService( pservice.getKeyword(), analyses_from_profiles) pservices.append({ 'title': pservice.Title(), 'price': panalysis.getPrice() if panalysis else pservice.getPrice(), 'priceVat': "%.2f" % panalysis.getVATAmount() if panalysis else pservice.getVATAmount(), 'priceTotal': "%.2f" % panalysis.getTotalPrice() if panalysis else pservice.getTotalPrice(), }) profiles.append({ 'name': profile.title, 'price': None, 'priceVat': None, 'priceTotal': None, 'analyses': pservices }) self.analyses = analyses self.profiles = profiles # Get subtotals self.subtotal = context.getSubtotal() self.subtotalVATAmount = "%.2f" % context.getSubtotalVATAmount() self.subtotalTotalPrice = "%.2f" % context.getSubtotalTotalPrice() # Get totals self.memberDiscount = Decimal(context.getDefaultMemberDiscount()) self.discountAmount = context.getDiscountAmount() self.VATAmount = "%.2f" % context.getVATAmount() self.totalPrice = "%.2f" % context.getTotalPrice() # Render the template return self.template()
def getSubtotal(self): """ Compute Subtotal """ if self.supplyorder_lineitems: return sum( [(Decimal(obj['Quantity']) * Decimal(obj['Price'])) for obj in self.supplyorder_lineitems]) return 0