/
item.py
871 lines (744 loc) · 32 KB
/
item.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import frappe
from frappe import msgprint, _
from frappe.utils import cstr, flt, cint, getdate, now_datetime, formatdate
from frappe.website.website_generator import WebsiteGenerator
from erpnext.setup.doctype.item_group.item_group import invalidate_cache_for, get_parent_item_groups
from frappe.website.render import clear_cache
from frappe.website.doctype.website_slideshow.website_slideshow import get_slideshow
import copy
class WarehouseNotSet(frappe.ValidationError): pass
class DuplicateVariant(frappe.ValidationError): pass
class ItemTemplateCannotHaveStock(frappe.ValidationError): pass
class Item(WebsiteGenerator):
website = frappe._dict(
page_title_field = "item_name",
condition_field = "show_in_website",
template = "templates/generators/item.html",
parent_website_route_field = "item_group",
)
def onload(self):
super(Item, self).onload()
self.get("__onload").sle_exists = self.check_if_sle_exists()
def autoname(self):
if(self.item_code=="000"):
self.item_code_gen()
#frappe.msgprint(self.item_code+"Auto name",raise_exception=1)
if frappe.db.get_default("item_naming_by")=="Naming Series" and not self.variant_of:
from frappe.model.naming import make_autoname
self.item_code = make_autoname(self.naming_series+'.#####')
elif not self.item_code:
msgprint(_("Item Code is mandatory because Item is not automatically numbered"), raise_exception=1)
self.name = self.item_code
self.item_code=self.name
def before_insert(self):
if self.is_sales_item=="Yes":
self.publish_in_hub = 1
def after_insert(self):
frappe.db.sql("""update tabItem set item_code=%s where name=%s""",(self.name,self.name))
if self.variant_of:
name_variants=self.name
name_variants_split=name_variants.split("-")
new_name=name_variants_split[1]
prefix_variant=self.prefix+new_name
#frappe.msgprint(prefix_variant)
frappe.db.sql("""update tabItem set prefix=%s where name=%s""",(prefix_variant,name_variants))
def validate(self):
super(Item, self).validate()
#self.item_code_gen()
#frappe.msgprint(self.item_code,raise_exception=1)
if not self.stock_uom:
msgprint(_("Please enter default Unit of Measure"), raise_exception=1)
if self.image and not self.website_image:
self.website_image = self.image
if self.variant_of:
self.copy_attributes_to_variant(frappe.get_doc("Item", self.variant_of), self)
self.check_warehouse_is_set_for_stock_item()
self.check_stock_uom_with_bin()
self.add_default_uom_in_conversion_factor_table()
self.validate_conversion_factor()
self.validate_item_type()
self.check_for_active_boms()
#self.fill_customer_code()
self.check_item_tax()
self.validate_barcode()
self.cant_change()
self.validate_reorder_level()
self.validate_warehouse_for_reorder()
self.validate_variants()
self.update_item_desc()
self.synced_with_hub = 0
self.customer_code_update()
if not self.get("__islocal"):
self.old_item_group = frappe.db.get_value(self.doctype, self.name, "item_group")
self.old_website_item_groups = frappe.db.sql_list("""select item_group from `tabWebsite Item Group`
where parentfield='website_item_groups' and parenttype='Item' and parent=%s""", self.name)
def on_update(self):
super(Item, self).on_update()
invalidate_cache_for_item(self)
self.validate_name_with_item_group()
self.update_item_price()
self.sync_variants()
def get_context(self, context):
context["parent_groups"] = get_parent_item_groups(self.item_group) + \
[{"name": self.name}]
if self.slideshow:
context.update(get_slideshow(self))
context["parents"] = self.get_parents(context)
return context
def check_warehouse_is_set_for_stock_item(self):
if self.is_stock_item=="Yes" and not self.default_warehouse and frappe.get_all("Warehouse"):
frappe.msgprint(_("Default Warehouse is mandatory for stock Item."),
raise_exception=WarehouseNotSet)
def item_code_gen(self):
#item code
'''
Item Group first letter +
Item Division Number +
Customer Short Name +
Product Short Name +
package category first letter +
Category Number +
Subcategory Number +
Running Number
'''
#group
if(self.material_type=="Item"):
item_group=self.item_group
item_group_first = item_group[0]
#division
item_division=self.item_division
auto_number_div= frappe.db.get_value("Item Division", item_division, "auto_number")
if auto_number_div==None:
auto_number_div=0;
#customer
customer_name=self.customer_name;
short_cust= frappe.db.get_value("Customer", customer_name, "short_name")
if short_cust == None:
frappe.msgprint("Customer Short Name Not Set for <b>"+customer_name+"</b> So Please Set Customer Short Name",raise_exception=1);
#product
pro_name=self.product
if(pro_name):
short_product= frappe.db.get_value("Item", pro_name, "product_short_name")
else:
short_product=""
if short_product == None:
frappe.msgprint("Product Short Name Not Set for <b>"+pro_name+"</b> So Please Set Product Short Name",raise_exception=1);
#if(!(short_product)):
#frappe.msgprint("Enter Product Short Name for "+pro_name,raise_exception=1)
#package
package_category=self.package_category
if(package_category):
package_category_letter = package_category[0]
else:
package_category_letter=""
#frappe.msgprint("Product Short Name Not Set for "+pro_name+" So Please Set Product Short Name",raise_exception=1);
#category
category=self.item_category
if(category):
#frappe.msgprint(category,raise_exception=1)
auto_number_cat= frappe.db.get_value("Item Category", category, "auto_number")
if auto_number_cat==None:
auto_number_cat=0;
auto_number_cat=int(auto_number_cat);
if(auto_number_cat<10):
auto_number_cat="0"+str(auto_number_cat)
else:
auto_number_cat="00"
#subcategory
subcategory=self.item_sub_category
if(subcategory):
#frappe.msgprint(subcategory,raise_exception=1);
auto_number_sub= frappe.db.get_value("Item Subcategory", subcategory, "auto_number")
if auto_number_sub==None:
auto_number_sub=0;
auto_number_sub=int(auto_number_sub);
if(auto_number_sub<10):
auto_number_sub="0"+str(auto_number_sub)
else:
auto_number_sub="00"
#prefix
prefix=str(item_group_first)+str(auto_number_div)+str(short_cust)+str(short_product)+str(package_category_letter)+str(auto_number_cat)+str(auto_number_sub)
#runing number
count_rows=frappe.db.count("Item",filters={'prefix':prefix,'material_type':"Item"})
count_rows=count_rows+1;
if(count_rows>=0 and count_rows<10):
str_num="000";
elif(count_rows>=10 and count_rows<100):
str_num="00"
elif(count_rows>=100 and count_rows<1000):
str_num="0"
else:
str_num="";
item_code=prefix+str_num+str(count_rows);
item_code=item_code.upper();
prefix=prefix.upper();
self.item_code=item_code
self.prefix=prefix
self.barcode=item_code
if(self.material_type=="Product"):
item_group=self.item_group
item_groups=item_group.split(" ")
if(len(item_groups)>=2):
item_groups_one=item_groups[0];
item_groups_two=item_groups[1];
item_group_first_letter = item_groups_one[0]
item_group_second_letter = item_groups_two[0]
if(len(item_groups)<2):
item_groups_one=item_groups[0];
item_group_first_letter = item_groups_one[0]
item_group_second_letter = item_groups_one[1]
item_group_letters=str(item_group_first_letter)+str(item_group_second_letter)
#division
item_division=self.item_division
auto_number_div= frappe.db.get_value("Item Division", item_division, "auto_number")
if auto_number_div==None:
auto_number_div=0;
#customer
customer_name=self.customer_name;
short_cust= frappe.db.get_value("Customer", customer_name, "short_name")
if short_cust == None:
frappe.msgprint("Customer Short Name Not Set for <b>"+customer_name+"</b> So Please Set Customer Short Name",raise_exception=1);
#product_short_name
product_short_name=self.product_short_name
#prefix
prefix=str(item_group_letters)+str(auto_number_div)+str(short_cust)+str(product_short_name)
count_rows=frappe.db.count("Item",filters={'prefix':prefix,'material_type':"Product"})
count_rows=count_rows+1;
if(count_rows>=0 and count_rows<10):
str_num="0";
else:
str_num="";
prefix=prefix.upper();
item_code=prefix+str_num+str(count_rows);
item_code=item_code.upper();
self.item_code=item_code
self.prefix=prefix
self.barcode=item_code
#frappe.msgprint("this is item code check",raise_exception=1)
def add_default_uom_in_conversion_factor_table(self):
uom_conv_list = [d.uom for d in self.get("uoms")]
if self.stock_uom not in uom_conv_list:
ch = self.append('uoms', {})
ch.uom = self.stock_uom
ch.conversion_factor = 1
to_remove = []
for d in self.get("uoms"):
if d.conversion_factor == 1 and d.uom != self.stock_uom:
to_remove.append(d)
[self.remove(d) for d in to_remove]
def check_stock_uom_with_bin(self):
if not self.get("__islocal"):
matched=True
ref_uom = frappe.db.get_value("Stock Ledger Entry",
{"item_code": self.name}, "stock_uom")
if ref_uom:
if cstr(ref_uom) != cstr(self.stock_uom):
matched = False
else:
bin_list = frappe.db.sql("select * from tabBin where item_code=%s",
self.item_code, as_dict=1)
for bin in bin_list:
if (bin.reserved_qty > 0 or bin.ordered_qty > 0 or bin.indented_qty > 0 \
or bin.planned_qty > 0) and cstr(bin.stock_uom) != cstr(self.stock_uom):
matched = False
break
if matched and bin_list:
frappe.db.sql("""update tabBin set stock_uom=%s where item_code=%s""",
(self.stock_uom, self.name))
if not matched:
frappe.throw(_("Default Unit of Measure can not be changed directly because you have already made some transaction(s) with another UOM. To change default UOM, use 'UOM Replace Utility' tool under Stock module."))
def validate_variants(self):
self.validate_variants_are_unique()
self.validate_stock_for_template_must_be_zero()
def validate_stock_for_template_must_be_zero(self):
if self.has_variants:
stock_in = frappe.db.sql_list("""select warehouse from tabBin
where item_code=%s and ifnull(actual_qty, 0) > 0""", self.name)
if stock_in:
frappe.throw(_("Item Template cannot have stock and varaiants. Please remove stock from warehouses {0}").format(", ".join(stock_in)),
ItemTemplateCannotHaveStock)
def validate_variants_are_unique(self):
if not self.has_variants:
self.variants = []
return
if self.variants:
if self.variant_of:
frappe.throw(_("Item cannot be a variant of a variant"))
variants, attributes = [], {}
for d in self.variants:
key = (d.item_attribute, d.item_attribute_value)
if key in variants:
frappe.throw(_("{0} {1} is entered more than once in Item Variants table")
.format(d.item_attribute, d.item_attribute_value), DuplicateVariant)
variants.append(key)
attributes.setdefault(d.item_attribute, [t.attribute_value for t in frappe.db.get_all("Item Attribute Value",
fields=["attribute_value"], filters={"parent": d.item_attribute })])
if d.item_attribute_value not in attributes.get(d.item_attribute):
frappe.throw(_("Attribute value {0} does not exist in Item Attribute Master.").format(d.item_attribute_value))
else:
frappe.throw(_("Please enter atleast one attribute row in Item Variants table"))
def sync_variants(self):
variant_item_codes = self.get_variant_item_codes()
# delete missing variants
existing_variants = [d.name for d in frappe.get_all("Item",
filters={"variant_of":self.name})]
updated, deleted = [], []
for existing_variant in existing_variants:
if existing_variant not in variant_item_codes:
frappe.delete_doc("Item", existing_variant)
deleted.append(existing_variant)
else:
self.update_variant(existing_variant)
updated.append(existing_variant)
inserted = []
for item_code in variant_item_codes:
if item_code not in existing_variants:
self.make_variant(item_code)
inserted.append(item_code)
if inserted:
frappe.msgprint(_("Item Variants {0} created").format(", ".join(inserted)))
if updated:
frappe.msgprint(_("Item Variants {0} updated").format(", ".join(updated)))
if deleted:
frappe.msgprint(_("Item Variants {0} deleted").format(", ".join(deleted)))
def get_variant_item_codes(self):
"""Get all possible suffixes for variants"""
if not self.variants:
return []
self.variant_attributes = {}
variant_dict = {}
variant_item_codes = []
for d in self.variants:
variant_dict.setdefault(d.item_attribute, []).append(d.item_attribute_value)
all_attributes = [d.name for d in frappe.get_all("Item Attribute", order_by = "priority asc")]
# sort attributes by their priority
attributes = filter(None, map(lambda d: d if d in variant_dict else None, all_attributes))
def add_attribute_suffixes(item_code, my_attributes, attributes):
attr = frappe.get_doc("Item Attribute", attributes[0])
for value in attr.item_attribute_values:
if value.attribute_value in variant_dict[attr.name]:
_my_attributes = copy.deepcopy(my_attributes)
_my_attributes.append([attr.name, value.attribute_value])
if len(attributes) > 1:
add_attribute_suffixes(item_code + "-" + value.abbr, _my_attributes, attributes[1:])
else:
variant_item_codes.append(item_code + "-" + value.abbr)
self.variant_attributes[item_code + "-" + value.abbr] = _my_attributes
add_attribute_suffixes(self.name, [], attributes)
return variant_item_codes
def make_variant(self, item_code):
item = frappe.new_doc("Item")
item.item_code = item_code
self.copy_attributes_to_variant(self, item, insert=True)
item.insert()
def update_variant(self, item_code):
item = frappe.get_doc("Item", item_code)
item.item_code = item_code
self.copy_attributes_to_variant(self, item)
item.save()
def copy_attributes_to_variant(self, template, variant, insert=False):
from frappe.model import no_value_fields
for field in self.meta.fields:
if field.fieldtype not in no_value_fields and (insert or not field.no_copy)\
and field.fieldname not in ("item_code", "item_name"):
if variant.get(field.fieldname) != template.get(field.fieldname):
variant.set(field.fieldname, template.get(field.fieldname))
variant.__dirty = True
variant.description += "\n"
if not getattr(template, "variant_attributes", None):
template.get_variant_item_codes()
for attr in template.variant_attributes[variant.item_code]:
variant.description += "<p>" + attr[0] + ": " + attr[1] + "</p>"
variant.item_name = self.item_name + variant.item_code[len(self.name):]
variant.variant_of = template.name
variant.has_variants = 0
variant.show_in_website = 0
def update_template_tables(self):
template = frappe.get_doc("Item", self.variant_of)
# add item taxes from template
for d in template.get("taxes"):
self.append("taxes", {"tax_type": d.tax_type, "tax_rate": d.tax_rate})
# copy re-order table if empty
if not self.get("reorder_levels"):
for d in template.get("reorder_levels"):
n = {}
for k in ("warehouse", "warehouse_reorder_level",
"warehouse_reorder_qty", "material_request_type"):
n[k] = d.get(k)
self.append("reorder_levels", n)
def validate_conversion_factor(self):
check_list = []
for d in self.get('uoms'):
if cstr(d.uom) in check_list:
frappe.throw(_("Unit of Measure {0} has been entered more than once in Conversion Factor Table").format(d.uom))
else:
check_list.append(cstr(d.uom))
if d.uom and cstr(d.uom) == cstr(self.stock_uom) and flt(d.conversion_factor) != 1:
frappe.throw(_("Conversion factor for default Unit of Measure must be 1 in row {0}").format(d.idx))
def validate_item_type(self):
if self.is_pro_applicable == 'Yes' and self.is_stock_item == 'No':
frappe.throw(_("As Production Order can be made for this item, it must be a stock item."))
if self.has_serial_no == 'Yes' and self.is_stock_item == 'No':
msgprint(_("'Has Serial No' can not be 'Yes' for non-stock item"), raise_exception=1)
if self.has_serial_no == "No" and self.serial_no_series:
self.serial_no_series = None
def check_for_active_boms(self):
if self.default_bom:
bom_item = frappe.db.get_value("BOM", self.default_bom, "item")
if bom_item not in (self.name, self.variant_of):
frappe.throw(_("Default BOM ({0}) must be active for this item or its template").format(bom_item))
if self.is_purchase_item != "Yes":
bom_mat = frappe.db.sql("""select distinct t1.parent
from `tabBOM Item` t1, `tabBOM` t2 where t2.name = t1.parent
and t1.item_code =%s and ifnull(t1.bom_no, '') = '' and t2.is_active = 1
and t2.docstatus = 1 and t1.docstatus =1 """, self.name)
if bom_mat and bom_mat[0][0]:
frappe.throw(_("Item must be a purchase item, as it is present in one or many Active BOMs"))
def fill_customer_code(self):
""" Append all the customer codes and insert into "customer_code" field of item table """
cust_code=[]
for d in self.get('customer_items'):
cust_code.append(d.ref_code)
self.customer_code=','.join(cust_code)
def check_item_tax(self):
"""Check whether Tax Rate is not entered twice for same Tax Type"""
check_list=[]
for d in self.get('taxes'):
if d.tax_type:
account_type = frappe.db.get_value("Account", d.tax_type, "account_type")
if account_type not in ['Tax', 'Chargeable', 'Income Account', 'Expense Account']:
frappe.throw(_("Item Tax Row {0} must have account of type Tax or Income or Expense or Chargeable").format(d.idx))
else:
if d.tax_type in check_list:
frappe.throw(_("{0} entered twice in Item Tax").format(d.tax_type))
else:
check_list.append(d.tax_type)
def validate_barcode(self):
self.barcode=self.name;
if self.barcode:
duplicate = frappe.db.sql("""select name from tabItem where barcode = %s
and name != %s""", (self.barcode, self.name))
if duplicate:
frappe.throw(_("Barcode {0} already used in Item {1}").format(self.barcode, duplicate[0][0]))
def cant_change(self):
if not self.get("__islocal"):
vals = frappe.db.get_value("Item", self.name,
["has_serial_no", "is_stock_item", "valuation_method", "has_batch_no"], as_dict=True)
if vals and ((self.is_stock_item == "No" and vals.is_stock_item == "Yes") or
vals.has_serial_no != self.has_serial_no or
vals.has_batch_no != self.has_batch_no or
cstr(vals.valuation_method) != cstr(self.valuation_method)):
if self.check_if_sle_exists() == "exists":
frappe.throw(_("As there are existing stock transactions for this item, you can not change the values of 'Has Serial No', 'Has Batch No', 'Is Stock Item' and 'Valuation Method'"))
def validate_reorder_level(self):
if cint(self.apply_warehouse_wise_reorder_level):
self.re_order_level, self.re_order_qty = 0, 0
else:
self.set("reorder_levels", [])
if self.re_order_level or len(self.get("reorder_levels", {"material_request_type": "Purchase"})):
if not self.is_purchase_item:
frappe.throw(_("""To set reorder level, item must be a Purchase Item"""))
def validate_warehouse_for_reorder(self):
warehouse = []
for i in self.get("reorder_levels"):
if i.get("warehouse") and i.get("warehouse") not in warehouse:
warehouse += [i.get("warehouse")]
else:
frappe.throw(_("Row {0}: An Reorder entry already exists for this warehouse {1}")
.format(i.idx, i.warehouse))
def check_if_sle_exists(self):
sle = frappe.db.sql("""select name from `tabStock Ledger Entry`
where item_code = %s""", self.name)
return sle and 'exists' or 'not exists'
def validate_name_with_item_group(self):
# causes problem with tree build
if frappe.db.exists("Item Group", self.name):
frappe.throw(_("An Item Group exists with same name, please change the item name or rename the item group"))
def update_item_price(self):
frappe.db.sql("""update `tabItem Price` set item_name=%s,
item_description=%s, modified=NOW() where item_code=%s""",
(self.item_name, self.description, self.name))
def get_tax_rate(self, tax_type):
return { "tax_rate": frappe.db.get_value("Account", tax_type, "tax_rate") }
def on_trash(self):
super(Item, self).on_trash()
frappe.db.sql("""delete from tabBin where item_code=%s""", self.item_code)
frappe.db.sql("delete from `tabItem Price` where item_code=%s", self.name)
for variant_of in frappe.get_all("Item", filters={"variant_of": self.name}):
frappe.delete_doc("Item", variant_of.name)
def before_rename(self, olddn, newdn, merge=False):
if merge:
# Validate properties before merging
if not frappe.db.exists("Item", newdn):
frappe.throw(_("Item {0} does not exist").format(newdn))
field_list = ["stock_uom", "is_stock_item", "has_serial_no", "has_batch_no"]
new_properties = [cstr(d) for d in frappe.db.get_value("Item", newdn, field_list)]
if new_properties != [cstr(self.get(fld)) for fld in field_list]:
frappe.throw(_("To merge, following properties must be same for both items")
+ ": \n" + ", ".join([self.meta.get_label(fld) for fld in field_list]))
frappe.db.sql("delete from `tabBin` where item_code=%s", olddn)
def after_rename(self, olddn, newdn, merge):
super(Item, self).after_rename(olddn, newdn, merge)
if self.page_name:
invalidate_cache_for_item(self)
clear_cache(self.page_name)
frappe.db.set_value("Item", newdn, "item_code", newdn)
if merge:
self.set_last_purchase_rate(newdn)
self.recalculate_bin_qty(newdn)
def set_last_purchase_rate(self, newdn):
last_purchase_rate = get_last_purchase_details(newdn).get("base_rate", 0)
frappe.db.set_value("Item", newdn, "last_purchase_rate", last_purchase_rate)
def recalculate_bin_qty(self, newdn):
from erpnext.utilities.repost_stock import repost_stock
frappe.db.auto_commit_on_many_writes = 1
existing_allow_negative_stock = frappe.db.get_value("Stock Settings", None, "allow_negative_stock")
frappe.db.set_value("Stock Settings", None, "allow_negative_stock", 1)
for warehouse in frappe.db.sql("select name from `tabWarehouse`"):
repost_stock(newdn, warehouse[0])
frappe.db.set_value("Stock Settings", None, "allow_negative_stock", existing_allow_negative_stock)
frappe.db.auto_commit_on_many_writes = 0
def copy_specification_from_item_group(self):
self.set("website_specifications", [])
if self.item_group:
for label, desc in frappe.db.get_values("Item Website Specification",
{"parent": self.item_group}, ["label", "description"]):
row = self.append("website_specifications")
row.label = label
row.description = desc
def update_item_desc(self):
if frappe.db.get_value('BOM',self.name, 'description') != self.description:
frappe.db.sql("""update `tabBOM` set description = %s where item = %s and docstatus < 2""",(self.description, self.name))
frappe.db.sql("""update `tabBOM Item` set description = %s where item_code = %s and docstatus < 2""",(self.description, self.name))
frappe.db.sql("""update `tabBOM Explosion Item` set description = %s where item_code = %s and docstatus < 2""",(self.description, self.name))
def customer_code_update(self):
customer_code=self.customer_code;
#frappe.msgprint(customer_code+"Cust Test");
if(customer_code==""):
customer_name=self.customer_name;
result_cust_code=frappe.db.sql("""select customer_code from tabCustomer where customer_name=%s""",customer_name)
self.customer_code=result_cust_code;
#frappe.msgprint("test",raise_exception=1)
def validate_end_of_life(item_code, end_of_life=None, verbose=1):
if not end_of_life:
end_of_life = frappe.db.get_value("Item", item_code, "end_of_life")
if end_of_life and end_of_life!="0000-00-00" and getdate(end_of_life) <= now_datetime().date():
msg = _("Item {0} has reached its end of life on {1}").format(item_code, formatdate(end_of_life))
_msgprint(msg, verbose)
def validate_is_stock_item(item_code, is_stock_item=None, verbose=1):
if not is_stock_item:
is_stock_item = frappe.db.get_value("Item", item_code, "is_stock_item")
if is_stock_item != "Yes":
msg = _("Item {0} is not a stock Item").format(item_code)
_msgprint(msg, verbose)
def validate_cancelled_item(item_code, docstatus=None, verbose=1):
if docstatus is None:
docstatus = frappe.db.get_value("Item", item_code, "docstatus")
if docstatus == 2:
msg = _("Item {0} is cancelled").format(item_code)
_msgprint(msg, verbose)
def _msgprint(msg, verbose):
if verbose:
msgprint(msg, raise_exception=True)
else:
raise frappe.ValidationError, msg
def get_last_purchase_details(item_code, doc_name=None, conversion_rate=1.0):
"""returns last purchase details in stock uom"""
# get last purchase order item details
last_purchase_order = frappe.db.sql("""\
select po.name, po.transaction_date, po.conversion_rate,
po_item.conversion_factor, po_item.base_price_list_rate,
po_item.discount_percentage, po_item.base_rate
from `tabPurchase Order` po, `tabPurchase Order Item` po_item
where po.docstatus = 1 and po_item.item_code = %s and po.name != %s and
po.name = po_item.parent
order by po.transaction_date desc, po.name desc
limit 1""", (item_code, cstr(doc_name)), as_dict=1)
# get last purchase receipt item details
last_purchase_receipt = frappe.db.sql("""\
select pr.name, pr.posting_date, pr.posting_time, pr.conversion_rate,
pr_item.conversion_factor, pr_item.base_price_list_rate, pr_item.discount_percentage,
pr_item.base_rate
from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item
where pr.docstatus = 1 and pr_item.item_code = %s and pr.name != %s and
pr.name = pr_item.parent
order by pr.posting_date desc, pr.posting_time desc, pr.name desc
limit 1""", (item_code, cstr(doc_name)), as_dict=1)
purchase_order_date = getdate(last_purchase_order and last_purchase_order[0].transaction_date \
or "1900-01-01")
purchase_receipt_date = getdate(last_purchase_receipt and \
last_purchase_receipt[0].posting_date or "1900-01-01")
if (purchase_order_date > purchase_receipt_date) or \
(last_purchase_order and not last_purchase_receipt):
# use purchase order
last_purchase = last_purchase_order[0]
purchase_date = purchase_order_date
elif (purchase_receipt_date > purchase_order_date) or \
(last_purchase_receipt and not last_purchase_order):
# use purchase receipt
last_purchase = last_purchase_receipt[0]
purchase_date = purchase_receipt_date
else:
return frappe._dict()
conversion_factor = flt(last_purchase.conversion_factor)
out = frappe._dict({
"base_price_list_rate": flt(last_purchase.base_price_list_rate) / conversion_factor,
"base_rate": flt(last_purchase.base_rate) / conversion_factor,
"discount_percentage": flt(last_purchase.discount_percentage),
"purchase_date": purchase_date
})
conversion_rate = flt(conversion_rate) or 1.0
out.update({
"price_list_rate": out.base_price_list_rate / conversion_rate,
"rate": out.base_rate / conversion_rate,
"base_rate": out.base_rate
})
return out
def invalidate_cache_for_item(doc):
invalidate_cache_for(doc, doc.item_group)
website_item_groups = list(set((doc.get("old_website_item_groups") or [])
+ [d.item_group for d in doc.get({"doctype":"Website Item Group"}) if d.item_group]))
for item_group in website_item_groups:
invalidate_cache_for(doc, item_group)
if doc.get("old_item_group") and doc.get("old_item_group") != doc.item_group:
invalidate_cache_for(doc, doc.old_item_group)
@frappe.whitelist()
def custom_validate(doc,method):
material_type=doc.material_type
if(material_type=="Item"):
product=doc.product
package_category=doc.package_category
item_category=doc.item_category
if(product==None):
frappe.msgprint("Product is mandatory",raise_exception=1)
if(package_category==None):
frappe.msgprint("Package Category is mandatory",raise_exception=1)
if(item_category==None):
frappe.msgprint("Item Category is mandatory",raise_exception=1)
@frappe.whitelist()
def customer_code_load(customer_name=None):
return frappe.db.sql("""select customer_code from tabCustomer where name=%s""",customer_name)
@frappe.whitelist()
def mpn_num_load(item_attribute_value=None):
return frappe.db.sql("""select manufacturer_part_number from `tabItem Attribute Value` where attribute_value=%s""",item_attribute_value)
@frappe.whitelist()
def item_code_ajax_gen(material_type=None,item_group=None,item_division=None,customer_name=None,pro_name=None,package_category=None,category=None,subcategory=None):
#item code
'''
Item Group first letter +
Item Division Number +
Customer Short Name +
Product Short Name +
package category first letter +
Category Number +
Subcategory Number +
Running Number
'''
#group
if(material_type=="Item"):
#item_group=self.item_group
item_group_first = item_group[0]
#division
#item_division=self.item_division
auto_number_div= frappe.db.get_value("Item Division", item_division, "auto_number")
if auto_number_div==None:
auto_number_div=0;
#customer
#customer_name=self.customer_name;
short_cust= frappe.db.get_value("Customer", customer_name, "short_name")
if short_cust==None:
short_cust=" "
#product
#pro_name=self.product
if(pro_name):
short_product= frappe.db.get_value("Item", pro_name, "product_short_name")
else:
short_product=""
#package
#package_category=self.package_category
if(package_category):
package_category_letter = package_category[0]
else:
package_category_letter=""
#category
#category=self.item_category
if(category):
#frappe.msgprint(category,raise_exception=1)
auto_number_cat= frappe.db.get_value("Item Category", category, "auto_number")
if auto_number_cat==None:
auto_number_cat=0;
auto_number_cat=int(auto_number_cat);
if(auto_number_cat<10):
auto_number_cat="0"+str(auto_number_cat)
else:
auto_number_cat="00"
#subcategory
#subcategory=self.item_sub_category
if(subcategory):
auto_number_sub= frappe.db.get_value("Item Subcategory", subcategory, "auto_number")
if auto_number_sub==None:
auto_number_sub=0;
auto_number_sub=int(auto_number_sub);
if(auto_number_sub<10):
auto_number_sub="0"+str(auto_number_sub)
else:
auto_number_sub="00"
#prefix
prefix=str(item_group_first)+str(auto_number_div)+str(short_cust)+str(short_product)+str(package_category_letter)+str(auto_number_cat)+str(auto_number_sub)
#runing number
count_rows=frappe.db.count("Item",filters={'prefix':prefix,'material_type':"Item"})
count_rows=count_rows+1;
if(count_rows>=0 and count_rows<10):
str_num="000";
elif(count_rows>=10 and count_rows<100):
str_num="00"
elif(count_rows>=100 and count_rows<1000):
str_num="0"
else:
str_num="";
item_code=prefix+str_num+str(count_rows);
item_code=item_code.upper();
prefix=prefix.upper();
#self.item_code=item_code
#self.prefix=prefix
#self.barcode=item_code
ret = {'item_code':item_code,'prefix':prefix}
return ret
if(material_type=="Product"):
item_groups=item_group.split(" ")
if(len(item_groups)>=2):
item_groups_one=item_groups[0];
item_groups_two=item_groups[1];
item_group_first_letter = item_groups_one[0]
item_group_second_letter = item_groups_two[0]
if(len(item_groups)<2):
item_groups_one=item_groups[0];
item_group_first_letter = item_groups_one[0]
item_group_second_letter = item_groups_one[1]
item_group_letters=str(item_group_first_letter)+str(item_group_second_letter)
#division
auto_number_div= frappe.db.get_value("Item Division", item_division, "auto_number")
if auto_number_div==None:
auto_number_div=0;
#customer
short_cust= frappe.db.get_value("Customer", customer_name, "short_name")
if short_cust==None:
short_cust="";
#product_short_name
product_short_name=product_short_name
#prefix
prefix=str(item_group_letters)+str(auto_number_div)+str(short_cust)+str(product_short_name)
count_rows=frappe.db.count("Item",filters={'prefix':prefix,'material_type':"Product"})
count_rows=count_rows+1;
if(count_rows>=0 and count_rows<10):
str_num="0";
else:
str_num="";
item_code=prefix+str_num+str(count_rows);
item_code=item_code.upper();
prefix=prefix.upper();
ret = {'item_code':item_code,'prefix':prefix}
return ret