OLDataProvider.m 509 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146
  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. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "Singleton.h"
  19. #import "QRCodeGenerator.h"
  20. #import "config.h"
  21. @interface OLDataProvider ()
  22. @end
  23. @implementation OLDataProvider
  24. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  25. {
  26. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  27. NSString* ret=@"No Price.";
  28. sqlite3_stmt * statement;
  29. // int count=0;
  30. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  31. {
  32. if (sqlite3_step(statement) == SQLITE_ROW)
  33. {
  34. char *price = (char*)sqlite3_column_text(statement, 0);
  35. if(price==nil)
  36. price="";
  37. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  38. double dp= [nsprice doubleValue];
  39. ret=[NSString stringWithFormat:@"%.2f",dp];
  40. }
  41. sqlite3_finalize(statement);
  42. }
  43. return ret;
  44. // // [iSalesDB close_db:db];
  45. //
  46. // return nil;
  47. //// if(dprice==DBL_MAX)
  48. //// ret= nil;
  49. //// else
  50. //// ret= [NSNumber numberWithDouble:dprice];
  51. //// return ret;
  52. }
  53. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  54. {
  55. NSString* ret= nil;
  56. NSString *sqlQuery = nil;
  57. // 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
  58. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  59. sqlite3_stmt * statement;
  60. // int count=0;
  61. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  62. {
  63. if (sqlite3_step(statement) == SQLITE_ROW)
  64. {
  65. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  66. if(imgurl==nil)
  67. imgurl="";
  68. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  69. ret=nsimgurl;
  70. }
  71. sqlite3_finalize(statement);
  72. }
  73. else
  74. {
  75. ret=@"";
  76. }
  77. // [iSalesDB close_db:db];
  78. // DebugLog(@"data string: %@",ret );
  79. return ret;
  80. }
  81. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  82. {
  83. NSMutableDictionary* values=params[@"replaceValue"];
  84. //生成portfolio pdf需要的数据
  85. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  86. data[@"npd_url"]=@"www.newpacificdirect.com";
  87. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  88. [formatter setDateFormat:@"MMMM yyyy"];
  89. NSString* date = [formatter stringFromDate:[NSDate date]];
  90. data[@"create_month"]=date;
  91. data[@"company_name"]=@"New Pacific Direct,Inc.";
  92. data[@"catalog_name"]=params[@"catalog_name"];
  93. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  94. data[@"TOTAL_PAGE"]=0;
  95. data[@"CURRENT_PAGE"]=0;
  96. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  97. bool show_stockout=[params[@"show_stockout"] boolValue];
  98. NSString *sql = nil;
  99. if(values==nil)
  100. {
  101. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id order by a.name",params[@"product_ids"],params[@"product_ids"]];
  102. }
  103. else
  104. {
  105. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in from product where product_id in(%@) order by name",params[@"product_ids"]];
  106. }
  107. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  108. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  109. sqlite3 *db = [iSalesDB get_db];
  110. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  111. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  112. int product_id = sqlite3_column_int(stmt, 0);
  113. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  114. NSString *name = [self textAtColumn:1 statement:stmt];
  115. NSString *description = [self textAtColumn:2 statement:stmt];
  116. double price = sqlite3_column_double(stmt, 3);
  117. NSString *s_price = [self textAtColumn:3 statement:stmt];
  118. double discount = sqlite3_column_double(stmt,4);
  119. int qty = sqlite3_column_int(stmt, 5);
  120. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  121. int is_percentage = sqlite3_column_int(stmt, 6);
  122. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  123. int item_id = sqlite3_column_int(stmt, 7);
  124. // int fashion_id = sqlite3_column_int(stmt, 8);
  125. NSString *line_note = [self textAtColumn:9 statement:stmt];
  126. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  127. double percent = sqlite3_column_double(stmt, 10);
  128. // int portfolio_id = sqlite3_column_int(stmt, 11);
  129. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  130. int availability = sqlite3_column_int(stmt, 12);
  131. NSString *color = [self textAtColumn:13 statement:stmt];
  132. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  133. NSString *demension = [self textAtColumn:15 statement:stmt];
  134. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  135. NSString *material = [self textAtColumn:17 statement:stmt];
  136. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  137. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  138. double volume = sqlite3_column_double(stmt, 19);
  139. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  140. double weight = sqlite3_column_double(stmt, 20);
  141. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  142. int model_set = sqlite3_column_int(stmt, 21);
  143. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  144. double load_ability = sqlite3_column_double(stmt, 22);
  145. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  146. NSString *assembling = [self textAtColumn:24 statement:stmt];
  147. NSString *made_in = [self textAtColumn:25 statement:stmt];
  148. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  149. if(is_percentage==0&&qty==0&&!show_stockout)
  150. {
  151. return;
  152. }
  153. if((is_percentage!=0||s_is_percentage.length==0)&&availability==0&&!show_stockout)
  154. {
  155. return;
  156. }
  157. if(s_percent.length==0||is_percentage==0)
  158. {
  159. percent=100.0;
  160. if([s_qty isEqualToString:@"null"])
  161. qty=availability;
  162. }
  163. else
  164. {
  165. qty=availability;
  166. }
  167. qty=qty*percent/100+0.5;
  168. for(int i=0;i<[values[@"count"] intValue];i++)
  169. {
  170. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  171. if([valueitem[@"product_id"] intValue]==product_id)
  172. {
  173. line_note=valueitem[@"line_note"];
  174. qty=[valueitem[@"available_qty"] intValue];
  175. if([[valueitem allKeys] containsObject:@"available_qty"])
  176. s_qty=[NSString stringWithFormat:@"%d",qty];
  177. else
  178. s_qty=@"null";
  179. if(valueitem[@"regular_price"]!=nil)
  180. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  181. s_price=valueitem[@"special_price"];
  182. price=[valueitem[@"special_price"] floatValue];
  183. discount=0;
  184. }
  185. }
  186. NSString* set_price=@"";
  187. if([params[@"entered_price"] boolValue])
  188. {
  189. if (s_price==nil ) {
  190. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  191. }
  192. else
  193. {
  194. if(price*(1-discount/100.0)!=gprice.floatValue)
  195. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  196. }
  197. }
  198. NSString* get_price=@"";
  199. {
  200. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  201. // DebugLog(@"price time interval");
  202. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  203. if(gprice==nil)
  204. get_price=@"Price:No Price.";
  205. else
  206. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  207. }
  208. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  209. // if ([qty_null isEqualToString:@"null"]) {
  210. // qty=availability;
  211. // }
  212. //
  213. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  214. // if ([qty_percent_null isEqualToString:@"null"]) {
  215. // percentage=1;
  216. // }
  217. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  218. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  219. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  220. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  221. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  222. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  223. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  224. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  225. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  226. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  227. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  228. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  229. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  230. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  231. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  232. if([params[@"availability"] boolValue]==false)
  233. str_availability=@"";
  234. else
  235. {
  236. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  237. str_availability=@"<b>Availability:</b> In Production";
  238. }
  239. if([params[@"color"] boolValue]==false || color.length==0 )
  240. str_color=@"";
  241. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  242. str_model_set=@"";
  243. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  244. str_legcolor=@"";
  245. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  246. str_demension=@"";
  247. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  248. str_seat_height=@"";
  249. if([params[@"material"] boolValue]==false || material.length==0 )
  250. str_meterial=@"";
  251. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  252. str_box_dim=@"";
  253. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  254. str_volume=@"";
  255. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  256. str_weight=@"";
  257. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  258. str_load_ability=@"";
  259. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  260. str_fabric_content=@"";
  261. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  262. str_assembling=@"";
  263. if([params[@"made"] boolValue]==false || made_in.length==0 )
  264. str_made_in=@"";
  265. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  266. str_line_note=@"";
  267. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  268. [arr_detail addObject:str_availability];
  269. [arr_detail addObject:str_color];
  270. [arr_detail addObject:str_model_set];
  271. [arr_detail addObject:str_legcolor];
  272. [arr_detail addObject:str_demension];
  273. [arr_detail addObject:str_seat_height];
  274. [arr_detail addObject:str_meterial];
  275. [arr_detail addObject:str_box_dim];
  276. [arr_detail addObject:str_volume];
  277. [arr_detail addObject:str_weight];
  278. [arr_detail addObject:str_load_ability];
  279. [arr_detail addObject:str_fabric_content];
  280. [arr_detail addObject:str_assembling];
  281. [arr_detail addObject:str_made_in];
  282. [arr_detail addObject:str_line_note];
  283. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  284. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  285. //model image;
  286. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  287. // qr image
  288. NSString* qrpath=nil;
  289. if([params[@"show_barcode"] boolValue])
  290. {
  291. NSString* temp = NSTemporaryDirectory();
  292. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  293. qrpath=[temp stringByAppendingPathComponent:filename];
  294. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  295. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  296. }
  297. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  298. item[@"img"]=img_path;
  299. item[@"img_qr"]=qrpath;
  300. item[@"special_price"]=set_price;
  301. item[@"price"]=get_price;
  302. item[@"name"]=name;
  303. item[@"description"]=description;
  304. item[@"detail"]=detail;
  305. item[@"img"]=img_path;
  306. // @{
  307. // //@"linenotes": line_note,
  308. //// @"product_id": product_id_string,
  309. //// @"available_qty": @(qty),
  310. //// @"available_percent" : @(percent),
  311. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  312. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  313. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  314. // @"": ,
  315. // @"": ,
  316. // @"": ,
  317. // @"": ,
  318. // @"": ,
  319. // @"": ,
  320. // @"":
  321. // }.mutableCopy;
  322. // if (percentage) {
  323. // [item removeObjectForKey:@"available_qty"];
  324. // } else {
  325. // [item removeObjectForKey:@"available_percent"];
  326. // }
  327. //
  328. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  329. // if ([qty_null isEqualToString:@"null"]) {
  330. // [item removeObjectForKey:@"available_qty"];
  331. // }
  332. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  333. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  334. }];
  335. [iSalesDB close_db:db];
  336. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  337. // [dic setValue:@"" forKey:@"email_content"];
  338. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  339. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  340. //
  341. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  342. // cell[@"count"]=[NSNumber numberWithInt:10];
  343. grid[@"cell0"]=dic;
  344. data[@"grid0"]=grid;
  345. return data;
  346. }
  347. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  348. {
  349. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  350. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  351. NSString *cache_folder=[paths objectAtIndex:0];
  352. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  353. BOOL bdir=NO;
  354. NSFileManager* fileManager = [NSFileManager defaultManager];
  355. if(PDF_DEBUG)
  356. {
  357. NSData *data = [NSData dataWithContentsOfFile:default_path];
  358. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  359. return ret;
  360. }
  361. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  362. {
  363. NSError * error=nil;
  364. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  365. {
  366. return nil;
  367. }
  368. }
  369. NSData *data = [NSData dataWithContentsOfFile:template_path];
  370. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  371. return ret;
  372. }
  373. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  374. {
  375. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  376. 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,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  377. DebugLog(@"offline_login sql:%@",sqlQuery);
  378. sqlite3_stmt * statement;
  379. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  380. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  381. {
  382. if (sqlite3_step(statement) == SQLITE_ROW)
  383. {
  384. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  385. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  386. int can_show_price = sqlite3_column_int(statement, 0);
  387. int can_see_price = sqlite3_column_int(statement, 1);
  388. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  389. if(contact_id==nil)
  390. contact_id="";
  391. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  392. int user_type = sqlite3_column_int(statement, 3);
  393. int can_cancel_order = sqlite3_column_int(statement, 4);
  394. int can_set_cart_price = sqlite3_column_int(statement, 5);
  395. int can_create_portfolio = sqlite3_column_int(statement, 6);
  396. int can_delete_order = sqlite3_column_int(statement, 7);
  397. int can_submit_order = sqlite3_column_int(statement, 8);
  398. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  399. int can_create_order = sqlite3_column_int(statement, 10);
  400. char *mode = (char*)sqlite3_column_text(statement, 11);
  401. if(mode==nil)
  402. mode="";
  403. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  404. char *username = (char*)sqlite3_column_text(statement, 12);
  405. if(username==nil)
  406. username="";
  407. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  408. int can_update_contact_info = sqlite3_column_int(statement, 13);
  409. char *first_name = (char*)sqlite3_column_text(statement, 14);
  410. if(first_name==nil)
  411. first_name="";
  412. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  413. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  414. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  415. // [header setValue:nscontact_id forKey:@"contact_id"];
  416. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  417. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  418. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  419. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  420. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  421. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  422. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  423. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  424. //
  425. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  426. //
  427. // [header setValue:nsusername forKey:@"username"];
  428. //
  429. // NSError* error=nil;
  430. //
  431. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  432. // [header setValue:statusFilter forKey:@"statusFilter"];
  433. //
  434. //
  435. //
  436. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  437. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  438. //
  439. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  440. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  441. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  442. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  443. //
  444. // [ret setObject:header forKey:@"header"];
  445. [ret setValue:nsfirst_name forKey:@"first_name"];
  446. }
  447. sqlite3_finalize(statement);
  448. }
  449. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  450. return ret;
  451. }
  452. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  453. {
  454. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  455. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  456. data[@"npd_url"]=@"www.newpacificdirect.com";
  457. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  458. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  459. NSString* date = [formatter stringFromDate:[NSDate date]];
  460. data[@"print_date"]=date;
  461. data[@"company_name"]=@"New Pacific Direct,Inc.";
  462. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  463. data[@"TOTAL_PAGE"]=0;
  464. data[@"CURRENT_PAGE"]=0;
  465. // [formatter setDateFormat:@"MM/dd/yyyy"];
  466. // date = [formatter stringFromDate:[NSDate date]];
  467. // data[@"creat_date"]=date;
  468. NSString* temp = NSTemporaryDirectory();
  469. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  470. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  471. UIImage* qrimg=[RAUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  472. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  473. data[@"img_barcode"]=qrpath;
  474. data[@"so_id"]=order_code;
  475. data[@"company"]=@"NEW PACIFIC DIRECT, INC.";
  476. // 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,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via 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,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  477. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes from offline_order where so_id='%@'",order_code];
  478. sqlite3 *db = [iSalesDB get_db];
  479. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  480. sqlite3_stmt * statement;
  481. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  482. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  483. row0[@"count"]=[NSNumber numberWithInt:1];
  484. datasource[@"row0"]=row0;
  485. NSString* print_status=@"";
  486. NSString* order_type=@"Offline Quote";
  487. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  488. {
  489. if (sqlite3_step(statement) == SQLITE_ROW)
  490. {
  491. int erpOrderStatus = sqlite3_column_int(statement, 0);
  492. int status = sqlite3_column_int(statement, 26);
  493. if(status==2)
  494. {
  495. switch (erpOrderStatus) {
  496. case 10:
  497. // order_type=@"Quoted Order";
  498. print_status=@"Quote";
  499. break;
  500. case 11:
  501. print_status=@"Booked";
  502. break;
  503. case 12:
  504. print_status=@"Picked";
  505. break;
  506. case 13:
  507. print_status=@"Shipped";
  508. break;
  509. case 14:
  510. print_status=@"Closed";
  511. break;
  512. case 15:
  513. print_status=@"Canceled";
  514. break;
  515. case 16:
  516. print_status=@"All";
  517. break;
  518. case 18:
  519. print_status=@"Processing";
  520. break;
  521. default:
  522. break;
  523. }
  524. }
  525. else{
  526. switch (status) {
  527. case 0:
  528. {
  529. print_status=@"Quote";
  530. break;
  531. }
  532. case 1:
  533. {
  534. print_status=@"Saved";
  535. break;
  536. }
  537. case 3:
  538. {
  539. //canceled
  540. print_status=@"Canceled";
  541. break;
  542. }
  543. default:
  544. break;
  545. }
  546. }
  547. NSString* sender_name = [self textAtColumn:1 statement:statement];
  548. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  549. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  550. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  551. NSString* sender_email = [self textAtColumn:5 statement:statement];
  552. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  553. NSString* billing_name = [self textAtColumn:7 statement:statement];
  554. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  555. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  556. if(billing_phone.length>0)
  557. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  558. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  559. if(billing_fax.length>0)
  560. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  561. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  562. [billto_arr0 addObject:billing_phone];
  563. [billto_arr0 addObject:billing_fax];
  564. NSString* billing_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  565. NSString* billing_email = [self textAtColumn:11 statement:statement];
  566. if(billing_email.length>0)
  567. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  568. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  569. if(billing_contact.length>0)
  570. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  571. NSString* receive_name = [self textAtColumn:12 statement:statement];
  572. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  573. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  574. if(receive_phone.length>0)
  575. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  576. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  577. if(receive_fax.length>0)
  578. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  579. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  580. [receive_arr0 addObject:receive_phone];
  581. [receive_arr0 addObject:receive_fax];
  582. NSString* receive_email = [self textAtColumn:16 statement:statement];
  583. NSString* receive_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  584. if(receive_email.length>0)
  585. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  586. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  587. if(receive_contact.length>0)
  588. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  589. NSString* poNumber = [self textAtColumn:17 statement:statement];
  590. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  591. NSString* carrier = [self textAtColumn:19 statement:statement];
  592. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  593. NSString* terms = [self textAtColumn:21 statement:statement];
  594. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  595. NSString* port_destination = [self textAtColumn:23 statement:statement];
  596. NSString* etd = [self textAtColumn:24 statement:statement];
  597. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  598. if(sign_picpath.length==0)
  599. sign_picpath=nil;
  600. NSString* create_date = [self textAtColumn:27 statement:statement];
  601. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  602. NSString* general_notes=[self textAtColumn:33 statement:statement];
  603. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  604. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  605. if(general_notes.length>0)
  606. {
  607. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  608. row2[@"item_0"]=@{@"g_notes":general_notes };
  609. row2[@"count"]=[NSNumber numberWithInt:1];
  610. datasource[@"row2"]=row2;
  611. }
  612. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  613. [billto_arr addObject:billing_ext];
  614. [billto_arr addObject:billing_pf];
  615. [billto_arr addObject:billing_contact];
  616. [billto_arr addObject:billing_email];
  617. NSString* billto=[RAUtils arr2string:billto_arr separator:@"\r\n" trim:true];
  618. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  619. [receive_arr addObject:receive_ext];
  620. [receive_arr addObject:receive_pf];
  621. [receive_arr addObject:receive_contact];
  622. [receive_arr addObject:receive_email];
  623. NSString* receive=[RAUtils arr2string:receive_arr separator:@"\r\n" trim:true];
  624. float shipping = sqlite3_column_double(statement, 28);
  625. float lift_gate_value = sqlite3_column_double(statement, 29);
  626. float handling_fee_value = sqlite3_column_double(statement, 30);
  627. data[@"company"]=sender_name;
  628. data[@"shipfrom_addr"]=sender_ext;
  629. data[@"shipfrom_phone"]=sender_phone;
  630. data[@"shipfrom_fax"]=sender_fax;
  631. data[@"shipfrom_email"]=sender_email;
  632. data[@"warehouse_name"]=warehouse_name;
  633. data[@"bill_to_name"]=billing_name;
  634. data[@"bill_to_ext"]=billto;
  635. data[@"ship_to_name"]=receive_name;
  636. data[@"ship_to_ext"]=receive;
  637. data[@"po_no"]=poNumber;
  638. data[@"rep"]=sales_rep;
  639. data[@"e_schdule"]=etd;
  640. data[@"sales_terms"]=sales_terms;
  641. data[@"port_destination"]=port_destination;
  642. data[@"terms"]=terms;
  643. data[@"carrier"]=carrier;
  644. data[@"vendor_no"]=vendor_no;
  645. data[@"customer_sign"]=sign_picpath;
  646. data[@"print_status"]=print_status;
  647. data[@"create_date"]=create_date;
  648. NSString *sql_cart=[ 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,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id ",order_code];;
  649. sqlite3_stmt * statement_cart;
  650. // NSDate *date1 = [NSDate date];
  651. int count=0;
  652. int cart_count=0;
  653. float credits=0;
  654. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  655. if ( dbresult== SQLITE_OK)
  656. {
  657. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  658. int row1count=0;
  659. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  660. {
  661. NSDate *row_date = [NSDate date];
  662. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  663. // int product_id = sqlite3_column_int(statement_cart, 0);
  664. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  665. int item_id = sqlite3_column_int(statement_cart, 7);
  666. double discount = sqlite3_column_double(statement_cart, 2);
  667. NSString* Price=nil;
  668. if(str_price==nil)
  669. {
  670. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  671. // NSDate *price_date = [NSDate date];
  672. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  673. // DebugLog(@"price time interval");
  674. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  675. if(price==nil)
  676. Price=@"No Price.";
  677. else
  678. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  679. }
  680. else
  681. {
  682. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  683. }
  684. float dunitprice=0;
  685. int item_count = sqlite3_column_int(statement_cart, 3);
  686. NSString* Amount=@"";
  687. if(![Price isEqualToString:@"No Price."])
  688. {
  689. dunitprice=[Price floatValue];
  690. dunitprice=dunitprice* (1-discount/100.0);
  691. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  692. credits+=dunitprice*item_count;
  693. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  694. }
  695. else
  696. {
  697. Price=@"";
  698. }
  699. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  700. // NSString *nsline_note=nil;
  701. // if(line_note!=nil)
  702. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  703. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  704. NSString *nsname=nil;
  705. if(name!=nil)
  706. nsname= [[NSString alloc]initWithUTF8String:name];
  707. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  708. NSString *nsdescription=nil;
  709. if(description!=nil)
  710. nsdescription= [[NSString alloc]initWithUTF8String:description];
  711. // int stockUom = sqlite3_column_int(statement_cart, 8);
  712. // int _id = sqlite3_column_int(statement_cart, 9);
  713. // NSDate *subtotal_date = [NSDate date];
  714. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  715. // DebugLog(@"subtotal_date time interval");
  716. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  717. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  718. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  719. // int carton=[bsubtotaljson[@"carton"] intValue];
  720. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  721. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  722. // NSDate *img_date = [NSDate date];
  723. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  724. // DebugLog(@"img_date time interval");
  725. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  726. itemjson[@"model"]=nsname;
  727. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  728. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  729. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  730. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  731. // itemjson[@"check"]=@"true";
  732. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  733. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  734. itemjson[@"unit_price"]=Price;
  735. itemjson[@"amount"]=Amount;
  736. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  737. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  738. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  739. // itemjson[@"note"]=nsline_note;
  740. // NSDate *date2 = [NSDate date];
  741. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  742. // DebugLog(@"model_bundle time interval");
  743. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  744. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  745. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  746. row1count++;
  747. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false];
  748. for(int c=0;c<[combine[@"count"] intValue];c++)
  749. {
  750. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  751. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  752. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  753. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue]];
  754. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  755. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  756. row1count++;
  757. }
  758. count++;
  759. // DebugLog(@"row time interval");
  760. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  761. }
  762. row1[@"count"]=[NSNumber numberWithInt:row1count];
  763. datasource[@"row1"]=row1;
  764. data[@"table_model"]=datasource;
  765. sqlite3_finalize(statement_cart);
  766. }
  767. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  768. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  769. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  770. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  771. if(credits>0)
  772. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  773. else
  774. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  775. if(shipping>0)
  776. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  777. else
  778. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  779. if(lift_gate_value>0)
  780. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  781. else
  782. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  783. if(handling_fee_value>0)
  784. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  785. else
  786. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  787. float total = credits+shipping+lift_gate_value+handling_fee_value;
  788. if(total>0)
  789. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  790. else
  791. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  792. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  793. fee[@"row0"]=fee_row0;
  794. data[@"table_fee"]=fee;
  795. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  796. // ret[@"sign_url"] = sign_url;
  797. //
  798. // customer_contact = [self textAtColumn:52 statement:statement];
  799. // customer_email = [self textAtColumn:53 statement:statement];
  800. // customer_phone = [self textAtColumn:54 statement:statement];
  801. // customer_fax = [self textAtColumn:55 statement:statement];
  802. //
  803. //
  804. // int offline_edit=sqlite3_column_int(statement, 56);
  805. //
  806. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  807. //
  808. // char *soid = (char*)sqlite3_column_text(statement, 1);
  809. // if(soid==nil)
  810. // soid= "";
  811. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  812. // // so#
  813. // ret[@"so#"] = nssoid;
  814. //
  815. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  816. // if(poNumber==nil)
  817. // poNumber= "";
  818. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  819. //
  820. //
  821. //
  822. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  823. // if(create_time==nil)
  824. // create_time= "";
  825. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  826. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  827. //
  828. // int status = sqlite3_column_int(statement, 4);
  829. // int erpStatus = sqlite3_column_int(statement, 49);
  830. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  831. //
  832. // // status
  833. // if (status > 1 && status != 3) {
  834. // status = erpStatus;
  835. // } else if (status == 3) {
  836. // status = 15;
  837. // }
  838. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  839. // ret[@"order_status"] = nsstatus;
  840. //
  841. //
  842. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  843. // if(company_name==nil)
  844. // company_name= "";
  845. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  846. // // company name
  847. // ret[@"company_name"] = nscompany_name;
  848. //
  849. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  850. // if(customer_contact==nil)
  851. // customer_contact= "";
  852. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  853. //
  854. //
  855. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  856. // if(addr_1==nil)
  857. // addr_1="";
  858. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  859. //
  860. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  861. // if(addr_2==nil)
  862. // addr_2="";
  863. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  864. //
  865. //
  866. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  867. // if(addr_3==nil)
  868. // addr_3="";
  869. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  870. //
  871. //
  872. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  873. // if(addr_4==nil)
  874. // addr_4="";
  875. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  876. //
  877. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  878. // [arr_addr addObject:nsaddr_1];
  879. // [arr_addr addObject:nsaddr_2];
  880. // [arr_addr addObject:nsaddr_3];
  881. // [arr_addr addObject:nsaddr_4];
  882. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  883. //
  884. //
  885. // char *logist = (char*)sqlite3_column_text(statement, 11);
  886. // if(logist==nil)
  887. // logist= "";
  888. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  889. // if (status == -11 || status == 10 || status == 11) {
  890. // nslogist = [self textAtColumn:59 statement:statement];
  891. // };
  892. //
  893. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  894. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  895. // shipping = @"Shipping To Be Quoted";
  896. // } else {
  897. // shippingFee = sqlite3_column_double(statement, 12);
  898. // }
  899. //
  900. // // Shipping
  901. // ret[@"Shipping"] = shipping;
  902. //
  903. // int have_lift_gate = sqlite3_column_int(statement, 17);
  904. // lift_gate = sqlite3_column_double(statement, 13);
  905. // // Liftgate Fee(No loading dock)
  906. // if (!have_lift_gate) {
  907. // lift_gate = 0;
  908. // }
  909. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  910. // if (sqlite3_column_int(statement, 57)) {
  911. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  912. // }
  913. //
  914. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  915. // if(general_notes==nil)
  916. // general_notes= "";
  917. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  918. //
  919. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  920. // if(internal_notes==nil)
  921. // internal_notes= "";
  922. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  923. //
  924. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  925. // if(payment_type==nil)
  926. // payment_type= "";
  927. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  928. //
  929. //
  930. //
  931. // // order info
  932. // orderinfo = [self textFileName:@"order_info.html"];
  933. //
  934. //
  935. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  936. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  937. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  938. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  939. //
  940. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  941. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  942. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  943. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  944. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  945. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  946. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  947. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  948. //
  949. // NSString *payment = nil;
  950. // if([nspayment_type isEqualToString:@"Credit Card"])
  951. // {
  952. // payment = [self textFileName:@"creditcardpayment.html"];
  953. //
  954. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  955. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  956. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  957. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  958. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  959. // NSString *card_type = [self textAtColumn:42 statement:statement];
  960. // if (card_type.length > 0) { // 显示星号
  961. // card_type = @"****";
  962. // }
  963. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  964. // if (card_number.length > 0 && card_number.length > 4) {
  965. // for (int i = 0; i < card_number.length - 4; i++) {
  966. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  967. // }
  968. // } else {
  969. // card_number = @"";
  970. // }
  971. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  972. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  973. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  974. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  975. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  976. // card_expiration = @"****";
  977. // }
  978. //
  979. //
  980. // NSString *card_city = [self textAtColumn:46 statement:statement];
  981. // NSString *card_state = [self textAtColumn:47 statement:statement];
  982. //
  983. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  984. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  985. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  986. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  987. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  988. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  989. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  990. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  991. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  992. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  993. //
  994. //
  995. // }
  996. // else
  997. // {
  998. // payment=[self textFileName:@"normalpayment.html"];
  999. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1000. // }
  1001. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1002. //
  1003. // ret[@"result"]= [NSNumber numberWithInt:2];
  1004. //
  1005. // // more info
  1006. // moreInfo = [self textFileName:@"more_info.html"];
  1007. //
  1008. //
  1009. // // ShipToCompany_or_&nbsp
  1010. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1011. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1012. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1013. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1014. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1015. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1016. //
  1017. //
  1018. // // ShipFromCompany_or_&nbsp
  1019. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1020. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1021. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1022. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1023. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1024. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1025. //
  1026. //
  1027. // // FreightBillToCompany_or_&nbsp
  1028. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1029. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1030. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1031. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1032. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1033. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1034. //
  1035. //
  1036. // // MerchandiseBillToCompany_or_&nbsp
  1037. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1038. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1039. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1040. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1041. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1042. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1043. //
  1044. //
  1045. // // ReturnToCompany_or_&nbsp
  1046. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1047. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1048. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1049. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1050. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1051. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1052. // //
  1053. //
  1054. // // DebugLog(@"more info : %@",moreInfo);
  1055. //
  1056. // // handling fee
  1057. // handlingFee = sqlite3_column_double(statement, 33);
  1058. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1059. // if (sqlite3_column_int(statement, 58)) {
  1060. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1061. // }
  1062. // //
  1063. //
  1064. // // customer info
  1065. // customerID = [self textAtColumn:36 statement:statement];
  1066. //
  1067. // // mode
  1068. // ret[@"mode"] = appDelegate.mode;
  1069. //
  1070. // // model_count
  1071. // ret[@"model_count"] = @(0);
  1072. }
  1073. sqlite3_finalize(statement);
  1074. }
  1075. [iSalesDB close_db:db];
  1076. data[@"order_type"]=order_type;
  1077. /*
  1078. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1079. // "customer_email" = "Shui Hu";
  1080. // "customer_fax" = "";
  1081. // "customer_first_name" = F;
  1082. // "customer_last_name" = L;
  1083. // "customer_name" = ",da He Xiang Dong Liu A";
  1084. // "customer_phone" = "Hey Xuan Feng";
  1085. contactInfo[@"customer_phone"] = customer_phone;
  1086. contactInfo[@"customer_fax"] = customer_fax;
  1087. contactInfo[@"customer_email"] = customer_email;
  1088. NSString *first_name = @"";
  1089. NSString *last_name = @"";
  1090. if ([customer_contact isEqualToString:@""]) {
  1091. } else if ([customer_contact containsString:@" "]) {
  1092. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1093. first_name = [customer_contact substringToIndex:first_space_index];
  1094. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1095. }
  1096. contactInfo[@"customer_first_name"] = first_name;
  1097. contactInfo[@"customer_last_name"] = last_name;
  1098. ret[@"customerInfo"] = contactInfo;
  1099. // models
  1100. if (nssoid) {
  1101. __block double TotalCuft = 0;
  1102. __block double TotalWeight = 0;
  1103. __block int TotalCarton = 0;
  1104. __block double allItemPrice = 0;
  1105. 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];
  1106. sqlite3 *db1 = [iSalesDB get_db];
  1107. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1108. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1109. int product_id = sqlite3_column_int(stmt, 0);
  1110. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1111. int item_id = sqlite3_column_int(stmt, 7);
  1112. NSString* Price=nil;
  1113. if(str_price==nil)
  1114. {
  1115. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1116. if(price==nil)
  1117. Price=@"No Price.";
  1118. else
  1119. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1120. }
  1121. else
  1122. {
  1123. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1124. }
  1125. double discount = sqlite3_column_double(stmt, 2);
  1126. int item_count = sqlite3_column_int(stmt, 3);
  1127. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1128. NSString *nsline_note=nil;
  1129. if(line_note!=nil)
  1130. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1131. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1132. // NSString *nsname = nil;
  1133. // if(name!=nil)
  1134. // nsname= [[NSString alloc]initWithUTF8String:name];
  1135. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1136. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1137. // NSString *nsdescription=nil;
  1138. // if(description!=nil)
  1139. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1140. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1141. // int stockUom = sqlite3_column_int(stmt, 8);
  1142. // int _id = sqlite3_column_int(stmt, 9);
  1143. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1144. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1145. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1146. int carton=[bsubtotaljson[@"carton"] intValue];
  1147. TotalCuft += cuft;
  1148. TotalWeight += weight;
  1149. TotalCarton += carton;
  1150. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1151. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1152. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1153. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1154. itemjson[@"note"]=nsline_note;
  1155. itemjson[@"origin_price"] = Price;
  1156. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1157. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1158. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1159. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1160. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1161. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1162. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1163. if(itemjson[@"combine"] != nil)
  1164. {
  1165. // int citem=0;
  1166. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1167. for(int bc=0;bc<bcount;bc++)
  1168. {
  1169. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1170. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1171. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1172. subTotal += uprice * modulus * item_count;
  1173. }
  1174. }
  1175. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1176. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1177. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1178. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1179. allItemPrice += subTotal;
  1180. }];
  1181. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1182. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1183. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1184. // payments/Credits
  1185. // payments_and_credist = sqlite3_column_double(statement, 34);
  1186. payments_and_credist = allItemPrice;
  1187. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1188. // // total
  1189. // totalPrice = sqlite3_column_double(statement, 35);
  1190. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1191. } else {
  1192. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1193. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1194. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1195. // payments/Credits
  1196. payments_and_credist = 0;
  1197. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1198. }
  1199. // total
  1200. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1201. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1202. ret[@"order_info"]= orderinfo;
  1203. ret[@"more_order_info"] = moreInfo;
  1204. return [RAUtils dict2data:ret];
  1205. */
  1206. DebugLog(@"debug sales order data:%@", [RAUtils dict2string:data]);
  1207. return data;
  1208. }
  1209. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1210. {
  1211. NSString* template_file=nil;
  1212. switch([params[@"thumb"] intValue])
  1213. {
  1214. case 0:
  1215. template_file= @"so.json";
  1216. break;
  1217. case 1:
  1218. template_file= @"so_thumb.json";
  1219. break;
  1220. default:
  1221. template_file= @"so.json";
  1222. }
  1223. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1224. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1225. if(data[@"table_model"][@"row2"]==nil)
  1226. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1227. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1228. DebugLog(@"%@",file);
  1229. // return nil;
  1230. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1231. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1232. if (file) {
  1233. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1234. [dic setObject:file forKey:@"pdf_path"];
  1235. dic[@"isLocalFile"]=@"true";
  1236. return [RAUtils dict2data:dic];
  1237. }
  1238. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1239. dic[@"isLocalFile"]=@"true";
  1240. return [RAUtils dict2data:dic];
  1241. }
  1242. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1243. {
  1244. NSString* template_file=nil;
  1245. switch([params[@"pdf_style"] intValue])
  1246. {
  1247. case 0:
  1248. template_file= @"portfolio_2x3.json";
  1249. break;
  1250. case 1:
  1251. template_file= @"portfolio_3x2.json";
  1252. break;
  1253. default:
  1254. template_file= @"portfolio_3x2.json";
  1255. }
  1256. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1257. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1258. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1259. DebugLog(@"%@",file);
  1260. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1261. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1262. if (file) {
  1263. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1264. [dic setObject:file forKey:@"pdf_path"];
  1265. dic[@"isLocalFile"]=@"true";
  1266. return [RAUtils dict2data:dic];
  1267. }
  1268. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1269. dic[@"isLocalFile"]=@"true";
  1270. return [RAUtils dict2data:dic];
  1271. }
  1272. +(NSString*) get_offline_soid:(sqlite3*)db
  1273. {
  1274. NSString* soid=nil;
  1275. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1276. [formatter setDateFormat:@"yyMMdd"];
  1277. NSString* date = [formatter stringFromDate:[NSDate date]];
  1278. for(int i=1;i<999;i++)
  1279. {
  1280. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1281. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1282. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1283. if(count==0)
  1284. return soid;
  1285. }
  1286. return nil;
  1287. }
  1288. +(NSArray*) enumOfflineOrder
  1289. {
  1290. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1291. sqlite3 *db = [iSalesDB get_db];
  1292. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1293. sqlite3_stmt * statement;
  1294. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1295. if ( dbresult== SQLITE_OK)
  1296. {
  1297. while (sqlite3_step(statement) == SQLITE_ROW)
  1298. {
  1299. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1300. NSString* nsso_id=nil;
  1301. if(so_id!=nil)
  1302. {
  1303. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1304. }
  1305. [ret addObject:nsso_id];
  1306. }
  1307. sqlite3_finalize(statement);
  1308. }
  1309. [iSalesDB close_db:db];
  1310. return ret;
  1311. }
  1312. +(void) uploadFile:(NSString*) file
  1313. {
  1314. NSData* data = [NSData dataWithContentsOfFile: file];
  1315. UIApplication * app = [UIApplication sharedApplication];
  1316. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1317. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1318. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1319. if(appDelegate.user!=nil)
  1320. [params setValue:appDelegate.user forKey:@"user"];
  1321. // if(appDelegate.contact_id!=nil)
  1322. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1323. if(appDelegate.password!=nil)
  1324. [params setValue:appDelegate.password forKey:@"password"];
  1325. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1326. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1327. } error:nil];
  1328. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1329. NSProgress *progress = nil;
  1330. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1331. if (error) {
  1332. NSString* err_msg = [error localizedDescription];
  1333. DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1334. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1335. DebugLog(@"data string: %@",str);
  1336. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1337. } else {
  1338. DebugLog(@"response ");
  1339. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1340. // 再将NSData转为字符串
  1341. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1342. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1343. DebugLog(@"data string: %@",jsonStr);
  1344. NSDictionary* json = responseObject;
  1345. if([[json valueForKey:@"result"] intValue]==2)
  1346. {
  1347. // NSString* img_url_down = json[@"img_url_aname"];
  1348. // NSString* img_url_up = json[@"img_url"];
  1349. }
  1350. else
  1351. {
  1352. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1353. }
  1354. }
  1355. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1356. }];
  1357. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1358. //
  1359. [uploadTask resume];
  1360. }
  1361. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1362. {
  1363. NSString* serial= [[NSUUID UUID] UUIDString];
  1364. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1365. NSString *cachefolder = [paths objectAtIndex:0];
  1366. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1367. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1368. NSFileManager* fileManager = [NSFileManager defaultManager];
  1369. BOOL bdir=YES;
  1370. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1371. {
  1372. NSError *error = nil;
  1373. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1374. if(!bsuccess)
  1375. DebugLog(@"Create UPLOAD folder failed");
  1376. }
  1377. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1378. ret[@"contact"]=[self prepareContact:serial];
  1379. ret[@"wishlist"]=[self prepareWishlist:serial];
  1380. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1381. ret[@"portfolio"] = [self preparePortfolio:serial];
  1382. ret[@"view_portfolio"] = [self preparePDF:serial];
  1383. NSString* str= [RAUtils dict2string:ret];
  1384. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1385. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1386. NSError *error=nil;
  1387. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1388. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1389. ZipArchive* zip = [[ZipArchive alloc] init];
  1390. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1391. for(int i=0;i<arr_files.count;i++)
  1392. {
  1393. NSString* file=arr_files[i];
  1394. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1395. }
  1396. if( ![zip CloseZipFile2] )
  1397. {
  1398. zippath = @"";
  1399. }
  1400. ret[@"file"]=zippath;
  1401. return ret;
  1402. }
  1403. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1404. {
  1405. if(filename.length==0)
  1406. return false;
  1407. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1408. bool ret=false;
  1409. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1410. NSString *cachefolder = [paths objectAtIndex:0];
  1411. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1412. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1413. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1414. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1415. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1416. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1417. NSFileManager* fileManager = [NSFileManager defaultManager];
  1418. BOOL bdir=NO;
  1419. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1420. {
  1421. NSError *error = nil;
  1422. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1423. {
  1424. ret=false;
  1425. }
  1426. else
  1427. {
  1428. ret=true;
  1429. }
  1430. }
  1431. return ret;
  1432. }
  1433. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1434. {
  1435. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1436. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1437. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1438. NSString* where=@" 1=1";
  1439. if (ver!=nil) {
  1440. where=@"is_dirty=1";
  1441. }
  1442. sqlite3 *db = [iSalesDB get_db];
  1443. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1444. sqlite3_stmt * statement;
  1445. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1446. int count=0;
  1447. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1448. if ( dbresult== SQLITE_OK)
  1449. {
  1450. while (sqlite3_step(statement) == SQLITE_ROW)
  1451. {
  1452. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1453. int _id = sqlite3_column_int(statement, 0);
  1454. int product_id = sqlite3_column_int(statement, 1);
  1455. int item_id = sqlite3_column_int(statement, 2);
  1456. int qty = sqlite3_column_int(statement, 3);
  1457. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1458. NSString* nscreate_time=nil;
  1459. if(create_time!=nil)
  1460. {
  1461. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1462. }
  1463. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1464. NSString* nsmodify_time=nil;
  1465. if(modify_time!=nil)
  1466. {
  1467. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1468. }
  1469. int is_delete = sqlite3_column_int(statement, 6);
  1470. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1471. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1472. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1473. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1474. itemjson[@"create_time"]=nscreate_time;
  1475. itemjson[@"modify_time"]=nsmodify_time;
  1476. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1477. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1478. count++;
  1479. }
  1480. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1481. sqlite3_finalize(statement);
  1482. }
  1483. ret[@"count"]=[NSNumber numberWithInt:count ];
  1484. [iSalesDB close_db:db];
  1485. return ret;
  1486. }
  1487. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1488. {
  1489. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1490. UIApplication * app = [UIApplication sharedApplication];
  1491. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1492. 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 ];
  1493. // 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 ];
  1494. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1495. DebugLog(@"offline_login sql:%@",sqlQuery);
  1496. sqlite3_stmt * statement;
  1497. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1498. int count=0;
  1499. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1500. if ( dbresult== SQLITE_OK)
  1501. {
  1502. while (sqlite3_step(statement) == SQLITE_ROW)
  1503. {
  1504. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1505. int product_id = sqlite3_column_int(statement, 0);
  1506. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1507. int item_id = sqlite3_column_int(statement, 7);
  1508. NSString* Price=nil;
  1509. if(str_price==nil)
  1510. {
  1511. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1512. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1513. if(price==nil)
  1514. Price=@"No Price.";
  1515. else
  1516. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1517. }
  1518. else
  1519. {
  1520. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1521. }
  1522. double discount = sqlite3_column_double(statement, 2);
  1523. int item_count = sqlite3_column_int(statement, 3);
  1524. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1525. NSString *nsline_note=nil;
  1526. if(line_note!=nil)
  1527. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1528. char *name = (char*)sqlite3_column_text(statement, 5);
  1529. NSString *nsname=nil;
  1530. if(name!=nil)
  1531. nsname= [[NSString alloc]initWithUTF8String:name];
  1532. char *description = (char*)sqlite3_column_text(statement, 6);
  1533. NSString *nsdescription=nil;
  1534. if(description!=nil)
  1535. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1536. // int stockUom = sqlite3_column_int(statement, 8);
  1537. // int _id = sqlite3_column_int(statement, 9);
  1538. //
  1539. //
  1540. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1541. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1542. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1543. // int carton=[bsubtotaljson[@"carton"] intValue];
  1544. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1545. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1546. itemjson[@"model"]=nsname;
  1547. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1548. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1549. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1550. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1551. // itemjson[@"check"]=@"true";
  1552. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1553. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1554. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1555. itemjson[@"unit_price"]=Price;
  1556. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1557. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1558. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1559. itemjson[@"note"]=nsline_note;
  1560. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  1561. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1562. count++;
  1563. }
  1564. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1565. sqlite3_finalize(statement);
  1566. }
  1567. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1568. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1569. return ret;
  1570. }
  1571. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1572. {
  1573. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1574. sqlite3 *db = [iSalesDB get_db];
  1575. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1576. // for(int i=0;i<arr_soid.count;i++)
  1577. sqlite3_stmt * statement;
  1578. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1579. //int count=0;
  1580. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1581. if ( dbresult== SQLITE_OK)
  1582. {
  1583. int count=0;
  1584. while (sqlite3_step(statement) == SQLITE_ROW)
  1585. {
  1586. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1587. int _id = sqlite3_column_int(statement, 0);
  1588. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1589. NSString* nssync_data=nil;
  1590. if(sync_data!=nil)
  1591. {
  1592. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1593. }
  1594. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1595. NSString* nsimg_1=nil;
  1596. if(img_1!=nil)
  1597. {
  1598. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1599. [self copy_upImg:serial file:nsimg_1];
  1600. }
  1601. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1602. NSString* nsso_no=nil;
  1603. if(so_no!=nil)
  1604. {
  1605. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1606. }
  1607. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1608. itemjson[@"order_type"]=@"submit order";
  1609. else
  1610. itemjson[@"order_type"]=@"archive order";
  1611. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1612. itemjson[@"json_data"]= nssync_data;
  1613. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1614. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1615. count++;
  1616. }
  1617. ret[@"count"]=[NSNumber numberWithInt:count ];
  1618. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1619. }
  1620. sqlite3_finalize(statement);
  1621. [iSalesDB close_db:db];
  1622. return ret;
  1623. }
  1624. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1625. {
  1626. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1627. sqlite3 *db = [iSalesDB get_db];
  1628. // UIApplication * app = [UIApplication sharedApplication];
  1629. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1630. 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";
  1631. sqlite3_stmt * statement;
  1632. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1633. int count=0;
  1634. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1635. if ( dbresult== SQLITE_OK)
  1636. {
  1637. while (sqlite3_step(statement) == SQLITE_ROW)
  1638. {
  1639. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1640. int _id = sqlite3_column_int(statement, 0);
  1641. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1642. NSString* nsimg_0=nil;
  1643. if(img_0!=nil)
  1644. {
  1645. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1646. [self copy_upImg:serial file:nsimg_0];
  1647. }
  1648. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1649. NSString* nsimg_1=nil;
  1650. if(img_1!=nil)
  1651. {
  1652. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1653. [self copy_upImg:serial file:nsimg_1];
  1654. }
  1655. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1656. NSString* nsimg_2=nil;
  1657. if(img_2!=nil)
  1658. {
  1659. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1660. [self copy_upImg:serial file:nsimg_2];
  1661. }
  1662. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1663. NSString* nssync_data=nil;
  1664. if(sync_data!=nil)
  1665. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1666. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1667. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1668. itemjson[@"json_data"]= nssync_data;
  1669. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1670. count++;
  1671. }
  1672. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1673. sqlite3_finalize(statement);
  1674. }
  1675. ret[@"count"]=[NSNumber numberWithInt:count ];
  1676. [iSalesDB close_db:db];
  1677. return ret;
  1678. }
  1679. +(bool) check_offlinedata
  1680. {
  1681. UIApplication * app = [UIApplication sharedApplication];
  1682. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1683. sqlite3 *db = [iSalesDB get_db];
  1684. NSString * where=@"1=1";
  1685. // if(appDelegate.user!=nil)
  1686. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1687. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1688. [iSalesDB close_db:db];
  1689. if(count==0)
  1690. {
  1691. return false;
  1692. }
  1693. return true;
  1694. //
  1695. //[iSalesDB close_db:db];
  1696. }
  1697. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1698. {
  1699. UIApplication * app = [UIApplication sharedApplication];
  1700. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1701. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1702. sqlite3 *db = [iSalesDB get_db];
  1703. NSString* collectId=params[@"collectId"];
  1704. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1705. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1706. [iSalesDB execSql:sqlQuery db:db];
  1707. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1708. [iSalesDB close_db:db];
  1709. appDelegate.wish_count =count;
  1710. [appDelegate update_count_mark];
  1711. ret[@"result"]= [NSNumber numberWithInt:2];
  1712. return ret;
  1713. }
  1714. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1715. //{
  1716. //
  1717. // UIApplication * app = [UIApplication sharedApplication];
  1718. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1719. //
  1720. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1721. // sqlite3 *db = [iSalesDB get_db];
  1722. // NSString* product_id=params[@"product_id"];
  1723. //
  1724. //
  1725. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1726. //
  1727. //
  1728. // for(int i=0;i<arr.count;i++)
  1729. // {
  1730. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1731. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1732. // if(count==0)
  1733. // {
  1734. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1735. // [iSalesDB execSql:sqlQuery db:db];
  1736. // }
  1737. // }
  1738. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1739. // [iSalesDB close_db:db];
  1740. //
  1741. // appDelegate.wish_count =count;
  1742. //
  1743. // [appDelegate update_count_mark];
  1744. // ret[@"result"]= [NSNumber numberWithInt:2];
  1745. // return ret;
  1746. // //
  1747. // //return ret;
  1748. //}
  1749. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1750. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1751. DebugLog(@"time interval: %lf",interval);
  1752. }
  1753. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1754. {
  1755. UIApplication * app = [UIApplication sharedApplication];
  1756. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1757. int sort = [[params objectForKey:@"sort"] intValue];
  1758. NSString *sort_str = @"";
  1759. switch (sort) {
  1760. case 0:{
  1761. sort_str = @"order by w.modify_time desc";
  1762. }
  1763. break;
  1764. case 1:{
  1765. sort_str = @"order by w.modify_time asc";
  1766. }
  1767. break;
  1768. case 2:{
  1769. sort_str = @"order by m.name asc";
  1770. }
  1771. break;
  1772. case 3:{
  1773. sort_str = @"order by m.name desc";
  1774. }
  1775. break;
  1776. case 4:{
  1777. sort_str = @"order by m.description asc";
  1778. }
  1779. break;
  1780. default:
  1781. break;
  1782. }
  1783. NSString* user = appDelegate.user;
  1784. sqlite3 *db = [iSalesDB get_db];
  1785. // order by w.create_time
  1786. NSString* sqlQuery = [NSString stringWithFormat:@"select w.product_id,m.name||'\n'||m.description,w._id,w.qty from wishlist as w left join product as m on w.product_id=m.product_id where w.is_delete is NULL or w.is_delete != 1 %@ ",sort_str];
  1787. // NSString* sqlQuery = [NSString stringWithFormat:@"select m.product_id,(m.name||'\n'||m.description) as des,w._id from product as m,wishlist as w where m.product_id = w.product_id %@",sort_str];
  1788. sqlite3_stmt * statement;
  1789. NSDate *date1 = [NSDate date];
  1790. NSDate *date2 = nil;
  1791. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1792. int count=0;
  1793. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1794. {
  1795. while (sqlite3_step(statement) == SQLITE_ROW)
  1796. {
  1797. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1798. int product_id = sqlite3_column_double(statement, 0);
  1799. char *description = (char*)sqlite3_column_text(statement, 1);
  1800. if(description==nil)
  1801. description= "";
  1802. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1803. int item_id = sqlite3_column_double(statement, 2);
  1804. NSDate *date_image = [NSDate date];
  1805. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1806. printf("image : ");
  1807. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1808. // char *url = (char*)sqlite3_column_text(statement, 3);
  1809. // if(url==nil)
  1810. // url="";
  1811. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1812. int qty = sqlite3_column_int(statement, 3);
  1813. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1814. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1815. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1816. item[@"description"]= nsdescription;
  1817. item[@"img"]= nsurl;
  1818. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1819. count++;
  1820. }
  1821. printf("total time:");
  1822. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1823. ret[@"count"]= [NSNumber numberWithInt:count];
  1824. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1825. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1826. ret[@"result"]= [NSNumber numberWithInt:2];
  1827. appDelegate.wish_count =count;
  1828. [appDelegate update_count_mark];
  1829. sqlite3_finalize(statement);
  1830. }
  1831. [iSalesDB close_db:db];
  1832. return ret;
  1833. }
  1834. +(NSDictionary*) offline_notimpl
  1835. {
  1836. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1837. ret[@"result"]=@"8";
  1838. ret[@"err_msg"]=@"offline mode does not support this function.";
  1839. return ret;
  1840. }
  1841. +(NSDictionary*) offline_home
  1842. {
  1843. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1844. NSString *cachefolder = [paths objectAtIndex:0];
  1845. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1846. NSData* json =nil;
  1847. json=[NSData dataWithContentsOfFile:img_cache];
  1848. NSError *error=nil;
  1849. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1850. return menu;
  1851. }
  1852. +(NSDictionary*) offline_category_menu
  1853. {
  1854. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1855. NSString *cachefolder = [paths objectAtIndex:0];
  1856. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1857. NSData* json =nil;
  1858. json=[NSData dataWithContentsOfFile:img_cache];
  1859. if(json==nil)
  1860. return nil;
  1861. NSError *error=nil;
  1862. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1863. return menu;
  1864. }
  1865. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1866. {
  1867. NSString* offline_command=params[@"offline_Command"];
  1868. NSDictionary* ret=nil;
  1869. if([offline_command isEqualToString:@"model_NIYMAL"])
  1870. {
  1871. NSString* category = params[@"category"];
  1872. ret = [self refresh_model_NIYMAL:category];
  1873. }
  1874. return ret;
  1875. }
  1876. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1877. {
  1878. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1879. [ret setValue:@"2" forKey:@"result"];
  1880. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1881. sqlite3* db= [iSalesDB get_db];
  1882. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1883. [iSalesDB close_db:db];
  1884. [ret setObject:detail1_section forKey:@"detail_1"];
  1885. return ret;
  1886. }
  1887. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1888. {
  1889. UIApplication * app = [UIApplication sharedApplication];
  1890. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1891. // NSArray* arr1 = [self get_user_all_price_type];
  1892. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1893. // NSSet *set1 = [NSSet setWithArray:arr1];
  1894. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1895. // if(appDelegate.contact_id==nil)
  1896. // set2=[set1 mutableCopy];
  1897. // else
  1898. // [set2 intersectsSet:set1];
  1899. // NSArray *retarr = [set2 allObjects];
  1900. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1901. // sqlite3 *db = [iSalesDB get_db];
  1902. NSString* sqlQuery = nil;
  1903. if(appDelegate.contact_id==nil)
  1904. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type 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];
  1905. else
  1906. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type 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];
  1907. sqlite3_stmt * statement;
  1908. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1909. int count=0;
  1910. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1911. {
  1912. while (sqlite3_step(statement) == SQLITE_ROW)
  1913. {
  1914. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1915. char *name = (char*)sqlite3_column_text(statement, 0);
  1916. if(name==nil)
  1917. name="";
  1918. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1919. // double price = sqlite3_column_double(statement, 1);
  1920. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1921. // if(isnull==nil)
  1922. // item[nsname]= @"No Price";
  1923. // else
  1924. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  1925. char *price = (char*)sqlite3_column_text(statement, 1);
  1926. if(price!=nil)
  1927. {
  1928. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1929. nsprice=[AESCrypt fastdecrypt:nsprice];
  1930. if(nsprice.length>0)
  1931. {
  1932. double dp= [nsprice doubleValue];
  1933. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  1934. }
  1935. }
  1936. else
  1937. {
  1938. item[nsname]= @"No Price";
  1939. }
  1940. // int type= sqlite3_column_int(statement, 2);
  1941. // item[@"type"]=[NSNumber numberWithInt:type ];
  1942. // item[nsname]= nsprice;
  1943. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1944. count++;
  1945. }
  1946. ret[@"count"]= [NSNumber numberWithInt:count];
  1947. sqlite3_finalize(statement);
  1948. }
  1949. // [iSalesDB close_db:db];
  1950. return ret;
  1951. }
  1952. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  1953. //{
  1954. // NSArray* arr1 = [self get_user_all_price_type:db];
  1955. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1956. //
  1957. // // NSSet *set1 = [NSSet setWithArray:arr1];
  1958. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1959. // // [set2 intersectsSet:set1];
  1960. // //
  1961. // //
  1962. // // NSArray *retarr = [set2 allObjects];
  1963. //
  1964. // NSString* whereprice=nil;
  1965. // if(contact_id==nil)
  1966. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1967. // else
  1968. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1969. //
  1970. //
  1971. // // sqlite3 *db = [iSalesDB get_db];
  1972. //
  1973. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  1974. // sqlite3_stmt * statement;
  1975. //
  1976. //
  1977. // NSNumber* ret = nil;
  1978. // double dprice=DBL_MAX;
  1979. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1980. // {
  1981. //
  1982. //
  1983. // while (sqlite3_step(statement) == SQLITE_ROW)
  1984. // {
  1985. //
  1986. // // double val = sqlite3_column_double(statement, 0);
  1987. // char *price = (char*)sqlite3_column_text(statement, 0);
  1988. // if(price!=nil)
  1989. // {
  1990. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1991. // nsprice=[AESCrypt fastdecrypt:nsprice];
  1992. // if(nsprice.length>0)
  1993. // {
  1994. // double dp= [nsprice doubleValue];
  1995. // if(dp<dprice)
  1996. // dprice=dp;
  1997. // }
  1998. // }
  1999. // }
  2000. //
  2001. //
  2002. //
  2003. //
  2004. // sqlite3_finalize(statement);
  2005. //
  2006. //
  2007. //
  2008. //
  2009. // }
  2010. //
  2011. // // [iSalesDB close_db:db];
  2012. //
  2013. // if(dprice==DBL_MAX)
  2014. // ret= nil;
  2015. // else
  2016. // ret= [NSNumber numberWithDouble:dprice];
  2017. // return ret;
  2018. //}
  2019. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2020. {
  2021. NSArray* arr1 = [self get_user_all_price_type:db];
  2022. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2023. NSString* whereprice=nil;
  2024. if(contact_id==nil)
  2025. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2026. else
  2027. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2028. // sqlite3 *db = [iSalesDB get_db];
  2029. NSString *productIdCondition = @"1 = 1";
  2030. if (product_id) {
  2031. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2032. }
  2033. NSString *itemIdCondition = @"";
  2034. if (item_id) {
  2035. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2036. }
  2037. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2038. sqlite3_stmt * statement;
  2039. NSNumber* ret = nil;
  2040. double dprice=DBL_MAX;
  2041. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2042. {
  2043. while (sqlite3_step(statement) == SQLITE_ROW)
  2044. {
  2045. // double val = sqlite3_column_double(statement, 0);
  2046. char *price = (char*)sqlite3_column_text(statement, 0);
  2047. if(price!=nil)
  2048. {
  2049. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2050. nsprice=[AESCrypt fastdecrypt:nsprice];
  2051. if(nsprice.length>0)
  2052. {
  2053. double dp= [nsprice doubleValue];
  2054. if(dp<dprice)
  2055. dprice=dp;
  2056. }
  2057. }
  2058. }
  2059. sqlite3_finalize(statement);
  2060. }
  2061. // [iSalesDB close_db:db];
  2062. if(dprice==DBL_MAX)
  2063. ret= nil;
  2064. else
  2065. ret= [NSNumber numberWithDouble:dprice];
  2066. return ret;
  2067. }
  2068. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2069. {
  2070. NSArray* ret=nil;
  2071. // sqlite3 *db = [iSalesDB get_db];
  2072. // no customer assigned , use login user contact_id
  2073. UIApplication * app = [UIApplication sharedApplication];
  2074. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2075. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2076. sqlite3_stmt * statement;
  2077. // int count=0;
  2078. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2079. {
  2080. if (sqlite3_step(statement) == SQLITE_ROW)
  2081. {
  2082. char *val = (char*)sqlite3_column_text(statement, 0);
  2083. if(val==nil)
  2084. val="";
  2085. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2086. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2087. }
  2088. sqlite3_finalize(statement);
  2089. }
  2090. // [iSalesDB close_db:db];
  2091. return ret;
  2092. }
  2093. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2094. {
  2095. // sqlite3 *db = [iSalesDB get_db];
  2096. if(contact_id==nil)
  2097. {
  2098. // no customer assigned , use login user contact_id
  2099. UIApplication * app = [UIApplication sharedApplication];
  2100. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2101. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2102. sqlite3_stmt * statement;
  2103. // int count=0;
  2104. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2105. {
  2106. if (sqlite3_step(statement) == SQLITE_ROW)
  2107. {
  2108. char *val = (char*)sqlite3_column_text(statement, 0);
  2109. if(val==nil)
  2110. val="";
  2111. contact_id = [[NSString alloc]initWithUTF8String:val];
  2112. }
  2113. sqlite3_finalize(statement);
  2114. }
  2115. if(contact_id.length<=0)
  2116. {
  2117. // [iSalesDB close_db:db];
  2118. return nil;
  2119. }
  2120. }
  2121. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2122. sqlite3_stmt * statement;
  2123. NSArray* ret=nil;
  2124. // int count=0;
  2125. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2126. {
  2127. if (sqlite3_step(statement) == SQLITE_ROW)
  2128. {
  2129. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2130. if(price_type==nil)
  2131. price_type="";
  2132. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2133. if(nsprice_type.length>0)
  2134. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2135. else
  2136. ret=nil;
  2137. }
  2138. sqlite3_finalize(statement);
  2139. }
  2140. // [iSalesDB close_db:db];
  2141. return ret;
  2142. }
  2143. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2144. {
  2145. NSString* ret= nil;
  2146. // sqlite3 *db = [iSalesDB get_db];
  2147. NSString *sqlQuery = nil;
  2148. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2149. // 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;
  2150. // 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
  2151. if(product_id==nil && model_name)
  2152. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where product_id in (select product_id from product where 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;
  2153. else if (product_id)
  2154. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where i.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  2155. sqlite3_stmt * statement;
  2156. // int count=0;
  2157. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2158. {
  2159. if (sqlite3_step(statement) == SQLITE_ROW)
  2160. {
  2161. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2162. if(imgurl==nil)
  2163. imgurl="";
  2164. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2165. ret=nsimgurl;
  2166. }
  2167. sqlite3_finalize(statement);
  2168. }
  2169. else
  2170. {
  2171. [ret setValue:@"8" forKey:@"result"];
  2172. }
  2173. // [iSalesDB close_db:db];
  2174. DebugLog(@"data string: %@",ret );
  2175. return ret;
  2176. }
  2177. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2178. {
  2179. NSString* ret= nil;
  2180. sqlite3 *db = [iSalesDB get_db];
  2181. NSString *sqlQuery = nil;
  2182. if(product_id==nil)
  2183. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2184. else
  2185. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2186. sqlite3_stmt * statement;
  2187. // int count=0;
  2188. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2189. {
  2190. if (sqlite3_step(statement) == SQLITE_ROW)
  2191. {
  2192. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2193. if(default_category==nil)
  2194. default_category="";
  2195. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2196. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2197. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2198. ret=nsdefault_category;
  2199. }
  2200. sqlite3_finalize(statement);
  2201. }
  2202. else
  2203. {
  2204. [ret setValue:@"8" forKey:@"result"];
  2205. }
  2206. [iSalesDB close_db:db];
  2207. DebugLog(@"data string: %@",ret );
  2208. return ret;
  2209. }
  2210. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2211. {
  2212. NSString* model_name = [params valueForKey:@"product_name"];
  2213. NSString* product_id = [params valueForKey:@"product_id"];
  2214. NSString* category = [params valueForKey:@"category"];
  2215. if(category==nil)
  2216. category = [self model_default_category:product_id model_name:model_name];
  2217. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2218. sqlite3 *db = [iSalesDB get_db];
  2219. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2220. NSString *sqlQuery = nil;
  2221. if(product_id==nil)
  2222. 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,ETA,item_id 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='%@';
  2223. else
  2224. 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,ETA,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=%@;
  2225. sqlite3_stmt * statement;
  2226. [ret setValue:@"2" forKey:@"result"];
  2227. [ret setValue:@"3" forKey:@"detail_section_count"];
  2228. // int count=0;
  2229. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2230. {
  2231. if (sqlite3_step(statement) == SQLITE_ROW)
  2232. {
  2233. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2234. char *name = (char*)sqlite3_column_text(statement, 0);
  2235. if(name==nil)
  2236. name="";
  2237. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2238. char *description = (char*)sqlite3_column_text(statement, 1);
  2239. if(description==nil)
  2240. description="";
  2241. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2242. int product_id = sqlite3_column_int(statement, 2);
  2243. char *color = (char*)sqlite3_column_text(statement, 3);
  2244. if(color==nil)
  2245. color="";
  2246. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2247. //
  2248. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2249. // if(legcolor==nil)
  2250. // legcolor="";
  2251. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2252. //
  2253. //
  2254. int availability = sqlite3_column_int(statement, 5);
  2255. //
  2256. int incoming_stock = sqlite3_column_int(statement, 6);
  2257. char *demension = (char*)sqlite3_column_text(statement, 7);
  2258. if(demension==nil)
  2259. demension="";
  2260. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2261. // ,,,,,,,,,
  2262. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2263. if(seat_height==nil)
  2264. seat_height="";
  2265. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2266. char *material = (char*)sqlite3_column_text(statement, 9);
  2267. if(material==nil)
  2268. material="";
  2269. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2270. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2271. if(box_dim==nil)
  2272. box_dim="";
  2273. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2274. char *volume = (char*)sqlite3_column_text(statement, 11);
  2275. if(volume==nil)
  2276. volume="";
  2277. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2278. double weight = sqlite3_column_double(statement, 12);
  2279. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2280. if(model_set==nil)
  2281. model_set="";
  2282. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2283. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2284. if(load_ability==nil)
  2285. load_ability="";
  2286. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2287. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2288. if(default_category==nil)
  2289. default_category="";
  2290. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2291. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2292. if(fabric_content==nil)
  2293. fabric_content="";
  2294. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2295. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2296. if(assembling==nil)
  2297. assembling="";
  2298. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2299. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2300. if(made_in==nil)
  2301. made_in="";
  2302. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2303. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2304. if(special_remarks==nil)
  2305. special_remarks="";
  2306. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2307. int stockUcom = sqlite3_column_double(statement, 20);
  2308. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2309. if(product_group==nil)
  2310. product_group="";
  2311. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2312. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2313. // if(fashion_selector==nil)
  2314. // fashion_selector="";
  2315. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2316. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2317. if(selector_field==nil)
  2318. selector_field="";
  2319. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2320. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2321. if(property_field==nil)
  2322. property_field="";
  2323. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2324. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2325. if(packaging==nil)
  2326. packaging="";
  2327. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2328. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2329. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2330. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2331. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2332. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2333. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2334. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2335. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2336. NSString* Availability=nil;
  2337. if(availability>0)
  2338. Availability=[NSString stringWithFormat:@"%d",availability];
  2339. else
  2340. Availability = @"In Production";
  2341. [img_section setValue:Availability forKey:@"Availability"];
  2342. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2343. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2344. char *eta = (char*)sqlite3_column_text(statement, 25);
  2345. if(eta==nil)
  2346. eta="";
  2347. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2348. [img_section setValue:nseta forKey:@"ETA"];
  2349. int item_id = sqlite3_column_int(statement, 26);
  2350. NSString* Price=nil;
  2351. if(appDelegate.bLogin==false)
  2352. Price=@"Must Sign in.";
  2353. else
  2354. {
  2355. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2356. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2357. if(price==nil)
  2358. Price=@"No Price.";
  2359. else
  2360. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2361. }
  2362. [img_section setValue:Price forKey:@"price"];
  2363. [img_section setValue:nsname forKey:@"model_name"];
  2364. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2365. [ret setObject:img_section forKey:@"img_section"];
  2366. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2367. int detail0_item_count=0;
  2368. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2369. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2370. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2371. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2372. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2373. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2374. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2375. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2376. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2377. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2378. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2379. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2380. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2381. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2382. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2383. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2384. {
  2385. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2386. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2387. }
  2388. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2389. [detail0_section setValue:@"kv" forKey:@"type"];
  2390. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2391. [ret setObject:detail0_section forKey:@"detail_0"];
  2392. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2393. // [detail1_section setValue:@"detail" forKey:@"target"];
  2394. // [detail1_section setValue:@"popup" forKey:@"action"];
  2395. // [detail1_section setValue:@"content" forKey:@"type"];
  2396. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2397. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2398. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2399. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2400. [ret setObject:detail1_section forKey:@"detail_1"];
  2401. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2402. [detail2_section setValue:@"detail" forKey:@"target"];
  2403. [detail2_section setValue:@"popup" forKey:@"action"];
  2404. [detail2_section setValue:@"content" forKey:@"type"];
  2405. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2406. [detail2_section setValue:@"true" forKey:@"single_row"];
  2407. [detail2_section setValue:@"local" forKey:@"data"];
  2408. [ret setObject:detail2_section forKey:@"detail_2"];
  2409. }
  2410. sqlite3_finalize(statement);
  2411. }
  2412. else
  2413. {
  2414. [ret setValue:@"8" forKey:@"result"];
  2415. }
  2416. DebugLog(@"count:%d",count);
  2417. [iSalesDB close_db:db];
  2418. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2419. return ret;
  2420. }
  2421. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2422. NSString* orderCode = [params valueForKey:@"orderCode"];
  2423. NSString* keyword = [params valueForKey:@"keyword"];
  2424. keyword=keyword.lowercaseString;
  2425. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  2426. int limit = [[params valueForKey:@"limit"] intValue];
  2427. int offset = [[params valueForKey:@"offset"] intValue];
  2428. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2429. NSString *limit_str = @"";
  2430. if (limited) {
  2431. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2432. }
  2433. sqlite3 *db = [iSalesDB get_db];
  2434. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2435. UIApplication * app = [UIApplication sharedApplication];
  2436. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2437. NSString *sqlQuery = nil;
  2438. if(exactMatch )
  2439. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name %@;",appDelegate.order_code, keyword,limit_str]; // 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 ;
  2440. else
  2441. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, keyword,keyword,limit_str];// 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
  2442. DebugLog(@"offline_search sql:%@",sqlQuery);
  2443. sqlite3_stmt * statement;
  2444. [ret setValue:@"2" forKey:@"result"];
  2445. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2446. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2447. // int count=0;
  2448. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2449. {
  2450. int i=0;
  2451. while (sqlite3_step(statement) == SQLITE_ROW)
  2452. {
  2453. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2454. // char *name = (char*)sqlite3_column_text(statement, 1);
  2455. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2456. char *name = (char*)sqlite3_column_text(statement, 0);
  2457. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2458. char *description = (char*)sqlite3_column_text(statement, 1);
  2459. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2460. int product_id = sqlite3_column_int(statement, 2);
  2461. // char *url = (char*)sqlite3_column_text(statement, 3);
  2462. // if(url==nil)
  2463. // url="";
  2464. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2465. int wid = sqlite3_column_int(statement, 3);
  2466. int closeout = sqlite3_column_int(statement, 4);
  2467. int cid = sqlite3_column_int(statement, 5);
  2468. int wisdelete = sqlite3_column_int(statement, 6);
  2469. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2470. if(wid !=0 && wisdelete != 1)
  2471. [item setValue:@"true" forKey:@"wish_exists"];
  2472. else
  2473. [item setValue:@"false" forKey:@"wish_exists"];
  2474. if(closeout==0)
  2475. [item setValue:@"false" forKey:@"is_closeout"];
  2476. else
  2477. [item setValue:@"true" forKey:@"is_closeout"];
  2478. if(cid==0)
  2479. [item setValue:@"false" forKey:@"cart_exists"];
  2480. else
  2481. [item setValue:@"true" forKey:@"cart_exists"];
  2482. [item addEntriesFromDictionary:imgjson];
  2483. // [item setValue:nsurl forKey:@"img"];
  2484. [item setValue:nsname forKey:@"fash_name"];
  2485. [item setValue:nsdescription forKey:@"description"];
  2486. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2487. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2488. i++;
  2489. }
  2490. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2491. [ret setObject:items forKey:@"items"];
  2492. sqlite3_finalize(statement);
  2493. }
  2494. DebugLog(@"count:%d",count);
  2495. [iSalesDB close_db:db];
  2496. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2497. return ret;
  2498. }
  2499. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2500. {
  2501. return [self search:params limited:YES];
  2502. }
  2503. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2504. {
  2505. UIApplication * app = [UIApplication sharedApplication];
  2506. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2507. appDelegate.disable_trigger=true;
  2508. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2509. appDelegate.disable_trigger=false;
  2510. //
  2511. // NSString* user = [params valueForKey:@"user"];
  2512. //
  2513. // NSString* password = [params valueForKey:@"password"];
  2514. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2515. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2516. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2517. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2518. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2519. [appDelegate update_count_mark];
  2520. appDelegate.can_show_price =false;
  2521. appDelegate.can_see_price =false;
  2522. appDelegate.can_create_portfolio =false;
  2523. appDelegate.can_create_order =false;
  2524. appDelegate.can_cancel_order =false;
  2525. appDelegate.can_set_cart_price =false;
  2526. appDelegate.can_delete_order =false;
  2527. appDelegate.can_submit_order =false;
  2528. appDelegate.can_set_tearsheet_price =false;
  2529. appDelegate.can_update_contact_info = false;
  2530. appDelegate.save_order_logout = false;
  2531. appDelegate.submit_order_logout = false;
  2532. appDelegate.alert_sold_in_quantities = false;
  2533. appDelegate.ipad_perm =nil ;
  2534. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2535. appDelegate.OrderFilter= nil;
  2536. [appDelegate SetSo:nil];
  2537. [appDelegate set_main_button_panel];
  2538. // sqlite3 *db = [iSalesDB get_db];
  2539. //
  2540. //
  2541. //
  2542. //
  2543. //
  2544. // 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"]];
  2545. //
  2546. //
  2547. //
  2548. //
  2549. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2550. // sqlite3_stmt * statement;
  2551. //
  2552. //
  2553. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2554. //
  2555. //
  2556. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2557. // {
  2558. //
  2559. //
  2560. // if (sqlite3_step(statement) == SQLITE_ROW)
  2561. // {
  2562. //
  2563. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2564. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2565. //
  2566. //
  2567. //
  2568. // int can_show_price = sqlite3_column_int(statement, 0);
  2569. // int can_see_price = sqlite3_column_int(statement, 1);
  2570. //
  2571. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2572. // if(contact_id==nil)
  2573. // contact_id="";
  2574. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2575. //
  2576. // int user_type = sqlite3_column_int(statement, 3);
  2577. //
  2578. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2579. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2580. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2581. // int can_delete_order = sqlite3_column_int(statement, 7);
  2582. // int can_submit_order = sqlite3_column_int(statement, 8);
  2583. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2584. // int can_create_order = sqlite3_column_int(statement, 10);
  2585. //
  2586. //
  2587. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2588. // if(mode==nil)
  2589. // mode="";
  2590. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2591. //
  2592. //
  2593. // char *username = (char*)sqlite3_column_text(statement, 12);
  2594. // if(username==nil)
  2595. // username="";
  2596. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2597. //
  2598. //
  2599. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2600. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2601. // [header setValue:nscontact_id forKey:@"contact_id"];
  2602. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2603. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2604. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2605. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2606. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2607. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2608. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2609. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2610. //
  2611. // [header setValue:nsusername forKey:@"username"];
  2612. //
  2613. //
  2614. // [ret setObject:header forKey:@"header"];
  2615. // [ret setValue:nsmode forKey:@"mode"];
  2616. //
  2617. //
  2618. // }
  2619. //
  2620. //
  2621. //
  2622. // sqlite3_finalize(statement);
  2623. // }
  2624. //
  2625. //
  2626. //
  2627. // [iSalesDB close_db:db];
  2628. //
  2629. //
  2630. //
  2631. //
  2632. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2633. return ret;
  2634. }
  2635. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2636. {
  2637. UIApplication * app = [UIApplication sharedApplication];
  2638. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2639. appDelegate.disable_trigger=true;
  2640. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2641. appDelegate.disable_trigger=false;
  2642. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2643. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2644. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2645. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2646. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2647. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2648. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2649. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2650. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2651. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2652. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2653. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2654. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2655. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2656. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2657. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2658. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2659. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2660. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2661. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2662. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2663. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2664. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2665. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2666. [arr_name addObject:customer_first_name];
  2667. [arr_name addObject:customer_last_name];
  2668. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2669. // default ship from
  2670. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,decrypt(phone) from offline_contact where contact_id = 'NPD';";
  2671. __block NSString *cid = @"";
  2672. __block NSString *name = @"";
  2673. __block NSString *ext = @"";
  2674. __block NSString *contact = @"";
  2675. __block NSString *email = @"";
  2676. __block NSString *fax = @"";
  2677. __block NSString *phone = @"";
  2678. sqlite3 *db = [iSalesDB get_db];
  2679. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2680. cid = [self textAtColumn:0 statement:statment];
  2681. name = [self textAtColumn:1 statement:statment];
  2682. ext = [self textAtColumn:2 statement:statment];
  2683. contact = [self textAtColumn:3 statement:statment];
  2684. email = [self textAtColumn:4 statement:statment];
  2685. fax = [self textAtColumn:5 statement:statment];
  2686. phone = [self textAtColumn:6 statement:statment];
  2687. }];
  2688. NSString* so_id = [self get_offline_soid:db];
  2689. if(so_id==nil)
  2690. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2691. 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,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,sales_terms) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','FOB')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email,customer_cid,customer_name,customer_contact_ext,customer_contact,customer_phone,customer_fax,customer_email];
  2692. int result =[iSalesDB execSql:sql_neworder db:db];
  2693. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2694. //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'
  2695. //soId
  2696. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2697. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2698. sqlite3_stmt * statement;
  2699. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2700. {
  2701. if (sqlite3_step(statement) == SQLITE_ROW)
  2702. {
  2703. // char *name = (char*)sqlite3_column_text(statement, 1);
  2704. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2705. //ret = sqlite3_column_int(statement, 0);
  2706. char *soId = (char*)sqlite3_column_text(statement, 0);
  2707. if(soId==nil)
  2708. soId="";
  2709. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2710. [ret setValue:nssoId forKey:@"soId"];
  2711. [ret setValue:nssoId forKey:@"orderCode"];
  2712. }
  2713. sqlite3_finalize(statement);
  2714. }
  2715. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2716. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2717. [iSalesDB close_db:db];
  2718. return [RAUtils dict2data:ret];
  2719. }
  2720. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2721. {
  2722. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2723. NSString* orderCode = [params valueForKey:@"orderCode"];
  2724. UIApplication * app = [UIApplication sharedApplication];
  2725. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2726. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2727. {
  2728. appDelegate.disable_trigger=true;
  2729. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2730. appDelegate.disable_trigger=false;
  2731. }
  2732. sqlite3 *db = [iSalesDB get_db];
  2733. int cart_count=[self query_ordercartcount:orderCode db:db];
  2734. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2735. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2736. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2737. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2738. [iSalesDB close_db:db];
  2739. return [RAUtils dict2data:ret];
  2740. }
  2741. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2742. {
  2743. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2744. sqlite3 *db = [iSalesDB get_db];
  2745. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2746. int count =0;
  2747. if(params[@"count"]!=nil)
  2748. {
  2749. count = [params[@"count"] intValue];
  2750. }
  2751. NSString* product_id=params[@"product_id"];
  2752. NSString* orderCode=params[@"orderCode"];
  2753. NSString *qty = params[@"qty"];
  2754. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2755. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2756. for(int i=0;i<arr_id.count;i++)
  2757. {
  2758. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2759. int item_qty= count;
  2760. if (qty) {
  2761. item_qty = [qty_arr[i] integerValue];
  2762. }
  2763. if(item_qty==0)
  2764. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2765. NSString* sql=nil;
  2766. sqlite3_stmt *stmt;
  2767. if(_id<0)
  2768. {
  2769. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2770. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2771. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2772. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  2773. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  2774. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2775. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2776. }
  2777. else
  2778. {
  2779. if (qty) {
  2780. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2781. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2782. sqlite3_bind_int(stmt, 1, _id);
  2783. } else {
  2784. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2785. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2786. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2787. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2788. }
  2789. }
  2790. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2791. [iSalesDB execSql:@"ROLLBACK" db:db];
  2792. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2793. [iSalesDB close_db:db];
  2794. DebugLog(@"add to cart error");
  2795. return [RAUtils dict2data:ret];
  2796. }
  2797. }
  2798. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2799. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2800. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2801. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2802. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2803. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2804. [iSalesDB close_db:db];
  2805. return [RAUtils dict2data:ret];
  2806. }
  2807. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  2808. {
  2809. UIApplication * app = [UIApplication sharedApplication];
  2810. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2811. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2812. 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];
  2813. sqlite3_stmt * statement;
  2814. int count=0;
  2815. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2816. {
  2817. while (sqlite3_step(statement) == SQLITE_ROW)
  2818. {
  2819. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  2820. int bitem_id = sqlite3_column_int(statement, 0);
  2821. int bitem_qty = sqlite3_column_int(statement, 1);
  2822. char *name = (char*)sqlite3_column_text(statement, 2);
  2823. if(name==nil)
  2824. name="";
  2825. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  2826. char *description = (char*)sqlite3_column_text(statement, 3);
  2827. if(description==nil)
  2828. description="";
  2829. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  2830. double unit_price = sqlite3_column_double(statement, 4);
  2831. int use_unitprice = sqlite3_column_int(statement, 5);
  2832. if(use_unitprice!=1)
  2833. {
  2834. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  2835. }
  2836. itemjson[@"model"]=nsname;
  2837. itemjson[@"description"]=nsdescription;
  2838. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  2839. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  2840. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  2841. if(compute)
  2842. {
  2843. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  2844. }
  2845. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2846. count++;
  2847. }
  2848. sqlite3_finalize(statement);
  2849. }
  2850. ret[@"count"]=@(count);
  2851. if(count==0)
  2852. return nil;
  2853. else
  2854. return ret;
  2855. }
  2856. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  2857. {
  2858. //compute: add part to subtotal;
  2859. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  2860. dict_item[@(item_id)]=@"1";
  2861. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2862. double cuft=0;
  2863. double weight=0;
  2864. int carton=0;
  2865. 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];
  2866. sqlite3_stmt * statement;
  2867. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2868. {
  2869. if (sqlite3_step(statement) == SQLITE_ROW)
  2870. {
  2871. double ulength = sqlite3_column_double(statement, 0);
  2872. double uwidth = sqlite3_column_double(statement, 1);
  2873. double uheight = sqlite3_column_double(statement, 2);
  2874. double uweight = sqlite3_column_double(statement, 3);
  2875. double mlength = sqlite3_column_double(statement, 4);
  2876. double mwidth = sqlite3_column_double(statement, 5);
  2877. double mheight = sqlite3_column_double(statement, 6);
  2878. double mweight = sqlite3_column_double(statement, 7);
  2879. double ilength = sqlite3_column_double(statement, 8);
  2880. double iwidth = sqlite3_column_double(statement, 9);
  2881. double iheight = sqlite3_column_double(statement, 10);
  2882. double iweight = sqlite3_column_double(statement, 11);
  2883. int pcs = sqlite3_column_int(statement,12);
  2884. int mpack = sqlite3_column_int(statement, 13);
  2885. int ipack = sqlite3_column_int(statement, 14);
  2886. double ucbf = sqlite3_column_double(statement, 15);
  2887. double icbf = sqlite3_column_double(statement, 16);
  2888. double mcbf = sqlite3_column_double(statement, 17);
  2889. if(ipack==0)
  2890. {
  2891. carton= count/mpack ;
  2892. weight = mweight*carton;
  2893. cuft= carton*(mlength*mwidth*mheight);
  2894. int remain=count%mpack;
  2895. if(remain==0)
  2896. {
  2897. //do nothing;
  2898. }
  2899. else
  2900. {
  2901. carton++;
  2902. weight += uweight*remain;
  2903. cuft += (ulength*uwidth*uheight)*remain;
  2904. }
  2905. }
  2906. else
  2907. {
  2908. carton = count/(mpack*ipack);
  2909. weight = mweight*carton;
  2910. cuft= carton*(mlength*mwidth*mheight);
  2911. int remain=count%(mpack*ipack);
  2912. if(remain==0)
  2913. {
  2914. // do nothing;
  2915. }
  2916. else
  2917. {
  2918. carton++;
  2919. int icarton =remain/ipack;
  2920. int iremain=remain%ipack;
  2921. weight += iweight*icarton;
  2922. cuft += (ilength*iwidth*iheight)*icarton;
  2923. if(iremain==0)
  2924. {
  2925. //do nothing;
  2926. }
  2927. else
  2928. {
  2929. weight += uweight*iremain;
  2930. cuft += (ulength*uwidth*uheight)*iremain;
  2931. }
  2932. }
  2933. }
  2934. #ifdef BUILD_NPD
  2935. cuft=ucbf*count;
  2936. weight= uweight*count;
  2937. #endif
  2938. }
  2939. sqlite3_finalize(statement);
  2940. }
  2941. if(compute)
  2942. {
  2943. NSArray * arr_count=nil;
  2944. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  2945. for(int i=0;i<arr_bundle.count;i++)
  2946. {
  2947. dict_item[arr_bundle[i]]=@"1";
  2948. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  2949. cuft+=[bundlejson[@"cuft"] doubleValue];
  2950. weight+=[bundlejson[@"weight"] doubleValue];
  2951. carton+=[bundlejson[@"carton"] intValue];
  2952. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  2953. }
  2954. }
  2955. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  2956. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  2957. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  2958. ret[@"items"]=dict_item;
  2959. return ret;
  2960. }
  2961. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  2962. {
  2963. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  2964. // 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 ];
  2965. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2966. DebugLog(@"offline_login sql:%@",sqlQuery);
  2967. sqlite3_stmt * statement;
  2968. int cart_count=0;
  2969. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2970. if ( dbresult== SQLITE_OK)
  2971. {
  2972. while (sqlite3_step(statement) == SQLITE_ROW)
  2973. {
  2974. int item_id = sqlite3_column_int(statement, 0);
  2975. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  2976. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2977. }
  2978. sqlite3_finalize(statement);
  2979. }
  2980. return cart_count;
  2981. }
  2982. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  2983. {
  2984. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2985. sqlite3 *db = [iSalesDB get_db];
  2986. UIApplication * app = [UIApplication sharedApplication];
  2987. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2988. NSString* orderCode=params[@"orderCode"];
  2989. int sort = [[params objectForKey:@"sort"] intValue];
  2990. NSString *sort_str = @"";
  2991. switch (sort) {
  2992. case 0:{
  2993. sort_str = @"order by c.modify_time desc";
  2994. }
  2995. break;
  2996. case 1:{
  2997. sort_str = @"order by c.modify_time asc";
  2998. }
  2999. break;
  3000. case 2:{
  3001. sort_str = @"order by m.name asc";
  3002. }
  3003. break;
  3004. case 3:{
  3005. sort_str = @"order by m.name desc";
  3006. }
  3007. break;
  3008. case 4:{
  3009. sort_str = @"order by m.description asc";
  3010. }
  3011. break;
  3012. default:
  3013. break;
  3014. }
  3015. 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,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id %@",orderCode,sort_str ];
  3016. // 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 ];
  3017. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3018. DebugLog(@"offline_login sql:%@",sqlQuery);
  3019. sqlite3_stmt * statement;
  3020. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3021. NSDate *date1 = [NSDate date];
  3022. int count=0;
  3023. int cart_count=0;
  3024. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3025. if ( dbresult== SQLITE_OK)
  3026. {
  3027. while (sqlite3_step(statement) == SQLITE_ROW)
  3028. {
  3029. NSDate *row_date = [NSDate date];
  3030. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3031. int product_id = sqlite3_column_int(statement, 0);
  3032. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3033. int item_id = sqlite3_column_int(statement, 7);
  3034. NSString* Price=nil;
  3035. if(str_price==nil)
  3036. {
  3037. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3038. // NSDate *price_date = [NSDate date];
  3039. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3040. // DebugLog(@"price time interval");
  3041. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3042. if(price==nil)
  3043. Price=@"No Price.";
  3044. else
  3045. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3046. }
  3047. else
  3048. {
  3049. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3050. }
  3051. double discount = sqlite3_column_double(statement, 2);
  3052. int item_count = sqlite3_column_int(statement, 3);
  3053. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3054. NSString *nsline_note=nil;
  3055. if(line_note!=nil)
  3056. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3057. char *name = (char*)sqlite3_column_text(statement, 5);
  3058. NSString *nsname=nil;
  3059. if(name!=nil)
  3060. nsname= [[NSString alloc]initWithUTF8String:name];
  3061. char *description = (char*)sqlite3_column_text(statement, 6);
  3062. NSString *nsdescription=nil;
  3063. if(description!=nil)
  3064. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3065. int stockUom = sqlite3_column_int(statement, 8);
  3066. int _id = sqlite3_column_int(statement, 9);
  3067. // NSDate *subtotal_date = [NSDate date];
  3068. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3069. // DebugLog(@"subtotal_date time interval");
  3070. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3071. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3072. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3073. int carton=[bsubtotaljson[@"carton"] intValue];
  3074. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3075. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3076. // NSDate *img_date = [NSDate date];
  3077. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3078. // DebugLog(@"img_date time interval");
  3079. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3080. itemjson[@"model"]=nsname;
  3081. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3082. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3083. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3084. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3085. itemjson[@"check"]=@"true";
  3086. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3087. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3088. itemjson[@"unit_price"]=Price;
  3089. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3090. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3091. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3092. itemjson[@"note"]=nsline_note;
  3093. // NSDate *date2 = [NSDate date];
  3094. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  3095. // DebugLog(@"model_bundle time interval");
  3096. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3097. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3098. count++;
  3099. // DebugLog(@"row time interval");
  3100. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3101. }
  3102. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3103. sqlite3_finalize(statement);
  3104. }
  3105. DebugLog(@"request cart total time interval");
  3106. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3107. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3108. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3109. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3110. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3111. ret[@"count"]=[NSNumber numberWithInt:count ];
  3112. ret[@"mode"]=@"Regular Mode";
  3113. [iSalesDB close_db:db];
  3114. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3115. DebugLog(@"general notes :%@",general_note);
  3116. ret[@"general_note"]= general_note;
  3117. return [RAUtils dict2data:ret];
  3118. }
  3119. +(NSData*) offline_login :(NSMutableDictionary *) params
  3120. {
  3121. NSString* user = [params valueForKey:@"user"];
  3122. NSString* password = [params valueForKey:@"password"];
  3123. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3124. sqlite3 *db = [iSalesDB get_db];
  3125. 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"]];
  3126. DebugLog(@"offline_login sql:%@",sqlQuery);
  3127. sqlite3_stmt * statement;
  3128. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3129. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3130. {
  3131. if (sqlite3_step(statement) == SQLITE_ROW)
  3132. {
  3133. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3134. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3135. int can_show_price = sqlite3_column_int(statement, 0);
  3136. int can_see_price = sqlite3_column_int(statement, 1);
  3137. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3138. if(contact_id==nil)
  3139. contact_id="";
  3140. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3141. int user_type = sqlite3_column_int(statement, 3);
  3142. int can_cancel_order = sqlite3_column_int(statement, 4);
  3143. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3144. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3145. int can_delete_order = sqlite3_column_int(statement, 7);
  3146. int can_submit_order = sqlite3_column_int(statement, 8);
  3147. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3148. int can_create_order = sqlite3_column_int(statement, 10);
  3149. char *mode = (char*)sqlite3_column_text(statement, 11);
  3150. if(mode==nil)
  3151. mode="";
  3152. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3153. char *username = (char*)sqlite3_column_text(statement, 12);
  3154. if(username==nil)
  3155. username="";
  3156. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3157. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3158. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3159. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3160. [header setValue:nscontact_id forKey:@"contact_id"];
  3161. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3162. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3163. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3164. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3165. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3166. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3167. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3168. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3169. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3170. [header setValue:nsusername forKey:@"username"];
  3171. NSError* error=nil;
  3172. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3173. [header setValue:statusFilter forKey:@"statusFilter"];
  3174. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3175. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3176. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3177. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3178. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3179. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3180. [ret setObject:header forKey:@"header"];
  3181. [ret setValue:nsmode forKey:@"mode"];
  3182. }
  3183. sqlite3_finalize(statement);
  3184. }
  3185. [iSalesDB close_db:db];
  3186. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3187. return [RAUtils dict2data:ret];
  3188. }
  3189. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3190. {
  3191. NSString* contactId = [params valueForKey:@"contactId"];
  3192. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3193. sqlite3 *db = [iSalesDB get_db];
  3194. NSString *sqlQuery = nil;
  3195. {
  3196. 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,type from offline_contact where contact_id='%@'",contactId];
  3197. }
  3198. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3199. sqlite3_stmt * statement;
  3200. [ret setValue:@"2" forKey:@"result"];
  3201. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3202. {
  3203. //int i = 0;
  3204. if (sqlite3_step(statement) == SQLITE_ROW)
  3205. {
  3206. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3207. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3208. // int editable = sqlite3_column_int(statement, 0);
  3209. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3210. NSString *nscompany_name =nil;
  3211. if(company_name==nil)
  3212. nscompany_name=@"";
  3213. else
  3214. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3215. char *country = (char*)sqlite3_column_text(statement, 2);
  3216. if(country==nil)
  3217. country="";
  3218. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3219. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3220. // if(addr==nil)
  3221. // addr="";
  3222. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3223. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3224. if(zipcode==nil)
  3225. zipcode="";
  3226. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3227. char *state = (char*)sqlite3_column_text(statement, 5);
  3228. if(state==nil)
  3229. state="";
  3230. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3231. char *city = (char*)sqlite3_column_text(statement, 6);
  3232. if(city==nil)
  3233. city="";
  3234. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3235. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3236. // NSString *nscontact_name = nil;
  3237. // if(contact_name==nil)
  3238. // nscontact_name=@"";
  3239. // else
  3240. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3241. char *phone = (char*)sqlite3_column_text(statement, 8);
  3242. NSString *nsphone = nil;
  3243. if(phone==nil)
  3244. nsphone=@"";
  3245. else
  3246. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3247. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3248. if(contact_id==nil)
  3249. contact_id="";
  3250. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3251. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3252. if(addr_1==nil)
  3253. addr_1="";
  3254. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3255. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3256. if(addr_2==nil)
  3257. addr_2="";
  3258. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3259. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3260. if(addr_3==nil)
  3261. addr_3="";
  3262. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3263. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3264. if(addr_4==nil)
  3265. addr_4="";
  3266. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3267. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3268. if(first_name==nil)
  3269. first_name="";
  3270. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3271. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3272. if(last_name==nil)
  3273. last_name="";
  3274. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3275. char *fax = (char*)sqlite3_column_text(statement, 16);
  3276. NSString *nsfax = nil;
  3277. if(fax==nil)
  3278. nsfax=@"";
  3279. else
  3280. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3281. char *email = (char*)sqlite3_column_text(statement, 17);
  3282. NSString *nsemail = nil;
  3283. if(email==nil)
  3284. nsemail=@"";
  3285. else
  3286. nsemail= [[NSString alloc]initWithUTF8String:email];
  3287. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3288. NSString *nsimg_0 = nil;
  3289. if(img_0==nil)
  3290. nsimg_0=@"";
  3291. else
  3292. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3293. [self copy_bcardImg:nsimg_0];
  3294. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3295. NSString *nsimg_1 = nil;
  3296. if(img_1==nil)
  3297. nsimg_1=@"";
  3298. else
  3299. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3300. [self copy_bcardImg:nsimg_1];
  3301. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3302. NSString *nsimg_2 = nil;
  3303. if(img_2==nil)
  3304. nsimg_2=@"";
  3305. else
  3306. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3307. [self copy_bcardImg:nsimg_2];
  3308. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3309. NSString *nsprice_type = nil;
  3310. if(price_type==nil)
  3311. nsprice_type=@"";
  3312. else
  3313. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3314. char *notes = (char*)sqlite3_column_text(statement, 22);
  3315. NSString *nsnotes = nil;
  3316. if(notes==nil)
  3317. nsnotes=@"";
  3318. else
  3319. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3320. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3321. NSString *nssalesrep = nil;
  3322. if(salesrep==nil)
  3323. nssalesrep=@"";
  3324. else
  3325. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3326. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3327. {
  3328. // decrypt
  3329. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3330. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3331. nsphone=[AESCrypt fastdecrypt:nsphone];
  3332. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3333. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3334. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3335. }
  3336. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3337. [arr_name addObject:nsfirst_name];
  3338. [arr_name addObject:nslast_name];
  3339. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3340. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3341. [arr_ext addObject:nsaddr_1];
  3342. [arr_ext addObject:nsaddr_2];
  3343. [arr_ext addObject:nsaddr_3];
  3344. [arr_ext addObject:nsaddr_4];
  3345. [arr_ext addObject:@"\r\n"];
  3346. [arr_ext addObject:nscity];
  3347. [arr_ext addObject:nsstate];
  3348. [arr_ext addObject:nszipcode];
  3349. [arr_ext addObject:nscountry];
  3350. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  3351. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3352. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3353. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3354. [item setValue:nscountry forKey:@"customer_country"];
  3355. [item setValue:nsphone forKey:@"customer_phone"];
  3356. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3357. [item setValue:nscompany_name forKey:@"customer_name"];
  3358. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3359. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3360. [item setValue:nsext forKey:@"customer_contact_ext"];
  3361. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3362. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3363. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3364. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3365. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3366. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3367. [item setValue:nslast_name forKey:@"customer_last_name"];
  3368. [item setValue:nscity forKey:@"customer_city"];
  3369. [item setValue:nsstate forKey:@"customer_state"];
  3370. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3371. [item setValue:contactId forKey:@"customer_cid"];
  3372. [item setValue:nscontact_name forKey:@"customer_contact"];
  3373. [item setValue:nsfax forKey:@"customer_fax"];
  3374. [item setValue:nsemail forKey:@"customer_email"];
  3375. [item setValue:contact_type forKey:@"customer_contact_type"];
  3376. [ret setObject:item forKey:@"customerInfo"];
  3377. // i++;
  3378. }
  3379. UIApplication * app = [UIApplication sharedApplication];
  3380. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3381. [ret setValue:appDelegate.mode forKey:@"mode"];
  3382. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3383. sqlite3_finalize(statement);
  3384. }
  3385. [iSalesDB close_db:db];
  3386. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3387. return ret;
  3388. }
  3389. + (bool) copy_bcardImg:(NSString*) filename
  3390. {
  3391. if(filename.length==0)
  3392. return false;
  3393. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3394. bool ret=false;
  3395. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3396. NSString *cachefolder = [paths objectAtIndex:0];
  3397. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3398. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3399. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3400. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3401. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3402. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3403. //
  3404. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3405. NSFileManager* fileManager = [NSFileManager defaultManager];
  3406. BOOL bdir=NO;
  3407. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3408. {
  3409. NSError *error = nil;
  3410. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3411. {
  3412. ret=false;
  3413. }
  3414. else
  3415. {
  3416. ret=true;
  3417. }
  3418. // NSError *error = nil;
  3419. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3420. //
  3421. // if(!bsuccess)
  3422. // {
  3423. // DebugLog(@"Create offline_createimg folder failed");
  3424. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3425. // return [RAUtils dict2data:ret];
  3426. // }
  3427. // if(bsuccess)
  3428. // {
  3429. // sqlite3 *db = [self get_db];
  3430. //
  3431. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3432. // [iSalesDB close_db:db];
  3433. // }
  3434. }
  3435. return ret;
  3436. //
  3437. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3438. // if(bsuccess)
  3439. // {
  3440. // NSError *error = nil;
  3441. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3442. // {
  3443. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3444. // }
  3445. // else
  3446. // {
  3447. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3448. //
  3449. // ret[@"img_url_aname"]=filename;
  3450. // ret[@"img_url"]=savedImagePath;
  3451. // }
  3452. // }
  3453. }
  3454. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3455. {
  3456. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3457. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3458. NSString *cachefolder = [paths objectAtIndex:0];
  3459. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3460. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3461. NSFileManager* fileManager = [NSFileManager defaultManager];
  3462. BOOL bdir=YES;
  3463. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3464. {
  3465. NSError *error = nil;
  3466. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3467. if(!bsuccess)
  3468. {
  3469. DebugLog(@"Create offline_createimg folder failed");
  3470. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3471. return [RAUtils dict2data:ret];
  3472. }
  3473. // if(bsuccess)
  3474. // {
  3475. // sqlite3 *db = [self get_db];
  3476. //
  3477. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3478. // [iSalesDB close_db:db];
  3479. // }
  3480. }
  3481. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3482. //JEPG格式
  3483. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3484. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3485. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3486. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3487. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3488. if(bsuccess)
  3489. {
  3490. NSError *error = nil;
  3491. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3492. {
  3493. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3494. }
  3495. else
  3496. {
  3497. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3498. ret[@"img_url_aname"]=filename;
  3499. ret[@"img_url"]=filename;
  3500. }
  3501. }
  3502. else
  3503. {
  3504. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3505. }
  3506. return [RAUtils dict2data:ret];
  3507. }
  3508. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3509. {
  3510. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3511. category = [category substringToIndex:3];
  3512. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3513. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3514. params[@"category"]= category;
  3515. ret[@"params"]= params;
  3516. [ret setValue:@"detail" forKey:@"target"];
  3517. [ret setValue:@"popup" forKey:@"action"];
  3518. [ret setValue:@"content" forKey:@"type"];
  3519. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3520. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3521. [ret setValue:@"true" forKey:@"single_row"];
  3522. [ret setValue:@"true" forKey:@"partial_refresh"];
  3523. // sqlite3 *db = [iSalesDB get_db];
  3524. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3525. 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 ;
  3526. sqlite3_stmt * statement;
  3527. int count = 0;
  3528. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3529. // int count=0;
  3530. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3531. {
  3532. int i=0;
  3533. while (sqlite3_step(statement) == SQLITE_ROW)
  3534. {
  3535. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3536. // char *name = (char*)sqlite3_column_text(statement, 1);
  3537. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3538. char *name = (char*)sqlite3_column_text(statement, 0);
  3539. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3540. int product_id = sqlite3_column_int(statement, 1);
  3541. char *url = (char*)sqlite3_column_text(statement, 2);
  3542. if(url==nil)
  3543. url="";
  3544. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3545. [item setValue:nsurl forKey:@"picture_path"];
  3546. [item setValue:nsname forKey:@"fash_name"];
  3547. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3548. [item setValue:category forKey:@"category"];
  3549. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3550. i++;
  3551. }
  3552. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3553. sqlite3_finalize(statement);
  3554. }
  3555. DebugLog(@"count:%d",count);
  3556. // [iSalesDB close_db:db];
  3557. return ret;
  3558. }
  3559. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3560. {
  3561. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3562. [ret setValue:key forKey:@"key"];
  3563. [ret setValue:value forKey:@"val"];
  3564. return ret;
  3565. }
  3566. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3567. {
  3568. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3569. [ret setValue:@"0" forKey:@"img_count"];
  3570. // sqlite3 *db = [iSalesDB get_db];
  3571. 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 ;
  3572. sqlite3_stmt * statement;
  3573. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3574. {
  3575. int i=0;
  3576. if (sqlite3_step(statement) == SQLITE_ROW)
  3577. {
  3578. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3579. // char *name = (char*)sqlite3_column_text(statement, 1);
  3580. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3581. char *value = (char*)sqlite3_column_text(statement, 0);
  3582. if(value==nil)
  3583. value="";
  3584. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3585. char *key = (char*)sqlite3_column_text(statement, 1);
  3586. if(key==nil)
  3587. key="";
  3588. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3589. [item setValue:nsvalue forKey:@"val"];
  3590. [item setValue:nskey forKey:@"key"];
  3591. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3592. [ret setValue:@"1" forKey:@"count"];
  3593. i++;
  3594. }
  3595. sqlite3_finalize(statement);
  3596. }
  3597. // [iSalesDB close_db:db];
  3598. return ret;
  3599. }
  3600. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3601. {
  3602. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3603. [ret setValue:@"0" forKey:@"count"];
  3604. // sqlite3 *db = [iSalesDB get_db];
  3605. 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;
  3606. sqlite3_stmt * statement;
  3607. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3608. {
  3609. int i=0;
  3610. while (sqlite3_step(statement) == SQLITE_ROW)
  3611. {
  3612. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3613. // char *name = (char*)sqlite3_column_text(statement, 1);
  3614. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3615. char *value = (char*)sqlite3_column_text(statement, 0);
  3616. if(value==nil)
  3617. value="";
  3618. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3619. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3620. if(selector_display==nil)
  3621. selector_display="";
  3622. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3623. int product_id = sqlite3_column_int(statement, 2);
  3624. char *category = (char*)sqlite3_column_text(statement, 3);
  3625. if(category==nil)
  3626. category="";
  3627. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3628. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3629. [item setValue:nsvalue forKey:@"title"];
  3630. [item setValue:url forKey:@"pic_url"];
  3631. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3632. [params setValue:@"2" forKey:@"count"];
  3633. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3634. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3635. [param0 setValue:@"product_id" forKey:@"name"];
  3636. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3637. [param1 setValue:nscategory forKey:@"val"];
  3638. [param1 setValue:@"category" forKey:@"name"];
  3639. [params setObject:param0 forKey:@"param_0"];
  3640. [params setObject:param1 forKey:@"param_1"];
  3641. [item setObject:params forKey:@"params"];
  3642. [ret setValue:nsselector_display forKey:@"name"];
  3643. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3644. i++;
  3645. }
  3646. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3647. [ret setValue:@"switch" forKey:@"action"];
  3648. sqlite3_finalize(statement);
  3649. }
  3650. // [iSalesDB close_db:db];
  3651. return ret;
  3652. }
  3653. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3654. {
  3655. // model 在 category search 显示的图片。
  3656. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3657. // sqlite3 *db = [iSalesDB get_db];
  3658. 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];
  3659. sqlite3_stmt * statement;
  3660. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3661. {
  3662. while (sqlite3_step(statement) == SQLITE_ROW)
  3663. {
  3664. char *url = (char*)sqlite3_column_text(statement, 0);
  3665. if(url==nil)
  3666. url="";
  3667. int type = sqlite3_column_int(statement, 1);
  3668. if(type==0)
  3669. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3670. else
  3671. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3672. }
  3673. sqlite3_finalize(statement);
  3674. }
  3675. // [iSalesDB close_db:db];
  3676. return ret;
  3677. }
  3678. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3679. {
  3680. int item_id=-1;
  3681. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3682. sqlite3_stmt * statement;
  3683. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3684. {
  3685. if (sqlite3_step(statement) == SQLITE_ROW)
  3686. {
  3687. item_id = sqlite3_column_int(statement, 0);
  3688. }
  3689. sqlite3_finalize(statement);
  3690. }
  3691. return item_id;
  3692. }
  3693. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3694. {
  3695. // NSString* ret = @"";
  3696. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3697. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3698. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3699. sqlite3_stmt * statement;
  3700. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3701. {
  3702. while (sqlite3_step(statement) == SQLITE_ROW)
  3703. {
  3704. int bitem_id = sqlite3_column_int(statement, 0);
  3705. int bitem_qty = sqlite3_column_int(statement, 1);
  3706. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3707. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3708. }
  3709. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3710. sqlite3_finalize(statement);
  3711. }
  3712. // if(ret==nil)
  3713. // ret=@"";
  3714. *count=arr_count;
  3715. return arr_bundle;
  3716. }
  3717. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3718. {
  3719. // get default sold qty, return -1 if model not found;
  3720. int ret = -1;
  3721. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3722. sqlite3_stmt * statement;
  3723. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3724. {
  3725. if (sqlite3_step(statement) == SQLITE_ROW)
  3726. {
  3727. ret = sqlite3_column_int(statement, 0);
  3728. }
  3729. sqlite3_finalize(statement);
  3730. }
  3731. return ret;
  3732. }
  3733. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3734. {
  3735. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3736. [ret setValue:@"0" forKey:@"img_count"];
  3737. // sqlite3 *db = [iSalesDB get_db];
  3738. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3739. sqlite3_stmt * statement;
  3740. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3741. {
  3742. int i=0;
  3743. while (sqlite3_step(statement) == SQLITE_ROW)
  3744. {
  3745. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3746. // char *name = (char*)sqlite3_column_text(statement, 1);
  3747. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3748. char *url = (char*)sqlite3_column_text(statement, 0);
  3749. if(url==nil)
  3750. url="";
  3751. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3752. [item setValue:nsurl forKey:@"s"];
  3753. [item setValue:nsurl forKey:@"l"];
  3754. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3755. i++;
  3756. }
  3757. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3758. sqlite3_finalize(statement);
  3759. }
  3760. // [iSalesDB close_db:db];
  3761. return ret;
  3762. }
  3763. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3764. UIApplication * app = [UIApplication sharedApplication];
  3765. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3766. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3767. sqlite3 *db = [iSalesDB get_db];
  3768. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3769. NSString* product_id=params[@"product_id"];
  3770. NSString *item_count_str = params[@"item_count"];
  3771. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3772. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3773. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3774. // NSString *sql = @"";
  3775. for(int i=0;i<arr.count;i++)
  3776. {
  3777. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3778. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3779. __block int cart_count = 0;
  3780. if (!item_count_str) {
  3781. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3782. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3783. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3784. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3785. cart_count = [[model_set_components lastObject] integerValue];
  3786. }];
  3787. }
  3788. if(count==0)
  3789. {
  3790. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3791. sqlite3_stmt *stmt;
  3792. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3793. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3794. if (item_count_arr) {
  3795. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3796. } else {
  3797. sqlite3_bind_int(stmt,2,cart_count);
  3798. }
  3799. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3800. [iSalesDB execSql:@"ROLLBACK" db:db];
  3801. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3802. [iSalesDB close_db:db];
  3803. DebugLog(@"add to wishlist error");
  3804. return ret;
  3805. }
  3806. } else {
  3807. int qty = 0;
  3808. if (item_count_arr) {
  3809. qty = [item_count_arr[i] integerValue];
  3810. } else {
  3811. qty = cart_count;
  3812. }
  3813. sqlQuery = [NSString stringWithFormat:@"update wishlist set qty = 1 where product_id = %@ and (is_delete is null or is_delete != 1);update wishlist set qty = %d,is_delete = 0 where product_id = %@ and is_delete = 1;",arr[i],qty,arr[i]];
  3814. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  3815. [iSalesDB execSql:@"ROLLBACK" db:db];
  3816. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  3817. [iSalesDB close_db:db];
  3818. DebugLog(@"add to wishlist error");
  3819. return ret;
  3820. }
  3821. }
  3822. }
  3823. // [iSalesDB execSql:sql db:db];
  3824. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3825. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3826. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  3827. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  3828. [iSalesDB close_db:db];
  3829. appDelegate.wish_count =count;
  3830. [appDelegate update_count_mark];
  3831. ret[@"result"]= [NSNumber numberWithInt:2];
  3832. return ret;
  3833. }
  3834. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  3835. // 0 category
  3836. // 1 search
  3837. // 2 itemsearch
  3838. NSData *ret = nil;
  3839. NSDictionary *items = nil;
  3840. switch (from) {
  3841. case 0:{
  3842. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  3843. }
  3844. break;
  3845. case 1:{
  3846. items = [[self search:params limited:NO] objectForKey:@"items"];
  3847. }
  3848. break;
  3849. case 2:{
  3850. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  3851. }
  3852. break;
  3853. default:
  3854. break;
  3855. }
  3856. if (!items) {
  3857. return ret;
  3858. }
  3859. int count = [[items objectForKey:@"count"] intValue];
  3860. NSMutableString *product_id_str = [@"" mutableCopy];
  3861. for (int i = 0; i < count; i++) {
  3862. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  3863. NSDictionary *item = [items objectForKey:key];
  3864. NSString *product_id = [item objectForKey:@"product_id"];
  3865. if (i == 0) {
  3866. [product_id_str appendString:product_id];
  3867. } else {
  3868. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  3869. }
  3870. }
  3871. NSString *add_to = [self valueInParams:params key:@"addTo"];
  3872. if ([add_to isEqualToString:@"cart"]) {
  3873. NSString *order_code = [params objectForKey:@"orderCode"];
  3874. if (order_code.length) {
  3875. NSDictionary *newParams = @{
  3876. @"product_id" : product_id_str,
  3877. @"orderCode" : order_code
  3878. };
  3879. ret = [self offline_add2cart:newParams.mutableCopy];
  3880. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3881. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3882. ret = [RAUtils dict2data:retDic];
  3883. }
  3884. } else if([add_to isEqualToString:@"wishlist"]) {
  3885. NSDictionary *newParams = @{
  3886. @"product_id" : product_id_str
  3887. };
  3888. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  3889. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3890. ret = [RAUtils dict2data:retDic];
  3891. } else if([add_to isEqualToString:@"portfolio"]) {
  3892. }
  3893. return ret;
  3894. }
  3895. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  3896. {
  3897. return [self addAll:params from:0];
  3898. }
  3899. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  3900. {
  3901. return [self addAll:params from:1];
  3902. }
  3903. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  3904. {
  3905. return [self addAll:params from:2];
  3906. }
  3907. #pragma mark - Jack
  3908. #warning 做SQL操作时转义!!
  3909. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  3910. // "val_227" : {
  3911. // "check" : 1,
  3912. // "value" : "US United States",
  3913. // "value_id" : "228"
  3914. // },
  3915. if (!countryCode) {
  3916. countryCode = @"US";
  3917. }
  3918. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  3919. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  3920. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  3921. int code_id = sqlite3_column_int(stmt, 3); // id
  3922. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3923. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  3924. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  3925. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3926. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  3927. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3928. }
  3929. long n = *count;
  3930. *count = n + 1;
  3931. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  3932. [container setValue:countryDic forKey:key];
  3933. }] mutableCopy];
  3934. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3935. return ret;
  3936. }
  3937. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  3938. countryCode = [self translateSingleQuote:countryCode];
  3939. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  3940. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3941. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  3942. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  3943. if (name == NULL) {
  3944. name = "";
  3945. }
  3946. if (code == NULL) {
  3947. code = "";
  3948. }
  3949. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3950. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3951. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3952. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3953. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  3954. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3955. }
  3956. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3957. [container setValue:stateDic forKey:key];
  3958. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  3959. DebugLog(@"query all state error: %@",err_msg);
  3960. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3961. // [stateDic setValue:@"Other" forKey:@"value"];
  3962. // [stateDic setValue:@"" forKey:@"value_id"];
  3963. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3964. //
  3965. // if (state_code && [@"" isEqualToString:state_code]) {
  3966. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3967. // }
  3968. //
  3969. // NSString *key = [NSString stringWithFormat:@"val_0"];
  3970. // [container setValue:stateDic forKey:key];
  3971. }] mutableCopy];
  3972. // failure 可以不用了,一样的
  3973. if (ret.allKeys.count == 0) {
  3974. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3975. [stateDic setValue:@"Other" forKey:@"value"];
  3976. [stateDic setValue:@"" forKey:@"value_id"];
  3977. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3978. if (state_code && [@"" isEqualToString:state_code]) {
  3979. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3980. }
  3981. NSString *key = [NSString stringWithFormat:@"val_0"];
  3982. [ret setValue:stateDic forKey:key];
  3983. }
  3984. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3985. return ret;
  3986. }
  3987. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  3988. codeId = [self translateSingleQuote:codeId];
  3989. 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];
  3990. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3991. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  3992. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  3993. if (name == NULL) {
  3994. name = "";
  3995. }
  3996. if (code == NULL) {
  3997. code = "";
  3998. }
  3999. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4000. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4001. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4002. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4003. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4004. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4005. }
  4006. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4007. [container setValue:stateDic forKey:key];
  4008. }] mutableCopy];
  4009. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4010. return ret;
  4011. }
  4012. + (NSDictionary *)offline_getPrice {
  4013. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  4014. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4015. char *name = (char *) sqlite3_column_text(stmt, 1);
  4016. int type = sqlite3_column_int(stmt, 2);
  4017. int orderBy = sqlite3_column_int(stmt, 3);
  4018. if (name == NULL) {
  4019. name = "";
  4020. }
  4021. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4022. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4023. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4024. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  4025. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4026. if (orderBy == 0) {
  4027. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4028. }
  4029. [container setValue:priceDic forKey:key];
  4030. }] mutableCopy];
  4031. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4032. return ret;
  4033. }
  4034. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4035. NSString *sql = @"select _id,type_name from contact_type";
  4036. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4037. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4038. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4039. int _id = sqlite3_column_int(stmt, 0);
  4040. NSString *name = [self textAtColumn:1 statement:stmt];
  4041. NSDictionary *typeDic = @{
  4042. @"value_id" : name,
  4043. @"value" : name,
  4044. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4045. };
  4046. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4047. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  4048. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4049. }];
  4050. return ret;
  4051. }
  4052. + (NSDictionary *)offline_getSalesRep {
  4053. // 首先从offline_login表中取出sales_code
  4054. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4055. NSString *user = app.user;
  4056. user = [self translateSingleQuote:user];
  4057. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4058. __block NSString *user_code = @"";
  4059. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4060. char *code = (char *)sqlite3_column_text(stmt, 0);
  4061. if (code == NULL) {
  4062. code = "";
  4063. }
  4064. user_code = [NSString stringWithUTF8String:code];
  4065. }];
  4066. // 再取所有salesRep
  4067. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4068. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4069. // 1 name 2 code 3 salesrep_id
  4070. char *name = (char *)sqlite3_column_text(stmt, 1);
  4071. char *code = (char *)sqlite3_column_text(stmt, 2);
  4072. int salesrep_id = sqlite3_column_int(stmt, 3);
  4073. if (name == NULL) {
  4074. name = "";
  4075. }
  4076. if (code == NULL) {
  4077. code = "";
  4078. }
  4079. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4080. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4081. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4082. // 比较code 相等则check
  4083. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4084. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4085. }
  4086. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4087. }] mutableCopy];
  4088. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4089. return ret;
  4090. }
  4091. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4092. zipcode = [self translateSingleQuote:zipcode];
  4093. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4094. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4095. char *country = (char *)sqlite3_column_text(stmt, 0);
  4096. char *state = (char *)sqlite3_column_text(stmt, 1);
  4097. char *city = (char *)sqlite3_column_text(stmt, 2);
  4098. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4099. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4100. if (country == NULL) {
  4101. country = "";
  4102. }
  4103. if (state == NULL) {
  4104. state = "";
  4105. }
  4106. if (city == NULL) {
  4107. city = "";
  4108. }
  4109. if (country_code == NULL) {
  4110. country_code = "";
  4111. }
  4112. if (state_code == NULL) {
  4113. state_code = "";
  4114. }
  4115. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4116. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4117. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4118. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4119. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4120. }] mutableCopy];
  4121. return ret;
  4122. }
  4123. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4124. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4125. [item setValue:value forKey:valueKey];
  4126. [dic setValue:item forKey:itemKey];
  4127. }
  4128. + (NSString *)countryCodeByid:(NSString *)code_id {
  4129. NSString *ret = nil;
  4130. code_id = [self translateSingleQuote:code_id];
  4131. sqlite3 *db = [iSalesDB get_db];
  4132. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4133. sqlite3_stmt * statement;
  4134. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4135. while (sqlite3_step(statement) == SQLITE_ROW) {
  4136. char *code = (char *)sqlite3_column_text(statement, 0);
  4137. if (code == NULL) {
  4138. code = "";
  4139. }
  4140. ret = [NSString stringWithUTF8String:code];
  4141. }
  4142. sqlite3_finalize(statement);
  4143. }
  4144. [iSalesDB close_db:db];
  4145. return ret;
  4146. }
  4147. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4148. NSString *ret = nil;
  4149. code = [self translateSingleQuote:code];
  4150. sqlite3 *db = [iSalesDB get_db];
  4151. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4152. sqlite3_stmt * statement;
  4153. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4154. while (sqlite3_step(statement) == SQLITE_ROW) {
  4155. char *_id = (char *)sqlite3_column_text(statement, 0);
  4156. if (_id == NULL) {
  4157. _id = "";
  4158. }
  4159. ret = [NSString stringWithFormat:@"%s",_id];
  4160. }
  4161. sqlite3_finalize(statement);
  4162. }
  4163. [iSalesDB close_db:db];
  4164. return ret;
  4165. }
  4166. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4167. NSString *name = nil;
  4168. codeId = [self translateSingleQuote:codeId];
  4169. sqlite3 *db = [iSalesDB get_db];
  4170. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4171. sqlite3_stmt * statement;
  4172. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4173. while (sqlite3_step(statement) == SQLITE_ROW) {
  4174. char *value = (char *)sqlite3_column_text(statement, 0);
  4175. if (value == NULL) {
  4176. value = "";
  4177. }
  4178. name = [NSString stringWithUTF8String:value];
  4179. }
  4180. sqlite3_finalize(statement);
  4181. }
  4182. [iSalesDB close_db:db];
  4183. return name;
  4184. }
  4185. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4186. NSString *ret = nil;
  4187. sqlite3 *db = [iSalesDB get_db];
  4188. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4189. sqlite3_stmt * statement;
  4190. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4191. while (sqlite3_step(statement) == SQLITE_ROW) {
  4192. char *name = (char *)sqlite3_column_text(statement, 0);
  4193. if (name == NULL) {
  4194. name = "";
  4195. }
  4196. ret = [NSString stringWithUTF8String:name];
  4197. }
  4198. sqlite3_finalize(statement);
  4199. }
  4200. [iSalesDB close_db:db];
  4201. return ret;
  4202. }
  4203. + (NSString *)salesRepCodeById:(NSString *)_id {
  4204. NSString *ret = nil;
  4205. _id = [self translateSingleQuote:_id];
  4206. sqlite3 *db = [iSalesDB get_db];
  4207. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4208. sqlite3_stmt * statement;
  4209. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4210. while (sqlite3_step(statement) == SQLITE_ROW) {
  4211. char *rep = (char *)sqlite3_column_text(statement, 0);
  4212. if (rep == NULL) {
  4213. rep = "";
  4214. }
  4215. ret = [NSString stringWithUTF8String:rep];
  4216. }
  4217. sqlite3_finalize(statement);
  4218. }
  4219. [iSalesDB close_db:db];
  4220. return ret;
  4221. }
  4222. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4223. char *tx = (char *)sqlite3_column_text(stmt, col);
  4224. if (tx == NULL) {
  4225. tx = "";
  4226. }
  4227. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4228. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4229. if (!text) {
  4230. text = @"";
  4231. }
  4232. // 将字符全部为' '的字符串干掉
  4233. int spaceCount = 0;
  4234. for (int i = 0; i < text.length; i++) {
  4235. if ([text characterAtIndex:i] == ' ') {
  4236. spaceCount++;
  4237. }
  4238. }
  4239. if (spaceCount == text.length) {
  4240. text = @"";
  4241. }
  4242. return text;
  4243. }
  4244. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4245. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4246. NSData *data = [NSData dataWithContentsOfFile:path];
  4247. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4248. return ret;
  4249. }
  4250. + (NSString *)textFileName:(NSString *)name {
  4251. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4252. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4253. if (!text) {
  4254. text = @"";
  4255. }
  4256. return text;
  4257. }
  4258. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4259. return [[dic objectForKey:key] mutableCopy];
  4260. }
  4261. + (id)translateSingleQuote:(NSString *)string {
  4262. if ([string isKindOfClass:[NSString class]])
  4263. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4264. return string;
  4265. }
  4266. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4267. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4268. }
  4269. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4270. NSString* ret= nil;
  4271. NSString *sqlQuery = nil;
  4272. // 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
  4273. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ order by i.default_img desc, i._id asc limit 1;",item_id];
  4274. sqlite3_stmt * statement;
  4275. // int count=0;
  4276. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4277. {
  4278. if (sqlite3_step(statement) == SQLITE_ROW)
  4279. {
  4280. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4281. if(imgurl==nil)
  4282. imgurl="";
  4283. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4284. ret=nsimgurl;
  4285. }
  4286. sqlite3_finalize(statement);
  4287. }
  4288. else
  4289. {
  4290. [ret setValue:@"8" forKey:@"result"];
  4291. }
  4292. // [iSalesDB close_db:db];
  4293. // DebugLog(@"data string: %@",ret );
  4294. return ret;
  4295. }
  4296. #pragma mark contact list
  4297. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4298. {
  4299. // contactType = "Sales_Order_Customer";
  4300. // limit = 25;
  4301. // offset = 0;
  4302. // password = 123456;
  4303. // "price_name" = 16;
  4304. // user = EvanK;
  4305. sqlite3 *db = [iSalesDB get_db];
  4306. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4307. if (contactType) {
  4308. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4309. } else {
  4310. contactType = @"1 = 1";
  4311. }
  4312. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4313. DebugLog(@"offline contact list keyword: %@",keyword);
  4314. // advanced search
  4315. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4316. if (contact_name) {
  4317. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4318. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4319. } else {
  4320. contact_name = @"";
  4321. }
  4322. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4323. if (customer_phone) {
  4324. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4325. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4326. } else {
  4327. customer_phone = @"";
  4328. }
  4329. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4330. if (customer_fax) {
  4331. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4332. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4333. } else {
  4334. customer_fax = @"";
  4335. }
  4336. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4337. if (customer_zipcode) {
  4338. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4339. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4340. } else {
  4341. customer_zipcode = @"";
  4342. }
  4343. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4344. if (customer_sales_rep) {
  4345. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4346. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4347. } else {
  4348. customer_sales_rep = @"";
  4349. }
  4350. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4351. if (customer_state) {
  4352. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4353. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4354. } else {
  4355. customer_state = @"";
  4356. }
  4357. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4358. if (customer_name) {
  4359. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4360. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4361. } else {
  4362. customer_name = @"";
  4363. }
  4364. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4365. if (customer_country) {
  4366. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4367. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4368. } else {
  4369. customer_country = @"";
  4370. }
  4371. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4372. if (customer_cid) {
  4373. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4374. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4375. } else {
  4376. customer_cid = @"";
  4377. }
  4378. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4379. if (customer_city) {
  4380. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4381. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4382. } else {
  4383. customer_city = @"";
  4384. }
  4385. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4386. if (customer_address) {
  4387. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4388. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4389. } else {
  4390. customer_address = @"";
  4391. }
  4392. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4393. if (customer_email) {
  4394. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4395. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4396. } else {
  4397. customer_email = @"";
  4398. }
  4399. NSString *price_name = [params valueForKey:@"price_name"];
  4400. if (price_name) {
  4401. if ([price_name containsString:@","]) {
  4402. // 首先从 price表中查处name
  4403. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4404. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4405. for (int i = 1;i < pArray.count;i++) {
  4406. NSString *p = pArray[i];
  4407. [mutablePStr appendFormat:@" or type = %@ ",p];
  4408. }
  4409. [mutablePStr appendString:@";"];
  4410. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4411. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4412. char *name = (char *)sqlite3_column_text(stmt, 0);
  4413. if (!name)
  4414. name = "";
  4415. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4416. }];
  4417. // 再根据name 拼sql
  4418. NSMutableString *mutable_price_name = [NSMutableString string];
  4419. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4420. for (int i = 1; i < price_name_array.count; i++) {
  4421. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4422. }
  4423. [mutable_price_name appendString:@")"];
  4424. price_name = mutable_price_name;
  4425. } else {
  4426. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4427. if ([price_name isEqualToString:@""]) {
  4428. price_name = @"";
  4429. } else {
  4430. __block NSString *price;
  4431. price_name = [self translateSingleQuote:price_name];
  4432. [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) {
  4433. char *p = (char *)sqlite3_column_text(stmt, 0);
  4434. if (p == NULL) {
  4435. p = "";
  4436. }
  4437. price = [NSString stringWithUTF8String:p];
  4438. }];
  4439. if ([price isEqualToString:@""]) {
  4440. price_name = @"";
  4441. } else {
  4442. price = [self translateSingleQuote:price];
  4443. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4444. }
  4445. }
  4446. }
  4447. } else {
  4448. price_name = @"";
  4449. }
  4450. int limit = [[params valueForKey:@"limit"] intValue];
  4451. int offset = [[params valueForKey:@"offset"] intValue];
  4452. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4453. 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];
  4454. 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];
  4455. // int result= [iSalesDB AddExFunction:db];
  4456. int count =0;
  4457. NSString *sqlQuery = nil;
  4458. if(keyword.length==0)
  4459. {
  4460. // 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];
  4461. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4462. sqlQuery = sql;
  4463. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4464. }
  4465. else
  4466. {
  4467. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4468. keyword = keyword.lowercaseString;
  4469. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4470. 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];
  4471. 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]];
  4472. }
  4473. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4474. sqlite3_stmt * statement;
  4475. [ret setValue:@"2" forKey:@"result"];
  4476. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4477. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4478. {
  4479. int i = 0;
  4480. while (sqlite3_step(statement) == SQLITE_ROW)
  4481. {
  4482. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4483. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4484. int editable = sqlite3_column_int(statement, 0);
  4485. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4486. NSString *nscompany_name =nil;
  4487. if(company_name==nil)
  4488. nscompany_name=@"";
  4489. else
  4490. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4491. char *country = (char*)sqlite3_column_text(statement, 2);
  4492. if(country==nil)
  4493. country="";
  4494. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4495. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4496. // if(addr==nil)
  4497. // addr="";
  4498. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4499. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4500. if(zipcode==nil)
  4501. zipcode="";
  4502. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4503. char *state = (char*)sqlite3_column_text(statement, 5);
  4504. if(state==nil)
  4505. state="";
  4506. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4507. char *city = (char*)sqlite3_column_text(statement, 6);
  4508. if(city==nil)
  4509. city="";
  4510. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4511. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4512. // NSString *nscontact_name = nil;
  4513. // if(contact_name==nil)
  4514. // nscontact_name=@"";
  4515. // else
  4516. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4517. char *phone = (char*)sqlite3_column_text(statement, 8);
  4518. NSString *nsphone = nil;
  4519. if(phone==nil)
  4520. nsphone=@"";
  4521. else
  4522. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4523. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4524. if(contact_id==nil)
  4525. contact_id="";
  4526. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4527. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4528. if(addr_1==nil)
  4529. addr_1="";
  4530. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4531. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4532. if(addr_2==nil)
  4533. addr_2="";
  4534. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4535. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4536. if(addr_3==nil)
  4537. addr_3="";
  4538. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4539. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4540. if(addr_4==nil)
  4541. addr_4="";
  4542. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4543. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4544. if(first_name==nil)
  4545. first_name="";
  4546. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4547. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4548. if(last_name==nil)
  4549. last_name="";
  4550. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4551. char *fax = (char*)sqlite3_column_text(statement, 16);
  4552. NSString *nsfax = nil;
  4553. if(fax==nil)
  4554. nsfax=@"";
  4555. else
  4556. {
  4557. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4558. if(nsfax.length>0)
  4559. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4560. }
  4561. char *email = (char*)sqlite3_column_text(statement, 17);
  4562. NSString *nsemail = nil;
  4563. if(email==nil)
  4564. nsemail=@"";
  4565. else
  4566. {
  4567. nsemail= [[NSString alloc]initWithUTF8String:email];
  4568. if(nsemail.length>0)
  4569. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4570. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4571. }
  4572. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4573. [arr_name addObject:nsfirst_name];
  4574. [arr_name addObject:nslast_name];
  4575. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  4576. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4577. {
  4578. // decrypt
  4579. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4580. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4581. nsphone=[AESCrypt fastdecrypt:nsphone];
  4582. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4583. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4584. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4585. }
  4586. [arr_addr addObject:nscompany_name];
  4587. [arr_addr addObject:nscontact_name];
  4588. [arr_addr addObject:@"<br>"];
  4589. [arr_addr addObject:nsaddr_1];
  4590. [arr_addr addObject:nsaddr_2];
  4591. [arr_addr addObject:nsaddr_3];
  4592. [arr_addr addObject:nsaddr_4];
  4593. //[arr_addr addObject:nsaddr];
  4594. [arr_addr addObject:nszipcode];
  4595. [arr_addr addObject:nscity];
  4596. [arr_addr addObject:nsstate];
  4597. [arr_addr addObject:nscountry];
  4598. [arr_addr addObject:@"<br>"];
  4599. [arr_addr addObject:nsphone];
  4600. [arr_addr addObject:nsfax];
  4601. [arr_addr addObject:nsemail];
  4602. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  4603. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4604. [item setValue:name forKey:@"name"];
  4605. [item setValue:nscontact_id forKey:@"contact_id"];
  4606. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4607. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4608. i++;
  4609. }
  4610. UIApplication * app = [UIApplication sharedApplication];
  4611. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4612. [ret setValue:appDelegate.mode forKey:@"mode"];
  4613. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4614. sqlite3_finalize(statement);
  4615. }
  4616. [iSalesDB close_db:db];
  4617. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4618. return ret;
  4619. }
  4620. #pragma mark contact Advanced search
  4621. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4622. {
  4623. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4624. return [RAUtils dict2data:contactAdvanceDic];
  4625. }
  4626. #pragma mark create new contact
  4627. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4628. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4629. NSData *data = [NSData dataWithContentsOfFile:path];
  4630. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4631. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4632. NSString *countryCode = nil;
  4633. NSString *countryCode_id = nil;
  4634. NSString *stateCode = nil;
  4635. NSString *city = nil;
  4636. NSString *zipCode = nil;
  4637. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4638. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4639. NSString *code_id = params[@"country"];
  4640. countryCode_id = code_id;
  4641. countryCode = [self countryCodeByid:code_id];
  4642. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4643. NSString *zip_code = params[@"zipcode"];
  4644. // 剔除全部为空格
  4645. int spaceCount = 0;
  4646. for (int i = 0; i < zip_code.length; i++) {
  4647. if ([zip_code characterAtIndex:i] == ' ') {
  4648. spaceCount++;
  4649. }
  4650. }
  4651. if (spaceCount == zip_code.length) {
  4652. zip_code = @"";
  4653. }
  4654. zipCode = zip_code;
  4655. if (zipCode.length > 0) {
  4656. countryCode_id = params[@"country"];
  4657. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4658. countryCode = [dic valueForKey:@"country_code"];
  4659. if (!countryCode) {
  4660. countryCode = @"US";
  4661. }
  4662. stateCode = [dic valueForKey:@"state_code"];
  4663. city = [dic valueForKey:@"city"];
  4664. // zip code
  4665. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4666. [zipDic setValue:zipCode forKey:@"value"];
  4667. [section_0 setValue:zipDic forKey:@"item_11"];
  4668. }
  4669. }
  4670. } else {
  4671. // default: US United States
  4672. countryCode = @"US";
  4673. countryCode_id = @"228";
  4674. }
  4675. // country
  4676. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4677. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4678. // state
  4679. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4680. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4681. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4682. // city
  4683. if (city) {
  4684. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4685. [cityDic setValue:city forKey:@"value"];
  4686. [section_0 setValue:cityDic forKey:@"item_13"];
  4687. }
  4688. // price type
  4689. NSDictionary *priceDic = [self offline_getPrice];
  4690. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4691. // contact type
  4692. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4693. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4694. // Sales Rep
  4695. NSDictionary *repDic = [self offline_getSalesRep];
  4696. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4697. [ret setValue:section_0 forKey:@"section_0"];
  4698. return [RAUtils dict2data:ret];
  4699. }
  4700. #pragma mark save
  4701. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4702. NSString *addr = nil;
  4703. NSString *contact_name = nil;
  4704. NSString *companyName = [params objectForKey:@"company"];
  4705. if (companyName) {
  4706. companyName = [AESCrypt fastencrypt:companyName];
  4707. } else {
  4708. companyName = @"";
  4709. }
  4710. DebugLog(@"company");
  4711. companyName = [self translateSingleQuote:companyName];
  4712. NSString *addr1 = [params objectForKey:@"address"];
  4713. NSString *addr2 = [params objectForKey:@"address2"];
  4714. NSString *addr3 = [params objectForKey:@"address_3"];
  4715. NSString *addr4 = [params objectForKey:@"address_4"];
  4716. if (!addr2) {
  4717. addr2 = @"";
  4718. }
  4719. if (!addr3) {
  4720. addr3 = @"";
  4721. }
  4722. if (!addr4) {
  4723. addr4 = @"";
  4724. }
  4725. if (!addr1) {
  4726. addr1 = @"";
  4727. }
  4728. DebugLog(@"addr");
  4729. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4730. addr = [AESCrypt fastencrypt:addr];
  4731. addr = [self translateSingleQuote:addr];
  4732. if (addr1 && ![addr1 isEqualToString:@""]) {
  4733. addr1 = [AESCrypt fastencrypt:addr1];
  4734. }
  4735. addr1 = [self translateSingleQuote:addr1];
  4736. addr2 = [self translateSingleQuote:addr2];
  4737. addr3 = [self translateSingleQuote:addr3];
  4738. addr4 = [self translateSingleQuote:addr4];
  4739. NSString *country = [params objectForKey:@"country"];
  4740. if (country) {
  4741. country = [self countryNameByCountryCodeId:country];
  4742. } else {
  4743. country = @"";
  4744. }
  4745. DebugLog(@"country");
  4746. country = [self translateSingleQuote:country];
  4747. NSString *state = [params objectForKey:@"state"];
  4748. if (!state) {
  4749. state = @"";
  4750. }
  4751. DebugLog(@"state");
  4752. state = [self translateSingleQuote:state];
  4753. NSString *city = [params objectForKey:@"city"];
  4754. if (!city) {
  4755. city = @"";
  4756. }
  4757. city = [self translateSingleQuote:city];
  4758. NSString *zipcode = [params objectForKey:@"zipcode"];
  4759. if (!zipcode) {
  4760. zipcode = @"";
  4761. }
  4762. DebugLog(@"zip");
  4763. zipcode = [self translateSingleQuote:zipcode];
  4764. NSString *fistName = [params objectForKey:@"firstname"];
  4765. if (!fistName) {
  4766. fistName = @"";
  4767. }
  4768. NSString *lastName = [params objectForKey:@"lastname"];
  4769. if (!lastName) {
  4770. lastName = @"";
  4771. }
  4772. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  4773. DebugLog(@"contact_name");
  4774. contact_name = [self translateSingleQuote:contact_name];
  4775. fistName = [self translateSingleQuote:fistName];
  4776. lastName = [self translateSingleQuote:lastName];
  4777. NSString *phone = [params objectForKey:@"phone"];
  4778. if (phone) {
  4779. phone = [AESCrypt fastencrypt:phone];
  4780. } else {
  4781. phone = @"";
  4782. }
  4783. DebugLog(@"PHONE");
  4784. phone = [self translateSingleQuote:phone];
  4785. NSString *fax = [params objectForKey:@"fax"];
  4786. if (!fax) {
  4787. fax = @"";
  4788. }
  4789. DebugLog(@"FAX");
  4790. fax = [self translateSingleQuote:fax];
  4791. NSString *email = [params objectForKey:@"email"];
  4792. if (!email) {
  4793. email = @"";
  4794. }
  4795. DebugLog(@"EMAIL:%@",email);
  4796. email = [self translateSingleQuote:email];
  4797. NSString *notes = [params objectForKey:@"contact_notes"];
  4798. if (!notes) {
  4799. notes = @"";
  4800. }
  4801. DebugLog(@"NOTE:%@",notes);
  4802. notes = [self translateSingleQuote:notes];
  4803. NSString *price = [params objectForKey:@"price_name"];
  4804. if (price) {
  4805. price = [self priceNameByPriceId:price];
  4806. } else {
  4807. price = @"";
  4808. }
  4809. DebugLog(@"PRICE");
  4810. price = [self translateSingleQuote:price];
  4811. NSString *salesRep = [params objectForKey:@"sales_rep"];
  4812. if (salesRep) {
  4813. salesRep = [self salesRepCodeById:salesRep];
  4814. } else {
  4815. salesRep = @"";
  4816. }
  4817. salesRep = [self translateSingleQuote:salesRep];
  4818. NSString *img = [params objectForKey:@"business_card"];
  4819. NSArray *array = [img componentsSeparatedByString:@","];
  4820. NSString *img_0 = array[0];
  4821. if (!img_0) {
  4822. img_0 = @"";
  4823. }
  4824. img_0 = [self translateSingleQuote:img_0];
  4825. NSString *img_1 = array[1];
  4826. if (!img_1) {
  4827. img_1 = @"";
  4828. }
  4829. img_1 = [self translateSingleQuote:img_1];
  4830. NSString *img_2 = array[2];
  4831. if (!img_2) {
  4832. img_2 = @"";
  4833. }
  4834. img_2 = [self translateSingleQuote:img_2];
  4835. NSString *contact_id = [NSUUID UUID].UUIDString;
  4836. NSString *contact_type = [params objectForKey:@"type_name"];
  4837. if (!contact_type) {
  4838. contact_type = @"";
  4839. }
  4840. contact_type = [self translateSingleQuote:contact_type];
  4841. // 判断更新时是否为customer
  4842. if (update) {
  4843. contact_id = [params objectForKey:@"contact_id"];
  4844. if (!contact_id) {
  4845. contact_id = @"";
  4846. }
  4847. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  4848. __block int customer = 0;
  4849. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4850. customer = sqlite3_column_int(stmt, 0);
  4851. }];
  4852. isCustomer = customer ? YES : NO;
  4853. }
  4854. NSMutableDictionary *sync_dic = [params mutableCopy];
  4855. if (isCustomer) {
  4856. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  4857. } else {
  4858. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  4859. }
  4860. NSString *sync_data = nil;
  4861. NSString *sql = nil;
  4862. if (update){
  4863. contact_id = [params objectForKey:@"contact_id"];
  4864. if (!contact_id) {
  4865. contact_id = @"";
  4866. }
  4867. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4868. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4869. sync_data = [RAUtils dict2string:sync_dic];
  4870. sync_data = [self translateSingleQuote:sync_data];
  4871. 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 = '%@',type = '%@' 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_type,contact_id];
  4872. } else {
  4873. contact_id = [self translateSingleQuote:contact_id];
  4874. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4875. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4876. sync_data = [RAUtils dict2string:sync_dic];
  4877. sync_data = [self translateSingleQuote:sync_data];
  4878. 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,type) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,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,isCustomer ? 1 : 0,isCustomer ? 1 : 0,isCustomer ? 1 : 0,sync_data,contact_name,addr,contact_type];
  4879. }
  4880. int result = [iSalesDB execSql:sql];
  4881. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  4882. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  4883. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  4884. }
  4885. #pragma mark save new contact
  4886. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  4887. {
  4888. return [self offline_saveContact:params update:NO isCustomer:YES];
  4889. }
  4890. #pragma mark edit contact
  4891. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  4892. {
  4893. // {
  4894. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  4895. // password = 123456;
  4896. // user = EvanK;
  4897. // }
  4898. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  4899. NSData *data = [NSData dataWithContentsOfFile:path];
  4900. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4901. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4902. NSString *countryCode = nil;
  4903. NSString *countryCode_id = nil;
  4904. NSString *stateCode = nil;
  4905. /*------contact infor------*/
  4906. __block NSString *country = nil;
  4907. __block NSString *company_name = nil;
  4908. __block NSString *contact_id = params[@"contact_id"];
  4909. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  4910. __block NSString *zipcode = nil;
  4911. __block NSString *state = nil; // state_code
  4912. __block NSString *city = nil; //
  4913. __block NSString *firt_name,*last_name;
  4914. __block NSString *phone,*fax,*email;
  4915. __block NSString *notes,*price_type,*sales_rep;
  4916. __block NSString *img_0,*img_1,*img_2;
  4917. __block NSString *contact_type;
  4918. contact_id = [self translateSingleQuote:contact_id];
  4919. 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,type from offline_contact where contact_id = '%@';",contact_id];
  4920. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4921. country = [self textAtColumn:0 statement:stmt]; // country name
  4922. company_name = [self textAtColumn:1 statement:stmt];
  4923. addr_1 = [self textAtColumn:2 statement:stmt];
  4924. addr_2 = [self textAtColumn:3 statement:stmt];
  4925. addr_3 = [self textAtColumn:4 statement:stmt];
  4926. addr_4 = [self textAtColumn:5 statement:stmt];
  4927. zipcode = [self textAtColumn:6 statement:stmt];
  4928. state = [self textAtColumn:7 statement:stmt]; // state code
  4929. city = [self textAtColumn:8 statement:stmt];
  4930. firt_name = [self textAtColumn:9 statement:stmt];
  4931. last_name = [self textAtColumn:10 statement:stmt];
  4932. phone = [self textAtColumn:11 statement:stmt];
  4933. fax = [self textAtColumn:12 statement:stmt];
  4934. email = [self textAtColumn:13 statement:stmt];
  4935. notes = [self textAtColumn:14 statement:stmt];
  4936. price_type = [self textAtColumn:15 statement:stmt]; // name
  4937. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  4938. img_0 = [self textAtColumn:17 statement:stmt];
  4939. img_1 = [self textAtColumn:18 statement:stmt];
  4940. img_2 = [self textAtColumn:19 statement:stmt];
  4941. contact_type = [self textAtColumn:20 statement:stmt];
  4942. }];
  4943. // decrypt
  4944. if (company_name) {
  4945. company_name = [AESCrypt fastdecrypt:company_name];
  4946. }
  4947. if (addr_1) {
  4948. addr_1 = [AESCrypt fastdecrypt:addr_1];
  4949. }
  4950. if (phone) {
  4951. phone = [AESCrypt fastdecrypt:phone];
  4952. }
  4953. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  4954. countryCode = [iSalesDB jk_queryText:countrySql];
  4955. stateCode = state;
  4956. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4957. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  4958. NSString *code_id = params[@"country"];
  4959. countryCode_id = code_id;
  4960. countryCode = [self countryCodeByid:code_id];
  4961. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  4962. NSString *zip_code = params[@"zipcode"];
  4963. // 剔除全部为空格
  4964. int spaceCount = 0;
  4965. for (int i = 0; i < zip_code.length; i++) {
  4966. if ([zip_code characterAtIndex:i] == ' ') {
  4967. spaceCount++;
  4968. }
  4969. }
  4970. if (spaceCount == zip_code.length) {
  4971. zip_code = @"";
  4972. }
  4973. if (zipcode.length > 0) {
  4974. zipcode = zip_code;
  4975. countryCode_id = params[@"country"];
  4976. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4977. countryCode = [dic valueForKey:@"country_code"];
  4978. stateCode = [dic valueForKey:@"state_code"];
  4979. city = [dic valueForKey:@"city"];
  4980. // zip code
  4981. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  4982. [zipDic setValue:zipcode forKey:@"value"];
  4983. [section_0 setValue:zipDic forKey:@"item_8"];
  4984. }
  4985. }
  4986. }
  4987. // 0 Country
  4988. // 1 Company Name
  4989. // 2 Contact ID
  4990. // 3 Picture
  4991. // 4 Address 1
  4992. // 5 Address 2
  4993. // 6 Address 3
  4994. // 7 Address 4
  4995. // 8 Zip Code
  4996. // 9 State/Province
  4997. // 10 City
  4998. // 11 Contact First Name
  4999. // 12 Contact Last Name
  5000. // 13 Phone
  5001. // 14 Fax
  5002. // 15 Email
  5003. // 16 Contact Notes
  5004. // 17 Price Type
  5005. // 18 Contact Type
  5006. // 19 Sales Rep
  5007. // country
  5008. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5009. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5010. // company
  5011. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5012. // contact_id
  5013. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5014. // picture
  5015. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5016. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5017. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5018. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5019. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5020. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5021. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5022. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5023. // addr 1 2 3 4
  5024. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5025. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5026. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5027. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5028. // zip code
  5029. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5030. // state
  5031. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5032. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5033. // city
  5034. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5035. // first last
  5036. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5037. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5038. // phone fax email
  5039. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5040. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5041. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5042. // notes
  5043. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5044. // price
  5045. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5046. for (NSString *key in priceDic.allKeys) {
  5047. if ([key containsString:@"val_"]) {
  5048. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5049. if ([dic[@"value"] isEqualToString:price_type]) {
  5050. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5051. [priceDic setValue:dic forKey:key];
  5052. }
  5053. }
  5054. }
  5055. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5056. // Contact Rep
  5057. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5058. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5059. // Sales Rep
  5060. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5061. for (NSString *key in repDic.allKeys) {
  5062. if ([key containsString:@"val_"]) {
  5063. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5064. NSString *value = dic[@"value"];
  5065. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5066. if (code && [code isEqualToString:sales_rep]) {
  5067. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5068. [repDic setValue:dic forKey:key];
  5069. }
  5070. }
  5071. }
  5072. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5073. [ret setValue:section_0 forKey:@"section_0"];
  5074. return [RAUtils dict2data:ret];
  5075. }
  5076. #pragma mark save contact
  5077. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5078. {
  5079. return [self offline_saveContact:params update:YES isCustomer:YES];
  5080. }
  5081. #pragma mark category
  5082. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5083. if (ck) {
  5084. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5085. for (NSString *key in res.allKeys) {
  5086. if (![key isEqualToString:@"count"]) {
  5087. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5088. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5089. if ([val[@"value"] isEqualToString:ck]) {
  5090. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5091. }
  5092. [res setValue:val forKey:key];
  5093. }
  5094. }
  5095. [dic setValue:res forKey:valueKey];
  5096. }
  5097. }
  5098. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5099. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5100. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5101. NSString* category = [params valueForKey:@"category"];
  5102. if (!category || [category isEqualToString:@""]) {
  5103. category = @"%";
  5104. }
  5105. category = [self translateSingleQuote:category];
  5106. int limit = [[params valueForKey:@"limit"] intValue];
  5107. int offset = [[params valueForKey:@"offset"] intValue];
  5108. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5109. NSString *limit_str = @"";
  5110. if (limited) {
  5111. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5112. }
  5113. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5114. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5115. sqlite3 *db = [iSalesDB get_db];
  5116. // [iSalesDB AddExFunction:db];
  5117. int count;
  5118. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5119. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, category,limit_str];
  5120. double price_min = 0;
  5121. double price_max = 0;
  5122. if ([params.allKeys containsObject:@"alert"]) {
  5123. // alert
  5124. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5125. NSString *alert = params[@"alert"];
  5126. if ([alert isEqualToString:@"Display All"]) {
  5127. alert = [NSString stringWithFormat:@""];
  5128. } else {
  5129. alert = [self translateSingleQuote:alert];
  5130. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5131. }
  5132. // available
  5133. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5134. NSString *available = params[@"available"];
  5135. NSString *available_condition;
  5136. if ([available isEqualToString:@"Display All"]) {
  5137. available_condition = @"";
  5138. } else if ([available isEqualToString:@"Available Now"]) {
  5139. available_condition = @"and availability > 0";
  5140. } else {
  5141. available_condition = @"and availability == 0";
  5142. }
  5143. // best seller
  5144. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5145. NSString *best_seller = @"";
  5146. NSString *order_best_seller = @"m.name asc";
  5147. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5148. best_seller = @"and best_seller > 0";
  5149. order_best_seller = @"m.best_seller desc,m.name asc";
  5150. }
  5151. // price
  5152. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5153. NSString *price = params[@"price"];
  5154. price_min = 0;
  5155. price_max = MAXFLOAT;
  5156. if (appDelegate.user) {
  5157. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5158. NSMutableString *priceName = [NSMutableString string];
  5159. for (int i = 0; i < priceTypeArray.count; i++) {
  5160. NSString *pricetype = priceTypeArray[i];
  5161. pricetype = [self translateSingleQuote:pricetype];
  5162. if (i == 0) {
  5163. [priceName appendFormat:@"'%@'",pricetype];
  5164. } else {
  5165. [priceName appendFormat:@",'%@'",pricetype];
  5166. }
  5167. }
  5168. if ([price isEqualToString:@"Display All"]) {
  5169. price = [NSString stringWithFormat:@""];
  5170. } else if([price containsString:@"+"]){
  5171. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5172. price_min = [price doubleValue];
  5173. 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];
  5174. } else {
  5175. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5176. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5177. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5178. 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];
  5179. }
  5180. } else {
  5181. price = @"";
  5182. }
  5183. // sold_by_qty : Sold in quantities of %@
  5184. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5185. NSString *qty = params[@"sold_by_qty"];
  5186. if ([qty isEqualToString:@"Display All"]) {
  5187. qty = @"";
  5188. } else {
  5189. qty = [self translateSingleQuote:qty];
  5190. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5191. }
  5192. // cate
  5193. category = [self translateSingleQuote:category];
  5194. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5195. // where bestseller > 0 order by bestseller desc
  5196. // sql query: alert availability(int) best_seller(int) price qty
  5197. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete 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 left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;",cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  5198. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5199. }
  5200. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5201. if (!appDelegate.user) {
  5202. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5203. }
  5204. [ret setValue:filter forKey:@"filter"];
  5205. DebugLog(@"offline_category sql:%@",sqlQuery);
  5206. sqlite3_stmt * statement;
  5207. [ret setValue:@"2" forKey:@"result"];
  5208. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5209. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5210. // int count=0;
  5211. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5212. {
  5213. int i=0;
  5214. while (sqlite3_step(statement) == SQLITE_ROW)
  5215. {
  5216. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5217. char *name = (char*)sqlite3_column_text(statement, 0);
  5218. if(name==nil)
  5219. name="";
  5220. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5221. char *description = (char*)sqlite3_column_text(statement, 1);
  5222. if(description==nil)
  5223. description="";
  5224. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5225. int product_id = sqlite3_column_int(statement, 2);
  5226. int wid = sqlite3_column_int(statement, 3);
  5227. int closeout = sqlite3_column_int(statement, 4);
  5228. int cid = sqlite3_column_int(statement, 5);
  5229. int wisdelete = sqlite3_column_int(statement, 6);
  5230. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5231. if(wid !=0 && wisdelete != 1)
  5232. [item setValue:@"true" forKey:@"wish_exists"];
  5233. else
  5234. [item setValue:@"false" forKey:@"wish_exists"];
  5235. if(closeout==0)
  5236. [item setValue:@"false" forKey:@"is_closeout"];
  5237. else
  5238. [item setValue:@"true" forKey:@"is_closeout"];
  5239. if(cid==0)
  5240. [item setValue:@"false" forKey:@"cart_exists"];
  5241. else
  5242. [item setValue:@"true" forKey:@"cart_exists"];
  5243. [item addEntriesFromDictionary:imgjson];
  5244. // [item setValue:nsurl forKey:@"img"];
  5245. [item setValue:nsname forKey:@"name"];
  5246. [item setValue:nsdescription forKey:@"description"];
  5247. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5248. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5249. i++;
  5250. }
  5251. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5252. [ret setObject:items forKey:@"items"];
  5253. sqlite3_finalize(statement);
  5254. } else {
  5255. DebugLog(@"nothing...");
  5256. }
  5257. DebugLog(@"count:%d",count);
  5258. [iSalesDB close_db:db];
  5259. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5260. return ret;
  5261. }
  5262. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5263. {
  5264. return [self categoryList:params limited:YES];
  5265. }
  5266. # pragma mark item search
  5267. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5268. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5269. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5270. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5271. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5272. // category
  5273. NSDictionary *category_menu = [self offline_category_menu];
  5274. [filter setValue:category_menu forKey:@"category"];
  5275. NSString* where= nil;
  5276. NSString* orderby= @"m.name";
  5277. if (!filterSearch) {
  5278. int covertype = [[params valueForKey:@"covertype"] intValue];
  5279. switch (covertype) {
  5280. case 0:
  5281. {
  5282. where=@"m.category like'%%#005#%%'";
  5283. break;
  5284. }
  5285. case 1:
  5286. {
  5287. where=@"m.alert like '%QS%'";
  5288. break;
  5289. }
  5290. case 2:
  5291. {
  5292. where=@"m.availability>0";
  5293. break;
  5294. }
  5295. case 3:
  5296. {
  5297. where=@"m.best_seller>0";
  5298. orderby=@"m.best_seller desc,m.name asc";
  5299. break;
  5300. }
  5301. default:
  5302. where=@"1=1";
  5303. break;
  5304. }
  5305. }
  5306. int limit = [[params valueForKey:@"limit"] intValue];
  5307. int offset = [[params valueForKey:@"offset"] intValue];
  5308. NSString *limit_str = @"";
  5309. if (limited) {
  5310. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5311. }
  5312. sqlite3 *db = [iSalesDB get_db];
  5313. // [iSalesDB AddExFunction:db];
  5314. int count;
  5315. NSString *sqlQuery = nil;
  5316. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5317. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where %@ order by %@ %@;",appDelegate.order_code, where,orderby,limit_str];
  5318. double price_min = 0;
  5319. double price_max = 0;
  5320. if (filterSearch) {
  5321. // alert
  5322. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5323. NSString *alert = params[@"alert"];
  5324. if ([alert isEqualToString:@"Display All"]) {
  5325. alert = [NSString stringWithFormat:@""];
  5326. } else {
  5327. alert = [self translateSingleQuote:alert];
  5328. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5329. }
  5330. // available
  5331. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5332. NSString *available = params[@"available"];
  5333. NSString *available_condition;
  5334. if ([available isEqualToString:@"Display All"]) {
  5335. available_condition = @"";
  5336. } else if ([available isEqualToString:@"Available Now"]) {
  5337. available_condition = @"and availability > 0";
  5338. } else {
  5339. available_condition = @"and availability == 0";
  5340. }
  5341. // best seller
  5342. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5343. NSString *best_seller = @"";
  5344. NSString *order_best_seller = @"m.name asc";
  5345. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5346. best_seller = @"and best_seller > 0";
  5347. order_best_seller = @"m.best_seller desc,m.name asc";
  5348. }
  5349. // price
  5350. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5351. NSString *price = params[@"price"];
  5352. price_min = 0;
  5353. price_max = MAXFLOAT;
  5354. if (appDelegate.user) {
  5355. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5356. NSMutableString *priceName = [NSMutableString string];
  5357. for (int i = 0; i < priceTypeArray.count; i++) {
  5358. NSString *pricetype = priceTypeArray[i];
  5359. pricetype = [self translateSingleQuote:pricetype];
  5360. if (i == 0) {
  5361. [priceName appendFormat:@"'%@'",pricetype];
  5362. } else {
  5363. [priceName appendFormat:@",'%@'",pricetype];
  5364. }
  5365. }
  5366. if ([price isEqualToString:@"Display All"]) {
  5367. price = [NSString stringWithFormat:@""];
  5368. } else if([price containsString:@"+"]){
  5369. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5370. price_min = [price doubleValue];
  5371. 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];
  5372. } else {
  5373. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5374. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5375. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5376. 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];
  5377. }
  5378. } else {
  5379. price = @"";
  5380. }
  5381. // sold_by_qty : Sold in quantities of %@
  5382. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5383. NSString *qty = params[@"sold_by_qty"];
  5384. if ([qty isEqualToString:@"Display All"]) {
  5385. qty = @"";
  5386. } else {
  5387. qty = [self translateSingleQuote:qty];
  5388. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5389. }
  5390. // category
  5391. NSString *category_id = params[@"ctgId"];
  5392. NSMutableArray *cate_id_array = nil;
  5393. NSMutableString *cateWhere = [NSMutableString string];
  5394. if ([category_id isEqualToString:@""] || !category_id) {
  5395. [cateWhere appendString:@"1 = 1"];
  5396. } else {
  5397. if ([category_id containsString:@","]) {
  5398. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5399. } else {
  5400. cate_id_array = [@[category_id] mutableCopy];
  5401. }
  5402. /*-----------*/
  5403. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  5404. for (int i = 0; i < cate_id_array.count; i++) {
  5405. for (NSString *key0 in cateDic.allKeys) {
  5406. if ([key0 containsString:@"category_"]) {
  5407. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  5408. for (NSString *key1 in category0.allKeys) {
  5409. if ([key1 containsString:@"category_"]) {
  5410. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  5411. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5412. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5413. cate_id_array[i] = [category1 objectForKey:@"id"];
  5414. if (i == 0) {
  5415. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5416. } else {
  5417. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  5418. }
  5419. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5420. [category0 setValue:category1 forKey:key1];
  5421. [cateDic setValue:category0 forKey:key0];
  5422. }
  5423. }
  5424. }
  5425. }
  5426. }
  5427. }
  5428. [filter setValue:cateDic forKey:@"category"];
  5429. }
  5430. // where bestseller > 0 order by bestseller desc
  5431. // sql query: alert availability(int) best_seller(int) price qty
  5432. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete 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 left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;", cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  5433. // count
  5434. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5435. }
  5436. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5437. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5438. if (!appDelegate.user) {
  5439. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5440. }
  5441. [ret setValue:filter forKey:@"filter"];
  5442. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5443. sqlite3_stmt * statement;
  5444. [ret setValue:@"2" forKey:@"result"];
  5445. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5446. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5447. // int count=0;
  5448. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5449. {
  5450. int i=0;
  5451. while (sqlite3_step(statement) == SQLITE_ROW)
  5452. {
  5453. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5454. char *name = (char*)sqlite3_column_text(statement, 0);
  5455. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5456. char *description = (char*)sqlite3_column_text(statement, 1);
  5457. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5458. int product_id = sqlite3_column_int(statement, 2);
  5459. int wid = sqlite3_column_int(statement, 3);
  5460. int closeout = sqlite3_column_int(statement, 4);
  5461. int cid = sqlite3_column_int(statement, 5);
  5462. int wisdelete = sqlite3_column_int(statement, 6);
  5463. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5464. if(wid !=0 && wisdelete != 1)
  5465. [item setValue:@"true" forKey:@"wish_exists"];
  5466. else
  5467. [item setValue:@"false" forKey:@"wish_exists"];
  5468. if(closeout==0)
  5469. [item setValue:@"false" forKey:@"is_closeout"];
  5470. else
  5471. [item setValue:@"true" forKey:@"is_closeout"];
  5472. if(cid==0)
  5473. [item setValue:@"false" forKey:@"cart_exists"];
  5474. else
  5475. [item setValue:@"true" forKey:@"cart_exists"];
  5476. [item addEntriesFromDictionary:imgjson];
  5477. // [item setValue:nsurl forKey:@"img"];
  5478. [item setValue:nsname forKey:@"fash_name"];
  5479. [item setValue:nsdescription forKey:@"description"];
  5480. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5481. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5482. i++;
  5483. }
  5484. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5485. [ret setObject:items forKey:@"items"];
  5486. sqlite3_finalize(statement);
  5487. }
  5488. [iSalesDB close_db:db];
  5489. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5490. return ret;
  5491. }
  5492. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5493. {
  5494. return [self itemsearch:params limited:YES];
  5495. }
  5496. #pragma mark order detail
  5497. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5498. if (str1.length == 0) {
  5499. str1 = @"&nbsp";
  5500. }
  5501. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5502. return str;
  5503. }
  5504. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5505. {
  5506. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5507. [fromformatter setDateFormat:from];
  5508. NSDate *date = [fromformatter dateFromString:datetime];
  5509. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5510. [toformatter setDateFormat:to];
  5511. NSString * ret = [toformatter stringFromDate:date];
  5512. return ret;
  5513. }
  5514. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5515. // 把毫秒去掉
  5516. if ([dateTime containsString:@"."]) {
  5517. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5518. }
  5519. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5520. formatter.dateFormat = formate;
  5521. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5522. NSDate *date = [formatter dateFromString:dateTime];
  5523. formatter.dateFormat = newFormate;
  5524. NSString *result = [formatter stringFromDate:date];
  5525. return result ? result : @"";
  5526. }
  5527. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5528. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5529. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5530. }
  5531. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5532. {
  5533. DebugLog(@"offline oderdetail params: %@",params);
  5534. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5535. int orderId = [params[@"orderId"] intValue];
  5536. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5537. // decrypt card number and card security code
  5538. // 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 ];
  5539. 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,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via 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,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  5540. sqlite3 *db = [iSalesDB get_db];
  5541. sqlite3_stmt * statement;
  5542. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5543. NSString *nssoid = @"";
  5544. NSString* orderinfo = @"";
  5545. NSString *moreInfo = @"";
  5546. double handlingFee = 0;
  5547. double payments_and_credist = 0;
  5548. double totalPrice = 0;
  5549. double shippingFee = 0;
  5550. double lift_gate = 0;
  5551. NSString *customer_contact = @"";
  5552. NSString *customer_email = @"";
  5553. NSString *customer_fax = @"";
  5554. NSString *customer_phone = @"";
  5555. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5556. {
  5557. if (sqlite3_step(statement) == SQLITE_ROW)
  5558. {
  5559. int order_id = sqlite3_column_int(statement, 0);
  5560. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5561. ret[@"sign_url"] = sign_url;
  5562. customer_contact = [self textAtColumn:52 statement:statement];
  5563. customer_email = [self textAtColumn:53 statement:statement];
  5564. customer_phone = [self textAtColumn:54 statement:statement];
  5565. customer_fax = [self textAtColumn:55 statement:statement];
  5566. int offline_edit=sqlite3_column_int(statement, 56);
  5567. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5568. char *soid = (char*)sqlite3_column_text(statement, 1);
  5569. if(soid==nil)
  5570. soid= "";
  5571. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5572. // so#
  5573. ret[@"so#"] = nssoid;
  5574. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5575. if(poNumber==nil)
  5576. poNumber= "";
  5577. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5578. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5579. if(create_time==nil)
  5580. create_time= "";
  5581. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5582. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5583. int status = sqlite3_column_int(statement, 4);
  5584. int erpStatus = sqlite3_column_int(statement, 49);
  5585. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5586. // status
  5587. if (status > 1 && status != 3) {
  5588. status = erpStatus;
  5589. } else if (status == 3) {
  5590. status = 15;
  5591. }
  5592. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5593. ret[@"order_status"] = nsstatus;
  5594. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5595. if(company_name==nil)
  5596. company_name= "";
  5597. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5598. // company name
  5599. ret[@"company_name"] = nscompany_name;
  5600. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5601. if(customer_contact==nil)
  5602. customer_contact= "";
  5603. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5604. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5605. if(addr_1==nil)
  5606. addr_1="";
  5607. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5608. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5609. if(addr_2==nil)
  5610. addr_2="";
  5611. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5612. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5613. if(addr_3==nil)
  5614. addr_3="";
  5615. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5616. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5617. if(addr_4==nil)
  5618. addr_4="";
  5619. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5620. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5621. [arr_addr addObject:nsaddr_1];
  5622. [arr_addr addObject:nsaddr_2];
  5623. [arr_addr addObject:nsaddr_3];
  5624. [arr_addr addObject:nsaddr_4];
  5625. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  5626. char *logist = (char*)sqlite3_column_text(statement, 11);
  5627. if(logist==nil)
  5628. logist= "";
  5629. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5630. if (status == -11 || status == 10 || status == 11) {
  5631. nslogist = [self textAtColumn:59 statement:statement];
  5632. };
  5633. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5634. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5635. shipping = @"Shipping To Be Quoted";
  5636. } else {
  5637. shippingFee = sqlite3_column_double(statement, 12);
  5638. }
  5639. // Shipping
  5640. ret[@"Shipping"] = shipping;
  5641. int have_lift_gate = sqlite3_column_int(statement, 17);
  5642. lift_gate = sqlite3_column_double(statement, 13);
  5643. // Liftgate Fee(No loading dock)
  5644. if (!have_lift_gate) {
  5645. lift_gate = 0;
  5646. }
  5647. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5648. if (sqlite3_column_int(statement, 57)) {
  5649. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  5650. }
  5651. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  5652. if(general_notes==nil)
  5653. general_notes= "";
  5654. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  5655. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  5656. if(internal_notes==nil)
  5657. internal_notes= "";
  5658. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  5659. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  5660. if(payment_type==nil)
  5661. payment_type= "";
  5662. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  5663. // order info
  5664. orderinfo = [self textFileName:@"order_info.html"];
  5665. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  5666. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  5667. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  5668. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  5669. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  5670. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  5671. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  5672. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  5673. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  5674. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  5675. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  5676. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  5677. NSString *payment = nil;
  5678. if([nspayment_type isEqualToString:@"Credit Card"])
  5679. {
  5680. payment = [self textFileName:@"creditcardpayment.html"];
  5681. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  5682. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  5683. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  5684. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  5685. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  5686. NSString *card_type = [self textAtColumn:42 statement:statement];
  5687. if (card_type.length > 0) { // 显示星号
  5688. card_type = @"****";
  5689. }
  5690. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  5691. if (card_number.length > 0 && card_number.length > 4) {
  5692. for (int i = 0; i < card_number.length - 4; i++) {
  5693. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  5694. }
  5695. } else {
  5696. card_number = @"";
  5697. }
  5698. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  5699. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  5700. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  5701. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  5702. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  5703. card_expiration = @"****";
  5704. }
  5705. NSString *card_city = [self textAtColumn:46 statement:statement];
  5706. NSString *card_state = [self textAtColumn:47 statement:statement];
  5707. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  5708. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  5709. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  5710. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  5711. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  5712. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  5713. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  5714. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  5715. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  5716. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  5717. }
  5718. else
  5719. {
  5720. payment=[self textFileName:@"normalpayment.html"];
  5721. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  5722. }
  5723. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  5724. ret[@"result"]= [NSNumber numberWithInt:2];
  5725. // more info
  5726. moreInfo = [self textFileName:@"more_info.html"];
  5727. /*****ship to******/
  5728. // ShipToCompany_or_&nbsp
  5729. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  5730. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  5731. NSString *shipToName = [self textAtColumn:19 statement:statement];
  5732. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  5733. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  5734. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  5735. /*****ship from******/
  5736. // ShipFromCompany_or_&nbsp
  5737. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  5738. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  5739. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  5740. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  5741. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  5742. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  5743. /*****freight to******/
  5744. // FreightBillToCompany_or_&nbsp
  5745. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  5746. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  5747. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  5748. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  5749. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  5750. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  5751. /*****merchandise to******/
  5752. // MerchandiseBillToCompany_or_&nbsp
  5753. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  5754. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  5755. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  5756. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  5757. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  5758. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  5759. /*****return to******/
  5760. // ReturnToCompany_or_&nbsp
  5761. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  5762. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  5763. NSString *returnToName = [self textAtColumn:31 statement:statement];
  5764. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  5765. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  5766. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  5767. //
  5768. // DebugLog(@"more info : %@",moreInfo);
  5769. // handling fee
  5770. handlingFee = sqlite3_column_double(statement, 33);
  5771. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  5772. if (sqlite3_column_int(statement, 58)) {
  5773. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  5774. }
  5775. //
  5776. // customer info
  5777. customerID = [self textAtColumn:36 statement:statement];
  5778. // mode
  5779. ret[@"mode"] = appDelegate.mode;
  5780. // model_count
  5781. ret[@"model_count"] = @(0);
  5782. }
  5783. sqlite3_finalize(statement);
  5784. }
  5785. [iSalesDB close_db:db];
  5786. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  5787. // "customer_email" = "Shui Hu";
  5788. // "customer_fax" = "";
  5789. // "customer_first_name" = F;
  5790. // "customer_last_name" = L;
  5791. // "customer_name" = ",da He Xiang Dong Liu A";
  5792. // "customer_phone" = "Hey Xuan Feng";
  5793. contactInfo[@"customer_phone"] = customer_phone;
  5794. contactInfo[@"customer_fax"] = customer_fax;
  5795. contactInfo[@"customer_email"] = customer_email;
  5796. NSString *first_name = @"";
  5797. NSString *last_name = @"";
  5798. if ([customer_contact isEqualToString:@""]) {
  5799. } else if ([customer_contact containsString:@" "]) {
  5800. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  5801. first_name = [customer_contact substringToIndex:first_space_index];
  5802. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  5803. }
  5804. contactInfo[@"customer_first_name"] = first_name;
  5805. contactInfo[@"customer_last_name"] = last_name;
  5806. ret[@"customerInfo"] = contactInfo;
  5807. // models
  5808. if (nssoid) {
  5809. __block double TotalCuft = 0;
  5810. __block double TotalWeight = 0;
  5811. __block int TotalCarton = 0;
  5812. __block double allItemPrice = 0;
  5813. 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];
  5814. sqlite3 *db1 = [iSalesDB get_db];
  5815. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5816. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5817. int product_id = sqlite3_column_int(stmt, 0);
  5818. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5819. int item_id = sqlite3_column_int(stmt, 7);
  5820. NSString* Price=nil;
  5821. if(str_price==nil)
  5822. {
  5823. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  5824. if(price==nil)
  5825. Price=@"No Price.";
  5826. else
  5827. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5828. }
  5829. else
  5830. {
  5831. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5832. }
  5833. double discount = sqlite3_column_double(stmt, 2);
  5834. int item_count = sqlite3_column_int(stmt, 3);
  5835. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  5836. NSString *nsline_note=nil;
  5837. if(line_note!=nil)
  5838. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  5839. // char *name = (char*)sqlite3_column_text(stmt, 5);
  5840. // NSString *nsname = nil;
  5841. // if(name!=nil)
  5842. // nsname= [[NSString alloc]initWithUTF8String:name];
  5843. NSString *nsname = [self textAtColumn:5 statement:stmt];
  5844. // char *description = (char*)sqlite3_column_text(stmt, 6);
  5845. // NSString *nsdescription=nil;
  5846. // if(description!=nil)
  5847. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  5848. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  5849. // int stockUom = sqlite3_column_int(stmt, 8);
  5850. // int _id = sqlite3_column_int(stmt, 9);
  5851. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  5852. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5853. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5854. int carton=[bsubtotaljson[@"carton"] intValue];
  5855. TotalCuft += cuft;
  5856. TotalWeight += weight;
  5857. TotalCarton += carton;
  5858. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  5859. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  5860. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  5861. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  5862. itemjson[@"note"]=nsline_note;
  5863. itemjson[@"origin_price"] = Price;
  5864. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5865. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5866. itemjson[@"order_item_status"] = @""; // 暂时不处理
  5867. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5868. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5869. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  5870. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5871. if(itemjson[@"combine"] != nil)
  5872. {
  5873. // int citem=0;
  5874. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5875. for(int bc=0;bc<bcount;bc++)
  5876. {
  5877. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5878. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5879. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5880. subTotal += uprice * modulus * item_count;
  5881. }
  5882. }
  5883. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5884. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  5885. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  5886. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  5887. allItemPrice += subTotal;
  5888. }];
  5889. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  5890. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  5891. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  5892. // payments/Credits
  5893. // payments_and_credist = sqlite3_column_double(statement, 34);
  5894. payments_and_credist = allItemPrice;
  5895. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  5896. // // total
  5897. // totalPrice = sqlite3_column_double(statement, 35);
  5898. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  5899. } else {
  5900. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  5901. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  5902. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  5903. // payments/Credits
  5904. payments_and_credist = 0;
  5905. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  5906. }
  5907. // total
  5908. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  5909. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  5910. ret[@"order_info"]= orderinfo;
  5911. ret[@"more_order_info"] = moreInfo;
  5912. return [RAUtils dict2data:ret];
  5913. }
  5914. #pragma mark order list
  5915. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  5916. NSString *nsstatus = @"";
  5917. switch (status) {
  5918. case 0:
  5919. {
  5920. nsstatus=@"Temp Order";
  5921. break;
  5922. }
  5923. case 1:
  5924. {
  5925. nsstatus=@"Saved Order";
  5926. break;
  5927. }
  5928. case 2: {
  5929. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  5930. break;
  5931. }
  5932. case 3:
  5933. case 15:
  5934. {
  5935. nsstatus=@"Cancelled";
  5936. break;
  5937. }
  5938. case 10:
  5939. {
  5940. nsstatus=@"Quote Submitted";
  5941. break;
  5942. }
  5943. case 11:
  5944. {
  5945. nsstatus=@"Sales Order Submitted";
  5946. break;
  5947. }
  5948. case 12:
  5949. {
  5950. nsstatus=@"Processing";
  5951. break;
  5952. }
  5953. case 13:
  5954. {
  5955. nsstatus=@"Shipped";
  5956. break;
  5957. }
  5958. case 14:
  5959. {
  5960. nsstatus=@"Closed";
  5961. break;
  5962. }
  5963. case -11:
  5964. {
  5965. nsstatus = @"Ready For Submit";
  5966. break;
  5967. }
  5968. default:
  5969. break;
  5970. }
  5971. return nsstatus;
  5972. }
  5973. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  5974. double total = 0;
  5975. __block double payments_and_credist = 0;
  5976. __block double allItemPrice = 0;
  5977. // sqlite3 *db1 = [iSalesDB get_db];
  5978. if (so_id) {
  5979. // 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];
  5980. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id from (select product_id,decrypt(str_price) as price,discount,item_count,item_id from offline_cart where so_no='%@');",so_id];
  5981. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5982. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5983. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5984. int product_id = sqlite3_column_int(stmt, 0);
  5985. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5986. int discount = sqlite3_column_int(stmt, 2);
  5987. int item_count = sqlite3_column_int(stmt, 3);
  5988. // int item_id = sqlite3_column_int(stmt, 7);
  5989. int item_id = sqlite3_column_int(stmt, 4);
  5990. NSString* Price=nil;
  5991. if(str_price==nil)
  5992. {
  5993. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  5994. if(price==nil)
  5995. Price=@"No Price.";
  5996. else
  5997. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5998. }
  5999. else
  6000. {
  6001. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6002. }
  6003. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  6004. itemjson[@"The unit price"]=Price;
  6005. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6006. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6007. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6008. if(itemjson[@"combine"] != nil)
  6009. {
  6010. // int citem=0;
  6011. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6012. for(int bc=0;bc<bcount;bc++)
  6013. {
  6014. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6015. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6016. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6017. subTotal += uprice * modulus * item_count;
  6018. }
  6019. }
  6020. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6021. allItemPrice += subTotal;
  6022. }];
  6023. payments_and_credist = allItemPrice;
  6024. } else {
  6025. // payments/Credits
  6026. payments_and_credist = 0;
  6027. }
  6028. // lift_gate handlingFee shippingFee
  6029. __block double lift_gate = 0;
  6030. __block double handlingFee = 0;
  6031. __block double shippingFee = 0;
  6032. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6033. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6034. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6035. if (have_lift_gate) {
  6036. lift_gate = sqlite3_column_double(stmt, 1);
  6037. }
  6038. handlingFee = sqlite3_column_double(stmt, 2);
  6039. shippingFee = sqlite3_column_double(stmt, 3);
  6040. }];
  6041. // total
  6042. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  6043. if (close) {
  6044. [iSalesDB close_db:db1];
  6045. }
  6046. return total;
  6047. }
  6048. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6049. {
  6050. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6051. int limit = [[params valueForKey:@"limit"] intValue];
  6052. int offset = [[params valueForKey:@"offset"] intValue];
  6053. NSString* keyword = [params valueForKey:@"keyWord"];
  6054. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6055. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6056. NSString* where=@"1 = 1";
  6057. if(keyword.length>0)
  6058. 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]];
  6059. if (orderStatus.length > 0) {
  6060. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6061. if (order_status_array.count == 1) {
  6062. int status_value = [[order_status_array firstObject] integerValue];
  6063. if (status_value <= 1 || status_value == 3) {
  6064. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6065. } else {
  6066. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6067. }
  6068. } else if (order_status_array.count > 1) {
  6069. for (int i = 0; i < order_status_array.count;i++) {
  6070. NSString *status = order_status_array[i];
  6071. NSString *condition = @" or";
  6072. if (i == 0) {
  6073. condition = @" and (";
  6074. }
  6075. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6076. int status_value = [status integerValue];
  6077. if (status_value <= 1 || status_value == 3) {
  6078. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6079. } else {
  6080. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6081. }
  6082. }
  6083. where = [where stringByAppendingString:@" )"];
  6084. }
  6085. }
  6086. 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,o.order_id is null from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.modify_time desc,o.create_time desc limit %d offset %d" ,where, limit, offset];
  6087. // DebugLog(@"order list sql: %@",sqlQuery);
  6088. sqlite3 *db = [iSalesDB get_db];
  6089. sqlite3_stmt * statement;
  6090. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6091. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6092. {
  6093. int count=0;
  6094. while (sqlite3_step(statement) == SQLITE_ROW)
  6095. {
  6096. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6097. int order_id = sqlite3_column_double(statement, 0);
  6098. char *soid = (char*)sqlite3_column_text(statement, 1);
  6099. if(soid==nil)
  6100. soid= "";
  6101. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6102. int status = sqlite3_column_double(statement, 2);
  6103. int erpStatus = sqlite3_column_double(statement, 9);
  6104. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6105. if(sales_rep==nil)
  6106. sales_rep= "";
  6107. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6108. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6109. if(create_by==nil)
  6110. create_by= "";
  6111. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6112. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6113. if(company_name==nil)
  6114. company_name= "";
  6115. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6116. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6117. if(create_time==nil)
  6118. create_time= "";
  6119. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6120. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6121. // double total_price = sqlite3_column_double(statement, 7);
  6122. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6123. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6124. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6125. int offline_edit = sqlite3_column_int(statement, 10);
  6126. // ": "JH",
  6127. // "": "$8307.00",
  6128. // "": "MOB1608050001",
  6129. // "": "ArpithaT",
  6130. // "": "1st Stage Property Transformations",
  6131. // "": "JANICE SUTTON",
  6132. // "": 2255,
  6133. // "": "08/02/2016 09:49:18",
  6134. // "": 1,
  6135. // "": "Saved Order"
  6136. // "": "1470384050483",
  6137. // "model_count": "6 / 28"
  6138. item[@"sales_rep"]= nssales_rep;
  6139. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6140. item[@"so#"]= nssoid;
  6141. item[@"create_by"]= nscreate_by;
  6142. item[@"customer_name"]= nscompany_name;
  6143. item[@"customer_contact"] = customer_contact;
  6144. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6145. item[@"purchase_time"]= nscreate_time;
  6146. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  6147. item[@"order_status"]= nsstatus;
  6148. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6149. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6150. // item[@"model_count"]
  6151. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6152. count++;
  6153. }
  6154. ret[@"count"]= [NSNumber numberWithInt:count];
  6155. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6156. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6157. ret[@"result"]= [NSNumber numberWithInt:2];
  6158. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6159. ret[@"time_zone"] = @"PST";
  6160. sqlite3_finalize(statement);
  6161. }
  6162. 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];
  6163. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6164. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6165. [iSalesDB close_db:db];
  6166. return [RAUtils dict2data:ret];
  6167. }
  6168. #pragma mark update gnotes
  6169. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6170. {
  6171. DebugLog(@"params: %@",params);
  6172. // comments = Meyoyoyoyoyoyoy;
  6173. // orderCode = MOB1608110001;
  6174. // password = 123456;
  6175. // user = EvanK;
  6176. NSString *sql = [NSString stringWithFormat:@"update offline_order set general_notes = '%@' where so_id = '%@';",[self valueInParams:params key:@"comments" translateSingleQuote:YES],[self valueInParams:params key:@"orderCode" translateSingleQuote:YES]];
  6177. int ret = [iSalesDB execSql:sql];
  6178. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6179. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6180. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6181. // [dic setValue:@"160409" forKey:@"min_ver"];
  6182. return [RAUtils dict2data:dic];
  6183. }
  6184. #pragma mark move to wishlist
  6185. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6186. {
  6187. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6188. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6189. _id = [NSString stringWithFormat:@"(%@)",_id];
  6190. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6191. sqlite3 *db = [iSalesDB get_db];
  6192. __block NSString *product_id = @"";
  6193. __block NSString *item_count_str = @"";
  6194. // __block NSString *item_id = nil;
  6195. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6196. // product_id = [self textAtColumn:0 statement:stmt];
  6197. int item_count = sqlite3_column_int(stmt, 1);
  6198. // item_id = [self textAtColumn:2 statement:stmt];
  6199. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6200. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6201. if (p_id.length) {
  6202. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6203. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6204. }
  6205. }];
  6206. [iSalesDB close_db:db];
  6207. // 去除第一个,
  6208. product_id = [product_id substringFromIndex:1];
  6209. item_count_str = [item_count_str substringFromIndex:1];
  6210. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6211. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6212. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6213. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6214. sqlite3 *db1 = [iSalesDB get_db];
  6215. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6216. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6217. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6218. // 删除
  6219. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6220. int ret = [iSalesDB execSql:deleteSql db:db1];
  6221. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6222. [iSalesDB close_db:db1];
  6223. return [RAUtils dict2data:dic];
  6224. }
  6225. #pragma mark cart delete
  6226. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6227. {
  6228. // cartItemId = 548;
  6229. // orderCode = MOB1608110001;
  6230. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6231. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6232. _id = [NSString stringWithFormat:@"(%@)",_id];
  6233. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6234. int ret = [iSalesDB execSql:sql];
  6235. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6236. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6237. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6238. // [dic setValue:@"160409" forKey:@"min_ver"];
  6239. return [RAUtils dict2data:dic];
  6240. }
  6241. #pragma mark set price
  6242. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6243. {
  6244. DebugLog(@"cart set price params: %@",params);
  6245. // "cartitem_id" = 1;
  6246. // discount = "0.000000";
  6247. // "item_note" = "";
  6248. // price = "269.000000";
  6249. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6250. NSString *notes = [self valueInParams:params key:@"item_note"];
  6251. NSString *discount = [self valueInParams:params key:@"discount"];
  6252. NSString *price = [self valueInParams:params key:@"price"];
  6253. // bool badd_price_changed=false;
  6254. // sqlite3* db=[iSalesDB get_db];
  6255. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6256. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6257. // NSRange range;
  6258. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6259. //
  6260. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6261. // badd_price_changed=true;
  6262. // [iSalesDB close_db:db];
  6263. //
  6264. // if(badd_price_changed)
  6265. // {
  6266. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6267. // }
  6268. //
  6269. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6270. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6271. int ret = [iSalesDB execSql:sql];
  6272. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6273. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6274. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6275. // [dic setValue:@"160409" forKey:@"min_ver"];
  6276. return [RAUtils dict2data:dic];
  6277. }
  6278. #pragma mark set line notes
  6279. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6280. {
  6281. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6282. NSString *notes = [self valueInParams:params key:@"notes"];
  6283. notes = [self translateSingleQuote:notes];
  6284. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6285. int ret = [iSalesDB execSql:sql];
  6286. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6287. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6288. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6289. // [dic setValue:@"160409" forKey:@"min_ver"];
  6290. return [RAUtils dict2data:dic];
  6291. }
  6292. #pragma mark set qty
  6293. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6294. {
  6295. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6296. int item_count = [params[@"inputInt"] integerValue];
  6297. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6298. int ret = [iSalesDB execSql:sql];
  6299. __block int item_id = 0;
  6300. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6301. item_id = sqlite3_column_int(stmt, 0);
  6302. }];
  6303. sqlite3 *db = [iSalesDB get_db];
  6304. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6305. [iSalesDB close_db:db];
  6306. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6307. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6308. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6309. // [dic setValue:@"160409" forKey:@"min_ver"];
  6310. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6311. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6312. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6313. return [RAUtils dict2data:dic];
  6314. }
  6315. #pragma mark place order
  6316. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6317. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6318. if (aname) {
  6319. [dic setValue:aname forKey:@"aname"];
  6320. }
  6321. if (control) {
  6322. [dic setValue:control forKey:@"control"];
  6323. }
  6324. if (keyboard) {
  6325. [dic setValue:keyboard forKey:@"keyboard"];
  6326. }
  6327. if (name) {
  6328. [dic setValue:name forKey:@"name"];
  6329. }
  6330. if (value) {
  6331. [dic setValue:value forKey:@"value"];
  6332. }
  6333. return dic;
  6334. }
  6335. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6336. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6337. orderCode = [self translateSingleQuote:orderCode];
  6338. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6339. 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,o.customer_email,o.customer_phone,o.customer_fax,c.zipcode,c.city,c.state,c.country,o.customer_contact from offline_contact as c join offline_order as o on c.contact_id = o.customer_cid where o.so_id = '%@';",orderCode];
  6340. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6341. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6342. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6343. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6344. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6345. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6346. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6347. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6348. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6349. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6350. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6351. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6352. NSString *email = [self textAtColumn:11 statement:stmt];
  6353. NSString *phone = [self textAtColumn:12 statement:stmt];
  6354. NSString *fax = [self textAtColumn:13 statement:stmt];
  6355. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6356. NSString *city = [self textAtColumn:15 statement:stmt];
  6357. NSString *state = [self textAtColumn:16 statement:stmt];
  6358. NSString *country = [self textAtColumn:17 statement:stmt];
  6359. NSString *name = [self textAtColumn:18 statement:stmt];
  6360. // contact id
  6361. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6362. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6363. [contact_id_dic setValue:@"text" forKey:@"control"];
  6364. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6365. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6366. [contact_id_dic setValue:@"true" forKey:@"required"];
  6367. [contact_id_dic setValue:contact_id forKey:@"value"];
  6368. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6369. // business card
  6370. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6371. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6372. [business_card_dic setValue:@"img" forKey:@"control"];
  6373. [business_card_dic setValue:@"1" forKey:@"disable"];
  6374. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6375. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6376. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6377. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6378. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6379. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6380. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6381. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6382. // fax
  6383. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6384. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6385. // zipcode
  6386. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6387. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6388. // city
  6389. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6390. [customer_dic setValue:city_dic forKey:@"item_12"];
  6391. // state
  6392. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6393. [customer_dic setValue:state_dic forKey:@"item_13"];
  6394. // country
  6395. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6396. [customer_dic setValue:country_dic forKey:@"item_14"];
  6397. // company name
  6398. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6399. [customer_dic setValue:company_dic forKey:@"item_2"];
  6400. // addr_1
  6401. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6402. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6403. // addr_2
  6404. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6405. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6406. // addr_3
  6407. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6408. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6409. // addr_4
  6410. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6411. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6412. // Contact
  6413. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6414. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6415. // email
  6416. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6417. [customer_dic setValue:email_dic forKey:@"item_8"];
  6418. // phone
  6419. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6420. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6421. // title
  6422. [customer_dic setValue:@"Customer" forKey:@"title"];
  6423. // count
  6424. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6425. }];
  6426. // setting
  6427. NSDictionary *setting = params[@"setting"];
  6428. NSNumber *hide = setting[@"CustomerHide"];
  6429. [customer_dic setValue:hide forKey:@"hide"];
  6430. return customer_dic;
  6431. }
  6432. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6433. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6434. // setting
  6435. NSDictionary *setting = params[@"setting"];
  6436. NSNumber *hide = setting[@"ShipToHide"];
  6437. [dic setValue:hide forKey:@"hide"];
  6438. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6439. orderCode = [self translateSingleQuote:orderCode];
  6440. 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];
  6441. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6442. NSString *cid = [self textAtColumn:0 statement:stmt];
  6443. NSString *name = [self textAtColumn:1 statement:stmt];
  6444. NSString *ext = [self textAtColumn:2 statement:stmt];
  6445. NSString *contact = [self textAtColumn:3 statement:stmt];
  6446. NSString *email = [self textAtColumn:4 statement:stmt];
  6447. NSString *fax = [self textAtColumn:5 statement:stmt];
  6448. NSString *phone = [self textAtColumn:6 statement:stmt];
  6449. // count
  6450. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6451. // title
  6452. [dic setValue:@"Ship To" forKey:@"title"];
  6453. // choose
  6454. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6455. [choose_dic setValue:@"choose" forKey:@"aname"];
  6456. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6457. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6458. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6459. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6460. @"receive_contact" : @"customer_contact",
  6461. @"receive_email" : @"customer_email",
  6462. @"receive_ext" : @"customer_contact_ext",
  6463. @"receive_fax" : @"customer_fax",
  6464. @"receive_name" : @"customer_name",
  6465. @"receive_phone" : @"customer_phone"},
  6466. @"refresh" : [NSNumber numberWithInteger:1],
  6467. @"type" : @"pull"};
  6468. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6469. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6470. @"name" : @"Add new address",
  6471. @"refresh" : [NSNumber numberWithInteger:1],
  6472. @"value" : @"new_addr"
  6473. };
  6474. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6475. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6476. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6477. @"receive_contact" : @"customer_contact",
  6478. @"receive_email" : @"customer_email",
  6479. @"receive_ext" : @"customer_contact_ext",
  6480. @"receive_fax" : @"customer_fax",
  6481. @"receive_name" : @"customer_name",
  6482. @"receive_phone" : @"customer_phone"},
  6483. @"name" : @"select_ship_to",
  6484. @"refresh" : [NSNumber numberWithInteger:1],
  6485. @"value" : @"Sales_Order_Ship_To"};
  6486. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6487. [dic setValue:choose_dic forKey:@"item_0"];
  6488. // contact id
  6489. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6490. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6491. [contact_id_dic setValue:@"true" forKey:@"required"];
  6492. [dic setValue:contact_id_dic forKey:@"item_1"];
  6493. // company name
  6494. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6495. [dic setValue:company_name_dic forKey:@"item_2"];
  6496. // address
  6497. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6498. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6499. if ([required integerValue]) {
  6500. [ext_dic setValue:@"true" forKey:@"required"];
  6501. }
  6502. [dic setValue:ext_dic forKey:@"item_3"];
  6503. // contact
  6504. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6505. [dic setValue:contact_dic forKey:@"item_4"];
  6506. // phone
  6507. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6508. [dic setValue:phone_dic forKey:@"item_5"];
  6509. // fax
  6510. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6511. [dic setValue:fax_dic forKey:@"item_6"];
  6512. // email
  6513. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6514. [dic setValue:email_dic forKey:@"item_7"];
  6515. }];
  6516. return dic;
  6517. }
  6518. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6519. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6520. // setting
  6521. NSDictionary *setting = params[@"setting"];
  6522. NSNumber *hide = setting[@"ShipFromHide"];
  6523. [dic setValue:hide forKey:@"hide"];
  6524. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6525. orderCode = [self translateSingleQuote:orderCode];
  6526. 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];
  6527. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,phone from offline_contact where contact_id = 'NPD';";
  6528. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6529. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6530. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6531. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6532. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  6533. __block NSString *email = [self textAtColumn:4 statement:stmt];
  6534. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  6535. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  6536. if (!cid.length) {
  6537. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  6538. cid = [self textAtColumn:0 statement:statment];
  6539. name = [self textAtColumn:1 statement:statment];
  6540. ext = [self textAtColumn:2 statement:statment];
  6541. contact = [self textAtColumn:3 statement:statment];
  6542. email = [self textAtColumn:4 statement:statment];
  6543. fax = [self textAtColumn:5 statement:statment];
  6544. phone = [self textAtColumn:6 statement:statment];
  6545. }];
  6546. }
  6547. // count
  6548. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6549. // title
  6550. [dic setValue:@"Ship From" forKey:@"title"];
  6551. // hide
  6552. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6553. // choose
  6554. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6555. [choose_dic setValue:@"choose" forKey:@"aname"];
  6556. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6557. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6558. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  6559. @"key_map" : @{@"sender_cid" : @"customer_cid",
  6560. @"sender_contact" : @"customer_contact",
  6561. @"sender_email" : @"customer_email",
  6562. @"sender_ext" : @"customer_contact_ext",
  6563. @"sender_fax" : @"customer_fax",
  6564. @"sender_name" : @"customer_name",
  6565. @"sender_phone" : @"customer_phone"},
  6566. @"name" : @"select_cid",
  6567. @"refresh" : [NSNumber numberWithInteger:0],
  6568. @"value" : @"Sales_Order_Ship_From"};
  6569. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  6570. [dic setValue:choose_dic forKey:@"item_0"];
  6571. // contact id
  6572. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6573. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6574. [contact_id_dic setValue:@"true" forKey:@"required"];
  6575. [dic setValue:contact_id_dic forKey:@"item_1"];
  6576. // company name
  6577. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6578. [dic setValue:company_name_dic forKey:@"item_2"];
  6579. // address
  6580. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6581. [dic setValue:ext_dic forKey:@"item_3"];
  6582. // contact
  6583. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6584. [dic setValue:contact_dic forKey:@"item_4"];
  6585. // phone
  6586. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6587. [dic setValue:phone_dic forKey:@"item_5"];
  6588. // fax
  6589. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6590. [dic setValue:fax_dic forKey:@"item_6"];
  6591. // email
  6592. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6593. [dic setValue:email_dic forKey:@"item_7"];
  6594. }];
  6595. return dic;
  6596. }
  6597. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  6598. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6599. // setting
  6600. NSDictionary *setting = params[@"setting"];
  6601. NSNumber *hide = setting[@"FreightBillToHide"];
  6602. [dic setValue:hide forKey:@"hide"];
  6603. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6604. orderCode = [self translateSingleQuote:orderCode];
  6605. 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];
  6606. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6607. NSString *cid = [self textAtColumn:0 statement:stmt];
  6608. NSString *name = [self textAtColumn:1 statement:stmt];
  6609. NSString *ext = [self textAtColumn:2 statement:stmt];
  6610. NSString *contact = [self textAtColumn:3 statement:stmt];
  6611. NSString *email = [self textAtColumn:4 statement:stmt];
  6612. NSString *fax = [self textAtColumn:5 statement:stmt];
  6613. NSString *phone = [self textAtColumn:6 statement:stmt];
  6614. // count
  6615. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6616. // title
  6617. [dic setValue:@"Freight Bill To" forKey:@"title"];
  6618. // hide
  6619. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6620. // choose
  6621. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6622. [choose_dic setValue:@"choose" forKey:@"aname"];
  6623. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6624. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6625. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6626. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  6627. @"shipping_billto_contact" : @"receive_contact",
  6628. @"shipping_billto_email" : @"receive_email",
  6629. @"shipping_billto_ext" : @"receive_ext",
  6630. @"shipping_billto_fax" : @"receive_fax",
  6631. @"shipping_billto_name" : @"receive_name",
  6632. @"shipping_billto_phone" : @"receive_phone"},
  6633. @"type" : @"pull"};
  6634. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6635. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6636. @"type" : @"pull",
  6637. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  6638. @"shipping_billto_contact" : @"customer_contact",
  6639. @"shipping_billto_email" : @"customer_email",
  6640. @"shipping_billto_ext" : @"customer_contact_ext",
  6641. @"shipping_billto_fax" : @"customer_fax",
  6642. @"shipping_billto_name" : @"customer_name",
  6643. @"shipping_billto_phone" : @"customer_phone"}
  6644. };
  6645. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6646. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6647. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  6648. @"shipping_billto_contact" : @"sender_contact",
  6649. @"shipping_billto_email" : @"sender_email",
  6650. @"shipping_billto_ext" : @"sender_ext",
  6651. @"shipping_billto_fax" : @"sender_fax",
  6652. @"shipping_billto_name" : @"sender_name",
  6653. @"shipping_billto_phone" : @"sender_phone"},
  6654. @"type" : @"pull"
  6655. };
  6656. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  6657. NSDictionary *select_freight_bill_to_dic = @{
  6658. @"aname" : @"Select freight bill to",
  6659. @"name" : @"select_cid",
  6660. @"refresh" : [NSNumber numberWithInteger:0],
  6661. @"value" : @"Sales_Order_Freight_Bill_To",
  6662. @"key_map" : @{
  6663. @"shipping_billto_cid" : @"customer_cid",
  6664. @"shipping_billto_contact" : @"customer_contact",
  6665. @"shipping_billto_email" : @"customer_email",
  6666. @"shipping_billto_ext" : @"customer_contact_ext",
  6667. @"shipping_billto_fax" : @"customer_fax",
  6668. @"shipping_billto_name" : @"customer_name",
  6669. @"shipping_billto_phone" : @"customer_phone"
  6670. }
  6671. };
  6672. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  6673. [dic setValue:choose_dic forKey:@"item_0"];
  6674. // contact id
  6675. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6676. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6677. [contact_id_dic setValue:@"true" forKey:@"required"];
  6678. [dic setValue:contact_id_dic forKey:@"item_1"];
  6679. // company name
  6680. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6681. [dic setValue:company_name_dic forKey:@"item_2"];
  6682. // address
  6683. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6684. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  6685. if ([ext_required integerValue]) {
  6686. [ext_dic setValue:@"true" forKey:@"required"];
  6687. }
  6688. [dic setValue:ext_dic forKey:@"item_3"];
  6689. // contact
  6690. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6691. [dic setValue:contact_dic forKey:@"item_4"];
  6692. // phone
  6693. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6694. [dic setValue:phone_dic forKey:@"item_5"];
  6695. // fax
  6696. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6697. [dic setValue:fax_dic forKey:@"item_6"];
  6698. // email
  6699. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6700. [dic setValue:email_dic forKey:@"item_7"];
  6701. }];
  6702. return dic;
  6703. }
  6704. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6705. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6706. // setting
  6707. NSDictionary *setting = params[@"setting"];
  6708. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  6709. [dic setValue:hide forKey:@"hide"];
  6710. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6711. orderCode = [self translateSingleQuote:orderCode];
  6712. 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];
  6713. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6714. NSString *cid = [self textAtColumn:0 statement:stmt];
  6715. NSString *name = [self textAtColumn:1 statement:stmt];
  6716. NSString *ext = [self textAtColumn:2 statement:stmt];
  6717. NSString *contact = [self textAtColumn:3 statement:stmt];
  6718. NSString *email = [self textAtColumn:4 statement:stmt];
  6719. NSString *fax = [self textAtColumn:5 statement:stmt];
  6720. NSString *phone = [self textAtColumn:6 statement:stmt];
  6721. // count
  6722. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6723. // title
  6724. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  6725. // hide
  6726. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6727. // choose
  6728. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6729. [choose_dic setValue:@"choose" forKey:@"aname"];
  6730. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6731. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6732. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6733. @"key_map" : @{@"billing_cid" : @"receive_cid",
  6734. @"billing_contact" : @"receive_contact",
  6735. @"billing_email" : @"receive_email",
  6736. @"billing_ext" : @"receive_ext",
  6737. @"billing_fax" : @"receive_fax",
  6738. @"billing_name" : @"receive_name",
  6739. @"billing_phone" : @"receive_phone"},
  6740. @"type" : @"pull"};
  6741. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6742. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6743. @"type" : @"pull",
  6744. @"key_map" : @{@"billing_cid" : @"customer_cid",
  6745. @"billing_contact" : @"customer_contact",
  6746. @"billing_email" : @"customer_email",
  6747. @"billing_ext" : @"customer_contact_ext",
  6748. @"billing_fax" : @"customer_fax",
  6749. @"billing_name" : @"customer_name",
  6750. @"billing_phone" : @"customer_phone"}
  6751. };
  6752. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6753. NSDictionary *select_bill_to_dic = @{
  6754. @"aname" : @"Select bill to",
  6755. @"name" : @"select_cid",
  6756. @"refresh" : [NSNumber numberWithInteger:0],
  6757. @"value" : @"Sales_Order_Merchandise_Bill_To",
  6758. @"key_map" : @{
  6759. @"billing_cid" : @"customer_cid",
  6760. @"billing_contact" : @"customer_contact",
  6761. @"billing_email" : @"customer_email",
  6762. @"billing_ext" : @"customer_contact_ext",
  6763. @"billing_fax" : @"customer_fax",
  6764. @"billing_name" : @"customer_name",
  6765. @"billing_phone" : @"customer_phone"
  6766. }
  6767. };
  6768. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  6769. [dic setValue:choose_dic forKey:@"item_0"];
  6770. // contact id
  6771. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6772. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6773. [contact_id_dic setValue:@"true" forKey:@"required"];
  6774. [dic setValue:contact_id_dic forKey:@"item_1"];
  6775. // company name
  6776. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6777. [dic setValue:company_name_dic forKey:@"item_2"];
  6778. // address
  6779. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6780. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  6781. if ([ext_required integerValue]) {
  6782. [ext_dic setValue:@"true" forKey:@"required"];
  6783. }
  6784. [dic setValue:ext_dic forKey:@"item_3"];
  6785. // contact
  6786. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6787. [dic setValue:contact_dic forKey:@"item_4"];
  6788. // phone
  6789. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6790. [dic setValue:phone_dic forKey:@"item_5"];
  6791. // fax
  6792. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6793. [dic setValue:fax_dic forKey:@"item_6"];
  6794. // email
  6795. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6796. [dic setValue:email_dic forKey:@"item_7"];
  6797. }];
  6798. return dic;
  6799. }
  6800. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6801. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6802. // setting
  6803. NSDictionary *setting = params[@"setting"];
  6804. NSNumber *hide = setting[@"ReturnToHide"];
  6805. [dic setValue:hide forKey:@"hide"];
  6806. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6807. orderCode = [self translateSingleQuote:orderCode];
  6808. 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];
  6809. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6810. NSString *cid = [self textAtColumn:0 statement:stmt];
  6811. NSString *name = [self textAtColumn:1 statement:stmt];
  6812. NSString *ext = [self textAtColumn:2 statement:stmt];
  6813. NSString *contact = [self textAtColumn:3 statement:stmt];
  6814. NSString *email = [self textAtColumn:4 statement:stmt];
  6815. NSString *fax = [self textAtColumn:5 statement:stmt];
  6816. NSString *phone = [self textAtColumn:6 statement:stmt];
  6817. // count
  6818. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6819. // title
  6820. [dic setValue:@"Return To" forKey:@"title"];
  6821. // hide
  6822. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6823. // choose
  6824. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6825. [choose_dic setValue:@"choose" forKey:@"aname"];
  6826. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6827. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  6828. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6829. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  6830. @"returnto_contact" : @"sender_contact",
  6831. @"returnto_email" : @"sender_email",
  6832. @"returnto_ext" : @"sender_ext",
  6833. @"returnto_fax" : @"sender_fax",
  6834. @"returnto_name" : @"sender_name",
  6835. @"returnto_phone" : @"sender_phone"},
  6836. @"type" : @"pull"};
  6837. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  6838. NSDictionary *select_return_to_dic = @{
  6839. @"aname" : @"Select return to",
  6840. @"name" : @"select_cid",
  6841. @"refresh" : [NSNumber numberWithInteger:0],
  6842. @"value" : @"Contact_Return_To",
  6843. @"key_map" : @{
  6844. @"returnto_cid" : @"customer_cid",
  6845. @"returnto_contact" : @"customer_contact",
  6846. @"returnto_email" : @"customer_email",
  6847. @"returnto_ext" : @"customer_contact_ext",
  6848. @"returnto_fax" : @"customer_fax",
  6849. @"returnto_name" : @"customer_name",
  6850. @"returnto_phone" : @"customer_phone"
  6851. }
  6852. };
  6853. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  6854. [dic setValue:choose_dic forKey:@"item_0"];
  6855. // contact id
  6856. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6857. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6858. [contact_id_dic setValue:@"true" forKey:@"required"];
  6859. [dic setValue:contact_id_dic forKey:@"item_1"];
  6860. // company name
  6861. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6862. [dic setValue:company_name_dic forKey:@"item_2"];
  6863. // address
  6864. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6865. [dic setValue:ext_dic forKey:@"item_3"];
  6866. // contact
  6867. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6868. [dic setValue:contact_dic forKey:@"item_4"];
  6869. // phone
  6870. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6871. [dic setValue:phone_dic forKey:@"item_5"];
  6872. // fax
  6873. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6874. [dic setValue:fax_dic forKey:@"item_6"];
  6875. // email
  6876. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6877. [dic setValue:email_dic forKey:@"item_7"];
  6878. }];
  6879. return dic;
  6880. }
  6881. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6882. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6883. orderCode = [self translateSingleQuote:orderCode];
  6884. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6885. __block double TotalCuft = 0;
  6886. __block double TotalWeight = 0;
  6887. __block int TotalCarton = 0;
  6888. __block double payments = 0;
  6889. // setting
  6890. NSDictionary *setting = params[@"setting"];
  6891. NSNumber *hide = setting[@"ModelInformationHide"];
  6892. [dic setValue:hide forKey:@"hide"];
  6893. NSString *sql = [NSString stringWithFormat:@"select c.item_id,c.item_count,m.stockUom,c.price,c.discount,m.name,m.description,c.line_note,m.availability 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];
  6894. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6895. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6896. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  6897. // item id
  6898. int item_id = sqlite3_column_int(stmt, 0);
  6899. // count
  6900. int item_count = sqlite3_column_int(stmt, 1);
  6901. // stockUom
  6902. int stockUom = sqlite3_column_int(stmt, 2);
  6903. // unit price
  6904. NSString *str_price = [self textAtColumn:3 statement:stmt];
  6905. NSString* Price=nil;
  6906. if([str_price isEqualToString:@""])
  6907. {
  6908. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  6909. if(price==nil)
  6910. Price=@"No Price.";
  6911. else
  6912. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6913. }
  6914. else
  6915. {
  6916. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  6917. }
  6918. // discount
  6919. double discount = sqlite3_column_double(stmt, 4);
  6920. // name
  6921. NSString *name = [self textAtColumn:5 statement:stmt];
  6922. // description
  6923. NSString *description = [self textAtColumn:6 statement:stmt];
  6924. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  6925. // line note
  6926. NSString *line_note = [self textAtColumn:7 statement:stmt];
  6927. int avaulability = sqlite3_column_int(stmt, 8);
  6928. // img
  6929. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  6930. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  6931. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6932. if(combine != nil)
  6933. {
  6934. // int citem=0;
  6935. int bcount=[[combine valueForKey:@"count"] intValue];
  6936. for(int bc=0;bc<bcount;bc++)
  6937. {
  6938. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6939. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6940. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6941. subTotal += uprice * modulus * item_count;
  6942. }
  6943. }
  6944. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  6945. [model_dic setValue:@"model" forKey:@"control"];
  6946. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  6947. [model_dic setValue:description forKey:@"description"];
  6948. [model_dic setValue:line_note forKey:@"note"];
  6949. [model_dic setValue:img forKey:@"img_url"];
  6950. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  6951. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  6952. [model_dic setValue:Price forKey:@"unit_price"];
  6953. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  6954. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  6955. if (combine) {
  6956. [model_dic setValue:combine forKey:@"combine"];
  6957. }
  6958. // well,what under the row is the info for total
  6959. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6960. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6961. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6962. int carton=[bsubtotaljson[@"carton"] intValue];
  6963. TotalCuft += cuft;
  6964. TotalWeight += weight;
  6965. TotalCarton += carton;
  6966. payments += subTotal;
  6967. //---------------------------
  6968. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  6969. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  6970. }];
  6971. [dic setValue:@"Model Information" forKey:@"title"];
  6972. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  6973. return dic;
  6974. }
  6975. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  6976. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6977. [dic setValue:@"Remarks Content" forKey:@"title"];
  6978. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6979. // setting
  6980. NSDictionary *setting = params[@"setting"];
  6981. NSNumber *hide = setting[@"RemarksContentHide"];
  6982. [dic setValue:hide forKey:@"hide"];
  6983. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6984. orderCode = [self translateSingleQuote:orderCode];
  6985. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  6986. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6987. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  6988. int mustCall = sqlite3_column_int(stmt, 1);
  6989. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  6990. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  6991. NSDictionary *po_dic = @{
  6992. @"aname" : @"PO#",
  6993. @"control" : @"edit",
  6994. @"keyboard" : @"text",
  6995. @"name" : @"poNumber",
  6996. @"value" : poNumber
  6997. };
  6998. NSDictionary *must_call_dic = @{
  6999. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7000. @"control" : @"switch",
  7001. @"name" : @"must_call",
  7002. @"value" : mustCall ? @"true" : @"false"
  7003. };
  7004. NSDictionary *general_notes_dic = @{
  7005. @"aname" : @"General notes",
  7006. @"control" : @"text_view",
  7007. @"keyboard" : @"text",
  7008. @"name" : @"comments",
  7009. @"value" : generalNotes
  7010. };
  7011. NSDictionary *internal_notes_dic = @{
  7012. @"aname" : @"Internal notes",
  7013. @"control" : @"text_view",
  7014. @"keyboard" : @"text",
  7015. @"name" : @"internal_notes",
  7016. @"value" : internalNotes
  7017. };
  7018. [dic setValue:po_dic forKey:@"item_0"];
  7019. [dic setValue:must_call_dic forKey:@"item_1"];
  7020. [dic setValue:general_notes_dic forKey:@"item_2"];
  7021. [dic setValue:internal_notes_dic forKey:@"item_3"];
  7022. }];
  7023. return dic;
  7024. }
  7025. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7026. // params
  7027. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7028. orderCode = [self translateSingleQuote:orderCode];
  7029. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7030. // setting
  7031. NSDictionary *setting = params[@"setting"];
  7032. NSNumber *hide = setting[@"OrderTotalHide"];
  7033. [dic setValue:hide forKey:@"hide"];
  7034. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7035. __block double lift_gate_value = 0;
  7036. __block double handling_fee = 0;
  7037. __block double shipping = 0;
  7038. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7039. int lift_gate = sqlite3_column_int(stmt, 0);
  7040. lift_gate_value = sqlite3_column_double(stmt, 1);
  7041. shipping = sqlite3_column_double(stmt, 2);
  7042. handling_fee = sqlite3_column_double(stmt, 3);
  7043. if (!lift_gate) {
  7044. lift_gate_value = 0;
  7045. }
  7046. }];
  7047. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7048. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7049. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7050. double payments = [[total valueForKey:@"payments"] doubleValue];
  7051. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7052. double totalPrice = payments;
  7053. [dic setValue:@"Order Total" forKey:@"title"];
  7054. NSDictionary *payments_dic = @{
  7055. @"align" : @"right",
  7056. @"aname" : @"Payments/Credits",
  7057. @"control" : @"text",
  7058. @"name" : @"paymentsAndCredits",
  7059. @"type" : @"price",
  7060. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7061. };
  7062. [dic setValue:payments_dic forKey:@"item_0"];
  7063. NSDictionary *handling_fee_dic = @{
  7064. @"align" : @"right",
  7065. @"aname" : @"Handling Fee",
  7066. @"control" : @"text",
  7067. @"name" : @"handling_fee_value",
  7068. @"required" : @"true",
  7069. @"type" : @"price",
  7070. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7071. };
  7072. NSDictionary *shipping_dic = @{
  7073. @"align" : @"right",
  7074. @"aname" : @"Shipping*",
  7075. @"control" : @"text",
  7076. @"name" : @"shipping",
  7077. @"required" : @"true",
  7078. @"type" : @"price",
  7079. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7080. };
  7081. NSDictionary *lift_gate_dic = @{
  7082. @"align" : @"right",
  7083. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7084. @"control" : @"text",
  7085. @"name" : @"lift_gate_value",
  7086. @"required" : @"true",
  7087. @"type" : @"price",
  7088. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7089. };
  7090. int item_count = 1;
  7091. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7092. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7093. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7094. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7095. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7096. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7097. } else {
  7098. }
  7099. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7100. NSDictionary *total_price_dic = @{
  7101. @"align" : @"right",
  7102. @"aname" : @"Total",
  7103. @"control" : @"text",
  7104. @"name" : @"totalPrice",
  7105. @"type" : @"price",
  7106. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7107. };
  7108. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7109. NSDictionary *total_cuft_dic = @{
  7110. @"align" : @"right",
  7111. @"aname" : @"Total Cuft",
  7112. @"control" : @"text",
  7113. @"name" : @"",
  7114. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7115. };
  7116. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7117. NSDictionary *total_weight_dic = @{
  7118. @"align" : @"right",
  7119. @"aname" : @"Total Weight",
  7120. @"control" : @"text",
  7121. @"name" : @"",
  7122. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7123. };
  7124. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7125. NSDictionary *total_carton_dic = @{
  7126. @"align" : @"right",
  7127. @"aname" : @"Total Carton",
  7128. @"control" : @"text",
  7129. @"name" : @"",
  7130. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7131. };
  7132. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7133. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7134. return dic;
  7135. }
  7136. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7137. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7138. orderCode = [self translateSingleQuote:orderCode];
  7139. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7140. // setting
  7141. NSDictionary *setting = params[@"setting"];
  7142. NSNumber *hide = setting[@"SignatureHide"];
  7143. [dic setValue:hide forKey:@"hide"];
  7144. [dic setValue:@"Signature" forKey:@"title"];
  7145. __block NSString *pic_path = @"";
  7146. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7147. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7148. pic_path = [self textAtColumn:0 statement:stmt];
  7149. }];
  7150. NSDictionary *pic_dic = @{
  7151. @"aname" : @"Signature",
  7152. @"avalue" :pic_path,
  7153. @"control" : @"signature",
  7154. @"name" : @"sign_picpath",
  7155. @"value" : pic_path
  7156. };
  7157. [dic setValue:pic_dic forKey:@"item_0"];
  7158. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7159. return dic;
  7160. }
  7161. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7162. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7163. // setting
  7164. NSDictionary *setting = params[@"setting"];
  7165. NSNumber *hide = setting[@"ShippingMethodHide"];
  7166. [dic setValue:hide forKey:@"hide"];
  7167. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7168. orderCode = [self translateSingleQuote:orderCode];
  7169. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7170. __block NSString *logist = @"";
  7171. __block NSString *lift_gate = @"";
  7172. __block int lift_gate_integer = 0;
  7173. __block NSString *logistic_note = @"";
  7174. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7175. logist = [self textAtColumn:0 statement:stmt];
  7176. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7177. logistic_note = [self textAtColumn:2 statement:stmt];
  7178. }];
  7179. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7180. [dic setValue:@"Shipping Method" forKey:@"title"];
  7181. // val_0
  7182. int PERSONAL_PICK_UP_check = 0;
  7183. int USE_MY_CARRIER_check = 0;
  7184. NSString *logistic_note_text = @"";
  7185. int will_call_check = 0;
  7186. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7187. will_call_check = 1;
  7188. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7189. PERSONAL_PICK_UP_check = 1;
  7190. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7191. USE_MY_CARRIER_check = 1;
  7192. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7193. logistic_note = [logistic_note_array firstObject];
  7194. if (logistic_note_array.count > 1) {
  7195. logistic_note_text = [logistic_note_array lastObject];
  7196. }
  7197. }
  7198. }
  7199. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7200. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7201. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7202. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7203. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7204. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7205. NSDictionary *val0_subItem_item0 = @{
  7206. @"aname" : @"Option",
  7207. @"cadedate" : @{
  7208. @"count" : [NSNumber numberWithInteger:2],
  7209. @"val_0" : @{
  7210. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7211. @"refresh" : [NSNumber numberWithInteger:0],
  7212. @"value" : @"PERSONAL PICK UP",
  7213. @"value_id" : @"PERSONAL PICK UP"
  7214. },
  7215. @"val_1" : @{
  7216. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7217. @"refresh" : [NSNumber numberWithInteger:0],
  7218. @"sub_item" : @{
  7219. @"count" : [NSNumber numberWithInteger:1],
  7220. @"item_0" : @{
  7221. @"aname" : @"BOL",
  7222. @"control" : @"edit",
  7223. @"keyboard" : @"text",
  7224. @"name" : @"logist_note_text",
  7225. @"refresh" : [NSNumber numberWithInteger:0],
  7226. @"required" : @"false",
  7227. @"value" : logistic_note_text
  7228. }
  7229. },
  7230. @"value" : @"USE MY CARRIER",
  7231. @"value_id" : @"USE MY CARRIER"
  7232. }
  7233. },
  7234. @"control" : @"enum",
  7235. @"name" : @"logistic_note",
  7236. @"required" : @"true",
  7237. @"single_select" : @"true"
  7238. };
  7239. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7240. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7241. // val_1
  7242. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7243. if([lift_gate isEqualToString:@""]) {
  7244. if (lift_gate_integer == 1) {
  7245. lift_gate = @"true";
  7246. } else {
  7247. lift_gate = @"false";
  7248. }
  7249. }
  7250. int common_carrier_check = 0;
  7251. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7252. common_carrier_check = 1;
  7253. [params setValue:lift_gate forKey:@"lift_gate"];
  7254. }
  7255. NSDictionary *val_1 = @{
  7256. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7257. @"sub_item" : @{
  7258. @"count" : [NSNumber numberWithInteger:1],
  7259. @"item_0" : @{
  7260. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7261. @"control" : @"switch",
  7262. @"name" : @"lift_gate",
  7263. @"refresh" : [NSNumber numberWithInteger:1],
  7264. @"required" : @"true",
  7265. @"value" : lift_gate
  7266. }
  7267. },
  7268. @"value" : @"COMMON CARRIER",
  7269. @"value_id" : @"COMMON CARRIER"
  7270. };
  7271. // cadedate
  7272. NSDictionary *cadedate = @{
  7273. @"count" : [NSNumber numberWithInteger:2],
  7274. @"val_0" : val_0,
  7275. @"val_1" : val_1
  7276. };
  7277. // item_0
  7278. NSMutableDictionary *item_0 = @{
  7279. @"aname" : @"Shipping",
  7280. @"cadedate" : cadedate,
  7281. @"control" : @"enum",
  7282. @"name" : @"logist",
  7283. @"refresh" : [NSNumber numberWithInteger:1],
  7284. @"single_select" : @"true",
  7285. }.mutableCopy;
  7286. NSNumber *required = setting[@"ShippingMethodRequire"];
  7287. if ([required integerValue]) {
  7288. [item_0 setValue:@"true" forKey:@"required"];
  7289. }
  7290. [dic setValue:item_0 forKey:@"item_0"];
  7291. return dic;
  7292. }
  7293. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7294. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7295. orderCode = [self translateSingleQuote:orderCode];
  7296. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7297. __block int submit_as_integer = 0;
  7298. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7299. submit_as_integer = sqlite3_column_int(stmt, 0);
  7300. }];
  7301. int check11 = 0;
  7302. int check10 = 0;
  7303. if (submit_as_integer == 11) {
  7304. check11 = 1;
  7305. }
  7306. if (submit_as_integer == 10) {
  7307. check10 = 1;
  7308. }
  7309. // section_0
  7310. NSMutableDictionary *dic = @{
  7311. @"count" : @(1),
  7312. @"item_0" : @{
  7313. @"aname" : @"Submit Order As",
  7314. @"cadedate" : @{
  7315. @"count" : @(2),
  7316. @"val_0" : @{
  7317. @"check" : [NSNumber numberWithInteger:check11],
  7318. @"value" : @"Sales Order",
  7319. @"value_id" : @(11)
  7320. },
  7321. @"val_1" : @{
  7322. @"check" : [NSNumber numberWithInteger:check10],
  7323. @"value" : @"Quote",
  7324. @"value_id" : @(10)
  7325. }
  7326. },
  7327. @"control" : @"enum",
  7328. @"name" : @"erpOrderStatus",
  7329. @"required" : @"true",
  7330. @"single_select" : @"true"
  7331. },
  7332. @"title" : @"Order Type"
  7333. }.mutableCopy;
  7334. // setting
  7335. NSDictionary *setting = params[@"setting"];
  7336. NSNumber *hide = setting[@"OrderTypeHide"];
  7337. [dic setValue:hide forKey:@"hide"];
  7338. return dic;
  7339. }
  7340. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7341. countryCode = [self translateSingleQuote:countryCode];
  7342. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7343. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7344. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7345. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7346. if (name == NULL) {
  7347. name = "";
  7348. }
  7349. if (code == NULL) {
  7350. code = "";
  7351. }
  7352. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7353. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7354. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7355. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7356. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7357. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7358. }
  7359. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7360. [container setValue:stateDic forKey:key];
  7361. }] mutableCopy];
  7362. // failure 可以不用了,一样的
  7363. if (ret.allKeys.count == 0) {
  7364. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7365. [stateDic setValue:@"Other" forKey:@"value"];
  7366. [stateDic setValue:@"" forKey:@"value_id"];
  7367. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7368. if (state_code && [@"" isEqualToString:state_code]) {
  7369. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7370. }
  7371. NSString *key = [NSString stringWithFormat:@"val_0"];
  7372. [ret setValue:stateDic forKey:key];
  7373. }
  7374. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  7375. return ret;
  7376. }
  7377. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7378. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7379. orderCode = [self translateSingleQuote:orderCode];
  7380. 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];
  7381. __block NSString *payType = @"";
  7382. __block NSString *firstName = @"";
  7383. __block NSString *lastName = @"";
  7384. __block NSString *addr1 = @"";
  7385. __block NSString *addr2 = @"";
  7386. __block NSString *zipcode = @"";
  7387. __block NSString *cardType = @"";
  7388. __block NSString *cardNumber = @"";
  7389. __block NSString *securityCode = @"";
  7390. __block NSString *month = @"";
  7391. __block NSString *year = @"";
  7392. __block NSString *city = @"";
  7393. __block NSString *state = @"";
  7394. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7395. payType = [self textAtColumn:0 statement:stmt];
  7396. firstName = [self textAtColumn:1 statement:stmt];
  7397. lastName = [self textAtColumn:2 statement:stmt];
  7398. addr1 = [self textAtColumn:3 statement:stmt];
  7399. addr2 = [self textAtColumn:4 statement:stmt];
  7400. zipcode = [self textAtColumn:5 statement:stmt];
  7401. cardType = [self textAtColumn:6 statement:stmt];
  7402. cardNumber = [self textAtColumn:7 statement:stmt];
  7403. securityCode = [self textAtColumn:8 statement:stmt];
  7404. month = [self textAtColumn:9 statement:stmt];
  7405. year = [self textAtColumn:10 statement:stmt];
  7406. city = [self textAtColumn:11 statement:stmt];
  7407. state = [self textAtColumn:12 statement:stmt];
  7408. }];
  7409. NSString *required = @"true";
  7410. // setting
  7411. NSDictionary *setting = params[@"setting"];
  7412. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7413. if ([requiredNumber integerValue]) {
  7414. required = @"true";
  7415. } else {
  7416. required = @"false";
  7417. }
  7418. // "section_2"
  7419. NSMutableDictionary *dic = @{
  7420. @"count" : @(1),
  7421. @"item_0" : @{
  7422. @"aname" : @"Payment",
  7423. @"required" : required,
  7424. @"cadedate" : @{
  7425. @"count" : @(6),
  7426. @"val_0" : @{
  7427. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  7428. @"value" : @"Check",
  7429. @"value_id" : @"Check"
  7430. },
  7431. @"val_1" : @{
  7432. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  7433. @"value" : @"Cash",
  7434. @"value_id" : @"Cash"
  7435. },
  7436. @"val_2" : @{
  7437. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  7438. @"value" : @"NET 30",
  7439. @"value_id" : @"NET 30"
  7440. },
  7441. @"val_3" : @{
  7442. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  7443. @"value" : @"Wire Transfer",
  7444. @"value_id" : @"Wire Transfer"
  7445. },
  7446. @"val_4" : @{
  7447. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  7448. @"sub_item" : @{
  7449. @"count" : @(3),
  7450. @"item_0" : @{
  7451. @"aname" : @"choose",
  7452. @"control" : @"multi_action",
  7453. @"count" : @(1),
  7454. @"item_0" : @{
  7455. @"aname" : @"Same as customer",
  7456. @"key_map" : @{
  7457. @"credit_card_address1" : @"customer_address1",
  7458. @"credit_card_address2" : @"customer_address2",
  7459. @"credit_card_city" : @"customer_city",
  7460. @"credit_card_first_name" : @"customer_first_name",
  7461. @"credit_card_last_name" : @"customer_last_name",
  7462. @"credit_card_state" : @"customer_state",
  7463. @"credit_card_zipcode" : @"customer_zipcode"
  7464. },
  7465. @"type" : @"pull"
  7466. }
  7467. },
  7468. @"item_1" : @{
  7469. @"aname" : @"",
  7470. @"color" : @"red",
  7471. @"control" : @"text",
  7472. @"name" : @"",
  7473. @"value" : @"USA Credit cards only"
  7474. },
  7475. @"item_2" : @{
  7476. @"aname" : @"Fill",
  7477. @"cadedate" : @{
  7478. @"count" : @(2),
  7479. @"val_0" : @{
  7480. @"check" : @(1),
  7481. @"sub_item" : @{
  7482. @"count" : @(11),
  7483. @"item_0" : @{
  7484. @"aname" : @"Type",
  7485. @"cadedate" : @{
  7486. @"count" : @(2),
  7487. @"val_0" : @{
  7488. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7489. @"value" : @"VISA",
  7490. @"value_id" : @(0)
  7491. },
  7492. @"val_1" : @{
  7493. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7494. @"value" : @"MASTER CARD",
  7495. @"value_id" : @(1)
  7496. }
  7497. },
  7498. @"control" : @"enum",
  7499. @"name" : @"credit_card_type",
  7500. @"required" : @"true",
  7501. @"single_select" : @"true"
  7502. },
  7503. @"item_1" : @{
  7504. @"aname" : @"Number",
  7505. @"control" : @"edit",
  7506. @"keyboard" : @"int",
  7507. @"length" : @"16",
  7508. @"name" : @"credit_card_number",
  7509. @"required" : @"true",
  7510. @"value" : cardNumber
  7511. },
  7512. @"item_10" : @{
  7513. @"aname" : @"State",
  7514. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7515. @"control" : @"enum",
  7516. @"enum" : @"true",
  7517. @"name" : @"credit_card_state",
  7518. @"required" : @"true",
  7519. @"single_select" : @"true"
  7520. },
  7521. @"item_2" : @{
  7522. @"aname" : @"Expiration Date",
  7523. @"control" : @"monthpicker",
  7524. @"name" : @"credit_card_expiration",
  7525. @"required" : @"true",
  7526. @"type" : @"date",
  7527. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7528. },
  7529. @"item_3" : @{
  7530. @"aname" : @"Security Code",
  7531. @"control" : @"edit",
  7532. @"keyboard" : @"int",
  7533. @"length" : @"3",
  7534. @"name" : @"credit_card_security_code",
  7535. @"required" : @"true",
  7536. @"value" : securityCode
  7537. },
  7538. @"item_4" : @{
  7539. @"aname" : @"First Name",
  7540. @"control" : @"edit",
  7541. @"keyboard" : @"text",
  7542. @"name" : @"credit_card_first_name",
  7543. @"required" : @"true",
  7544. @"value" : firstName
  7545. },
  7546. @"item_5" : @{
  7547. @"aname" : @"Last Name",
  7548. @"control" : @"edit",
  7549. @"keyboard" : @"text",
  7550. @"name" : @"credit_card_last_name",
  7551. @"required" : @"true",
  7552. @"value" : lastName
  7553. },
  7554. @"item_6" : @{
  7555. @"aname" : @"Address 1",
  7556. @"control" : @"edit",
  7557. @"keyboard" : @"text",
  7558. @"name" : @"credit_card_address1",
  7559. @"required" : @"true",
  7560. @"value" : addr1
  7561. },
  7562. @"item_7" : @{
  7563. @"aname" : @"Address 2",
  7564. @"control" : @"edit",
  7565. @"keyboard" : @"text",
  7566. @"name" : @"credit_card_address2",
  7567. @"value" : addr2
  7568. },
  7569. @"item_8" : @{
  7570. @"aname" : @"zip code",
  7571. @"control" : @"edit",
  7572. @"keyboard" : @"text",
  7573. @"name" : @"credit_card_zipcode",
  7574. @"required" : @"true",
  7575. @"value" : zipcode
  7576. },
  7577. @"item_9" : @{
  7578. @"aname" : @"City",
  7579. @"control" : @"edit",
  7580. @"keyboard" : @"text",
  7581. @"name" : @"credit_card_city",
  7582. @"required" : @"true",
  7583. @"value" : city
  7584. }
  7585. },
  7586. @"value" : @"Fill Now",
  7587. @"value_id" : @""
  7588. },
  7589. @"val_1" : @{
  7590. @"check" : @(0),
  7591. @"value" : @"Fill Later",
  7592. @"value_id" : @""
  7593. }
  7594. },
  7595. @"control" : @"enum",
  7596. @"name" : @"",
  7597. @"single_select" : @"true"
  7598. }
  7599. },
  7600. @"value" : @"Credit Card",
  7601. @"value_id" : @"Credit Card"
  7602. },
  7603. @"val_5" : @{
  7604. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  7605. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  7606. @"value_id" : @"FOLLOW EXISTING"
  7607. }
  7608. },
  7609. @"control" : @"enum",
  7610. @"name" : @"paymentType",
  7611. @"single_select" : @"true"
  7612. },
  7613. @"title" : @"Payment Information"
  7614. }.mutableCopy;
  7615. NSNumber *hide = setting[@"PaymentInformationHide"];
  7616. [dic setValue:hide forKey:@"hide"];
  7617. return dic;
  7618. }
  7619. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  7620. {
  7621. sqlite3 *db = [iSalesDB get_db];
  7622. // UISetting
  7623. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7624. NSString *cachefolder = [paths objectAtIndex:0];
  7625. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  7626. NSData* json =nil;
  7627. json=[NSData dataWithContentsOfFile:img_cache];
  7628. NSError *error=nil;
  7629. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  7630. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  7631. [params setObject:setting forKey:@"setting"];
  7632. int section_count = 0;
  7633. // params
  7634. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7635. orderCode = [self translateSingleQuote:orderCode];
  7636. // 0 Order Type 1 Shipping Method 2 Payment Information
  7637. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  7638. // 0 Order Type
  7639. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  7640. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  7641. [ret setValue:order_type_dic forKey:key0];
  7642. // 1 Shipping Method
  7643. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  7644. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  7645. [ret setValue:shipping_method_dic forKey:key1];
  7646. // 2 Payment Information
  7647. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  7648. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  7649. [ret setValue:payment_info_dic forKey:key2];
  7650. // 3 Customer
  7651. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  7652. NSDictionary *customer_dic = [self customerDic:params db:db];
  7653. [ret setValue:customer_dic forKey:key3];
  7654. // 4 Ship To
  7655. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  7656. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  7657. [ret setValue:ship_to_dic forKey:key4];
  7658. // 5 Ship From
  7659. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  7660. if (![shipFromDisable integerValue]) {
  7661. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  7662. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  7663. [ret setValue:ship_from_dic forKey:key5];
  7664. }
  7665. // 6 Freight Bill To
  7666. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  7667. if (![freightBillToDisable integerValue]) {
  7668. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  7669. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  7670. [ret setValue:freight_bill_to forKey:key6];
  7671. }
  7672. // 7 Merchandise Bill To
  7673. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  7674. if (![merchandiseBillToDisable integerValue]) {
  7675. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  7676. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  7677. [ret setValue:merchandise_bill_to_dic forKey:key7];
  7678. }
  7679. // 8 Return To
  7680. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  7681. if (![returnToDisable integerValue]) {
  7682. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  7683. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  7684. [ret setValue:return_to_dic forKey:key8];
  7685. }
  7686. // 9 Model Information
  7687. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  7688. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  7689. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  7690. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  7691. [ret setValue:model_info_dic forKey:key9];
  7692. // 10 Remarks Content
  7693. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  7694. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  7695. [ret setValue:remarks_content_dic forKey:key10];
  7696. // 11 Order Total
  7697. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  7698. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  7699. [ret setValue:order_total_dic forKey:key11];
  7700. // 12 Signature
  7701. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  7702. NSDictionary *sign_dic = [self signatureDic:params db:db];
  7703. [ret setValue:sign_dic forKey:key12];
  7704. [ret setValue:@(section_count) forKey:@"section_count"];
  7705. [iSalesDB close_db:db];
  7706. return [RAUtils dict2data:ret];
  7707. // return nil;
  7708. }
  7709. #pragma mark addr editor
  7710. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  7711. NSMutableDictionary *new_item = [item mutableCopy];
  7712. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  7713. return new_item;
  7714. }
  7715. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  7716. {
  7717. // "is_subaction" = true;
  7718. // orderCode = MOB1608240002;
  7719. // password = 123456;
  7720. // "subaction_tag" = 1;
  7721. // user = EvanK;
  7722. // {
  7723. // "is_subaction" = true;
  7724. // orderCode = MOB1608240002;
  7725. // password = 123456;
  7726. // "refresh_trigger" = zipcode;
  7727. // "subaction_tag" = 1;
  7728. // user = EvanK;
  7729. // }
  7730. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  7731. [ret removeObjectForKey:@"up_params"];
  7732. [ret setObject:@"New Address" forKey:@"title"];
  7733. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  7734. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  7735. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  7736. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  7737. [country_dic removeObjectForKey:@"refresh"];
  7738. [country_dic removeObjectForKey:@"restore"];
  7739. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  7740. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  7741. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  7742. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  7743. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  7744. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  7745. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  7746. [zip_code_dic removeObjectForKey:@"refresh"];
  7747. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  7748. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  7749. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  7750. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  7751. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  7752. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  7753. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  7754. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  7755. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  7756. // country
  7757. NSString *countryCode = @"US";
  7758. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  7759. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  7760. // state
  7761. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  7762. NSDictionary *tmpDic = @{
  7763. @"value" : @"Other",
  7764. @"value_id" : @"",
  7765. @"check" : [NSNumber numberWithInteger:0]
  7766. };
  7767. for (int i = 0; i < allState.allKeys.count; i++) {
  7768. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  7769. NSDictionary *tmp = allState[key];
  7770. [allState setValue:tmpDic forKey:key];
  7771. tmpDic = tmp;
  7772. }
  7773. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  7774. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  7775. [ret setValue:new_section_0 forKey:@"section_0"];
  7776. return [RAUtils dict2data:ret];
  7777. }
  7778. #pragma mark save addr
  7779. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  7780. {
  7781. // NSString *companyName = [self valueInParams:params key:@"company"];
  7782. // NSString *addr1 = [self valueInParams:params key:@"address"];
  7783. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  7784. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  7785. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  7786. // NSString *countryId = [self valueInParams:params key:@"country"];
  7787. // NSString *stateCode = [self valueInParams:params key:@"state"];
  7788. // NSString *city = [self valueInParams:params key:@"city"];
  7789. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  7790. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  7791. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  7792. // NSString *phone = [self valueInParams:params key:@"phone"];
  7793. // NSString *fax = [self valueInParams:params key:@"fax"];
  7794. // NSString *email = [self valueInParams:params key:@"email"];
  7795. return [self offline_saveContact:params update:NO isCustomer:NO];
  7796. }
  7797. #pragma mark cancel order
  7798. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  7799. {
  7800. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7801. NSString *order_id = [self valueInParams:params key:@"orderId"];
  7802. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  7803. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  7804. int ret = [iSalesDB execSql:sql];
  7805. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7806. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7807. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7808. // [dic setValue:@"160409" forKey:@"min_ver"];
  7809. return [RAUtils dict2data:dic];
  7810. }
  7811. #pragma mark sign order
  7812. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  7813. {
  7814. //参考 offline_saveBusinesscard
  7815. DebugLog(@"sign order params: %@",params);
  7816. // orderCode = MOB1608240002;
  7817. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  7818. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7819. orderCode = [self translateSingleQuote:orderCode];
  7820. NSString *picPath = [self valueInParams:params key:@"picpath"];
  7821. picPath = [self translateSingleQuote:picPath];
  7822. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  7823. int ret = [iSalesDB execSql:sql];
  7824. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7825. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7826. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7827. // [dic setValue:@"160409" forKey:@"min_ver"];
  7828. return [RAUtils dict2data:dic];
  7829. }
  7830. #pragma mark save order
  7831. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  7832. NSString *ret = [self valueInParams:params key:key];
  7833. if (translate) {
  7834. ret = [self translateSingleQuote:ret];
  7835. }
  7836. return ret;
  7837. }
  7838. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  7839. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  7840. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  7841. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  7842. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  7843. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  7844. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  7845. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  7846. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  7847. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  7848. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  7849. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  7850. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  7851. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  7852. if (![total_price isEqualToString:@""]) {
  7853. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  7854. } else {
  7855. total_price = @"";
  7856. }
  7857. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  7858. if ([paymentsAndCredits isEqualToString:@""]) {
  7859. paymentsAndCredits = @"";
  7860. } else {
  7861. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  7862. }
  7863. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  7864. if ([handling_fee_value isEqualToString:@""]) {
  7865. handling_fee_value = @"";
  7866. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  7867. handling_fee_value = @"";
  7868. } else {
  7869. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  7870. }
  7871. NSString *handling_fee_placeholder = handling_fee_value;
  7872. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  7873. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  7874. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  7875. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  7876. NSString *lift_gate_placeholder = @"";
  7877. if ([lift_gate_value isEqualToString:@""]) {
  7878. lift_gate_placeholder = @"";
  7879. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  7880. lift_gate_placeholder = @"";
  7881. } else {
  7882. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  7883. }
  7884. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  7885. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  7886. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  7887. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  7888. 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];
  7889. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  7890. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  7891. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  7892. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  7893. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  7894. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  7895. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  7896. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  7897. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  7898. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  7899. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  7900. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  7901. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  7902. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  7903. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  7904. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  7905. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  7906. BOOL number_nil = [credit_card_number isEqualToString:@""];
  7907. if (!number_nil) {
  7908. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  7909. }
  7910. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  7911. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  7912. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  7913. if (!security_code_nil) {
  7914. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  7915. }
  7916. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  7917. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  7918. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  7919. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  7920. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  7921. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  7922. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  7923. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  7924. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  7925. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  7926. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  7927. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  7928. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  7929. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  7930. NSString *contact_id = customer_cid;
  7931. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  7932. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  7933. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  7934. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  7935. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  7936. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  7937. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  7938. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  7939. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  7940. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  7941. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  7942. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  7943. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  7944. //
  7945. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  7946. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  7947. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  7948. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  7949. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  7950. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  7951. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  7952. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  7953. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  7954. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  7955. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  7956. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  7957. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  7958. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  7959. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  7960. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  7961. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  7962. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  7963. 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];
  7964. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  7965. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  7966. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  7967. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  7968. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  7969. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  7970. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  7971. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  7972. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  7973. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  7974. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  7975. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  7976. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  7977. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  7978. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  7979. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  7980. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  7981. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  7982. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  7983. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  7984. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  7985. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  7986. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  7987. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  7988. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  7989. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  7990. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  7991. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  7992. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  7993. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  7994. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  7995. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  7996. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  7997. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  7998. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  7999. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8000. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8001. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8002. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8003. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8004. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8005. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8006. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8007. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8008. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8009. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8010. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8011. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8012. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8013. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8014. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8015. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8016. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8017. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8018. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8019. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8020. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8021. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8022. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8023. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8024. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8025. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8026. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8027. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8028. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8029. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8030. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8031. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8032. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8033. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8034. NSString *order_status = @"status = 1,";
  8035. if (submit) {
  8036. order_status = @"status = -11,";
  8037. }
  8038. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8039. NSString *sync_sql = @"";
  8040. if (submit) {
  8041. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8042. [param setValue:sales_rep forKey:@"sales_rep"];
  8043. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  8044. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8045. }
  8046. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ 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_sql,so_id];
  8047. DebugLog(@"save order contactSql: %@",contactSql);
  8048. DebugLog(@"save order orderSql: %@",orderSql);
  8049. // int contact_ret = [iSalesDB execSql:contactSql];
  8050. int order_ret = [iSalesDB execSql:orderSql];
  8051. int ret = order_ret;
  8052. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8053. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8054. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8055. // [dic setValue:@"160409" forKey:@"min_ver"];
  8056. [dic setObject:so_id forKey:@"so#"];
  8057. return [RAUtils dict2data:dic];
  8058. }
  8059. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8060. {
  8061. // id foo = nil;
  8062. // NSMutableArray *a = @[].mutableCopy;
  8063. // [a addObject:foo];
  8064. return [self saveorder:param submit:NO];
  8065. }
  8066. #pragma mark add to cart by name
  8067. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8068. {
  8069. NSString *orderCode = [params objectForKey:@"orderCode"];
  8070. NSString *product_name = [params objectForKey:@"product_name"];
  8071. product_name = [self translateSingleQuote:product_name];
  8072. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8073. sqlite3 *db = [iSalesDB get_db];
  8074. __block NSMutableString *product_id_string = [NSMutableString string];
  8075. for (int i = 0; i < product_name_array.count; i++) {
  8076. NSString *name = [product_name_array objectAtIndex:i];
  8077. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8078. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8079. int product_id = sqlite3_column_int(stmt, 0);
  8080. if (i == product_name_array.count - 1) {
  8081. [product_id_string appendFormat:@"%d",product_id];
  8082. } else {
  8083. [product_id_string appendFormat:@"%d,",product_id];
  8084. }
  8085. }];
  8086. }
  8087. NSDictionary *newParams = @{
  8088. @"product_id" : product_id_string,
  8089. @"orderCode" : orderCode
  8090. };
  8091. [iSalesDB close_db:db];
  8092. return [self offline_add2cart:[newParams mutableCopy]];
  8093. }
  8094. #pragma mark reset order
  8095. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8096. {
  8097. UIApplication * app = [UIApplication sharedApplication];
  8098. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8099. appDelegate.disable_trigger=true;
  8100. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8101. appDelegate.disable_trigger=false;
  8102. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8103. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8104. return [RAUtils dict2data:dic];
  8105. }
  8106. #pragma mark submit order
  8107. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8108. {
  8109. return [self saveorder:params submit:YES];
  8110. }
  8111. #pragma mark copy order
  8112. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8113. {
  8114. NSMutableDictionary *ret = @{}.mutableCopy;
  8115. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8116. sqlite3 *db = [iSalesDB get_db];
  8117. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8118. // 首先查看联系人是否active
  8119. NSString *customer_sql = [NSString stringWithFormat:@"select is_active from offline_contact where contact_id in (select customer_cid from offline_order where so_id = '%@');",order_code];
  8120. __block int customer_is_active = 1;
  8121. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8122. customer_is_active = sqlite3_column_int(stmt, 0);
  8123. }];
  8124. if (!customer_is_active) {
  8125. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8126. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8127. [iSalesDB close_db:db];
  8128. return [RAUtils dict2data:ret];
  8129. }
  8130. // new order
  8131. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8132. NSString *new_order_code = [self get_offline_soid:db];
  8133. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8134. user = [self translateSingleQuote:user];
  8135. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,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,must_call,poNumber,comments,comments_ext,sales_terms) select '%@',1,submit_as,internal_notes,sales_rep,'%@',logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,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,must_call,poNumber,comments,comments_ext,sales_terms from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  8136. __block int result = 1;
  8137. result = [iSalesDB execSql:insert_order_sql db:db];
  8138. if (!result) {
  8139. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8140. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8141. [iSalesDB close_db:db];
  8142. return [RAUtils dict2data:ret];
  8143. }
  8144. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  8145. // __block NSString *product_id = @"";
  8146. __weak typeof(self) weakSelf = self;
  8147. NSString *product_id_sql = [NSString stringWithFormat:@"select product_id,is_active from product WHERE product_id in (select product_id from offline_cart where so_no = '%@');",order_code];
  8148. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8149. int is_active = sqlite3_column_int(stmt, 1);
  8150. if (is_active) {
  8151. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  8152. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  8153. NSString *insert_cart_sql = [NSString stringWithFormat:@"insert into offline_cart (so_no,orderitem_id,product_id,item_id,item_count,bundle_item,type) select '%@',orderitem_id,product_id,item_id,item_count,bundle_item,type from offline_cart where product_id = %@ and so_no = '%@'",new_order_code,product_id,order_code];
  8154. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  8155. }
  8156. }];
  8157. // product_id = [product_id substringFromIndex:1];
  8158. //
  8159. // [self offline_add2cart:@{}.mutableCopy];
  8160. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8161. [iSalesDB close_db:db];
  8162. if (result) {
  8163. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8164. } else {
  8165. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8166. }
  8167. [ret setObject:new_order_code forKey:@"so_id"];
  8168. return [RAUtils dict2data:ret];
  8169. }
  8170. #pragma mark move wish list to cart
  8171. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  8172. NSString *collectId = params[@"collectId"];
  8173. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  8174. __block NSString *product_id = @"";
  8175. __block NSString *qty = @"";
  8176. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8177. int productId = sqlite3_column_int(stmt, 0);
  8178. int item_qty = sqlite3_column_int(stmt, 1);
  8179. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8180. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8181. }];
  8182. product_id = [product_id substringFromIndex:1];
  8183. qty = [qty substringFromIndex:1];
  8184. NSDictionary *newParams = @{
  8185. @"product_id" : product_id,
  8186. @"orderCode" : params[@"orderCode"],
  8187. @"qty" : qty
  8188. };
  8189. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  8190. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  8191. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  8192. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  8193. //
  8194. // int ret = [iSalesDB execSql:deleteSQL];
  8195. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  8196. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  8197. }
  8198. return [RAUtils dict2data:retDic];
  8199. }
  8200. #pragma mark - portfolio
  8201. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  8202. int sort = [[params valueForKey:@"sort"] integerValue];
  8203. int offset = [[params valueForKey:@"offset"] integerValue];
  8204. int limit = [[params valueForKey:@"limit"] integerValue];
  8205. NSString *orderBy = @"";
  8206. switch (sort) {
  8207. case 0:{
  8208. orderBy = @"modify_time desc";
  8209. }
  8210. break;
  8211. case 1:{
  8212. orderBy = @"modify_time asc";
  8213. }
  8214. break;
  8215. case 2:{
  8216. orderBy = @"name asc";
  8217. }
  8218. break;
  8219. case 3:{
  8220. orderBy = @"name desc";
  8221. }
  8222. break;
  8223. case 4:{
  8224. orderBy = @"description asc";
  8225. }
  8226. break;
  8227. default:
  8228. break;
  8229. }
  8230. NSString *sql = [NSString stringWithFormat:@"select p.product_id,p.name,p.description,ifnull(p.sheet_price,'null'),p.sheet_discount,ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.fashion_id,i.url,p.line_note,p.percent from offline_portfolio as p left join model_image as i on i.product_id = p.product_id where i.type = 1 and (p.is_delete is null or p.is_delete = 0) group by p.product_id order by %@ limit %d offset %d;",orderBy,limit,offset];
  8231. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8232. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8233. sqlite3 *db = [iSalesDB get_db];
  8234. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8235. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8236. int product_id = sqlite3_column_int(stmt, 0);
  8237. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8238. NSString *name = [self textAtColumn:1 statement:stmt];
  8239. NSString *description = [self textAtColumn:2 statement:stmt];
  8240. double price = sqlite3_column_double(stmt, 3);
  8241. double discount = sqlite3_column_double(stmt,4);
  8242. int qty = sqlite3_column_int(stmt, 5);
  8243. int percentage = sqlite3_column_int(stmt, 6);
  8244. int item_id = sqlite3_column_int(stmt, 7);
  8245. // int fashion_id = sqlite3_column_int(stmt, 8);
  8246. NSString *img_path = [self textAtColumn:9 statement:stmt];
  8247. NSString *line_note = [self textAtColumn:10 statement:stmt];
  8248. double percent = sqlite3_column_double(stmt, 11);
  8249. NSString *price_null = [self textAtColumn:3 statement:stmt];
  8250. if ([price_null isEqualToString:@"null"]) {
  8251. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8252. }
  8253. NSMutableDictionary *item = @{
  8254. @"linenotes": line_note,
  8255. @"check": @(1),
  8256. @"product_id": product_id_string,
  8257. @"available_qty": @(qty),
  8258. @"available_percent" : @(percent),
  8259. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  8260. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8261. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  8262. @"img": img_path,
  8263. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  8264. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  8265. }.mutableCopy;
  8266. if (percentage) {
  8267. [item removeObjectForKey:@"available_qty"];
  8268. } else {
  8269. [item removeObjectForKey:@"available_percent"];
  8270. }
  8271. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  8272. if ([qty_null isEqualToString:@"null"]) {
  8273. [item removeObjectForKey:@"available_qty"];
  8274. }
  8275. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8276. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8277. }];
  8278. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8279. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8280. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8281. }
  8282. [iSalesDB close_db:db];
  8283. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  8284. [dic setValue:@"" forKey:@"email_content"];
  8285. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  8286. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8287. return [RAUtils dict2data:dic];
  8288. }
  8289. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  8290. __block int qty = 0;
  8291. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8292. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8293. qty = sqlite3_column_int(stmt, 0);
  8294. }];
  8295. return qty;
  8296. }
  8297. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  8298. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  8299. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  8300. NSMutableDictionary * values = params[@"replaceValue"];
  8301. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  8302. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  8303. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  8304. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  8305. NSString *pdf_path = @"";
  8306. if (direct) {
  8307. configureParams = [self valueInParams:params key:@"pdfUrl"];
  8308. } else {
  8309. pdf_path = [self valueInParams:params key:@"pdfPath"];
  8310. }
  8311. NSString *create_user = [self valueInParams:params key:@"user"];
  8312. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  8313. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  8314. // model info
  8315. NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),ifnull(sheet_discount,'null'),ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  8316. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8317. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  8318. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8319. sqlite3 *db = [iSalesDB get_db];
  8320. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8321. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8322. int product_id = sqlite3_column_int(stmt, 0);
  8323. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8324. double price = sqlite3_column_double(stmt, 1);
  8325. double discount = sqlite3_column_double(stmt,2);
  8326. int qty = sqlite3_column_int(stmt, 3);
  8327. int percentage = sqlite3_column_int(stmt, 4);
  8328. int item_id = sqlite3_column_int(stmt, 5);
  8329. NSString *line_note = [self textAtColumn:6 statement:stmt];
  8330. double percent = sqlite3_column_double(stmt, 7);
  8331. NSString *price_null = [self textAtColumn:1 statement:stmt];
  8332. /* if ([price_null isEqualToString:@"null"]) {
  8333. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8334. }
  8335. */
  8336. [product_ids_string appendFormat:@"%@,",product_id_string];
  8337. // Regular Price
  8338. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  8339. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  8340. // QTY
  8341. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  8342. if ([qty_null isEqualToString:@"null"]) {
  8343. // 查available
  8344. qty = [self model_QTY:product_id_string db:db];
  8345. }
  8346. if (percentage) {
  8347. qty = qty * percent / 100;
  8348. }
  8349. // Special Price
  8350. if ([price_null isEqualToString:@"null"]) {
  8351. // price = regular price
  8352. price = [regular_price_str doubleValue];
  8353. }
  8354. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  8355. if (![discount_null isEqualToString:@"null"]) {
  8356. price = price * (1 - discount / 100.0);
  8357. }
  8358. NSMutableDictionary *item = @{
  8359. @"line_note": line_note,
  8360. @"product_id": product_id_string,
  8361. @"available_qty": @(qty),
  8362. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8363. @"regular_price" : regular_price_str,
  8364. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  8365. }.mutableCopy;
  8366. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8367. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8368. }];
  8369. [iSalesDB close_db:db];
  8370. if (product_ids_string.length > 0) {
  8371. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  8372. }
  8373. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  8374. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  8375. return [RAUtils dict2data:resultDictionary];
  8376. }
  8377. NSString *model_info = [RAUtils dict2string:dic];
  8378. // 创建PDF
  8379. // 在preview情况下保存,则不需要新建了
  8380. if (direct) {
  8381. NSMutableDictionary *tear_sheet_params = params;
  8382. // if (tear_sheet_id) {
  8383. // tear_sheet_params = values;
  8384. // }
  8385. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  8386. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  8387. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  8388. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  8389. resultDictionary = pdfInfo.mutableCopy;
  8390. } else { // 创建PDF失败
  8391. return pdfData;
  8392. }
  8393. } else {
  8394. // pdf_path 就是本地路径
  8395. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  8396. }
  8397. // 将文件移动到PDF Cache文件夹
  8398. NSString *newPath = [pdf_path lastPathComponent];
  8399. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8400. NSString *cachefolder = [paths objectAtIndex:0];
  8401. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8402. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  8403. NSFileManager *fileManager = [NSFileManager defaultManager];
  8404. NSError *error = nil;
  8405. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  8406. if (error) { // 移动文件失败
  8407. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8408. return [RAUtils dict2data:resultDictionary];
  8409. }
  8410. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  8411. pdf_path = [newPath lastPathComponent];
  8412. // 保存信息
  8413. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  8414. NSString *off_params = [RAUtils dict2string:params];
  8415. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  8416. pdf_path = [self translateSingleQuote:pdf_path];
  8417. create_user = [self translateSingleQuote:create_user];
  8418. tear_note = [self translateSingleQuote:tear_note];
  8419. tear_name = [self translateSingleQuote:tear_name];
  8420. model_info = [self translateSingleQuote:model_info];
  8421. configureParams = [self translateSingleQuote:configureParams];
  8422. off_params = [self translateSingleQuote:off_params];
  8423. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (pdf_path,create_user,tear_note,tear_name,model_info,configureParams,is_local,off_params) values ('%@','%@','%@','%@','%@','%@',1,'%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams,off_params];
  8424. if (tear_sheet_id) {
  8425. int _id = [tear_sheet_id integerValue];
  8426. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  8427. }
  8428. int result = [iSalesDB execSql:save_pdf_sql];
  8429. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8430. //
  8431. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  8432. if (remove_Item) {
  8433. // portfolioId
  8434. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  8435. }
  8436. return [RAUtils dict2data:resultDictionary];
  8437. }
  8438. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  8439. return [self offline_savePDF:params direct:YES];
  8440. }
  8441. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  8442. return [self offline_savePDF:params direct:NO];
  8443. }
  8444. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  8445. int offset = [[params valueForKey:@"offset"] integerValue];
  8446. int limit = [[params valueForKey:@"limit"] integerValue];
  8447. NSString *keyword = [params valueForKey:@"keyWord"];
  8448. NSString *where = @"where is_delete is null or is_delete = 0";
  8449. if (keyword.length) {
  8450. keyword = [self translateSingleQuote:keyword];
  8451. where = [NSString stringWithFormat:@"where (is_delete is null or is_delete = 0 ) and lower(create_user) like '%%%@%%' or lower(tear_note) like '%%%@%%' or lower(tear_name) like '%%%@%%'",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString];
  8452. }
  8453. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,_id,model_info,off_params from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];// tearsheets_id ---> _id
  8454. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8455. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8456. NSString *cachefolder = [paths objectAtIndex:0];
  8457. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8458. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8459. NSString *name = [self textAtColumn:0 statement:stmt];
  8460. NSString *note = [self textAtColumn:1 statement:stmt];
  8461. NSString *time = [self textAtColumn:2 statement:stmt];
  8462. NSString *user = [self textAtColumn:3 statement:stmt];
  8463. NSString *path = [self textAtColumn:4 statement:stmt];
  8464. NSString *model_info = [self textAtColumn:6 statement:stmt];
  8465. NSString *off_params = [self textAtColumn:7 statement:stmt];
  8466. path = [pdfFolder stringByAppendingPathComponent:path];
  8467. BOOL bdir=NO;
  8468. NSFileManager* fileManager = [NSFileManager defaultManager];
  8469. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  8470. {
  8471. //pdf文件不存在
  8472. path=nil;
  8473. }
  8474. time = [self changeDateTimeFormate:time];
  8475. time = [time stringByAppendingString:@" PST"];
  8476. int sheet_id = sqlite3_column_int(stmt, 5);
  8477. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  8478. item[@"tearsheetsId"]=@(sheet_id);
  8479. item[@"pdf_path"]=path;
  8480. item[@"create_time"]=time;
  8481. item[@"create_user"]=user;
  8482. item[@"tear_note"]=note;
  8483. item[@"tear_name"]=name;
  8484. item[@"isLocalFile"]=@"true";
  8485. item[@"model_info"]=model_info;
  8486. item[@"off_params"]=off_params;
  8487. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8488. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8489. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8490. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8491. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8492. }];
  8493. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8494. return [RAUtils dict2data:dic];
  8495. }
  8496. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  8497. NSString *prodct_ids = [params objectForKey:@"product_id"];
  8498. // NSString *user = [params objectForKey:@"user"];
  8499. // NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and (select count(0) from offline_portfolio where product_id in (%@) and (is_delete is null or is_delete = 0)) = 0);",prodct_ids,prodct_ids];
  8500. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio));insert into offline_portfolio (product_id,name,description,item_id,is_delete) select * from (select p.product_id,p.name,p.description,p.item_id,0 from product as p left join offline_portfolio as f on p.product_id = f.product_id where p.product_id in (%@) and p.product_id in (select product_id from offline_portfolio) and f.is_delete = 1);delete from offline_portfolio where (select count(0) from offline_portfolio where product_id in (%@)) > 1 and is_delete = 1;",prodct_ids,prodct_ids,prodct_ids];
  8501. int result = [iSalesDB execSql:sql];
  8502. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8503. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  8504. if (result == RESULT_TRUE) {
  8505. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  8506. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  8507. sqlite3 *db = [iSalesDB get_db];
  8508. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  8509. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  8510. [iSalesDB close_db:db];
  8511. }
  8512. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8513. return [RAUtils dict2data:dic];
  8514. }
  8515. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  8516. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  8517. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  8518. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  8519. // Regurlar Price
  8520. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  8521. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  8522. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8523. __block long val_count = 0;
  8524. NSString *sql = @"select name,type,order_by from price order by order_by";
  8525. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8526. NSString *name = [self textAtColumn:0 statement:stmt];
  8527. int type = sqlite3_column_int(stmt, 1);
  8528. int order_by = sqlite3_column_int(stmt, 2);
  8529. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8530. NSDictionary *price_dic = @{
  8531. @"value" : name,
  8532. @"value_id" : [NSNumber numberWithInteger:type],
  8533. @"check" : order_by == 0 ? @(1) : @(0)
  8534. };
  8535. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8536. val_count = ++(*count);
  8537. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8538. val_count = 0;
  8539. }];
  8540. [cadedate setObject:@{@"value" : @"None",
  8541. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8542. val_count++;
  8543. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  8544. [price setObject:cadedate forKey:@"cadedate"];
  8545. [section1 setObject:price forKey:@"item_2"];
  8546. [dic setObject:section1 forKey:@"section_1"];
  8547. return [RAUtils dict2data:dic];
  8548. }
  8549. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  8550. NSString *product_id = [params objectForKey:@"fashionId"];
  8551. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8552. __block int result = RESULT_TRUE;
  8553. __block int qty = 0;
  8554. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8555. qty = sqlite3_column_int(stmt, 0);
  8556. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8557. result = RESULT_FALSE;
  8558. }];
  8559. NSMutableDictionary *dic = @{
  8560. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  8561. @"mode" : @"Regular Mode",
  8562. @"quantity_available" : @(qty),
  8563. @"result" : @(result),
  8564. }.mutableCopy;
  8565. return [RAUtils dict2data:dic];
  8566. }
  8567. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  8568. NSString *item_ids = [params objectForKey:@"item_id"];
  8569. NSString *line_notes = [params objectForKey:@"notes"];
  8570. NSString *price_str = [params objectForKey:@"price"];
  8571. NSString *discount_str = [params objectForKey:@"discount"];
  8572. NSString *percent = [params objectForKey:@"available_percent"];
  8573. NSString *qty = [params objectForKey:@"available_qty"];
  8574. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  8575. int dot = 0;
  8576. if (line_notes) {
  8577. line_notes = [self translateSingleQuote:line_notes];
  8578. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  8579. sql = [sql stringByAppendingString:line_notes];
  8580. dot = 1;
  8581. } else {
  8582. line_notes = @"";
  8583. }
  8584. if (price_str) {
  8585. if (dot) {
  8586. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  8587. } else {
  8588. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  8589. dot = 1;
  8590. }
  8591. sql = [sql stringByAppendingString:price_str];
  8592. } else {
  8593. price_str = @"";
  8594. }
  8595. if (discount_str) {
  8596. if (dot) {
  8597. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  8598. } else {
  8599. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  8600. dot = 1;
  8601. }
  8602. sql = [sql stringByAppendingString:discount_str];
  8603. } else {
  8604. discount_str = @"";
  8605. }
  8606. if (percent) {
  8607. if (dot) {
  8608. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  8609. } else {
  8610. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  8611. dot = 1;
  8612. }
  8613. sql = [sql stringByAppendingString:percent];
  8614. } else {
  8615. percent = @"";
  8616. }
  8617. if (qty) {
  8618. if (dot) {
  8619. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  8620. } else {
  8621. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  8622. dot = 1;
  8623. }
  8624. sql = [sql stringByAppendingString:qty];
  8625. } else {
  8626. qty = @"";
  8627. }
  8628. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  8629. sql = [sql stringByAppendingString:where];
  8630. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8631. int result = [iSalesDB execSql:sql];
  8632. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8633. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8634. return [RAUtils dict2data:dic];
  8635. }
  8636. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  8637. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  8638. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8639. sqlite3 *db = [iSalesDB get_db];
  8640. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set is_delete = 1 where item_id in (%@) and (is_delete is null or is_delete = 0);",portfolio_ids];
  8641. int result = [iSalesDB execSql:sql db:db];
  8642. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8643. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8644. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  8645. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  8646. [iSalesDB close_db:db];
  8647. return [RAUtils dict2data:dic];
  8648. }
  8649. + (void)offline_removePDFWithName:(NSString *)name {
  8650. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8651. NSString *cachefolder = [paths objectAtIndex:0];
  8652. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8653. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  8654. NSFileManager *fileManager = [NSFileManager defaultManager];
  8655. [fileManager removeItemAtPath:path error:nil];
  8656. }
  8657. + (void)offline_clear_PDFCache {
  8658. NSFileManager *fileManager = [NSFileManager defaultManager];
  8659. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8660. NSString *cachefolder = [paths objectAtIndex:0];
  8661. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8662. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  8663. for (NSString *path in pdf_files) {
  8664. [self offline_removePDFWithName:[path lastPathComponent]];
  8665. }
  8666. }
  8667. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  8668. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8669. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  8670. NSString *user = [params objectForKey:@"user"];
  8671. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  8672. if (![create_user isEqualToString:user]) {
  8673. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8674. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8675. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  8676. return [RAUtils dict2data:dic];
  8677. }
  8678. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8679. __block int is_local = 0;
  8680. __block NSString *path = @"";
  8681. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8682. is_local = sqlite3_column_int(stmt, 0);
  8683. path = [self textAtColumn:1 statement:stmt];
  8684. }];
  8685. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8686. if (is_local == 1) {
  8687. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8688. // 删除文件
  8689. [self offline_removePDFWithName:path];
  8690. }
  8691. int result = [iSalesDB execSql:sql];
  8692. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8693. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8694. return [RAUtils dict2data:dic];
  8695. }
  8696. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  8697. {
  8698. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8699. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8700. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8701. NSString* where=@"1=1";
  8702. if (ver!=nil) {
  8703. where=@"is_dirty=1";
  8704. }
  8705. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,name,description,item_id,fashion_id,ifnull(available_qty,'null'),ifnull(percentage,'null'),ifnull(percent,'null'),ifnull(sheet_price,'null'),sheet_discount,img,line_note,is_delete,createtime,modify_time from offline_portfolio where %@",where];
  8706. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8707. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8708. sqlite3 *db = [iSalesDB get_db];
  8709. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8710. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8711. NSInteger _id = sqlite3_column_int(stmt, 0);
  8712. NSInteger product_id = sqlite3_column_int(stmt, 1);
  8713. NSString *name = [self textAtColumn:2 statement:stmt];
  8714. NSString *desc = [self textAtColumn:3 statement:stmt];
  8715. NSInteger item_id = sqlite3_column_int(stmt, 4);
  8716. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  8717. NSInteger qty = sqlite3_column_int(stmt, 6);
  8718. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  8719. double percent = sqlite3_column_double(stmt, 8);
  8720. double price = sqlite3_column_double(stmt, 9);
  8721. double discount = sqlite3_column_double(stmt, 10);
  8722. NSString *img = [self textAtColumn:11 statement:stmt];
  8723. NSString *line_note = [self textAtColumn:12 statement:stmt];
  8724. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  8725. NSString *create_time = [self textAtColumn:14 statement:stmt];
  8726. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  8727. NSString *price_null = [self textAtColumn:9 statement:stmt];
  8728. if ([price_null isEqualToString:@"null"]) {
  8729. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8730. }
  8731. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  8732. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  8733. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  8734. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  8735. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  8736. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  8737. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  8738. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  8739. if ([qty_null isEqualToString:@"null"]) {
  8740. // [item setValue:@"null" forKey:@"available_qty"];
  8741. } else {
  8742. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  8743. }
  8744. if ([is_percent_null isEqualToString:@"null"]) {
  8745. // [item setValue:@"null" forKey:@"percentage"];
  8746. } else {
  8747. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  8748. }
  8749. if ([percent_null isEqualToString:@"null"]) {
  8750. // [item setValue:@"null" forKey:@"percent"];
  8751. } else {
  8752. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  8753. }
  8754. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  8755. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  8756. [item setValue:name forKey:@"name"];
  8757. [item setValue:desc forKey:@"description"];
  8758. [item setValue:img forKey:@"img"];
  8759. [item setValue:line_note forKey:@"line_note"];
  8760. [item setValue:create_time forKey:@"createtime"];
  8761. [item setValue:modify_time forKey:@"modifytime"];
  8762. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8763. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8764. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  8765. }];
  8766. [iSalesDB close_db:db];
  8767. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8768. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8769. }
  8770. return ret;
  8771. }
  8772. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  8773. {
  8774. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8775. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8776. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8777. NSString* where=@"1=1";
  8778. if (ver!=nil) {
  8779. where=@"is_dirty=1";
  8780. }
  8781. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,ifnull(tearsheets_id,'null'),pdf_path,create_user,tear_note,tear_name,model_info,createtime,modify_time,configureParams,off_params,is_delete,is_local from offline_pdf where %@",where];
  8782. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8783. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8784. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8785. NSInteger _id = sqlite3_column_int(stmt, 0);
  8786. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  8787. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  8788. NSString *create_user = [self textAtColumn:3 statement:stmt];
  8789. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  8790. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  8791. NSString *model_info = [self textAtColumn:6 statement:stmt];
  8792. NSString *createtime = [self textAtColumn:7 statement:stmt];
  8793. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  8794. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  8795. NSString *off_params = [self textAtColumn:10 statement:stmt];
  8796. NSString *uuid = [NSUUID UUID].UUIDString;
  8797. int is_delete = sqlite3_column_int(stmt, 11);
  8798. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  8799. int is_local = sqlite3_column_int(stmt, 12);
  8800. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  8801. if (![tearsheet_id_null isEqualToString:@"null"]) {
  8802. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  8803. }
  8804. [item setObject:pdf_path forKey:@"pdf_path"];
  8805. [item setObject:create_user forKey:@"create_user"];
  8806. [item setObject:tear_note forKey:@"tear_note"];
  8807. [item setObject:tear_name forKey:@"tear_name"];
  8808. [item setObject:model_info forKey:@"model_info"];
  8809. [item setObject:createtime forKey:@"createtime"];
  8810. [item setObject:modifytime forKey:@"modifytime"];
  8811. [item setObject:urlParams forKey:@"urlParams"];
  8812. [item setObject:off_params forKey:@"off_params"];
  8813. [item setObject:uuid forKey:@"pdf_token"];
  8814. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  8815. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  8816. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  8817. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  8818. } else {
  8819. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8820. }
  8821. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8822. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  8823. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8824. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8825. }];
  8826. return ret;
  8827. }
  8828. @end