CartViewController.m 192 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151
  1. //
  2. // CartViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-6-6.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RAUtils.h"
  9. #import "CartViewController.h"
  10. #ifdef BUILD_CONTRAST
  11. #import "ContrastModelItemCell.h"
  12. #else
  13. #import "ModelItemCell.h"
  14. #endif
  15. #import "RANetwork.h"
  16. //#import "CommonGridViewController.h"
  17. #import "CreateOrderViewController.h"
  18. #import "DetailViewController.h"
  19. #import "EditModelPriceViewController.h"
  20. #import "MainViewController.h"
  21. #import "LoginViewController.h"
  22. #import "ItemNotesViewController.h"
  23. #import "CartGeneralNotesViewController.h"
  24. #import "DefaultAppearance.h"
  25. #import "DefaultTableHeaderView.h"
  26. #import "SortItemViewController.h"
  27. #import "SortButton.h"
  28. #import "NotificationNameCenter.h"
  29. #import "RASingleton.h"
  30. #import "JKMessageBoxController.h"
  31. #import "RAPDFViewController.h"
  32. #ifdef SCANNER_ORDER
  33. #import "ScanCartItemCell.h"
  34. #import "ScanDiscountViewController.h"
  35. #else
  36. #endif
  37. #ifdef BUILD_CONTRAST
  38. #import "OrderDiscountViewController.h"
  39. #endif
  40. #ifdef BACKORDER_PROCESS
  41. #import "RAOrderEditorViewController.h"
  42. #endif
  43. #import "CustomerEditViewController.h"
  44. //#define ALERT_FREE 1024
  45. //#define ALERT_DEL 1025
  46. //#define ALERT_SET_FREE 1026
  47. //#define ALERT_RESTORE_FREE 1027
  48. @interface CartViewController ()
  49. {
  50. CGRect cancelButtonFrame;
  51. }
  52. @property (nonatomic,strong) SortItemViewController *sortItemController;
  53. @property (nonatomic,assign) int sortIndex;
  54. @property (nonatomic,strong) SortButton *sortButton;
  55. @property (strong, nonatomic) IBOutlet UIButton *placeOrderButton;
  56. @property (strong, nonatomic) IBOutlet UIButton *cancelOrderButton;
  57. @property (nonatomic,strong) NSOperationQueue *dataOperationQueue;
  58. @property (strong, nonatomic) IBOutlet UIButton *emailButton;
  59. @property (strong, nonatomic) IBOutlet UIButton *printButton;
  60. @property (weak, nonatomic) IBOutlet UIButton *editButton;
  61. @property (weak, nonatomic) IBOutlet UIButton *continueButton;
  62. @property (weak, nonatomic) IBOutlet UILabel *labelTotalcuft;
  63. @property (weak, nonatomic) IBOutlet UILabel *labelTotalWeight;
  64. @property (weak, nonatomic) IBOutlet UILabel *labelTotalCarton;
  65. @property (weak, nonatomic) IBOutlet UILabel *valuecuft;
  66. @property (weak, nonatomic) IBOutlet UILabel *valueweight;
  67. @property (weak, nonatomic) IBOutlet UILabel *valuecarton;
  68. @property (nonatomic,assign) BOOL currentOrderIsMerged;
  69. @property (nonatomic,strong) DefaultTableHeaderView *cartIndicatorBar;
  70. @property (nonatomic,assign) BOOL back_order_flag;
  71. @property (nonatomic,strong) NSDictionary *cart_json;
  72. @property (nonatomic,copy) NSString *print_url;
  73. @property (nonatomic,assign) BOOL available;///<所有Model均有库存,才能Place Order。
  74. @property bool scan_response;
  75. @end
  76. @implementation CartViewController
  77. - (NSOperationQueue *)dataOperationQueue {
  78. if (!_dataOperationQueue) {
  79. _dataOperationQueue = [[NSOperationQueue alloc] init];
  80. _dataOperationQueue.maxConcurrentOperationCount = 1;
  81. }
  82. return _dataOperationQueue;
  83. }
  84. - (void)dealloc {
  85. #ifndef RA_NOTIFICATION
  86. [[NSNotificationCenter defaultCenter] removeObserver:self];
  87. #endif
  88. #if defined(USE_BLE_SCANNER) || defined(USE_MFI_SCANNER)
  89. [[NSNotificationCenter defaultCenter] removeObserver:self];
  90. #endif
  91. }
  92. -(void) logout
  93. {
  94. [super logout];
  95. [self.content_arr removeAllObjects];
  96. [self refresh_ui];
  97. self.cart_json = nil;
  98. }
  99. - (IBAction)OnEditSelectClick:(id)sender {
  100. NSMutableArray* arr_ids = [[NSMutableArray alloc] init];
  101. long count = self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  102. if(count==0)
  103. {
  104. [RAUtils message_box:@"Cart" message:@"No item in the cart" completion:nil] ;
  105. return;
  106. }
  107. for(int i=0;i<count;i++)
  108. {
  109. NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  110. [arr_ids addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"cart_item_id"] intValue]]];
  111. }
  112. NSString * ids = [arr_ids componentsJoinedByString:@","];
  113. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Select all model and" message:nil preferredStyle:UIAlertControllerStyleAlert];
  114. //block代码块取代了delegate
  115. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  116. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure remove all models from cart?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  117. //block代码块取代了delegate
  118. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  119. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Remove Models From Cart" completion:^{
  120. PopWaitAlert* pop = [RAUtils waiting_pop:@"Remove Models From Cart" completion:nil];
  121. [RANetwork request_remove_cart:ids completionHandler:^(NSMutableDictionary *result) {
  122. NSMutableDictionary* cart_json =result;
  123. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  124. // [waitalert dismissViewControllerAnimated:YES completion:^{
  125. [pop hide];
  126. if([[cart_json valueForKey:@"result"] intValue]==2)
  127. {
  128. [self end_edit];
  129. #ifdef RA_NOTIFICATION
  130. [ActiveViewController Notify:@"CartViewController,CategoryViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  131. #else
  132. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  133. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  134. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:true];
  135. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  136. #endif
  137. }
  138. else
  139. {
  140. [RAUtils message_box:@"Delete Model" message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  141. }
  142. // }];
  143. }];
  144. // }];
  145. }];
  146. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  147. DebugLog(@"No");
  148. }];
  149. [alertControl addAction:actionOne];
  150. [alertControl addAction:alertthree];
  151. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  152. [self presentViewController:alertControl animated:YES completion:nil];
  153. DebugLog(@"%@",self.edit_select_arr);
  154. }];
  155. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Move To Wish List" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  156. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Move Models To Wish List" completion:^{
  157. PopWaitAlert* pop = [RAUtils waiting_pop:@"Move Models To Wish List" completion:nil];
  158. [RANetwork request_move_cart2wish:ids completionHandler:^(NSMutableDictionary *result) {
  159. NSDictionary* cart_json =result;
  160. // [waitalert dismissViewControllerAnimated:YES completion:^{
  161. [pop hide];
  162. if([[cart_json valueForKey:@"result"] intValue]==2)
  163. {
  164. #ifdef RA_NOTIFICATION
  165. [ActiveViewController Notify:@"WatchListViewController,CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  166. #else
  167. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  168. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  169. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  170. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  171. #endif
  172. [self end_edit];
  173. }
  174. else
  175. {
  176. [RAUtils message_box:@"Move Models To Wish List" message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  177. ;
  178. }
  179. // }];
  180. }];
  181. // }];
  182. }];
  183. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  184. DebugLog(@"No");
  185. }];
  186. [alertControl addAction:actionOne];
  187. [alertControl addAction:actionTwo];
  188. [alertControl addAction:alertthree];
  189. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  190. [self presentViewController:alertControl animated:YES completion:nil];
  191. DebugLog(@"%@",self.edit_select_arr);
  192. }
  193. - (IBAction)onEditAddWishClick:(id)sender {
  194. if(self.edit_select_arr.count==0)
  195. {
  196. [RAUtils message_box:@"Cannot Move To Wish List" message:@"You must select at least one model." completion:nil] ;
  197. return;
  198. }
  199. NSString * ids = [self.edit_select_arr componentsJoinedByString:@","];
  200. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Move Models To Wish List" completion:^{
  201. PopWaitAlert* pop = [RAUtils waiting_pop:@"Move Models To Wish List" completion:nil];
  202. [RANetwork request_move_cart2wish:ids completionHandler:^(NSMutableDictionary *result) {
  203. NSDictionary* cart_json = result;
  204. // [waitalert dismissViewControllerAnimated:YES completion:^{
  205. [pop hide];
  206. if([[cart_json valueForKey:@"result"] intValue]==2)
  207. {
  208. [self end_edit];
  209. #ifdef RA_NOTIFICATION
  210. [ActiveViewController Notify:@"WatchListViewController,CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  211. #else
  212. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  213. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  214. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  215. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  216. #endif
  217. }
  218. else
  219. {
  220. [RAUtils message_box:@"Move To Wish List" message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  221. }
  222. // }];
  223. }];
  224. // }];
  225. }
  226. - (IBAction)onEditDelClick:(id)sender {
  227. if(self.edit_select_arr.count==0)
  228. {
  229. [RAUtils message_box:@"Cannot Delete From Cart" message:@"You must select at least one model." completion:nil] ;
  230. return;
  231. }
  232. NSString * ids = [self.edit_select_arr componentsJoinedByString:@","];
  233. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure remove selected models from cart?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  234. //block代码块取代了delegate
  235. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  236. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Remove Models From Cart" completion:^{
  237. PopWaitAlert* pop = [RAUtils waiting_pop:@"Remove Models From Cart" completion:nil];
  238. [RANetwork request_remove_cart:ids completionHandler:^(NSMutableDictionary *result) {
  239. NSMutableDictionary* cart_json =result;
  240. // [waitalert dismissViewControllerAnimated:YES completion:^{
  241. [pop hide];
  242. if([[cart_json valueForKey:@"result"] intValue]==2)
  243. {
  244. [self end_edit];
  245. #ifdef RA_NOTIFICATION
  246. [ActiveViewController Notify:@"CartViewController,CategoryViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  247. #else
  248. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  249. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  250. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:true];
  251. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  252. #endif
  253. }
  254. else
  255. {
  256. [RAUtils message_box:@"Delete Model" message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  257. }
  258. // }];
  259. }];
  260. // }];
  261. }];
  262. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  263. DebugLog(@"No");
  264. }];
  265. [alertControl addAction:actionOne];
  266. [alertControl addAction:alertthree];
  267. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  268. [self presentViewController:alertControl animated:YES completion:nil];
  269. DebugLog(@"%@",self.edit_select_arr);
  270. }
  271. - (IBAction)onEndEditClick:(id)sender {
  272. [self end_edit];
  273. }
  274. -(void)begin_edit
  275. {
  276. self.toolpanel.hidden = true;
  277. self.toolbar.hidden = false;
  278. // DebugLog(@"begin edit %@",NSStringFromCGRect(self.cartItemView.frame));
  279. UIInterfaceOrientation orientation = [RAUtils query_orientation:self];//= [[UIApplication sharedApplication]statusBarOrientation];
  280. if ( UIInterfaceOrientationIsPortrait(orientation))
  281. self.cartItemView.frame = CGRectMake(0,40,768,840);
  282. else
  283. self.cartItemView.frame = CGRectMake(0,40,1024,584);
  284. [self.itemListTable setEditing:true animated:YES];
  285. [self.itemListTable reloadData];
  286. }
  287. -(void) end_edit
  288. {
  289. self.toolpanel.hidden=false;
  290. self.toolbar.hidden=true;
  291. // DebugLog(@"end edit %@",NSStringFromCGRect(self.cartItemView.frame));
  292. UIInterfaceOrientation orientation = [RAUtils query_orientation:self];//= [[UIApplication sharedApplication]statusBarOrientation];
  293. if ( UIInterfaceOrientationIsPortrait(orientation))
  294. self.cartItemView.frame = CGRectMake(0,40,768,760);
  295. else
  296. self.cartItemView.frame = CGRectMake(0,40,1024,508);
  297. [self.itemListTable setEditing:false animated:YES];
  298. [self.edit_select_arr removeAllObjects];
  299. [self.itemListTable reloadData];
  300. // [self reload_data];
  301. }
  302. - (IBAction)onEditClick:(id)sender {
  303. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  304. //
  305. // [appDelegate test_onDecodedDataResult:@"4400022"];
  306. //
  307. //
  308. // return;
  309. [self begin_edit];
  310. }
  311. #ifndef RA_NOTIFICATION
  312. -(void) reload_container_getdata:(bool) update_data
  313. {
  314. [super reload_container_getdata:update_data];
  315. if(update_data)
  316. [self reload_data];
  317. else
  318. {
  319. [self.itemListTable reloadData];
  320. [self refresh_total];
  321. }
  322. }
  323. #endif
  324. -(void) showHidePrice
  325. {
  326. #ifndef RA_NOTIFICATION
  327. [self reload_container_getdata:false];
  328. #endif
  329. // [self refresh_total];
  330. }
  331. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  332. {
  333. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  334. if (self) {
  335. // Custom initialization
  336. }
  337. return self;
  338. }
  339. - (IBAction)imgbtnClick:(UIButton *)sender {
  340. #ifdef SCANNER_ORDER
  341. return;
  342. #endif
  343. UITableViewCell* cell =(UITableViewCell*) sender.superview.superview;
  344. NSIndexPath * indexPath = [self.itemListTable indexPathForCell:cell];
  345. // DebugLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  346. NSDictionary * item_json = nil;
  347. if (self.back_order_flag) {
  348. item_json = [self itemJsonAtIndexPath:indexPath];
  349. } else {
  350. item_json = self.content_arr[indexPath.row];//[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  351. }
  352. NSString* product_id = [item_json valueForKey:@"product_id"];
  353. NSString *name = [item_json valueForKey:@"model"];
  354. int is_rate = [item_json[@"is_rate"] intValue];
  355. if(is_rate==1)
  356. return;
  357. DetailViewController* dvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Model" bundle:nil] instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  358. // dvc
  359. dvc.product_id=product_id;
  360. dvc.category_id=nil;
  361. dvc.ispush=true;
  362. dvc.model_name = name;
  363. [dvc reload];
  364. [self.navigationController pushViewController:dvc animated:true];
  365. }
  366. -(void)viewWillLayoutSubviews
  367. {
  368. // if(self.showDetail==true)
  369. // {
  370. // UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;
  371. // int width=175;
  372. // // int height;
  373. // if(orientation == UIDeviceOrientationLandscapeLeft ||orientation == UIDeviceOrientationLandscapeRight)
  374. // {
  375. // width = 370;
  376. // // height = 400;
  377. // }
  378. // else
  379. // {
  380. // // height = 300;
  381. // width = 175;
  382. // }
  383. //
  384. //
  385. // CGRect frame = CGRectMake(0, 64, width, self.view.bounds.size.height-64);
  386. //
  387. // self.collectionview.frame=frame;
  388. //
  389. //
  390. //
  391. // CGRect framedetail = CGRectMake(width,64 ,self.view.bounds.size.width-width,self.view.bounds.size.height-64);
  392. // self.detailView.frame = framedetail;
  393. // }
  394. self.toolpanel.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.toolpanel.bounds].CGPath;
  395. // self.collectionview.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.collectionview.bounds].CGPath;
  396. }
  397. - (void)viewWillAppear:(BOOL)animated
  398. {
  399. [super viewWillAppear:animated];
  400. // print 13 email 19 cancel 24 place order
  401. // Shop 权限检查
  402. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  403. if (appDelegate.user && appDelegate.user_type == USER_ROLE_CUSTOMER && appDelegate.customer_type == CustomerTypeStore) {
  404. if (![RASingleton sharedInstance].permissions_edit_order) {
  405. CGRect frame0 = self.cancelOrderButton.frame;
  406. CGRect frame1 = self.placeOrderButton.frame;
  407. CGRect frame2 = self.emailButton.frame;
  408. CGRect frame3 = self.printButton.frame;
  409. CGFloat x = CGRectGetMaxX(frame1) - frame0.size.width;
  410. frame0.origin.x = x;
  411. self.cancelOrderButton.frame = frame0;
  412. x -= 20 + CGRectGetWidth(frame2);
  413. frame2.origin.x = x;
  414. self.emailButton.frame = frame2;
  415. x -= 20 + CGRectGetWidth(frame3);
  416. frame3.origin.x = x;
  417. self.printButton.frame = frame3;
  418. self.placeOrderButton.hidden = YES;
  419. } else {
  420. self.placeOrderButton.hidden = NO;
  421. CGSize size = cancelButtonFrame.size;
  422. CGFloat x = CGRectGetMinX(self.placeOrderButton.frame) - 24 - size.width;
  423. CGFloat y = CGRectGetMinY(self.placeOrderButton.frame);
  424. self.cancelOrderButton.frame = CGRectMake(x, y, size.width, size.height);
  425. CGRect frame2 = self.emailButton.frame;
  426. x -= 20 + CGRectGetWidth(frame2);
  427. frame2.origin.x = x;
  428. self.emailButton.frame = frame2;
  429. CGRect frame3 = self.printButton.frame;
  430. x -= 20 + CGRectGetWidth(frame3);
  431. frame3.origin.x = x;
  432. self.printButton.frame = frame3;
  433. }
  434. } else {
  435. // self.cancelOrderButton.frame = cancelButtonFrame;
  436. self.placeOrderButton.hidden = NO;
  437. CGSize size = cancelButtonFrame.size;
  438. CGFloat x = CGRectGetMinX(self.placeOrderButton.frame) - 24 - size.width;
  439. CGFloat y = CGRectGetMinY(self.placeOrderButton.frame);
  440. self.cancelOrderButton.frame = CGRectMake(x, y, size.width, size.height);
  441. CGRect frame2 = self.emailButton.frame;
  442. x -= 20 + CGRectGetWidth(frame2);
  443. frame2.origin.x = x;
  444. self.emailButton.frame = frame2;
  445. CGRect frame3 = self.printButton.frame;
  446. x -= 20 + CGRectGetWidth(frame3);
  447. frame3.origin.x = x;
  448. self.printButton.frame = frame3;
  449. }
  450. #ifdef OFFLINE_MODE
  451. // 离线隐藏Email
  452. self.emailButton.hidden = appDelegate.offline_mode;
  453. // 离线隐藏Print
  454. self.printButton.hidden = appDelegate.offline_mode;
  455. #endif
  456. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  457. #ifdef SCANNER_ORDER
  458. if(appDelegate.user_type == USER_ROLE_EMPLOYEE)
  459. {
  460. self.btnBulkDiscount.hidden = false;
  461. }
  462. else
  463. self.btnBulkDiscount.hidden = true;
  464. #endif
  465. }
  466. - (void)checkProductAvailable:(NSArray *)contents {
  467. if (contents == nil) {
  468. self.available = NO;
  469. return;
  470. }
  471. [contents enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  472. if ([obj isKindOfClass:[NSDictionary class]]) {
  473. NSDictionary *item_json = (NSDictionary *)obj;
  474. BOOL isAvailable = ![[item_json valueForKey:@"is_out_of_stock"] boolValue];
  475. self.available = self.available && isAvailable;
  476. }
  477. }];
  478. }
  479. - (void)clearContent {
  480. dispatch_async(dispatch_get_main_queue(), ^{
  481. self.freejson = nil;
  482. self.notes = nil;
  483. self.content_arr = nil;
  484. self.currentOrderIsMerged = nil;
  485. self.cart_json = nil;
  486. [RASingleton sharedInstance].currentOrderIsMerged = NO;
  487. self.print_url = nil;
  488. [self checkProductAvailable:self.content_arr];
  489. [self refresh_total];
  490. [self.itemListTable reloadData];
  491. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  492. appDelegate.cart_count = 0;
  493. [appDelegate update_count_mark];
  494. });
  495. }
  496. -(void) operation_reload_data
  497. {
  498. if (self.dataOperationQueue.operationCount > 1) {
  499. DebugLog(@"operationCount >1 cancel");
  500. return;
  501. }
  502. DebugLog(@"operating...");
  503. __block AppDelegate *appDelegate = nil;
  504. dispatch_sync(dispatch_get_main_queue(), ^{
  505. //some UI methods ej
  506. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  507. });
  508. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  509. //#ifndef SCANNER_ORDER
  510. //扫码订单允许后建订单
  511. if (appDelegate.order_code == nil || appDelegate.order_code.length == 0) {
  512. [self clearContent];
  513. return;
  514. }
  515. //#endif
  516. __weak typeof(self) weakself = self;
  517. dispatch_async(dispatch_get_main_queue(), ^{
  518. if(weakself.isrefreshing)
  519. return;
  520. weakself.available = YES;
  521. weakself.itemListTable.hidden = true;
  522. weakself.label_net_err.hidden=true;
  523. weakself.isrefreshing=true;
  524. // [self.content_data removeAllObjects];
  525. // [self.itemListTable reloadData];
  526. // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  527. //
  528. // [reF endRefreshing];
  529. //
  530. // reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  531. weakself.mum.center = weakself.view.center;
  532. weakself.mum.hidden = YES;
  533. // [self.mum startAnimating];
  534. DebugLog(@"reloading...");
  535. // __block UIAlertController *loadingView = [RAUtils waiting_alert:self title:@"Loading" completion:^{
  536. PopWaitAlert* pop = [RAUtils waiting_pop:@"Loading" completion:nil];
  537. [RANetwork request_cart:weakself.sortIndex completionHandler:^(NSMutableDictionary *result) {
  538. DebugLog(@"BEGIN LOAD CART");
  539. NSDictionary* cart_json = result;
  540. //
  541. // [self.mum stopAnimating];
  542. // [loadingView dismissViewControllerAnimated:YES completion:^{
  543. [pop hide];
  544. // [loadingView dismissWithClickedButtonIndex:0 animated:YES];
  545. // self.content_data = [cart_json mutableCopy];
  546. #ifdef BACKORDER_PROCESS
  547. weakself.back_order_flag = [[cart_json objectForKey:@"back_order_version"] boolValue];
  548. #else
  549. weakself.back_order_flag = NO;
  550. #endif
  551. #ifdef BUILD_CONTRAST
  552. weakself.orderdiscount = [cart_json[@"orderDiscount"] doubleValue];
  553. #endif
  554. if (!weakself.back_order_flag) {
  555. weakself.freejson = [cart_json objectForKey:@"freeGiveaway"];
  556. weakself.notes = [cart_json valueForKey:@"general_note"];
  557. weakself.content_arr = [RAUtils dictionary2array:cart_json count_fields:@"count" item_mark:@"item_" items_mark:nil];
  558. weakself.currentOrderIsMerged = [[cart_json valueForKey:@"is_merged_order"] boolValue];
  559. [RASingleton sharedInstance].currentOrderIsMerged = weakself.currentOrderIsMerged;
  560. [weakself.itemListTable reloadData];
  561. weakself.print_url = [cart_json valueForKey:@"cart_print_url"];
  562. [weakself checkProductAvailable:weakself.content_arr];
  563. [weakself refresh_total];
  564. } else {
  565. // v1.94
  566. weakself.freejson = [cart_json objectForKey:@"freeGiveaway"];
  567. weakself.notes = [cart_json valueForKey:@"general_note"];
  568. weakself.cart_json = cart_json;
  569. [weakself.itemListTable reloadData];
  570. weakself.print_url = [cart_json valueForKey:@"cart_print_url"];
  571. [weakself checkProductAvailable:weakself.content_arr];
  572. }
  573. int result_code=[[cart_json valueForKey:@"result"] intValue];
  574. if (weakself.dataOperationQueue.operationCount > 1) {
  575. weakself.isrefreshing=false;
  576. return;
  577. }
  578. if(result_code==2||result_code==1||result_code==0)
  579. {
  580. [weakself.edit_select_arr removeAllObjects];
  581. weakself.itemListTable.hidden = false;
  582. // UIApplication * app = [UIApplication sharedApplication];
  583. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  584. // appDelegate.cart_count =weakself.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  585. weakself.itemListTable.hidden=false;
  586. if(weakself.onFinishLoad)
  587. weakself.onFinishLoad();
  588. }
  589. else
  590. if(result_code==RESULT_NET_ERROR)
  591. {
  592. weakself.label_net_err.hidden=false;
  593. weakself.itemListTable.hidden=true;
  594. }
  595. else
  596. {
  597. if(result_code!=1)
  598. [RAUtils message_box:@"Loading Cart" message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  599. }
  600. weakself.isrefreshing=false;
  601. DebugLog(@"FINISH LOAD CART");
  602. // }];
  603. }];
  604. // }];
  605. });
  606. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  607. }
  608. -(void) operation_scan_reload_data
  609. {
  610. if (self.dataOperationQueue.operationCount > 1) {
  611. DebugLog(@"operationCount >1 cancel");
  612. return;
  613. }
  614. DebugLog(@"operating...");
  615. __block AppDelegate *appDelegate = nil;
  616. dispatch_sync(dispatch_get_main_queue(), ^{
  617. //some UI methods ej
  618. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  619. });
  620. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  621. #ifndef SCANNER_ORDER
  622. //扫码订单允许后建订单
  623. if (appDelegate.order_code == nil || appDelegate.order_code.length == 0) {
  624. [self clearContent];
  625. return;
  626. }
  627. #endif
  628. __weak typeof(self) weakself = self;
  629. dispatch_async(dispatch_get_main_queue(), ^{
  630. if(weakself.isrefreshing)
  631. return;
  632. weakself.available = YES;
  633. weakself.itemListTable.hidden = true;
  634. weakself.label_net_err.hidden=true;
  635. weakself.isrefreshing=true;
  636. // [self.content_data removeAllObjects];
  637. // [self.itemListTable reloadData];
  638. // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  639. //
  640. // [reF endRefreshing];
  641. //
  642. // reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  643. weakself.mum.center = weakself.view.center;
  644. weakself.mum.hidden = YES;
  645. // [self.mum startAnimating];
  646. DebugLog(@"reloading...");
  647. // __block UIAlertController *loadingView = [RAUtils waiting_alert:self title:@"Loading" completion:^{
  648. PopWaitAlert* pop = [RAUtils waiting_pop:@"Loading" completion:nil];
  649. [RANetwork request_cart:weakself.sortIndex completionHandler:^(NSMutableDictionary *result) {
  650. DebugLog(@"BEGIN LOAD CART");
  651. NSDictionary* cart_json = result;
  652. [pop hide];
  653. self.stop_response=false;
  654. //
  655. // [self.mum stopAnimating];
  656. // [loadingView dismissViewControllerAnimated:YES completion:^{
  657. // [loadingView dismissWithClickedButtonIndex:0 animated:YES];
  658. // self.content_data = [cart_json mutableCopy];
  659. #ifdef BACKORDER_PROCESS
  660. weakself.back_order_flag = [[cart_json objectForKey:@"back_order_version"] boolValue];
  661. #else
  662. weakself.back_order_flag = NO;
  663. #endif
  664. if (!weakself.back_order_flag) {
  665. weakself.freejson = [cart_json objectForKey:@"freeGiveaway"];
  666. weakself.notes = [cart_json valueForKey:@"general_note"];
  667. weakself.content_arr = [RAUtils dictionary2array:cart_json count_fields:@"count" item_mark:@"item_" items_mark:nil];
  668. weakself.currentOrderIsMerged = [[cart_json valueForKey:@"is_merged_order"] boolValue];
  669. [RASingleton sharedInstance].currentOrderIsMerged = weakself.currentOrderIsMerged;
  670. [weakself.itemListTable reloadData];
  671. weakself.print_url = [cart_json valueForKey:@"cart_print_url"];
  672. [weakself checkProductAvailable:weakself.content_arr];
  673. [weakself refresh_total];
  674. } else {
  675. // v1.94
  676. weakself.freejson = [cart_json objectForKey:@"freeGiveaway"];
  677. weakself.notes = [cart_json valueForKey:@"general_note"];
  678. weakself.cart_json = cart_json;
  679. [weakself.itemListTable reloadData];
  680. weakself.print_url = [cart_json valueForKey:@"cart_print_url"];
  681. [weakself checkProductAvailable:weakself.content_arr];
  682. }
  683. int result_code=[[cart_json valueForKey:@"result"] intValue];
  684. if (weakself.dataOperationQueue.operationCount > 1) {
  685. weakself.isrefreshing=false;
  686. return;
  687. }
  688. if(result_code==2||result_code==1||result_code==0)
  689. {
  690. [weakself.edit_select_arr removeAllObjects];
  691. weakself.itemListTable.hidden = false;
  692. // UIApplication * app = [UIApplication sharedApplication];
  693. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  694. // appDelegate.cart_count =weakself.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  695. weakself.itemListTable.hidden=false;
  696. if(weakself.onFinishLoad)
  697. weakself.onFinishLoad();
  698. }
  699. else
  700. if(result_code==RESULT_NET_ERROR)
  701. {
  702. weakself.label_net_err.hidden=false;
  703. weakself.itemListTable.hidden=true;
  704. }
  705. else
  706. {
  707. if(result_code!=1)
  708. [RAUtils message_box:@"Loading Cart" message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  709. }
  710. weakself.isrefreshing=false;
  711. DebugLog(@"FINISH LOAD CART");
  712. // }];
  713. }];
  714. // }];
  715. });
  716. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  717. }
  718. -(void)manually_refresh
  719. {
  720. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  721. if(self.isrefreshing)
  722. {
  723. [reF endRefreshing];
  724. return;
  725. }
  726. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"];
  727. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  728. // DebugLog(@"refresh!!!!!!!!");
  729. }
  730. -(void) ReloadData
  731. {
  732. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  733. [reF endRefreshing];
  734. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  735. [self reload_data];
  736. }
  737. - (void)viewDidLayoutSubviews {
  738. [super viewDidLayoutSubviews];
  739. [self layoutCartIndicatorBar];
  740. }
  741. - (DefaultTableHeaderView *)setupCartIndicatorBar {
  742. NSString* value =[DefaultAppearance get_noneappearance_value:@"DefaultTableHeaderView" valuename:@"title_text_color"];
  743. if(value==nil)
  744. value=@"";
  745. unsigned long color = strtoul([value UTF8String],0,16);
  746. DefaultTableHeaderView* myView = [[DefaultTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
  747. // myView.backgroundColor = UIColorFromRGB(0x996633);
  748. myView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  749. myView.layer.shadowPath =[UIBezierPath bezierPathWithRect:myView.bounds].CGPath;
  750. myView.layer.shadowColor = [UIColor blackColor].CGColor;
  751. myView.layer.shadowOffset = CGSizeMake(0, 0);
  752. myView.layer.shadowOpacity = 0.5;
  753. myView.layer.shadowRadius = 2.0;
  754. #ifndef SCANNER_ORDER
  755. self.sortButton.tag = 5678 + 0;
  756. [myView addSubview:self.sortButton];
  757. #endif
  758. UILabel *solabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 9, 90, 22)];
  759. solabel.tag = 5678 + 1;
  760. solabel.textColor=UIColorFromRGB(color);
  761. solabel.backgroundColor = [UIColor clearColor];
  762. solabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
  763. solabel.text=NSLocalizedString(@"Description", nil);
  764. [solabel sizeToFit];
  765. [myView addSubview:solabel];
  766. CGFloat x = self.itemListTable.bounds.size.width - (768 - 320);
  767. UILabel *contactlabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 9, 90, 22)];
  768. contactlabel.tag = 5678 + 2;
  769. contactlabel.textColor=UIColorFromRGB(color);
  770. contactlabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
  771. contactlabel.backgroundColor = [UIColor clearColor];
  772. #ifdef SCANNER_ORDER
  773. NSString* pricename;
  774. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  775. NSString* servername = [addressDic objectForKey:@"show_name"];
  776. NSArray* arrsn = [RAConvertor string2arr:servername separator:@","];
  777. NSString * shortname = arrsn[0];
  778. if(shortname.length==0)
  779. shortname = servername;
  780. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  781. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  782. // self.labelPrice0.text = price_group[@"price_0"][@"display"];//appDelegate.price0_name;
  783. // self.labelPrice1.text = price_group[@"price_1"][@"display"];//appDelegate.price1_name;
  784. switch(RASingleton.sharedInstance.price_type)
  785. {
  786. case 0:
  787. // pricename =[NSString stringWithFormat:@"%@_DDP",shortname];
  788. pricename = price_group[@"price_0"][@"display"];
  789. break;;
  790. case 1:
  791. // pricename = [NSString stringWithFormat:@"%@_WHSE",shortname];;
  792. pricename=price_group[@"price_1"][@"display"];//@"WHSE";
  793. break;;
  794. // case 2: pricename = @"25%";
  795. // break;
  796. }
  797. contactlabel.text=pricename;
  798. // contactlabel.frame= CGRectMake(x-100, 9, 90, 22);
  799. // contactlabel.tran
  800. #else
  801. contactlabel.text=NSLocalizedString(@"Unit price", nil);
  802. #endif
  803. [contactlabel sizeToFit];
  804. [myView addSubview:contactlabel];
  805. x = self.itemListTable.bounds.size.width - (768 - 470);
  806. UILabel *modellabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 9, 90, 22)];
  807. modellabel.tag = 5678 + 3;
  808. modellabel.textColor=UIColorFromRGB(color);
  809. modellabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
  810. modellabel.backgroundColor = [UIColor clearColor];
  811. modellabel.text=NSLocalizedString(@"Discount/QTY", nil);
  812. [modellabel sizeToFit];
  813. [myView addSubview:modellabel];
  814. x = self.itemListTable.bounds.size.width - (768 - 640);
  815. UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 9, 90, 22)];
  816. pricelabel.tag = 5678 + 4;
  817. pricelabel.textColor=UIColorFromRGB(color);
  818. pricelabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
  819. pricelabel.backgroundColor = [UIColor clearColor];
  820. pricelabel.text=NSLocalizedString(@"Set/Subtotal", nil);
  821. [pricelabel sizeToFit];
  822. [myView addSubview:pricelabel];
  823. return myView;
  824. }
  825. - (void)layoutCartIndicatorBar {
  826. CGFloat x = self.itemListTable.bounds.size.width - (768 - 320);
  827. UILabel *solabel = [self.cartIndicatorBar viewWithTag:5678 + 1];
  828. [solabel sizeToFit];
  829. UILabel *contactlabel = [self.cartIndicatorBar viewWithTag:5678 + 2];
  830. contactlabel.frame = CGRectMake(x, 9, 90, 22);
  831. [contactlabel sizeToFit];
  832. x = self.itemListTable.bounds.size.width - (768 - 470);
  833. UILabel *modellabel = [self.cartIndicatorBar viewWithTag:5678 + 3];
  834. modellabel.frame = CGRectMake(x, 9, 90, 22);
  835. [modellabel sizeToFit];
  836. x = self.itemListTable.bounds.size.width - (768 - 640);
  837. UILabel *pricelabel = [self.cartIndicatorBar viewWithTag:5678 + 4];
  838. pricelabel.frame = CGRectMake(x, 9, 90, 22);
  839. [pricelabel sizeToFit];
  840. }
  841. - (void)viewDidLoad
  842. {
  843. [super viewDidLoad];
  844. // if(_scanlock==nil)
  845. // _scanlock= [[NSLock alloc] init];
  846. #if defined(USE_BLE_SCANNER) || defined(USE_MFI_SCANNER)
  847. self.batch_scan=false;
  848. self.stop_response=false;
  849. #endif
  850. #ifdef BUILD_CONTRAST
  851. self.btnOrderDiscount.hidden=false;
  852. self.labelDiscount.hidden = false;
  853. #endif
  854. #ifdef SCANNER_ORDER
  855. self.printButton.hidden = true;
  856. self.emailButton.hidden = true;
  857. self.editButton.hidden = true;
  858. self.continueButton.hidden = true;
  859. self.labelTotalcuft.hidden = false;
  860. self.labelTotalWeight.hidden = true;
  861. self.labelTotalCarton.hidden = true;
  862. self.valuecuft.hidden = false;
  863. self.valueweight.hidden = true;
  864. self.valuecarton.hidden = true;
  865. #endif
  866. #ifdef RA_NOTIFICATION
  867. self.support_scanner = true;
  868. #endif
  869. self.edit_select_arr= [[NSMutableArray alloc] init];
  870. self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor;
  871. self.label_net_err.layer.borderWidth = 2.0;
  872. self.label_net_err.layer.cornerRadius=15;
  873. self.label_net_err.layer.masksToBounds=true;
  874. #if defined(USE_BLE_SCANNER) || defined(USE_MFI_SCANNER)
  875. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scanner_refresh) name:RA_NOTIFICATION_SCAN_RELOAD_DATA object:nil];
  876. #endif
  877. #ifdef SCANNER_ORDER
  878. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cartrefresh:) name:@"RA_NOTIFICATION_CART_REFRESH" object:nil];
  879. #endif
  880. #ifndef RA_NOTIFICATION
  881. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(priceTypeChanged:) name:Change_Price_Type_Notification object:nil];
  882. #endif
  883. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)];
  884. // tap.minimumPressDuration = 0.8; //定义按的时间
  885. [self.label_net_err addGestureRecognizer:tap];
  886. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  887. ref.tag = 200 ;
  888. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"];
  889. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  890. // ref.hidden = true;
  891. [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged];
  892. [self.itemListTable addSubview:ref];
  893. self.sortIndex = 0;
  894. self.currentOrderIsMerged = NO;
  895. self.toolpanel.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.toolpanel.bounds].CGPath;
  896. self.toolpanel.layer.masksToBounds = false;
  897. //添加四个边阴影
  898. self.toolpanel.layer.shadowColor = [UIColor blackColor].CGColor;
  899. self.toolpanel.layer.shadowOffset = CGSizeMake(0, 0);
  900. self.toolpanel.layer.shadowOpacity = 0.5;
  901. self.toolpanel.layer.shadowRadius = 2.0;
  902. cancelButtonFrame = self.cancelOrderButton.frame;
  903. // CGRect frame =self.orderInfoTableView.tableHeaderView.frame;
  904. // self.orderInfoTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.orderInfoTableView.bounds.size.width, 0.0001)];
  905. // self.orderInfoTableView.tableHeaderView.backgroundColor = [UIColor redColor];
  906. // // self.detailTable.sectionHeaderHeight = 0.0001;
  907. // self.orderInfoTableView.sectionFooterHeight = 0.0001;
  908. // Do any additional setup after loading the view.
  909. self.cartIndicatorBar = [self setupCartIndicatorBar];
  910. [self.view addSubview:self.cartIndicatorBar];
  911. }
  912. #ifdef SCANNER_ORDER
  913. -(void) cartrefresh: (NSNotification *)notification
  914. {
  915. [OLDataProvider scanRefreshCart];
  916. [ActiveViewController Notify:@"CartViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  917. // [self refresh_total];
  918. // [self refresh_ui];
  919. }
  920. #endif
  921. //-(void)SelectOrder:(NSString *)orderid
  922. //{
  923. //// self.editOrderID.text=orderid;
  924. //// self.btnAddToOrder.enabled = true;
  925. //}
  926. - (void)didReceiveMemoryWarning
  927. {
  928. [super didReceiveMemoryWarning];
  929. // Dispose of any resources that can be recreated.
  930. }
  931. //- (IBAction)stepperAction:(UIStepper *)sender {
  932. //
  933. //
  934. //}
  935. - (IBAction)onContinueShoppingClick:(id)sender {
  936. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  937. [appDelegate switchToPreviousVC];
  938. }
  939. - (IBAction)onOrderDiscount:(id)sender {
  940. #ifdef BUILD_CONTRAST
  941. OrderDiscountViewController * vc =[[UIStoryboard storyboardWithName:@"ERP_Mobile_Cart" bundle:nil] instantiateViewControllerWithIdentifier:@"OrderDiscountViewController"];
  942. // vc.discount=[[item_json valueForKey:@"discount"] floatValue];
  943. // vc.price = [[item_json valueForKey:@"unit_price"] floatValue];
  944. // vc.cart_id = [item_json valueForKey:@"cart_item_id"];//[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  945. // __block NSString* cartid=[item_json valueForKey:@"cart_item_id"];
  946. // vc.delegate = parentvc.self;
  947. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  948. vc.onSetValue = ^(double discount){
  949. // bool bmodify=false;
  950. // if(discount>0)
  951. // bmodify=true;
  952. // NSString* notes = [item_json valueForKey:@"note"];
  953. // if(notes.length==0)
  954. // notes=@"";
  955. // if(bmodify)
  956. // {
  957. //
  958. //
  959. // // if([notes isEqualToString:@""])
  960. //// notes=[NSString stringWithFormat:@"Note: discount %d%% off.",(int)discount];
  961. ////
  962. //// NSRange range;
  963. //// range = [notes rangeOfString:@"PRICE MODIFIED"];
  964. //// if (range.location != NSNotFound) {
  965. ////
  966. //// }else{
  967. //// notes= [notes stringByAppendingString:@"\nPRICE MODIFIED"];
  968. //// }
  969. // }
  970. // else
  971. //
  972. // {
  973. //// notes =@"";
  974. // }
  975. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Update Discount" completion:^{
  976. PopWaitAlert* pop = [RAUtils waiting_pop:@"Update Discount" completion:nil];
  977. [RANetwork request_order_discount:discount completionHandler:^(NSMutableDictionary *result) {
  978. // [waitalert dismissViewControllerAnimated:YES completion:^{
  979. [pop hide];
  980. int iresult = [result[@"result"] intValue];
  981. if(iresult==RESULT_TRUE)
  982. {
  983. [self reload_data];
  984. }
  985. else if(iresult==RESULT_NET_ERROR)
  986. {
  987. [RAUtils message_box:@"Set Order Discount" message:@"Can't connect to the server, please try again." completion:nil];
  988. }
  989. else
  990. {
  991. [RAUtils message_box:@"Set Order Discount" message:@"Can't set order discount, please contact administrator" completion:nil];
  992. }
  993. // }];
  994. }];
  995. // }];
  996. };
  997. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  998. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  999. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  1000. [self presentViewController:navi animated:YES completion:^{
  1001. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  1002. DebugLog(@"EditModelPriceViewController present.........");
  1003. // self.btop = false;
  1004. // <#code#>
  1005. }];
  1006. #endif
  1007. }
  1008. - (IBAction)onPlaceOrder:(UIButton *)sender {
  1009. #ifndef SCANNER_ORDER
  1010. if (![self isOrderAvailable]) {
  1011. return;
  1012. }
  1013. #endif
  1014. [self placeOrder];
  1015. }
  1016. -(void) scanPlaceOrder
  1017. {
  1018. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1019. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  1020. // NSString * string = [checked componentsJoinedByString:@","];
  1021. // [params setValue:string forKey:@"cart2Checkbox"];
  1022. // NSData* data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:URL_SO_ORDER ofType:@"json" ]];
  1023. // NSError *error=nil;
  1024. // NSMutableDictionary* dict=[[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1025. //
  1026. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1027. // NSString *documents = [paths objectAtIndex:0];
  1028. NSString *orderdir = [OLDataProvider getScanPath];
  1029. NSMutableDictionary* dict= [[RAUtils dictfromfile:[orderdir stringByAppendingPathComponent:@"order.json"]] mutableCopy];
  1030. int section_count = [dict[@"section_count"] intValue];
  1031. for(int i=0;i<section_count;i++)
  1032. {
  1033. NSMutableDictionary* section = [dict[[NSString stringWithFormat:@"section_%d",i]] mutableCopy];
  1034. NSString* title = section[@"title"];
  1035. if([title isEqualToString:@"Model Information"])
  1036. {
  1037. NSMutableDictionary* cart_section = RASingleton.sharedInstance.scan_cart[@"section_0"];
  1038. int cart_count = [cart_section[@"count"] intValue];
  1039. for(int j=0;j<cart_count;j++)
  1040. {
  1041. NSMutableDictionary* cart_item = [cart_section[[NSString stringWithFormat:@"item_%d",j]] mutableCopy];
  1042. cart_item[@"description"]= [NSString stringWithFormat:@"%@\n%@\n%@",cart_item[@"model"],cart_item[@"description"],cart_item[@"dimension"] ];
  1043. cart_item[@"control"]=@"model";
  1044. section[[NSString stringWithFormat:@"item_%d",j]] = cart_item;
  1045. }
  1046. section[@"count"] = @(cart_count);
  1047. dict[[NSString stringWithFormat:@"section_%d",i]] = section;
  1048. }
  1049. else
  1050. if([title isEqualToString:@"Customer"])
  1051. {
  1052. int scount = [section[@"count"] intValue];
  1053. for(int j=0;j<scount;j++)
  1054. {
  1055. NSMutableDictionary * item = [section[[NSString stringWithFormat:@"item_%d",j]] mutableCopy];
  1056. NSString * name = item[@"name"];
  1057. if([name isEqualToString:@"business_card"])
  1058. {
  1059. NSArray*arr =[RAConvertor string2arr:appDelegate.customerInfo[name] separator:@","];
  1060. for(int l=0;l<arr.count;l++)
  1061. {
  1062. item[[NSString stringWithFormat:@"img_url_%d",l] ]=arr[l];
  1063. }
  1064. }
  1065. else
  1066. {
  1067. if(item[@"value"]==nil ||[item[@"value"] stringValue].length==0) //order 内没有值才从customer 复制
  1068. item[@"value"]=appDelegate.customerInfo[name];
  1069. }
  1070. section[[NSString stringWithFormat:@"item_%d",j]] = item;
  1071. }
  1072. // NSDictionary* item_0=@{@"control":@"text",@"name":@"customer_cid",@"aname":@"Contact",@"value":appDelegate.contact_id};
  1073. dict[[NSString stringWithFormat:@"section_%d",i]] = section;
  1074. }
  1075. else
  1076. if([title isEqualToString:@"Remarks Content"])
  1077. {
  1078. int scount = [section[@"count"] intValue];
  1079. for(int j=0;j<scount;j++)
  1080. {
  1081. NSMutableDictionary * item = [section[[NSString stringWithFormat:@"item_%d",j]] mutableCopy];
  1082. NSString * name = item[@"name"];
  1083. if([name isEqualToString:@"comments"])
  1084. {
  1085. item[@"value"] = RASingleton.sharedInstance.scan_cart[@"general_note"];
  1086. section[[NSString stringWithFormat:@"item_%d",j]] = item;
  1087. }
  1088. }
  1089. // NSDictionary* item_0=@{@"control":@"text",@"name":@"customer_cid",@"aname":@"Contact",@"value":appDelegate.contact_id};
  1090. dict[[NSString stringWithFormat:@"section_%d",i]] = section;
  1091. }
  1092. else if([title isEqualToString:@"Order Total"])
  1093. {
  1094. int scount = [section[@"count"] intValue];
  1095. for(int j=0;j<scount;j++)
  1096. {
  1097. NSMutableDictionary * item = [section[[NSString stringWithFormat:@"item_%d",j]] mutableCopy];
  1098. NSString * name = item[@"name"];
  1099. if([name isEqualToString:@"totalPrice"])
  1100. {
  1101. item[@"value"] = [RAConvertor currencyNumber:self.total+self.surcharge];//[NSString stringWithFormat:@"%.2f",self.total+self.surcharge];
  1102. section[[NSString stringWithFormat:@"item_%d",j]] = item;
  1103. }
  1104. else if([name isEqualToString:@"subtotalPrice"])
  1105. {
  1106. item[@"value"] = [RAConvertor currencyNumber:self.total];//[NSString stringWithFormat:@"%.2f",self.total];
  1107. section[[NSString stringWithFormat:@"item_%d",j]] = item;
  1108. }
  1109. else if([name isEqualToString:@"surcharge"])
  1110. {
  1111. item[@"value"] = [RAConvertor currencyNumber:self.surcharge];//[NSString stringWithFormat:@"%.2f",self.surcharge];
  1112. section[[NSString stringWithFormat:@"item_%d",j]] = item;
  1113. }
  1114. }
  1115. // NSDictionary* item_0=@{@"control":@"text",@"name":@"customer_cid",@"aname":@"Contact",@"value":appDelegate.contact_id};
  1116. dict[[NSString stringWithFormat:@"section_%d",i]] = section;
  1117. }
  1118. }
  1119. // NSString *path = NSTemporaryDirectory();
  1120. //
  1121. // NSString* file=[NSString stringWithFormat:@"SO_%@.json",appDelegate.order_code];
  1122. // NSString *filePath = [path stringByAppendingPathComponent:file];
  1123. //
  1124. //
  1125. //
  1126. [RAUtils dicttofile:[orderdir stringByAppendingPathComponent:@"order.json"] dict:dict];
  1127. CreateOrderViewController * orderinfoVC = [[CreateOrderViewController alloc] initWithNibName:@"CommonEditor.iPad" bundle:nil];
  1128. orderinfoVC.disable_dropdown_refresh=true;
  1129. orderinfoVC.url_type = URL_FILE;
  1130. orderinfoVC.request_url=[orderdir stringByAppendingPathComponent:@"order.json"];
  1131. orderinfoVC.params = params;
  1132. orderinfoVC.title=@"";
  1133. orderinfoVC.delegate=self;
  1134. //
  1135. // if(checked.count==count)
  1136. // {
  1137. // orderinfoVC.have_tail = true
  1138. // }
  1139. [self.navigationController pushViewController:orderinfoVC animated:true];
  1140. return;
  1141. }
  1142. - (IBAction)onBulkDiscount:(id)sender {
  1143. #ifdef SCANNER_ORDER
  1144. ScanDiscountViewController * vc =[[UIStoryboard storyboardWithName:@"SO" bundle:nil] instantiateViewControllerWithIdentifier:@"ScanDiscountViewController"];
  1145. vc.bulk = true;
  1146. vc.discount=0;
  1147. // vc.price = 0;
  1148. // vc.cart_id = [item_json valueForKey:@"cart_item_id"];//[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1149. // __block NSString* cartid=[item_json valueForKey:@"cart_item_id"];
  1150. // vc.delegate = parentvc.self;
  1151. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  1152. vc.onSetValue = ^(double price,double discount){
  1153. bool bmodify=false;
  1154. if(discount>0)
  1155. bmodify=true;
  1156. __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Update Price" completion:^{
  1157. [RANetwork request_scan_bulkdiscount:discount notes:nil completionHandler:^(NSMutableDictionary *result) {
  1158. NSDictionary* cart_json = result;
  1159. [waitalert dismissViewControllerAnimated:YES completion:^{
  1160. if([[cart_json valueForKey:@"result"] intValue]==2)
  1161. {
  1162. [self reload_data];
  1163. #ifdef RA_NOTIFICATION
  1164. [ActiveViewController Notify:@"OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1165. #else
  1166. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1167. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  1168. #endif
  1169. }
  1170. else
  1171. {
  1172. [RAUtils message_box:@"Update Price" message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  1173. }
  1174. }];
  1175. }];
  1176. }];
  1177. };
  1178. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  1179. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  1180. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  1181. [self presentViewController:navi animated:YES completion:^{
  1182. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  1183. DebugLog(@"EditModelPriceViewController present.........");
  1184. // self.btop = false;
  1185. // <#code#>
  1186. }];
  1187. #endif
  1188. }
  1189. - (IBAction)onChangePrice:(id)sender {
  1190. #ifdef SCANNER_ORDER
  1191. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1192. if(RASingleton.sharedInstance.scan_cart ==nil)
  1193. {
  1194. // NSData* json =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:URL_SO_CART ofType:@"json" ]];
  1195. NSMutableDictionary* cartTemplate=[OLDataProvider loadScanTemplate:@"scan_cart.json"];
  1196. RASingleton.sharedInstance.scan_cart=cartTemplate;//[[RAConvertor data2dict:json] mutableCopy];
  1197. }
  1198. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  1199. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Change Price Type" message:nil preferredStyle:UIAlertControllerStyleAlert];
  1200. UIAlertAction *action1 = [UIAlertAction actionWithTitle:price_group[@"price_0"][@"display"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  1201. self.btnBulkDiscount.hidden = true;
  1202. // self.btnPrice.titleLabel.text = @"FOB CTNR";
  1203. [self.btnPrice setTitle: price_group[@"price_0"][@"display"] forState:UIControlStateNormal];
  1204. RASingleton.sharedInstance.price_type=0;
  1205. NSMutableDictionary* section = [RASingleton.sharedInstance.scan_cart[@"section_0"] mutableCopy];
  1206. int count = [section[@"count"] intValue];
  1207. for(int i=0;i<count;i++)
  1208. {
  1209. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  1210. item[@"price"]=item[ price_group[@"price_0"][@"name"]];
  1211. item[@"unit_price"]= item[price_group[@"price_0"][@"name"]];
  1212. item[@"erp_unit_price"]= item[price_group[@"price_0"][@"name"]];
  1213. item[@"discount"]=@(0);
  1214. item[@"note"]=@"";
  1215. section[[NSString stringWithFormat:@"item_%d",i]] = item;
  1216. }
  1217. RASingleton.sharedInstance.scan_cart[@"section_0"] = section;
  1218. RASingleton.sharedInstance.scan_cart[@"price_type"] = @0;
  1219. [OLDataProvider saveScanCart:RASingleton.sharedInstance.scan_cart];
  1220. [self.cartIndicatorBar removeFromSuperview];
  1221. self.cartIndicatorBar = [self setupCartIndicatorBar];
  1222. [self.view addSubview:self.cartIndicatorBar];
  1223. [self reload_data];
  1224. }];
  1225. UIAlertAction *action2 = [UIAlertAction actionWithTitle:price_group[@"price_1"][@"display"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  1226. self.btnBulkDiscount.hidden = false;
  1227. [self.btnPrice setTitle: price_group[@"price_1"][@"display"] forState:UIControlStateNormal];
  1228. RASingleton.sharedInstance.price_type=1;
  1229. NSMutableDictionary* section = [RASingleton.sharedInstance.scan_cart[@"section_0"] mutableCopy];
  1230. int count = [section[@"count"] intValue];
  1231. for(int i=0;i<count;i++)
  1232. {
  1233. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  1234. NSString * unit_price;
  1235. if(item [@"net_price"])
  1236. unit_price=item[price_group[@"price_3"][@"name"]];
  1237. else if(item [@"special_price"])
  1238. {
  1239. unit_price= item[price_group[@"price_2"][@"name"]];
  1240. }
  1241. else
  1242. {
  1243. unit_price= item[price_group[@"price_1"][@"name"]];
  1244. }
  1245. if([unit_price isEqualToString:@"N/A"])
  1246. unit_price = @"0";
  1247. else
  1248. {
  1249. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  1250. }
  1251. item[@"unit_price"] = unit_price;
  1252. item[@"erp_unit_price"] = unit_price;
  1253. item[@"price"]= unit_price;//item[@"price1"];
  1254. // item[@"unit_price"]= item[@"price1"];
  1255. section[[NSString stringWithFormat:@"item_%d",i]] = item;
  1256. }
  1257. RASingleton.sharedInstance.scan_cart[@"price_type"] = @1;
  1258. RASingleton.sharedInstance.scan_cart[@"section_0"] = section;
  1259. [OLDataProvider saveScanCart:RASingleton.sharedInstance.scan_cart];
  1260. [self.cartIndicatorBar removeFromSuperview];
  1261. self.cartIndicatorBar = [self setupCartIndicatorBar];
  1262. [self.view addSubview:self.cartIndicatorBar];
  1263. [self reload_data];
  1264. }];
  1265. UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"%25" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  1266. // self.btnPrice.titleLabel.text = @"%25";
  1267. [self.btnPrice setTitle: @"%25" forState:UIControlStateNormal];
  1268. RASingleton.sharedInstance.price_type=2;
  1269. NSMutableDictionary* section = [RASingleton.sharedInstance.scan_cart[@"section_0"] mutableCopy];
  1270. int count = [section[@"count"] intValue];
  1271. for(int i=0;i<count;i++)
  1272. {
  1273. NSMutableDictionary* item = [section[[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  1274. item[@"price"]= item[price_group[@"price_2"][@"name"]];
  1275. item[@"unit_price"]= item[price_group[@"price_2"][@"name"]];
  1276. section[[NSString stringWithFormat:@"item_%d",i]] = item;
  1277. }
  1278. RASingleton.sharedInstance.scan_cart[@"section_0"] = section;
  1279. RASingleton.sharedInstance.scan_cart[@"price_type"] = @2;
  1280. [OLDataProvider saveScanCart:RASingleton.sharedInstance.scan_cart];
  1281. [self.cartIndicatorBar removeFromSuperview];
  1282. self.cartIndicatorBar = [self setupCartIndicatorBar];
  1283. [self.view addSubview:self.cartIndicatorBar];
  1284. [self reload_data];
  1285. }];
  1286. UIAlertAction *alertCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  1287. }];
  1288. [alertControl addAction:action1];
  1289. [alertControl addAction:action2];
  1290. // [alertControl addAction:action3];
  1291. // [alertControl addAction:alertthree];
  1292. [alertControl addAction:alertCancel];
  1293. [self presentViewController:alertControl animated:YES completion:nil];
  1294. #endif
  1295. }
  1296. //-(void) newoloorder
  1297. //{
  1298. //
  1299. // NSString *orderdir =[OLDataProvider getScanPath];
  1300. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1301. // appDelegate.can_set_cart_price = true;
  1302. // appDelegate.can_see_price = true;
  1303. //
  1304. //
  1305. // {
  1306. //
  1307. //
  1308. // if(appDelegate.user_type== USER_ROLE_EMPLOYEE)
  1309. // {
  1310. //
  1311. //
  1312. //
  1313. // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Select Customer type" message:@"" preferredStyle:UIAlertControllerStyleAlert];
  1314. // //block代码块取代了delegate
  1315. //
  1316. //
  1317. //
  1318. // UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Existing Customer" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  1319. //
  1320. // NSMutableDictionary* dict = nil;
  1321. //
  1322. //// NSMutableDictionary* dict = nil;
  1323. //
  1324. // NSString* ordertemplate = nil;
  1325. // if([RADataProvider getSiteHasERP])
  1326. // ordertemplate =@"e_order.json";
  1327. // else
  1328. // ordertemplate =@"e_order_noerp.json";
  1329. //
  1330. // dict=[OLDataProvider loadScanTemplate:ordertemplate];
  1331. //
  1332. // NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1333. //
  1334. //
  1335. //
  1336. // [RAUtils dicttofile:orderPath dict:dict];
  1337. //
  1338. //
  1339. //
  1340. //
  1341. //
  1342. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  1343. //
  1344. //
  1345. //
  1346. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  1347. //
  1348. // [self scanPlaceOrder];
  1349. //
  1350. // }];
  1351. //
  1352. // UIAlertAction *alert2 = [UIAlertAction actionWithTitle:@"New customer" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  1353. // NSMutableDictionary* dict = nil;
  1354. // dict=[OLDataProvider loadScanTemplate:@"n_order.json"];
  1355. //
  1356. // NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1357. //
  1358. //
  1359. //
  1360. // [RAUtils dicttofile:orderPath dict:dict];
  1361. //
  1362. //
  1363. //
  1364. //
  1365. //
  1366. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  1367. //
  1368. //
  1369. //
  1370. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  1371. // [self scanPlaceOrder];
  1372. // }];
  1373. // UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  1374. // appDelegate.order_code = nil;
  1375. // DebugLog(@"No");
  1376. // }];
  1377. // [alertControl addAction:actionOne];
  1378. // [alertControl addAction:alert2];
  1379. // [alertControl addAction:alertthree];
  1380. //
  1381. // //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  1382. //
  1383. //
  1384. // [self presentViewController:alertControl animated:YES completion:nil];
  1385. //
  1386. // DebugLog(@"%@",self.edit_select_arr);
  1387. //
  1388. //
  1389. // }
  1390. // else
  1391. // {
  1392. // NSMutableDictionary* dict = nil;
  1393. //
  1394. // if([appDelegate.customerInfo[@"customer_cid"] stringValue].length ==0 )
  1395. // {
  1396. // dict=[OLDataProvider loadScanTemplate:@"nc_order.json"];
  1397. // }
  1398. // else
  1399. // dict=[OLDataProvider loadScanTemplate:@"c_order.json"];
  1400. //
  1401. // NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1402. //
  1403. //
  1404. //
  1405. // [RAUtils dicttofile:orderPath dict:dict];
  1406. //
  1407. //
  1408. //
  1409. //
  1410. //
  1411. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  1412. //
  1413. //
  1414. //
  1415. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  1416. // [self scanPlaceOrder];
  1417. // }
  1418. //
  1419. //
  1420. //// NSMutableDictionary* dict=[OLDataProvider loadScanTemplate:@"scan_order.json"];
  1421. //
  1422. //
  1423. //
  1424. //
  1425. //
  1426. //
  1427. // }
  1428. //// MainViewController * mainvc=(MainViewController * )appDelegate.main_vc;
  1429. //// mainvc.btnScan.enabled = true;
  1430. //
  1431. //// [appDelegate updateScanButton:true];
  1432. //
  1433. //
  1434. //
  1435. //}
  1436. //-(void) newoloorder1
  1437. //{
  1438. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1439. //
  1440. // appDelegate.can_set_cart_price = true;
  1441. // appDelegate.can_see_price = true;
  1442. //
  1443. //// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1444. //// NSString *documents = [paths objectAtIndex:0];
  1445. //
  1446. // {
  1447. //
  1448. //
  1449. // NSString *orderdir =[OLDataProvider getScanPath];
  1450. //
  1451. // if(appDelegate.user_type== USER_ROLE_EMPLOYEE)
  1452. // {
  1453. //
  1454. //
  1455. //
  1456. // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Select Customer type" message:@"" preferredStyle:UIAlertControllerStyleAlert];
  1457. // //block代码块取代了delegate
  1458. //
  1459. //
  1460. //
  1461. // UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Existing Customer" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  1462. //
  1463. // NSMutableDictionary* dict = nil;
  1464. //
  1465. // NSString* ordertemplate = nil;
  1466. // if([RADataProvider getSiteHasERP])
  1467. // ordertemplate =@"e_order.json";
  1468. // else
  1469. // ordertemplate =@"e_order_noerp.json";
  1470. //
  1471. // dict=[OLDataProvider loadScanTemplate:ordertemplate];
  1472. //
  1473. // NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1474. //
  1475. //
  1476. //
  1477. // [RAUtils dicttofile:orderPath dict:dict];
  1478. //
  1479. //
  1480. //
  1481. //
  1482. //
  1483. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  1484. //
  1485. //
  1486. //
  1487. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  1488. //
  1489. // [self scanPlaceOrder];
  1490. //
  1491. // }];
  1492. //
  1493. // UIAlertAction *alert2 = [UIAlertAction actionWithTitle:@"New customer" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  1494. // NSMutableDictionary* dict = nil;
  1495. // dict=[OLDataProvider loadScanTemplate:@"n_order.json"];
  1496. //
  1497. // NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1498. //
  1499. //
  1500. //
  1501. // [RAUtils dicttofile:orderPath dict:dict];
  1502. //
  1503. //
  1504. //
  1505. //
  1506. //
  1507. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  1508. //
  1509. //
  1510. //
  1511. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  1512. // [self scanPlaceOrder];
  1513. // }];
  1514. // UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  1515. // appDelegate.order_code = nil;
  1516. // DebugLog(@"No");
  1517. // }];
  1518. // [alertControl addAction:actionOne];
  1519. // [alertControl addAction:alert2];
  1520. // [alertControl addAction:alertthree];
  1521. //
  1522. // //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  1523. //
  1524. //
  1525. // [self presentViewController:alertControl animated:YES completion:nil];
  1526. //
  1527. // DebugLog(@"%@",self.edit_select_arr);
  1528. //
  1529. //
  1530. // }
  1531. // else
  1532. // {
  1533. // NSMutableDictionary* dict = nil;
  1534. // if([appDelegate.customerInfo[@"customer_cid"] stringValue].length ==0 )
  1535. // {
  1536. // dict=[OLDataProvider loadScanTemplate:@"nc_order.json"];
  1537. // }
  1538. // else
  1539. // dict=[OLDataProvider loadScanTemplate:@"c_order.json"];
  1540. //
  1541. // NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1542. //
  1543. //
  1544. //
  1545. // [RAUtils dicttofile:orderPath dict:dict];
  1546. //
  1547. //
  1548. //
  1549. //
  1550. //
  1551. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  1552. //
  1553. //
  1554. //
  1555. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  1556. // [self scanPlaceOrder];
  1557. // }
  1558. //
  1559. //
  1560. //// NSMutableDictionary* dict=[OLDataProvider loadScanTemplate:@"scan_order.json"];
  1561. //
  1562. //
  1563. //
  1564. //
  1565. //
  1566. //
  1567. // }
  1568. //// MainViewController * mainvc=(MainViewController * )appDelegate.main_vc;
  1569. //// mainvc.btnScan.enabled = true;
  1570. //
  1571. //// [appDelegate updateScanButton:true];
  1572. //
  1573. //
  1574. //
  1575. //}
  1576. //-(void)newScanOrder1
  1577. //{
  1578. //
  1579. ////
  1580. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1581. ////
  1582. ////
  1583. //// if(appDelegate.order_code.length>0)
  1584. //// {
  1585. ////// 当前订单先关闭
  1586. ////// [OLDataProvider deleteScanOrder:appDelegate.order_code];
  1587. //// appDelegate.order_code = nil;
  1588. //// appDelegate.customerInfo = nil;
  1589. //// RASingleton.sharedInstance.scan_cart = nil;
  1590. //// appDelegate.contact_id = nil;
  1591. ////// [appDelegate updateScanButton:false];
  1592. ////
  1593. //// [appDelegate update_count_mark];
  1594. //// [appDelegate closeOrder];
  1595. ////
  1596. //// }
  1597. ////
  1598. ////
  1599. // {
  1600. // [self newoloorder1];
  1601. //
  1602. // }
  1603. //}
  1604. //-(void)newScanOrder
  1605. //{
  1606. //
  1607. //
  1608. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1609. //
  1610. //
  1611. // {
  1612. // [self newoloorder];
  1613. //
  1614. // }
  1615. //}
  1616. -(void)placeOrder
  1617. {
  1618. //
  1619. //#ifndef SCANNER_ORDER
  1620. // if (appDelegate.user_type == USER_ROLE_CUSTOMER && appDelegate.customer_type == CustomerTypeStore) {
  1621. //
  1622. //
  1623. // if (![RASingleton sharedInstance].permissions_edit_order) {
  1624. //
  1625. // return;
  1626. // }
  1627. //
  1628. //
  1629. //
  1630. // }
  1631. //#endif
  1632. // NSMutableArray* checked = [[NSMutableArray alloc] init];
  1633. bool have_free = false;
  1634. long count = self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  1635. if(count==0)
  1636. {
  1637. [RAUtils message_box:@"Cart Is Empty." message:@"Add some model first." completion:nil] ;
  1638. return;
  1639. }
  1640. for(int i=0;i<count;i++)
  1641. {
  1642. NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  1643. // bool check = [[item_json valueForKey:@"check"] boolValue];
  1644. NSString* product_id = [item_json valueForKey:@"product_id"];
  1645. // if(check)
  1646. // {
  1647. // [checked addObject:[NSNumber numberWithInt:[[item_json valueForKey:@"cart_item_id"] intValue]]];
  1648. //
  1649. // }
  1650. if(product_id.length==0)
  1651. {
  1652. [RAUtils message_box:@"No Such Model" message:@"Some models in cart does not exist, you must remove them before place order." completion:nil] ;
  1653. return;
  1654. }
  1655. bool is_free = [[item_json valueForKey:@"is_free"]boolValue];
  1656. if(is_free)
  1657. have_free=true;
  1658. }
  1659. #ifdef SCANNER_ORDER
  1660. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1661. if(appDelegate.bLogin==false)
  1662. {
  1663. LoginViewController * loginvc =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"];
  1664. // loginvc.delegate = self;
  1665. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  1666. loginvc.returnValue = ^(bool blogin){
  1667. if(blogin)
  1668. {
  1669. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1670. if(appDelegate.order_code.length==0)
  1671. {
  1672. [self newScanOrder];
  1673. }
  1674. }
  1675. };
  1676. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  1677. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  1678. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  1679. [self presentViewController:navi animated:YES completion:^{
  1680. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  1681. DebugLog(@"LoginViewController present.........");
  1682. // self.btop = false;
  1683. // <#code#>
  1684. }];
  1685. return;
  1686. }
  1687. else
  1688. {
  1689. NSString *orderdir =[OLDataProvider getScanPath];
  1690. NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  1691. if(appDelegate.order_code.length==0 )
  1692. {
  1693. [self newScanOrder];
  1694. }
  1695. else if( ![[NSFileManager defaultManager] fileExistsAtPath:orderPath])
  1696. {
  1697. [self newScanOrder1];
  1698. }
  1699. else
  1700. {
  1701. [self scanPlaceOrder];
  1702. //place order
  1703. }
  1704. return;
  1705. }
  1706. // if(RASingleton.sharedInstance.enable_OfflineOrder)
  1707. // {
  1708. //
  1709. // NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  1710. // // NSString * string = [checked componentsJoinedByString:@","];
  1711. // // [params setValue:string forKey:@"cart2Checkbox"];
  1712. //
  1713. //// NSData* data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:URL_SO_ORDER ofType:@"json" ]];
  1714. //// NSError *error=nil;
  1715. //// NSMutableDictionary* dict=[[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1716. ////
  1717. //
  1718. //
  1719. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  1720. // NSString *documents = [paths objectAtIndex:0];
  1721. //
  1722. //
  1723. //
  1724. // NSString *orderdir = [documents stringByAppendingPathComponent:appDelegate.order_code];
  1725. //
  1726. // NSMutableDictionary* dict= [[RAUtils dictfromfile:[orderdir stringByAppendingPathComponent:@"order.json"]] mutableCopy];
  1727. //
  1728. // int section_count = [dict[@"section_count"] intValue];
  1729. //
  1730. // for(int i=0;i<section_count;i++)
  1731. // {
  1732. // NSMutableDictionary* section = [dict[[NSString stringWithFormat:@"section_%d",i]] mutableCopy];
  1733. // NSString* title = section[@"title"];
  1734. // if([title isEqualToString:@"Model Information"])
  1735. // {
  1736. //
  1737. // NSMutableDictionary* cart_section = RASingleton.sharedInstance.scan_cart[@"section_0"];
  1738. // int cart_count = [cart_section[@"count"] intValue];
  1739. // for(int j=0;j<cart_count;j++)
  1740. // {
  1741. // NSMutableDictionary* cart_item = [cart_section[[NSString stringWithFormat:@"item_%d",j]] mutableCopy];
  1742. // cart_item[@"control"]=@"model";
  1743. // section[[NSString stringWithFormat:@"item_%d",j]] = cart_item;
  1744. //
  1745. // }
  1746. // section[@"count"] = @(cart_count);
  1747. // dict[[NSString stringWithFormat:@"section_%d",i]] = section;
  1748. // }
  1749. // else
  1750. // if([title isEqualToString:@"Customer"])
  1751. // {
  1752. //
  1753. // int scount = [section[@"count"] intValue];
  1754. // for(int j=0;j<scount;j++)
  1755. // {
  1756. // NSMutableDictionary * item = [section[[NSString stringWithFormat:@"item_%d",j]] mutableCopy];
  1757. // NSString * name = item[@"name"];
  1758. //
  1759. // if([name isEqualToString:@"business_card"])
  1760. // {
  1761. // NSArray*arr =[RAConvertor string2arr:appDelegate.customerInfo[name] separator:@","];
  1762. // for(int l=0;l<arr.count;l++)
  1763. // {
  1764. // item[[NSString stringWithFormat:@"img_url_%d",l] ]=arr[l];
  1765. // }
  1766. // }
  1767. // else
  1768. // item[@"value"]=appDelegate.customerInfo[name];
  1769. //
  1770. // section[[NSString stringWithFormat:@"item_%d",j]] = item;
  1771. // }
  1772. //
  1773. //// NSDictionary* item_0=@{@"control":@"text",@"name":@"customer_cid",@"aname":@"Contact",@"value":appDelegate.contact_id};
  1774. // dict[[NSString stringWithFormat:@"section_%d",i]] = section;
  1775. //
  1776. //
  1777. // }
  1778. // else
  1779. // if([title isEqualToString:@"Remarks Content"])
  1780. // {
  1781. //
  1782. // int scount = [section[@"count"] intValue];
  1783. // for(int j=0;j<scount;j++)
  1784. // {
  1785. // NSMutableDictionary * item = [section[[NSString stringWithFormat:@"item_%d",j]] mutableCopy];
  1786. // NSString * name = item[@"name"];
  1787. //
  1788. //
  1789. // if([name isEqualToString:@"comments"])
  1790. // {
  1791. // item[@"value"] = RASingleton.sharedInstance.scan_cart[@"general_note"];
  1792. // section[[NSString stringWithFormat:@"item_%d",j]] = item;
  1793. // }
  1794. //
  1795. //
  1796. // }
  1797. //
  1798. // // NSDictionary* item_0=@{@"control":@"text",@"name":@"customer_cid",@"aname":@"Contact",@"value":appDelegate.contact_id};
  1799. // dict[[NSString stringWithFormat:@"section_%d",i]] = section;
  1800. //
  1801. //
  1802. // }
  1803. // else if([title isEqualToString:@"Order Total"])
  1804. // {
  1805. //
  1806. // int scount = [section[@"count"] intValue];
  1807. // for(int j=0;j<scount;j++)
  1808. // {
  1809. // NSMutableDictionary * item = [section[[NSString stringWithFormat:@"item_%d",j]] mutableCopy];
  1810. // NSString * name = item[@"name"];
  1811. //
  1812. //
  1813. // if([name isEqualToString:@"totalPrice"])
  1814. // {
  1815. // item[@"value"] = [NSString stringWithFormat:@"%.2f",self.total];
  1816. // section[[NSString stringWithFormat:@"item_%d",j]] = item;
  1817. // }
  1818. //
  1819. //
  1820. // }
  1821. //
  1822. //// NSDictionary* item_0=@{@"control":@"text",@"name":@"customer_cid",@"aname":@"Contact",@"value":appDelegate.contact_id};
  1823. // dict[[NSString stringWithFormat:@"section_%d",i]] = section;
  1824. //
  1825. //
  1826. //
  1827. // }
  1828. //
  1829. //
  1830. //
  1831. //
  1832. // }
  1833. //
  1834. //// NSString *path = NSTemporaryDirectory();
  1835. ////
  1836. //// NSString* file=[NSString stringWithFormat:@"SO_%@.json",appDelegate.order_code];
  1837. //// NSString *filePath = [path stringByAppendingPathComponent:file];
  1838. ////
  1839. ////
  1840. ////
  1841. // [RAUtils dicttofile:[orderdir stringByAppendingPathComponent:@"order.json"] dict:dict];
  1842. //
  1843. // CreateOrderViewController * orderinfoVC = [[CreateOrderViewController alloc] initWithNibName:@"CommonEditor.iPad" bundle:nil];
  1844. //
  1845. //
  1846. //
  1847. // orderinfoVC.disable_dropdown_refresh=true;
  1848. // orderinfoVC.url_type = URL_FILE;
  1849. // orderinfoVC.request_url=[orderdir stringByAppendingPathComponent:@"order.json"];
  1850. //
  1851. // orderinfoVC.params = params;
  1852. // orderinfoVC.title=@"";
  1853. // orderinfoVC.delegate=self;
  1854. // //
  1855. // // if(checked.count==count)
  1856. // // {
  1857. // // orderinfoVC.have_tail = true
  1858. // // }
  1859. //
  1860. // [self.navigationController pushViewController:orderinfoVC animated:true];
  1861. //
  1862. // return;
  1863. // }
  1864. #endif
  1865. #ifdef BACKORDER_PROCESS
  1866. if (self.back_order_flag) {
  1867. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  1868. [params setObject:@(0) forKey:@"step"];
  1869. RAOrderEditorViewController *orderEditorVC =[[UIStoryboard storyboardWithName:@"ERP_Mobile_Order" bundle:nil] instantiateViewControllerWithIdentifier:@"RAOrderEditorViewController"];
  1870. //[[RAOrderEditorViewController alloc] initWithNibName:@"CommonEditor.iPad" bundle:nil];
  1871. orderEditorVC.disable_dropdown_refresh=true;
  1872. orderEditorVC.url_type = URL_REMOTE;
  1873. orderEditorVC.request_url=URL_CARTDELIVERY;
  1874. orderEditorVC.params = params;
  1875. orderEditorVC.delegate=self;
  1876. orderEditorVC.title=@"";
  1877. [self.navigationController pushViewController:orderEditorVC animated:true];
  1878. }
  1879. else
  1880. #endif
  1881. {
  1882. NSMutableDictionary * params= [[NSMutableDictionary alloc] init];
  1883. // NSString * string = [checked componentsJoinedByString:@","];
  1884. // [params setValue:string forKey:@"cart2Checkbox"];
  1885. CreateOrderViewController * orderinfoVC = [[CreateOrderViewController alloc] initWithNibName:@"CommonEditor.iPad" bundle:nil];
  1886. orderinfoVC.disable_dropdown_refresh=true;
  1887. orderinfoVC.url_type = URL_REMOTE;
  1888. orderinfoVC.request_url=URL_CARTDELIVERY;
  1889. orderinfoVC.params = params;
  1890. orderinfoVC.title=@"";
  1891. orderinfoVC.delegate=self;
  1892. //
  1893. // if(checked.count==count)
  1894. // {
  1895. // orderinfoVC.have_tail = true
  1896. // }
  1897. [self.navigationController pushViewController:orderinfoVC animated:true];
  1898. }
  1899. }
  1900. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  1901. {
  1902. return NO;
  1903. }
  1904. - (IBAction)onScanDiscount:(UIButton*)sender {
  1905. #ifdef SCANNER_ORDER
  1906. UITableViewCell* cell =(UITableViewCell*) sender.superview.superview;
  1907. NSIndexPath * indexPath = [self.itemListTable indexPathForCell:cell];
  1908. UIViewController* parentvc=self;
  1909. // EditModelPriceViewController
  1910. ScanDiscountViewController * vc =[[UIStoryboard storyboardWithName:@"SO" bundle:nil] instantiateViewControllerWithIdentifier:@"ScanDiscountViewController"];
  1911. NSMutableDictionary * item_json = nil;
  1912. if (self.back_order_flag) {
  1913. item_json = [[self itemJsonAtIndexPath:indexPath] mutableCopy];
  1914. } else {
  1915. item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  1916. }
  1917. vc.discount=[[item_json valueForKey:@"discount"] floatValue];
  1918. vc.price = [[item_json valueForKey:@"unit_price"] floatValue];
  1919. vc.cart_id = [item_json valueForKey:@"cart_item_id"];//[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  1920. __block NSString* cartid=[item_json valueForKey:@"cart_item_id"];
  1921. // vc.delegate = parentvc.self;
  1922. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  1923. vc.onSetValue = ^(double price,double discount){
  1924. bool bmodify=false;
  1925. if(discount>0)
  1926. bmodify=true;
  1927. // NSString* notes = [item_json valueForKey:@"note"];
  1928. // if(notes.length==0)
  1929. // notes=@"";
  1930. // if(bmodify)
  1931. // {
  1932. //
  1933. //
  1934. // // if([notes isEqualToString:@""])
  1935. //// notes=[NSString stringWithFormat:@"Note: discount %d%% off.",(int)discount];
  1936. ////
  1937. //// NSRange range;
  1938. //// range = [notes rangeOfString:@"PRICE MODIFIED"];
  1939. //// if (range.location != NSNotFound) {
  1940. ////
  1941. //// }else{
  1942. //// notes= [notes stringByAppendingString:@"\nPRICE MODIFIED"];
  1943. //// }
  1944. // }
  1945. // else
  1946. //
  1947. // {
  1948. //// notes =@"";
  1949. // }
  1950. __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Update Price" completion:^{
  1951. [RANetwork request_update_cart_modelprice:cartid price:price discount:discount notes:nil completionHandler:^(NSMutableDictionary *result) {
  1952. NSDictionary* cart_json = result;
  1953. [waitalert dismissViewControllerAnimated:YES completion:^{
  1954. if([[cart_json valueForKey:@"result"] intValue]==2)
  1955. {
  1956. // [item_json setValue:[NSNumber numberWithDouble:price ] forKey:@"unit_price"];
  1957. //
  1958. // [item_json setValue:[NSNumber numberWithDouble:discount ] forKey:@"discount"];
  1959. // [item_json setValue:notes forKey:@"note"];
  1960. // RASingleton.sharedInstance.scan_cart[@"section_0"][[NSString stringWithFormat:@"item_%d",indexPath.row]]=item_json;
  1961. // if (self.back_order_flag) {
  1962. // [self updateItemJson:item_json atIndexPath:indexPath];
  1963. // } else {
  1964. // self.content_arr[indexPath.row ] = item_json;
  1965. // }
  1966. // tableView.editing = false;
  1967. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  1968. // [self refresh_total];
  1969. [self reload_data];
  1970. #ifdef RA_NOTIFICATION
  1971. [ActiveViewController Notify:@"OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  1972. #else
  1973. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  1974. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  1975. #endif
  1976. }
  1977. else
  1978. {
  1979. [RAUtils message_box:@"Update Price" message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  1980. }
  1981. }];
  1982. }];
  1983. }];
  1984. };
  1985. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  1986. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  1987. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  1988. [parentvc presentViewController:navi animated:YES completion:^{
  1989. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  1990. DebugLog(@"EditModelPriceViewController present.........");
  1991. // self.btop = false;
  1992. // <#code#>
  1993. }];
  1994. #endif
  1995. }
  1996. -(void)refresh_total
  1997. {
  1998. self.total=0;
  1999. self.total_carton=0;
  2000. self.total_cuft=0;
  2001. self.total_weight=0;
  2002. self.total_QTY=0;
  2003. self.total_item=0;
  2004. self.surcharge=0;
  2005. #ifdef BUILD_CONTRAST
  2006. if(self.orderdiscount==0)
  2007. self.labelDiscount.text = @"None";
  2008. else
  2009. self.labelDiscount.text = [NSString stringWithFormat:@"%d%% off",(int)self.orderdiscount];
  2010. #endif
  2011. #ifdef SCANNER_ORDER
  2012. NSString* pricename;
  2013. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  2014. NSString* servername = [addressDic objectForKey:@"show_name"];
  2015. NSArray* arrsn = [RAConvertor string2arr:servername separator:@","];
  2016. NSString * shortname = arrsn[0];
  2017. if(shortname.length==0)
  2018. shortname = servername;
  2019. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2020. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2021. switch(RASingleton.sharedInstance.price_type )
  2022. {
  2023. case 0: pricename =price_group[@"price_0"][@"display"];//@"DDP";//[NSString stringWithFormat:@"%@_DDP",shortname];
  2024. break;;
  2025. case 1: pricename =price_group[@"price_1"][@"display"];//@"WHSE";//[NSString stringWithFormat:@"%@_WHSE",shortname];;
  2026. break;;
  2027. // case 2: pricename = @"25%";
  2028. // break;
  2029. }
  2030. [self.btnPrice setTitle:pricename forState:UIControlStateNormal];
  2031. // self.cartIndicatorBar = [self setupCartIndicatorBar];
  2032. [self.cartIndicatorBar removeFromSuperview];
  2033. self.cartIndicatorBar = [self setupCartIndicatorBar];
  2034. [self.view addSubview:self.cartIndicatorBar];
  2035. #endif
  2036. long count =self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  2037. for(int i=0;i<count;i++)
  2038. {
  2039. NSMutableDictionary * item_json = [self.content_arr[i] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%d",i]] mutableCopy];
  2040. bool bcheck = [[item_json valueForKey:@"check"]boolValue];
  2041. if(bcheck)
  2042. {
  2043. double unitprice = [[item_json valueForKey:@"unit_price"] doubleValue];
  2044. double discount = [[item_json valueForKey:@"discount"] doubleValue];
  2045. int count=[[item_json valueForKey:@"count"] intValue];
  2046. double totalprice =count*unitprice*(1-discount/100.0);
  2047. double cuft=[[item_json valueForKey:@"cuft"] doubleValue];
  2048. double weight=[[item_json valueForKey:@"weight"] doubleValue];
  2049. int carton=/*count**/[[item_json valueForKey:@"carton"] intValue];
  2050. int item_count=1;
  2051. int item_qty=1;
  2052. // NSDictionary * bundle_item =[item_json objectForKey:@"combine"];
  2053. self.total+=totalprice;
  2054. self.total_cuft+=cuft;
  2055. self.total_carton+=carton;
  2056. self.total_weight+=weight;
  2057. self.total_item+=item_count;
  2058. self.total_QTY+=(item_qty)*count;
  2059. }
  2060. }
  2061. #ifdef BUILD_CONTRAST
  2062. self.total= self.total* (1-(self.orderdiscount/100.0));
  2063. #endif
  2064. NSString* subtotal = [RAConvertor currencyNumber:self.total];//[NSString stringWithFormat:@"$%@",];
  2065. NSString* subcuft = [NSString stringWithFormat:@"%.2f",self.total_cuft];
  2066. NSString* subcarton = [NSString stringWithFormat:@"%d",self.total_carton];
  2067. NSString* subweight = [NSString stringWithFormat:@"%.2f lbs",self.total_weight];
  2068. NSString* surcharge = [RAConvertor currencyNumber:self.surcharge];//[NSString stringWithFormat:@"$%@",];
  2069. NSString* totalprice = [RAConvertor currencyNumber:(self.total+self.surcharge)];//[NSString stringWithFormat:@"$%@",];
  2070. NSString* subitems = [NSString stringWithFormat:@"%d",self.total_item];
  2071. NSString* subqty = [NSString stringWithFormat:@"%d",self.total_QTY];
  2072. #ifndef SCANNER_ORDER
  2073. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2074. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  2075. {
  2076. }
  2077. else
  2078. {
  2079. subtotal=nil;
  2080. }
  2081. #else
  2082. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2083. [OLDataProvider updateScanOrder:appDelegate.order_code];
  2084. #endif
  2085. self.labelTotalSurcharge.text = surcharge;
  2086. self.labelTotalPrice.text = totalprice;
  2087. self.labelTotal.text=subtotal;
  2088. self.label_cuft.text=subcuft;
  2089. self.label_carton.text=subcarton;
  2090. self.label_weight.text=subweight;
  2091. self.label_items.text = subitems;
  2092. self.label_qty.text = subqty;
  2093. self.label_notes.text = [self.notes stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
  2094. }
  2095. - (IBAction)onEditGeneralNotes:(id)sender {
  2096. #ifndef SCANNER_ORDER
  2097. if (![self isOrderAvailable]) {
  2098. return;
  2099. }
  2100. #endif
  2101. UIViewController* parentvc=self;
  2102. CartGeneralNotesViewController * vc =[[UIStoryboard storyboardWithName:@"ERP_Mobile_Cart" bundle:nil] instantiateViewControllerWithIdentifier:@"CartGeneralNotesViewController"];
  2103. // NSMutableDictionary * item_json = [self.content_arr[indexPath.row ] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  2104. vc.notes=self.notes;
  2105. // vc.delegate = parentvc.self;
  2106. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  2107. vc.onSetValue = ^(NSString* notes){
  2108. self.notes = notes;
  2109. [self refresh_total];
  2110. };
  2111. //
  2112. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  2113. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  2114. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  2115. [parentvc presentViewController:navi animated:YES completion:^{
  2116. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  2117. DebugLog(@"general notes editor present.........");
  2118. // self.btop = false;
  2119. // <#code#>
  2120. }];
  2121. }
  2122. - (IBAction)OnCancelOrderClicked:(id)sender {
  2123. }
  2124. #pragma mark - Private
  2125. - (BOOL)isOrderAvailable {
  2126. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2127. if (appDelegate.order_code.length == 0) {
  2128. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"please create a new order first" preferredStyle:UIAlertControllerStyleAlert];
  2129. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  2130. }];
  2131. [alertVC addAction:okAction];
  2132. [self presentViewController:alertVC animated:YES completion:nil];
  2133. return NO;
  2134. }
  2135. return YES;
  2136. }
  2137. #pragma mark - commoneditor controller delegate
  2138. -(void) returnValue:(NSMutableDictionary *)value indexPath :(NSIndexPath*) indexPath
  2139. {
  2140. // [self reload_data];
  2141. if([value[@"commitorder_logout"] boolValue])
  2142. [self logout];
  2143. [[self navigationController] setNavigationBarHidden:YES animated:NO];
  2144. }
  2145. #pragma mark - Table view data source
  2146. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  2147. {
  2148. NSDictionary * item_json = nil;
  2149. if (self.back_order_flag) {
  2150. item_json = [self itemJsonAtIndexPath:indexPath];
  2151. } else {
  2152. item_json = self.content_arr[indexPath.row ];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  2153. }
  2154. NSString* notes =[item_json valueForKey:@"note"];
  2155. if(notes.length>0)
  2156. {
  2157. return 146;
  2158. }
  2159. else
  2160. return 120;
  2161. }
  2162. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  2163. //{
  2164. // return 0;
  2165. //}
  2166. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  2167. //{
  2168. // return 0;
  2169. //}
  2170. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  2171. //{
  2172. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  2173. // return myView;
  2174. //
  2175. //}
  2176. //- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  2177. //{
  2178. // UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0001)];
  2179. //// myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  2180. //// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
  2181. //// titleLabel.textColor=[UIColor whiteColor];
  2182. //// titleLabel.backgroundColor = [UIColor clearColor];
  2183. //// if(section==0)
  2184. //// titleLabel.text=NSLocalizedString(@"display_items", nil);
  2185. //// else
  2186. //// titleLabel.text=NSLocalizedString(@"hide_items", nil);
  2187. //// [titleLabel sizeToFit];
  2188. //// [myView addSubview:titleLabel];
  2189. ////
  2190. // return myView;
  2191. //}
  2192. //
  2193. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  2194. // if(section==0)
  2195. // return nil;
  2196. // else
  2197. // return @"detail section";
  2198. //}
  2199. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  2200. {
  2201. if (self.back_order_flag) {
  2202. return [[self.cart_json objectForKey:@"section_count"] integerValue];
  2203. } else {
  2204. return 1;
  2205. }
  2206. }
  2207. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  2208. {
  2209. #ifdef SCANNER_ORDER
  2210. return 0;
  2211. #endif
  2212. if (self.back_order_flag) {
  2213. return 50;
  2214. } else {
  2215. return 0;
  2216. }
  2217. }
  2218. - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  2219. {
  2220. #ifdef SCANNER_ORDER
  2221. return nil;
  2222. #endif
  2223. if (self.back_order_flag) {
  2224. // UIView *header = [tableView dequeueReusableCellWithIdentifier:@"section_header_cell"];
  2225. // UILabel *title_lb = [header viewWithTag:8520];
  2226. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 50.0f)];
  2227. header.backgroundColor = [UIColor colorWithRed:128 / 255.0 green:95 / 255.0 blue:54 / 255.0 alpha:1];
  2228. header.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  2229. UILabel *title_lb = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, CGRectGetWidth(header.bounds) - 10, 50.0f)];
  2230. title_lb.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  2231. title_lb.font = [UIFont systemFontOfSize:25.0f];
  2232. title_lb.textColor = [UIColor whiteColor];
  2233. [header addSubview:title_lb];
  2234. title_lb.text = [self titleForSection:section];
  2235. return header;
  2236. } else {
  2237. return nil;
  2238. }
  2239. }
  2240. #ifdef SCANNER_ORDER
  2241. - (IBAction)onScanChangeCustomerType:(id)sender {
  2242. return;
  2243. NSString *orderdir =[OLDataProvider getScanPath];
  2244. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Select Customer type" message:@"" preferredStyle:UIAlertControllerStyleAlert];
  2245. //block代码块取代了delegate
  2246. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Existing Customer" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  2247. NSMutableDictionary* dict = nil;
  2248. NSString* ordertemplate = nil;
  2249. if([RADataProvider getSiteHasERP])
  2250. ordertemplate =@"e_order.json";
  2251. else
  2252. ordertemplate =@"e_order_noerp.json";
  2253. dict=[OLDataProvider loadScanTemplate:ordertemplate];
  2254. NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  2255. [RAUtils dicttofile:orderPath dict:dict];
  2256. //
  2257. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  2258. //
  2259. //
  2260. //
  2261. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  2262. //
  2263. // [self scanPlaceOrder];
  2264. //
  2265. }];
  2266. UIAlertAction *alert2 = [UIAlertAction actionWithTitle:@"New customer" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  2267. NSMutableDictionary* dict = nil;
  2268. dict=[OLDataProvider loadScanTemplate:@"n_order.json"];
  2269. NSString *orderPath = [orderdir stringByAppendingPathComponent:@"order.json"];
  2270. [RAUtils dicttofile:orderPath dict:dict];
  2271. //
  2272. //
  2273. // NSString *contactpath = [orderdir stringByAppendingPathComponent:@"contact.json"];
  2274. //
  2275. //
  2276. //
  2277. // [RAUtils dicttofile:contactpath dict:appDelegate.customerInfo];
  2278. // [self scanPlaceOrder];
  2279. }];
  2280. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  2281. DebugLog(@"No");
  2282. }];
  2283. [alertControl addAction:actionOne];
  2284. [alertControl addAction:alert2];
  2285. [alertControl addAction:alertthree];
  2286. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  2287. [self presentViewController:alertControl animated:YES completion:nil];
  2288. DebugLog(@"%@",self.edit_select_arr);
  2289. }
  2290. #endif
  2291. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  2292. {
  2293. if (self.back_order_flag) {
  2294. return [self itemCountInSection:section];
  2295. } else {
  2296. // NSDictionary * item_json = [self.content_data objectForKey:@"items"];
  2297. long count =self.content_arr.count;//[[self.content_data valueForKey:@"count"] intValue];
  2298. return count;
  2299. }
  2300. }
  2301. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2302. {
  2303. // if(tableView==self.itemListTable)
  2304. // {
  2305. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2306. NSString *CellIdentifier;
  2307. #ifdef SCANNER_ORDER
  2308. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2309. CellIdentifier = @"ScanCartItemCell";
  2310. ScanCartItemCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  2311. NSDictionary * item_json = nil;//[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  2312. item_json = [self itemJsonAtIndexPath:indexPath];
  2313. NSString* notes = [item_json valueForKey:@"note"];
  2314. if(notes.length>0)
  2315. {
  2316. cell.labelNotes.text = notes;
  2317. }
  2318. else
  2319. {
  2320. cell.labelNotes.text = nil;
  2321. }
  2322. NSString* description =[item_json valueForKey:@"description"];
  2323. NSString* model =[item_json valueForKey:@"model"];
  2324. NSString* dimension =[item_json valueForKey:@"dimension"] ;
  2325. int count =[[item_json valueForKey:@"count"] intValue];
  2326. int stockUom =[[item_json valueForKey:@"stockUom"] intValue];
  2327. double unitprice=[[item_json valueForKey:@"unit_price"] doubleValue];
  2328. double discount=[[item_json valueForKey:@"discount"] doubleValue];
  2329. cell.discount = discount;
  2330. if(appDelegate.user_type == USER_ROLE_CUSTOMER)
  2331. {
  2332. cell.btnScanDiscount.hidden = true;
  2333. }
  2334. else
  2335. {
  2336. switch(RASingleton.sharedInstance.price_type)
  2337. {
  2338. case 0: cell.btnScanDiscount.hidden = true;
  2339. break;;
  2340. case 1:
  2341. {
  2342. if(![item_json[@"special_price"] boolValue]&&![item_json[@"net_price"] boolValue])
  2343. cell.btnScanDiscount.hidden = false;
  2344. else
  2345. cell.btnScanDiscount.hidden = true;
  2346. }
  2347. break;;
  2348. // case 2: pricename = @"25%";
  2349. // break;
  2350. }
  2351. }
  2352. NSString* product_id = [item_json valueForKey:@"product_id"];
  2353. CGRect frame = cell.frame;
  2354. cell.labelMpack.text =[NSString stringWithFormat:@"Sold in QTY of:%d", stockUom];
  2355. __weak typeof(self) weakSelf = self;
  2356. cell.onReturnQTY=^(int qty,NSDictionary* ext)
  2357. {
  2358. int result = [[ext objectForKey:@"result"] intValue];
  2359. BOOL refresh = [[ext objectForKey:@"refresh"] boolValue];
  2360. if (result == 2 && refresh) {
  2361. [self ReloadData];
  2362. return;
  2363. }
  2364. NSMutableDictionary * item_json = nil;
  2365. if (weakSelf.back_order_flag) {
  2366. item_json = [[weakSelf itemJsonAtIndexPath:indexPath] mutableCopy];
  2367. } else {
  2368. item_json = [weakSelf.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  2369. }
  2370. // int count=[[item_json valueForKey:@"count"] intValue];
  2371. // if(count==qty)
  2372. // return;
  2373. if ([[ext valueForKey:@"result"] integerValue] == 8) {
  2374. return ;
  2375. }
  2376. [item_json setValue:[NSString stringWithFormat:@"%d",qty] forKey:@"count"];
  2377. double unit_cuft = [[item_json valueForKey:@"unit_cuft"] doubleValue];
  2378. [item_json setValue:[NSString stringWithFormat:@"%f",unit_cuft*qty] forKey:@"cuft"];
  2379. // [item_json setValue:[ext valueForKey:@"carton"] forKey:@"carton"];
  2380. // [item_json setValue:[ext valueForKey:@"weight"] forKey:@"weight"];
  2381. double unit_price = [[item_json valueForKey:@"unit_price"] doubleValue];
  2382. // double sub_total=[[item_json valueForKey:@"subtotal_price"] doubleValue];
  2383. [item_json setValue:[NSString stringWithFormat:@"%f",unit_price*qty*(1-discount/100.0)] forKey:@"subtotal_price"];
  2384. if (weakSelf.back_order_flag) {
  2385. [weakSelf updateItemJson:item_json atIndexPath:indexPath];
  2386. } else {
  2387. weakSelf.content_arr[indexPath.row]= item_json;
  2388. }
  2389. // [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  2390. [weakSelf refresh_total];
  2391. #ifdef RA_NOTIFICATION
  2392. [ActiveViewController Notify:@"OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2393. #else
  2394. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2395. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  2396. #endif
  2397. };
  2398. // cell.delegate=self;
  2399. // cell.from=indexPath;
  2400. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  2401. if(RASingleton.sharedInstance.price_type==1&&([item_json[@"net_price"] boolValue]||[item_json[@"special_price"] boolValue]||discount>0))
  2402. {
  2403. cell.labelUnitPrice.textColor= [UIColor redColor];
  2404. if([item_json[@"special_price"] boolValue]||discount>0)
  2405. {
  2406. cell.labelOldPrice.hidden = false;
  2407. // if([item_json[@"special_price"] boolValue])
  2408. // {
  2409. cell.labelOldPrice.text=[RAConvertor currencyNumber:[item_json[price_group[@"price_1"][@"name"]] doubleValue]];//[NSString stringWithFormat:@"%.2f",[item_json[@"price1"] doubleValue]];
  2410. // }
  2411. // else
  2412. // {
  2413. // }
  2414. }
  2415. else
  2416. cell.labelOldPrice.hidden = true;
  2417. }
  2418. else
  2419. {
  2420. cell.labelUnitPrice.textColor= [UIColor blackColor];
  2421. cell.labelOldPrice.hidden = true;
  2422. }
  2423. // if(discount>0)
  2424. // {
  2425. // cell.labelNotes.text = [NSString stringWithFormat:@"Note: discount %d%% off.",(int)discount];
  2426. // cell.labelOldPrice.hidden = false;
  2427. //// cell.labelOldPrice.textColor = [UIColor redColor];
  2428. // cell.labelUnitPrice.textColor= [UIColor redColor];
  2429. // cell.labelOldPrice.text=[item_json[@"unit_price"] stringValue];
  2430. // frame.size.height = 120;
  2431. // }
  2432. // else
  2433. // {
  2434. //
  2435. //
  2436. // cell.labelOldPrice.hidden = true;
  2437. // cell.labelNotes.text = nil;
  2438. // frame.size.height = 94;
  2439. // }
  2440. cell.unit_price = unitprice;
  2441. // cell.labelAttribute.text = attribute;
  2442. // cell.labelCurrency.text = currency;
  2443. NSString* subtotal = [RAConvertor currencyNumber:unitprice*count*(1-discount/100)];//[NSString stringWithFormat:@"%.2f",unitprice*count*(1-discount/100)];
  2444. NSString* newunitprice = [RAConvertor currencyNumber:unitprice*(1-discount/100)];//[NSString stringWithFormat:@"%.2f",unitprice*(1-discount/100)];
  2445. cell.labelModel.text = model;
  2446. cell.labelDescription.text = [NSString stringWithFormat:@"%@\n%@",description,dimension];
  2447. // if(noprice&&unitprice==0.0)
  2448. // {
  2449. // cell.labelPrice.text = @"No Price";
  2450. //
  2451. // cell.labelUnitPrice.text = @"No Price";
  2452. // }
  2453. // else
  2454. {
  2455. cell.labelSubtotal.text = subtotal;
  2456. cell.labelUnitPrice.text = newunitprice;
  2457. }
  2458. bool check = [[item_json valueForKey:@"check"] boolValue];
  2459. NSString* cart_item_id= [item_json valueForKey:@"cart_item_id"];
  2460. cell.cart_id = cart_item_id;
  2461. if(/*appDelegate.user_type==USER_ROLE_CUSTOMER*/ true)
  2462. [cell init_Stepper:stockUom max:9999 min:stockUom value:count];
  2463. // else
  2464. // [cell init_Stepper:1 max:9999 min:1 value:count];
  2465. [cell set_Count:count];
  2466. // Button 设置了大小约束,Size就不会随内容变化
  2467. cell.backgroundColor = [UIColor whiteColor];
  2468. return cell;
  2469. #else
  2470. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2471. #ifdef BUILD_CONTRAST
  2472. CellIdentifier = @"ContrastCartItemCell";
  2473. ContrastModelItemCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  2474. #else
  2475. CellIdentifier = @"CartItemCell";
  2476. ModelItemCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  2477. #endif
  2478. NSDictionary * item_json = nil;//[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  2479. if (self.back_order_flag) {
  2480. item_json = [self itemJsonAtIndexPath:indexPath];
  2481. } else {
  2482. item_json = self.content_arr[indexPath.row];
  2483. }
  2484. NSDictionary * combine_json =[item_json objectForKey:@"combine"];
  2485. NSDictionary * master_json = [item_json objectForKey:@"combine_master"];
  2486. if (cell.masterBtn != nil) {
  2487. if (master_json == nil) {
  2488. cell.masterBtn.hidden = YES;
  2489. } else{
  2490. cell.masterBtn.hidden = NO;
  2491. cell.master_items = master_json;
  2492. }
  2493. }
  2494. bool isfree = [[item_json valueForKey:@"is_free"]boolValue];
  2495. NSString* notes = [item_json valueForKey:@"note"];
  2496. BOOL is_out_of_stock = [[item_json valueForKey:@"is_out_of_stock"] boolValue];
  2497. bool noprice =[[item_json valueForKey:@"is_no_price"]boolValue];
  2498. cell.noprice = noprice;
  2499. cell.enable_longpress = true;
  2500. cell.bundle_item=combine_json;
  2501. NSString* img_url = [item_json valueForKey:@"img_url"];
  2502. NSString* description = [item_json valueForKey:@"description"];
  2503. // NSString* identifier = [item_json valueForKey:@"identifier"];
  2504. // NSString* attribute = [item_json valueForKey:@"attribute"];
  2505. // NSString* currency = [item_json valueForKey:@"currency"];
  2506. int count =[[item_json valueForKey:@"count"] intValue];
  2507. int stockUom =[[item_json valueForKey:@"stockUom"] intValue];
  2508. #ifdef BUILD_CONTRAST
  2509. int mpack =[[item_json valueForKey:@"mpack"] intValue];
  2510. cell.labelMpack.text =[NSString stringWithFormat:@"Master Pack:%d", mpack];
  2511. #endif
  2512. // double totalprice =[[item_json valueForKey:@"subtotal_price"] doubleValue];
  2513. double unitprice=[[item_json valueForKey:@"unit_price"] doubleValue];
  2514. double discount =[[item_json valueForKey:@"discount"] doubleValue];
  2515. NSString* product_id = [item_json valueForKey:@"product_id"];
  2516. // handle no such model
  2517. if(product_id.length==0)
  2518. cell.labelNoSuchModel.hidden=false;
  2519. else
  2520. cell.labelNoSuchModel.hidden=true;
  2521. //if (appDelegate.user_type == USER_ROLE_CUSTOMER) {
  2522. if (!is_out_of_stock) {
  2523. cell.labelOutOfStock.hidden = YES;
  2524. } else {
  2525. cell.labelOutOfStock.hidden = NO;
  2526. }
  2527. // } else {
  2528. // cell.labelOutOfStock.hidden = YES;
  2529. // }
  2530. // discount = 5.0;
  2531. cell.discount = discount;
  2532. CGRect frame = cell.frame;
  2533. if(notes.length>0)
  2534. {
  2535. cell.labelNotes.text = [@"Note: " stringByAppendingString:[notes stringByReplacingOccurrencesOfString:@"\n" withString:@" "]];
  2536. frame.size.height = 146;
  2537. }
  2538. else
  2539. {
  2540. cell.labelNotes.text = nil;
  2541. frame.size.height = 120;
  2542. }
  2543. // cell.frame = frame;
  2544. NSString* discountstr=[NSString stringWithFormat:@"%@%% off", [RAUtils FloatFormat:discount]];
  2545. if(isfree)
  2546. {
  2547. cell.labelOldPrice.hidden = false;
  2548. cell.labelOldPrice.textColor = [UIColor redColor];
  2549. cell.labelOldPrice.text=@"Free";
  2550. cell.labelOldPrice.hideline = true;
  2551. cell.labelDiscount.hidden = true;
  2552. }
  2553. else
  2554. {
  2555. cell.labelOldPrice.textColor = [UIColor blackColor];
  2556. cell.labelOldPrice.hideline = false;
  2557. if(discount==0)
  2558. {
  2559. cell.labelOldPrice.hidden = true;
  2560. cell.labelDiscount.hidden = true;
  2561. }
  2562. else
  2563. {
  2564. cell.labelOldPrice.hidden = false;
  2565. cell.labelDiscount.hidden = false;
  2566. }
  2567. }
  2568. __weak typeof(self) weakSelf = self;
  2569. cell.onReturnQTY=^(int qty,NSDictionary* ext)
  2570. {
  2571. int result = [[ext objectForKey:@"result"] intValue];
  2572. BOOL refresh = [[ext objectForKey:@"refresh"] boolValue];
  2573. if (result == 2 && refresh) {
  2574. [self ReloadData];
  2575. return;
  2576. }
  2577. NSMutableDictionary * item_json = nil;
  2578. if (weakSelf.back_order_flag) {
  2579. item_json = [[weakSelf itemJsonAtIndexPath:indexPath] mutableCopy];
  2580. } else {
  2581. item_json = [weakSelf.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  2582. }
  2583. // int count=[[item_json valueForKey:@"count"] intValue];
  2584. // if(count==qty)
  2585. // return;
  2586. if ([[ext valueForKey:@"result"] integerValue] == 8) {
  2587. return ;
  2588. }
  2589. [item_json setValue:[NSString stringWithFormat:@"%d",qty] forKey:@"count"];
  2590. [item_json setValue:[ext valueForKey:@"cuft"] forKey:@"cuft"];
  2591. [item_json setValue:[ext valueForKey:@"carton"] forKey:@"carton"];
  2592. [item_json setValue:[ext valueForKey:@"weight"] forKey:@"weight"];
  2593. double unit_price = [[item_json valueForKey:@"unit_price"] doubleValue];
  2594. // double sub_total=[[item_json valueForKey:@"subtotal_price"] doubleValue];
  2595. [item_json setValue:[NSString stringWithFormat:@"%f",unit_price*qty] forKey:@"subtotal_price"];
  2596. if (weakSelf.back_order_flag) {
  2597. [weakSelf updateItemJson:item_json atIndexPath:indexPath];
  2598. } else {
  2599. weakSelf.content_arr[indexPath.row]= item_json;
  2600. }
  2601. // [self.content_data setObject:item_json forKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  2602. [weakSelf refresh_total];
  2603. #ifdef RA_NOTIFICATION
  2604. [ActiveViewController Notify:@"OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2605. #else
  2606. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2607. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  2608. #endif
  2609. };
  2610. // cell.delegate=self;
  2611. // cell.from=indexPath;
  2612. cell.unit_price = unitprice;
  2613. // cell.labelAttribute.text = attribute;
  2614. // cell.labelCurrency.text = currency;
  2615. NSString* oldprice =[NSString stringWithFormat:@"%.2f",unitprice];
  2616. NSString* newprice = [NSString stringWithFormat:@"%.2f",unitprice* (1.0-discount/100)*count];
  2617. NSString* newunitprice = [NSString stringWithFormat:@"%.2f",unitprice* (1.0-discount/100)];
  2618. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  2619. {
  2620. }
  2621. else
  2622. {
  2623. oldprice=nil;
  2624. newprice=nil;
  2625. newunitprice=nil;
  2626. }
  2627. cell.labelDiscount.text = discountstr;
  2628. cell.labelOldPrice.text = oldprice;
  2629. cell.labelMasterpack.text =[NSString stringWithFormat:@"Sold in QTY of:%d", stockUom];
  2630. cell.labelDescription.text = description;
  2631. // cell.labelIdentifier.text = identifier;
  2632. if(noprice&&unitprice==0.0)
  2633. {
  2634. cell.labelPrice.text = @"No Price";
  2635. cell.labelUnitPrice.text = @"No Price";
  2636. }
  2637. else
  2638. {
  2639. cell.labelPrice.text = newprice;
  2640. cell.labelUnitPrice.text = newunitprice;
  2641. }
  2642. bool check = [[item_json valueForKey:@"check"] boolValue];
  2643. if(check)
  2644. {
  2645. cell.img_checkmark.hidden=false;
  2646. // [item_json setValue:@"0" forKey:@"check"];
  2647. }
  2648. else
  2649. {
  2650. // [item_json setValue:@"1" forKey:@"check"];
  2651. cell.img_checkmark.hidden=true;
  2652. }
  2653. NSString* cart_item_id= [item_json valueForKey:@"cart_item_id"];
  2654. cell.cart_id = cart_item_id;
  2655. if(/*appDelegate.user_type==USER_ROLE_CUSTOMER*/ true)
  2656. {
  2657. #ifdef BUILD_CONTRAST
  2658. [cell init_Stepper:stockUom max:9999 min:1 value:count];
  2659. #else
  2660. [cell init_Stepper:stockUom max:9999 min:stockUom value:count];
  2661. #endif
  2662. }
  2663. // else
  2664. // [cell init_Stepper:1 max:9999 min:1 value:count];
  2665. [cell set_Count:count];
  2666. // Button 设置了大小约束,Size就不会随内容变化
  2667. [cell.btnImage setContentMode:UIViewContentModeScaleAspectFit];
  2668. [cell.btnImage setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
  2669. [cell.btnImage setContentHorizontalAlignment:UIControlContentHorizontalAlignmentFill];
  2670. [cell.btnImage setContentVerticalAlignment:UIControlContentVerticalAlignmentFill];
  2671. cell.btnImage.imageView.contentMode = UIViewContentModeScaleAspectFit;
  2672. if (![cell.imageName isEqualToString:img_url]) {
  2673. cell.imageName = img_url;
  2674. // [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"loading_s"] forState:UIControlStateNormal];
  2675. [cell.btnImage setImage:[UIImage imageNamed:@"loading_s"] forState:UIControlStateNormal];
  2676. NSString* file_name=[img_url lastPathComponent];
  2677. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  2678. if(img_data!=nil)
  2679. {
  2680. UIImage * img =[UIImage imageWithData:img_data];
  2681. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  2682. [cell.btnImage setImage:img forState:UIControlStateNormal];
  2683. }
  2684. else
  2685. {
  2686. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  2687. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  2688. dispatch_async(dispatch_get_main_queue(), ^{
  2689. if(downloadimg_data!=nil)
  2690. {
  2691. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  2692. UIImage * img =[UIImage imageWithData:downloadimg_data];
  2693. // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal];
  2694. [cell.btnImage setImage:img forState:UIControlStateNormal];
  2695. }
  2696. else
  2697. {
  2698. // [cell.btnImage setBackgroundImage:[UIImage imageNamed:@"notfound_s"] forState:UIControlStateNormal];
  2699. [cell.btnImage setImage:[UIImage imageNamed:@"notfound_s"] forState:UIControlStateNormal];
  2700. }
  2701. });
  2702. });
  2703. }
  2704. }
  2705. cell.backgroundColor = [UIColor whiteColor];
  2706. if(self.itemListTable.editing)
  2707. {
  2708. cell.editCount.enabled = false;
  2709. cell.stepper.hidden=true;
  2710. }
  2711. else
  2712. {
  2713. cell.editCount.enabled=true;
  2714. cell.stepper.hidden=false;
  2715. }
  2716. return cell;
  2717. // }
  2718. // else
  2719. // {
  2720. // NSString *CellIdentifier = @"OrderInfoListItem";
  2721. // UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  2722. // return cell;
  2723. // }
  2724. #endif
  2725. }
  2726. -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
  2727. {
  2728. if(!self.itemListTable.editing)
  2729. return;
  2730. // NSMutableDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  2731. NSMutableDictionary * item_json = nil;
  2732. if (self.back_order_flag) {
  2733. item_json = [[self itemJsonAtIndexPath:indexPath] mutableCopy];
  2734. } else {
  2735. item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  2736. }
  2737. NSString *cart_item_id=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  2738. if ([self.edit_select_arr containsObject:cart_item_id])
  2739. {
  2740. [self.edit_select_arr removeObject:cart_item_id];
  2741. }
  2742. }
  2743. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  2744. {
  2745. #ifdef SCANNER_ORDER
  2746. return;
  2747. #endif
  2748. NSMutableDictionary * item_json = nil;
  2749. if (self.back_order_flag) {
  2750. item_json = [[self itemJsonAtIndexPath:indexPath] mutableCopy];
  2751. } else {
  2752. item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  2753. }
  2754. // NSMutableDictionary * item_json =[self.content_arr[indexPath.row] mutableCopy];// [[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  2755. if(self.itemListTable.editing)
  2756. {
  2757. NSString *cart_item_id=[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  2758. if (![self.edit_select_arr containsObject:cart_item_id]) {
  2759. [self.edit_select_arr addObject:cart_item_id];
  2760. }
  2761. return;
  2762. }
  2763. // no checkmark support anymore for cart
  2764. return;
  2765. }
  2766. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  2767. if(self.itemListTable.editing)
  2768. return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
  2769. else
  2770. return UITableViewCellEditingStyleDelete;
  2771. }
  2772. #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
  2773. //- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  2774. - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
  2775. {
  2776. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2777. // 添加一个删除按钮
  2778. self.indexPath=indexPath;
  2779. NSMutableDictionary * item_json = nil;
  2780. if (self.back_order_flag) {
  2781. item_json = [[self itemJsonAtIndexPath:indexPath] mutableCopy];
  2782. } else {
  2783. item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  2784. }
  2785. NSString* productID = [item_json valueForKey:@"product_id"];
  2786. BOOL outOfStock = [[item_json valueForKey:@"is_out_of_stock"] boolValue];
  2787. __weak typeof(self) weakself = self;
  2788. // UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" Delete " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  2789. UIContextualAction* deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@" Delete " handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
  2790. DebugLog(@"delete click");
  2791. //[self.itemListTable setEditing:false animated:YES];
  2792. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Confirm Deletion" message:@"Are you sure to delete this model?" preferredStyle:UIAlertControllerStyleAlert];
  2793. UIAlertAction *action_2 = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  2794. // [self.itemListTable reloadRowsAtIndexPaths:@[self.indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  2795. NSMutableDictionary * item_json = nil;
  2796. if (self.back_order_flag) {
  2797. item_json = [[self itemJsonAtIndexPath:self.indexPath] mutableCopy];
  2798. } else {
  2799. item_json = [self.content_arr[self.indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  2800. }
  2801. NSString *ids=[NSString stringWithFormat:@"%@",[item_json valueForKey:@"cart_item_id"] ];
  2802. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Remove Model" completion:^{
  2803. PopWaitAlert* pop = [RAUtils waiting_pop:@"Remove Model" completion:nil];
  2804. [RANetwork request_remove_cart:ids completionHandler:^(NSMutableDictionary *result) {
  2805. NSMutableDictionary* cart_json = result;
  2806. // [waitalert dismissViewControllerAnimated:YES completion:^{
  2807. [pop hide];
  2808. if([[cart_json valueForKey:@"result"] intValue]==2)
  2809. {
  2810. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2811. if (self.back_order_flag) {
  2812. NSInteger section_count = [self sectionCount];
  2813. for (int i = 0; i < section_count; i++) {
  2814. NSInteger item_count = [self itemCountInSection:i];
  2815. BOOL ready_break = NO;
  2816. for (int j = 0; j < item_count; j++) {
  2817. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];
  2818. NSDictionary* obj_json = [self itemJsonAtIndexPath:indexPath];
  2819. NSString* _id =[obj_json valueForKey:@"cart_item_id"];
  2820. if([_id isEqualToString:ids])
  2821. {
  2822. [self updateItemJson:nil atIndexPath:indexPath];
  2823. ready_break = YES;
  2824. break;
  2825. }
  2826. }
  2827. if (ready_break) {
  2828. break;
  2829. }
  2830. }
  2831. } else {
  2832. for(int i=0;i<self.content_arr.count;i++)
  2833. {
  2834. NSDictionary* obj_json = self.content_arr[i];
  2835. NSString* _id =[obj_json valueForKey:@"cart_item_id"];
  2836. if([_id isEqualToString:ids])
  2837. {
  2838. [self.content_arr removeObjectAtIndex:i];
  2839. // [self.itemListTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  2840. break;
  2841. // appDelegate.cart_count--;
  2842. // [appDelegate update_count_mark];
  2843. }
  2844. }
  2845. }
  2846. #ifdef SCANNER_ORDER
  2847. self.cart_json = cart_json;
  2848. #endif
  2849. [self.itemListTable reloadData];
  2850. [self refresh_total];
  2851. #ifdef RA_NOTIFICATION
  2852. [ActiveViewController Notify:@"OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2853. #else
  2854. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2855. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  2856. #endif
  2857. // [self reload_data];
  2858. }
  2859. else
  2860. {
  2861. [RAUtils message_box:@"Delete Model" message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  2862. }
  2863. // }];
  2864. }];
  2865. // }];
  2866. }];
  2867. UIAlertAction *action_3 = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  2868. }];
  2869. [alertController addAction:action_2];
  2870. [alertController addAction:action_3];
  2871. [self presentViewController:alertController animated:YES completion:nil];
  2872. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Delete confirm", nil) message:NSLocalizedString(@"Are you sure remove model from cart?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  2873. // alert.tag = ALERT_DEL;
  2874. // // alert.
  2875. // [alert show];
  2876. }];
  2877. deleteRowAction.backgroundColor = [UIColor redColor];
  2878. // UITableViewRowAction *notifyMeAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Notify Me" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  2879. UIContextualAction* notifyMeAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"Notify Me" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
  2880. {
  2881. // __block UIAlertController *waitting_alert = [RAUtils waiting_alert:self title:@"Sending Email" completion:^{
  2882. PopWaitAlert* pop = [RAUtils waiting_pop:@"Sending Email" completion:nil];
  2883. [RANetwork request_notifymodel:productID emailAddr:nil withScreen:ScreenCodeCart completionHandler:^(NSMutableDictionary *result) {
  2884. NSMutableDictionary *dic = result;
  2885. // [waitting_alert dismissWithClickedButtonIndex:0 animated:NO];
  2886. // [waitting_alert dismissViewControllerAnimated:YES completion:^{
  2887. [pop hide];
  2888. int result_code = [[dic valueForKey:@"result"] intValue];
  2889. if (result_code != RESULT_TRUE && result_code != RESULT_NO_EMAIL_ADDRESS)
  2890. {
  2891. NSString *msg = [NSString stringWithFormat:@"The email send failed"];
  2892. if ([dic valueForKey:@"err_msg"]) {
  2893. msg = [dic valueForKey:@"err_msg"];
  2894. }
  2895. UIAlertController *errorAlertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:msg preferredStyle:UIAlertControllerStyleAlert];
  2896. UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  2897. }];
  2898. [errorAlertVC addAction:action];
  2899. [weakself presentViewController:errorAlertVC animated:YES completion:nil];
  2900. } else if (result_code == RESULT_TRUE){
  2901. [RAUtils message_box:@"Message" message:@"Notification is sent to default email address." completion:nil];
  2902. } else if (result_code == RESULT_NO_EMAIL_ADDRESS) {
  2903. [weakself showAddressBoxToEmailProduct:productID];
  2904. }
  2905. // }];
  2906. }];
  2907. // }];
  2908. };
  2909. }];
  2910. notifyMeAction.backgroundColor = UIColorFromRGB(0x9BBF5A);
  2911. // UITableViewRowAction *addWatchAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Move To Wish List" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  2912. UIContextualAction* addWatchAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"Move To Wish List" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
  2913. DebugLog(@"Wishlist click");
  2914. NSMutableDictionary * item_json = nil;
  2915. if (self.back_order_flag) {
  2916. item_json = [[self itemJsonAtIndexPath:indexPath] mutableCopy];
  2917. } else {
  2918. item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  2919. }
  2920. NSString* cart_item_id = [item_json valueForKey:@"cart_item_id"];
  2921. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  2922. [RANetwork request_move_cart2wish:cart_item_id completionHandler:^(NSMutableDictionary *result) {
  2923. NSDictionary* return_json = result;
  2924. if([[return_json valueForKey:@"result"] intValue]==2)
  2925. {
  2926. #ifdef RA_NOTIFICATION
  2927. [ActiveViewController Notify:@"WatchListViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  2928. #else
  2929. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  2930. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  2931. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  2932. #endif
  2933. for(int i=0;i<self.content_arr.count;i++)
  2934. {
  2935. NSDictionary* obj_json = self.content_arr[i];
  2936. NSString* _id =[obj_json valueForKey:@"cart_item_id"];
  2937. if([_id isEqualToString:cart_item_id])
  2938. {
  2939. if (self.back_order_flag) {
  2940. [self updateItemJson:nil atIndexPath:indexPath];
  2941. } else {
  2942. [self.content_arr removeObjectAtIndex:i];
  2943. }
  2944. [self.itemListTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  2945. appDelegate.cart_count--;
  2946. [appDelegate update_count_mark];
  2947. [self refresh_total];
  2948. }
  2949. }
  2950. [self.itemListTable reloadData];
  2951. }
  2952. else
  2953. {
  2954. [RAUtils message_box:@"Move To Wish List" message:[return_json valueForKey:@"err_msg"] completion:nil] ;
  2955. }
  2956. }];
  2957. }];
  2958. addWatchAction.backgroundColor = UIColorFromRGB(0x339966);
  2959. // 添加一个编辑按钮
  2960. // UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Edit Price"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  2961. UIContextualAction* editRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"Edit Price" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
  2962. DebugLog(@"edit click");
  2963. if (appDelegate.user_type == USER_ROLE_CUSTOMER && appDelegate.customer_type == CustomerTypeStore) {
  2964. if (self.currentOrderIsMerged) { // 合并过的订单不能编辑价格
  2965. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Purchase Order Can't Edit Price" preferredStyle:UIAlertControllerStyleAlert];
  2966. UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  2967. [alert dismissViewControllerAnimated:YES completion:nil];
  2968. }];
  2969. [alert addAction:action];
  2970. if (weakself) {
  2971. __strong typeof(weakself) strongself = weakself;
  2972. [strongself presentViewController:alert animated:YES completion:nil];
  2973. }
  2974. return ;
  2975. }
  2976. if ([RASingleton sharedInstance].npd_shop_price_type == 0) { // 提货价不能编辑价格
  2977. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"Current Price Type is %@,You Can't Change it",[RASingleton sharedInstance].deliveryString] preferredStyle:UIAlertControllerStyleAlert];
  2978. UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  2979. [alert dismissViewControllerAnimated:YES completion:nil];
  2980. }];
  2981. [alert addAction:action];
  2982. if (weakself) {
  2983. __strong typeof(weakself) strongself = weakself;
  2984. [strongself presentViewController:alert animated:YES completion:nil];
  2985. }
  2986. return ;
  2987. }
  2988. }
  2989. UIViewController* parentvc=self;
  2990. EditModelPriceViewController * vc =[[UIStoryboard storyboardWithName:@"ERP_Mobile_Cart" bundle:nil] instantiateViewControllerWithIdentifier:@"EditModelPriceViewController"];
  2991. NSMutableDictionary * item_json = nil;
  2992. if (self.back_order_flag) {
  2993. item_json = [[self itemJsonAtIndexPath:indexPath] mutableCopy];
  2994. } else {
  2995. item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  2996. }
  2997. vc.discount=[[item_json valueForKey:@"discount"] floatValue];
  2998. vc.price = [[item_json valueForKey:@"unit_price"] floatValue];
  2999. vc.cart_id = [item_json valueForKey:@"cart_item_id"];//[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  3000. __block NSString* cartid=[item_json valueForKey:@"cart_item_id"];
  3001. // vc.delegate = parentvc.self;
  3002. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  3003. vc.onSetValue = ^(double price,double discount){
  3004. bool bmodify=false;
  3005. if(price!= [[item_json valueForKey:@"unit_price"] floatValue])
  3006. bmodify=true;
  3007. NSString* notes = [item_json valueForKey:@"note"];
  3008. if(notes.length==0)
  3009. notes=@"";
  3010. if(bmodify)
  3011. {
  3012. if([notes isEqualToString:@""])
  3013. notes=@"PRICE MODIFIED";
  3014. NSRange range;
  3015. range = [notes rangeOfString:@"PRICE MODIFIED"];
  3016. if (range.location != NSNotFound) {
  3017. }else{
  3018. notes= [notes stringByAppendingString:@"\nPRICE MODIFIED"];
  3019. }
  3020. }
  3021. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Update Price" completion:^{
  3022. PopWaitAlert* pop = [RAUtils waiting_pop:@"Update Price" completion:nil];
  3023. [RANetwork request_update_cart_modelprice:cartid price:price discount:discount notes:notes completionHandler:^(NSMutableDictionary *result) {
  3024. NSDictionary* cart_json = result;
  3025. // [waitalert dismissViewControllerAnimated:YES completion:^{
  3026. [pop hide];
  3027. if([[cart_json valueForKey:@"result"] intValue]==2)
  3028. {
  3029. [item_json setValue:[NSNumber numberWithDouble:price ] forKey:@"unit_price"];
  3030. [item_json setValue:[NSNumber numberWithDouble:discount ] forKey:@"discount"];
  3031. [item_json setValue:notes forKey:@"note"];
  3032. if (self.back_order_flag) {
  3033. [self updateItemJson:item_json atIndexPath:indexPath];
  3034. } else {
  3035. self.content_arr[indexPath.row ] = item_json;
  3036. }
  3037. tableView.editing = false;
  3038. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  3039. // [self refresh_total];
  3040. [weakself reload_data];
  3041. #ifdef RA_NOTIFICATION
  3042. [ActiveViewController Notify:@"OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  3043. #else
  3044. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  3045. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  3046. #endif
  3047. }
  3048. else
  3049. {
  3050. [RAUtils message_box:@"Update Price" message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  3051. }
  3052. // }];
  3053. }];
  3054. // }];
  3055. };
  3056. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  3057. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  3058. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  3059. [parentvc presentViewController:navi animated:YES completion:^{
  3060. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  3061. DebugLog(@"EditModelPriceViewController present.........");
  3062. // self.btop = false;
  3063. // <#code#>
  3064. }];
  3065. // [self.itemListTable setEditing:false animated:YES];
  3066. //[tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  3067. }];
  3068. editRowAction.backgroundColor = UIColorFromRGB(0xff9933);
  3069. // // 添加一个更多按钮
  3070. //
  3071. // UITableViewRowAction *moreRowAction = [UITableView RowActionrowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  3072. //
  3073. // DebugLog(@点击了更多);
  3074. //
  3075. //
  3076. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  3077. //
  3078. // }];
  3079. //
  3080. // moreRowAction.backgroundEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleDark];
  3081. //
  3082. // 将设置好的按钮放到数组中返回
  3083. // UITableViewRowAction *freeRowAction = nil;
  3084. //
  3085. //
  3086. // NSDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  3087. //
  3088. // bool isfree = [[item_json valueForKey:@"is_free"]boolValue];
  3089. //
  3090. // if(!isfree)
  3091. // {
  3092. //
  3093. // freeRowAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Set Free" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  3094. //
  3095. // DebugLog(@"free click");
  3096. //
  3097. //
  3098. //
  3099. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Free give away confirm", nil) message:NSLocalizedString(@"Are you sure make this model free?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  3100. // alert.tag = ALERT_SET_FREE;
  3101. // // alert.
  3102. // [alert show];
  3103. // }];
  3104. // }
  3105. // else
  3106. // {
  3107. //
  3108. // freeRowAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Not Free" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  3109. //
  3110. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Restore free model confirm", nil) message:NSLocalizedString(@"Are you sure restore free model?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
  3111. // alert.tag = ALERT_RESTORE_FREE;
  3112. // // alert.
  3113. // [alert show];
  3114. // }];
  3115. // }
  3116. //
  3117. //
  3118. //
  3119. // freeRowAction.backgroundColor = [UIColor lightGrayColor];
  3120. // UITableViewRowAction *noteRowAction = nil;
  3121. //
  3122. //
  3123. // // NSDictionary * item_json = [self.content_arr[indexPath.row] mutableCopy];//[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  3124. //
  3125. //
  3126. //
  3127. //
  3128. // noteRowAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Edit note" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  3129. UIContextualAction* noteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"Edit note" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
  3130. DebugLog(@"item note click");
  3131. UIViewController* parentvc=self;
  3132. ItemNotesViewController * vc =[[UIStoryboard storyboardWithName:@"ERP_Mobile_Cart" bundle:nil] instantiateViewControllerWithIdentifier:@"ItemNotesViewController"];
  3133. NSMutableDictionary * item_json = nil;
  3134. if (self.back_order_flag) {
  3135. item_json = [[self itemJsonAtIndexPath:indexPath] mutableCopy];
  3136. } else {
  3137. item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  3138. }
  3139. vc.onSaveNote=^(NSString* notes){
  3140. [item_json setValue:notes forKey:@"note"];
  3141. if (self.back_order_flag) {
  3142. [self updateItemJson:item_json atIndexPath:indexPath];
  3143. } else {
  3144. self.content_arr[indexPath.row ] = item_json;
  3145. }
  3146. tableView.editing = false;
  3147. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  3148. };
  3149. vc.notes=[item_json valueForKey:@"note"];
  3150. vc.cart_id = [item_json valueForKey:@"cart_item_id"];//[NSString stringWithFormat:@"%d",[[item_json valueForKey:@"cart_item_id"] intValue]];
  3151. // vc.delegate = parentvc.self;
  3152. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  3153. // vc.onSetValue = ^(){
  3154. // [self reload_data];//[main_vc checklogin:true];
  3155. // };
  3156. //
  3157. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  3158. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  3159. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  3160. [parentvc presentViewController:navi animated:YES completion:^{
  3161. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  3162. DebugLog(@"EditModelPriceViewController present.........");
  3163. // self.btop = false;
  3164. // <#code#>
  3165. }];
  3166. // [self.itemListTable setEditing:false animated:YES];
  3167. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  3168. }];
  3169. noteRowAction.backgroundColor = [UIColor lightGrayColor];
  3170. // UITableViewRowAction *move2BackAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Move to Back Order" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  3171. UIContextualAction* move2BackAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"Move to Back Order" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
  3172. NSDictionary *item_json = [self itemJsonAtIndexPath:indexPath];
  3173. NSString* cart_item_id = [item_json valueForKey:@"cart_item_id"];
  3174. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Move To Back Order" completion:^{
  3175. PopWaitAlert* pop = [RAUtils waiting_pop:@"Move To Back Order" completion:nil];
  3176. [RANetwork request_movetobackorder:cart_item_id completionHandler:^(NSMutableDictionary *result) {
  3177. NSMutableDictionary* return_json =result ;
  3178. // [waitalert dismissViewControllerAnimated:YES completion:^{
  3179. [pop hide];
  3180. if([[return_json valueForKey:@"result"] intValue]==2) {
  3181. [self reload_data];
  3182. } // if 2
  3183. else {
  3184. [RAUtils message_box:@"Move To Back Order" message:[return_json valueForKey:@"err_msg"] completion:nil] ;
  3185. }
  3186. // }];
  3187. }];
  3188. // }];
  3189. }];
  3190. move2BackAction.backgroundColor = UIColorFromRGB(0x336699);
  3191. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  3192. {
  3193. NSMutableDictionary * item_json = nil;
  3194. if (self.back_order_flag) {
  3195. item_json = [[self itemJsonAtIndexPath:indexPath] mutableCopy];
  3196. } else {
  3197. item_json = [self.content_arr[indexPath.row] mutableCopy];//[[self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]] mutableCopy];
  3198. }
  3199. NSString* product_id = [item_json valueForKey:@"product_id"];
  3200. int is_rate = [item_json[@"is_rate"] intValue];
  3201. NSMutableArray* arr=[@[] mutableCopy];
  3202. BOOL condition = outOfStock;
  3203. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  3204. #ifdef OFFLINE_MODE
  3205. condition = !appDelegate.offline_mode && outOfStock;
  3206. #endif
  3207. if (condition) {
  3208. [arr addObject:notifyMeAction];
  3209. }
  3210. NSDictionary *section_json = [self jsonAtSection:indexPath.section];
  3211. BOOL available = [[section_json objectForKey:@"available"] boolValue]; // available list
  3212. if (self.back_order_flag && available) {
  3213. [arr addObject:move2BackAction];
  3214. }
  3215. #endif
  3216. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3217. #ifdef OFFLINE_MODE
  3218. condition = !appDelegate.offline_mode && outOfStock;
  3219. #endif
  3220. if (condition) {
  3221. [arr addObject:notifyMeAction];
  3222. }
  3223. #endif
  3224. if(product_id.length>0)
  3225. [arr addObject:noteRowAction];
  3226. [arr addObject:deleteRowAction];
  3227. #ifndef SCANNER_ORDER
  3228. if(product_id.length>0&& is_rate==0)
  3229. [arr addObject:addWatchAction];
  3230. if(appDelegate.can_set_cart_price && product_id.length>0&& is_rate==0)
  3231. {
  3232. [arr addObject:editRowAction];
  3233. // [arr addObject:freeRowAction];
  3234. }
  3235. #endif
  3236. return [UISwipeActionsConfiguration configurationWithActions:arr];
  3237. // return arr;
  3238. // return @[deleteRowAction,addWatchAction, editRowAction,freeRowAction];
  3239. } else if (appDelegate.user_type == USER_ROLE_CUSTOMER) {
  3240. if (appDelegate.customer_type == CustomerTypeStore) {
  3241. #ifndef SCANNER_ORDER
  3242. return [UISwipeActionsConfiguration configurationWithActions:@[notifyMeAction,deleteRowAction,addWatchAction,editRowAction]];
  3243. // return @[notifyMeAction,deleteRowAction,addWatchAction,editRowAction];
  3244. #else
  3245. return [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
  3246. // return @[deleteRowAction];
  3247. #endif
  3248. } else {
  3249. // normal
  3250. // NSArray *actionArray = @[notifyMeAction,deleteRowAction,addWatchAction];
  3251. NSMutableArray *actionArray = [NSMutableArray array];
  3252. // move to backorder
  3253. NSDictionary *section_json = [self jsonAtSection:indexPath.section];
  3254. BOOL available = [[section_json objectForKey:@"available"] boolValue]; // available list
  3255. if (self.back_order_flag && available) {
  3256. // actionArray = @[move2BackAction,notifyMeAction,deleteRowAction,addWatchAction];
  3257. [actionArray addObject:move2BackAction];
  3258. }
  3259. [actionArray addObjectsFromArray:@[notifyMeAction,deleteRowAction,addWatchAction]];
  3260. // edit price
  3261. NSMutableDictionary * item_json = nil;
  3262. if (self.back_order_flag) {
  3263. item_json = [[self itemJsonAtIndexPath:indexPath] mutableCopy];
  3264. } else {
  3265. item_json = [self.content_arr[indexPath.row] mutableCopy];
  3266. }
  3267. NSString* product_id = [item_json valueForKey:@"product_id"];
  3268. int is_rate = [item_json[@"is_rate"] intValue];
  3269. if(appDelegate.can_set_cart_price && product_id.length>0&& is_rate==0) {
  3270. // actionArray = @[move2BackAction,notifyMeAction,deleteRowAction,addWatchAction,editRowAction];
  3271. [actionArray addObject:editRowAction];
  3272. }
  3273. // return actionArray;
  3274. return [UISwipeActionsConfiguration configurationWithActions:actionArray];
  3275. }
  3276. }
  3277. else
  3278. #ifndef SCANNER_ORDER
  3279. return [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction,addWatchAction]];
  3280. // return @[deleteRowAction,addWatchAction];
  3281. #else
  3282. // return @[deleteRowAction];
  3283. return [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
  3284. #endif
  3285. }
  3286. - (void)tableView:(UITableView *)tableView
  3287. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  3288. }
  3289. #pragma mark - Support scanner
  3290. -(void) onDecodedData:(NSString*) value
  3291. {
  3292. //
  3293. #if defined(USE_BLE_SCANNER) || defined(USE_MFI_SCANNER)
  3294. if(self.stop_response)
  3295. {
  3296. [RAUtils playSound:@"wrong" type:@"wav"];
  3297. return;
  3298. }
  3299. #endif
  3300. int cqty=0;
  3301. if (self.back_order_flag) {
  3302. NSInteger section_count = [self sectionCount];
  3303. for (int i = 0; i < section_count; i++) {
  3304. NSInteger item_count = [self itemCountInSection:i];
  3305. for (int j = 0; j < item_count; j++) {
  3306. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];
  3307. NSDictionary* item = [self itemJsonAtIndexPath:indexPath];
  3308. if([item[@"model"] isEqualToString:value]) {
  3309. cqty=[item[@"count"] intValue];
  3310. }
  3311. }
  3312. }
  3313. } else {
  3314. for(int i=0;i<self.content_arr.count;i++)
  3315. {
  3316. NSDictionary* item= self.content_arr[i];
  3317. if([item[@"model"] isEqualToString:value])
  3318. cqty=[item[@"count"]intValue];
  3319. }
  3320. }
  3321. if(cqty>0)
  3322. {
  3323. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"QTY: %d of this model already in cart. Continue ?",cqty] message:nil preferredStyle:UIAlertControllerStyleAlert];
  3324. //block代码块取代了delegate
  3325. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  3326. [self addtocart:value];
  3327. }];
  3328. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  3329. DebugLog(@"No");
  3330. }];
  3331. [alertControl addAction:actionOne];
  3332. [alertControl addAction:alertthree];
  3333. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  3334. [self presentViewController:alertControl animated:YES completion:nil];
  3335. }
  3336. else
  3337. [self addtocart:value];
  3338. }
  3339. -(void) addtocart:(NSString*) modelname
  3340. {
  3341. #ifdef QUERY_BY_UPC
  3342. {
  3343. #if defined(USE_BLE_SCANNER) || defined(USE_MFI_SCANNER)
  3344. self.stop_response=true;
  3345. #endif
  3346. [RANetwork request_addto_cart_byupc:modelname withScreen:ScreenCodeCamScan completionHandler:^(NSMutableDictionary *result) {
  3347. NSDictionary* return_json = result;
  3348. if([[return_json valueForKey:@"result"] intValue]==2)
  3349. {
  3350. [ActiveViewController Notify:@"OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  3351. [ActiveViewController Notify:@"CartViewController" Message:RA_NOTIFICATION_SCAN_RELOAD_DATA];
  3352. [RAUtils playSound:@"add" type:@"wav"];
  3353. }
  3354. else
  3355. {
  3356. self.stop_response=true;
  3357. [RAUtils playSound:@"wrong" type:@"wav"];
  3358. [RAUtils message_box:@"Add To Cart" message:return_json[@"err_msg"] completion:^{
  3359. self.stop_response=false;
  3360. }];
  3361. }
  3362. //#if defined(USE_BLE_SCANNER) || defined(USE_MFI_SCANNER)
  3363. //
  3364. //
  3365. //
  3366. //#endif
  3367. }];
  3368. }
  3369. #else
  3370. [RANetwork request_addto_cart_byname:modelname withScreen:ScreenCodeCart completionHandler:^(NSMutableDictionary *result) {
  3371. NSDictionary* return_json =result;
  3372. if([[return_json valueForKey:@"result"] intValue]==2)
  3373. {
  3374. NSDictionary* newitem = [return_json objectForKey:@"item_0"];
  3375. NSString* item_id = [newitem valueForKey:@"cart_item_id"];
  3376. // bool isnew=false;
  3377. //
  3378. // [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  3379. if (self.back_order_flag) {
  3380. NSInteger section_count = [self sectionCount];
  3381. for (int i = 0; i < section_count; i++) {
  3382. NSInteger item_count = [self itemCountInSection:i];
  3383. BOOL ready_break = NO;
  3384. for (int j = 0; j < item_count; j++) {
  3385. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];
  3386. NSDictionary* obj_json = [self itemJsonAtIndexPath:indexPath];
  3387. NSString* _id =[obj_json valueForKey:@"cart_item_id"];
  3388. if([_id isEqualToString:item_id])
  3389. {
  3390. [self updateItemJson:nil atIndexPath:indexPath];
  3391. ready_break = YES;
  3392. break;
  3393. }
  3394. }
  3395. if (ready_break) {
  3396. break;
  3397. }
  3398. }
  3399. } else {
  3400. for(int i=0;i<self.content_arr.count;i++)
  3401. {
  3402. NSDictionary* obj_json = self.content_arr[i];
  3403. NSString* _id =[obj_json valueForKey:@"cart_item_id"];
  3404. if([_id isEqualToString:item_id])
  3405. {
  3406. // self.content_arr[i] = newitem;
  3407. [self.content_arr removeObjectAtIndex:i];
  3408. break;
  3409. // [self.content_arr removeObjectAtIndex:i];
  3410. // [self.itemListTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  3411. // appDelegate.cart_count--;
  3412. // [appDelegate update_count_mark];
  3413. }
  3414. }
  3415. }
  3416. AppDelegate *appDelegate =nil;
  3417. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  3418. #ifdef OFFLINE_MODE
  3419. if(appDelegate.offline_mode)
  3420. {
  3421. [self ReloadData];
  3422. }
  3423. else
  3424. #endif
  3425. {
  3426. if (self.back_order_flag) {
  3427. [self ReloadData];
  3428. } else {
  3429. [self.content_arr insertObject:newitem atIndex:0];
  3430. [self.itemListTable reloadData];
  3431. [self refresh_total];
  3432. }
  3433. }
  3434. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  3435. #ifdef RA_NOTIFICATION
  3436. [ActiveViewController Notify:@"CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  3437. #else
  3438. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  3439. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  3440. #endif
  3441. }
  3442. else
  3443. {
  3444. [RAUtils message_box:@"Add To Cart" message:[return_json valueForKey:@"err_msg"] completion:nil] ;
  3445. }
  3446. }];
  3447. #endif
  3448. }
  3449. #pragma mark - sort button
  3450. - (void)cartSortButtonClicked:(UIButton *)sender {
  3451. DebugLog(@"cart sort button clicked");
  3452. [self.view addSubview:self.sortItemController.view];
  3453. }
  3454. - (SortItemViewController *)sortItemController {
  3455. if (!_sortItemController) {
  3456. _sortItemController = [[SortItemViewController alloc] initWithTableOrigin:CGPointMake(20, 50)];
  3457. _sortItemController.sortIndex = self.sortIndex;
  3458. _sortItemController.sortData = @[
  3459. @{@"title":@"Last",@"icon":@"TX_18"},
  3460. @{@"title":@"First",@"icon":@"TS_18"},
  3461. @{@"title":@"Item number a-z",@"icon":@"IX_18"},
  3462. @{@"title":@"Item number z-a",@"icon":@"IS_18"},
  3463. @{@"title":@"Description",@"icon":@"DX_18"},
  3464. ];
  3465. __weak typeof(self) weakSelf = self;
  3466. _sortItemController.sortBlock = ^(int sort){
  3467. weakSelf.sortIndex = sort;
  3468. [weakSelf reload_data];
  3469. };
  3470. }
  3471. // _sortItemController.sortIndex = self.sortIndex;
  3472. return _sortItemController;
  3473. }
  3474. - (SortButton *)sortButton {
  3475. if (!_sortButton) {
  3476. _sortButton = [SortButton sortButtonWithHeight:40];
  3477. _sortButton.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
  3478. [_sortButton addTarget:self action:@selector(cartSortButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  3479. }
  3480. return _sortButton;
  3481. }
  3482. - (void)setSortIndex:(int)sortIndex {
  3483. _sortIndex = sortIndex;
  3484. NSString *selectedImageName = @"";
  3485. switch (sortIndex) {
  3486. case 0:{
  3487. selectedImageName = @"TX_22";
  3488. }
  3489. break;
  3490. case 1:{
  3491. selectedImageName = @"TS_22";
  3492. }
  3493. break;
  3494. case 2:{
  3495. selectedImageName = @"IX_22";
  3496. }
  3497. break;
  3498. case 3:{
  3499. selectedImageName = @"IS_22";
  3500. }
  3501. break;
  3502. case 4:{
  3503. selectedImageName = @"DX_22";
  3504. }
  3505. break;
  3506. default:
  3507. break;
  3508. }
  3509. self.sortButton.imageView.image = [UIImage imageNamed:selectedImageName];
  3510. }
  3511. #pragma mark - rotation
  3512. //- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  3513. //
  3514. // self.sortItemController.view.frame = self.view.bounds;
  3515. //}
  3516. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  3517. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  3518. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  3519. // what ever you want to prepare
  3520. } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  3521. // self.sortItemController.view.frame = self.view.bounds;
  3522. self.sortItemController.view.frame = self.view.bounds;
  3523. self.toolpanel.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.toolpanel.bounds].CGPath;
  3524. // [self.itemListTable reloadData];
  3525. self.cartIndicatorBar.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.cartIndicatorBar.bounds].CGPath;
  3526. [self layoutCartIndicatorBar];
  3527. }];
  3528. }
  3529. -(BOOL) shouldAutorotate
  3530. {
  3531. return YES;
  3532. }
  3533. //- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  3534. // return YES;
  3535. //}
  3536. //- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  3537. // self.sortItemController.view.frame = self.view.bounds;
  3538. // self.toolpanel.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.toolpanel.bounds].CGPath;
  3539. //
  3540. //// [self.itemListTable reloadData];
  3541. //
  3542. // self.cartIndicatorBar.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.cartIndicatorBar.bounds].CGPath;
  3543. // [self layoutCartIndicatorBar];
  3544. //}
  3545. #pragma mark - notification action
  3546. - (void)priceTypeChanged:(id)notification {
  3547. [self reload_data];
  3548. #ifdef RA_NOTIFICATION
  3549. [ActiveViewController Notify:@"OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  3550. #else
  3551. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3552. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  3553. #endif
  3554. }
  3555. #pragma mark - button action
  3556. - (IBAction)emailButtonClick:(UIButton *)sender {
  3557. if (![self isOrderAvailable]) {
  3558. return;
  3559. }
  3560. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3561. JKMessageBoxController *emailAddrVC = [JKMessageBoxController messageBoxControllerWithTip:@"Please enter email address"];
  3562. emailAddrVC.textFiled.secureTextEntry = NO;
  3563. emailAddrVC.yesButtonTitle = @"send";
  3564. __weak typeof(emailAddrVC) weakVC = emailAddrVC;
  3565. __weak typeof(self) weakself = self;
  3566. emailAddrVC.textHandler = ^(NSString *text){
  3567. // 验证邮箱格式是否正确
  3568. // NSString *match = EMAIL_MATCHES;
  3569. // NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",match];
  3570. // BOOL isEmailAddr = [predicate evaluateWithObject:text];
  3571. BOOL isEmailAddr = YES;
  3572. if (isEmailAddr) {
  3573. // 验证是邮件地址,发送邮件
  3574. [weakVC dismissViewControllerAnimated:YES completion:^{
  3575. // __block UIAlertController *waitting_alert = [RAUtils waiting_alert:self title:@"Sending Email" completion:^{
  3576. PopWaitAlert* pop = [RAUtils waiting_pop:@"Sending Email" completion:nil];
  3577. [RANetwork request_qouteorder:appDelegate.order_code emailAddr:text completionHandler:^(NSMutableDictionary *result) {
  3578. DebugLog(@"email cart result: %@",result);
  3579. // [waitting_alert dismissViewControllerAnimated:YES completion:^{
  3580. [pop hide];
  3581. // [waitting_alert dismissWithClickedButtonIndex:0 animated:YES];
  3582. if ([[result objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  3583. UIAlertController *errorAlertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"The email send to %@ failed",text] preferredStyle:UIAlertControllerStyleAlert];
  3584. UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  3585. }];
  3586. [errorAlertVC addAction:action];
  3587. [weakself presentViewController:errorAlertVC animated:YES completion:nil];
  3588. }else {
  3589. [RAUtils message_box:@"Message" message:@"Send Email Success" completion:nil];
  3590. }
  3591. // }];
  3592. }];
  3593. // }];
  3594. }];
  3595. } else {
  3596. // 非邮件地址,警告
  3597. [weakVC warning:@"Please enter right email address"];
  3598. }
  3599. };
  3600. [self presentViewController:emailAddrVC animated:YES completion:nil];
  3601. }
  3602. - (IBAction)printCartButtonClick:(UIButton *)sender {
  3603. if (![self isOrderAvailable]) {
  3604. return;
  3605. }
  3606. RAPDFViewController *ViewController = [[UIStoryboard storyboardWithName:@"wkweb" bundle:nil] instantiateViewControllerWithIdentifier:@"RAPDFViewController"];
  3607. ViewController.url = self.print_url;
  3608. ViewController.canSave = false;
  3609. ViewController.isLocalfile=NO;
  3610. NSString* subject;
  3611. subject =@"Cart Print";
  3612. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  3613. NSString* customer_email= [appDelegate.customerInfo valueForKey:@"customer_email"];
  3614. NSMutableArray* send_to = [[NSMutableArray alloc]init];
  3615. if(customer_email.length>0)
  3616. {
  3617. send_to=[[customer_email componentsSeparatedByString:NSLocalizedString(@";", nil)] mutableCopy];
  3618. }
  3619. ViewController.mail_to = send_to;
  3620. ViewController.filename = [NSString stringWithFormat:@"%@_Product_List.pdf",COMPANY_SHORT_NAME];
  3621. ViewController.mail_subject = subject;
  3622. ViewController.hidenavi = false;
  3623. [self.navigationController pushViewController:ViewController animated:YES];
  3624. }
  3625. - (void)showAddressBoxToEmailProduct:(NSString *)productID {
  3626. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  3627. JKMessageBoxController *emailAddrVC = [JKMessageBoxController messageBoxControllerWithTip:@"E-mail not setup, please enter your default e-mail address" ContentSize:CGSizeMake(400, 150)];
  3628. emailAddrVC.textFiled.secureTextEntry = NO;
  3629. emailAddrVC.yesButtonTitle = @"send";
  3630. __weak typeof(emailAddrVC) weakVC = emailAddrVC;
  3631. __weak typeof(self) weakself = self;
  3632. emailAddrVC.textHandler = ^(NSString *text){
  3633. // 验证邮箱格式是否正确
  3634. // NSString *match = EMAIL_MATCHES;
  3635. // NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",match];
  3636. // BOOL isEmailAddr = [predicate evaluateWithObject:text];
  3637. BOOL isEmailAddr = YES;
  3638. if (isEmailAddr) {
  3639. // 验证是邮件地址,发送邮件
  3640. [weakVC dismissViewControllerAnimated:YES completion:^{
  3641. // __block UIAlertController *waitting_alert = [RAUtils waiting_alert:self title:@"Sending Email" completion:^{
  3642. PopWaitAlert* pop = [RAUtils waiting_pop:@"Sending Email" completion:nil];
  3643. [RANetwork request_notifymodel:productID emailAddr:text withScreen:ScreenCodeCart completionHandler:^(NSMutableDictionary *result) {
  3644. // [waitting_alert dismissWithClickedButtonIndex:0 animated:NO];
  3645. // [waitting_alert dismissViewControllerAnimated:YES completion:^{
  3646. [pop hide];
  3647. int resultStatus = [[result objectForKey:@"result"] intValue];
  3648. if (resultStatus != RESULT_TRUE && resultStatus != RESULT_NO_EMAIL_ADDRESS) {
  3649. NSString *msg = [NSString stringWithFormat:@"The email send failed"];
  3650. if ([result valueForKey:@"err_msg"]) {
  3651. msg = [result valueForKey:@"err_msg"];
  3652. }
  3653. UIAlertController *errorAlertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:msg preferredStyle:UIAlertControllerStyleAlert];
  3654. UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  3655. }];
  3656. [errorAlertVC addAction:action];
  3657. [weakself presentViewController:errorAlertVC animated:YES completion:nil];
  3658. }else if(resultStatus == RESULT_TRUE){
  3659. [RAUtils message_box:@"Message" message:@"Notification is sent to default email address." completion:nil];
  3660. } else if (resultStatus == RESULT_NO_EMAIL_ADDRESS) {
  3661. [weakself showAddressBoxToEmailProduct:productID];
  3662. }
  3663. // }];
  3664. }];
  3665. // }];
  3666. }];
  3667. } else {
  3668. // 非邮件地址,警告
  3669. [weakVC warning:@"Please enter right email address"];
  3670. }
  3671. };
  3672. [self presentViewController:emailAddrVC animated:YES completion:nil];
  3673. }
  3674. #pragma mark - Back Order
  3675. - (void) setCart_json:(NSDictionary *)cart_json {
  3676. _cart_json = cart_json;
  3677. [self fillContentArray];
  3678. }
  3679. - (void)fillContentArray {
  3680. NSMutableArray *arr = [NSMutableArray array];
  3681. for (int i = 0; i < [self sectionCount]; i++) {
  3682. NSInteger item_count = [self itemCountInSection:i];
  3683. for (int j = 0; j < item_count; j++) {
  3684. NSDictionary *item = [self itemJsonAtIndexPath:[NSIndexPath indexPathForRow:j inSection:i]];
  3685. [arr addObject:item];
  3686. }
  3687. }
  3688. self.content_arr = arr;
  3689. [self refresh_total];
  3690. }
  3691. - (NSInteger)sectionCount {
  3692. return [[self.cart_json objectForKey:@"section_count"] integerValue];
  3693. }
  3694. - (NSDictionary *)jsonAtSection:(NSInteger)section {
  3695. return [self.cart_json objectForKey:[NSString stringWithFormat:@"section_%ld",section]];
  3696. }
  3697. - (NSInteger)itemCountInSection:(NSInteger)section {
  3698. NSDictionary *section_json = [self jsonAtSection:section];
  3699. return [[section_json objectForKey:@"count"] integerValue];
  3700. }
  3701. - (NSString *)titleForSection:(NSInteger)section {
  3702. NSDictionary *section_json = [self jsonAtSection:section];
  3703. return [section_json objectForKey:@"title"];
  3704. }
  3705. - (NSDictionary *)itemJsonAtIndexPath:(NSIndexPath *)indexPath {
  3706. NSDictionary *section = [self jsonAtSection:indexPath.section];
  3707. return [section objectForKey:[NSString stringWithFormat:@"item_%ld",indexPath.row]];
  3708. }
  3709. - (void)updateItemJson:(NSDictionary *)new_item_json atIndexPath:(NSIndexPath *)indexPath {
  3710. NSMutableDictionary *new_cart_json = [self.cart_json mutableCopy];
  3711. NSMutableDictionary *new_section_json = [[self jsonAtSection:indexPath.section] mutableCopy];
  3712. NSString *section_key = [NSString stringWithFormat:@"section_%ld",indexPath.section];
  3713. NSString *item_key = [NSString stringWithFormat:@"item_%ld",indexPath.row];
  3714. if (new_item_json) {
  3715. // 更新Item
  3716. [new_section_json setObject:new_item_json forKey:item_key];
  3717. } else {
  3718. // 删除Item
  3719. NSInteger item_count = [self itemCountInSection:indexPath.section];
  3720. for (NSInteger i = 0; i < item_count; i++) {
  3721. if (i >= indexPath.row) {
  3722. if (i < item_count - 1) {
  3723. item_key = [NSString stringWithFormat:@"item_%ld",i + 1];
  3724. NSDictionary *followed_item_json = [new_section_json objectForKey:item_key];
  3725. item_key = [NSString stringWithFormat:@"item_%ld",i];
  3726. [new_section_json setObject:followed_item_json forKey:item_key];
  3727. } else {
  3728. item_key = [NSString stringWithFormat:@"item_%ld",i];
  3729. [new_section_json removeObjectForKey:item_key];
  3730. }
  3731. }
  3732. }
  3733. [new_section_json setObject:@(item_count - 1) forKey:@"count"];
  3734. }
  3735. [new_cart_json setObject:new_section_json forKey:section_key];
  3736. self.cart_json = [new_cart_json copy];
  3737. }
  3738. - (void)reRefreshView {
  3739. [self.itemListTable reloadData];
  3740. }
  3741. #pragma mark - RA_NOTIFICAITON
  3742. -(void) refresh_ui
  3743. {
  3744. [self.itemListTable reloadData];
  3745. [self refresh_total];
  3746. }
  3747. -(void) refresh_price
  3748. {
  3749. [self refresh_ui];
  3750. }
  3751. - (void) reload_data {
  3752. //
  3753. // [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayRun) object:nil];
  3754. // [self performSelector:@selector(delayRun) withObject:nil afterDelay:0.8];
  3755. //
  3756. __weak typeof(self) weakself = self;
  3757. self.currentOrderIsMerged = NO;
  3758. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  3759. if (weakself) {
  3760. __strong typeof(weakself) strongself = weakself;
  3761. [strongself operation_reload_data];
  3762. }
  3763. }];
  3764. [self.dataOperationQueue addOperation:operation];
  3765. }
  3766. -(void) scanner_refresh
  3767. {
  3768. __weak typeof(self) weakself = self;
  3769. self.currentOrderIsMerged = NO;
  3770. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  3771. if (weakself) {
  3772. __strong typeof(weakself) strongself = weakself;
  3773. [strongself operation_scan_reload_data];
  3774. }
  3775. }];
  3776. [self.dataOperationQueue addOperation:operation];
  3777. }
  3778. //-(void) delayRun
  3779. //{
  3780. // __weak typeof(self) weakself = self;
  3781. // self.currentOrderIsMerged = NO;
  3782. // NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  3783. //
  3784. // if (weakself) {
  3785. // __strong typeof(weakself) strongself = weakself;
  3786. // [strongself operation_reload_data];
  3787. // }
  3788. //
  3789. // }];
  3790. //
  3791. // [self.dataOperationQueue addOperation:operation];
  3792. //}
  3793. @end