OLDataProvider.m 353 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. #import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. #import "UIProgressView+AFNetworking.h"
  15. #import "AFHTTPSessionManager.h"
  16. @interface OLDataProvider ()
  17. @end
  18. @implementation OLDataProvider
  19. +(NSArray*) enumOfflineOrder
  20. {
  21. NSMutableArray* ret = [[NSMutableArray alloc] init];
  22. sqlite3 *db = [iSalesDB get_db];
  23. NSString *sqlQuery =@"select so_id from offline_order where order_id is null";
  24. sqlite3_stmt * statement;
  25. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  26. if ( dbresult== SQLITE_OK)
  27. {
  28. while (sqlite3_step(statement) == SQLITE_ROW)
  29. {
  30. char *so_id = (char*)sqlite3_column_text(statement, 0);
  31. NSString* nsso_id=nil;
  32. if(so_id!=nil)
  33. {
  34. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  35. }
  36. [ret addObject:nsso_id];
  37. }
  38. sqlite3_finalize(statement);
  39. }
  40. [iSalesDB close_db:db];
  41. return ret;
  42. }
  43. +(void) uploadFile:(NSString*) file
  44. {
  45. NSData* data = [NSData dataWithContentsOfFile: file];
  46. UIApplication * app = [UIApplication sharedApplication];
  47. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  48. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  49. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  50. if(appDelegate.user!=nil)
  51. [params setValue:appDelegate.user forKey:@"user"];
  52. // if(appDelegate.contact_id!=nil)
  53. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  54. if(appDelegate.password!=nil)
  55. [params setValue:appDelegate.password forKey:@"password"];
  56. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  57. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  58. } error:nil];
  59. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  60. NSProgress *progress = nil;
  61. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  62. if (error) {
  63. NSString* err_msg = [error localizedDescription];
  64. NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  65. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  66. NSLog(@"data string: %@",str);
  67. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  68. } else {
  69. NSLog(@"response ");
  70. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  71. // 再将NSData转为字符串
  72. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  73. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  74. NSLog(@"data string: %@",jsonStr);
  75. NSDictionary* json = responseObject;
  76. if([[json valueForKey:@"result"] intValue]==2)
  77. {
  78. // NSString* img_url_down = json[@"img_url_aname"];
  79. // NSString* img_url_up = json[@"img_url"];
  80. }
  81. else
  82. {
  83. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  84. }
  85. }
  86. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  87. }];
  88. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  89. //
  90. [uploadTask resume];
  91. }
  92. +(NSString*) prepareUpload:(NSArray*) arr_order
  93. {
  94. NSString* serial= [[NSUUID UUID] UUIDString];
  95. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  96. NSString *cachefolder = [paths objectAtIndex:0];
  97. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  98. upfolder = [upfolder stringByAppendingPathComponent:serial];
  99. NSFileManager* fileManager = [NSFileManager defaultManager];
  100. BOOL bdir=YES;
  101. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  102. {
  103. NSError *error = nil;
  104. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  105. if(!bsuccess)
  106. DebugLog(@"Create UPLOAD folder failed");
  107. }
  108. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  109. ret[@"contact"]=[self prepareContact:serial];
  110. ret[@"wishlist"]=[self prepareWishlist:serial];
  111. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  112. NSString* str= [RAUtils dict2string:ret];
  113. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  114. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  115. NSError *error=nil;
  116. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  117. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  118. ZipArchive* zip = [[ZipArchive alloc] init];
  119. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  120. for(int i=0;i<arr_files.count;i++)
  121. {
  122. NSString* file=arr_files[i];
  123. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  124. }
  125. if( ![zip CloseZipFile2] )
  126. {
  127. zippath = @"";
  128. }
  129. return zippath;
  130. }
  131. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  132. {
  133. if(filename.length==0)
  134. return false;
  135. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  136. bool ret=false;
  137. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  138. NSString *cachefolder = [paths objectAtIndex:0];
  139. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  140. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  141. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  142. upfolder = [upfolder stringByAppendingPathComponent:serial];
  143. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  144. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  145. NSFileManager* fileManager = [NSFileManager defaultManager];
  146. BOOL bdir=NO;
  147. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  148. {
  149. NSError *error = nil;
  150. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  151. {
  152. ret=false;
  153. }
  154. else
  155. {
  156. ret=true;
  157. }
  158. }
  159. return ret;
  160. }
  161. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  162. {
  163. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  164. sqlite3 *db = [iSalesDB get_db];
  165. NSString *sqlQuery =@"select _id,product_id,item_id,qty,create_time from wishlist";
  166. sqlite3_stmt * statement;
  167. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  168. int count=0;
  169. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  170. if ( dbresult== SQLITE_OK)
  171. {
  172. while (sqlite3_step(statement) == SQLITE_ROW)
  173. {
  174. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  175. int _id = sqlite3_column_int(statement, 0);
  176. int product_id = sqlite3_column_int(statement, 1);
  177. int item_id = sqlite3_column_int(statement, 2);
  178. int qty = sqlite3_column_int(statement, 3);
  179. char *create_time = (char*)sqlite3_column_text(statement, 4);
  180. NSString* nscreate_time=nil;
  181. if(create_time!=nil)
  182. {
  183. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  184. }
  185. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  186. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  187. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  188. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  189. itemjson[@"create_time"]=nscreate_time;
  190. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  191. count++;
  192. }
  193. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  194. sqlite3_finalize(statement);
  195. }
  196. ret[@"count"]=[NSNumber numberWithInt:count ];
  197. [iSalesDB close_db:db];
  198. return ret;
  199. }
  200. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  201. {
  202. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  203. UIApplication * app = [UIApplication sharedApplication];
  204. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  205. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",orderCode ];
  206. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  207. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  208. DebugLog(@"offline_login sql:%@",sqlQuery);
  209. sqlite3_stmt * statement;
  210. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  211. int count=0;
  212. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  213. if ( dbresult== SQLITE_OK)
  214. {
  215. while (sqlite3_step(statement) == SQLITE_ROW)
  216. {
  217. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  218. int product_id = sqlite3_column_int(statement, 0);
  219. char *str_price = (char*)sqlite3_column_text(statement, 1);
  220. int item_id = sqlite3_column_int(statement, 7);
  221. NSString* Price=nil;
  222. if(str_price==nil)
  223. {
  224. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  225. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  226. if(price==nil)
  227. Price=@"No Price.";
  228. else
  229. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  230. }
  231. else
  232. {
  233. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  234. }
  235. double discount = sqlite3_column_double(statement, 2);
  236. int item_count = sqlite3_column_int(statement, 3);
  237. char *line_note = (char*)sqlite3_column_text(statement, 4);
  238. NSString *nsline_note=nil;
  239. if(line_note!=nil)
  240. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  241. char *name = (char*)sqlite3_column_text(statement, 5);
  242. NSString *nsname=nil;
  243. if(name!=nil)
  244. nsname= [[NSString alloc]initWithUTF8String:name];
  245. char *description = (char*)sqlite3_column_text(statement, 6);
  246. NSString *nsdescription=nil;
  247. if(description!=nil)
  248. nsdescription= [[NSString alloc]initWithUTF8String:description];
  249. // int stockUom = sqlite3_column_int(statement, 8);
  250. // int _id = sqlite3_column_int(statement, 9);
  251. //
  252. //
  253. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  254. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  255. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  256. // int carton=[bsubtotaljson[@"carton"] intValue];
  257. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  258. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  259. itemjson[@"model"]=nsname;
  260. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  261. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  262. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  263. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  264. // itemjson[@"check"]=@"true";
  265. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  266. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  267. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  268. itemjson[@"unit_price"]=Price;
  269. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  270. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  271. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  272. itemjson[@"note"]=nsline_note;
  273. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  274. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  275. count++;
  276. }
  277. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  278. sqlite3_finalize(statement);
  279. }
  280. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  281. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  282. return ret;
  283. }
  284. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  285. {
  286. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  287. sqlite3 *db = [iSalesDB get_db];
  288. for(int i=0;i<arr_soid.count;i++)
  289. {
  290. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  291. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,sync_data,sign_picpath,so_id from offline_order where so_id='%@'",arr_soid[i]];
  292. sqlite3_stmt * statement;
  293. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  294. //int count=0;
  295. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  296. if ( dbresult== SQLITE_OK)
  297. {
  298. if (sqlite3_step(statement) == SQLITE_ROW)
  299. {
  300. int _id = sqlite3_column_int(statement, 0);
  301. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  302. NSString* nssync_data=nil;
  303. if(sync_data!=nil)
  304. {
  305. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  306. }
  307. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  308. NSString* nsimg_1=nil;
  309. if(img_1!=nil)
  310. {
  311. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  312. [self copy_upImg:serial file:nsimg_1];
  313. }
  314. char *so_no = (char*)sqlite3_column_text(statement, 3);
  315. NSString* nsso_no=nil;
  316. if(so_no!=nil)
  317. {
  318. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  319. }
  320. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  321. itemjson[@"json_data"]= nssync_data;
  322. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  323. }
  324. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  325. }
  326. sqlite3_finalize(statement);
  327. ret[[NSString stringWithFormat:@"item_%d",i]]=itemjson;
  328. }
  329. ret[@"count"]=[NSNumber numberWithInt:arr_soid.count ];
  330. [iSalesDB close_db:db];
  331. return ret;
  332. }
  333. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  334. {
  335. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  336. sqlite3 *db = [iSalesDB get_db];
  337. // UIApplication * app = [UIApplication sharedApplication];
  338. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  339. NSString *sqlQuery =@"select _id,img_0,img_1,img_2,sync_data from offline_contact where is_active=1 and not sync_data is null";
  340. sqlite3_stmt * statement;
  341. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  342. int count=0;
  343. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  344. if ( dbresult== SQLITE_OK)
  345. {
  346. while (sqlite3_step(statement) == SQLITE_ROW)
  347. {
  348. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  349. int _id = sqlite3_column_int(statement, 0);
  350. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  351. NSString* nsimg_0=nil;
  352. if(img_0!=nil)
  353. {
  354. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  355. [self copy_upImg:serial file:nsimg_0];
  356. }
  357. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  358. NSString* nsimg_1=nil;
  359. if(img_1!=nil)
  360. {
  361. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  362. [self copy_upImg:serial file:nsimg_1];
  363. }
  364. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  365. NSString* nsimg_2=nil;
  366. if(img_2!=nil)
  367. {
  368. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  369. [self copy_upImg:serial file:nsimg_2];
  370. }
  371. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  372. NSString* nssync_data=nil;
  373. if(sync_data!=nil)
  374. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  375. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  376. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  377. itemjson[@"json_data"]= nssync_data;
  378. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  379. count++;
  380. }
  381. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  382. sqlite3_finalize(statement);
  383. }
  384. ret[@"count"]=[NSNumber numberWithInt:count ];
  385. [iSalesDB close_db:db];
  386. return ret;
  387. }
  388. +(bool) check_offlinedata
  389. {
  390. UIApplication * app = [UIApplication sharedApplication];
  391. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  392. sqlite3 *db = [iSalesDB get_db];
  393. NSString * where=@"1=1";
  394. // if(appDelegate.user!=nil)
  395. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  396. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  397. [iSalesDB close_db:db];
  398. if(count==0)
  399. {
  400. return false;
  401. }
  402. return true;
  403. //
  404. //[iSalesDB close_db:db];
  405. }
  406. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  407. {
  408. UIApplication * app = [UIApplication sharedApplication];
  409. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  410. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  411. sqlite3 *db = [iSalesDB get_db];
  412. NSString* collectId=params[@"collectId"];
  413. NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  414. [iSalesDB execSql:sqlQuery db:db];
  415. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  416. [iSalesDB close_db:db];
  417. appDelegate.wish_count =count;
  418. [appDelegate update_count_mark];
  419. ret[@"result"]= [NSNumber numberWithInt:2];
  420. return ret;
  421. }
  422. +(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  423. {
  424. UIApplication * app = [UIApplication sharedApplication];
  425. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  426. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  427. sqlite3 *db = [iSalesDB get_db];
  428. NSString* product_id=params[@"product_id"];
  429. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  430. for(int i=0;i<arr.count;i++)
  431. {
  432. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  433. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  434. if(count==0)
  435. {
  436. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  437. [iSalesDB execSql:sqlQuery db:db];
  438. }
  439. }
  440. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  441. [iSalesDB close_db:db];
  442. appDelegate.wish_count =count;
  443. [appDelegate update_count_mark];
  444. ret[@"result"]= [NSNumber numberWithInt:2];
  445. return ret;
  446. //
  447. //return ret;
  448. }
  449. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  450. {
  451. UIApplication * app = [UIApplication sharedApplication];
  452. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  453. NSString* user = appDelegate.user;
  454. sqlite3 *db = [iSalesDB get_db];
  455. NSString* sqlQuery = @"select w.product_id,m.name||'\n'||m.description,w._id from wishlist as w left join product as m on w.product_id=m.product_id order by w.create_time;";
  456. sqlite3_stmt * statement;
  457. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  458. int count=0;
  459. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  460. {
  461. while (sqlite3_step(statement) == SQLITE_ROW)
  462. {
  463. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  464. int product_id = sqlite3_column_double(statement, 0);
  465. char *description = (char*)sqlite3_column_text(statement, 1);
  466. if(description==nil)
  467. description= "";
  468. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  469. int item_id = sqlite3_column_double(statement, 2);
  470. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  471. // char *url = (char*)sqlite3_column_text(statement, 3);
  472. // if(url==nil)
  473. // url="";
  474. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  475. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  476. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  477. item[@"description"]= nsdescription;
  478. item[@"img"]= nsurl;
  479. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  480. count++;
  481. }
  482. ret[@"count"]= [NSNumber numberWithInt:count];
  483. ret[@"total_count"]= [NSNumber numberWithInt:count];
  484. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  485. ret[@"result"]= [NSNumber numberWithInt:2];
  486. appDelegate.wish_count =count;
  487. [appDelegate update_count_mark];
  488. sqlite3_finalize(statement);
  489. }
  490. [iSalesDB close_db:db];
  491. return ret;
  492. }
  493. +(NSDictionary*) offline_notimpl
  494. {
  495. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  496. ret[@"result"]=@"8";
  497. ret[@"err_msg"]=@"offline mode does not support this function.";
  498. return ret;
  499. }
  500. +(NSDictionary*) offline_home
  501. {
  502. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  503. NSString *cachefolder = [paths objectAtIndex:0];
  504. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  505. NSData* json =nil;
  506. json=[NSData dataWithContentsOfFile:img_cache];
  507. NSError *error=nil;
  508. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  509. return menu;
  510. }
  511. +(NSDictionary*) offline_category_menu
  512. {
  513. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  514. NSString *cachefolder = [paths objectAtIndex:0];
  515. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  516. NSData* json =nil;
  517. json=[NSData dataWithContentsOfFile:img_cache];
  518. NSError *error=nil;
  519. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  520. return menu;
  521. }
  522. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  523. {
  524. NSString* offline_command=params[@"offline_Command"];
  525. NSDictionary* ret=nil;
  526. if([offline_command isEqualToString:@"model_NIYMAL"])
  527. {
  528. NSString* category = params[@"category"];
  529. ret = [self refresh_model_NIYMAL:category];
  530. }
  531. return ret;
  532. }
  533. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  534. {
  535. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  536. [ret setValue:@"2" forKey:@"result"];
  537. [ret setValue:@"Regular Mode" forKey:@"mode"];
  538. sqlite3* db= [iSalesDB get_db];
  539. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  540. [iSalesDB close_db:db];
  541. [ret setObject:detail1_section forKey:@"detail_1"];
  542. return ret;
  543. }
  544. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  545. {
  546. UIApplication * app = [UIApplication sharedApplication];
  547. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  548. // NSArray* arr1 = [self get_user_all_price_type];
  549. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  550. // NSSet *set1 = [NSSet setWithArray:arr1];
  551. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  552. // if(appDelegate.contact_id==nil)
  553. // set2=[set1 mutableCopy];
  554. // else
  555. // [set2 intersectsSet:set1];
  556. // NSArray *retarr = [set2 allObjects];
  557. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  558. // sqlite3 *db = [iSalesDB get_db];
  559. NSString* sqlQuery = nil;
  560. if(appDelegate.contact_id==nil)
  561. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d order by a.order_by;",item_id];
  562. else
  563. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d and b.price_name in(%@) order by a.order_by;",item_id,whereprice];
  564. sqlite3_stmt * statement;
  565. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  566. int count=0;
  567. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  568. {
  569. while (sqlite3_step(statement) == SQLITE_ROW)
  570. {
  571. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  572. char *name = (char*)sqlite3_column_text(statement, 0);
  573. if(name==nil)
  574. name="";
  575. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  576. // double price = sqlite3_column_double(statement, 1);
  577. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  578. // if(isnull==nil)
  579. // item[nsname]= @"No Price";
  580. // else
  581. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  582. char *price = (char*)sqlite3_column_text(statement, 1);
  583. if(price!=nil)
  584. {
  585. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  586. nsprice=[AESCrypt fastdecrypt:nsprice];
  587. if(nsprice.length>0)
  588. {
  589. double dp= [nsprice doubleValue];
  590. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  591. }
  592. }
  593. else
  594. {
  595. item[nsname]= @"No Price";
  596. }
  597. // item[nsname]= nsprice;
  598. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  599. count++;
  600. }
  601. ret[@"count"]= [NSNumber numberWithInt:count];
  602. sqlite3_finalize(statement);
  603. }
  604. // [iSalesDB close_db:db];
  605. return ret;
  606. }
  607. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  608. //{
  609. // NSArray* arr1 = [self get_user_all_price_type:db];
  610. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  611. //
  612. // // NSSet *set1 = [NSSet setWithArray:arr1];
  613. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  614. // // [set2 intersectsSet:set1];
  615. // //
  616. // //
  617. // // NSArray *retarr = [set2 allObjects];
  618. //
  619. // NSString* whereprice=nil;
  620. // if(contact_id==nil)
  621. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  622. // else
  623. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  624. //
  625. //
  626. // // sqlite3 *db = [iSalesDB get_db];
  627. //
  628. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  629. // sqlite3_stmt * statement;
  630. //
  631. //
  632. // NSNumber* ret = nil;
  633. // double dprice=DBL_MAX;
  634. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  635. // {
  636. //
  637. //
  638. // while (sqlite3_step(statement) == SQLITE_ROW)
  639. // {
  640. //
  641. // // double val = sqlite3_column_double(statement, 0);
  642. // char *price = (char*)sqlite3_column_text(statement, 0);
  643. // if(price!=nil)
  644. // {
  645. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  646. // nsprice=[AESCrypt fastdecrypt:nsprice];
  647. // if(nsprice.length>0)
  648. // {
  649. // double dp= [nsprice doubleValue];
  650. // if(dp<dprice)
  651. // dprice=dp;
  652. // }
  653. // }
  654. // }
  655. //
  656. //
  657. //
  658. //
  659. // sqlite3_finalize(statement);
  660. //
  661. //
  662. //
  663. //
  664. // }
  665. //
  666. // // [iSalesDB close_db:db];
  667. //
  668. // if(dprice==DBL_MAX)
  669. // ret= nil;
  670. // else
  671. // ret= [NSNumber numberWithDouble:dprice];
  672. // return ret;
  673. //}
  674. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  675. {
  676. NSArray* arr1 = [self get_user_all_price_type:db];
  677. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  678. NSString* whereprice=nil;
  679. if(contact_id==nil)
  680. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  681. else
  682. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  683. // sqlite3 *db = [iSalesDB get_db];
  684. NSString *productIdCondition = @"1 = 1";
  685. if (product_id) {
  686. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  687. }
  688. NSString *itemIdCondition = @"";
  689. if (item_id) {
  690. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  691. }
  692. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  693. sqlite3_stmt * statement;
  694. NSNumber* ret = nil;
  695. double dprice=DBL_MAX;
  696. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  697. {
  698. while (sqlite3_step(statement) == SQLITE_ROW)
  699. {
  700. // double val = sqlite3_column_double(statement, 0);
  701. char *price = (char*)sqlite3_column_text(statement, 0);
  702. if(price!=nil)
  703. {
  704. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  705. nsprice=[AESCrypt fastdecrypt:nsprice];
  706. if(nsprice.length>0)
  707. {
  708. double dp= [nsprice doubleValue];
  709. if(dp<dprice)
  710. dprice=dp;
  711. }
  712. }
  713. }
  714. sqlite3_finalize(statement);
  715. }
  716. // [iSalesDB close_db:db];
  717. if(dprice==DBL_MAX)
  718. ret= nil;
  719. else
  720. ret= [NSNumber numberWithDouble:dprice];
  721. return ret;
  722. }
  723. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  724. {
  725. NSArray* ret=nil;
  726. // sqlite3 *db = [iSalesDB get_db];
  727. // no customer assigned , use login user contact_id
  728. UIApplication * app = [UIApplication sharedApplication];
  729. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  730. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  731. sqlite3_stmt * statement;
  732. // int count=0;
  733. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  734. {
  735. if (sqlite3_step(statement) == SQLITE_ROW)
  736. {
  737. char *val = (char*)sqlite3_column_text(statement, 0);
  738. if(val==nil)
  739. val="";
  740. NSString* price = [[NSString alloc]initWithUTF8String:val];
  741. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  742. }
  743. sqlite3_finalize(statement);
  744. }
  745. // [iSalesDB close_db:db];
  746. return ret;
  747. }
  748. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  749. {
  750. // sqlite3 *db = [iSalesDB get_db];
  751. if(contact_id==nil)
  752. {
  753. // no customer assigned , use login user contact_id
  754. UIApplication * app = [UIApplication sharedApplication];
  755. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  756. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  757. sqlite3_stmt * statement;
  758. // int count=0;
  759. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  760. {
  761. if (sqlite3_step(statement) == SQLITE_ROW)
  762. {
  763. char *val = (char*)sqlite3_column_text(statement, 0);
  764. if(val==nil)
  765. val="";
  766. contact_id = [[NSString alloc]initWithUTF8String:val];
  767. }
  768. sqlite3_finalize(statement);
  769. }
  770. if(contact_id.length<=0)
  771. {
  772. // [iSalesDB close_db:db];
  773. return nil;
  774. }
  775. }
  776. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  777. sqlite3_stmt * statement;
  778. NSArray* ret=nil;
  779. // int count=0;
  780. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  781. {
  782. if (sqlite3_step(statement) == SQLITE_ROW)
  783. {
  784. char *price_type = (char*)sqlite3_column_text(statement, 0);
  785. if(price_type==nil)
  786. price_type="";
  787. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  788. if(nsprice_type.length>0)
  789. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  790. else
  791. ret=nil;
  792. }
  793. sqlite3_finalize(statement);
  794. }
  795. // [iSalesDB close_db:db];
  796. return ret;
  797. }
  798. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  799. {
  800. NSString* ret= nil;
  801. // sqlite3 *db = [iSalesDB get_db];
  802. NSString *sqlQuery = nil;
  803. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  804. if(product_id==nil && model_name)
  805. sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  806. else if (product_id)
  807. sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  808. sqlite3_stmt * statement;
  809. // int count=0;
  810. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  811. {
  812. if (sqlite3_step(statement) == SQLITE_ROW)
  813. {
  814. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  815. if(imgurl==nil)
  816. imgurl="";
  817. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  818. ret=nsimgurl;
  819. }
  820. sqlite3_finalize(statement);
  821. }
  822. else
  823. {
  824. [ret setValue:@"8" forKey:@"result"];
  825. }
  826. // [iSalesDB close_db:db];
  827. DebugLog(@"data string: %@",ret );
  828. return ret;
  829. }
  830. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  831. {
  832. NSString* ret= nil;
  833. sqlite3 *db = [iSalesDB get_db];
  834. NSString *sqlQuery = nil;
  835. if(product_id==nil)
  836. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  837. else
  838. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  839. sqlite3_stmt * statement;
  840. // int count=0;
  841. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  842. {
  843. if (sqlite3_step(statement) == SQLITE_ROW)
  844. {
  845. char *default_category = (char*)sqlite3_column_text(statement, 0);
  846. if(default_category==nil)
  847. default_category="";
  848. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  849. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  850. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  851. ret=nsdefault_category;
  852. }
  853. sqlite3_finalize(statement);
  854. }
  855. else
  856. {
  857. [ret setValue:@"8" forKey:@"result"];
  858. }
  859. [iSalesDB close_db:db];
  860. DebugLog(@"data string: %@",ret );
  861. return ret;
  862. }
  863. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  864. {
  865. NSString* model_name = [params valueForKey:@"product_name"];
  866. NSString* product_id = [params valueForKey:@"product_id"];
  867. NSString* category = [params valueForKey:@"category"];
  868. if(category==nil)
  869. category = [self model_default_category:product_id model_name:model_name];
  870. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  871. sqlite3 *db = [iSalesDB get_db];
  872. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  873. NSString *sqlQuery = nil;
  874. if(product_id==nil)
  875. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from product where name='%@';",model_name]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where name='%@';
  876. else
  877. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,item_id from product where product_id=%@;",product_id]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where product_id=%@;
  878. sqlite3_stmt * statement;
  879. [ret setValue:@"2" forKey:@"result"];
  880. [ret setValue:@"3" forKey:@"detail_section_count"];
  881. // int count=0;
  882. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  883. {
  884. if (sqlite3_step(statement) == SQLITE_ROW)
  885. {
  886. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  887. char *name = (char*)sqlite3_column_text(statement, 0);
  888. if(name==nil)
  889. name="";
  890. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  891. char *description = (char*)sqlite3_column_text(statement, 1);
  892. if(description==nil)
  893. description="";
  894. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  895. int product_id = sqlite3_column_int(statement, 2);
  896. char *color = (char*)sqlite3_column_text(statement, 3);
  897. if(color==nil)
  898. color="";
  899. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  900. //
  901. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  902. // if(legcolor==nil)
  903. // legcolor="";
  904. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  905. //
  906. //
  907. int availability = sqlite3_column_int(statement, 5);
  908. //
  909. // int incoming_stock = sqlite3_column_int(statement, 6);
  910. char *demension = (char*)sqlite3_column_text(statement, 7);
  911. if(demension==nil)
  912. demension="";
  913. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  914. // ,,,,,,,,,
  915. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  916. if(seat_height==nil)
  917. seat_height="";
  918. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  919. char *material = (char*)sqlite3_column_text(statement, 9);
  920. if(material==nil)
  921. material="";
  922. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  923. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  924. if(box_dim==nil)
  925. box_dim="";
  926. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  927. char *volume = (char*)sqlite3_column_text(statement, 11);
  928. if(volume==nil)
  929. volume="";
  930. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  931. double weight = sqlite3_column_double(statement, 12);
  932. char *model_set = (char*)sqlite3_column_text(statement, 13);
  933. if(model_set==nil)
  934. model_set="";
  935. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  936. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  937. if(load_ability==nil)
  938. load_ability="";
  939. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  940. char *default_category = (char*)sqlite3_column_text(statement, 15);
  941. if(default_category==nil)
  942. default_category="";
  943. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  944. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  945. if(fabric_content==nil)
  946. fabric_content="";
  947. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  948. char *assembling = (char*)sqlite3_column_text(statement, 17);
  949. if(assembling==nil)
  950. assembling="";
  951. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  952. char *made_in = (char*)sqlite3_column_text(statement, 18);
  953. if(made_in==nil)
  954. made_in="";
  955. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  956. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  957. if(special_remarks==nil)
  958. special_remarks="";
  959. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  960. int stockUcom = sqlite3_column_double(statement, 20);
  961. char *product_group = (char*)sqlite3_column_text(statement, 21);
  962. if(product_group==nil)
  963. product_group="";
  964. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  965. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  966. // if(fashion_selector==nil)
  967. // fashion_selector="";
  968. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  969. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  970. if(selector_field==nil)
  971. selector_field="";
  972. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  973. char *property_field = (char*)sqlite3_column_text(statement, 23);
  974. if(property_field==nil)
  975. property_field="";
  976. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  977. char *packaging = (char*)sqlite3_column_text(statement, 24);
  978. if(packaging==nil)
  979. packaging="";
  980. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  981. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  982. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  983. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  984. [img_section setValue:model_s_img forKey:@"model_s_img"];
  985. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  986. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  987. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  988. NSString* Availability=nil;
  989. if(availability>0)
  990. Availability=[NSString stringWithFormat:@"%d",availability];
  991. else
  992. Availability = @"In Production";
  993. [img_section setValue:Availability forKey:@"Availability"];
  994. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  995. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  996. int item_id = sqlite3_column_int(statement, 25);
  997. NSString* Price=nil;
  998. if(appDelegate.bLogin==false)
  999. Price=@"Must Sign in.";
  1000. else
  1001. {
  1002. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1003. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1004. if(price==nil)
  1005. Price=@"No Price.";
  1006. else
  1007. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1008. }
  1009. [img_section setValue:Price forKey:@"price"];
  1010. [img_section setValue:nsname forKey:@"model_name"];
  1011. [img_section setValue:nsdescription forKey:@"model_descrition"];
  1012. [ret setObject:img_section forKey:@"img_section"];
  1013. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  1014. int detail0_item_count=0;
  1015. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1016. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1017. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1018. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1019. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1020. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1021. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1022. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1023. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1024. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1025. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1026. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1027. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1028. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  1029. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  1030. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  1031. {
  1032. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  1033. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1034. }
  1035. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  1036. [detail0_section setValue:@"kv" forKey:@"type"];
  1037. [detail0_section setValue:@"Product Information" forKey:@"title"];
  1038. [ret setObject:detail0_section forKey:@"detail_0"];
  1039. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1040. // [detail1_section setValue:@"detail" forKey:@"target"];
  1041. // [detail1_section setValue:@"popup" forKey:@"action"];
  1042. // [detail1_section setValue:@"content" forKey:@"type"];
  1043. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  1044. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1045. // [detail1_section setValue:@"true" forKey:@"single_row"];
  1046. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  1047. [ret setObject:detail1_section forKey:@"detail_1"];
  1048. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  1049. [detail2_section setValue:@"detail" forKey:@"target"];
  1050. [detail2_section setValue:@"popup" forKey:@"action"];
  1051. [detail2_section setValue:@"content" forKey:@"type"];
  1052. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  1053. [detail2_section setValue:@"true" forKey:@"single_row"];
  1054. [detail2_section setValue:@"local" forKey:@"data"];
  1055. [ret setObject:detail2_section forKey:@"detail_2"];
  1056. }
  1057. sqlite3_finalize(statement);
  1058. }
  1059. else
  1060. {
  1061. [ret setValue:@"8" forKey:@"result"];
  1062. }
  1063. NSLog(@"count:%d",count);
  1064. [iSalesDB close_db:db];
  1065. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1066. return ret;
  1067. }
  1068. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  1069. {
  1070. NSString* orderCode = [params valueForKey:@"orderCode"];
  1071. NSString* keyword = [params valueForKey:@"keyword"];
  1072. keyword=keyword.lowercaseString;
  1073. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  1074. int limit = [[params valueForKey:@"limit"] intValue];
  1075. int offset = [[params valueForKey:@"offset"] intValue];
  1076. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1077. sqlite3 *db = [iSalesDB get_db];
  1078. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  1079. NSString *sqlQuery = nil;
  1080. if(exactMatch )
  1081. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from product m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name limit %d offset %d ;",keyword,limit, offset]; // select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' order by m.name limit %d offset %d ;
  1082. else
  1083. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from product m left join wishlist w on m.product_id=w.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and m.is_active = 1 order by m.name limit %d offset %d ;",keyword,keyword,limit, offset];// select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or
  1084. DebugLog(@"offline_search sql:%@",sqlQuery);
  1085. sqlite3_stmt * statement;
  1086. [ret setValue:@"2" forKey:@"result"];
  1087. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  1088. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  1089. // int count=0;
  1090. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1091. {
  1092. int i=0;
  1093. while (sqlite3_step(statement) == SQLITE_ROW)
  1094. {
  1095. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1096. // char *name = (char*)sqlite3_column_text(statement, 1);
  1097. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1098. char *name = (char*)sqlite3_column_text(statement, 0);
  1099. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1100. char *description = (char*)sqlite3_column_text(statement, 1);
  1101. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1102. int product_id = sqlite3_column_int(statement, 2);
  1103. // char *url = (char*)sqlite3_column_text(statement, 3);
  1104. // if(url==nil)
  1105. // url="";
  1106. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1107. int wid = sqlite3_column_int(statement, 3);
  1108. int closeout = sqlite3_column_int(statement, 4);
  1109. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  1110. if(wid==0)
  1111. [item setValue:@"false" forKey:@"wish_exists"];
  1112. else
  1113. [item setValue:@"true" forKey:@"wish_exists"];
  1114. if(closeout==0)
  1115. [item setValue:@"false" forKey:@"is_closeout"];
  1116. else
  1117. [item setValue:@"true" forKey:@"is_closeout"];
  1118. [item addEntriesFromDictionary:imgjson];
  1119. // [item setValue:nsurl forKey:@"img"];
  1120. [item setValue:nsname forKey:@"fash_name"];
  1121. [item setValue:nsdescription forKey:@"description"];
  1122. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1123. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1124. i++;
  1125. }
  1126. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1127. [ret setObject:items forKey:@"items"];
  1128. sqlite3_finalize(statement);
  1129. }
  1130. NSLog(@"count:%d",count);
  1131. [iSalesDB close_db:db];
  1132. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1133. return ret;
  1134. }
  1135. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  1136. {
  1137. //
  1138. // NSString* user = [params valueForKey:@"user"];
  1139. //
  1140. // NSString* password = [params valueForKey:@"password"];
  1141. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1142. ret[@"result"]=[NSNumber numberWithInt:2 ];
  1143. UIApplication * app = [UIApplication sharedApplication];
  1144. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1145. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  1146. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  1147. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  1148. [appDelegate update_count_mark];
  1149. appDelegate.can_show_price =false;
  1150. appDelegate.can_see_price =false;
  1151. appDelegate.can_create_portfolio =false;
  1152. appDelegate.can_create_order =false;
  1153. appDelegate.can_cancel_order =false;
  1154. appDelegate.can_set_cart_price =false;
  1155. appDelegate.can_delete_order =false;
  1156. appDelegate.can_submit_order =false;
  1157. appDelegate.can_set_tearsheet_price =false;
  1158. appDelegate.can_update_contact_info = false;
  1159. appDelegate.save_order_logout = false;
  1160. appDelegate.submit_order_logout = false;
  1161. appDelegate.alert_sold_in_quantities = false;
  1162. appDelegate.ipad_perm =nil ;
  1163. appDelegate.user_type = USER_ROLE_UNKNOWN;
  1164. appDelegate.OrderFilter= nil;
  1165. [appDelegate SetSo:nil];
  1166. [appDelegate set_main_button_panel];
  1167. // sqlite3 *db = [iSalesDB get_db];
  1168. //
  1169. //
  1170. //
  1171. //
  1172. //
  1173. // NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  1174. //
  1175. //
  1176. //
  1177. //
  1178. // DebugLog(@"offline_login sql:%@",sqlQuery);
  1179. // sqlite3_stmt * statement;
  1180. //
  1181. //
  1182. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1183. //
  1184. //
  1185. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1186. // {
  1187. //
  1188. //
  1189. // if (sqlite3_step(statement) == SQLITE_ROW)
  1190. // {
  1191. //
  1192. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1193. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1194. //
  1195. //
  1196. //
  1197. // int can_show_price = sqlite3_column_int(statement, 0);
  1198. // int can_see_price = sqlite3_column_int(statement, 1);
  1199. //
  1200. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1201. // if(contact_id==nil)
  1202. // contact_id="";
  1203. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1204. //
  1205. // int user_type = sqlite3_column_int(statement, 3);
  1206. //
  1207. // int can_cancel_order = sqlite3_column_int(statement, 4);
  1208. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  1209. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  1210. // int can_delete_order = sqlite3_column_int(statement, 7);
  1211. // int can_submit_order = sqlite3_column_int(statement, 8);
  1212. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1213. // int can_create_order = sqlite3_column_int(statement, 10);
  1214. //
  1215. //
  1216. // char *mode = (char*)sqlite3_column_text(statement, 11);
  1217. // if(mode==nil)
  1218. // mode="";
  1219. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1220. //
  1221. //
  1222. // char *username = (char*)sqlite3_column_text(statement, 12);
  1223. // if(username==nil)
  1224. // username="";
  1225. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1226. //
  1227. //
  1228. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1229. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1230. // [header setValue:nscontact_id forKey:@"contact_id"];
  1231. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1232. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1233. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1234. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1235. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1236. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1237. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1238. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1239. //
  1240. // [header setValue:nsusername forKey:@"username"];
  1241. //
  1242. //
  1243. // [ret setObject:header forKey:@"header"];
  1244. // [ret setValue:nsmode forKey:@"mode"];
  1245. //
  1246. //
  1247. // }
  1248. //
  1249. //
  1250. //
  1251. // sqlite3_finalize(statement);
  1252. // }
  1253. //
  1254. //
  1255. //
  1256. // [iSalesDB close_db:db];
  1257. //
  1258. //
  1259. //
  1260. //
  1261. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1262. return ret;
  1263. }
  1264. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  1265. {
  1266. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1267. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  1268. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  1269. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  1270. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  1271. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  1272. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  1273. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  1274. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  1275. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  1276. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  1277. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  1278. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  1279. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  1280. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  1281. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  1282. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  1283. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  1284. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  1285. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  1286. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  1287. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  1288. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1289. [arr_name addObject:customer_first_name];
  1290. [arr_name addObject:customer_last_name];
  1291. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1292. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact) values('OFFLINE'||hex(randomblob(16)),0,'%@','%@','%@','%@','%@','%@','%@','%@')",customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name];
  1293. sqlite3 *db = [iSalesDB get_db];
  1294. int result =[iSalesDB execSql:sql_neworder db:db];
  1295. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  1296. //insert into offline_order(so_id,status,contact_id,sales_rep,create_by,customer_contact) select 'OFFLINE'||hex(randomblob(16)),-1,contact_id,sales_rep,company_name from offline_contact where contact_id='99FURO0002'
  1297. //soId
  1298. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  1299. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  1300. sqlite3_stmt * statement;
  1301. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1302. {
  1303. if (sqlite3_step(statement) == SQLITE_ROW)
  1304. {
  1305. // char *name = (char*)sqlite3_column_text(statement, 1);
  1306. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1307. //ret = sqlite3_column_int(statement, 0);
  1308. char *soId = (char*)sqlite3_column_text(statement, 0);
  1309. if(soId==nil)
  1310. soId="";
  1311. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  1312. [ret setValue:nssoId forKey:@"soId"];
  1313. [ret setValue:nssoId forKey:@"orderCode"];
  1314. }
  1315. sqlite3_finalize(statement);
  1316. }
  1317. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  1318. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1319. [iSalesDB close_db:db];
  1320. return [RAUtils dict2data:ret];
  1321. }
  1322. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  1323. {
  1324. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1325. NSString* orderCode = [params valueForKey:@"orderCode"];
  1326. sqlite3 *db = [iSalesDB get_db];
  1327. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1328. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1329. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1330. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1331. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1332. [iSalesDB close_db:db];
  1333. return [RAUtils dict2data:ret];
  1334. }
  1335. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  1336. {
  1337. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1338. sqlite3 *db = [iSalesDB get_db];
  1339. int count =0;
  1340. if(params[@"count"]!=nil)
  1341. {
  1342. count = [params[@"count"] intValue];
  1343. }
  1344. NSString* product_id=params[@"product_id"];
  1345. NSString* orderCode=params[@"orderCode"];
  1346. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  1347. for(int i=0;i<arr_id.count;i++)
  1348. {
  1349. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  1350. int item_qty= count;
  1351. if(item_qty==0)
  1352. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  1353. NSString* sql=nil;
  1354. if(_id<0)
  1355. {
  1356. // NSString* bundle_serialno= [self model_bundle:[arr_id[i] intValue] db:db];
  1357. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  1358. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id) values(%@,'%@',%d,%d)",arr_id[i],orderCode,item_qty,item_id];
  1359. }
  1360. else
  1361. {
  1362. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+%d where _id=%d",item_qty,_id];
  1363. }
  1364. [iSalesDB execSql:sql db:db];
  1365. }
  1366. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1367. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1368. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1369. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1370. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1371. [iSalesDB close_db:db];
  1372. return [RAUtils dict2data:ret];
  1373. }
  1374. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  1375. {
  1376. return [RAUtils dict2data:[self offline_notimpl]];
  1377. }
  1378. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  1379. {
  1380. UIApplication * app = [UIApplication sharedApplication];
  1381. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1382. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1383. NSString *sqlQuery = [NSString stringWithFormat:@"select b.item_id,b.qty,c.name,c.description,b.unit_price,b.use_unitprice from (select item_id,qty,decrypt(unit_price) as unit_price,use_unitprice from bundle where product_item_id=%d) b left join catalog c on b.item_id=c.item_id;",item_id];
  1384. sqlite3_stmt * statement;
  1385. int count=0;
  1386. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1387. {
  1388. while (sqlite3_step(statement) == SQLITE_ROW)
  1389. {
  1390. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  1391. int bitem_id = sqlite3_column_int(statement, 0);
  1392. int bitem_qty = sqlite3_column_int(statement, 1);
  1393. char *name = (char*)sqlite3_column_text(statement, 2);
  1394. if(name==nil)
  1395. name="";
  1396. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  1397. char *description = (char*)sqlite3_column_text(statement, 3);
  1398. if(description==nil)
  1399. description="";
  1400. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  1401. double unit_price = sqlite3_column_double(statement, 4);
  1402. int use_unitprice = sqlite3_column_int(statement, 5);
  1403. if(use_unitprice!=1)
  1404. {
  1405. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  1406. }
  1407. itemjson[@"model"]=nsname;
  1408. itemjson[@"description"]=nsdescription;
  1409. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  1410. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  1411. if(compute)
  1412. {
  1413. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  1414. }
  1415. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1416. count++;
  1417. }
  1418. sqlite3_finalize(statement);
  1419. }
  1420. ret[@"count"]=@(count);
  1421. if(count==0)
  1422. return nil;
  1423. else
  1424. return ret;
  1425. }
  1426. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  1427. {
  1428. //compute: add part to subtotal;
  1429. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1430. double cuft=0;
  1431. double weight=0;
  1432. int carton=0;
  1433. NSString *sqlQuery = [NSString stringWithFormat:@"select ulength ,uwidth ,uheight ,uweight ,mlength ,mwidth ,mheight ,mweigth ,ilength ,iwidth ,iheight , iweight ,pcs ,mpack ,ipack,ucbf,icbf,mcbf from catalog where item_id=%d;",item_id];
  1434. sqlite3_stmt * statement;
  1435. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1436. {
  1437. if (sqlite3_step(statement) == SQLITE_ROW)
  1438. {
  1439. double ulength = sqlite3_column_double(statement, 0);
  1440. double uwidth = sqlite3_column_double(statement, 1);
  1441. double uheight = sqlite3_column_double(statement, 2);
  1442. double uweight = sqlite3_column_double(statement, 3);
  1443. double mlength = sqlite3_column_double(statement, 4);
  1444. double mwidth = sqlite3_column_double(statement, 5);
  1445. double mheight = sqlite3_column_double(statement, 6);
  1446. double mweight = sqlite3_column_double(statement, 7);
  1447. double ilength = sqlite3_column_double(statement, 8);
  1448. double iwidth = sqlite3_column_double(statement, 9);
  1449. double iheight = sqlite3_column_double(statement, 10);
  1450. double iweight = sqlite3_column_double(statement, 11);
  1451. int pcs = sqlite3_column_int(statement,12);
  1452. int mpack = sqlite3_column_int(statement, 13);
  1453. int ipack = sqlite3_column_int(statement, 14);
  1454. double ucbf = sqlite3_column_double(statement, 15);
  1455. double icbf = sqlite3_column_double(statement, 16);
  1456. double mcbf = sqlite3_column_double(statement, 17);
  1457. if(ipack==0)
  1458. {
  1459. carton= count/mpack ;
  1460. weight = mweight*carton;
  1461. cuft= carton*(mlength*mwidth*mheight);
  1462. int remain=count%mpack;
  1463. if(remain==0)
  1464. {
  1465. //do nothing;
  1466. }
  1467. else
  1468. {
  1469. carton++;
  1470. weight += uweight*remain;
  1471. cuft += (ulength*uwidth*uheight)*remain;
  1472. }
  1473. }
  1474. else
  1475. {
  1476. carton = count/(mpack*ipack);
  1477. weight = mweight*carton;
  1478. cuft= carton*(mlength*mwidth*mheight);
  1479. int remain=count%(mpack*ipack);
  1480. if(remain==0)
  1481. {
  1482. // do nothing;
  1483. }
  1484. else
  1485. {
  1486. carton++;
  1487. int icarton =remain/ipack;
  1488. int iremain=remain%ipack;
  1489. weight += iweight*icarton;
  1490. cuft += (ilength*iwidth*iheight)*icarton;
  1491. if(iremain==0)
  1492. {
  1493. //do nothing;
  1494. }
  1495. else
  1496. {
  1497. weight += uweight*iremain;
  1498. cuft += (ulength*uwidth*uheight)*iremain;
  1499. }
  1500. }
  1501. }
  1502. #ifdef BUILD_NPD
  1503. cuft=ucbf*count;
  1504. weight= uweight*count;
  1505. #endif
  1506. }
  1507. sqlite3_finalize(statement);
  1508. }
  1509. if(compute)
  1510. {
  1511. NSArray * arr_count=nil;
  1512. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  1513. for(int i=0;i<arr_bundle.count;i++)
  1514. {
  1515. [arr_bundle[i] intValue];
  1516. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  1517. cuft+=[bundlejson[@"cuft"] doubleValue];
  1518. weight+=[bundlejson[@"weight"] doubleValue];
  1519. carton+=[bundlejson[@"carton"] intValue];
  1520. }
  1521. }
  1522. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  1523. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  1524. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  1525. return ret;
  1526. }
  1527. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  1528. {
  1529. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1530. sqlite3 *db = [iSalesDB get_db];
  1531. UIApplication * app = [UIApplication sharedApplication];
  1532. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1533. NSString* orderCode=params[@"orderCode"];
  1534. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",orderCode ];
  1535. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  1536. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1537. DebugLog(@"offline_login sql:%@",sqlQuery);
  1538. sqlite3_stmt * statement;
  1539. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1540. int count=0;
  1541. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1542. if ( dbresult== SQLITE_OK)
  1543. {
  1544. while (sqlite3_step(statement) == SQLITE_ROW)
  1545. {
  1546. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1547. int product_id = sqlite3_column_int(statement, 0);
  1548. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1549. int item_id = sqlite3_column_int(statement, 7);
  1550. NSString* Price=nil;
  1551. if(str_price==nil)
  1552. {
  1553. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1554. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1555. if(price==nil)
  1556. Price=@"No Price.";
  1557. else
  1558. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1559. }
  1560. else
  1561. {
  1562. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1563. }
  1564. double discount = sqlite3_column_double(statement, 2);
  1565. int item_count = sqlite3_column_int(statement, 3);
  1566. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1567. NSString *nsline_note=nil;
  1568. if(line_note!=nil)
  1569. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1570. char *name = (char*)sqlite3_column_text(statement, 5);
  1571. NSString *nsname=nil;
  1572. if(name!=nil)
  1573. nsname= [[NSString alloc]initWithUTF8String:name];
  1574. char *description = (char*)sqlite3_column_text(statement, 6);
  1575. NSString *nsdescription=nil;
  1576. if(description!=nil)
  1577. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1578. int stockUom = sqlite3_column_int(statement, 8);
  1579. int _id = sqlite3_column_int(statement, 9);
  1580. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1581. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1582. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1583. int carton=[bsubtotaljson[@"carton"] intValue];
  1584. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1585. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1586. itemjson[@"model"]=nsname;
  1587. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1588. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1589. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1590. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1591. itemjson[@"check"]=@"true";
  1592. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1593. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1594. itemjson[@"unit_price"]=Price;
  1595. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1596. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1597. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1598. itemjson[@"note"]=nsline_note;
  1599. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  1600. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1601. count++;
  1602. }
  1603. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1604. sqlite3_finalize(statement);
  1605. }
  1606. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1607. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1608. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1609. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1610. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1611. ret[@"mode"]=@"Regular Mode";
  1612. [iSalesDB close_db:db];
  1613. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  1614. DebugLog(@"general notes :%@",general_note);
  1615. ret[@"general_note"]= general_note;
  1616. return [RAUtils dict2data:ret];
  1617. }
  1618. +(NSData*) offline_login :(NSMutableDictionary *) params
  1619. {
  1620. NSString* user = [params valueForKey:@"user"];
  1621. NSString* password = [params valueForKey:@"password"];
  1622. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1623. sqlite3 *db = [iSalesDB get_db];
  1624. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  1625. DebugLog(@"offline_login sql:%@",sqlQuery);
  1626. sqlite3_stmt * statement;
  1627. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1628. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1629. {
  1630. if (sqlite3_step(statement) == SQLITE_ROW)
  1631. {
  1632. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1633. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1634. int can_show_price = sqlite3_column_int(statement, 0);
  1635. int can_see_price = sqlite3_column_int(statement, 1);
  1636. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1637. if(contact_id==nil)
  1638. contact_id="";
  1639. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1640. int user_type = sqlite3_column_int(statement, 3);
  1641. int can_cancel_order = sqlite3_column_int(statement, 4);
  1642. int can_set_cart_price = sqlite3_column_int(statement, 5);
  1643. int can_create_portfolio = sqlite3_column_int(statement, 6);
  1644. int can_delete_order = sqlite3_column_int(statement, 7);
  1645. int can_submit_order = sqlite3_column_int(statement, 8);
  1646. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1647. int can_create_order = sqlite3_column_int(statement, 10);
  1648. char *mode = (char*)sqlite3_column_text(statement, 11);
  1649. if(mode==nil)
  1650. mode="";
  1651. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1652. char *username = (char*)sqlite3_column_text(statement, 12);
  1653. if(username==nil)
  1654. username="";
  1655. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1656. int can_update_contact_info = sqlite3_column_int(statement, 13);
  1657. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1658. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1659. [header setValue:nscontact_id forKey:@"contact_id"];
  1660. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1661. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1662. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1663. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1664. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1665. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1666. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1667. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1668. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  1669. [header setValue:nsusername forKey:@"username"];
  1670. NSError* error=nil;
  1671. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  1672. [header setValue:statusFilter forKey:@"statusFilter"];
  1673. [ret setObject:header forKey:@"header"];
  1674. [ret setValue:nsmode forKey:@"mode"];
  1675. }
  1676. sqlite3_finalize(statement);
  1677. }
  1678. [iSalesDB close_db:db];
  1679. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1680. return [RAUtils dict2data:ret];
  1681. }
  1682. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  1683. {
  1684. NSString* contactId = [params valueForKey:@"contactId"];
  1685. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1686. sqlite3 *db = [iSalesDB get_db];
  1687. NSString *sqlQuery = nil;
  1688. {
  1689. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email,img_0,img_1,img_2,price_type,notes,sales_rep from offline_contact where contact_id='%@'",contactId];
  1690. }
  1691. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  1692. sqlite3_stmt * statement;
  1693. [ret setValue:@"2" forKey:@"result"];
  1694. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1695. {
  1696. //int i = 0;
  1697. if (sqlite3_step(statement) == SQLITE_ROW)
  1698. {
  1699. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1700. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1701. // int editable = sqlite3_column_int(statement, 0);
  1702. char *company_name = (char*)sqlite3_column_text(statement, 1);
  1703. NSString *nscompany_name =nil;
  1704. if(company_name==nil)
  1705. nscompany_name=@"";
  1706. else
  1707. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  1708. char *country = (char*)sqlite3_column_text(statement, 2);
  1709. if(country==nil)
  1710. country="";
  1711. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  1712. // char *addr = (char*)sqlite3_column_text(statement, 3);
  1713. // if(addr==nil)
  1714. // addr="";
  1715. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  1716. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  1717. if(zipcode==nil)
  1718. zipcode="";
  1719. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  1720. char *state = (char*)sqlite3_column_text(statement, 5);
  1721. if(state==nil)
  1722. state="";
  1723. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  1724. char *city = (char*)sqlite3_column_text(statement, 6);
  1725. if(city==nil)
  1726. city="";
  1727. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  1728. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  1729. // NSString *nscontact_name = nil;
  1730. // if(contact_name==nil)
  1731. // nscontact_name=@"";
  1732. // else
  1733. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  1734. char *phone = (char*)sqlite3_column_text(statement, 8);
  1735. NSString *nsphone = nil;
  1736. if(phone==nil)
  1737. nsphone=@"";
  1738. else
  1739. nsphone= [[NSString alloc]initWithUTF8String:phone];
  1740. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  1741. if(contact_id==nil)
  1742. contact_id="";
  1743. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1744. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1745. if(addr_1==nil)
  1746. addr_1="";
  1747. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1748. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1749. if(addr_2==nil)
  1750. addr_2="";
  1751. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1752. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1753. if(addr_3==nil)
  1754. addr_3="";
  1755. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  1756. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  1757. if(addr_4==nil)
  1758. addr_4="";
  1759. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  1760. char *first_name = (char*)sqlite3_column_text(statement, 14);
  1761. if(first_name==nil)
  1762. first_name="";
  1763. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  1764. char *last_name = (char*)sqlite3_column_text(statement, 15);
  1765. if(last_name==nil)
  1766. last_name="";
  1767. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  1768. char *fax = (char*)sqlite3_column_text(statement, 16);
  1769. NSString *nsfax = nil;
  1770. if(fax==nil)
  1771. nsfax=@"";
  1772. else
  1773. nsfax= [[NSString alloc]initWithUTF8String:fax];
  1774. char *email = (char*)sqlite3_column_text(statement, 17);
  1775. NSString *nsemail = nil;
  1776. if(email==nil)
  1777. nsemail=@"";
  1778. else
  1779. nsemail= [[NSString alloc]initWithUTF8String:email];
  1780. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  1781. NSString *nsimg_0 = nil;
  1782. if(img_0==nil)
  1783. nsimg_0=@"";
  1784. else
  1785. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1786. [self copy_bcardImg:nsimg_0];
  1787. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  1788. NSString *nsimg_1 = nil;
  1789. if(img_1==nil)
  1790. nsimg_1=@"";
  1791. else
  1792. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1793. [self copy_bcardImg:nsimg_1];
  1794. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  1795. NSString *nsimg_2 = nil;
  1796. if(img_2==nil)
  1797. nsimg_2=@"";
  1798. else
  1799. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1800. [self copy_bcardImg:nsimg_2];
  1801. char *price_type = (char*)sqlite3_column_text(statement, 21);
  1802. NSString *nsprice_type = nil;
  1803. if(price_type==nil)
  1804. nsprice_type=@"";
  1805. else
  1806. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  1807. char *notes = (char*)sqlite3_column_text(statement, 22);
  1808. NSString *nsnotes = nil;
  1809. if(notes==nil)
  1810. nsnotes=@"";
  1811. else
  1812. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  1813. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  1814. NSString *nssalesrep = nil;
  1815. if(salesrep==nil)
  1816. nssalesrep=@"";
  1817. else
  1818. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  1819. {
  1820. // decrypt
  1821. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  1822. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  1823. nsphone=[AESCrypt fastdecrypt:nsphone];
  1824. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1825. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  1826. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1827. }
  1828. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1829. [arr_name addObject:nsfirst_name];
  1830. [arr_name addObject:nslast_name];
  1831. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1832. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  1833. [arr_ext addObject:nsaddr_1];
  1834. [arr_ext addObject:nsaddr_2];
  1835. [arr_ext addObject:nsaddr_3];
  1836. [arr_ext addObject:nsaddr_4];
  1837. [arr_ext addObject:@"\r\n"];
  1838. [arr_ext addObject:nscity];
  1839. [arr_ext addObject:nsstate];
  1840. [arr_ext addObject:nszipcode];
  1841. [arr_ext addObject:nscountry];
  1842. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  1843. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  1844. [item setValue:nsimg_2 forKey:@"business_card_2"];
  1845. [item setValue:nsimg_0 forKey:@"business_card_0"];
  1846. [item setValue:nscountry forKey:@"customer_country"];
  1847. [item setValue:nsphone forKey:@"customer_phone"];
  1848. [item setValue:nsimg_1 forKey:@"business_card_1"];
  1849. [item setValue:nscompany_name forKey:@"customer_name"];
  1850. [item setValue:nsprice_type forKey:@"customer_price_type"];
  1851. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  1852. [item setValue:nsext forKey:@"customer_contact_ext"];
  1853. [item setValue:nszipcode forKey:@"customer_zipcode"];
  1854. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  1855. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  1856. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  1857. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  1858. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  1859. [item setValue:nslast_name forKey:@"customer_last_name"];
  1860. [item setValue:nscity forKey:@"customer_city"];
  1861. [item setValue:nsstate forKey:@"customer_state"];
  1862. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  1863. [item setValue:contactId forKey:@"customer_cid"];
  1864. [item setValue:nscontact_name forKey:@"customer_contact"];
  1865. [item setValue:nsfax forKey:@"customer_fax"];
  1866. [item setValue:nsemail forKey:@"customer_email"];
  1867. [ret setObject:item forKey:@"customerInfo"];
  1868. // i++;
  1869. }
  1870. UIApplication * app = [UIApplication sharedApplication];
  1871. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1872. [ret setValue:appDelegate.mode forKey:@"mode"];
  1873. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  1874. sqlite3_finalize(statement);
  1875. }
  1876. [iSalesDB close_db:db];
  1877. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1878. return ret;
  1879. }
  1880. + (bool) copy_bcardImg:(NSString*) filename
  1881. {
  1882. if(filename.length==0)
  1883. return false;
  1884. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1885. bool ret=false;
  1886. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1887. NSString *cachefolder = [paths objectAtIndex:0];
  1888. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1889. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1890. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  1891. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1892. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  1893. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  1894. //
  1895. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  1896. NSFileManager* fileManager = [NSFileManager defaultManager];
  1897. BOOL bdir=NO;
  1898. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1899. {
  1900. NSError *error = nil;
  1901. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1902. {
  1903. ret=false;
  1904. }
  1905. else
  1906. {
  1907. ret=true;
  1908. }
  1909. // NSError *error = nil;
  1910. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  1911. //
  1912. // if(!bsuccess)
  1913. // {
  1914. // DebugLog(@"Create offline_createimg folder failed");
  1915. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1916. // return [RAUtils dict2data:ret];
  1917. // }
  1918. // if(bsuccess)
  1919. // {
  1920. // sqlite3 *db = [self get_db];
  1921. //
  1922. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1923. // [iSalesDB close_db:db];
  1924. // }
  1925. }
  1926. return ret;
  1927. //
  1928. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  1929. // if(bsuccess)
  1930. // {
  1931. // NSError *error = nil;
  1932. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  1933. // {
  1934. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1935. // }
  1936. // else
  1937. // {
  1938. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  1939. //
  1940. // ret[@"img_url_aname"]=filename;
  1941. // ret[@"img_url"]=savedImagePath;
  1942. // }
  1943. // }
  1944. }
  1945. +(NSData *) offline_saveBusinesscard:(NSData *) image
  1946. {
  1947. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1948. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1949. NSString *cachefolder = [paths objectAtIndex:0];
  1950. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1951. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  1952. NSFileManager* fileManager = [NSFileManager defaultManager];
  1953. BOOL bdir=YES;
  1954. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  1955. {
  1956. NSError *error = nil;
  1957. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  1958. if(!bsuccess)
  1959. {
  1960. DebugLog(@"Create offline_createimg folder failed");
  1961. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1962. return [RAUtils dict2data:ret];
  1963. }
  1964. // if(bsuccess)
  1965. // {
  1966. // sqlite3 *db = [self get_db];
  1967. //
  1968. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1969. // [iSalesDB close_db:db];
  1970. // }
  1971. }
  1972. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  1973. //JEPG格式
  1974. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  1975. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  1976. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  1977. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  1978. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  1979. if(bsuccess)
  1980. {
  1981. NSError *error = nil;
  1982. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  1983. {
  1984. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1985. }
  1986. else
  1987. {
  1988. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  1989. ret[@"img_url_aname"]=filename;
  1990. ret[@"img_url"]=filename;
  1991. }
  1992. }
  1993. else
  1994. {
  1995. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1996. }
  1997. return [RAUtils dict2data:ret];
  1998. }
  1999. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  2000. {
  2001. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  2002. category = [category substringToIndex:3];
  2003. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2004. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  2005. params[@"category"]= category;
  2006. ret[@"params"]= params;
  2007. [ret setValue:@"detail" forKey:@"target"];
  2008. [ret setValue:@"popup" forKey:@"action"];
  2009. [ret setValue:@"content" forKey:@"type"];
  2010. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  2011. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2012. [ret setValue:@"true" forKey:@"single_row"];
  2013. [ret setValue:@"true" forKey:@"partial_refresh"];
  2014. // sqlite3 *db = [iSalesDB get_db];
  2015. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2016. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.product_id,i.url,random() as aa from product m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;",category,OFFLINE_IMG_TYPE_CATEGORY]; // select m.name,m.product_id,i.url,random() as aa from model m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;
  2017. sqlite3_stmt * statement;
  2018. int count = 0;
  2019. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  2020. // int count=0;
  2021. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2022. {
  2023. int i=0;
  2024. while (sqlite3_step(statement) == SQLITE_ROW)
  2025. {
  2026. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2027. // char *name = (char*)sqlite3_column_text(statement, 1);
  2028. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2029. char *name = (char*)sqlite3_column_text(statement, 0);
  2030. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2031. int product_id = sqlite3_column_int(statement, 1);
  2032. char *url = (char*)sqlite3_column_text(statement, 2);
  2033. if(url==nil)
  2034. url="";
  2035. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2036. [item setValue:nsurl forKey:@"picture_path"];
  2037. [item setValue:nsname forKey:@"fash_name"];
  2038. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2039. [item setValue:category forKey:@"category"];
  2040. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2041. i++;
  2042. }
  2043. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2044. sqlite3_finalize(statement);
  2045. }
  2046. NSLog(@"count:%d",count);
  2047. // [iSalesDB close_db:db];
  2048. return ret;
  2049. }
  2050. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  2051. {
  2052. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2053. [ret setValue:key forKey:@"key"];
  2054. [ret setValue:value forKey:@"val"];
  2055. return ret;
  2056. }
  2057. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  2058. {
  2059. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2060. [ret setValue:@"0" forKey:@"img_count"];
  2061. // sqlite3 *db = [iSalesDB get_db];
  2062. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from product where product_id=%d ;",field,product_id]; // select %@,property_display from model where product_id=%d ;
  2063. sqlite3_stmt * statement;
  2064. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2065. {
  2066. int i=0;
  2067. if (sqlite3_step(statement) == SQLITE_ROW)
  2068. {
  2069. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2070. // char *name = (char*)sqlite3_column_text(statement, 1);
  2071. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2072. char *value = (char*)sqlite3_column_text(statement, 0);
  2073. if(value==nil)
  2074. value="";
  2075. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2076. char *key = (char*)sqlite3_column_text(statement, 1);
  2077. if(key==nil)
  2078. key="";
  2079. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  2080. [item setValue:nsvalue forKey:@"val"];
  2081. [item setValue:nskey forKey:@"key"];
  2082. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2083. [ret setValue:@"1" forKey:@"count"];
  2084. i++;
  2085. }
  2086. sqlite3_finalize(statement);
  2087. }
  2088. // [iSalesDB close_db:db];
  2089. return ret;
  2090. }
  2091. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  2092. {
  2093. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2094. [ret setValue:@"0" forKey:@"count"];
  2095. // sqlite3 *db = [iSalesDB get_db];
  2096. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from product where product_group='%@' order by name;",field,product_group]; // select %@,selector_display,product_id,category from model where product_group='%@' order by name;
  2097. sqlite3_stmt * statement;
  2098. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2099. {
  2100. int i=0;
  2101. while (sqlite3_step(statement) == SQLITE_ROW)
  2102. {
  2103. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2104. // char *name = (char*)sqlite3_column_text(statement, 1);
  2105. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2106. char *value = (char*)sqlite3_column_text(statement, 0);
  2107. if(value==nil)
  2108. value="";
  2109. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2110. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  2111. if(selector_display==nil)
  2112. selector_display="";
  2113. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  2114. int product_id = sqlite3_column_int(statement, 2);
  2115. char *category = (char*)sqlite3_column_text(statement, 3);
  2116. if(category==nil)
  2117. category="";
  2118. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  2119. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  2120. [item setValue:nsvalue forKey:@"title"];
  2121. [item setValue:url forKey:@"pic_url"];
  2122. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  2123. [params setValue:@"2" forKey:@"count"];
  2124. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  2125. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  2126. [param0 setValue:@"product_id" forKey:@"name"];
  2127. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  2128. [param1 setValue:nscategory forKey:@"val"];
  2129. [param1 setValue:@"category" forKey:@"name"];
  2130. [params setObject:param0 forKey:@"param_0"];
  2131. [params setObject:param1 forKey:@"param_1"];
  2132. [item setObject:params forKey:@"params"];
  2133. [ret setValue:nsselector_display forKey:@"name"];
  2134. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2135. i++;
  2136. }
  2137. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2138. [ret setValue:@"switch" forKey:@"action"];
  2139. sqlite3_finalize(statement);
  2140. }
  2141. // [iSalesDB close_db:db];
  2142. return ret;
  2143. }
  2144. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  2145. {
  2146. // model 在 category search 显示的图片。
  2147. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  2148. // sqlite3 *db = [iSalesDB get_db];
  2149. NSString *sqlQuery = [NSString stringWithFormat:@"select url,type from model_image where product_id=%d and type<2 order by default_img desc , _id asc limit 2;",product_id];
  2150. sqlite3_stmt * statement;
  2151. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2152. {
  2153. while (sqlite3_step(statement) == SQLITE_ROW)
  2154. {
  2155. char *url = (char*)sqlite3_column_text(statement, 0);
  2156. if(url==nil)
  2157. url="";
  2158. int type = sqlite3_column_int(statement, 1);
  2159. if(type==0)
  2160. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  2161. else
  2162. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  2163. }
  2164. sqlite3_finalize(statement);
  2165. }
  2166. // [iSalesDB close_db:db];
  2167. return ret;
  2168. }
  2169. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  2170. {
  2171. int item_id=-1;
  2172. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  2173. sqlite3_stmt * statement;
  2174. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2175. {
  2176. if (sqlite3_step(statement) == SQLITE_ROW)
  2177. {
  2178. item_id = sqlite3_column_int(statement, 0);
  2179. }
  2180. sqlite3_finalize(statement);
  2181. }
  2182. return item_id;
  2183. }
  2184. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  2185. {
  2186. // NSString* ret = @"";
  2187. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  2188. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  2189. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  2190. sqlite3_stmt * statement;
  2191. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2192. {
  2193. while (sqlite3_step(statement) == SQLITE_ROW)
  2194. {
  2195. int bitem_id = sqlite3_column_int(statement, 0);
  2196. int bitem_qty = sqlite3_column_int(statement, 1);
  2197. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  2198. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  2199. }
  2200. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  2201. sqlite3_finalize(statement);
  2202. }
  2203. // if(ret==nil)
  2204. // ret=@"";
  2205. *count=arr_count;
  2206. return arr_bundle;
  2207. }
  2208. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  2209. {
  2210. // get default sold qty, return -1 if model not found;
  2211. int ret = -1;
  2212. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  2213. sqlite3_stmt * statement;
  2214. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2215. {
  2216. if (sqlite3_step(statement) == SQLITE_ROW)
  2217. {
  2218. ret = sqlite3_column_int(statement, 0);
  2219. }
  2220. sqlite3_finalize(statement);
  2221. }
  2222. return ret;
  2223. }
  2224. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  2225. {
  2226. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2227. [ret setValue:@"0" forKey:@"img_count"];
  2228. // sqlite3 *db = [iSalesDB get_db];
  2229. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  2230. sqlite3_stmt * statement;
  2231. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2232. {
  2233. int i=0;
  2234. while (sqlite3_step(statement) == SQLITE_ROW)
  2235. {
  2236. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2237. // char *name = (char*)sqlite3_column_text(statement, 1);
  2238. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2239. char *url = (char*)sqlite3_column_text(statement, 0);
  2240. if(url==nil)
  2241. url="";
  2242. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2243. [item setValue:nsurl forKey:@"s"];
  2244. [item setValue:nsurl forKey:@"l"];
  2245. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  2246. i++;
  2247. }
  2248. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  2249. sqlite3_finalize(statement);
  2250. }
  2251. // [iSalesDB close_db:db];
  2252. return ret;
  2253. }
  2254. #pragma mark - Jack
  2255. #warning 做SQL操作时转义!!
  2256. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  2257. // "val_227" : {
  2258. // "check" : 1,
  2259. // "value" : "US United States",
  2260. // "value_id" : "228"
  2261. // },
  2262. if (!countryCode) {
  2263. countryCode = @"US";
  2264. }
  2265. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  2266. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  2267. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  2268. int code_id = sqlite3_column_int(stmt, 3); // id
  2269. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2270. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  2271. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  2272. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2273. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  2274. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2275. }
  2276. long n = *count;
  2277. *count = n + 1;
  2278. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  2279. [container setValue:countryDic forKey:key];
  2280. }] mutableCopy];
  2281. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2282. return ret;
  2283. }
  2284. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  2285. countryCode = [self translateSingleQuote:countryCode];
  2286. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  2287. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2288. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  2289. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  2290. if (name == NULL) {
  2291. name = "";
  2292. }
  2293. if (code == NULL) {
  2294. code = "";
  2295. }
  2296. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2297. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2298. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  2299. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2300. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  2301. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2302. }
  2303. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  2304. [container setValue:stateDic forKey:key];
  2305. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  2306. DebugLog(@"query all state error: %@",err_msg);
  2307. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2308. // [stateDic setValue:@"Other" forKey:@"value"];
  2309. // [stateDic setValue:@"" forKey:@"value_id"];
  2310. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2311. //
  2312. // if (state_code && [@"" isEqualToString:state_code]) {
  2313. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2314. // }
  2315. //
  2316. // NSString *key = [NSString stringWithFormat:@"val_0"];
  2317. // [container setValue:stateDic forKey:key];
  2318. }] mutableCopy];
  2319. // failure 可以不用了,一样的
  2320. if (ret.allKeys.count == 0) {
  2321. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2322. [stateDic setValue:@"Other" forKey:@"value"];
  2323. [stateDic setValue:@"" forKey:@"value_id"];
  2324. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2325. if (state_code && [@"" isEqualToString:state_code]) {
  2326. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2327. }
  2328. NSString *key = [NSString stringWithFormat:@"val_0"];
  2329. [ret setValue:stateDic forKey:key];
  2330. }
  2331. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2332. return ret;
  2333. }
  2334. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  2335. codeId = [self translateSingleQuote:codeId];
  2336. NSString *sql = [NSString stringWithFormat:@"select name,code from offline_state where country_code = (select code as country_code FROM offline_country where countrycode_id = %@);",codeId];
  2337. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2338. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  2339. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  2340. if (name == NULL) {
  2341. name = "";
  2342. }
  2343. if (code == NULL) {
  2344. code = "";
  2345. }
  2346. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2347. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2348. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  2349. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2350. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  2351. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2352. }
  2353. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  2354. [container setValue:stateDic forKey:key];
  2355. }] mutableCopy];
  2356. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2357. return ret;
  2358. }
  2359. + (NSDictionary *)offline_getPrice {
  2360. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  2361. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2362. char *name = (char *) sqlite3_column_text(stmt, 1);
  2363. int type = sqlite3_column_int(stmt, 2);
  2364. int orderBy = sqlite3_column_int(stmt, 3);
  2365. if (name == NULL) {
  2366. name = "";
  2367. }
  2368. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  2369. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2370. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  2371. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  2372. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2373. if (orderBy == 0) {
  2374. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2375. }
  2376. [container setValue:priceDic forKey:key];
  2377. }] mutableCopy];
  2378. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2379. return ret;
  2380. }
  2381. + (NSDictionary *)offline_getSalesRep {
  2382. // 首先从offline_login表中取出sales_code
  2383. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2384. NSString *user = app.user;
  2385. user = [self translateSingleQuote:user];
  2386. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  2387. __block NSString *user_code = @"";
  2388. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2389. char *code = (char *)sqlite3_column_text(stmt, 0);
  2390. if (code == NULL) {
  2391. code = "";
  2392. }
  2393. user_code = [NSString stringWithUTF8String:code];
  2394. }];
  2395. // 再取所有salesRep
  2396. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  2397. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2398. // 1 name 2 code 3 salesrep_id
  2399. char *name = (char *)sqlite3_column_text(stmt, 1);
  2400. char *code = (char *)sqlite3_column_text(stmt, 2);
  2401. int salesrep_id = sqlite3_column_int(stmt, 3);
  2402. if (name == NULL) {
  2403. name = "";
  2404. }
  2405. if (code == NULL) {
  2406. code = "";
  2407. }
  2408. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  2409. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  2410. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  2411. // 比较code 相等则check
  2412. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  2413. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2414. }
  2415. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  2416. }] mutableCopy];
  2417. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2418. return ret;
  2419. }
  2420. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  2421. zipcode = [self translateSingleQuote:zipcode];
  2422. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  2423. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2424. char *country = (char *)sqlite3_column_text(stmt, 0);
  2425. char *state = (char *)sqlite3_column_text(stmt, 1);
  2426. char *city = (char *)sqlite3_column_text(stmt, 2);
  2427. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  2428. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  2429. if (country == NULL) {
  2430. country = "";
  2431. }
  2432. if (state == NULL) {
  2433. state = "";
  2434. }
  2435. if (city == NULL) {
  2436. city = "";
  2437. }
  2438. if (country_code == NULL) {
  2439. country_code = "";
  2440. }
  2441. if (state_code == NULL) {
  2442. state_code = "";
  2443. }
  2444. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  2445. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  2446. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  2447. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  2448. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  2449. }] mutableCopy];
  2450. return ret;
  2451. }
  2452. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  2453. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  2454. [item setValue:value forKey:valueKey];
  2455. [dic setValue:item forKey:itemKey];
  2456. }
  2457. + (NSString *)countryCodeByid:(NSString *)code_id {
  2458. NSString *ret = nil;
  2459. code_id = [self translateSingleQuote:code_id];
  2460. sqlite3 *db = [iSalesDB get_db];
  2461. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  2462. sqlite3_stmt * statement;
  2463. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2464. while (sqlite3_step(statement) == SQLITE_ROW) {
  2465. char *code = (char *)sqlite3_column_text(statement, 0);
  2466. if (code == NULL) {
  2467. code = "";
  2468. }
  2469. ret = [NSString stringWithUTF8String:code];
  2470. }
  2471. sqlite3_finalize(statement);
  2472. }
  2473. [iSalesDB close_db:db];
  2474. return ret;
  2475. }
  2476. + (NSString *)countryCodeIdByCode:(NSString *)code {
  2477. NSString *ret = nil;
  2478. code = [self translateSingleQuote:code];
  2479. sqlite3 *db = [iSalesDB get_db];
  2480. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  2481. sqlite3_stmt * statement;
  2482. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2483. while (sqlite3_step(statement) == SQLITE_ROW) {
  2484. char *_id = (char *)sqlite3_column_text(statement, 0);
  2485. if (_id == NULL) {
  2486. _id = "";
  2487. }
  2488. ret = [NSString stringWithFormat:@"%s",_id];
  2489. }
  2490. sqlite3_finalize(statement);
  2491. }
  2492. [iSalesDB close_db:db];
  2493. return ret;
  2494. }
  2495. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  2496. NSString *name = nil;
  2497. codeId = [self translateSingleQuote:codeId];
  2498. sqlite3 *db = [iSalesDB get_db];
  2499. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  2500. sqlite3_stmt * statement;
  2501. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2502. while (sqlite3_step(statement) == SQLITE_ROW) {
  2503. char *value = (char *)sqlite3_column_text(statement, 0);
  2504. if (value == NULL) {
  2505. value = "";
  2506. }
  2507. name = [NSString stringWithUTF8String:value];
  2508. }
  2509. sqlite3_finalize(statement);
  2510. }
  2511. [iSalesDB close_db:db];
  2512. return name;
  2513. }
  2514. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  2515. NSString *ret = nil;
  2516. sqlite3 *db = [iSalesDB get_db];
  2517. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  2518. sqlite3_stmt * statement;
  2519. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2520. while (sqlite3_step(statement) == SQLITE_ROW) {
  2521. char *name = (char *)sqlite3_column_text(statement, 0);
  2522. if (name == NULL) {
  2523. name = "";
  2524. }
  2525. ret = [NSString stringWithUTF8String:name];
  2526. }
  2527. sqlite3_finalize(statement);
  2528. }
  2529. [iSalesDB close_db:db];
  2530. return ret;
  2531. }
  2532. + (NSString *)salesRepCodeById:(NSString *)_id {
  2533. NSString *ret = nil;
  2534. _id = [self translateSingleQuote:_id];
  2535. sqlite3 *db = [iSalesDB get_db];
  2536. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  2537. sqlite3_stmt * statement;
  2538. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2539. while (sqlite3_step(statement) == SQLITE_ROW) {
  2540. char *rep = (char *)sqlite3_column_text(statement, 0);
  2541. if (rep == NULL) {
  2542. rep = "";
  2543. }
  2544. ret = [NSString stringWithUTF8String:rep];
  2545. }
  2546. sqlite3_finalize(statement);
  2547. }
  2548. [iSalesDB close_db:db];
  2549. return ret;
  2550. }
  2551. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  2552. char *tx = (char *)sqlite3_column_text(stmt, col);
  2553. if (tx == NULL) {
  2554. tx = "";
  2555. }
  2556. NSString *text = [NSString stringWithFormat:@"%s",tx];
  2557. if (!text) {
  2558. text = @"";
  2559. }
  2560. // 将字符全部为' '的字符串干掉
  2561. int spaceCount = 0;
  2562. for (int i = 0; i < text.length; i++) {
  2563. if ([text characterAtIndex:i] == ' ') {
  2564. spaceCount++;
  2565. }
  2566. }
  2567. if (spaceCount == text.length) {
  2568. text = @"";
  2569. }
  2570. return text;
  2571. }
  2572. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  2573. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  2574. NSData *data = [NSData dataWithContentsOfFile:path];
  2575. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2576. return ret;
  2577. }
  2578. + (NSString *)textFileName:(NSString *)name {
  2579. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  2580. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  2581. if (!text) {
  2582. text = @"";
  2583. }
  2584. return text;
  2585. }
  2586. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  2587. return [[dic objectForKey:key] mutableCopy];
  2588. }
  2589. + (id)translateSingleQuote:(NSString *)string {
  2590. if ([string isKindOfClass:[NSString class]])
  2591. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2592. return string;
  2593. }
  2594. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  2595. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  2596. }
  2597. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  2598. NSString* ret= nil;
  2599. NSString *sqlQuery = nil;
  2600. sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  2601. sqlite3_stmt * statement;
  2602. // int count=0;
  2603. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2604. {
  2605. if (sqlite3_step(statement) == SQLITE_ROW)
  2606. {
  2607. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2608. if(imgurl==nil)
  2609. imgurl="";
  2610. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2611. ret=nsimgurl;
  2612. }
  2613. sqlite3_finalize(statement);
  2614. }
  2615. else
  2616. {
  2617. [ret setValue:@"8" forKey:@"result"];
  2618. }
  2619. // [iSalesDB close_db:db];
  2620. DebugLog(@"data string: %@",ret );
  2621. return ret;
  2622. }
  2623. #pragma mark contact list
  2624. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  2625. {
  2626. // contactType = "Sales_Order_Customer";
  2627. // limit = 25;
  2628. // offset = 0;
  2629. // password = 123456;
  2630. // "price_name" = 16;
  2631. // user = EvanK;
  2632. sqlite3 *db = [iSalesDB get_db];
  2633. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  2634. if (contactType) {
  2635. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  2636. } else {
  2637. contactType = @"1 = 1";
  2638. }
  2639. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  2640. DebugLog(@"offline contact list keyword: %@",keyword);
  2641. // advanced search
  2642. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  2643. if (contact_name) {
  2644. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2645. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  2646. } else {
  2647. contact_name = @"";
  2648. }
  2649. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  2650. if (customer_phone) {
  2651. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2652. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  2653. } else {
  2654. customer_phone = @"";
  2655. }
  2656. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  2657. if (customer_fax) {
  2658. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2659. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  2660. } else {
  2661. customer_fax = @"";
  2662. }
  2663. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  2664. if (customer_zipcode) {
  2665. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2666. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  2667. } else {
  2668. customer_zipcode = @"";
  2669. }
  2670. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  2671. if (customer_sales_rep) {
  2672. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2673. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  2674. } else {
  2675. customer_sales_rep = @"";
  2676. }
  2677. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  2678. if (customer_state) {
  2679. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2680. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  2681. } else {
  2682. customer_state = @"";
  2683. }
  2684. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  2685. if (customer_name) {
  2686. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2687. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  2688. } else {
  2689. customer_name = @"";
  2690. }
  2691. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  2692. if (customer_country) {
  2693. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2694. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  2695. } else {
  2696. customer_country = @"";
  2697. }
  2698. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  2699. if (customer_cid) {
  2700. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2701. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  2702. } else {
  2703. customer_cid = @"";
  2704. }
  2705. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  2706. if (customer_city) {
  2707. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2708. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  2709. } else {
  2710. customer_city = @"";
  2711. }
  2712. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  2713. if (customer_address) {
  2714. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2715. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  2716. } else {
  2717. customer_address = @"";
  2718. }
  2719. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  2720. if (customer_email) {
  2721. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2722. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  2723. } else {
  2724. customer_email = @"";
  2725. }
  2726. NSString *price_name = [params valueForKey:@"price_name"];
  2727. if (price_name) {
  2728. if ([price_name containsString:@","]) {
  2729. // 首先从 price表中查处name
  2730. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  2731. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  2732. for (int i = 1;i < pArray.count;i++) {
  2733. NSString *p = pArray[i];
  2734. [mutablePStr appendFormat:@" or type = %@ ",p];
  2735. }
  2736. [mutablePStr appendString:@";"];
  2737. __block NSMutableArray *price_name_array = [NSMutableArray array];
  2738. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2739. char *name = (char *)sqlite3_column_text(stmt, 0);
  2740. if (!name)
  2741. name = "";
  2742. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  2743. }];
  2744. // 再根据name 拼sql
  2745. NSMutableString *mutable_price_name = [NSMutableString string];
  2746. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  2747. for (int i = 1; i < price_name_array.count; i++) {
  2748. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  2749. }
  2750. [mutable_price_name appendString:@")"];
  2751. price_name = mutable_price_name;
  2752. } else {
  2753. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  2754. if ([price_name isEqualToString:@""]) {
  2755. price_name = @"";
  2756. } else {
  2757. __block NSString *price;
  2758. price_name = [self translateSingleQuote:price_name];
  2759. [iSalesDB jk_query:[NSString stringWithFormat:@"SELECT name FROM price where type = %@ ;",price_name] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2760. char *p = (char *)sqlite3_column_text(stmt, 0);
  2761. if (p == NULL) {
  2762. p = "";
  2763. }
  2764. price = [NSString stringWithUTF8String:p];
  2765. }];
  2766. if ([price isEqualToString:@""]) {
  2767. price_name = @"";
  2768. } else {
  2769. price = [self translateSingleQuote:price];
  2770. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  2771. }
  2772. }
  2773. }
  2774. } else {
  2775. price_name = @"";
  2776. }
  2777. int limit = [[params valueForKey:@"limit"] intValue];
  2778. int offset = [[params valueForKey:@"offset"] intValue];
  2779. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2780. NSString *where = [NSString stringWithFormat:@" %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ and is_active=1 ",contactType,contact_name,customer_phone,customer_fax,customer_zipcode,customer_sales_rep,customer_state,customer_name,customer_country,customer_cid,customer_city,customer_address,customer_email,price_name];
  2781. NSString *sql = [NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ order by decrypt(company_name) limit %d offset %d",where,limit,offset];
  2782. // int result= [iSalesDB AddExFunction:db];
  2783. int count =0;
  2784. NSString *sqlQuery = nil;
  2785. if(keyword.length==0)
  2786. {
  2787. // sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@=1 order by decrypt(company_name) limit %d offset %d",contactType , limit, offset];
  2788. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  2789. sqlQuery = sql;
  2790. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  2791. }
  2792. else
  2793. {
  2794. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  2795. keyword = keyword.lowercaseString;
  2796. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2797. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ and (lower(contact_id ) like'%%%@%%' or lower(decrypt(company_name)) like '%%%@%%' or lower(decrypt(phone)) like '%%%@%%' or lower(decrypt(addr)) like '%%%@%%') and is_active=1 order by decrypt(company_name) limit %d offset %d",contactType , keyword,keyword,keyword,keyword,limit, offset];
  2798. count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@=1 and (lower(contact_id ) like'%@' or lower(company_name) like'%@') and is_active=1",contactType,keyword,keyword]];
  2799. }
  2800. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  2801. sqlite3_stmt * statement;
  2802. [ret setValue:@"2" forKey:@"result"];
  2803. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  2804. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2805. {
  2806. int i = 0;
  2807. while (sqlite3_step(statement) == SQLITE_ROW)
  2808. {
  2809. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2810. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2811. int editable = sqlite3_column_int(statement, 0);
  2812. char *company_name = (char*)sqlite3_column_text(statement, 1);
  2813. NSString *nscompany_name =nil;
  2814. if(company_name==nil)
  2815. nscompany_name=@"";
  2816. else
  2817. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  2818. char *country = (char*)sqlite3_column_text(statement, 2);
  2819. if(country==nil)
  2820. country="";
  2821. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  2822. // char *addr = (char*)sqlite3_column_text(statement, 3);
  2823. // if(addr==nil)
  2824. // addr="";
  2825. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  2826. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  2827. if(zipcode==nil)
  2828. zipcode="";
  2829. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  2830. char *state = (char*)sqlite3_column_text(statement, 5);
  2831. if(state==nil)
  2832. state="";
  2833. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  2834. char *city = (char*)sqlite3_column_text(statement, 6);
  2835. if(city==nil)
  2836. city="";
  2837. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  2838. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  2839. // NSString *nscontact_name = nil;
  2840. // if(contact_name==nil)
  2841. // nscontact_name=@"";
  2842. // else
  2843. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  2844. char *phone = (char*)sqlite3_column_text(statement, 8);
  2845. NSString *nsphone = nil;
  2846. if(phone==nil)
  2847. nsphone=@"";
  2848. else
  2849. nsphone= [[NSString alloc]initWithUTF8String:phone];
  2850. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  2851. if(contact_id==nil)
  2852. contact_id="";
  2853. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2854. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  2855. if(addr_1==nil)
  2856. addr_1="";
  2857. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  2858. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  2859. if(addr_2==nil)
  2860. addr_2="";
  2861. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  2862. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  2863. if(addr_3==nil)
  2864. addr_3="";
  2865. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  2866. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  2867. if(addr_4==nil)
  2868. addr_4="";
  2869. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  2870. char *first_name = (char*)sqlite3_column_text(statement, 14);
  2871. if(first_name==nil)
  2872. first_name="";
  2873. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  2874. char *last_name = (char*)sqlite3_column_text(statement, 15);
  2875. if(last_name==nil)
  2876. last_name="";
  2877. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  2878. char *fax = (char*)sqlite3_column_text(statement, 16);
  2879. NSString *nsfax = nil;
  2880. if(fax==nil)
  2881. nsfax=@"";
  2882. else
  2883. {
  2884. nsfax= [[NSString alloc]initWithUTF8String:fax];
  2885. if(nsfax.length>0)
  2886. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  2887. }
  2888. char *email = (char*)sqlite3_column_text(statement, 17);
  2889. NSString *nsemail = nil;
  2890. if(email==nil)
  2891. nsemail=@"";
  2892. else
  2893. {
  2894. nsemail= [[NSString alloc]initWithUTF8String:email];
  2895. if(nsemail.length>0)
  2896. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  2897. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  2898. }
  2899. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2900. [arr_name addObject:nsfirst_name];
  2901. [arr_name addObject:nslast_name];
  2902. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2903. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  2904. {
  2905. // decrypt
  2906. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2907. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2908. nsphone=[AESCrypt fastdecrypt:nsphone];
  2909. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2910. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2911. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2912. }
  2913. [arr_addr addObject:nscompany_name];
  2914. [arr_addr addObject:nscontact_name];
  2915. [arr_addr addObject:@"<br>"];
  2916. [arr_addr addObject:nsaddr_1];
  2917. [arr_addr addObject:nsaddr_2];
  2918. [arr_addr addObject:nsaddr_3];
  2919. [arr_addr addObject:nsaddr_4];
  2920. //[arr_addr addObject:nsaddr];
  2921. [arr_addr addObject:nszipcode];
  2922. [arr_addr addObject:nscity];
  2923. [arr_addr addObject:nsstate];
  2924. [arr_addr addObject:nscountry];
  2925. [arr_addr addObject:@"<br>"];
  2926. [arr_addr addObject:nsphone];
  2927. [arr_addr addObject:nsfax];
  2928. [arr_addr addObject:nsemail];
  2929. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  2930. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  2931. [item setValue:name forKey:@"name"];
  2932. [item setValue:nscontact_id forKey:@"contact_id"];
  2933. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  2934. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2935. i++;
  2936. }
  2937. UIApplication * app = [UIApplication sharedApplication];
  2938. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2939. [ret setValue:appDelegate.mode forKey:@"mode"];
  2940. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  2941. sqlite3_finalize(statement);
  2942. }
  2943. [iSalesDB close_db:db];
  2944. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2945. return ret;
  2946. }
  2947. #pragma mark contact Advanced search
  2948. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  2949. {
  2950. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  2951. return [RAUtils dict2data:contactAdvanceDic];
  2952. }
  2953. #pragma mark create new contact
  2954. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  2955. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  2956. NSData *data = [NSData dataWithContentsOfFile:path];
  2957. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2958. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  2959. NSString *countryCode = nil;
  2960. NSString *countryCode_id = nil;
  2961. NSString *stateCode = nil;
  2962. NSString *city = nil;
  2963. NSString *zipCode = nil;
  2964. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  2965. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  2966. NSString *code_id = params[@"country"];
  2967. countryCode_id = code_id;
  2968. countryCode = [self countryCodeByid:code_id];
  2969. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  2970. NSString *zip_code = params[@"zipcode"];
  2971. // 剔除全部为空格
  2972. int spaceCount = 0;
  2973. for (int i = 0; i < zip_code.length; i++) {
  2974. if ([zip_code characterAtIndex:i] == ' ') {
  2975. spaceCount++;
  2976. }
  2977. }
  2978. if (spaceCount == zip_code.length) {
  2979. zip_code = @"";
  2980. }
  2981. zipCode = zip_code;
  2982. if (zipCode.length > 0) {
  2983. countryCode_id = params[@"country"];
  2984. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  2985. countryCode = [dic valueForKey:@"country_code"];
  2986. if (!countryCode) {
  2987. countryCode = @"US";
  2988. }
  2989. stateCode = [dic valueForKey:@"state_code"];
  2990. city = [dic valueForKey:@"city"];
  2991. // zip code
  2992. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  2993. [zipDic setValue:zipCode forKey:@"value"];
  2994. [section_0 setValue:zipDic forKey:@"item_10"];
  2995. }
  2996. }
  2997. } else {
  2998. // default: US United States
  2999. countryCode = @"US";
  3000. countryCode_id = @"228";
  3001. }
  3002. // country
  3003. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3004. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  3005. // state
  3006. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3007. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  3008. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  3009. // city
  3010. if (city) {
  3011. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  3012. [cityDic setValue:city forKey:@"value"];
  3013. [section_0 setValue:cityDic forKey:@"item_12"];
  3014. }
  3015. // price type
  3016. NSDictionary *priceDic = [self offline_getPrice];
  3017. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  3018. // Sales Rep
  3019. NSDictionary *repDic = [self offline_getSalesRep];
  3020. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3021. [ret setValue:section_0 forKey:@"section_0"];
  3022. return [RAUtils dict2data:ret];
  3023. }
  3024. #pragma mark save
  3025. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  3026. NSString *addr = nil;
  3027. NSString *contact_name = nil;
  3028. NSString *companyName = [params objectForKey:@"company"];
  3029. if (companyName) {
  3030. companyName = [AESCrypt fastencrypt:companyName];
  3031. } else {
  3032. companyName = @"";
  3033. }
  3034. DebugLog(@"company");
  3035. companyName = [self translateSingleQuote:companyName];
  3036. NSString *addr1 = [params objectForKey:@"address"];
  3037. NSString *addr2 = [params objectForKey:@"address2"];
  3038. NSString *addr3 = [params objectForKey:@"address_3"];
  3039. NSString *addr4 = [params objectForKey:@"address_4"];
  3040. if (!addr2) {
  3041. addr2 = @"";
  3042. }
  3043. if (!addr3) {
  3044. addr3 = @"";
  3045. }
  3046. if (!addr4) {
  3047. addr4 = @"";
  3048. }
  3049. if (!addr1) {
  3050. addr1 = @"";
  3051. }
  3052. DebugLog(@"addr");
  3053. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  3054. addr = [AESCrypt fastencrypt:addr];
  3055. addr = [self translateSingleQuote:addr];
  3056. if (addr1 && ![addr1 isEqualToString:@""]) {
  3057. addr1 = [AESCrypt fastencrypt:addr1];
  3058. }
  3059. addr1 = [self translateSingleQuote:addr1];
  3060. addr2 = [self translateSingleQuote:addr2];
  3061. addr3 = [self translateSingleQuote:addr3];
  3062. addr4 = [self translateSingleQuote:addr4];
  3063. NSString *country = [params objectForKey:@"country"];
  3064. if (country) {
  3065. country = [self countryNameByCountryCodeId:country];
  3066. } else {
  3067. country = @"";
  3068. }
  3069. DebugLog(@"country");
  3070. country = [self translateSingleQuote:country];
  3071. NSString *state = [params objectForKey:@"state"];
  3072. if (!state) {
  3073. state = @"";
  3074. }
  3075. DebugLog(@"state");
  3076. state = [self translateSingleQuote:state];
  3077. NSString *city = [params objectForKey:@"city"];
  3078. if (!city) {
  3079. city = @"";
  3080. }
  3081. city = [self translateSingleQuote:city];
  3082. NSString *zipcode = [params objectForKey:@"zipcode"];
  3083. if (!zipcode) {
  3084. zipcode = @"";
  3085. }
  3086. DebugLog(@"zip");
  3087. zipcode = [self translateSingleQuote:zipcode];
  3088. NSString *fistName = [params objectForKey:@"firstname"];
  3089. if (!fistName) {
  3090. fistName = @"";
  3091. }
  3092. NSString *lastName = [params objectForKey:@"lastname"];
  3093. if (!lastName) {
  3094. lastName = @"";
  3095. }
  3096. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  3097. DebugLog(@"contact_name");
  3098. contact_name = [self translateSingleQuote:contact_name];
  3099. fistName = [self translateSingleQuote:fistName];
  3100. lastName = [self translateSingleQuote:lastName];
  3101. NSString *phone = [params objectForKey:@"phone"];
  3102. if (phone) {
  3103. phone = [AESCrypt fastencrypt:phone];
  3104. } else {
  3105. phone = @"";
  3106. }
  3107. DebugLog(@"PHONE");
  3108. phone = [self translateSingleQuote:phone];
  3109. NSString *fax = [params objectForKey:@"fax"];
  3110. if (!fax) {
  3111. fax = @"";
  3112. }
  3113. DebugLog(@"FAX");
  3114. fax = [self translateSingleQuote:fax];
  3115. NSString *email = [params objectForKey:@"email"];
  3116. if (!email) {
  3117. email = @"";
  3118. }
  3119. DebugLog(@"EMAIL:%@",email);
  3120. email = [self translateSingleQuote:email];
  3121. NSString *notes = [params objectForKey:@"contact_notes"];
  3122. if (!notes) {
  3123. notes = @"";
  3124. }
  3125. DebugLog(@"NOTE:%@",notes);
  3126. notes = [self translateSingleQuote:notes];
  3127. NSString *price = [params objectForKey:@"price_name"];
  3128. if (price) {
  3129. price = [self priceNameByPriceId:price];
  3130. } else {
  3131. price = @"";
  3132. }
  3133. DebugLog(@"PRICE");
  3134. price = [self translateSingleQuote:price];
  3135. NSString *salesRep = [params objectForKey:@"sales_rep"];
  3136. if (salesRep) {
  3137. salesRep = [self salesRepCodeById:salesRep];
  3138. } else {
  3139. salesRep = @"";
  3140. }
  3141. salesRep = [self translateSingleQuote:salesRep];
  3142. NSString *img = [params objectForKey:@"business_card"];
  3143. NSArray *array = [img componentsSeparatedByString:@","];
  3144. NSString *img_0 = array[0];
  3145. if (!img_0) {
  3146. img_0 = @"";
  3147. }
  3148. img_0 = [self translateSingleQuote:img_0];
  3149. NSString *img_1 = array[1];
  3150. if (!img_1) {
  3151. img_1 = @"";
  3152. }
  3153. img_1 = [self translateSingleQuote:img_1];
  3154. NSString *img_2 = array[2];
  3155. if (!img_2) {
  3156. img_2 = @"";
  3157. }
  3158. img_2 = [self translateSingleQuote:img_2];
  3159. NSString *contact_id = [NSUUID UUID].UUIDString;
  3160. NSMutableDictionary *sync_dic = [params mutableCopy];
  3161. if (isCustomer) {
  3162. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  3163. } else {
  3164. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  3165. }
  3166. NSString *sync_data = nil;
  3167. NSString *sql = nil;
  3168. if (update){
  3169. contact_id = [params objectForKey:@"contact_id"];
  3170. if (!contact_id) {
  3171. contact_id = @"";
  3172. }
  3173. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3174. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3175. sync_data = [RAUtils dict2string:sync_dic];
  3176. sync_data = [self translateSingleQuote:sync_data];
  3177. sql = [NSString stringWithFormat:@"update offline_contact set company_name = '%@',addr_1 = '%@',addr_2 = '%@',addr_3 = '%@',addr_4 = '%@',country = '%@',state = '%@',city = '%@',zipcode = '%@',first_name = '%@',last_name = '%@',phone = '%@',fax = '%@',email = '%@',notes = '%@',price_type = '%@',sales_rep = '%@',img_0 = '%@',img_1 = '%@',img_2 = '%@',sync_data = '%@',contact_name = '%@',addr = '%@' where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,sync_data,contact_name,addr,contact_id];
  3178. } else {
  3179. contact_id = [self translateSingleQuote:contact_id];
  3180. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3181. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3182. sync_data = [RAUtils dict2string:sync_dic];
  3183. sync_data = [self translateSingleQuote:sync_data];
  3184. sql = [NSString stringWithFormat:@"insert into offline_contact (is_active,company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To,sync_data,contact_name,addr) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',1,1,0,1,0,1,'%@','%@','%@')",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id,sync_data,contact_name,addr];
  3185. }
  3186. int result = [iSalesDB execSql:sql];
  3187. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  3188. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  3189. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  3190. }
  3191. #pragma mark save new contact
  3192. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  3193. {
  3194. return [self offline_saveContact:params update:NO isCustomer:YES];
  3195. }
  3196. #pragma mark edit contact
  3197. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  3198. {
  3199. // {
  3200. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  3201. // password = 123456;
  3202. // user = EvanK;
  3203. // }
  3204. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  3205. NSData *data = [NSData dataWithContentsOfFile:path];
  3206. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3207. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3208. NSString *countryCode = nil;
  3209. NSString *countryCode_id = nil;
  3210. NSString *stateCode = nil;
  3211. /*------contact infor------*/
  3212. __block NSString *country = nil;
  3213. __block NSString *company_name = nil;
  3214. __block NSString *contact_id = params[@"contact_id"];
  3215. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  3216. __block NSString *zipcode = nil;
  3217. __block NSString *state = nil; // state_code
  3218. __block NSString *city = nil; //
  3219. __block NSString *firt_name,*last_name;
  3220. __block NSString *phone,*fax,*email;
  3221. __block NSString *notes,*price_type,*sales_rep;
  3222. __block NSString *img_0,*img_1,*img_2;
  3223. contact_id = [self translateSingleQuote:contact_id];
  3224. NSString *sql = [NSString stringWithFormat:@"select country,company_name,addr_1,addr_2,addr_3,addr_4,zipcode,state,city,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2 from offline_contact where contact_id = '%@';",contact_id];
  3225. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3226. country = [self textAtColumn:0 statement:stmt]; // country name
  3227. company_name = [self textAtColumn:1 statement:stmt];
  3228. addr_1 = [self textAtColumn:2 statement:stmt];
  3229. addr_2 = [self textAtColumn:3 statement:stmt];
  3230. addr_3 = [self textAtColumn:4 statement:stmt];
  3231. addr_4 = [self textAtColumn:5 statement:stmt];
  3232. zipcode = [self textAtColumn:6 statement:stmt];
  3233. state = [self textAtColumn:7 statement:stmt]; // state code
  3234. city = [self textAtColumn:8 statement:stmt];
  3235. firt_name = [self textAtColumn:9 statement:stmt];
  3236. last_name = [self textAtColumn:10 statement:stmt];
  3237. phone = [self textAtColumn:11 statement:stmt];
  3238. fax = [self textAtColumn:12 statement:stmt];
  3239. email = [self textAtColumn:13 statement:stmt];
  3240. notes = [self textAtColumn:14 statement:stmt];
  3241. price_type = [self textAtColumn:15 statement:stmt]; // name
  3242. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  3243. img_0 = [self textAtColumn:17 statement:stmt];
  3244. img_1 = [self textAtColumn:18 statement:stmt];
  3245. img_2 = [self textAtColumn:19 statement:stmt];
  3246. }];
  3247. // decrypt
  3248. if (company_name) {
  3249. company_name = [AESCrypt fastdecrypt:company_name];
  3250. }
  3251. if (addr_1) {
  3252. addr_1 = [AESCrypt fastdecrypt:addr_1];
  3253. }
  3254. if (phone) {
  3255. phone = [AESCrypt fastdecrypt:phone];
  3256. }
  3257. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  3258. countryCode = [iSalesDB jk_queryText:countrySql];
  3259. stateCode = state;
  3260. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3261. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  3262. NSString *code_id = params[@"country"];
  3263. countryCode_id = code_id;
  3264. countryCode = [self countryCodeByid:code_id];
  3265. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  3266. NSString *zip_code = params[@"zipcode"];
  3267. // 剔除全部为空格
  3268. int spaceCount = 0;
  3269. for (int i = 0; i < zip_code.length; i++) {
  3270. if ([zip_code characterAtIndex:i] == ' ') {
  3271. spaceCount++;
  3272. }
  3273. }
  3274. if (spaceCount == zip_code.length) {
  3275. zip_code = @"";
  3276. }
  3277. if (zipcode.length > 0) {
  3278. zipcode = zip_code;
  3279. countryCode_id = params[@"country"];
  3280. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3281. countryCode = [dic valueForKey:@"country_code"];
  3282. stateCode = [dic valueForKey:@"state_code"];
  3283. city = [dic valueForKey:@"city"];
  3284. // zip code
  3285. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  3286. [zipDic setValue:zipcode forKey:@"value"];
  3287. [section_0 setValue:zipDic forKey:@"item_8"];
  3288. }
  3289. }
  3290. }
  3291. // 0 Country
  3292. // 1 Company Name
  3293. // 2 Contact ID
  3294. // 3 Picture
  3295. // 4 Address 1
  3296. // 5 Address 2
  3297. // 6 Address 3
  3298. // 7 Address 4
  3299. // 8 Zip Code
  3300. // 9 State/Province
  3301. // 10 City
  3302. // 11 Contact First Name
  3303. // 12 Contact Last Name
  3304. // 13 Phone
  3305. // 14 Fax
  3306. // 15 Email
  3307. // 16 Contact Notes
  3308. // 17 Price Type
  3309. // 18 Sales Rep
  3310. // country
  3311. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3312. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  3313. // company
  3314. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  3315. // contact_id
  3316. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  3317. // picture
  3318. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  3319. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  3320. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  3321. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  3322. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  3323. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  3324. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  3325. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  3326. // addr 1 2 3 4
  3327. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  3328. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  3329. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  3330. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  3331. // zip code
  3332. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  3333. // state
  3334. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3335. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  3336. // city
  3337. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  3338. // first last
  3339. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  3340. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  3341. // phone fax email
  3342. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  3343. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  3344. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  3345. // notes
  3346. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  3347. // price
  3348. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  3349. for (NSString *key in priceDic.allKeys) {
  3350. if ([key containsString:@"val_"]) {
  3351. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  3352. if ([dic[@"value"] isEqualToString:price_type]) {
  3353. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3354. [priceDic setValue:dic forKey:key];
  3355. }
  3356. }
  3357. }
  3358. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3359. // Sales Rep
  3360. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  3361. for (NSString *key in repDic.allKeys) {
  3362. if ([key containsString:@"val_"]) {
  3363. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  3364. NSString *value = dic[@"value"];
  3365. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  3366. if (code && [code isEqualToString:sales_rep]) {
  3367. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3368. [repDic setValue:dic forKey:key];
  3369. }
  3370. }
  3371. }
  3372. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  3373. [ret setValue:section_0 forKey:@"section_0"];
  3374. return [RAUtils dict2data:ret];
  3375. }
  3376. #pragma mark save contact
  3377. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  3378. {
  3379. return [self offline_saveContact:params update:YES isCustomer:YES];
  3380. }
  3381. #pragma mark category
  3382. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  3383. if (ck) {
  3384. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  3385. for (NSString *key in res.allKeys) {
  3386. if (![key isEqualToString:@"count"]) {
  3387. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  3388. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  3389. if ([val[@"value"] isEqualToString:ck]) {
  3390. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3391. }
  3392. [res setValue:val forKey:key];
  3393. }
  3394. }
  3395. [dic setValue:res forKey:valueKey];
  3396. }
  3397. }
  3398. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  3399. {
  3400. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3401. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3402. NSString* category = [params valueForKey:@"category"];
  3403. if (!category || [category isEqualToString:@""]) {
  3404. category = @"%";
  3405. }
  3406. category = [self translateSingleQuote:category];
  3407. int limit = [[params valueForKey:@"limit"] intValue];
  3408. int offset = [[params valueForKey:@"offset"] intValue];
  3409. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3410. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  3411. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  3412. sqlite3 *db = [iSalesDB get_db];
  3413. // [iSalesDB AddExFunction:db];
  3414. int count;
  3415. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  3416. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from product m left join wishlist w on m.product_id=w.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name limit %d offset %d ;",category,limit, offset];
  3417. // {
  3418. // alert = SP;
  3419. // available = "Available Now";
  3420. // bestseller = No;
  3421. // category = 001;
  3422. // "custom_id" = 0;
  3423. // limit = 20;
  3424. // offset = 0;
  3425. // password = 123456;
  3426. // price = "Display All";
  3427. // "price_template" = 0;
  3428. // "sold_by_qty" = "Display All";
  3429. // "sort_by" = 0;
  3430. // user = EvanK;
  3431. // }
  3432. double price_min = 0;
  3433. double price_max = 0;
  3434. if ([params.allKeys containsObject:@"alert"]) {
  3435. // alert
  3436. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  3437. NSString *alert = params[@"alert"];
  3438. if ([alert isEqualToString:@"Display All"]) {
  3439. alert = [NSString stringWithFormat:@""];
  3440. } else {
  3441. alert = [self translateSingleQuote:alert];
  3442. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  3443. }
  3444. // available
  3445. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  3446. NSString *available = params[@"available"];
  3447. NSString *available_condition;
  3448. if ([available isEqualToString:@"Display All"]) {
  3449. available_condition = @"";
  3450. } else if ([available isEqualToString:@"Available Now"]) {
  3451. available_condition = @"and availability > 0";
  3452. } else {
  3453. available_condition = @"and availability == 0";
  3454. }
  3455. // best seller
  3456. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  3457. NSString *best_seller = @"";
  3458. NSString *order_best_seller = @"m.name asc";
  3459. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  3460. best_seller = @"and best_seller > 0";
  3461. order_best_seller = @"m.best_seller desc,m.name asc";
  3462. }
  3463. // price
  3464. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  3465. NSString *price = params[@"price"];
  3466. price_min = 0;
  3467. price_max = MAXFLOAT;
  3468. if (appDelegate.user) {
  3469. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  3470. NSMutableString *priceName = [NSMutableString string];
  3471. for (int i = 0; i < priceTypeArray.count; i++) {
  3472. NSString *pricetype = priceTypeArray[i];
  3473. pricetype = [self translateSingleQuote:pricetype];
  3474. if (i == 0) {
  3475. [priceName appendFormat:@"'%@'",pricetype];
  3476. } else {
  3477. [priceName appendFormat:@",'%@'",pricetype];
  3478. }
  3479. }
  3480. if ([price isEqualToString:@"Display All"]) {
  3481. price = [NSString stringWithFormat:@""];
  3482. } else if([price containsString:@"+"]){
  3483. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  3484. price_min = [price doubleValue];
  3485. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  3486. } else {
  3487. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  3488. price_min = [[priceArray objectAtIndex:0] doubleValue];
  3489. price_max = [[priceArray objectAtIndex:1] doubleValue];
  3490. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  3491. }
  3492. } else {
  3493. price = @"";
  3494. }
  3495. // sold_by_qty : Sold in quantities of %@
  3496. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  3497. NSString *qty = params[@"sold_by_qty"];
  3498. if ([qty isEqualToString:@"Display All"]) {
  3499. qty = @"";
  3500. } else {
  3501. qty = [self translateSingleQuote:qty];
  3502. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  3503. }
  3504. // cate
  3505. category = [self translateSingleQuote:category];
  3506. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  3507. // where bestseller > 0 order by bestseller desc
  3508. // sql query: alert availability(int) best_seller(int) price qty
  3509. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id order by %@ limit %d offset %d;",cateWhere,best_seller,alert,available_condition,qty,price,order_best_seller,limit,offset];
  3510. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  3511. }
  3512. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  3513. if (!appDelegate.user) {
  3514. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  3515. }
  3516. [ret setValue:filter forKey:@"filter"];
  3517. DebugLog(@"offline_category sql:%@",sqlQuery);
  3518. sqlite3_stmt * statement;
  3519. [ret setValue:@"2" forKey:@"result"];
  3520. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3521. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3522. // int count=0;
  3523. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3524. {
  3525. int i=0;
  3526. while (sqlite3_step(statement) == SQLITE_ROW)
  3527. {
  3528. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3529. char *name = (char*)sqlite3_column_text(statement, 0);
  3530. if(name==nil)
  3531. name="";
  3532. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3533. char *description = (char*)sqlite3_column_text(statement, 1);
  3534. if(description==nil)
  3535. description="";
  3536. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3537. int product_id = sqlite3_column_int(statement, 2);
  3538. int wid = sqlite3_column_int(statement, 3);
  3539. int closeout = sqlite3_column_int(statement, 4);
  3540. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3541. if(wid==0)
  3542. [item setValue:@"false" forKey:@"wish_exists"];
  3543. else
  3544. [item setValue:@"true" forKey:@"wish_exists"];
  3545. if(closeout==0)
  3546. [item setValue:@"false" forKey:@"is_closeout"];
  3547. else
  3548. [item setValue:@"true" forKey:@"is_closeout"];
  3549. [item addEntriesFromDictionary:imgjson];
  3550. // [item setValue:nsurl forKey:@"img"];
  3551. [item setValue:nsname forKey:@"name"];
  3552. [item setValue:nsdescription forKey:@"description"];
  3553. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3554. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3555. i++;
  3556. }
  3557. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3558. [ret setObject:items forKey:@"items"];
  3559. sqlite3_finalize(statement);
  3560. } else {
  3561. DebugLog(@"nothing...");
  3562. }
  3563. DebugLog(@"count:%d",count);
  3564. [iSalesDB close_db:db];
  3565. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3566. return ret;
  3567. }
  3568. # pragma mark item search
  3569. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  3570. {
  3571. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3572. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  3573. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  3574. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  3575. // category
  3576. NSDictionary *category_menu = [self offline_category_menu];
  3577. [filter setValue:category_menu forKey:@"category"];
  3578. NSString* where= nil;
  3579. NSString* orderby= @"m.name";
  3580. if (!filterSearch) {
  3581. int covertype = [[params valueForKey:@"covertype"] intValue];
  3582. switch (covertype) {
  3583. case 0:
  3584. {
  3585. where=@"m.category like'%%#005#%%'";
  3586. break;
  3587. }
  3588. case 1:
  3589. {
  3590. where=@"m.alert like '%QS%'";
  3591. break;
  3592. }
  3593. case 2:
  3594. {
  3595. where=@"m.availability>0";
  3596. break;
  3597. }
  3598. case 3:
  3599. {
  3600. where=@"m.best_seller>0";
  3601. orderby=@"m.best_seller desc,m.name asc";
  3602. break;
  3603. }
  3604. default:
  3605. where=@"1=1";
  3606. break;
  3607. }
  3608. }
  3609. int limit = [[params valueForKey:@"limit"] intValue];
  3610. int offset = [[params valueForKey:@"offset"] intValue];
  3611. sqlite3 *db = [iSalesDB get_db];
  3612. // [iSalesDB AddExFunction:db];
  3613. int count;
  3614. NSString *sqlQuery = nil;
  3615. where = [where stringByAppendingString:@" and m.is_active = 1"];
  3616. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from product m left join wishlist w on m.product_id=w.product_id where %@ order by %@ limit %d offset %d ;",where,orderby,limit, offset];
  3617. double price_min = 0;
  3618. double price_max = 0;
  3619. if (filterSearch) {
  3620. // alert
  3621. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  3622. NSString *alert = params[@"alert"];
  3623. if ([alert isEqualToString:@"Display All"]) {
  3624. alert = [NSString stringWithFormat:@""];
  3625. } else {
  3626. alert = [self translateSingleQuote:alert];
  3627. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  3628. }
  3629. // available
  3630. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  3631. NSString *available = params[@"available"];
  3632. NSString *available_condition;
  3633. if ([available isEqualToString:@"Display All"]) {
  3634. available_condition = @"";
  3635. } else if ([available isEqualToString:@"Available Now"]) {
  3636. available_condition = @"and availability > 0";
  3637. } else {
  3638. available_condition = @"and availability == 0";
  3639. }
  3640. // best seller
  3641. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  3642. NSString *best_seller = @"";
  3643. NSString *order_best_seller = @"m.name asc";
  3644. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  3645. best_seller = @"and best_seller > 0";
  3646. order_best_seller = @"m.best_seller desc,m.name asc";
  3647. }
  3648. // price
  3649. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  3650. NSString *price = params[@"price"];
  3651. price_min = 0;
  3652. price_max = MAXFLOAT;
  3653. if (appDelegate.user) {
  3654. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  3655. NSMutableString *priceName = [NSMutableString string];
  3656. for (int i = 0; i < priceTypeArray.count; i++) {
  3657. NSString *pricetype = priceTypeArray[i];
  3658. pricetype = [self translateSingleQuote:pricetype];
  3659. if (i == 0) {
  3660. [priceName appendFormat:@"'%@'",pricetype];
  3661. } else {
  3662. [priceName appendFormat:@",'%@'",pricetype];
  3663. }
  3664. }
  3665. if ([price isEqualToString:@"Display All"]) {
  3666. price = [NSString stringWithFormat:@""];
  3667. } else if([price containsString:@"+"]){
  3668. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  3669. price_min = [price doubleValue];
  3670. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  3671. } else {
  3672. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  3673. price_min = [[priceArray objectAtIndex:0] doubleValue];
  3674. price_max = [[priceArray objectAtIndex:1] doubleValue];
  3675. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  3676. }
  3677. } else {
  3678. price = @"";
  3679. }
  3680. // sold_by_qty : Sold in quantities of %@
  3681. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  3682. NSString *qty = params[@"sold_by_qty"];
  3683. if ([qty isEqualToString:@"Display All"]) {
  3684. qty = @"";
  3685. } else {
  3686. qty = [self translateSingleQuote:qty];
  3687. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  3688. }
  3689. // category
  3690. NSString *category_id = params[@"ctgId"];
  3691. NSMutableArray *cate_id_array = nil;
  3692. NSMutableString *cateWhere = [NSMutableString string];
  3693. if ([category_id isEqualToString:@""] || !category_id) {
  3694. [cateWhere appendString:@"1 = 1"];
  3695. } else {
  3696. if ([category_id containsString:@","]) {
  3697. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  3698. } else {
  3699. cate_id_array = [@[category_id] mutableCopy];
  3700. }
  3701. /*-----------*/
  3702. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  3703. for (int i = 0; i < cate_id_array.count; i++) {
  3704. for (NSString *key0 in cateDic.allKeys) {
  3705. if ([key0 containsString:@"category_"]) {
  3706. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  3707. for (NSString *key1 in category0.allKeys) {
  3708. if ([key1 containsString:@"category_"]) {
  3709. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  3710. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  3711. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  3712. cate_id_array[i] = [category1 objectForKey:@"id"];
  3713. if (i == 0) {
  3714. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  3715. } else {
  3716. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  3717. }
  3718. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3719. [category0 setValue:category1 forKey:key1];
  3720. [cateDic setValue:category0 forKey:key0];
  3721. }
  3722. }
  3723. }
  3724. }
  3725. }
  3726. }
  3727. [filter setValue:cateDic forKey:@"category"];
  3728. }
  3729. // where bestseller > 0 order by bestseller desc
  3730. // sql query: alert availability(int) best_seller(int) price qty
  3731. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id order by %@ limit %d offset %d;",cateWhere,best_seller,alert,available_condition,qty,price,order_best_seller,limit,offset];
  3732. // count
  3733. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  3734. }
  3735. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  3736. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  3737. if (!appDelegate.user) {
  3738. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  3739. }
  3740. [ret setValue:filter forKey:@"filter"];
  3741. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  3742. sqlite3_stmt * statement;
  3743. [ret setValue:@"2" forKey:@"result"];
  3744. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3745. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3746. // int count=0;
  3747. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3748. {
  3749. int i=0;
  3750. while (sqlite3_step(statement) == SQLITE_ROW)
  3751. {
  3752. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3753. char *name = (char*)sqlite3_column_text(statement, 0);
  3754. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3755. char *description = (char*)sqlite3_column_text(statement, 1);
  3756. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3757. int product_id = sqlite3_column_int(statement, 2);
  3758. int wid = sqlite3_column_int(statement, 3);
  3759. int closeout = sqlite3_column_int(statement, 4);
  3760. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3761. if(wid==0)
  3762. [item setValue:@"false" forKey:@"wish_exists"];
  3763. else
  3764. [item setValue:@"true" forKey:@"wish_exists"];
  3765. if(closeout==0)
  3766. [item setValue:@"false" forKey:@"is_closeout"];
  3767. else
  3768. [item setValue:@"true" forKey:@"is_closeout"];
  3769. [item addEntriesFromDictionary:imgjson];
  3770. // [item setValue:nsurl forKey:@"img"];
  3771. [item setValue:nsname forKey:@"fash_name"];
  3772. [item setValue:nsdescription forKey:@"description"];
  3773. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3774. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3775. i++;
  3776. }
  3777. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3778. [ret setObject:items forKey:@"items"];
  3779. sqlite3_finalize(statement);
  3780. }
  3781. [iSalesDB close_db:db];
  3782. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3783. return ret;
  3784. }
  3785. #pragma mark order detail
  3786. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  3787. if (str1.length == 0) {
  3788. str1 = @"&nbsp";
  3789. }
  3790. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  3791. return str;
  3792. }
  3793. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  3794. {
  3795. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  3796. [fromformatter setDateFormat:from];
  3797. NSDate *date = [fromformatter dateFromString:datetime];
  3798. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  3799. [toformatter setDateFormat:to];
  3800. NSString * ret = [toformatter stringFromDate:date];
  3801. return ret;
  3802. }
  3803. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  3804. // 把毫秒去掉
  3805. if ([dateTime containsString:@"."]) {
  3806. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  3807. }
  3808. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  3809. formatter.dateFormat = formate;
  3810. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  3811. NSDate *date = [formatter dateFromString:dateTime];
  3812. formatter.dateFormat = newFormate;
  3813. return [formatter stringFromDate:date];
  3814. }
  3815. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  3816. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  3817. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  3818. }
  3819. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  3820. {
  3821. DebugLog(@"offline oderdetail params: %@",params);
  3822. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3823. int orderId = [params[@"orderId"] intValue];
  3824. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  3825. // decrypt card number and card security code
  3826. // NSString* sql=[NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,o.TotalCuft,o.TotalWeight,o.TotalCarton,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),o.credit_card_expiration,o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus from (select _id,so_id,poNumber,create_time,status,TotalCuft,TotalWeight,TotalCarton,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId ];
  3827. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  3828. sqlite3 *db = [iSalesDB get_db];
  3829. sqlite3_stmt * statement;
  3830. NSString *customerID = nil; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  3831. NSString *nssoid = nil;
  3832. NSString* orderinfo = nil;
  3833. NSString *moreInfo = nil;
  3834. double handlingFee = 0;
  3835. double payments_and_credist = 0;
  3836. double totalPrice = 0;
  3837. double shippingFee = 0;
  3838. double lift_gate = 0;
  3839. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  3840. {
  3841. if (sqlite3_step(statement) == SQLITE_ROW)
  3842. {
  3843. int order_id = sqlite3_column_int(statement, 0);
  3844. NSString *sign_url = [self textAtColumn:51 statement:statement];
  3845. ret[@"sign_url"] = sign_url;
  3846. char *soid = (char*)sqlite3_column_text(statement, 1);
  3847. if(soid==nil)
  3848. soid= "";
  3849. nssoid= [[NSString alloc]initWithUTF8String:soid];
  3850. // so#
  3851. ret[@"so#"] = nssoid;
  3852. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  3853. if(poNumber==nil)
  3854. poNumber= "";
  3855. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  3856. char *create_time = (char*)sqlite3_column_text(statement, 3);
  3857. if(create_time==nil)
  3858. create_time= "";
  3859. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  3860. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  3861. int status = sqlite3_column_int(statement, 4);
  3862. int erpStatus = sqlite3_column_int(statement, 49);
  3863. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  3864. // status
  3865. if (status > 1 && status != 3) {
  3866. status = erpStatus;
  3867. } else if (status == 3) {
  3868. status = 15;
  3869. }
  3870. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  3871. ret[@"order_status"] = nsstatus;
  3872. char *company_name = (char*)sqlite3_column_text(statement, 5);
  3873. if(company_name==nil)
  3874. company_name= "";
  3875. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  3876. // company name
  3877. ret[@"company_name"] = nscompany_name;
  3878. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  3879. if(customer_contact==nil)
  3880. customer_contact= "";
  3881. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  3882. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  3883. if(addr_1==nil)
  3884. addr_1="";
  3885. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3886. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  3887. if(addr_2==nil)
  3888. addr_2="";
  3889. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3890. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  3891. if(addr_3==nil)
  3892. addr_3="";
  3893. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3894. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  3895. if(addr_4==nil)
  3896. addr_4="";
  3897. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3898. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  3899. [arr_addr addObject:nsaddr_1];
  3900. [arr_addr addObject:nsaddr_2];
  3901. [arr_addr addObject:nsaddr_3];
  3902. [arr_addr addObject:nsaddr_4];
  3903. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  3904. char *logist = (char*)sqlite3_column_text(statement, 11);
  3905. if(logist==nil)
  3906. logist= "";
  3907. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  3908. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  3909. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  3910. shipping = @"Shipping To Be Quoted";
  3911. } else {
  3912. shippingFee = sqlite3_column_double(statement, 12);
  3913. }
  3914. // Shipping
  3915. ret[@"Shipping"] = shipping;
  3916. int have_lift_gate = sqlite3_column_int(statement, 17);
  3917. lift_gate = sqlite3_column_double(statement, 13);
  3918. // Liftgate Fee(No loading dock)
  3919. if (!have_lift_gate) {
  3920. lift_gate = 0;
  3921. }
  3922. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  3923. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  3924. if(general_notes==nil)
  3925. general_notes= "";
  3926. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  3927. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  3928. if(internal_notes==nil)
  3929. internal_notes= "";
  3930. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  3931. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  3932. if(payment_type==nil)
  3933. payment_type= "";
  3934. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  3935. // order info
  3936. orderinfo = [self textFileName:@"order_info.html"];
  3937. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  3938. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  3939. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  3940. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  3941. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  3942. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  3943. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  3944. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  3945. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  3946. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  3947. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  3948. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  3949. NSString *payment = nil;
  3950. if([nspayment_type isEqualToString:@"Credit Card"])
  3951. {
  3952. payment = [self textFileName:@"creditcardpayment.html"];
  3953. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  3954. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  3955. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  3956. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  3957. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  3958. NSString *card_type = [self textAtColumn:42 statement:statement];
  3959. if (card_type.length > 0) { // 显示星号
  3960. card_type = @"****";
  3961. }
  3962. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  3963. if (card_number.length > 0 && card_number.length > 4) {
  3964. for (int i = 0; i < card_number.length - 4; i++) {
  3965. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  3966. }
  3967. } else {
  3968. card_number = @"";
  3969. }
  3970. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  3971. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  3972. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  3973. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  3974. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  3975. card_expiration = @"****";
  3976. }
  3977. NSString *card_city = [self textAtColumn:46 statement:statement];
  3978. NSString *card_state = [self textAtColumn:47 statement:statement];
  3979. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  3980. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  3981. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  3982. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  3983. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  3984. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  3985. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  3986. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  3987. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  3988. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  3989. }
  3990. else
  3991. {
  3992. payment=[self textFileName:@"normalpayment.html"];
  3993. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  3994. }
  3995. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  3996. ret[@"result"]= [NSNumber numberWithInt:2];
  3997. // more info
  3998. moreInfo = [self textFileName:@"more_info.html"];
  3999. /*****ship to******/
  4000. // ShipToCompany_or_&nbsp
  4001. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  4002. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  4003. NSString *shipToName = [self textAtColumn:19 statement:statement];
  4004. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  4005. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  4006. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  4007. /*****ship from******/
  4008. // ShipFromCompany_or_&nbsp
  4009. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  4010. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  4011. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  4012. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  4013. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  4014. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  4015. /*****freight to******/
  4016. // FreightBillToCompany_or_&nbsp
  4017. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  4018. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  4019. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  4020. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  4021. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  4022. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  4023. /*****merchandise to******/
  4024. // MerchandiseBillToCompany_or_&nbsp
  4025. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  4026. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  4027. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  4028. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  4029. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  4030. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  4031. /*****return to******/
  4032. // ReturnToCompany_or_&nbsp
  4033. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  4034. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  4035. NSString *returnToName = [self textAtColumn:31 statement:statement];
  4036. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  4037. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  4038. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  4039. //
  4040. DebugLog(@"more info : %@",moreInfo);
  4041. // handling fee
  4042. handlingFee = sqlite3_column_double(statement, 33);
  4043. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  4044. // customer info
  4045. customerID = [self textAtColumn:36 statement:statement];
  4046. // mode
  4047. ret[@"mode"] = appDelegate.mode;
  4048. // model_count
  4049. ret[@"model_count"] = @(0);
  4050. }
  4051. sqlite3_finalize(statement);
  4052. }
  4053. [iSalesDB close_db:db];
  4054. NSDictionary *contactInfo = [self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]];
  4055. ret[@"customerInfo"] = contactInfo;
  4056. // models
  4057. if (nssoid) {
  4058. __block double TotalCuft = 0;
  4059. __block double TotalWeight = 0;
  4060. __block int TotalCarton = 0;
  4061. __block double allItemPrice = 0;
  4062. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  4063. sqlite3 *db1 = [iSalesDB get_db];
  4064. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4065. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4066. int product_id = sqlite3_column_int(stmt, 0);
  4067. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4068. int item_id = sqlite3_column_int(stmt, 7);
  4069. NSString* Price=nil;
  4070. if(str_price==nil)
  4071. {
  4072. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4073. if(price==nil)
  4074. Price=@"No Price.";
  4075. else
  4076. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4077. }
  4078. else
  4079. {
  4080. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4081. }
  4082. double discount = sqlite3_column_double(stmt, 2);
  4083. int item_count = sqlite3_column_int(stmt, 3);
  4084. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  4085. NSString *nsline_note=nil;
  4086. if(line_note!=nil)
  4087. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4088. // char *name = (char*)sqlite3_column_text(stmt, 5);
  4089. // NSString *nsname = nil;
  4090. // if(name!=nil)
  4091. // nsname= [[NSString alloc]initWithUTF8String:name];
  4092. NSString *nsname = [self textAtColumn:5 statement:stmt];
  4093. // char *description = (char*)sqlite3_column_text(stmt, 6);
  4094. // NSString *nsdescription=nil;
  4095. // if(description!=nil)
  4096. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4097. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  4098. // int stockUom = sqlite3_column_int(stmt, 8);
  4099. // int _id = sqlite3_column_int(stmt, 9);
  4100. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  4101. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4102. double weight=[bsubtotaljson[@"weight"] doubleValue];
  4103. int carton=[bsubtotaljson[@"carton"] intValue];
  4104. TotalCuft += cuft;
  4105. TotalWeight += weight;
  4106. TotalCarton += carton;
  4107. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4108. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  4109. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  4110. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4111. itemjson[@"note"]=nsline_note;
  4112. itemjson[@"origin_price"] = Price;
  4113. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4114. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4115. itemjson[@"order_item_status"] = @""; // 暂时不处理
  4116. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4117. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4118. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  4119. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4120. if(itemjson[@"combine"] != nil)
  4121. {
  4122. // int citem=0;
  4123. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4124. for(int bc=0;bc<bcount;bc++)
  4125. {
  4126. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4127. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4128. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4129. subTotal += uprice * modulus * item_count;
  4130. }
  4131. }
  4132. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4133. NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  4134. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  4135. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  4136. allItemPrice += subTotal;
  4137. }];
  4138. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  4139. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  4140. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  4141. // payments/Credits
  4142. // payments_and_credist = sqlite3_column_double(statement, 34);
  4143. payments_and_credist = allItemPrice;
  4144. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4145. // // total
  4146. // totalPrice = sqlite3_column_double(statement, 35);
  4147. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4148. } else {
  4149. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  4150. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  4151. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  4152. // payments/Credits
  4153. payments_and_credist = 0;
  4154. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4155. }
  4156. // total
  4157. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4158. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4159. ret[@"order_info"]= orderinfo;
  4160. ret[@"more_order_info"] = moreInfo;
  4161. return [RAUtils dict2data:ret];
  4162. }
  4163. #pragma mark order list
  4164. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  4165. NSString *nsstatus = @"";
  4166. switch (status) {
  4167. case 0:
  4168. {
  4169. nsstatus=@"Temp Order";
  4170. break;
  4171. }
  4172. case 1:
  4173. {
  4174. nsstatus=@"Saved Order";
  4175. break;
  4176. }
  4177. case 2: {
  4178. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  4179. break;
  4180. }
  4181. case 3:
  4182. case 15:
  4183. {
  4184. nsstatus=@"Cancelled";
  4185. break;
  4186. }
  4187. case 10:
  4188. {
  4189. nsstatus=@"Quote Submitted";
  4190. break;
  4191. }
  4192. case 11:
  4193. {
  4194. nsstatus=@"Sales Order Submitted";
  4195. break;
  4196. }
  4197. case 12:
  4198. {
  4199. nsstatus=@"Processing";
  4200. break;
  4201. }
  4202. case 13:
  4203. {
  4204. nsstatus=@"Shipped";
  4205. break;
  4206. }
  4207. case 14:
  4208. {
  4209. nsstatus=@"Closed";
  4210. break;
  4211. }
  4212. default:
  4213. break;
  4214. }
  4215. return nsstatus;
  4216. }
  4217. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  4218. double total = 0;
  4219. __block double payments_and_credist = 0;
  4220. __block double allItemPrice = 0;
  4221. // sqlite3 *db1 = [iSalesDB get_db];
  4222. if (so_id) {
  4223. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",so_id];
  4224. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4225. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4226. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4227. int product_id = sqlite3_column_int(stmt, 0);
  4228. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4229. int item_id = sqlite3_column_int(stmt, 7);
  4230. NSString* Price=nil;
  4231. if(str_price==nil)
  4232. {
  4233. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4234. if(price==nil)
  4235. Price=@"No Price.";
  4236. else
  4237. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4238. }
  4239. else
  4240. {
  4241. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4242. }
  4243. int discount = sqlite3_column_int(stmt, 2);
  4244. int item_count = sqlite3_column_int(stmt, 3);
  4245. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4246. itemjson[@"The unit price"]=Price;
  4247. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4248. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4249. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4250. if(itemjson[@"combine"] != nil)
  4251. {
  4252. // int citem=0;
  4253. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4254. for(int bc=0;bc<bcount;bc++)
  4255. {
  4256. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4257. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4258. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4259. subTotal += uprice * modulus * item_count;
  4260. }
  4261. }
  4262. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4263. allItemPrice += subTotal;
  4264. }];
  4265. payments_and_credist = allItemPrice;
  4266. } else {
  4267. // payments/Credits
  4268. payments_and_credist = 0;
  4269. }
  4270. // lift_gate handlingFee shippingFee
  4271. __block double lift_gate = 0;
  4272. __block double handlingFee = 0;
  4273. __block double shippingFee = 0;
  4274. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  4275. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4276. int have_lift_gate = sqlite3_column_int(stmt, 0);
  4277. if (have_lift_gate) {
  4278. lift_gate = sqlite3_column_double(stmt, 1);
  4279. }
  4280. handlingFee = sqlite3_column_double(stmt, 2);
  4281. shippingFee = sqlite3_column_double(stmt, 3);
  4282. }];
  4283. // total
  4284. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4285. if (close) {
  4286. [iSalesDB close_db:db1];
  4287. }
  4288. return total;
  4289. }
  4290. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  4291. {
  4292. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4293. int limit = [[params valueForKey:@"limit"] intValue];
  4294. int offset = [[params valueForKey:@"offset"] intValue];
  4295. NSString* keyword = [params valueForKey:@"keyWord"];
  4296. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  4297. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  4298. NSString* where=@"1 = 1";
  4299. if(keyword.length>0)
  4300. where=[where stringByAppendingString:[NSString stringWithFormat:@" and (lower(o.so_id) like '%%%@%%' or lower(o.sales_rep) like '%%%@%%' or lower(decrypt(c.company_name)) like '%%%@%%' or lower(o.create_by) like '%%%@%%')",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString]];
  4301. if (orderStatus.length > 0) {
  4302. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  4303. if (order_status_array.count == 1) {
  4304. int status_value = [[order_status_array firstObject] integerValue];
  4305. if (status_value <= 1 || status_value == 3) {
  4306. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  4307. } else {
  4308. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  4309. }
  4310. } else if (order_status_array.count > 1) {
  4311. for (int i = 0; i < order_status_array.count;i++) {
  4312. NSString *status = order_status_array[i];
  4313. NSString *condition = @" or";
  4314. if (i == 0) {
  4315. condition = @" and (";
  4316. }
  4317. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  4318. int status_value = [status integerValue];
  4319. if (status_value <= 1 || status_value == 3) {
  4320. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  4321. } else {
  4322. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  4323. }
  4324. }
  4325. where = [where stringByAppendingString:@" )"];
  4326. }
  4327. }
  4328. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price,o.customer_contact,o.erpOrderStatus from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.create_time desc limit %d offset %d" ,where, limit, offset];
  4329. DebugLog(@"order list sql: %@",sqlQuery);
  4330. sqlite3 *db = [iSalesDB get_db];
  4331. sqlite3_stmt * statement;
  4332. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4333. {
  4334. int count=0;
  4335. while (sqlite3_step(statement) == SQLITE_ROW)
  4336. {
  4337. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  4338. int order_id = sqlite3_column_double(statement, 0);
  4339. char *soid = (char*)sqlite3_column_text(statement, 1);
  4340. if(soid==nil)
  4341. soid= "";
  4342. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  4343. int status = sqlite3_column_double(statement, 2);
  4344. int erpStatus = sqlite3_column_double(statement, 9);
  4345. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  4346. if(sales_rep==nil)
  4347. sales_rep= "";
  4348. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  4349. char *create_by = (char*)sqlite3_column_text(statement, 4);
  4350. if(create_by==nil)
  4351. create_by= "";
  4352. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  4353. char *company_name = (char*)sqlite3_column_text(statement, 5);
  4354. if(company_name==nil)
  4355. company_name= "";
  4356. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  4357. char *create_time = (char*)sqlite3_column_text(statement, 6);
  4358. if(create_time==nil)
  4359. create_time= "";
  4360. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  4361. DebugLog(@"time: %@",nscreate_time);
  4362. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  4363. // double total_price = sqlite3_column_double(statement, 7);
  4364. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  4365. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  4366. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  4367. // ": "JH",
  4368. // "": "$8307.00",
  4369. // "": "MOB1608050001",
  4370. // "": "ArpithaT",
  4371. // "": "1st Stage Property Transformations",
  4372. // "": "JANICE SUTTON",
  4373. // "": 2255,
  4374. // "": "08/02/2016 09:49:18",
  4375. // "": 1,
  4376. // "": "Saved Order"
  4377. // "": "1470384050483",
  4378. // "model_count": "6 / 28"
  4379. item[@"sales_rep"]= nssales_rep;
  4380. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  4381. item[@"so#"]= nssoid;
  4382. item[@"create_by"]= nscreate_by;
  4383. item[@"customer_name"]= nscompany_name;
  4384. item[@"customer_contact"] = customer_contact;
  4385. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  4386. item[@"purchase_time"]= nscreate_time;
  4387. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  4388. item[@"order_status"]= nsstatus;
  4389. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  4390. // item[@"model_count"]
  4391. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  4392. count++;
  4393. }
  4394. ret[@"count"]= [NSNumber numberWithInt:count];
  4395. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  4396. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  4397. ret[@"result"]= [NSNumber numberWithInt:2];
  4398. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  4399. ret[@"time_zone"] = @"PST";
  4400. sqlite3_finalize(statement);
  4401. }
  4402. int total_count = [iSalesDB get_recordcount:db table:@"offline_order o left join offline_contact c on o.customer_cid=c.contact_id" where:where];
  4403. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  4404. [iSalesDB close_db:db];
  4405. return [RAUtils dict2data:ret];
  4406. }
  4407. #pragma mark update gnotes
  4408. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  4409. {
  4410. DebugLog(@"params: %@",params);
  4411. // comments = Meyoyoyoyoyoyoy;
  4412. // orderCode = MOB1608110001;
  4413. // password = 123456;
  4414. // user = EvanK;
  4415. NSString *sql = [NSString stringWithFormat:@"update offline_order set general_notes = '%@' where so_id = '%@';",[self valueInParams:params key:@"comments"],[self valueInParams:params key:@"orderCode"]];
  4416. int ret = [iSalesDB execSql:sql];
  4417. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4418. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4419. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4420. // [dic setValue:@"160409" forKey:@"min_ver"];
  4421. return [RAUtils dict2data:dic];
  4422. }
  4423. #pragma mark move to wishlist
  4424. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  4425. {
  4426. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  4427. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4428. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id = %@;",orderCode,_id];
  4429. sqlite3 *db = [iSalesDB get_db];
  4430. __block NSString *product_id = nil;
  4431. __block int item_count = 0;
  4432. __block NSString *item_id = nil;
  4433. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4434. product_id = [self textAtColumn:0 statement:stmt];
  4435. item_count = sqlite3_column_int(stmt, 1);
  4436. item_id = [self textAtColumn:2 statement:stmt];
  4437. }];
  4438. //
  4439. // NSString *contactIdSql = [NSString stringWithFormat:@"select customer_cid from offline_order where so_id = '%@';",orderCode];
  4440. // __block NSString *contact_id = nil;
  4441. // [iSalesDB jk_query:contactIdSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4442. // contact_id = [self textAtColumn:0 statement:stmt];
  4443. // }];
  4444. NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  4445. int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  4446. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:nil];
  4447. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4448. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4449. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4450. // [dic setValue:@"160409" forKey:@"min_ver"];
  4451. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  4452. // 删除
  4453. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id = %@;",_id];
  4454. [iSalesDB execSql:deleteSql db:db];
  4455. [iSalesDB close_db:db];
  4456. return [RAUtils dict2data:dic];
  4457. }
  4458. #pragma mark cart delete
  4459. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  4460. {
  4461. // cartItemId = 548;
  4462. // orderCode = MOB1608110001;
  4463. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  4464. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4465. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where _id = %@ and so_no = '%@';",_id,orderCode];
  4466. int ret = [iSalesDB execSql:sql];
  4467. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4468. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4469. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4470. // [dic setValue:@"160409" forKey:@"min_ver"];
  4471. return [RAUtils dict2data:dic];
  4472. }
  4473. #pragma mark set price
  4474. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  4475. {
  4476. DebugLog(@"cart set price params: %@",params);
  4477. // "cartitem_id" = 1;
  4478. // discount = "0.000000";
  4479. // "item_note" = "";
  4480. // price = "269.000000";
  4481. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  4482. NSString *notes = [self valueInParams:params key:@"item_note"];
  4483. NSString *discount = [self valueInParams:params key:@"discount"];
  4484. NSString *price = [self valueInParams:params key:@"price"];
  4485. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  4486. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f where _id = %@",price,discount.doubleValue,_id];
  4487. int ret = [iSalesDB execSql:sql];
  4488. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4489. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4490. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4491. // [dic setValue:@"160409" forKey:@"min_ver"];
  4492. return [RAUtils dict2data:dic];
  4493. }
  4494. #pragma mark set line notes
  4495. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  4496. {
  4497. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  4498. NSString *notes = [self valueInParams:params key:@"notes"];
  4499. notes = [self translateSingleQuote:notes];
  4500. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  4501. int ret = [iSalesDB execSql:sql];
  4502. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4503. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4504. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4505. // [dic setValue:@"160409" forKey:@"min_ver"];
  4506. return [RAUtils dict2data:dic];
  4507. }
  4508. #pragma mark set qty
  4509. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  4510. {
  4511. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  4512. int item_count = [params[@"inputInt"] integerValue];
  4513. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  4514. int ret = [iSalesDB execSql:sql];
  4515. __block int item_id = 0;
  4516. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4517. item_id = sqlite3_column_int(stmt, 0);
  4518. }];
  4519. sqlite3 *db = [iSalesDB get_db];
  4520. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4521. [iSalesDB close_db:db];
  4522. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4523. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4524. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4525. // [dic setValue:@"160409" forKey:@"min_ver"];
  4526. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  4527. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  4528. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  4529. return [RAUtils dict2data:dic];
  4530. }
  4531. #pragma mark place order
  4532. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  4533. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4534. if (aname) {
  4535. [dic setValue:aname forKey:@"aname"];
  4536. }
  4537. if (control) {
  4538. [dic setValue:control forKey:@"control"];
  4539. }
  4540. if (keyboard) {
  4541. [dic setValue:keyboard forKey:@"keyboard"];
  4542. }
  4543. if (name) {
  4544. [dic setValue:name forKey:@"name"];
  4545. }
  4546. if (value) {
  4547. [dic setValue:value forKey:@"value"];
  4548. }
  4549. return dic;
  4550. }
  4551. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  4552. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4553. orderCode = [self translateSingleQuote:orderCode];
  4554. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  4555. NSString *customerSql = [NSString stringWithFormat:@"select c.contact_id,c.img_0,c.img_1,c.img_2,decrypt(c.company_name),decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,c.first_name,c.last_name,c.email,decrypt(c.phone),c.fax,c.zipcode,c.city,c.state,c.country from offline_contact as c join offline_order as o on c.contact_id = o.customer_cid where o.so_id = '%@';",orderCode];
  4556. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4557. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  4558. NSString *img0 = [self textAtColumn:1 statement:stmt];
  4559. NSString *img1 = [self textAtColumn:2 statement:stmt];
  4560. NSString *img2 = [self textAtColumn:3 statement:stmt];
  4561. NSString *company_name = [self textAtColumn:4 statement:stmt];
  4562. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  4563. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  4564. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  4565. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  4566. NSString *first_name = [self textAtColumn:9 statement:stmt];
  4567. NSString *last_name = [self textAtColumn:10 statement:stmt];
  4568. NSString *email = [self textAtColumn:11 statement:stmt];
  4569. NSString *phone = [self textAtColumn:12 statement:stmt];
  4570. NSString *fax = [self textAtColumn:13 statement:stmt];
  4571. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  4572. NSString *city = [self textAtColumn:15 statement:stmt];
  4573. NSString *state = [self textAtColumn:16 statement:stmt];
  4574. NSString *country = [self textAtColumn:17 statement:stmt];
  4575. NSString *name = [first_name stringByAppendingFormat:@" %@",last_name];
  4576. // contact id
  4577. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  4578. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  4579. [contact_id_dic setValue:@"text" forKey:@"control"];
  4580. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  4581. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  4582. [contact_id_dic setValue:@"true" forKey:@"required"];
  4583. [contact_id_dic setValue:contact_id forKey:@"value"];
  4584. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  4585. // business card
  4586. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  4587. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  4588. [business_card_dic setValue:@"img" forKey:@"control"];
  4589. [business_card_dic setValue:@"1" forKey:@"disable"];
  4590. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  4591. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  4592. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  4593. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  4594. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  4595. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  4596. [business_card_dic setValue:@"business_card" forKey:@"name"];
  4597. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  4598. // fax
  4599. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  4600. [customer_dic setValue:fax_dic forKey:@"item_10"];
  4601. // zipcode
  4602. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  4603. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  4604. // city
  4605. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  4606. [customer_dic setValue:city_dic forKey:@"item_12"];
  4607. // state
  4608. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  4609. [customer_dic setValue:state_dic forKey:@"item_13"];
  4610. // country
  4611. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  4612. [customer_dic setValue:country_dic forKey:@"item_14"];
  4613. // company name
  4614. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  4615. [customer_dic setValue:company_dic forKey:@"item_2"];
  4616. // addr_1
  4617. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  4618. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  4619. // addr_2
  4620. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  4621. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  4622. // addr_3
  4623. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  4624. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  4625. // addr_4
  4626. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  4627. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  4628. // Contact
  4629. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  4630. [customer_dic setValue:contact_dic forKey:@"item_7"];
  4631. // email
  4632. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  4633. [customer_dic setValue:email_dic forKey:@"item_8"];
  4634. // phone
  4635. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  4636. [customer_dic setValue:phone_dic forKey:@"item_9"];
  4637. // title
  4638. [customer_dic setValue:@"Customer" forKey:@"title"];
  4639. // count
  4640. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  4641. }];
  4642. return customer_dic;
  4643. }
  4644. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  4645. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4646. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4647. orderCode = [self translateSingleQuote:orderCode];
  4648. NSString *sql = [NSString stringWithFormat:@"select receive_cid,receive_name,receive_ext,receive_contact,receive_email,receive_fax,receive_phone from offline_order where so_id = '%@';",orderCode];
  4649. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4650. NSString *cid = [self textAtColumn:0 statement:stmt];
  4651. NSString *name = [self textAtColumn:1 statement:stmt];
  4652. NSString *ext = [self textAtColumn:2 statement:stmt];
  4653. NSString *contact = [self textAtColumn:3 statement:stmt];
  4654. NSString *email = [self textAtColumn:4 statement:stmt];
  4655. NSString *fax = [self textAtColumn:5 statement:stmt];
  4656. NSString *phone = [self textAtColumn:6 statement:stmt];
  4657. // count
  4658. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  4659. // title
  4660. [dic setValue:@"Ship To" forKey:@"title"];
  4661. // choose
  4662. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  4663. [choose_dic setValue:@"choose" forKey:@"aname"];
  4664. [choose_dic setValue:@"multi_action" forKey:@"control"];
  4665. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  4666. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  4667. @"key_map" : @{@"receive_cid" : @"customer_cid",
  4668. @"receive_contact" : @"customer_contact",
  4669. @"receive_email" : @"customer_email",
  4670. @"receive_ext" : @"customer_contact_ext",
  4671. @"receive_fax" : @"customer_fax",
  4672. @"receive_name" : @"customer_name",
  4673. @"receive_phone" : @"customer_phone"},
  4674. @"refresh" : [NSNumber numberWithInteger:1],
  4675. @"type" : @"pull"};
  4676. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  4677. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  4678. @"name" : @"Add new address",
  4679. @"refresh" : [NSNumber numberWithInteger:1],
  4680. @"value" : @"new_addr"
  4681. };
  4682. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  4683. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  4684. @"key_map" : @{@"receive_cid" : @"customer_cid",
  4685. @"receive_contact" : @"customer_contact",
  4686. @"receive_email" : @"customer_email",
  4687. @"receive_ext" : @"customer_contact_ext",
  4688. @"receive_fax" : @"customer_fax",
  4689. @"receive_name" : @"customer_name",
  4690. @"receive_phone" : @"customer_phone"},
  4691. @"name" : @"select_ship_to",
  4692. @"refresh" : [NSNumber numberWithInteger:1],
  4693. @"value" : @"Sales_Order_Ship_To"};
  4694. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  4695. [dic setValue:choose_dic forKey:@"item_0"];
  4696. // contact id
  4697. NSDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  4698. [contact_id_dic setValue:@"1" forKey:@"disable"];
  4699. [contact_id_dic setValue:@"true" forKey:@"required"];
  4700. [dic setValue:contact_id_dic forKey:@"item_1"];
  4701. // company name
  4702. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  4703. [dic setValue:company_name_dic forKey:@"item_2"];
  4704. // address
  4705. NSDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  4706. [dic setValue:ext_dic forKey:@"item_3"];
  4707. // contact
  4708. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  4709. [dic setValue:contact_dic forKey:@"item_4"];
  4710. // phone
  4711. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  4712. [dic setValue:phone_dic forKey:@"item_5"];
  4713. // fax
  4714. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  4715. [dic setValue:fax_dic forKey:@"item_6"];
  4716. // email
  4717. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  4718. [dic setValue:email_dic forKey:@"item_7"];
  4719. }];
  4720. return dic;
  4721. }
  4722. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  4723. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4724. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4725. orderCode = [self translateSingleQuote:orderCode];
  4726. NSString *sql = [NSString stringWithFormat:@"select sender_cid,sender_name,sender_ext,sender_contact,sender_email,sender_fax,sender_phone from offline_order where so_id = '%@';",orderCode];
  4727. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4728. NSString *cid = [self textAtColumn:0 statement:stmt];
  4729. NSString *name = [self textAtColumn:1 statement:stmt];
  4730. NSString *ext = [self textAtColumn:2 statement:stmt];
  4731. NSString *contact = [self textAtColumn:3 statement:stmt];
  4732. NSString *email = [self textAtColumn:4 statement:stmt];
  4733. NSString *fax = [self textAtColumn:5 statement:stmt];
  4734. NSString *phone = [self textAtColumn:6 statement:stmt];
  4735. // count
  4736. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  4737. // title
  4738. [dic setValue:@"Ship From" forKey:@"title"];
  4739. // hide
  4740. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  4741. // choose
  4742. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  4743. [choose_dic setValue:@"choose" forKey:@"aname"];
  4744. [choose_dic setValue:@"multi_action" forKey:@"control"];
  4745. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  4746. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  4747. @"key_map" : @{@"sender_cid" : @"customer_cid",
  4748. @"sender_contact" : @"customer_contact",
  4749. @"sender_email" : @"customer_email",
  4750. @"sender_ext" : @"customer_contact_ext",
  4751. @"sender_fax" : @"customer_fax",
  4752. @"sender_name" : @"customer_name",
  4753. @"sender_phone" : @"customer_phone"},
  4754. @"name" : @"select_cid",
  4755. @"refresh" : [NSNumber numberWithInteger:0],
  4756. @"value" : @"Sales_Order_Ship_From"};
  4757. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  4758. [dic setValue:choose_dic forKey:@"item_0"];
  4759. // contact id
  4760. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  4761. [contact_id_dic setValue:@"1" forKey:@"disable"];
  4762. [contact_id_dic setValue:@"true" forKey:@"required"];
  4763. [dic setValue:contact_id_dic forKey:@"item_1"];
  4764. // company name
  4765. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  4766. [dic setValue:company_name_dic forKey:@"item_2"];
  4767. // address
  4768. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  4769. [dic setValue:ext_dic forKey:@"item_3"];
  4770. // contact
  4771. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  4772. [dic setValue:contact_dic forKey:@"item_4"];
  4773. // phone
  4774. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  4775. [dic setValue:phone_dic forKey:@"item_5"];
  4776. // fax
  4777. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  4778. [dic setValue:fax_dic forKey:@"item_6"];
  4779. // email
  4780. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  4781. [dic setValue:email_dic forKey:@"item_7"];
  4782. }];
  4783. return dic;
  4784. }
  4785. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  4786. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4787. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4788. orderCode = [self translateSingleQuote:orderCode];
  4789. NSString *sql = [NSString stringWithFormat:@"select shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_email,shipping_billto_fax,shipping_billto_phone from offline_order where so_id = '%@';",orderCode];
  4790. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4791. NSString *cid = [self textAtColumn:0 statement:stmt];
  4792. NSString *name = [self textAtColumn:1 statement:stmt];
  4793. NSString *ext = [self textAtColumn:2 statement:stmt];
  4794. NSString *contact = [self textAtColumn:3 statement:stmt];
  4795. NSString *email = [self textAtColumn:4 statement:stmt];
  4796. NSString *fax = [self textAtColumn:5 statement:stmt];
  4797. NSString *phone = [self textAtColumn:6 statement:stmt];
  4798. // count
  4799. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  4800. // title
  4801. [dic setValue:@"Freight Bill To" forKey:@"title"];
  4802. // hide
  4803. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  4804. // choose
  4805. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  4806. [choose_dic setValue:@"choose" forKey:@"aname"];
  4807. [choose_dic setValue:@"multi_action" forKey:@"control"];
  4808. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  4809. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  4810. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  4811. @"shipping_billto_contact" : @"receive_contact",
  4812. @"shipping_billto_email" : @"receive_email",
  4813. @"shipping_billto_ext" : @"receive_ext",
  4814. @"shipping_billto_fax" : @"receive_fax",
  4815. @"shipping_billto_name" : @"receive_name",
  4816. @"shipping_billto_phone" : @"receive_phone"},
  4817. @"type" : @"pull"};
  4818. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  4819. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  4820. @"type" : @"pull",
  4821. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  4822. @"shipping_billto_contact" : @"customer_contact",
  4823. @"shipping_billto_email" : @"customer_email",
  4824. @"shipping_billto_ext" : @"customer_contact_ext",
  4825. @"shipping_billto_fax" : @"customer_fax",
  4826. @"shipping_billto_name" : @"customer_name",
  4827. @"shipping_billto_phone" : @"customer_phone"}
  4828. };
  4829. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  4830. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  4831. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  4832. @"shipping_billto_contact" : @"sender_contact",
  4833. @"shipping_billto_email" : @"sender_email",
  4834. @"shipping_billto_ext" : @"sender_ext",
  4835. @"shipping_billto_fax" : @"sender_fax",
  4836. @"shipping_billto_name" : @"sender_name",
  4837. @"shipping_billto_phone" : @"sender_phone"},
  4838. @"type" : @"pull"
  4839. };
  4840. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  4841. NSDictionary *select_freight_bill_to_dic = @{
  4842. @"aname" : @"Select freight bill to",
  4843. @"name" : @"select_cid",
  4844. @"refresh" : [NSNumber numberWithInteger:0],
  4845. @"value" : @"Sales_Order_Freight_Bill_To",
  4846. @"key_map" : @{
  4847. @"shipping_billto_cid" : @"customer_cid",
  4848. @"shipping_billto_contact" : @"customer_contact",
  4849. @"shipping_billto_email" : @"customer_email",
  4850. @"shipping_billto_ext" : @"customer_contact_ext",
  4851. @"shipping_billto_fax" : @"customer_fax",
  4852. @"shipping_billto_name" : @"customer_name",
  4853. @"shipping_billto_phone" : @"customer_phone"
  4854. }
  4855. };
  4856. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  4857. [dic setValue:choose_dic forKey:@"item_0"];
  4858. // contact id
  4859. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  4860. [contact_id_dic setValue:@"1" forKey:@"disable"];
  4861. [contact_id_dic setValue:@"true" forKey:@"required"];
  4862. [dic setValue:contact_id_dic forKey:@"item_1"];
  4863. // company name
  4864. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  4865. [dic setValue:company_name_dic forKey:@"item_2"];
  4866. // address
  4867. NSDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  4868. [dic setValue:ext_dic forKey:@"item_3"];
  4869. // contact
  4870. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  4871. [dic setValue:contact_dic forKey:@"item_4"];
  4872. // phone
  4873. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  4874. [dic setValue:phone_dic forKey:@"item_5"];
  4875. // fax
  4876. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  4877. [dic setValue:fax_dic forKey:@"item_6"];
  4878. // email
  4879. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  4880. [dic setValue:email_dic forKey:@"item_7"];
  4881. }];
  4882. return dic;
  4883. }
  4884. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  4885. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4886. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4887. orderCode = [self translateSingleQuote:orderCode];
  4888. NSString *sql = [NSString stringWithFormat:@"select billing_cid,billing_name,billing_ext,billing_contact,billing_email,billing_fax,billing_phone from offline_order where so_id = '%@';",orderCode];
  4889. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4890. NSString *cid = [self textAtColumn:0 statement:stmt];
  4891. NSString *name = [self textAtColumn:1 statement:stmt];
  4892. NSString *ext = [self textAtColumn:2 statement:stmt];
  4893. NSString *contact = [self textAtColumn:3 statement:stmt];
  4894. NSString *email = [self textAtColumn:4 statement:stmt];
  4895. NSString *fax = [self textAtColumn:5 statement:stmt];
  4896. NSString *phone = [self textAtColumn:6 statement:stmt];
  4897. // count
  4898. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  4899. // title
  4900. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  4901. // hide
  4902. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  4903. // choose
  4904. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  4905. [choose_dic setValue:@"choose" forKey:@"aname"];
  4906. [choose_dic setValue:@"multi_action" forKey:@"control"];
  4907. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  4908. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  4909. @"key_map" : @{@"billing_cid" : @"receive_cid",
  4910. @"billing_contact" : @"receive_contact",
  4911. @"billing_email" : @"receive_email",
  4912. @"billing_ext" : @"receive_ext",
  4913. @"billing_fax" : @"receive_fax",
  4914. @"billing_name" : @"receive_name",
  4915. @"billing_phone" : @"receive_phone"},
  4916. @"type" : @"pull"};
  4917. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  4918. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  4919. @"type" : @"pull",
  4920. @"key_map" : @{@"billing_cid" : @"customer_cid",
  4921. @"billing_contact" : @"customer_contact",
  4922. @"billing_email" : @"customer_email",
  4923. @"billing_ext" : @"customer_contact_ext",
  4924. @"billing_fax" : @"customer_fax",
  4925. @"billing_name" : @"customer_name",
  4926. @"billing_phone" : @"customer_phone"}
  4927. };
  4928. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  4929. NSDictionary *select_bill_to_dic = @{
  4930. @"aname" : @"Select bill to",
  4931. @"name" : @"select_cid",
  4932. @"refresh" : [NSNumber numberWithInteger:0],
  4933. @"value" : @"Sales_Order_Merchandise_Bill_To",
  4934. @"key_map" : @{
  4935. @"billing_cid" : @"customer_cid",
  4936. @"billing_contact" : @"customer_contact",
  4937. @"billing_email" : @"customer_email",
  4938. @"billing_ext" : @"customer_contact_ext",
  4939. @"billing_fax" : @"customer_fax",
  4940. @"billing_name" : @"customer_name",
  4941. @"billing_phone" : @"customer_phone"
  4942. }
  4943. };
  4944. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  4945. [dic setValue:choose_dic forKey:@"item_0"];
  4946. // contact id
  4947. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  4948. [contact_id_dic setValue:@"1" forKey:@"disable"];
  4949. [contact_id_dic setValue:@"true" forKey:@"required"];
  4950. [dic setValue:contact_id_dic forKey:@"item_1"];
  4951. // company name
  4952. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  4953. [dic setValue:company_name_dic forKey:@"item_2"];
  4954. // address
  4955. NSDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  4956. [dic setValue:ext_dic forKey:@"item_3"];
  4957. // contact
  4958. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  4959. [dic setValue:contact_dic forKey:@"item_4"];
  4960. // phone
  4961. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  4962. [dic setValue:phone_dic forKey:@"item_5"];
  4963. // fax
  4964. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  4965. [dic setValue:fax_dic forKey:@"item_6"];
  4966. // email
  4967. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  4968. [dic setValue:email_dic forKey:@"item_7"];
  4969. }];
  4970. return dic;
  4971. }
  4972. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  4973. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4974. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4975. orderCode = [self translateSingleQuote:orderCode];
  4976. NSString *sql = [NSString stringWithFormat:@"select returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_email,returnto_fax,returnto_phone from offline_order where so_id = '%@';",orderCode];
  4977. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4978. NSString *cid = [self textAtColumn:0 statement:stmt];
  4979. NSString *name = [self textAtColumn:1 statement:stmt];
  4980. NSString *ext = [self textAtColumn:2 statement:stmt];
  4981. NSString *contact = [self textAtColumn:3 statement:stmt];
  4982. NSString *email = [self textAtColumn:4 statement:stmt];
  4983. NSString *fax = [self textAtColumn:5 statement:stmt];
  4984. NSString *phone = [self textAtColumn:6 statement:stmt];
  4985. // count
  4986. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  4987. // title
  4988. [dic setValue:@"Return To" forKey:@"title"];
  4989. // hide
  4990. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  4991. // choose
  4992. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  4993. [choose_dic setValue:@"choose" forKey:@"aname"];
  4994. [choose_dic setValue:@"multi_action" forKey:@"control"];
  4995. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  4996. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  4997. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  4998. @"returnto_contact" : @"sender_contact",
  4999. @"returnto_email" : @"sender_email",
  5000. @"returnto_ext" : @"sender_ext",
  5001. @"returnto_fax" : @"sender_fax",
  5002. @"returnto_name" : @"sender_name",
  5003. @"returnto_phone" : @"sender_phone"},
  5004. @"type" : @"pull"};
  5005. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  5006. NSDictionary *select_return_to_dic = @{
  5007. @"aname" : @"Select return to",
  5008. @"name" : @"select_cid",
  5009. @"refresh" : [NSNumber numberWithInteger:0],
  5010. @"value" : @"Contact_Return_To",
  5011. @"key_map" : @{
  5012. @"returnto_cid" : @"customer_cid",
  5013. @"returnto_contact" : @"customer_contact",
  5014. @"returnto_email" : @"customer_email",
  5015. @"returnto_ext" : @"customer_contact_ext",
  5016. @"returnto_fax" : @"customer_fax",
  5017. @"returnto_name" : @"customer_name",
  5018. @"returnto_phone" : @"customer_phone"
  5019. }
  5020. };
  5021. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  5022. [dic setValue:choose_dic forKey:@"item_0"];
  5023. // contact id
  5024. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5025. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5026. [contact_id_dic setValue:@"true" forKey:@"required"];
  5027. [dic setValue:contact_id_dic forKey:@"item_1"];
  5028. // company name
  5029. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5030. [dic setValue:company_name_dic forKey:@"item_2"];
  5031. // address
  5032. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5033. [dic setValue:ext_dic forKey:@"item_3"];
  5034. // contact
  5035. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5036. [dic setValue:contact_dic forKey:@"item_4"];
  5037. // phone
  5038. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5039. [dic setValue:phone_dic forKey:@"item_5"];
  5040. // fax
  5041. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5042. [dic setValue:fax_dic forKey:@"item_6"];
  5043. // email
  5044. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5045. [dic setValue:email_dic forKey:@"item_7"];
  5046. }];
  5047. return dic;
  5048. }
  5049. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  5050. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5051. orderCode = [self translateSingleQuote:orderCode];
  5052. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5053. __block double TotalCuft = 0;
  5054. __block double TotalWeight = 0;
  5055. __block int TotalCarton = 0;
  5056. __block double payments = 0;
  5057. NSString *sql = [NSString stringWithFormat:@"select c.item_id,c.item_count,m.stockUom,c.price,c.discount,m.name,m.description,c.line_note from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id;",orderCode];
  5058. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5059. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5060. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  5061. // item id
  5062. int item_id = sqlite3_column_int(stmt, 0);
  5063. // count
  5064. int item_count = sqlite3_column_int(stmt, 1);
  5065. // stockUom
  5066. int stockUom = sqlite3_column_int(stmt, 2);
  5067. // unit price
  5068. NSString *str_price = [self textAtColumn:3 statement:stmt];
  5069. NSString* Price=nil;
  5070. if([str_price isEqualToString:@""])
  5071. {
  5072. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  5073. if(price==nil)
  5074. Price=@"No Price.";
  5075. else
  5076. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5077. }
  5078. else
  5079. {
  5080. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  5081. }
  5082. // discount
  5083. double discount = sqlite3_column_double(stmt, 4);
  5084. // name
  5085. NSString *name = [self textAtColumn:5 statement:stmt];
  5086. // description
  5087. NSString *description = [self textAtColumn:6 statement:stmt];
  5088. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  5089. // line note
  5090. NSString *line_note = [self textAtColumn:7 statement:stmt];
  5091. // img
  5092. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  5093. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  5094. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5095. if(combine != nil)
  5096. {
  5097. // int citem=0;
  5098. int bcount=[[combine valueForKey:@"count"] intValue];
  5099. for(int bc=0;bc<bcount;bc++)
  5100. {
  5101. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5102. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5103. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5104. subTotal += uprice * modulus * item_count;
  5105. }
  5106. }
  5107. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  5108. [model_dic setValue:@"model" forKey:@"control"];
  5109. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  5110. [model_dic setValue:description forKey:@"description"];
  5111. [model_dic setValue:line_note forKey:@"note"];
  5112. [model_dic setValue:img forKey:@"img_url"];
  5113. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  5114. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  5115. [model_dic setValue:Price forKey:@"unit_price"];
  5116. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  5117. if (combine) {
  5118. [model_dic setValue:combine forKey:@"combine"];
  5119. }
  5120. // well,what under the row is the info for total
  5121. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5122. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5123. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5124. int carton=[bsubtotaljson[@"carton"] intValue];
  5125. TotalCuft += cuft;
  5126. TotalWeight += weight;
  5127. TotalCarton += carton;
  5128. payments += subTotal;
  5129. //---------------------------
  5130. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  5131. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  5132. }];
  5133. [dic setValue:@"Model Information" forKey:@"title"];
  5134. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  5135. return dic;
  5136. }
  5137. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  5138. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5139. [dic setValue:@"Remarks Content" forKey:@"title"];
  5140. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  5141. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5142. orderCode = [self translateSingleQuote:orderCode];
  5143. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  5144. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5145. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  5146. int mustCall = sqlite3_column_int(stmt, 1);
  5147. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  5148. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  5149. NSDictionary *po_dic = @{
  5150. @"aname" : @"PO#",
  5151. @"control" : @"edit",
  5152. @"keyboard" : @"text",
  5153. @"name" : @"poNumber",
  5154. @"value" : poNumber
  5155. };
  5156. NSDictionary *must_call_dic = @{
  5157. @"aname" : @"MUST CALL BEFORE DELIVERY",
  5158. @"control" : @"switch",
  5159. @"name" : @"must_call",
  5160. @"value" : mustCall ? @"true" : @"false"
  5161. };
  5162. NSDictionary *general_notes_dic = @{
  5163. @"aname" : @"General notes",
  5164. @"control" : @"text_view",
  5165. @"keyboard" : @"text",
  5166. @"name" : @"comments",
  5167. @"value" : generalNotes
  5168. };
  5169. NSDictionary *internal_notes_dic = @{
  5170. @"aname" : @"Internal notes",
  5171. @"control" : @"text_view",
  5172. @"keyboard" : @"text",
  5173. @"name" : @"internal_notes",
  5174. @"value" : internalNotes
  5175. };
  5176. [dic setValue:po_dic forKey:@"item_0"];
  5177. [dic setValue:must_call_dic forKey:@"item_1"];
  5178. [dic setValue:general_notes_dic forKey:@"item_2"];
  5179. [dic setValue:internal_notes_dic forKey:@"item_3"];
  5180. }];
  5181. return dic;
  5182. }
  5183. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  5184. // params
  5185. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5186. orderCode = [self translateSingleQuote:orderCode];
  5187. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5188. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  5189. __block double lift_gate_value = 0;
  5190. __block double handling_fee = 0;
  5191. __block double shipping = 0;
  5192. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5193. int lift_gate = sqlite3_column_int(stmt, 0);
  5194. lift_gate_value = sqlite3_column_double(stmt, 1);
  5195. shipping = sqlite3_column_double(stmt, 2);
  5196. handling_fee = sqlite3_column_double(stmt, 3);
  5197. if (!lift_gate) {
  5198. lift_gate_value = 0;
  5199. }
  5200. }];
  5201. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  5202. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  5203. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  5204. double payments = [[total valueForKey:@"payments"] doubleValue];
  5205. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  5206. double totalPrice = payments;
  5207. [dic setValue:@"Order Total" forKey:@"title"];
  5208. NSDictionary *payments_dic = @{
  5209. @"align" : @"right",
  5210. @"aname" : @"Payments/Credits",
  5211. @"control" : @"text",
  5212. @"name" : @"paymentsAndCredits",
  5213. @"type" : @"price",
  5214. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  5215. };
  5216. [dic setValue:payments_dic forKey:@"item_0"];
  5217. NSDictionary *handling_fee_dic = @{
  5218. @"align" : @"right",
  5219. @"aname" : @"Handling Fee",
  5220. @"control" : @"text",
  5221. @"name" : @"handling_fee_value",
  5222. @"required" : @"true",
  5223. @"type" : @"price",
  5224. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  5225. };
  5226. NSDictionary *shipping_dic = @{
  5227. @"align" : @"right",
  5228. @"aname" : @"Shipping*",
  5229. @"control" : @"text",
  5230. @"name" : @"shipping",
  5231. @"required" : @"true",
  5232. @"type" : @"price",
  5233. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  5234. };
  5235. NSDictionary *lift_gate_dic = @{
  5236. @"align" : @"right",
  5237. @"aname" : @"Liftgate Fee(No Loading Dock)",
  5238. @"control" : @"text",
  5239. @"name" : @"lift_gate_value",
  5240. @"required" : @"true",
  5241. @"type" : @"price",
  5242. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  5243. };
  5244. int item_count = 1;
  5245. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  5246. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  5247. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5248. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5249. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  5250. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5251. } else {
  5252. }
  5253. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5254. NSDictionary *total_price_dic = @{
  5255. @"align" : @"right",
  5256. @"aname" : @"Total",
  5257. @"control" : @"text",
  5258. @"name" : @"totalPrice",
  5259. @"type" : @"price",
  5260. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  5261. };
  5262. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5263. NSDictionary *total_cuft_dic = @{
  5264. @"align" : @"right",
  5265. @"aname" : @"Total Cuft",
  5266. @"control" : @"text",
  5267. @"name" : @"",
  5268. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  5269. };
  5270. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5271. NSDictionary *total_weight_dic = @{
  5272. @"align" : @"right",
  5273. @"aname" : @"Total Weight",
  5274. @"control" : @"text",
  5275. @"name" : @"",
  5276. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  5277. };
  5278. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5279. NSDictionary *total_carton_dic = @{
  5280. @"align" : @"right",
  5281. @"aname" : @"Total Carton",
  5282. @"control" : @"text",
  5283. @"name" : @"",
  5284. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  5285. };
  5286. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5287. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  5288. return dic;
  5289. }
  5290. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  5291. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5292. orderCode = [self translateSingleQuote:orderCode];
  5293. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5294. [dic setValue:@"Signature" forKey:@"title"];
  5295. __block NSString *pic_path = @"";
  5296. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  5297. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5298. pic_path = [self textAtColumn:0 statement:stmt];
  5299. }];
  5300. NSDictionary *pic_dic = @{
  5301. @"aname" : @"Signature",
  5302. @"avalue" :pic_path,
  5303. @"control" : @"signature",
  5304. @"name" : @"sign_picpath",
  5305. @"value" : pic_path
  5306. };
  5307. [dic setValue:pic_dic forKey:@"item_0"];
  5308. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5309. return dic;
  5310. }
  5311. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  5312. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5313. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5314. orderCode = [self translateSingleQuote:orderCode];
  5315. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  5316. __block NSString *logist = @"";
  5317. __block NSString *lift_gate = @"";
  5318. __block int lift_gate_integer = 0;
  5319. __block NSString *logistic_note = @"";
  5320. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5321. logist = [self textAtColumn:0 statement:stmt];
  5322. lift_gate_integer = sqlite3_column_int(stmt, 1);
  5323. logistic_note = [self textAtColumn:2 statement:stmt];
  5324. }];
  5325. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5326. [dic setValue:@"Shipping Method" forKey:@"title"];
  5327. // val_0
  5328. int PERSONAL_PICK_UP_check = 0;
  5329. int USE_MY_CARRIER_check = 0;
  5330. NSString *logistic_note_text = @"";
  5331. int will_call_check = 0;
  5332. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  5333. will_call_check = 1;
  5334. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  5335. PERSONAL_PICK_UP_check = 1;
  5336. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  5337. USE_MY_CARRIER_check = 1;
  5338. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  5339. logistic_note = [logistic_note_array firstObject];
  5340. if (logistic_note_array.count > 1) {
  5341. logistic_note_text = [logistic_note_array lastObject];
  5342. }
  5343. }
  5344. }
  5345. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  5346. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  5347. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  5348. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  5349. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  5350. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5351. NSDictionary *val0_subItem_item0 = @{
  5352. @"aname" : @"Option",
  5353. @"cadedate" : @{
  5354. @"count" : [NSNumber numberWithInteger:2],
  5355. @"val_0" : @{
  5356. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  5357. @"refresh" : [NSNumber numberWithInteger:0],
  5358. @"value" : @"PERSONAL PICK UP",
  5359. @"value_id" : @"PERSONAL PICK UP"
  5360. },
  5361. @"val_1" : @{
  5362. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  5363. @"refresh" : [NSNumber numberWithInteger:0],
  5364. @"sub_item" : @{
  5365. @"count" : [NSNumber numberWithInteger:1],
  5366. @"item_0" : @{
  5367. @"aname" : @"BOL",
  5368. @"control" : @"edit",
  5369. @"keyboard" : @"text",
  5370. @"name" : @"logist_note_text",
  5371. @"refresh" : [NSNumber numberWithInteger:0],
  5372. @"required" : @"false",
  5373. @"value" : logistic_note_text
  5374. }
  5375. },
  5376. @"value" : @"USE MY CARRIER",
  5377. @"value_id" : @"USE MY CARRIER"
  5378. }
  5379. },
  5380. @"control" : @"enum",
  5381. @"name" : @"logistic_note",
  5382. @"required" : @"true",
  5383. @"single_select" : @"true"
  5384. };
  5385. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  5386. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  5387. // val_1
  5388. lift_gate = [self valueInParams:params key:@"lift_gate"];
  5389. if([lift_gate isEqualToString:@""]) {
  5390. if (lift_gate_integer == 1) {
  5391. lift_gate = @"true";
  5392. } else {
  5393. lift_gate = @"false";
  5394. }
  5395. }
  5396. int common_carrier_check = 0;
  5397. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  5398. common_carrier_check = 1;
  5399. [params setValue:lift_gate forKey:@"lift_gate"];
  5400. }
  5401. NSDictionary *val_1 = @{
  5402. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  5403. @"sub_item" : @{
  5404. @"count" : [NSNumber numberWithInteger:1],
  5405. @"item_0" : @{
  5406. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  5407. @"control" : @"switch",
  5408. @"name" : @"lift_gate",
  5409. @"refresh" : [NSNumber numberWithInteger:1],
  5410. @"required" : @"true",
  5411. @"value" : lift_gate
  5412. }
  5413. },
  5414. @"value" : @"COMMON CARRIER",
  5415. @"value_id" : @"COMMON CARRIER"
  5416. };
  5417. // cadedate
  5418. NSDictionary *cadedate = @{
  5419. @"count" : [NSNumber numberWithInteger:2],
  5420. @"val_0" : val_0,
  5421. @"val_1" : val_1
  5422. };
  5423. // item_0
  5424. NSDictionary *item_0 = @{
  5425. @"aname" : @"Shipping",
  5426. @"cadedate" : cadedate,
  5427. @"control" : @"enum",
  5428. @"name" : @"logist",
  5429. @"refresh" : [NSNumber numberWithInteger:1],
  5430. @"single_select" : @"true",
  5431. };
  5432. [dic setValue:item_0 forKey:@"item_0"];
  5433. return dic;
  5434. }
  5435. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  5436. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5437. orderCode = [self translateSingleQuote:orderCode];
  5438. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  5439. __block int submit_as_integer = 0;
  5440. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5441. submit_as_integer = sqlite3_column_int(stmt, 0);
  5442. }];
  5443. int check11 = 0;
  5444. int check10 = 0;
  5445. if (submit_as_integer == 11) {
  5446. check11 = 1;
  5447. }
  5448. if (submit_as_integer == 10) {
  5449. check10 = 1;
  5450. }
  5451. // section_0
  5452. NSDictionary *dic = @{
  5453. @"count" : @(1),
  5454. @"item_0" : @{
  5455. @"aname" : @"Submit Order As",
  5456. @"cadedate" : @{
  5457. @"count" : @(2),
  5458. @"val_0" : @{
  5459. @"check" : [NSNumber numberWithInteger:check11],
  5460. @"value" : @"Sales Order",
  5461. @"value_id" : @(11)
  5462. },
  5463. @"val_1" : @{
  5464. @"check" : [NSNumber numberWithInteger:check10],
  5465. @"value" : @"Quote",
  5466. @"value_id" : @(10)
  5467. }
  5468. },
  5469. @"control" : @"enum",
  5470. @"name" : @"erpOrderStatus",
  5471. @"required" : @"true",
  5472. @"single_select" : @"true"
  5473. },
  5474. @"title" : @"Order Type"
  5475. };
  5476. return dic;
  5477. }
  5478. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  5479. countryCode = [self translateSingleQuote:countryCode];
  5480. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5481. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5482. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5483. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5484. if (name == NULL) {
  5485. name = "";
  5486. }
  5487. if (code == NULL) {
  5488. code = "";
  5489. }
  5490. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5491. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  5492. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  5493. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5494. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  5495. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5496. }
  5497. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  5498. [container setValue:stateDic forKey:key];
  5499. }] mutableCopy];
  5500. // failure 可以不用了,一样的
  5501. if (ret.allKeys.count == 0) {
  5502. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  5503. [stateDic setValue:@"Other" forKey:@"value"];
  5504. [stateDic setValue:@"" forKey:@"value_id"];
  5505. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  5506. if (state_code && [@"" isEqualToString:state_code]) {
  5507. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  5508. }
  5509. NSString *key = [NSString stringWithFormat:@"val_0"];
  5510. [ret setValue:stateDic forKey:key];
  5511. }
  5512. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  5513. return ret;
  5514. }
  5515. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  5516. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5517. orderCode = [self translateSingleQuote:orderCode];
  5518. NSString *sql = [NSString stringWithFormat:@"select paymentType,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,decrypt(credit_card_number),decrypt(credit_card_security_code),decrypt(credit_card_expiration_month),decrypt(credit_card_expiration_year),credit_card_city,credit_card_state from offline_order where so_id = '%@';",orderCode];
  5519. __block NSString *payType = @"";
  5520. __block NSString *firstName = @"";
  5521. __block NSString *lastName = @"";
  5522. __block NSString *addr1 = @"";
  5523. __block NSString *addr2 = @"";
  5524. __block NSString *zipcode = @"";
  5525. __block NSString *cardType = @"";
  5526. __block NSString *cardNumber = @"";
  5527. __block NSString *securityCode = @"";
  5528. __block NSString *month = @"";
  5529. __block NSString *year = @"";
  5530. __block NSString *city = @"";
  5531. __block NSString *state = @"";
  5532. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5533. payType = [self textAtColumn:0 statement:stmt];
  5534. firstName = [self textAtColumn:1 statement:stmt];
  5535. lastName = [self textAtColumn:2 statement:stmt];
  5536. addr1 = [self textAtColumn:3 statement:stmt];
  5537. addr2 = [self textAtColumn:4 statement:stmt];
  5538. zipcode = [self textAtColumn:5 statement:stmt];
  5539. cardType = [self textAtColumn:6 statement:stmt];
  5540. cardNumber = [self textAtColumn:7 statement:stmt];
  5541. securityCode = [self textAtColumn:8 statement:stmt];
  5542. month = [self textAtColumn:9 statement:stmt];
  5543. year = [self textAtColumn:10 statement:stmt];
  5544. city = [self textAtColumn:11 statement:stmt];
  5545. state = [self textAtColumn:12 statement:stmt];
  5546. }];
  5547. // if ([year isEqualToString:@""]) {
  5548. // NSDate *date = [NSDate date];
  5549. // NSCalendar *calendar = [NSCalendar currentCalendar];
  5550. // int year_integer = [calendar component:NSCalendarUnitYear fromDate:date];
  5551. // year = [NSString stringWithFormat:@"%d",year_integer];
  5552. // }
  5553. // if ([month isEqualToString:@""]) {
  5554. // month = @"01";
  5555. // }
  5556. // "section_2"
  5557. NSDictionary *dic = @{
  5558. @"count" : @(1),
  5559. @"item_0" : @{
  5560. @"aname" : @"Payment",
  5561. @"cadedate" : @{
  5562. @"count" : @(6),
  5563. @"val_0" : @{
  5564. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  5565. @"value" : @"Check",
  5566. @"value_id" : @"Check"
  5567. },
  5568. @"val_1" : @{
  5569. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  5570. @"value" : @"Cash",
  5571. @"value_id" : @"Cash"
  5572. },
  5573. @"val_2" : @{
  5574. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  5575. @"value" : @"NET 30",
  5576. @"value_id" : @"NET 30"
  5577. },
  5578. @"val_3" : @{
  5579. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  5580. @"value" : @"Wire Transfer",
  5581. @"value_id" : @"Wire Transfer"
  5582. },
  5583. @"val_4" : @{
  5584. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  5585. @"sub_item" : @{
  5586. @"count" : @(3),
  5587. @"item_0" : @{
  5588. @"aname" : @"choose",
  5589. @"control" : @"multi_action",
  5590. @"count" : @(1),
  5591. @"item_0" : @{
  5592. @"aname" : @"Same as customer",
  5593. @"key_map" : @{
  5594. @"credit_card_address1" : @"customer_address1",
  5595. @"credit_card_address2" : @"customer_address2",
  5596. @"credit_card_city" : @"customer_city",
  5597. @"credit_card_first_name" : @"customer_first_name",
  5598. @"credit_card_last_name" : @"customer_last_name",
  5599. @"credit_card_state" : @"customer_state",
  5600. @"credit_card_zipcode" : @"customer_zipcode"
  5601. },
  5602. @"type" : @"pull"
  5603. }
  5604. },
  5605. @"item_1" : @{
  5606. @"aname" : @"",
  5607. @"color" : @"red",
  5608. @"control" : @"text",
  5609. @"name" : @"",
  5610. @"value" : @"USA Credit cards only"
  5611. },
  5612. @"item_2" : @{
  5613. @"aname" : @"Fill",
  5614. @"cadedate" : @{
  5615. @"count" : @(2),
  5616. @"val_0" : @{
  5617. @"check" : @(1),
  5618. @"sub_item" : @{
  5619. @"count" : @(11),
  5620. @"item_0" : @{
  5621. @"aname" : @"Type",
  5622. @"cadedate" : @{
  5623. @"count" : @(2),
  5624. @"val_0" : @{
  5625. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  5626. @"value" : @"VISA",
  5627. @"value_id" : @(0)
  5628. },
  5629. @"val_1" : @{
  5630. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  5631. @"value" : @"MASTER CARD",
  5632. @"value_id" : @(1)
  5633. }
  5634. },
  5635. @"control" : @"enum",
  5636. @"name" : @"credit_card_type",
  5637. @"required" : @"true",
  5638. @"single_select" : @"true"
  5639. },
  5640. @"item_1" : @{
  5641. @"aname" : @"Number",
  5642. @"control" : @"edit",
  5643. @"keyboard" : @"int",
  5644. @"length" : @"16",
  5645. @"name" : @"credit_card_number",
  5646. @"required" : @"true",
  5647. @"value" : cardNumber
  5648. },
  5649. @"item_10" : @{
  5650. @"aname" : @"State",
  5651. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  5652. @"control" : @"enum",
  5653. @"enum" : @"true",
  5654. @"name" : @"credit_card_state",
  5655. @"required" : @"true",
  5656. @"single_select" : @"true"
  5657. },
  5658. @"item_2" : @{
  5659. @"aname" : @"Expiration Date",
  5660. @"control" : @"monthpicker",
  5661. @"name" : @"credit_card_expiration",
  5662. @"required" : @"true",
  5663. @"type" : @"date",
  5664. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  5665. },
  5666. @"item_3" : @{
  5667. @"aname" : @"Security Code",
  5668. @"control" : @"edit",
  5669. @"keyboard" : @"int",
  5670. @"length" : @"3",
  5671. @"name" : @"credit_card_security_code",
  5672. @"required" : @"true",
  5673. @"value" : securityCode
  5674. },
  5675. @"item_4" : @{
  5676. @"aname" : @"First Name",
  5677. @"control" : @"edit",
  5678. @"keyboard" : @"text",
  5679. @"name" : @"credit_card_first_name",
  5680. @"required" : @"true",
  5681. @"value" : firstName
  5682. },
  5683. @"item_5" : @{
  5684. @"aname" : @"Last Name",
  5685. @"control" : @"edit",
  5686. @"keyboard" : @"text",
  5687. @"name" : @"credit_card_last_name",
  5688. @"required" : @"true",
  5689. @"value" : lastName
  5690. },
  5691. @"item_6" : @{
  5692. @"aname" : @"Address 1",
  5693. @"control" : @"edit",
  5694. @"keyboard" : @"text",
  5695. @"name" : @"credit_card_address1",
  5696. @"required" : @"true",
  5697. @"value" : addr1
  5698. },
  5699. @"item_7" : @{
  5700. @"aname" : @"Address 2",
  5701. @"control" : @"edit",
  5702. @"keyboard" : @"text",
  5703. @"name" : @"credit_card_address2",
  5704. @"value" : addr2
  5705. },
  5706. @"item_8" : @{
  5707. @"aname" : @"zip code",
  5708. @"control" : @"edit",
  5709. @"keyboard" : @"text",
  5710. @"name" : @"credit_card_zipcode",
  5711. @"required" : @"true",
  5712. @"value" : zipcode
  5713. },
  5714. @"item_9" : @{
  5715. @"aname" : @"City",
  5716. @"control" : @"edit",
  5717. @"keyboard" : @"text",
  5718. @"name" : @"credit_card_city",
  5719. @"required" : @"true",
  5720. @"value" : city
  5721. }
  5722. },
  5723. @"value" : @"Fill Now",
  5724. @"value_id" : @""
  5725. },
  5726. @"val_1" : @{
  5727. @"check" : @(0),
  5728. @"value" : @"Fill Later",
  5729. @"value_id" : @""
  5730. }
  5731. },
  5732. @"control" : @"enum",
  5733. @"name" : @"",
  5734. @"single_select" : @"true"
  5735. }
  5736. },
  5737. @"value" : @"Credit Card",
  5738. @"value_id" : @"Credit Card"
  5739. },
  5740. @"val_5" : @{
  5741. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  5742. @"value" : @"FOLLOW EXISTING",
  5743. @"value_id" : @"FOLLOW EXISTING"
  5744. }
  5745. },
  5746. @"control" : @"enum",
  5747. @"name" : @"paymentType",
  5748. @"single_select" : @"true"
  5749. },
  5750. @"title" : @"Payment Information"
  5751. };
  5752. return dic;
  5753. }
  5754. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  5755. {
  5756. sqlite3 *db = [iSalesDB get_db];
  5757. // params
  5758. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5759. orderCode = [self translateSingleQuote:orderCode];
  5760. // 0 Order Type 1 Shipping Method 2 Payment Information
  5761. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  5762. // 0 Order Type
  5763. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  5764. [ret setValue:order_type_dic forKey:@"section_0"];
  5765. // 1 Shipping Method
  5766. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  5767. // [params setValue:[shipping_method_dic objectForKey:@"lift_gate"] forKey:@"lift_gate"];
  5768. // [shipping_method_dic removeObjectForKey:@"lift_gate"];
  5769. [ret setValue:shipping_method_dic forKey:@"section_1"];
  5770. // 2 Payment Information
  5771. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  5772. [ret setValue:payment_info_dic forKey:@"section_2"];
  5773. // 3 Customer
  5774. NSDictionary *customer_dic = [self customerDic:params db:db];
  5775. [ret setValue:customer_dic forKey:@"section_3"];
  5776. // 4 Ship To
  5777. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  5778. [ret setValue:ship_to_dic forKey:@"section_4"];
  5779. // 5 Ship From
  5780. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  5781. [ret setValue:ship_from_dic forKey:@"section_5"];
  5782. // 6 Freight Bill To
  5783. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  5784. [ret setValue:freight_bill_to forKey:@"section_6"];
  5785. // 7 Merchandise Bill To
  5786. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  5787. [ret setValue:merchandise_bill_to_dic forKey:@"section_7"];
  5788. // 8 Return To
  5789. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  5790. [ret setValue:return_to_dic forKey:@"section_8"];
  5791. // 9 Model Information
  5792. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  5793. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  5794. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  5795. [ret setValue:model_info_dic forKey:@"section_9"];
  5796. // 10 Remarks Content
  5797. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  5798. [ret setValue:remarks_content_dic forKey:@"section_10"];
  5799. // 11 Order Total
  5800. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  5801. [ret setValue:order_total_dic forKey:@"section_11"];
  5802. // 12 Signature
  5803. NSDictionary *sign_dic = [self signatureDic:params db:db];
  5804. [ret setValue:sign_dic forKey:@"section_12"];
  5805. [iSalesDB close_db:db];
  5806. return [RAUtils dict2data:ret];
  5807. // return nil;
  5808. }
  5809. #pragma mark addr editor
  5810. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  5811. NSMutableDictionary *new_item = [item mutableCopy];
  5812. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  5813. return new_item;
  5814. }
  5815. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  5816. {
  5817. // "is_subaction" = true;
  5818. // orderCode = MOB1608240002;
  5819. // password = 123456;
  5820. // "subaction_tag" = 1;
  5821. // user = EvanK;
  5822. // {
  5823. // "is_subaction" = true;
  5824. // orderCode = MOB1608240002;
  5825. // password = 123456;
  5826. // "refresh_trigger" = zipcode;
  5827. // "subaction_tag" = 1;
  5828. // user = EvanK;
  5829. // }
  5830. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  5831. [ret removeObjectForKey:@"up_params"];
  5832. [ret setObject:@"New Address" forKey:@"title"];
  5833. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  5834. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  5835. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  5836. NSMutableDictionary *country_dic = [section_0[@"item_5"] mutableCopy];
  5837. [country_dic removeObjectForKey:@"refresh"];
  5838. [country_dic removeObjectForKey:@"restore"];
  5839. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  5840. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  5841. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_6"] add:YES] forKey:@"item_2"]; // Address 1*
  5842. [new_section_0 setObject:section_0[@"item_7"] forKey:@"item_3"]; // Address 2
  5843. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_4"]; // Address 3
  5844. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_5"]; // Address 4
  5845. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_10"] add:YES];
  5846. [zip_code_dic removeObjectForKey:@"refresh"];
  5847. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  5848. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_11"] add:YES] forKey:@"item_7"]; // State/Province*
  5849. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_8"]; // City*
  5850. [new_section_0 setObject:section_0[@"item_13"] forKey:@"item_9"]; // Contact First Name
  5851. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_10"]; // Contact Last Name
  5852. [new_section_0 setObject:section_0[@"item_4"] forKey:@"item_11"]; // Phone*
  5853. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_12"]; // Fax
  5854. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_3"] add:NO] forKey:@"item_13"]; // Email
  5855. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  5856. // country
  5857. NSString *countryCode = @"US";
  5858. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5859. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  5860. // state
  5861. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  5862. NSDictionary *tmpDic = @{
  5863. @"value" : @"Other",
  5864. @"value_id" : @"",
  5865. @"check" : [NSNumber numberWithInteger:0]
  5866. };
  5867. for (int i = 0; i < allState.allKeys.count; i++) {
  5868. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  5869. NSDictionary *tmp = allState[key];
  5870. [allState setValue:tmpDic forKey:key];
  5871. tmpDic = tmp;
  5872. }
  5873. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  5874. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  5875. [ret setValue:new_section_0 forKey:@"section_0"];
  5876. return [RAUtils dict2data:ret];
  5877. }
  5878. #pragma mark save addr
  5879. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  5880. {
  5881. // NSString *companyName = [self valueInParams:params key:@"company"];
  5882. // NSString *addr1 = [self valueInParams:params key:@"address"];
  5883. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  5884. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  5885. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  5886. // NSString *countryId = [self valueInParams:params key:@"country"];
  5887. // NSString *stateCode = [self valueInParams:params key:@"state"];
  5888. // NSString *city = [self valueInParams:params key:@"city"];
  5889. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  5890. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  5891. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  5892. // NSString *phone = [self valueInParams:params key:@"phone"];
  5893. // NSString *fax = [self valueInParams:params key:@"fax"];
  5894. // NSString *email = [self valueInParams:params key:@"email"];
  5895. return [self offline_saveContact:params update:NO isCustomer:NO];
  5896. }
  5897. #pragma mark cancel order
  5898. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  5899. {
  5900. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5901. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  5902. int ret = [iSalesDB execSql:sql];
  5903. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5904. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5905. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5906. // [dic setValue:@"160409" forKey:@"min_ver"];
  5907. return [RAUtils dict2data:dic];
  5908. }
  5909. #pragma mark sign order
  5910. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  5911. {
  5912. //参考 offline_saveBusinesscard
  5913. NSLog(@"sign order params: %@",params);
  5914. // orderCode = MOB1608240002;
  5915. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  5916. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5917. orderCode = [self translateSingleQuote:orderCode];
  5918. NSString *picPath = [self valueInParams:params key:@"picpath"];
  5919. picPath = [self translateSingleQuote:picPath];
  5920. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  5921. int ret = [iSalesDB execSql:sql];
  5922. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5923. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5924. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5925. // [dic setValue:@"160409" forKey:@"min_ver"];
  5926. return [RAUtils dict2data:dic];
  5927. }
  5928. #pragma mark save order
  5929. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  5930. NSString *ret = [self valueInParams:params key:key];
  5931. if (translate) {
  5932. ret = [self translateSingleQuote:ret];
  5933. }
  5934. return ret;
  5935. }
  5936. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  5937. {
  5938. // "logistic_note" = "PERSONAL PICK UP";
  5939. // logist_note_text
  5940. // "logistic_note" = "USE MY CARRIER";
  5941. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  5942. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  5943. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  5944. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  5945. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  5946. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  5947. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  5948. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  5949. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  5950. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  5951. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  5952. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  5953. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  5954. if (![total_price isEqualToString:@""]) {
  5955. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  5956. } else {
  5957. total_price = @"";
  5958. }
  5959. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  5960. if ([paymentsAndCredits isEqualToString:@""]) {
  5961. paymentsAndCredits = @"";
  5962. } else {
  5963. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  5964. }
  5965. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  5966. if ([handling_fee_value isEqualToString:@""]) {
  5967. handling_fee_value = @"";
  5968. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  5969. handling_fee_value = @"";
  5970. } else {
  5971. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  5972. }
  5973. NSString *handling_fee_placeholder = handling_fee_value;
  5974. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  5975. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  5976. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  5977. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  5978. NSString *lift_gate_placeholder = @"";
  5979. if ([lift_gate_value isEqualToString:@""]) {
  5980. lift_gate_placeholder = @"";
  5981. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  5982. lift_gate_placeholder = @"";
  5983. } else {
  5984. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  5985. }
  5986. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  5987. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  5988. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  5989. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  5990. logistic_note = [logistic_note isEqualToString:@"USE MY CARRIER"] ? [NSString stringWithFormat:@"logistic_note = '%@\n\t%@',",logistic_note,logistic_note_text] : [NSString stringWithFormat:@"logistic_note = '%@',",logistic_note];
  5991. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  5992. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  5993. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number];
  5994. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  5995. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  5996. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  5997. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  5998. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  5999. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  6000. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  6001. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  6002. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  6003. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  6004. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  6005. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  6006. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  6007. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  6008. BOOL number_nil = [credit_card_number isEqualToString:@""];
  6009. if (!number_nil) {
  6010. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  6011. }
  6012. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  6013. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  6014. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  6015. if (!security_code_nil) {
  6016. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  6017. }
  6018. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  6019. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  6020. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  6021. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  6022. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  6023. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  6024. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  6025. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  6026. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  6027. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  6028. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  6029. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  6030. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  6031. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  6032. NSString *contact_id = customer_cid;
  6033. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  6034. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  6035. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  6036. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  6037. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  6038. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  6039. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  6040. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  6041. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  6042. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  6043. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  6044. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  6045. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  6046. //
  6047. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  6048. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  6049. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  6050. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  6051. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  6052. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  6053. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  6054. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  6055. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  6056. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  6057. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  6058. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  6059. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  6060. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  6061. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  6062. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  6063. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  6064. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  6065. NSString *contactSql = [NSString stringWithFormat:@"update offline_contact set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ where contact_id = '%@'",contact_contact,contact_email,contact_phone,contact_fax,customer_name,customer_address1,customer_address2,customer_address3,customer_address4,customer_country,customer_state,customer_city,customer_zipcode,contact_id];
  6066. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  6067. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  6068. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  6069. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  6070. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  6071. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  6072. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  6073. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  6074. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  6075. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  6076. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  6077. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  6078. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  6079. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  6080. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  6081. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  6082. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  6083. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  6084. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  6085. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  6086. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  6087. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  6088. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  6089. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  6090. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  6091. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  6092. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  6093. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  6094. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  6095. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  6096. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  6097. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  6098. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  6099. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  6100. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  6101. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  6102. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  6103. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  6104. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  6105. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  6106. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  6107. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  6108. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  6109. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  6110. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  6111. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  6112. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  6113. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  6114. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  6115. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  6116. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  6117. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  6118. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  6119. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  6120. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  6121. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  6122. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  6123. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  6124. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  6125. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  6126. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  6127. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  6128. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  6129. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  6130. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  6131. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  6132. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  6133. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  6134. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  6135. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  6136. NSString *order_status = @"status = 1,";
  6137. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  6138. NSString *sync_data = [RAUtils dict2string:param];
  6139. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@',sync_data = '%@' where so_id = '%@';",order_status,general_notes,internal_notes,poNumber,must_call,sign_picpath,total_price,paymentsAndCredits,handling_fee_placeholder,lift_gate,lift_gate_placeholder,logist,logistic_note,erpOrderStatus,paymentType,credit_card_address1,credit_card_address2,credit_card_city,credit_card_first_name,credit_card_last_name,credit_card_number,credit_card_security_code,credit_card_state,credit_card_type,credit_card_zipcode,credit_card_expiration_year,credit_card_expiration_month,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_email,receive_fax,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,so_id,sync_data,so_id];
  6140. DebugLog(@"save order contactSql: %@",contactSql);
  6141. DebugLog(@"save order orderSql: %@",orderSql);
  6142. int contact_ret = [iSalesDB execSql:contactSql];
  6143. int order_ret = [iSalesDB execSql:orderSql];
  6144. int ret = contact_ret == 2 && order_ret == 2 ? 2 : -1;
  6145. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6146. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6147. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6148. // [dic setValue:@"160409" forKey:@"min_ver"];
  6149. return [RAUtils dict2data:dic];
  6150. }
  6151. @end