-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgjp.js
More file actions
executable file
·2881 lines (2528 loc) · 110 KB
/
gjp.js
File metadata and controls
executable file
·2881 lines (2528 loc) · 110 KB
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var ajax_object,
cl=console.log,
removeSpaceInBeginEnd = /^( | | )*|( | | )*$/g;
if (window.XMLHttpRequest) ajax_object = new XMLHttpRequest();
else {/*code for IE6, IE5*/ajax_object = new ActiveXObject("Microsoft.XMLHTTP");}
// var query_result;
PRODUCT_ITEMS_queried = {
product_items_array: [],
PRODUCT_CONSTRUCTOR: function (
product_id,
admin_defined_id,
full_name,
unit, //is an array
specification,
amount,
price_per_unit,
total_sum,
comment) {
this.product_id = product_id;
this.admin_defined_id = admin_defined_id;
this.full_name = full_name;
this.unit = unit;
this.specification = specification;
this.amount = amount;
this.price_per_unit = price_per_unit;
this.total_sum = total_sum;
this.comment = comment;
}
};
var ui = {
tabManager: function(tabOperationType, tabAttr){
//该函数用来管理tab,以对象的方式返回对应tab 的html属性:
// tabAttr:
// {
// tabElement: 对应的Element
// tabContent:
// html_attr: {
// class:
// tab_type:invoice
// my_invoice_id: someid
// }
// eventListeners: []
// }
// {
// tabElement: 对应的Element
// tabContent:
// html_attr: {
// class:
// tab_type:list
// list_type: product_info
// }
// eventListeners: []
// }
//tabOperationType操作类型,
// create,创建tab,如果type是info类型,则直接active 元素
// delete,删除tab
// modify,修改tab,比如将给元素class添加active
// isActive, 检测是否是active,
switch(tabOperationType){
//1573
case "create":
// 检查tabAttr.tab_type的值,遍历所有这些tab
if(tabAttr.html_attr.tab_type.search(/list/)!=-1){
var a = document.querySelector("#documents_tab>ul").querySelector('*[list_type="'+tabAttr.html_attr.list_type+'"]');
}
if(tabAttr.html_attr.tab_type.search(/invoice/)!=-1){
var a = document.querySelector("#documents_tab>ul").querySelector('*[my_invoice_id="'+tabAttr.html_attr.my_invoice_id+'"]');
}
if(!a){
var a = document.createElement("li");
//a 指的是tab中的li元素
a.appendChild(tabAttr.tabContent);
tabAttr.tabElement = a;
tabAttr.eventListeners.push(["click", function(){
ui.tabManager("modify",
{
html_attr:{class:"active"},
tabElement: this
}
);
}]);
ui.tabManager("modify",tabAttr);
document.querySelector("#documents_tab>ul").appendChild(a);
}
tabAttr.tabElement = a;
ui.tabManager("modify",
{
html_attr:{class:"active"},
tabElement: a
}
);
break;
case "modify":
if(tabAttr.html_attr){
if(tabAttr.html_attr.hasOwnProperty("class") && tabAttr.html_attr.class.search(/active/)!=-1){
$("#documents_tab").find(".active").removeClass("active");
}
for(var html_attrProperty in tabAttr.html_attr){
tabAttr.tabElement.setAttribute(html_attrProperty, tabAttr.html_attr[html_attrProperty]);
}
}
if(tabAttr.tabContent){
$(tabAttr.tabElement).html("").append($(tabAttr.tabContent));
}
if(tabAttr.eventListeners){
for (var i = 0; i < tabAttr.eventListeners.length; i++) {
tabAttr.tabElement.addEventListener(tabAttr.eventListeners[i][0], tabAttr.eventListeners[i][1]);
};
}
break;
case "delete":
$(tabAttr.tabElement).remove();
break;
case "isActive":
$(tabAttr.tabElement).hasClass('active');
break;
default:
break;
}
return tabAttr;
},
close_page: function(event, click_tab_after_close, tab_for_close, close_type){
//此函数用于关闭页面,不进行关闭前检查
//close_type,支持关闭两种页面,单据页面和信息页面,关闭页面时可以传入等待删除的tab,如果不传入,将默认删除当前活动的tab
//关闭invoice 时,可以传入invoice id
click_tab_after_close = click_tab_after_close?click_tab_after_close:$('#main_page').get(0);
tab_for_close = tab_for_close?tab_for_close:$("#documents_tab li.active").get(0);
ui.tabManager("delete", {tabElement: tab_for_close});
$(click_tab_after_close).click();
return true;
}
};
//这是单据函数、多个单据数组的对象
var td = TRANSACTION_DOCUMENT = {
//此处的id 与lists 中的id
//一一对应,用于查找对应的list
//:creator
"ids_in_document_lists":[],
"queried_result_products":[],
//文件列表指的是发票的列表,因为用户可能同时编辑多个发票
//文件列表时一个数组,数组的元素是单独的发票
//单个发票的构成:发票描述、发票内容
"document_lists": {
invoice_content_head_description_array: [
[{t: "a",v:["name","line_number"]},{t: "i",v:"行号"}],
[{t: "a",v:["name","product_id"]},{t: "i",v:"商品id"}],
[{t: "a",v:["name","manufacturer"]},{t: "i",v:"厂家"}],
[{t: "a",v:["name","full_name"]},{t: "i",v:"商品全名"}],
[{t: "a",v:["name","amount"]},{t: "i",v:"数量"}],
[{t: "a",v:["name","item_income"]},{t: "i",v:"金额"}],
[{t: "a",v:["name","price"]},{t: "i",v:"单价"}],
[{t: "a",v:["name","units_factor"]},{t: "i",v:"单位和规格"}],
[{t: "a",v:["name","comment_for_item"]},{t: "i",v:"备注"}]
],
"invoice_id1": {
////INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
"trading_object": [1,"系统"],//[id, full_name]
"doc_type" : "xs",//doc_type enum('xs','jh','db','dd'),销售,进货,调拨,用户订单
"store_house" : [1,"仓库1"],//[id, full_name]
"money_received" : 0,//Decimal(12,2)
"comment" : '',//varchar(63),描述区的备注
"document_status" : "管理员编辑", /*enum("完成","管理员反冲", "管理员编辑","管理员取消", "用户编辑", "用户取消") not null default "管理员编辑"*/
"created_time": "2016-11-21 11:46:15",
"update_time" : "0000-00-00 00:00:00",
// "document_description":
"document_content_array": [{
"product_id": 123,
"full_name": "小脆筒",
"unit": "箱",//标注用户所选用的计算规格的方式
"amount": 0,
"item_income": 0,
"comment": ""
},{
"product_id":"",
"manufacturer":"",
"full_name":"",
"amount":"",
"price":"",
"unit":"",//unit1还是其他?
"units_factor":[
"unit_1",
["unit_1", "箱", 1],
["unit_2", "支", 50],
["unit_3", "g", 60]
],
"item_income":"",
"comment_for_item":""
}]
}
},
//方法————单据对象的查看,查看之前需先保存当前显示的销售单
// 1、获取当前invoice 的id
// 2、注明当前的invoice type
// 3、给部分表格(客户)添加事件监听器
// 4、把新的id 写入表格描述部分
// 5、更新发票对应的对象
// 6、修改tab 状态
"viewer": function(editable_tag ,invoice_id){
//当前视图如果存在单据,id=i,则先保存该单据
var section = document.querySelector("#display_section");
var doc_type_caption = document.querySelector("*[my_invoice_id='"+invoice_id+"']").querySelector("a").innerHTML.replace(/^(销售|进货).*/,"$1"+"单");
section.innerHTML=[
'<!-- i 表示invoice -->',
'<table id="i" class="table table-bordered col-md-12" >',
'<caption class="text-center lead">'+doc_type_caption+'</caption>',
'<!-- i_des 表示invoice_description-->',
'<thead id="i_des">',
'<tr>',
'<td colspan=2>往来单位:</td>',
'<td name="trading_object" colspan="2" contenteditable></td>',
'<td name="invoice_id" invoice_id="'+invoice_id+'">单据编号:</td>',
'<td name="generated_id" colspan="1"></td>',
'<td>制单时间:</td> <td name="created_time" ></td>',
'</tr>',
'<tr>',
'<td colspan=2>仓库:</td>',
'<td name="store_house" store_house_id colspan="2"></td>',
'<td>备注:</td>',
'<td name="comment" colspan="3" contenteditable></td>',
'</tr>',
'</thead>',
'<!-- i_c表示invoice content -->',
'<tbody id="i_c">',
'</tbody>',
'<tfoot>',
'<tr>',
'<td>合计</td>',
'<td colspan=2></td>',
'<td name="total_amount"></td>',
'<td colspan=1 name="money_received"></td>',
'<td ></td>',
'<td colspan=3 name="money_received_chinese"></td>',
'</tr>',
'</tfoot>'
].join("\n");
var ls_fucntion_tab = $([
'<div id="ls_fucntion_tab" class="">',
'<button id="print_invoice" class="btn btn-default"><a href="#">打印单据</a></button>',
'<button id="save_as_draft" class="btn btn-default"><a href="#">存为草稿</a></button>',
'<button id="invoice_to_db" class="btn btn-default"><a href="#">单据过账</a></button>',
'<button id="close_invoice" class="btn btn-default"><a href="#">关闭单据</a></button>',
'</div>'
].join("\n")
);
ls_fucntion_tab.find("#print_invoice").on("click", function(){window.print();});
var save_as_draft_btn = ls_fucntion_tab.find("#save_as_draft");
var invoice_to_db_btn = ls_fucntion_tab.find("#invoice_to_db");
var close_btn = ls_fucntion_tab.find("#close_invoice");
if(editable_tag){
save_as_draft_btn.on("click", function(){td.saver_toSever("草稿");});
invoice_to_db_btn.on("click", function(){td.saver_toSever("完成");});
close_btn.remove();
}else{
save_as_draft_btn.attr("disabled","disabled");
invoice_to_db_btn.attr("disabled","disabled");
close_btn.on("click", function(){
var click_tab_after_close = $('[list_type="invoice_history"]')[0];
ui.close_page({}, click_tab_after_close);
delete td.document_lists["invoice_id"+invoice_id];
});
}
$(section).append(ls_fucntion_tab);
document.querySelector("td[name='trading_object']").addEventListener("keypress", td.builder.query_people);
//获取对应的对象文件
var a = td.document_lists["invoice_id"+invoice_id];
var tab = document.querySelector('[my_invoice_id="'+invoice_id+'"]');
//修改tab
ui.tabManager("modify", {
tabElement: tab,
tabContent: $("<a/>",{
href: "#",
text: a.trading_object[1]?tab.querySelector("a").innerHTML.replace(/^(销售给|进货从).*/,'$1'+a.trading_object[1]):""
}).get(0)
});
var des = document.querySelector('#i_des');
//从对象文件中读取描述和数据
des.querySelector('td[name="invoice_id"]').invoice_id = invoice_id;
//展示往来单位
var des_td = des.querySelector("td[name='trading_object']");
des_td.addEventListener("click",td.builder.cell_checker);
des_td.setAttribute("people_id",a.trading_object[0]?a.trading_object[0]:"");
des_td.innerHTML = a.trading_object[1]?a.trading_object[1]:"";
//展示单据描述数据
des.querySelector('td[name="generated_id"]').innerHTML = a.doc_type+"-"+invoice_id;
des.querySelector('td[name="created_time"]').innerHTML = a.created_time;
des.querySelector('td[name="store_house"]').innerHTML = a.store_house[1];
var des_comment = des.querySelector('td[name="comment"]');
des_comment.innerHTML = a.comment;
des_comment.addEventListener("click",td.builder.cell_checker);
des_comment.addEventListener("blur",function(){
a.comment = this.innerHTML;
});
document.querySelector('td[name="money_received"]').innerHTML = a.money_received?a.money_received:"";
var c = document.querySelector('#i_c');
var table_head_fields_description_array = td.document_lists.invoice_content_head_description_array;
//初始化表格头
c.appendChild(td.builder.creat_new_tr("th",table_head_fields_description_array));
for (var i = 0; i < a.document_content_array.length; i++) {
c.appendChild(td.builder.create_invoice_line(a.document_content_array[i], table_head_fields_description_array, editable_tag));
};
//last line in tbody
var last_line = td.builder.create_invoice_line("td",table_head_fields_description_array, 0);
var full_name_td = last_line.querySelector("[name='full_name']");
if(editable_tag){
full_name_td.setAttribute("contenteditable", "true");
full_name_td.removeEventListener("blur", td.builder.cell_checker);
}
c.appendChild(last_line);
for (var i = 1; i < c.childNodes.length; i++) {
c.childNodes[i].querySelector('[name="full_name"]').addEventListener("keypress",td.builder.query_products);
}
td.builder.line_number_refresher(c);
td.builder.sum_refresher();
},
"saver_toBg": function(id){
var a = document.querySelector("#i_c").querySelectorAll("tr");
var d_a=td.document_lists["invoice_id"+id].document_content_array;
d_a.splice(0,d_a.length);// 思考为什么不能用d_a=[];
for (var i = 1; i < a.length-1; i++) {
var o = {
"product_id": Number(a[i].querySelector('*[name="product_id"]').innerHTML),
"manufacturer": a[i].querySelector('*[name="manufacturer"]').innerHTML,
"full_name": a[i].querySelector('*[name="full_name"]').innerHTML,
"unit": td.builder.priceUnitManager("sv", a[i].querySelector('*[name="units_factor"]').querySelector("div"))[0],//标注用户所选用的计算规格的方式
"units_factor": td.builder.priceUnitManager("sv", a[i].querySelector('*[name="units_factor"]').querySelector("div")),
"price": Number(a[i].querySelector('*[name="price"]').innerHTML),
"amount": Number(a[i].querySelector('*[name="amount"]').innerHTML),
"item_income": Number(a[i].querySelector('*[name="item_income"]').innerHTML),
"comment_for_item": a[i].querySelector('*[name="comment_for_item"]').innerHTML
};
d_a.push(o);
}
},
"saver_toSever": function(document_status, id ){
var invoice_id = id?id:document.querySelector("td[name=invoice_id]").getAttribute("invoice_id");
if(document_status != "草稿")
{
if(td.invoice_validator(document_status, invoice_id)==false) return;
}
td.saver_toBg(invoice_id);
td.document_lists["invoice_id"+invoice_id].document_status = document_status;
$.ajax({
url: "update.php?type=save_invoice",
method: "POST",
contentType: "application/json",
data: JSON.stringify(td.document_lists["invoice_id"+invoice_id]),
success: function(data, status, XMLHttpRequest_object){
cl(data);
//删除对应 tab
var tab_for_close = $('#documents_tab li[my_invoice_id="'+invoice_id+'"]');
var click_tab_after_close = tab_for_close.next().length?tab_for_close.next():tab_for_close.prev();
ui.close_page({},click_tab_after_close[0], tab_for_close[0]);
delete td.document_lists["invoice_id"+invoice_id];
}
});
},
"invoice_validator": function(document_status, id ){
var invoice = $("#i");
var i_des = invoice.find("#i_des");
var div = $("<div/>");
var value = true;
if(i_des.find('[name="trading_object"]').attr("people_id")=="1"||
i_des.find('[name="trading_object"]').text()==""){
$("<div/>",{
"text": "往来单位有误"
}).appendTo(div);
value = false;
}
var i_content = invoice.find("#i_c");
var trs = i_content.find("tr");cl(trs);
if(trs.length<3){
$("<div/>",{
"text": "列表中无商品!"
}).appendTo(div);
value = false;
}
for (var i = 1; i < trs.length-1; i++) {
var mark = false;
var outer = $("<div/>");
var line_number = $(trs[i]).find('[name="line_number"]').text();
outer.append($("<div/>",{
"text": "行号为"+line_number+"的产品存在以下问题:"
}));
var full_name = $(trs[i]).find('[name="full_name"]');
var amount = $(trs[i]).find('[name="amount"]');
var price = $(trs[i]).find('[name="price"]');
if(full_name.text()=="") {
outer.append($("<div>商品名称不正确</div>"));
mark = true;
}
if(typeof Number(amount.text())!="number"||Number(amount.text())==0) {
outer.append($("<div>商品数量不正确</div>"));
mark = true;
}
if(typeof Number(price.text())!="number"||Number(price.text())==0) {
outer.append($("<div>商品价格不正确</div>"));
mark = true;
}
if(mark){
div.append(outer);
value = false;
}
};
if(value==false){
ls.checker.status.pop_up_creator("您不能保存单据,原因是", div.get(0));
}
return value==false?false:true;
},
//方法————单据对象的创建
//在主数据库创建一个单据,主数据库返回单据id
"creator": function (doc_type, operationType, invoice_id){
//doc_type,主要有xs,jh
//operationType,主要有
// cr,create
// vw,view
// ed, editor
var c_new_i,
doc_type_Chinese;
if(doc_type=="xs") {
doc_type_Chinese="销售给某单位";
}
if(doc_type=="jh") {
doc_type_Chinese="进货从某单位";
}
(function query(string){
var ajax_object;
if (window.XMLHttpRequest) ajax_object = new XMLHttpRequest();
else {/*code for IE6, IE5*/ajax_object = new ActiveXObject("Microsoft.XMLHTTP");}
ajax_object.onreadystatechange = function(){
if (ajax_object.readyState === XMLHttpRequest.DONE && ajax_object.status === 200){
//transaction_documents_description, Object
var tdd = JSON.parse(ajax_object.response);cl(tdd);
invoice_id = tdd.id;
var invoice = "invoice_id"+invoice_id;//invoice_id12331
td.document_lists[invoice] = tdd;
var viewer_arg = operationType=="cr"||operationType=="ed"?1:0;
//tab 对象创建
ui.tabManager("create",{
html_attr:{
tab_type: "invoice",
my_invoice_id: invoice_id
},
eventListeners: [
["click",function(){ td.viewer(viewer_arg, this.getAttribute("my_invoice_id")); }],
],
tabContent: $("<a/>",{
href: "#",
text: doc_type_Chinese
}).get(0)
});
//创建并添加到视图
td.viewer(viewer_arg, invoice_id);
}
if (ajax_object.readyState === XMLHttpRequest.DONE && ajax_object.status == 404){
alert("数据库连接错误");
}
};
ajax_object.open("GET", "query.php?" + string, true);
ajax_object.send();
})//此处传入的c_new_i是字符串,php发现此标记后,进行新发票的创建,返回新发票id
("query_invoice"+
(operationType=="cr"?("&c_new_i&doc_type="+doc_type):("&invoice_id="+invoice_id))
);
},
"creator_xs": function(){
td.creator("xs", "cr");
},
"creator_jh": function(){
td.creator("jh", "cr");
},
"creator_in_history_list": function(){
var $_this = $(this);
var doc_type = $_this.find("[name='doc_type']").text();
var document_status = $_this.find("[name='document_status']").text();
var invoice_id = $_this.find("[name='invoice_id']").text();
var doc_create_type = document_status=="完成"?"vw":"ed";
td.creator(doc_type, doc_create_type, invoice_id);
},
/*
如果该方法由viewer 唤起,则为相应td 元素添加事件处理器;
如果该方法用于创建表格列表,则
使用字符串或者py_code来检索商品,陈列商品,为列表添加时间处理器
当用户选中某几个条目时,将条目写入发票中,写入td的lists 中
*/
"builder": {
creat_new_tr: function(tag_name_within_line_tr, fields_description_array){
var tr=document.createElement("tr");
for (var i = 0; i < fields_description_array.length; i++) {
tr.appendChild(td.builder.create_new_element_in_tr(tag_name_within_line_tr, fields_description_array[i]));
};
return tr;
},
create_new_element_in_tr: function(tag_name, field_description_array){
var element = document.createElement(tag_name);
for (var i = 0; i < field_description_array.length; i++) {
switch(field_description_array[i].t){
case "a":
element.setAttribute(field_description_array[i].v[0],field_description_array[i].v[1]);
break;
case "i":
if(field_description_array[i].v==undefined) field_description_array[i].v = "";
if(typeof field_description_array[i].v=="number") field_description_array[i].v = field_description_array[i].v.toString();
element.innerHTML = "";
element.appendChild(typeof field_description_array[i].v=="string"?document.createTextNode(field_description_array[i].v):field_description_array[i].v);
break;
case "e":
element.addEventListener(field_description_array[i].v[0],field_description_array[i].v[1]);
break;
}
};
return element;
},
create_invoice_line: function (a, table_head_fields_description_array, editable_tag){//a是document_content_array的item
var tr_fields_description=[]
for (var i = 0; i < table_head_fields_description_array.length; i++) {
tr_fields_description[i] = [table_head_fields_description_array[i][0]];
};
for (var i = 0; i < tr_fields_description.length; i++) {
//innerHTML
var td_des = tr_fields_description[i];
if(tr_fields_description[i][0]["v"][1]!="units_factor")
tr_fields_description[i].push({t:"i",v:a[tr_fields_description[i][0]["v"][1]]});
if(editable_tag){
switch(tr_fields_description[i][0]["v"][1]){
case "line_number":
break;
case "product_id":
td_des.push(
{t:"a",v:["product_id", a.product_id]}
);
break;
case "manufacturer":
break;
case "full_name":
td_des.push(
{t:"e",v:["name","line_number"]},
{t:"e",v:["keypress",td.builder.query_products]},
{t:"e",v:["blur", td.builder.cell_checker]},
{t:"e",v:["click", td.builder.cell_checker]},
{t:"e",v:["keydown", ls.edit.arrow_key_control]},
{t:"e",v:["click",
function(){
$("[input_end_point]").removeAttr("input_end_point");
$(this).parent().attr("input_end_point","");
}
]},
{t:"a", v:["placeholder",a.full_name]},//用于checker
{t:"a", v:["contenteditable","true"]}
);
break;
case "units_factor":
td_des.push(
{t:"i",v: td.builder.priceUnitManager("cr", a.units_factor)}
);
break;
case "amount":
td_des.push(
{t:"e",v:["click", td.builder.cell_checker]},
{t:"e",v:["blur",td.builder.number_check_after_input]},
{t:"e",v:["blur",td.builder.amount_or_price_affect_received]},
{t:"e",v:["blur", td.builder.whether_amount_out_of_storage]},
{t:"e",v:["keydown", ls.edit.arrow_key_control]},
{t:"e",v:["keypress", td.builder.number_check_when_input]},
{t:"a",v:["contenteditable","true"]}
);
break;
case "price":
td_des.push(
{t:"e",v:["click", td.builder.cell_checker]},
{t:"e",v:["blur",td.builder.number_check_after_input]},
{t:"e",v:["blur",td.builder.amount_or_price_affect_received]},
{t:"e",v:["keydown", ls.edit.arrow_key_control]},
{t:"e",v:["keypress", td.builder.number_check_when_input]},
{t:"e",v:["blur", td.builder.whether_price_reasonable]},
{t:"a",v:["contenteditable","true"]}
);
if(Number(a.amount)){
td_des.push(
{t:"i",v: Number(a.item_income/a.amount)}
);
}
break;
case "item_income":
td_des.push(
{t:"e",v:["click", td.builder.cell_checker]},
{t:"e",v:["blur",td.builder.number_check_after_input]},
{t:"e",v:["keydown", ls.edit.arrow_key_control]},
{t:"e",v:["keypress", td.builder.number_check_when_input]},
{t:"e",v:["blur",td.builder.receive_affects_price]},
{t:"a",v:["contenteditable","true"]}
);
break;
case "comment_for_item":
td_des.push(
{t:"e",v:["click", td.builder.cell_checker]},
{t:"e",v:["keydown", ls.edit.arrow_key_control]},
{t:"a",v:["contenteditable","true"]}
)
break;
}
}
if(!editable_tag){
switch(tr_fields_description[i][0]["v"][1]){
case "line_number":
break;
case "product_id":
break;
case "manufacturer":
break;
case "full_name":
td_des.push(
{t:"e",v:["name","line_number"]},
{t:"e",v:["keypress",td.builder.query_products]},
{t:"e",v:["blur", td.builder.cell_checker]},
{t:"e",v:["click", td.builder.cell_checker]},
{t:"e",v:["keydown", ls.edit.arrow_key_control]},
{t:"e",v:["click",
function(){
$("[input_end_point]").removeAttr("input_end_point");
$(this).parent().attr("input_end_point","");
}
]},
{t:"a", v:["placeholder",a.full_name]}//用于checker
);
break;
case "units_factor":
td_des.push(
{t:"i",v: td.builder.priceUnitManager("cr", a.units_factor)}
);
break;
case "amount":
break;
case "price":
if(a.amount){
td_des.push(
{t:"i",v: Number(a.item_income/a.amount)}
);
}
break;
case "item_income":
break;
case "comment_for_item":
break;
}
}
};
//tr created
return td.builder.creat_new_tr("td",tr_fields_description);
},
// 两个功能,如果是click和focus,则选中表格内容
// 如果是blur,则检查输入结果是否为空
"cell_checker": function(e){
//点击后自动选择表格内容,用于td
if(e.type=="click" || e.type=="focus"){
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(this);
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNodeContents(this);
window.getSelection().addRange(range);
}
}
if(e.type=="blur"){
// console.log(this.innerHTML.replace(/ /g,"").replace(/ /g,""));
if(this.innerHTML.replace(removeSpaceInBeginEnd,"")==""){//正则表达式
console.log(this.parentNode);
td.builder.line_deleter(this.parentNode);
}else
if(this.hasAttribute("placeholder") && this.innerHTML != this.getAttribute("placeholder"))
this.innerHTML=this.getAttribute("placeholder");
}
},
"line_deleter": function(tr_node){
tr_node.parentNode.removeChild(tr_node);
td.builder.sum_refresher();
td.builder.line_number_refresher(document.querySelector("#i_c"));
},
"line_number_refresher": function(tbody){
var tr = tbody.querySelectorAll("tr");
for (var i = 1; i < tr.length; i++) {
tr[i].querySelector("td").innerHTML = i;
}
},
priceUnitManager: function(operationType, units_factor_array_or_div){
//进行 种操作
//第一种,创建,
//根据服务器提供的"价格、单位、规格"对象创建表格内容
//第二种,检查状态,event listener
//根据当前的invoice状态、用户操作,调整对应的价格和单位
//当用户调整价格或者单元的同时
//第三种,生成服务器接受的"价格、单元、规格"格式
if(operationType == "cr"){
var element = $("<div/>");
if(units_factor_array_or_div){
for (var i = 1; i < units_factor_array_or_div.length; i++) {
var outer_span = $("<span/>",{
name: units_factor_array_or_div[i][0],
name_for_user: units_factor_array_or_div[i][1],
factor: units_factor_array_or_div[i][2]
}).text(units_factor_array_or_div[i][2]);
outer_span.on("click", function(){
var tr = $(this).closest('tr');
var price_element = tr.find('[name="price"]');
var price = Number(price_element.text());
var amount_element = tr.find('[name="amount"]');
var amount = Number(amount_element.text());
var span_siblings = $(this).parent().children('span');
var allUnits = [];
for (var j = 0; j < span_siblings.length; j++) {
allUnits.push([$(span_siblings[j]).attr("name"), $(span_siblings[j]).attr("factor")]);
if($(span_siblings[j]).hasClass('info')){
var prevUnitElement = $(span_siblings[j]);
var prevUnitElement_pointer = j;
prevUnitElement.removeClass('info');
}
if(this == span_siblings[j]){
var currentUnitElement_pointer = j;
}
};
for(pointer = prevUnitElement_pointer;pointer<currentUnitElement_pointer;pointer++){
var price = price / allUnits[pointer+1][1];
// var amount = amount * allUnits[pointer+1][1];
}
for(pointer = prevUnitElement_pointer;pointer>currentUnitElement_pointer;pointer--){
var price = price * allUnits[pointer][1];
// var amount = amount / allUnits[pointer][1];
}
price_element.text(price);
$(this).addClass('info');
td.builder.amount_or_price_affect_received.apply($(price_element)[0]);
td.builder.whether_price_reasonable.apply($(price_element)[0]);
});
var inner_span = $("<span/>",{
name: "name_for_user",
text: units_factor_array_or_div[i][1]
}).appendTo(outer_span);
if(units_factor_array_or_div[i][0]==units_factor_array_or_div[0]){
outer_span.addClass('info');
}
element.append(outer_span);
if(i<units_factor_array_or_div.length-1){
element.append($(document.createTextNode("×")));
}
};
}
return element.get(0);
}
if(operationType == "sv"){
var units_factor = [];
var div = $(units_factor_array_or_div);
var units = div.children('span');
var info = div.find('.info');
units_factor.push(info.attr("name"));
for (var i = 0; i < units.length; i++) {
units_i = $(units[i]);
units_factor.push([units_i.attr("name"), units_i.attr("name_for_user"), units_i.attr("factor")]);
};
return units_factor;
}
},
"sum_refresher": function(){
var i_c_tr=document.querySelector("#i_c").querySelectorAll('tr');
var amount=money_received=0;
for (var i = 1; i < i_c_tr.length; i++) {
amount += Number(i_c_tr[i].querySelector("*[name='amount']").innerHTML);
money_received += Number(i_c_tr[i].querySelector("*[name='item_income']").innerHTML);
};
money_received=money_received.toFixed(2);
document.querySelector("tfoot").querySelector("*[name='total_amount']").innerHTML = amount;
document.querySelector("tfoot").querySelector("*[name='money_received']").innerHTML = Number(money_received);
function convertCurrency(currencyDigits) {
// Constants:
var MAXIMUM_NUMBER = 99999999999.99;
// Predefine the radix characters and currency symbols for output:
var CN_ZERO = "零";
var CN_ONE = "壹";
var CN_TWO = "贰";
var CN_THREE = "叁";
var CN_FOUR = "肆";
var CN_FIVE = "伍";
var CN_SIX = "陆";
var CN_SEVEN = "柒";
var CN_EIGHT = "捌";
var CN_NINE = "玖";
var CN_TEN = "拾";
var CN_HUNDRED = "佰";
var CN_THOUSAND = "仟";
var CN_TEN_THOUSAND = "万";
var CN_HUNDRED_MILLION = "亿";
var CN_SYMBOL = "";
var CN_DOLLAR = "元";
var CN_TEN_CENT = "角";
var CN_CENT = "分";
var CN_INTEGER = "整";
// Variables:
var integral; // Represent integral part of digit number.
var decimal; // Represent decimal part of digit number.
var outputCharacters; // The output result.
var parts;
var digits, radices, bigRadices, decimals;
var zeroCount;
var i, p, d;
var quotient, modulus;
// Validate input string:
currencyDigits = currencyDigits.toString();
if (currencyDigits == "") {
alert("Empty input!");
return "";
}
if (currencyDigits.match(/[^,.\d]/) != null) {
alert("Invalid characters in the input string!");
return "";
}
if ((currencyDigits).match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) == null) {
alert("Illegal format of digit number!");
return "";
}
// Normalize the format of input digits:
currencyDigits = currencyDigits.replace(/,/g, ""); // Remove comma delimiters.
currencyDigits = currencyDigits.replace(/^0+/, ""); // Trim zeros at the beginning.
// Assert the number is not greater than the maximum number.
if (Number(currencyDigits) > MAXIMUM_NUMBER) {
alert("Too large a number to convert!");
return "";
}
// Process the coversion from currency digits to characters:
// Separate integral and decimal parts before processing coversion:
parts = currencyDigits.split(".");
if (parts.length > 1) {
integral = parts[0];
decimal = parts[1];
// Cut down redundant decimal digits that are after the second.
decimal = decimal.substr(0, 2);
}
else {
integral = parts[0];
decimal = "";
}
// Prepare the characters corresponding to the digits:
digits = new Array(CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE);
radices = new Array("", CN_TEN, CN_HUNDRED, CN_THOUSAND);
bigRadices = new Array("", CN_TEN_THOUSAND, CN_HUNDRED_MILLION);
decimals = new Array(CN_TEN_CENT, CN_CENT);
// Start processing:
outputCharacters = "";
// Process integral part if it is larger than 0:
if (Number(integral) > 0) {
zeroCount = 0;
for (i = 0; i < integral.length; i++) {
p = integral.length - i - 1;
d = integral.substr(i, 1);
quotient = p / 4;
modulus = p % 4;
if (d == "0") {
zeroCount++;
}
else {
if (zeroCount > 0)
{
outputCharacters += digits[0];
}
zeroCount = 0;
outputCharacters += digits[Number(d)] + radices[modulus];
}
if (modulus == 0 && zeroCount < 4) {
outputCharacters += bigRadices[quotient];
}
}
outputCharacters += CN_DOLLAR;
}
// Process decimal part if there is:
if (decimal != "") {
for (i = 0; i < decimal.length; i++) {
d = decimal.substr(i, 1);
if (d != "0") {
outputCharacters += digits[Number(d)] + decimals[i];
}
}
}
// Confirm and return the final output string:
if (outputCharacters == "") {
outputCharacters = CN_ZERO + CN_DOLLAR;
}
if (decimal == "") {
outputCharacters += CN_INTEGER;
}
outputCharacters = CN_SYMBOL + outputCharacters;
return outputCharacters;