CartViewController.m 192 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150
  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. int mpack =[[item_json valueForKey:@"mpack"] intValue];
  2509. #ifdef BUILD_CONTRAST
  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